-
Notifications
You must be signed in to change notification settings - Fork 253
fix set Set, add toList Tally #3770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Conversation
M2/Macaulay2/d/sets.dd
Outdated
@@ -19,6 +19,7 @@ makeSet(e:Expr):Expr := ( | |||
is v:Sequence do makeSet(v) | |||
is w:List do if ancestor(w.Class,visibleListClass) then makeSet(w.v) | |||
else WrongArg("a visible list") | |||
is h:HashTable do makeSet(keys(h)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a typical value comment so methods set
also lists (set, HashTable)
? Also this should be documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, actually, this is worse than set Set := identity
because it allocates new memory, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I'll rewrite it more cleanly, more like top-level applyValues(h,x->1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, an easier solution might be to make it so people can install methods on set!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I don't know about that. we've been doing that a lot (superseding the d function, e.g., toList
has suffered this fate) but I don't think it's great, it's not very clean. I'd rather basic functions like set
be at d level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's not clean is having a d function, then renaming it at the m2 level, then introducing a new m2 function which shares the same name as the original d function but which potentially behaves differently. it's not good practice and can make debugging confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another common approach is to give the d function a slightly different name, often appending "0" to the name. So set
could be the method function, and some of the installed methods could be the compiled function set0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose you could declare methods in the interpreter and directly install new method functions on them in the top level. Would this be confusing? I agree that renaming the function and assigning a method is kind of hacky (and the set0
trick is not much better), but the solution to that is to make it possible to declare methods in the interpreter, not prevent set
from becoming a method in the top level!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suppose you could declare methods in the interpreter and directly install new method functions on them in the top level.
yeah that'd be nice... but unless I'm mistaken this isn't currently possible. I was trying to keep this PR to a minimum.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's not possible. My point was that what you called unclean is a kludge until we have this feature, not that you should implement it now :)
Anyway, I think I'd like set to become a method, either in this PR or another time.
@@ -15,7 +15,7 @@ tally String := | |||
tally VisibleList := Tally => tally | |||
|
|||
elements = method() | |||
elements Tally := x -> splice apply(pairs x, (k,v) -> v:k) | |||
elements Tally := toList Tally := x -> splice apply(pairs x, (k,v) -> v:k) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you document (toList, Tally)
(perhaps just adding it to the documentation of (elements, Tally)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
decided to add it to toList
... but why does the doc say that the input can be an integer??
A, a set, a string, a basic list, *or an integer*, or an instance of a class with the iterator method installed
I think
|
M2/Macaulay2/m2/expressions.m2
Outdated
@@ -1240,7 +1240,7 @@ texMath MapExpression := x -> texMath x#0 | "\\," | (if #x>2 then "\\xleftarrow{ | |||
expressionValue MapExpression := x -> map toSequence apply(x,expressionValue) | |||
|
|||
-- moved from set.m2 because of loadsequence order | |||
expression Set := x -> Adjacent {set, expression keys x} | |||
expression Set := x -> Adjacent {set, expression sortByName keys x} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want to sort for two reasons:
- it's slow, for no reason other than aesthetics.
- it can be misleading if the order of
keys S
andnet S
are different.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then why do we do it for HashTable
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably shouldn't, it's misleading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so should I just revert the sorting for Set
, or also remove the sorting for HashTable
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just remove it here for now, but separately we should discuss whether both should be sorted or unsorted.
I don't know too much about the background on this specific topic but I agree that it shouldn't matter if hashTable keys are sorted or not. (At least all checks seem to have passed so it is probably OK.) However, a situation where this might be used (and which might cause bugs that are difficult to detect) is the case of a hash table with integer keys. One would need to look into that further and some methods may implicitly assume the keys to be sorted. A related topic concerns the "spots" function (which I learned from Dan Grayson's ChainComplex code many years ago) and which I think should be part of the core. But this "spots" function and its need has caused some controversy. The aim of the sports function is to output the sorted integer keys (if any) of a hashTable. |
M2/submodules/googletest
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Submodules were inadvertently changed.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how can I revert the submodule change? this is driving me crazy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following should get you back to where it's supposed to be (possibly replacing origin
with whatever remote you use for the main M2 repo):
git checkout origin/development -- M2/submodules/googletest
Rereading the changes in this PR, was
that would be pretty useful |
I would be worried about people using |
this is another (minor) fix of #3574: the definition of
set Set
didn't work sinceset
is defined at d level.Instead we let
set
be equivalent toset @@ keys
on hash tables. We also add a missingtoList Tally
.