Skip to content

Commit 84c6418

Browse files
Enable local variables in functions (#1023)
Closes keyboardDrummer#24 ## Changes **LaurelToCoreTranslator.lean**: In the `translateExpr` case for `Block` with `LocalVariable` (with initializer), replaced the `disallowed` error with the actual translation: ```lean let coreMonoType ← translateType ty return .app () (.abs () name.text (some coreMonoType) bodyExpr) valueExpr ``` This encodes `var x: T := v; body` as the beta-redex `(λ x:T. body) v`, which the Core evaluator already handles via beta reduction, and the `Preconditions` module already handles as a let-binding pattern. **T3_ControlFlowError.lean**: Removed the three `error: local variables in functions are not YET supported` annotations and the associated comments, since local variables in functions are now supported. ## Verification - `LaurelToCoreTranslator` builds successfully - All Laurel fundamental tests pass (`T1` through `T21`) - All transform tests pass Co-authored-by: keyboardDrummer-bot <keyboardDrummer-bot@users.noreply.github.com>
1 parent bab843c commit 84c6418

2 files changed

Lines changed: 2 additions & 9 deletions

File tree

Strata/Languages/Laurel/LaurelToCoreTranslator.lean

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,8 @@ def translateExpr (expr : StmtExprMd)
275275
| .Block (⟨ .LocalVariable name ty (some initializer), innerSrc, innerMd⟩ :: rest) label => do
276276
let valueExpr ← translateExpr initializer boundVars isPureContext
277277
let bodyExpr ← translateExpr { val := StmtExpr.Block rest label, source := innerSrc, md := innerMd } (name :: boundVars) isPureContext
278-
disallowed (fileRangeToCoreMd innerSrc innerMd) "local variables in functions are not YET supported"
279-
-- This doesn't work because of a limitation in Core.
280-
-- let coreMonoType := translateType ty
281-
-- return .app () (.abs () (some coreMonoType) bodyExpr) valueExpr
278+
let coreMonoType ← translateType ty
279+
return .app () (.abs () name.text (some coreMonoType) bodyExpr) valueExpr
282280
| .Block (⟨ .LocalVariable name ty none, innerSrc, innerMd⟩ :: rest) label =>
283281
disallowed (fileRangeToCoreMd innerSrc innerMd) "local variables in functions must have initializers"
284282
| .Block (⟨ .IfThenElse cond thenBranch (some elseBranch), innerSrc, innerMd⟩ :: rest) label =>

StrataTest/Languages/Laurel/Examples/Fundamentals/T3_ControlFlowError.lean

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ function assertAndAssumeInFunctions(a: int) returns (r: int)
2222
a
2323
};
2424
25-
// Lettish bindings in functions not yet supported
26-
// because Core expressions do not support let bindings
2725
function letsInFunction() returns (r: int) {
2826
var x: int := 0;
29-
//^^^^^^^^^^^^^^^ error: local variables in functions are not YET supported
3027
var y: int := x + 1;
31-
//^^^^^^^^^^^^^^^^^^^ error: local variables in functions are not YET supported
3228
var z: int := y + 1;
33-
//^^^^^^^^^^^^^^^^^^^ error: local variables in functions are not YET supported
3429
z
3530
};
3631

0 commit comments

Comments
 (0)