What is the best way to read the code of an already defined function (especially from the context of the system)?

Sometimes we like to watch how certain System` functions System` defined (when they are written in Mathematica). This question is about the best way to do this.

Points to keep in mind:

  • First you need to remove couse ReadProtected .

  • Embedded ones should usually be used at least once before they are loaded into the kernel. Usually a simple call is enough for this when they have advanced functionality (for example, through options)?

  • Information ( ?? ) gives a definition in a hard-to-read format (without indentation and all private personal names). What is the best way to get rid of context names and get formatted code?

    One idea to get rid of certain contexts is Block[{$ContextPath = Append[$ContextPath, "SomeContext`Private`"], Information[symbol]] . Code can be formatted using the Workbench tool. Some problems remain, for example. Information does not quote strings, preventing the code from being copied to the Workbench.

In general, I'm interested in how people do this, what methods they use to make the code of built-in functions as simple as possible.

Use case: for example, I recently dug up RunThrough code when I found out that it simply does not work in Windows XP (it turns out that it cannot specify the names of temporary files when the path to them contains spaces).


Update: It appears that a function was used to print definitions without adding context, Developer`ContextFreeForm , but it no longer works in newer versions.

+6
source share
2 answers

As for beautiful printing: the following code is very sketchy, which is based on @ Mr.Wizard's answer, to show that a few simple rules can significantly help improve code readability:

 Internal`InheritedBlock[{RunThrough}, Unprotect[RunThrough]; ClearAttributes[RunThrough, ReadProtected]; Block[{$ContextPath = Append[$ContextPath, "System`Dump`"]}, With[{boxes = ToBoxes@ DownValues[RunThrough]}, CellPrint[Cell[BoxData[#], "Input"]] &[ boxes /. f_[left___, "\[RuleDelayed]", right___] :> f[left, "\[RuleDelayed]", "\n", right] //. { RowBox[{left___, ";", next : Except["\n"], right___}] :> RowBox[{left, ";", "\n", "\t", next, right}], RowBox[{sc : ("Block" | "Module" | "With"), "[", RowBox[{vars_, ",", body_}], "]"}] :> RowBox[{sc, "[", RowBox[{vars, ",", "\n\t", body}], "]"}] }]]]] 

This is certainly not a general solution (in particular, it will not work on a deeply nested functional code without many separate operators), but I am sure that it can be improved and generalized without any problems to cover many cases of interest.

+7
source

Good question, because I don’t think I saw it again.

I am essentially the same as you have outlined. You can get a slightly different fingerprint with Definition and more details with FullDefinition :

 Unprotect[RunThrough]; ClearAttributes[RunThrough, ReadProtected] Block[{$ContextPath = Append[$ContextPath, "System`Dump`"]}, Print @ FullDefinition @ RunThrough ] 
+5
source

Source: https://habr.com/ru/post/904332/


All Articles