2
$\begingroup$

I usually use WReach's define tool to define new functions.

For example, running

define[f[x_]:=x]
FullDefinition[f]

we got a bunch of definitions.

f[x_]:=x
 
def$:f[___]:=(Message[define::badargs,f,def$];Abort[])


 
Attributes[def$]={Temporary}
 
Attributes[define]={HoldAll}
 
define[body_,attribute_Symbol]:=define[body,{attribute}]
 
define[body:_Set|_SetDelayed,attributes_List:{}]:=define[CompoundExpression[body],attributes]
 
define[body:CompoundExpression[(Set|SetDelayed)[name_Symbol[___],_]..],attributes_List:{}]:=(ClearAll[name];SetAttributes[name,DeleteCases[attributes,Open]];If[!MemberQ[attributes,Open],def:name[___]:=(Message[define::badargs,name,def];Abort[])];body;)
 
def:define[___]:=(Message[define::malformed,def];Abort[])


 
define/:define::badargs=There is no definition for '``' applicable to ``.
 
define/:define::malformed=Malformed definition: ``

Now I define another function

define[g[x_]:=x]

We know function g is actually equivalent to f except with a different name. Is there a way to have a function

compareFullDefinition[f,g]

that check this actual equivalence and will give True for this case?

$\endgroup$
5
  • $\begingroup$ Can you explain the context in which you need this? I’m failing to imagine a use case so I have a hard time thinking of a useful approach. $\endgroup$
    – MarcoB
    Feb 8, 2021 at 5:35
  • $\begingroup$ @MarcoB I have an evaluation process depends on a function which is slow, but it can be ran over and over to get finer result based on previous result. In each cycle, I dump the result as well as the function which I used to do the evaluation. In the next run, I want to first compare the dumped function definition to the one I passed, so that I won't mess thing up if I mistakenly pass a different function from the previous one. $\endgroup$
    – matheorem
    Feb 8, 2021 at 6:10
  • $\begingroup$ Might you be able to use hashing in any way? $\endgroup$ Feb 8, 2021 at 12:28
  • $\begingroup$ @J.M. any explanation? :) $\endgroup$
    – matheorem
    Feb 8, 2021 at 12:52
  • $\begingroup$ I was speculating whether someone might think of hashing the e.g. DownValues[] somehow, after making some obvious function name replacements. $\endgroup$ Feb 9, 2021 at 12:25

1 Answer 1

2
$\begingroup$

We get Information for both functions and change the information into a list. Now we replace the the name of the first function by the name of the second function. Unfortunately the names appear not only as symbols, but also in strings. Therefore we need to throw out these strings. After having prepared the lists, we can finally compare them:

CompareFullDefinition[f_, g_] := Module[{t1, t2},
  t1 = Normal[Information[f][[1]]] /. {f -> g, 
     HoldPattern[Rule["Usage", _]] -> Nothing, 
     HoldPattern["FullName" -> _] -> Nothing};
  t2 = Normal[Information[g][[1]]] /. {f -> g, 
     HoldPattern[Rule["Usage", _]] -> Nothing, 
     HoldPattern["FullName" -> _] -> Nothing};
  t1 === t2
  ]

CompareFullDefinition[f, g]
(*True*)
$\endgroup$
4
  • $\begingroup$ Hi, Daniel Huber. It seems does not work. No matter what the definition of g is, it all gives True. $\endgroup$
    – matheorem
    Feb 8, 2021 at 11:59
  • 1
    $\begingroup$ FullDefinition[f] // FullForm just gives FullDefinition[f]. It doesn't actually evaluate to anything; all it does it give the FE the task to print the definitions. $\endgroup$ Feb 8, 2021 at 12:12
  • 1
    $\begingroup$ That was bit trickier than I thought. As said by Sjoerd, FullDefinition does actually not evaluate until printing. That is why we always get True. We need to use Information plus some fiddling to get the right answer. I corrected my answer. Hope I did not get tricked again. $\endgroup$ Feb 8, 2021 at 13:41
  • $\begingroup$ Great job! It seems working at far as I can see. Information did not come to my mind back then :) $\endgroup$
    – matheorem
    Feb 10, 2021 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.