Reusing existing computation
Consider the following Keelung program, notReused
, which returns two copies of the input raised to the 4th power:
Elaborating the program produces the following output:
The elaboration seems alright, but there's a problem! Let's compile it and see what's wrong:
As you can see, there are two copies of the same constraints. This is because the variable y
has no meaning to Keelung:
And after elaboration it is simply replaced by the same expression used twice.
reuse
to the rescue!
reuse
to the rescue!To solve this issue, we can use the reuse
function to assign an expression to a variable that is observable by Keelung, so that we can reference it in the future when we want to reuse it:
For example, let's consider another program reused
which uses reuse
to compute the same expression as notReused
:
Compiling this program would result in a lot less constraints because we're able to share and reuse existing computations:
Last updated