M2
M2 copied to clipboard
Making sets Sets
Currently, the set method is only defined on VisibleLists. Trying to turn a Set into a Set gives an error:
i1 : methods set
o1 = {0 => (set, VisibleList)}
o1 : NumberedVerticalList
i2 : set set{1,2}
stdio:2:1:(3): error: expected a visible list
It seems sensible for set to act as the identity on a Set (which is how e.g. sequence and toList behave). Can this be done at top-level?
i3 : set Set := Set => identity
o3 = Set => identity
o3 : Option
i4 : methods set
o4 = {0 => (set, Set) }
{1 => (set, VisibleList)}
o4 : NumberedVerticalList
i5 : set set{1,2}
stdio:5:1:(3): error: expected a visible list
reminds me of https://github.com/Macaulay2/M2/issues/1499 (if we made ideal idempotent, we could make leadTerm be more sensible while preserving existing code)
This is a good idea. Other things might come along later that can be converted to sets, too, so it would be good to change it from a compiled function to a method function. I did that recently for toList, this way:
diff --git a/M2/Macaulay2/d/actors2.dd b/M2/Macaulay2/d/actors2.dd
index 761c7fe98..6215a3a34 100644
--- a/M2/Macaulay2/d/actors2.dd
+++ b/M2/Macaulay2/d/actors2.dd
@@ -64,7 +64,7 @@ toList(e:Expr):Expr := (
false)))
is s:stringCell do list(strtoseq(s))
else WrongArg("list, sequence, set, or string"));
-setupfun("toList",toList);
+setupfun("toList1",toList);
values(e:Expr):Expr := (
when e
is oc:DictionaryClosure do (
diff --git a/M2/Macaulay2/m2/methods.m2 b/M2/Macaulay2/m2/methods.m2
index d578af659..6902dc846 100644
--- a/M2/Macaulay2/m2/methods.m2
+++ b/M2/Macaulay2/m2/methods.m2
@@ -419,6 +419,12 @@ depth String := s -> 0
-----------------------------------------------------------------------------
+toList = method(Dispatch => Thing)
+toList List := List => x -> x
+toList BasicList := toList Set := toList String := List => toList1
+
+-----------------------------------------------------------------------------
+
oldflatten := flatten
erase symbol flatten
flatten = method(Dispatch => Thing)
diff --git a/M2/Macaulay2/m2/typicalvalues.m2 b/M2/Macaulay2/m2/typicalvalues.m2
index 1edd2a2e3..d89910df2 100644
--- a/M2/Macaulay2/m2/typicalvalues.m2
+++ b/M2/Macaulay2/m2/typicalvalues.m2
@@ -107,7 +107,6 @@ substring(String,ZZ,ZZ) := String => substring
substring(ZZ,String) := String => substring
substring(Sequence,String) := String => substring
substring(ZZ,ZZ,String) := String => substring
-toList Set := toList BasicList := toList String := List => toList
toSequence BasicList := toSequence String := Sequence => toSequence
ascii String := List => ascii
ascii List := String => ascii