Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| creating:closures [2025/09/02 19:27] – Added stub ahelwer | creating:closures [2026/11/20 21:33] (current) – Wrote intro ahelwer | ||
|---|---|---|---|
| Line 4: | Line 4: | ||
| This was very literal - the expressions provided as parameters to an operator should be substituted into the operator body itself, //then// evaluated. | This was very literal - the expressions provided as parameters to an operator should be substituted into the operator body itself, //then// evaluated. | ||
| Instead, we evaluated the operator parameters first, //then// substituted the resulting values into the operator body. | Instead, we evaluated the operator parameters first, //then// substituted the resulting values into the operator body. | ||
| - | This didn't matter before implementing the prime operator, but | + | This didn't matter before implementing the prime operator, but now it does! |
| + | Consider the spec: | ||
| + | <code haskell> | ||
| + | VARIABLE x | ||
| + | Init == x = 0 | ||
| + | Increment(v) == v' = v + 1 | ||
| + | Next == Increment(x) | ||
| + | </ | ||
| - | TODO | + | With our current operator evaluation method, '' |
| + | Thus this spec deadlocks after the initial state instead of repeatedly incrementing, | ||
| - | [[creating:safety|< Previous Page]] | [[creating:start# | + | How can we ensure operator arguments are evaluated after being placed in the operator body, instead of when they are passed in? |
| + | Taking a look at our current implementation of '' | ||
| + | <code java> | ||
| + | List< | ||
| + | for (Expr argument | ||
| + | arguments.add(evaluate(argument)); | ||
| + | } | ||
| + | // (some validation code omitted) | ||
| + | |||
| + | return operator.call(this, | ||
| + | </ | ||
| + | So '' | ||
| + | Can we just pass in the un-evaluated '' | ||
| + | Even if we could get the type-checking to work out, unfortunately we run into the problem that these arguments might refer to identifiers that no longer have any definition once evaluated within the operator. | ||
| + | Consider: | ||
| + | <code haskell> | ||
| + | Set(var, val) == var' = val | ||
| + | IncremementBy(v, | ||
| + | </ | ||
| + | When '' | ||
| + | But then what is '' | ||
| + | Neither of those variables are in-scope in the body of '' | ||
| + | So we arrive at the idea of // | ||
| + | Read through that section now. | ||
| + | The trick we'll use here - a bit different from the book - is automatically turning operator arguments into zero-arity closures. | ||
| + | In formal language design, these are whimsically called // | ||
| + | |||
| + | ====== Section 10.6: Local Functions and Closures ====== | ||
| + | |||
| + | To implement thunks, we will first follow along with [[https:// | ||
| + | |||
| + | [[creating: | ||