M2 icon indicating copy to clipboard operation
M2 copied to clipboard

make "code" work on more types of hooks

Open DanGrayson opened this issue 5 years ago • 4 comments

This should work:


i1 : addHook(ZZ,foo,cosh)

o1 = cosh

o1 : CompiledFunction

i2 : addHook(ZZ,foo,x->x^3)

o2 = -*Function[stdio:2:17-2:21]*-

o2 : FunctionClosure

i3 : hooks (ZZ,foo)

o3 = {0 => (foo, Strategy => 0)}
     {1 => (foo, Strategy => 1)}

o3 : NumberedVerticalList

i4 : code oo

o4 = -- no method function found: foo()
     ---------------------------------
     -- no method function found: foo()

Originally posted by @DanGrayson in https://github.com/Macaulay2/M2/pull/1385#discussion_r531605063

DanGrayson avatar Nov 27 '20 22:11 DanGrayson

I don't think it should work, here is why, quoted from that thread:

Notice that in hooks(ZZ, foo), the entries "(foo, Strategy => i)" don't include any reference to ZZ. This is because you intentionally wanted the hook to be stored in ZZ#foo rather than ZZ.Hooks#foo. If you insist on doing this, you can access the function using ZZ#foo. Otherwise, you should use the other way of installing a hook:

i1 : addHook((foo, ZZ),cosh)

o1 = cosh

o1 : CompiledFunction

i2 : addHook((foo, ZZ),x->x^3)

o2 = -*Function[stdio:2:20-2:24]*-

o2 : FunctionClosure

i3 : hooks (foo, ZZ)

o3 = {0 => (foo, ZZ, Strategy => 0)}
     {1 => (foo, ZZ, Strategy => 1)}

o3 : NumberedVerticalList

i4 : code oo

o4 = -- code for method: foo(ZZ)
     function cosh: source code not available
     ---------------------------------
     -- code for method: foo(ZZ)

This is a good reason to deprecate the addHook(MutableHashTable, Thing, Function) syntax for good, since it's an anti-pattern.

However, for now, this syntax is almost never used anywhere, so I don't see an urgency for either deprecating it or making hooks work the way you imagine.

mahrud avatar Nov 27 '20 22:11 mahrud

It should work. A good way for it to work would be for "hooks" a list of pairs, where the first entry is the storage location (a mutable hash table), and the second entry is the key under which the hooks are stored.

DanGrayson avatar Nov 30 '20 15:11 DanGrayson

This is a good reason to deprecate the addHook(MutableHashTable, Thing, Function) syntax for good, since it's an anti-pattern.

That depends on what you think the pattern is. I think that is the pattern.

DanGrayson avatar Nov 30 '20 15:11 DanGrayson

A related improvement is to make hooks(intersect, Ideal) list (intersect, Ideal, Ideal, Strategy => ...). Currently it doesn't, because there is no method (intersect, Ideal, Ideal), as it gets included with (intersect, List) or (intersect, Sequence). Should we add that method, even though it is never used, or change the hook key to (intersect, Ideal), or change hooks to also list that hook? I'm not sure.

mahrud avatar Dec 01 '20 20:12 mahrud