How to get all definitions associated with other characters?

How to get all the definitions for the symbols associated with other characters, using TagSet, TagSetDelayed, UpSetor UpSetDelayed?

For example, if you defined

area[square] ^= s^2
area[cube] ^= 6*s^2

how to obtain these definitions, not knowing the names square, cubebut knowing only the name area?


I just found that it UpValuesdoes not return definitions for MakeBoxesand Nsince they are stored in FormatValuesand NValuesrespectively:

In[1]:= rotate /: MakeBoxes[expr_rotate, "StandardForm"] := x
UpValues[rotate]
FormatValues[rotate]

Out[2]= {}

Out[3]= {HoldPattern[MakeBoxes[expr_rotate, "StandardForm"]] :> x}

In[4]:= pi /: N[pi] = 3.14
UpValues[pi]
NValues[pi]

Out[4]= 3.14

Out[5]= {}

Out[6]= {HoldPattern[N[pi, {MachinePrecision, MachinePrecision}]] :> 
  3.14}

Therefore, instead UpValueswe should use a combination of UpValues, FormatValuesand NValues.


FormatValues MakeBoxes, FormatValues MakeBoxes MakeBoxes on FrontEnd. FormatType OutputForm .

In[1]:= SetOptions[$Output,FormatType->OutputForm];
FormatValues[DialogNotebook]
Out[2]= {HoldPattern[MakeBoxes[BoxForm`apat$:HoldPattern[DialogNotebook[___]], BoxForm`fpat$_]] :> 

   BoxForm`BoxFormAutoLoad[MakeBoxes, BoxForm`apat$, BoxForm`fpat$, Typeset`CellNotebook`, 

    {{CellGroup, _}, {DocumentNotebook, _}, {PaletteNotebook, _}, {DialogNotebook, _}, {ExpressionCell, _}, {Text, _}, 

     {TextCell, _}, {Cell, HoldPattern[MakeExpression[_Cell, _]]}, {Notebook, HoldPattern[MakeExpression[_Notebook, _]]}}]}

In[1]:= ToString@FormatValues[DialogNotebook]
Out[1]= {HoldPattern[MakeBoxes[BoxForm`apat$:HoldPattern[DialogNotebook[___]], BoxForm`fpat$_]] :> BoxForm`BoxFormAutoLoad[MakeBoxes, BoxForm`apat$, BoxForm`fpat$, Typeset`CellNotebook`, {{CellGroup, _}, {DocumentNotebook, _}, {PaletteNotebook, _}, {DialogNotebook, _}, {ExpressionCell, _}, {Text, _}, {TextCell, _}, {Cell, HoldPattern[MakeExpression[_Cell, _]]}, {Notebook, HoldPattern[MakeExpression[_Notebook, _]]}}]}
+5
3

, :

Cases[
   UpValues @@@ MakeExpression /@ Names["Global`*"],
   HoldPattern[_@_area :> _],
   {2}
]

, :

SetAttributes[otherValues, HoldFirst]

otherValues[sym_] :=
  With[{names = MakeExpression /@ Names["Global`*"]},
    Join[
      Cases[UpValues @@@ names, HoldPattern[_@_sym :> _], {2}],
      Cases[NValues @@@ names, HoldPattern[_@N[sym, ___] :> _], {2}],
      Select[Join @@ FormatValues @@@ names, ! FreeQ[#, HoldPattern@sym] &]
    ]
  ]
+4

Select[UpValues /@ Cases[ToExpression[Names["*"]], _Symbol], ! FreeQ[#, area] &]

{{HoldPattern[area[cube]] :> 6 s^2}, {HoldPattern[area[square]] :> s^2}}
+3

Cases[
  Flatten@Map[
    ToExpression[#, InputForm, Function[sym, UpValues[sym], HoldAllComplete]] &,
    Names["Global`*"]],
  Verbatim[RuleDelayed][Verbatim[HoldPattern][_area], _]
]

. @Mr. , ToExpression MakeExpression, FrontEnd ( ), ( , MakeExpression).

Mathematica , TagSet, TagSetDelayed, UpSet UpSetDelayed, . UpSet:

Unprotect[UpSet];
Module[{tried, upsetHash},
  upsetHash[_] = {};
  getUpsetHash[] := upsetHash;
  UpSet[f_[args___], rhs_] :=
    Block[{tried = True},
       AppendTo[upsetHash[f], 
          Select[HoldComplete[args], 
            Function[Null, Head[Unevaluated[#]] === Symbol, HoldAll]]];
       UpSet[f[args], rhs]] /; ! TrueQ[tried]
];
Protect[UpSet];

All appointments made using UpSetafter this redefinition will be recorded. For example, after executing your example above, you can call

In[6]:= getUpsetHash[][area]

Out[6]= {HoldComplete[square], HoldComplete[cube]} 

Thus, you get your information much faster, especially if you want to make such requests often and / or you have many downloaded packages. You can also automate the process to go to the standard definitions of appointments after loading the function of interest.

+3
source

All Articles