Say I have this code:
#lang racket
(define a
((λ (x) x)
((λ (y) y)
(λ (z)
((λ (w) w) z)))))
I know intuitively that this lambda expression is (extensionally) equal to (λ (z) z)
My question is if there is a way to print out the body of a
in case I want to see how much the function got simplified internally by Racket.
More information:
By default, if I type a
into the interpreter, I get #<procedure:y>
(This seems to give a hint as to how much evaluation happened). I can change the output style to "constructor", say, and then the result is (lambda (a1) ...)
which is closer to what I want, but I still don't know what's in the body which is the important part.
I imagine one could answer this with a more thorough knowledge of Racket's evaluation strategy, but I'm still interested if displaying procedure bodies is a thing that can happen in general.
In general there is no way to see body of a closure at runtime. The "body" is part of the source code. It gets compiled into bytecode (and later to machine code by the JIT). The closure consists at runtime of the captured values for the free variables in the body and a pointer to the code.
That said, you might be satisfied with studying the output of
expand
orcompile
.Now
expand
only expands syntax, so you won't be able to see any optimizations yet:Note that
#%app
means "application".To see the result after optimizations are applied we need to invoke the compiler. The builtin
compile
produces bytecodes, so we usecompile-zo
which converts the bytecode into structures representing bytecode (and they print nicely in the repl).There is only one application left, so the program was indeed simplified during compilation.
See https://github.com/soegaard/meta/blob/master/runtime/racket-eval.rkt for a definition of
compile-zo
.Finally yet another option is to look at the machine code produced by the JIT. See https://github.com/samth/disassemble
UPDATE
If I read the bytecode correctly, it corresponds to: