M2
M2 copied to clipboard
memoize and options
Should memoize be sensitive to options?
I can see arguments both ways:
- sometimes the options contain important parameters, like coefficient ring or variable name (e.g. see https://github.com/Macaulay2/M2/pull/3481#discussion_r1768381949);
- on the other hand, sometimes options are just verbosity level or strategy, and if the code has already run maybe we don't care about that.
I don't think using memoize (as opposed to other means of caching) should be recommended anyway, so maybe this is a moot point.
Note that it's not so easy to use memoize with say a method with options, as I think I pointed out in some other thread.
e.g., givin
f= method(Options=>new OptionTable from{a=>1})
neither
f ZZ := o -> memoize(x -> (print (x,o#a); x+o#a))
nor
f ZZ := memoize(o -> x -> (print (x,o#a); x+o#a))
will work, as can be checked by applying repeatedly. this does work:
f ZZ := memoize(o -> memoize(x -> (print (x,o#a); x+o#a)))
but seems heavy.
If one wants to ignore the option for memoize purposes, I don't even know any simple option.
Related problem for memoizing methods:
i1 : f = method();
i2 : f ZZ := x -> 2*x;
i3 : f = memoize f;
i4 : f QQ := x -> 3*x;
i5 : f(1_ZZ)
o5 = 2
i6 : f(1_QQ)
stdio:6:1:(3): error: no method found for applying MethodFunction[] to:
argument : 1 (of class QQ)