truffle is great, it's the most exciting tech in pldev domain to me. I'll just mention truffle has few other perks than just performance boost -- you get a debugger with a fairly small effort, and you get interop with other languages implemented using truffle (which does include fairly mainstream ones like python, js, and others).
This type of optimization is also known as compile-time constant expression folding.
The problem with expanding it to include function calls is that it compromises binary compatibility. A code change on a function that results in a different range or has different side effects, will not be realized unless the caller is recompiled.
Which is why the second most important optimization of compilers is inlining. If the callee is small enough then hoisting it up into the caller can expose additional optimizations, including noticing that parameters in the caller to the callee are in fact constant.
Zig isn't perfect, but I've definitely done this futamura stuff before. It's one of my favourite ways to implement a Forth in Zig (which, incidentally, is the default way that I introduce a backprop library or other DSL as I'm prototyping an idea). Interpreters are dead-simple to write, and I just write an interpreter with a comptime code argument. It definitely works, and the codegen is reasoanble out of the box.
It allows us to implement operators or functions without having to write them in terms of LLVM IR DSL (or similar). Rather, you write your functions over a wrapper type (and returning one), for which there is an overload of anything which you can overload in c++.
You can instantiate a wrapper from some c++ value, in which case the wrapper will just forward everything to this value, which was nice for debugging in the preagentic era.
But, you can also instantiate such a wrapper as a tracer, and the result of the functions evaluation will be a trace.
For controlflow, we record whenever a wrapper type is evaluated as a bool, first return true, and when we reach the end of one branch, throw an exception to get back to the beginning, and start all over again, but evaluate it to false.
The resulting trace we can turn into LLVM IR and compile, or run for example in wasm
Very interesting concept! It reminds me of Hazel (https://hazel.org/) which takes the idea further and into the editor; any program written in it can be partially evaluated, even if variables or even part of the syntax are missing. Missing parts are called "holes", and those are preserved during evaluation so that something like
abs( + 2 * 3 - 10)
becomes
abs( - 4)
which is great fun. My first attempts using this showed me that if you're not used to this, it actually gets pretty confusing quite quickly, but I guess it can be learned.
> For me, the most beautiful pieces of software are those that reveal something about the nature of programming languages.
There are three moments that make the functional programmer evolve their understanding of how a programming language really works as a symbolic representation of human thought, beyond the metaphor taught in school of a computer program as a grinder that crunches input data and produces results.
The first one is when you really understand recursion and how a function can be defined in terms of itself (with varying parameters for each call).
The second is when you discover Prolog variable unification or Haskell lazy evaluation and how you can represent infinite data structures without having the program hang in an infinite loop.
Third one is when you learn that the behavior of any imperative language (down to the working of the microprocessor data paths) can be fully represented in a pure functional rewrite language (without destructive assignment / cell state changes).
Those insights make one realize that all programming paradigms have the nature of symbols processing in common and there's no "true description" of a program's runtime execution - the imperative, functional and logic views of the running program are all equivalent representations.
> Third one is when you learn that the behavior of any imperative language (down to the working of the microprocessor data paths) can be fully represented in a pure functional rewrite language (without destructive assignment / cell state changes).
Can you either expand on that one or link me to sources that do? I think I follow, but I'd like more info
Not OP, and probably not what he meant, but for me the insight that any for loop can be expressed as a fully equivalent tail-recursive call and vice-versa is what I would describe as my No.3 moment.
I don't think that's what is being said. The statement is more like 'any imperative program can be represented by a purely functional representation', which goes beyond simply modeling loops with tail call elimination.
> Third one is when you learn that the behavior of any imperative language (down to the working of the microprocessor data paths) can be fully represented in a pure functional rewrite language (without destructive assignment / cell state changes).
What no one is ever able to explain is: why is this a good thing?
The underlying machine does destructive assignment. The whole thing is a big mutable array. Why should we represent it in another way?
All the examples in here look pretty clean, but doesn't partial application like this in practice on "average" codebases result in massive binary size explosion?
I once tried this for an interpreting parser I wrote and it resulted in a very large program that took far longer to compile and was also slower than the original interpreter, probably due the code no longer fitting in the processor caches.
measurablefunc | 6 hours ago
grashalm | 6 hours ago
https://www.graalvm.org/latest/graalvm-as-a-platform/languag...
arvyy | 4 hours ago
measurablefunc | 3 hours ago
owlstuffing | 6 hours ago
The problem with expanding it to include function calls is that it compromises binary compatibility. A code change on a function that results in a different range or has different side effects, will not be realized unless the caller is recompiled.
cogman10 | 5 hours ago
apignotti | 5 hours ago
https://labs.leaningtech.com/blog/reducing-webassembly-size-...
infogulch | 5 hours ago
jimmyhmiller | 5 hours ago
Consider this example. https://jimmyhmiller.com/split-shot/#/vm
Make the code a comptime argument in zig and you get "store to comptime variable depends on runtime condition".
hansvm | 4 hours ago
speedster217 | 3 hours ago
artrax | 4 hours ago
https://github.com/nebulastream/nautilus
It allows us to implement operators or functions without having to write them in terms of LLVM IR DSL (or similar). Rather, you write your functions over a wrapper type (and returning one), for which there is an overload of anything which you can overload in c++.
You can instantiate a wrapper from some c++ value, in which case the wrapper will just forward everything to this value, which was nice for debugging in the preagentic era. But, you can also instantiate such a wrapper as a tracer, and the result of the functions evaluation will be a trace.
For controlflow, we record whenever a wrapper type is evaluated as a bool, first return true, and when we reach the end of one branch, throw an exception to get back to the beginning, and start all over again, but evaluate it to false.
The resulting trace we can turn into LLVM IR and compile, or run for example in wasm
johntortugo | 4 hours ago
airtnp | 4 hours ago
Skill => Generic Program LLM skill optimizer => Partial evaluator Context => Env/Input Agent => Interpreter
First projection: mix(interpreter, program) => specialized skill AOT compiling => offline skill rewriting JIT compiling => runtime specializing, defer loading
Second projection: mix(mix, interpreter) => Agent/Model specific skill optimizer
Third projection: mix(mix, mix) => self-improvement skill optimizer
Whatever maybe I'm just thinking LLM as magic partial evaluator for any tasks
iFreilicht | 3 hours ago
abs( + 2 * 3 - 10)
becomes
abs( - 4)
which is great fun. My first attempts using this showed me that if you're not used to this, it actually gets pretty confusing quite quickly, but I guess it can be learned.
fmbb | 3 hours ago
TuringTest | 3 hours ago
There are three moments that make the functional programmer evolve their understanding of how a programming language really works as a symbolic representation of human thought, beyond the metaphor taught in school of a computer program as a grinder that crunches input data and produces results.
The first one is when you really understand recursion and how a function can be defined in terms of itself (with varying parameters for each call).
The second is when you discover Prolog variable unification or Haskell lazy evaluation and how you can represent infinite data structures without having the program hang in an infinite loop.
Third one is when you learn that the behavior of any imperative language (down to the working of the microprocessor data paths) can be fully represented in a pure functional rewrite language (without destructive assignment / cell state changes).
Those insights make one realize that all programming paradigms have the nature of symbols processing in common and there's no "true description" of a program's runtime execution - the imperative, functional and logic views of the running program are all equivalent representations.
packetlost | 3 hours ago
Can you either expand on that one or link me to sources that do? I think I follow, but I'd like more info
IsTom | 2 hours ago
tpoacher | 2 hours ago
packetlost | 2 hours ago
amw-zero | 58 minutes ago
What no one is ever able to explain is: why is this a good thing?
The underlying machine does destructive assignment. The whole thing is a big mutable array. Why should we represent it in another way?
AlotOfReading | 23 minutes ago
Optimizing deeply enough will eventually make any abstraction leaky though, so you'll never fully escape mechanical sympathy.
p1necone | 3 hours ago
fjfaase | 2 hours ago