How can I access the abstract syntax tree for a generic function in Julia?
To recap: It looks like Simon was looking for the AST for a specific method associated with a generic function. We can get a LambdaStaticData object, which contains the AST, for a specific method as follows:
julia> f(x,y)=x+y
julia> f0 = methods(f, (Any, Any))[1]
((Any,Any),(),AST(:($(expr(:lambda, {x, y}, {{}, {{x, Any, 0}, {y, Any, 0}}, {}}, quote # none, line 1:
return +(x,y)
end)))),())
julia> f0[3]
AST(:($(expr(:lambda, {x, y}, {{}, {{x, Any, 0}, {y, Any, 0}}, {}}, quote # none, line 1:
return +(x,y)
end))))
julia> typeof(ans)
LambdaStaticData
Apparently this AST can either be an Expr object or a compressed AST object, represented as a sequence of bytes:
julia> typeof(f0[3].ast)
Array{Uint8,1}
The show() method for LambdaStaticData from base/show.jl illustrates how to decompress this, when encountered:
julia> ccall(:jl_uncompress_ast, Any, (Any, Any), f0[3], f0[3].ast)
:($(expr(:lambda, {x, y}, {{}, {{x, Any, 0}, {y, Any, 0}}, {}}, quote # none, line 1:
return +(x,y)
end)))
julia> typeof(ans)
Expr
Julia has four functions and four macros analog to those functions, used to inspect a lot about generic function's methods:
julia> f(x, y) = x + y
f (generic function with 1 method)
julia> methods(f)
# 1 method for generic function "f":
f(x,y) at none:1
Lowered code:
julia> code_lowered(f, (Int, Int))
1-element Array{Any,1}:
:($(Expr(:lambda, {:x,:y}, {{},{{:x,:Any,0},{:y,:Any,0}},{}}, :(begin # none, line 1:
return x + y
end))))
julia> #code_lowered f(1, 1) # Both `Int`s
...same output.
Typed code:
julia> code_typed(f, (Int, Int))
1-element Array{Any,1}:
:($(Expr(:lambda, {:x,:y}, {{},{{:x,Int64,0},{:y,Int64,0}},{}}, :(begin # none, line 1:
return (top(box))(Int64,(top(add_int))(x::Int64,y::Int64))::Int64
end::Int64))))
julia> #code_lowered f(1, 1) # Both `Int`s
...same output.
LLVM code:
julia> code_llvm(f, (Int, Int))
define i64 #julia_f_24771(i64, i64) {
top:
%2 = add i64 %1, %0, !dbg !1014
ret i64 %2, !dbg !1014
}
julia> #code_llvm f(1, 1) # Both `Int`s
...same output.
Native code:
julia> code_native(f, (Int, Int))
.text
Filename: none
Source line: 1
push RBP
mov RBP, RSP
Source line: 1
add RDI, RSI
mov RAX, RDI
pop RBP
ret
julia> #code_llvm f(1, 1) # Both `Int`s
...same output.
Type instability warnings (v0.4+):
julia> #code_warntype f(1, 1)
Variables:
x::Int64
y::Int64
Body:
begin # In[17], line 1:
return (top(box))(Int64,(top(add_int))(x::Int64,y::Int64))
end::Int64
Reflection and introspection
I'm not sure that there is an AST associated with a generic function because of multiple dispatch. If you're writing a function definition fbody, you should be able to get the AST by doing dump(quote(fbody)).
Related
Suppose that you have a vector a tuple $a$, I want to define a function p(x)=x^a in julia.
For example, if a=(1,2,3), the resultant function would be x^1 *y^2 * z^3.
I would like to have a general method for any tuple, however I don't know the appropiate notation. In my code, I have an array of tuples and I want to define a monomial for each tuple in the array.
Thank you very much for your collaboration.
Is this what you want?
julia> function genmonomial(t::Tuple{Vararg{Integer}})
#assert !isempty(t) && all(>=(0), t)
return (x...) -> begin
#assert length(x) == length(t)
return mapreduce(x -> x[1]^x[2], *, zip(x, t))
end
end
genmonomial (generic function with 1 method)
julia> f = genmonomial((2,3,4))
#1 (generic function with 1 method)
julia> f(2, 1, 1)
4
julia> f(1, 2, 1)
8
julia> f(1, 1, 2)
16
julia> f(2, 2, 2)
512
julia> f(1, 1)
ERROR: AssertionError: length(x) == length(t)
julia> genmonomial(())
ERROR: AssertionError: !(isempty(t)) && all((>=)(0), t)
julia> genmonomial((1.5,))
ERROR: MethodError: no method matching genmonomial(::Tuple{Float64})
Closest candidates are:
genmonomial(::Tuple{Vararg{Integer}}) at REPL[1]:1
I came across Julia in some graduate research and have done a few projects already in C++. I'm trying to "translate" some of my C++ work into Julia to compare performance, among other things.
Basically what I'm trying to do is implement something like the functional library from C++ such that I can do something like
g(x, b) = x*b # Modifier function
A = [1,2,...] # Some array of values
f(x) = 1 # Initialize the function
### This is the part that I am seeking
for i = 1:length(A)
f(x) = f(x)*g(x, A[i]) # This thing right here
end
###
and then be able to call f(x) and get the value of all the g(x, _) terms included (similar to using bind in C++)
I'm not sure if there is native syntax to support this, or if I'll need to look into some symbolic representation stuff. Any help is appreciated!
You are probably looking for this:
julia> g(x, b) = x * b
g (generic function with 1 method)
julia> bind(g, b) = x -> g(x, b) # a general way to bind the second argument in a two argument function
bind (generic function with 1 method)
julia> A = [1, 2, 3]
3-element Vector{Int64}:
1
2
3
julia> const f = bind(g, 10) # bind a second argument of our specific function g to 10; I use use const for performance reasons only
#1 (generic function with 1 method)
julia> f.(A) # broadcasting f, as you want to apply it to a collection of arguments
3-element Vector{Int64}:
10
20
30
julia> f(A) # in this particular case this also works as A*10 is a valid operation, but in general broadcasting would be required
3-element Vector{Int64}:
10
20
30
In particular for fixing the first and the second argument of the two argument function there are standard functions in Julia Base that are called Base.Fix1 and Base.Fix2 respectively.
You may be looking for the function composition operator ∘
help?> ∘
"∘" can be typed by \circ<tab>
search: ∘
f ∘ g
Compose functions: i.e. (f ∘ g)(args...) means f(g(args...)). The ∘ symbol can be
entered in the Julia REPL (and most editors, appropriately configured) by typing
\circ<tab>.
Function composition also works in prefix form: ∘(f, g) is the same as f ∘ g. The
prefix form supports composition of multiple functions: ∘(f, g, h) = f ∘ g ∘ h and
splatting ∘(fs...) for composing an iterable collection of functions.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> map(uppercase∘first, ["apple", "banana", "carrot"])
3-element Vector{Char}:
'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
'B': ASCII/Unicode U+0042 (category Lu: Letter, uppercase)
'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
julia> fs = [
x -> 2x
x -> x/2
x -> x-1
x -> x+1
];
julia> ∘(fs...)(3)
3.0
so, for example
julia> g(b) = function(x); x*b; end # modifier function
g (generic function with 1 method)
julia> g(3)(2)
6
julia> f() = 3.14
f (generic function with 1 method)
julia> h = g(2) ∘ f
var"#1#2"{Int64}(2) ∘ f
julia> h()
6.28
I want to evaluate a set of vectors (or tuples) in a function $f$ but Julia says me that is imposible.
For example: If I have an array of tuples p=[(1,1), (1,-1), (-1,1), (-1,-1)] and a function f(x,y)=x+y. I would like to calculate f(p[1]) = f(1,1)= 2. But Julia says me that the types are incompatible.
Can you help me please?
You have to splat a tuple like this:
julia> p=[(1,1), (1,-1), (-1,1), (-1,-1)]
4-element Array{Tuple{Int64,Int64},1}:
(1, 1)
(1, -1)
(-1, 1)
(-1, -1)
julia> f(x,y)=x+y
f (generic function with 1 method)
julia> f(p[1]...)
2
you could also define a higher order function splat that would conveniently wrap any function and perform splatting. It is useful as then you can e.g. broadcast such function:
julia> splat(f) = x -> f(x...)
splat (generic function with 1 method)
julia> splat(f)(p[1])
2
julia> splat(f).(p)
4-element Array{Int64,1}:
2
0
0
-2
Alternatively you can define your function f like this:
julia> f((x,y),)=x+y
f (generic function with 1 method)
julia> f(p[1])
2
and now you do not have to do splatting.
Just use the ... operator to unpack the tuple as parameters:
julia> f(p[1]...)
2
In addition to other answers, if your task allows you, you can just define
julia> f(x) = f(x...)
and use it as
julia> f.(p)
4-element Vector{Int64}:
2
0
0
-2
Is there a way to get the surface syntax AST of a method?
according to the docs http://docs.julialang.org/en/stable/devdocs/reflection/ there is a function/macro for everything below surface AST, starting with code_lowered for lowered AST.
it would be great to have something like
f(a,b) = 2*a + b
#code_surface f(1,2)
# :(2a + b)
where code_surface shall return f's definition in form of a standard Expr abstract syntax tree.
#code_expr from CodeTracking.jl returns the Expr of the method definition (needs the Revise.jl package to also be installed).
julia> f(a,b) = 2*a + b
f (generic function with 1 method)
julia> using CodeTracking
julia> #code_expr f(1, 2)
:(f(a, b) = begin
#= REPL[65]:1 =#
2a + b
end)
julia> #code_string f(1, 2)
"f(a,b) = 2*a + b"
(There was discussion about adding a similar feature to base Julia, but it seems unlikely to happen at this point.)
Having the LHS of the method definition in the result helps with knowing which method was applied to the given arguments
julia> f(a::String,b::String) = a * b
f (generic function with 2 methods)
julia> #code_expr f("hi", " friend")
:(f(a::String, b::String) = begin
#= REPL[74]:1 =#
a * b
end)
julia> #code_expr f(2, 5)
:(f(a, b) = begin
#= REPL[65]:1 =#
2a + b
end)
But if you only need the Expr of the function body, you can extract that part of the expression from the result (ex = #code_expr(f(2, 5)); ex.args[2]).
Is there a way to concatenate ArrayViews in Julia, that doesn't copy the underlying data? (I'd also be glad to use a SubArray, if that solves the problem.)
In the code below, for example, I want a single ArrayView that references the data in both y1 and y2.
julia> x = [1:50];
julia> using ArrayViews;
julia> y1 = view(x, 2:5);
julia> y2 = view(x, 44:48);
julia> concat(y1, y2) # I wish there were a function like this
ERROR: concat not defined
julia> [y1, y2] # This copies the data in y1 and y2, unfortunately
9-element Array{Int64,1}:
2
3
4
5
44
45
46
47
48
Not directly. But you could roll your own type with something like:
julia> type CView{A<:AbstractArray} <: AbstractArray
a::A
b::A
end
julia> import Base: size, getindex, setindex!
julia> size(c::CView) = tuple([sa+sb for (sa, sb) in zip(size(c.a), size(c.b))]...)
size (generic function with 57 methods)
julia> getindex(c::CView, i::Int) = i <= length(c.a) ? getindex(c.a, i) : getindex(c.b, i)
getindex (generic function with 180 methods)
julia> c = CView(y1, y2);
julia> size(c)
(9,)
julia> c[1]
2
julia> c[4]
5
julia> c[5]
48
These methods may not be optimal but they can certainly get you started. To be useful, more methods would probably be needed. Note that the key is simply in deciding which member array to index into. For multidimensional indexing sub2ind can be used.