Let and Bind
In Hakaru, we can give names for expressions to our programs with =
,
which we call Let. This gives us the ability to share computation
that might be needed in the program.
x = 2
x + 3
We can use =
to give a name to any expression in our language. The
name you assign is in scope for the rest of the body it was defined in.
Bind
Hakaru also has the operator <~
. This operator, which call Bind
can only be used with expressions that denote probability distributions.
Bind allows us to talk about draws from a distribution using a name for
any particular value that could have come from that distribution.
# Bad
x <~ 2 + 3
x
# Good
x <~ normal(0,1)
return x
Because Bind is about draws from a distribution, the rest of the body must also denote a probability distribution.
# Bad
x <~ normal(0,1)
x
# Good
x <~ normal(0,1)
return x
To help distinguish Let and Bind. Here is a probabilistic program, where we let f be equal to the normal distribution, and take draws from f.
f = normal(0,1)
x <~ f
return x*x