Source code of a function

#1

Hello,

Is it possible to recover the source code of a function as a string?
For example,

f(x,y) = 2sin(x+y)+1

Is there a function call on f that can get me s="2sin(x+y)+1"?

Thanks!

2 Likes

#2

If you do

@which f(x,y)

it will print the file and line number where the function was defined (or it will tell you it was done in the REPL). Try, for example @which sin(Ď€).

0 Likes

#3

In the REPL you could also try

@less f(x,y)

which wraps the call to the pager on the result from @which

0 Likes

#4

For reference, the source expression is available using the following:

Base.uncompressed_ast(methods(f).ms[1]).code

But right now it is necessary to go to the original code to get the unparsed version, as suggested above. It’s on the roadmap for 1.0; see this issue and others cross-referenced there.

1 Like

#5

Take a look at Sugar.jl, specifically to its macro_form() function. It first tries to find and parse function’s source, and only if it’s unavailable, falls back to parsing AST. macro_form often fails for functions generated using @eval (since source code includes interpolated values from the context) and may produce somewhat different results for functions defined in REPL, but in general I found this function extremely helpful.

4 Likes

#6

Thanks! That’s exactly what I was looking for.

In particular, these lines did it:

using Sugar
method = Sugar.get_method(f, types)
code, str = Sugar.get_source(method)
2 Likes

#7

It took me some time to realize that types is supposed to be a tuple.
So, if you want to get the source of a method with the type Vector{Float64} you need to write :
method = Sugar.get_method(f, (Vector{Float64),))

0 Likes

#8

well, the Gallium debugger seems to be capable of showing the source, either in a file or if entered from the REPL. how does it do it?

and, more importantly, in 1.0, what is the syntax that I should use to view the method source for function f?

regards,

/iaw

0 Likes

#9

The rcnlee procedure with Sugar.jl works just fine…

1 Like

#10

IIRC Gallium reparses the source.

0 Likes

#11

I’m using Julia 1.0.2, it looks like I can not install Sugar package now.

|__/ |

julia> using Sugar
ERROR: ArgumentError: Package Sugar not found in current path:

  • Run import Pkg; Pkg.add("Sugar") to install the Sugar package.

Stacktrace:
[1] require(::Module, ::Symbol) at ./loading.jl:823

julia> import Pkg

julia> Pkg.add(“Sugar”)
Updating registry at ~/.julia/registries/General
Updating git-repo https://github.com/JuliaRegistries/General.git
Resolving package versions…
ERROR: Unsatisfiable requirements detected for package Matcha [8d673c98]:
Matcha [8d673c98] log:
├─possible versions are: [0.0.1-0.0.2, 0.1.0-0.1.1] or uninstalled
├─restricted by compatibility requirements with Sugar [e18849f4] to versions: [0.0.1-0.0.2, 0.1.0-0.1.1]
│ └─Sugar [e18849f4] log:
│ ├─possible versions are: [0.0.1-0.0.3, 0.1.0, 0.2.0, 0.3.0-0.3.1, 0.4.0-0.4.5] or uninstalled
│ └─restricted to versions * by an explicit requirement, leaving only versions [0.0.1-0.0.3, 0.1.0, 0.2.0, 0.3.0-0.3.1, 0.4.0-0.4.5]
└─restricted by julia compatibility requirements to versions: uninstalled — no versions left

0 Likes

#12

Take a look at CodeTracking.jl.

2 Likes