My question is very similar to https://stackoverflow.com/questions/5840313/how-to-get-all-definitions-associated-with-other-symbols except I am looking to find all definitions in which a symbol appears.
I.e. I want an ever more full definition than FullDefinition.
Taking their example:
g[x_] := x^3;
f[x_] := g[x^2];
I want something to return the definition of f[x_] when asking for the fullestDefinition[g]. (Also perhaps a better example would be if f was replaced by CenterDot since I also want to consider built-in functions.)
My attempt, which actually works better now than when I started asking this question, but errors are popping up when I start checking the RHS of rules ;) :
SetAttributes[otherValues,HoldFirst]
otherValues[sym_]:=With[{names=MakeExpression/@Names["*"] },
Join[
Cases[DownValues@@@names,HoldPattern[RuleDelayed[in_,out_]/;Not[FreeQ[in,sym]]],{2}],
Cases[SubValues@@@names,HoldPattern[RuleDelayed[in_,out_]/;Not[FreeQ[in,sym]]],{2}],
Cases[UpValues@@@names,HoldPattern[RuleDelayed[in_,out_]/;Not[FreeQ[in,sym]]],{2}],
Cases[NValues@@@names,HoldPattern[RuleDelayed[in_,out_]/;Not[FreeQ[in,sym]]],{2}],
Select[Join@@FormatValues@@@names,!FreeQ[#,HoldPattern@sym]&]]]
WReach suggested
Needs["GeneralUtilities`"];
DefinitionCases["Global`", g]
But in the example:
g[x_] := x^3;
CenterDot[x_] := g[x^2];
DefinitionCases["Global`", g]
This does not return the CenterDot definition containing g. I also tried DefinitionCases["*", g] but it returned an empty list.
However, we can search in System`:
DefinitionCases["System`", g]
which returns what we want (but also returns historic In and Output that contains the symbol it would be nice if we could exclude this.).
Needs["GeneralUtilities`"]followed byDefinitionCases["Global`", g]. Does this suit your purpose? – WReach Mar 05 '24 at 17:28fwithCenterDotin the example above that definition is not returned. I tried changing the context to"*"since that works in Names but then it returns only an empty list. – Kvothe Mar 05 '24 at 18:13