Reusing existing computation
Consider the following Keelung program, notReused, which returns two copies of the input raised to the 4th power:
notReused :: Comp [Field]
notReused = do
x <- input Public
let y = x * x * x * x
return [y, y]Elaborating the program produces the following output:
> elaborate notReused
Right {
expression:
[ $FI0 * $FI0 * $FI0 * $FI0
, $FI0 * $FI0 * $FI0 * $FI0
]
compuation state:
{
variable counter: 0
input variable counter: 1
address counter: 0
input variables: $I0
num assignments: []
bool assignments: []
assertions: []
}
}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