3 ms·
For reference, a staged function is something sorta similar to a macro, except it lets you take advantage of Julia's inferred type info. Contrived (and wholly r
by one-more-minute 12y ago
For reference, a staged function is something sorta similar to a macro, except it lets you take advantage of Julia's inferred type info. Contrived (and wholly redundant) example:
stagedfunction foo(x)
if x == Int
:(2x)
else
:x
end
end
y = 1
foo(y) == 2
y = 1.
foo(y) == 1.0
Again, just like a macro, except that before `x` is quoted you work with the type of y instead of the symbol `y`. This means you can do even more crazy code specialisation, including on e.g. matrix dimensions or generating appropriate FFI calls based on arbitrary input types. It has zero overhead and you can use it just like a regular function.
Very cool, and I think people will do really interesting things with this (especially after mixing them with macros).
- infinite8s 12y agoThis sounds very similar to an approach called Lightweight Modular Staging (pioneered in Scala by the Oderski group - http://scala-lms.github.io/ http://scala-lms.github.io/ and also in Lua - http://terralang.org/ http://terralang.org/). While this is great for specializing numeric code based on runtime invariants like dimensionality, I think people are finally looking at using the idea in other domains - for example, runtime generation of DSL code on a per instantiation basis. Imagine taking a high level SQL query, building the operator tree in a Julia DSL and optimizing that, and then JITing the entire thing taking into account low level storage details and the particular sets of operators and joins (http://msr-waypoint.com/en-us/events/dcp2014/rompf.pdf http://msr-waypoint.com/en-us/events/dcp2014/rompf.pdf). This is very exciting to see in another typed language!
- tlmr 12y agoYou mean like blaze in python?: http://blaze.pydata.org/docs/v_0_6_5/index.html http://blaze.pydata.org/docs/v_0_6_5/index.html
- infinite8s 12y agoBlaze has a bit of a broader focus than what I was talking about, since blaze mostly offloads the actual computation to a particular backend. But a combination of blaze and a custom lowering of the computation into machine code using numba would be similar (although without the type safety for guaranteeing that certain optimizations are possible)
- tlmr 12y agoI believe that the numba compilation is planned for the coming year.