Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
7e34b04
Improve Laurel printer
keyboardDrummer May 26, 2026
f246686
Introduce transparency pass
keyboardDrummer May 26, 2026
b11e042
Fixes
keyboardDrummer May 26, 2026
9e4ee85
Refactoring
keyboardDrummer May 26, 2026
7408103
Remove pack multiple outputs pass
keyboardDrummer May 26, 2026
4e8fa8b
Add error file
keyboardDrummer May 26, 2026
2d19629
Update tests
keyboardDrummer May 26, 2026
c9fd151
Add sources to lift phase
keyboardDrummer May 26, 2026
19987e8
Update tests and improve errors
keyboardDrummer May 26, 2026
24c3983
Update comment
keyboardDrummer May 26, 2026
3c23531
Trigger CI
keyboardDrummer May 26, 2026
6046381
Trigger CI
keyboardDrummer May 26, 2026
86962e4
Trigger CI
keyboardDrummer May 26, 2026
d192346
Replace panic with safe access
keyboardDrummer May 27, 2026
7a04e66
Move proof skipping logic from LaurelToCore translator to transparenc…
keyboardDrummer May 28, 2026
a45f8f0
Small refactoring
keyboardDrummer May 28, 2026
96b2686
Merge branch 'formatting-and-debugging-improvements' into transparenc…
keyboardDrummer May 28, 2026
d0fdd35
Cleanup
keyboardDrummer May 28, 2026
2e0d7e1
Merge branch 'transparency-pass-only' of github.com:keyboardDrummer/S…
keyboardDrummer May 28, 2026
c62cfb4
Add contract pass
keyboardDrummer May 28, 2026
a95da10
Cleanup
keyboardDrummer May 28, 2026
49255b4
Fixes
keyboardDrummer May 28, 2026
10640f7
Fixes
keyboardDrummer May 28, 2026
8c15c16
Do not run the contract pass for functions yet
keyboardDrummer May 28, 2026
573a36b
Reduce id changes
keyboardDrummer May 28, 2026
74ec111
Fixes
keyboardDrummer May 28, 2026
800f6c4
Test fixes
keyboardDrummer May 28, 2026
e56cb20
Fix test
keyboardDrummer May 28, 2026
a352b47
Fix test
keyboardDrummer May 28, 2026
65c92f4
Fix test
keyboardDrummer May 28, 2026
4a8c667
Remove unused files
keyboardDrummer May 28, 2026
d7d7205
Undo Python test changes
keyboardDrummer May 28, 2026
60c9ca3
Fixes
keyboardDrummer May 28, 2026
a526dad
Add missing bindings for PrimitiveOp
keyboardDrummer May 28, 2026
ace39ca
Improve comment
keyboardDrummer May 28, 2026
2e94fc5
Add missing bindings for PrimitiveOp
keyboardDrummer May 28, 2026
200e828
Improve comment
keyboardDrummer May 28, 2026
8875ffd
Merge branch 'transparency-pass-only' into issue-924-contract-and-pro…
keyboardDrummer May 28, 2026
9400328
Generate separate $pre and $post procedures for each condition
keyboardDrummer-bot May 28, 2026
65ab6ae
Rename SepFormat.semicolon to SepFormat.semicolonNewline
keyboardDrummer-bot May 28, 2026
6c0c39c
Fix contract pass: preserve postconditions in Opaque/Abstract bodies
keyboardDrummer-bot May 28, 2026
d6bff50
Fix contract pass: make postcondition helpers functional
keyboardDrummer-bot May 28, 2026
2cae5e0
Update golden-file expected outputs for changed label counters
keyboardDrummer-bot May 28, 2026
5c0a458
Extract emitCoreDiagnostic helper to reduce repetition
keyboardDrummer-bot May 28, 2026
ff09522
Code review
keyboardDrummer Jun 1, 2026
afaf9a2
Stop dropping source location from callee
keyboardDrummer Jun 1, 2026
9f65463
Code review
keyboardDrummer Jun 1, 2026
d143eeb
Merge remote-tracking branch 'origin/formatting-and-debugging-improve…
keyboardDrummer Jun 1, 2026
35d7d75
Merge commit 'a995388cb~1' into transparency-pass-only
keyboardDrummer Jun 1, 2026
fc62415
Merge commit 'a995388cb' into transparency-pass-only
keyboardDrummer Jun 1, 2026
e1caee4
Trigger CI
keyboardDrummer Jun 1, 2026
3e76abc
Fix nit
keyboardDrummer Jun 1, 2026
6f0d365
Use HashSet for asFunctionNames and add isDestructorName/isTesterName…
keyboardDrummer-bot Jun 1, 2026
80f22a6
Merge branch 'transparency-pass-only' of github.com:keyboardDrummer/S…
keyboardDrummer Jun 1, 2026
1e9710b
Fix warning
keyboardDrummer Jun 1, 2026
944ffbd
Merge branch 'transparency-pass-only' into issue-924-contract-and-pro…
keyboardDrummer Jun 1, 2026
20225bd
Trigger CI
keyboardDrummer Jun 2, 2026
b52f50b
Trigger CI
keyboardDrummer Jun 2, 2026
b6f0f64
Don't generate intermediate files
keyboardDrummer Jun 2, 2026
6ee3c7e
Exclude hidden files from the lake cache hash
keyboardDrummer Jun 2, 2026
4b44fbf
Exclude hidden files from the lake cache hash
keyboardDrummer Jun 2, 2026
1c5a9e1
Add polymorphism comment and recursive transparent procedure test
keyboardDrummer-bot Jun 2, 2026
81b558c
Fixes
keyboardDrummer Jun 2, 2026
ac0fdb1
Update recursive test to use procedures
keyboardDrummer Jun 3, 2026
cf29a09
Delete recursive transparent
keyboardDrummer Jun 3, 2026
780cbee
Merge branch 'main2' into transparency-pass-only
keyboardDrummer Jun 3, 2026
3dc9527
Add a test-case
keyboardDrummer Jun 3, 2026
74d532a
Merge upstream/main2 into transparency-pass-only
keyboardDrummer-bot Jun 4, 2026
e9887a6
Merge branch 'transparency-pass-only' into issue-924-contract-and-pro…
keyboardDrummer Jun 4, 2026
70cb26e
Fixes
keyboardDrummer Jun 4, 2026
17c1d84
Fix test after merge
keyboardDrummer Jun 4, 2026
68cec09
Fix test expectation
keyboardDrummer Jun 4, 2026
dbfcb95
Remove unused module
keyboardDrummer Jun 4, 2026
2f8eb11
Fix bug in Resolution.lean
keyboardDrummer Jun 4, 2026
389e214
Merge branch 'transparency-pass-only' into issue-924-contract-and-pro…
keyboardDrummer Jun 10, 2026
d141467
Merge commit 'a364a70a~1' into issue-924-contract-and-proof-pass
keyboardDrummer Jun 10, 2026
87d7214
Merge commit 'a364a70a' into issue-924-contract-and-proof-pass
keyboardDrummer Jun 10, 2026
61c2e83
Address quality follow-ups from review
keyboardDrummer-bot Jun 11, 2026
9ca5c77
Add test and fix for counter created by contract pass
keyboardDrummer Jun 11, 2026
b4938f0
Update Strata/Languages/Laurel/ContractPass.lean
keyboardDrummer Jun 11, 2026
3c0e745
Merge branch 'issue-924-contract-and-proof-pass' of github.com:keyboa…
keyboardDrummer Jun 11, 2026
9c9ace9
Update Strata/Languages/Laurel/EliminateValuesInReturns.lean
keyboardDrummer Jun 11, 2026
d8d9b96
Merge branch 'issue-924-contract-and-proof-pass' of github.com:keyboa…
keyboardDrummer Jun 11, 2026
bef6885
Cleanup
keyboardDrummer Jun 11, 2026
96982e3
Merge remote-tracking branch 'fork/quality-followups-from-fabiomadge'…
keyboardDrummer Jun 11, 2026
551e699
partial
keyboardDrummer Jun 11, 2026
67f43bf
Simplify termination proof
keyboardDrummer Jun 11, 2026
ba8524a
Merge branch 'main2' into issue-924-contract-and-proof-pass
keyboardDrummer Jun 11, 2026
ac0b6be
Fix test
keyboardDrummer Jun 11, 2026
31e9007
Merge branch 'noHiddenFilesInHash' into issue-924-contract-and-proof-…
keyboardDrummer Jun 11, 2026
7eef859
Code review changes
keyboardDrummer Jun 12, 2026
4b8ef55
Merge remote-tracking branch 'origin/main2' into issue-924-contract-a…
keyboardDrummer Jun 12, 2026
237eabe
Pass tweaks
keyboardDrummer Jun 12, 2026
4ffc62f
Fixes
keyboardDrummer Jun 12, 2026
5178451
More pass refactoring changes
keyboardDrummer Jun 12, 2026
0ba057d
Fixes
keyboardDrummer Jun 12, 2026
1627ce4
Fixes
keyboardDrummer Jun 12, 2026
816a738
Include final pass in the all pass list
keyboardDrummer Jun 13, 2026
8470d93
Fixes
keyboardDrummer Jun 13, 2026
aebe44c
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 13, 2026
b7508d7
Fix test
keyboardDrummer Jun 13, 2026
f1507d1
Update Laurel docs
keyboardDrummer Jun 13, 2026
1077979
Change text
keyboardDrummer Jun 13, 2026
b30f4cb
Fix invokeOn axiom: guard with preconditions + reject output-referenc…
keyboardDrummer-bot Jun 13, 2026
6dd545a
Fix if-then-else prepend duplication in LiftImperativeExpressions
keyboardDrummer-bot Jun 13, 2026
d1f9d6c
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 16, 2026
0322796
Fix test
keyboardDrummer Jun 16, 2026
878f325
Code review
keyboardDrummer Jun 16, 2026
e0c8b68
Fix early return
keyboardDrummer Jun 16, 2026
a63384a
Fix lifting bug
keyboardDrummer Jun 16, 2026
02ad82c
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 16, 2026
799f167
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 16, 2026
83e793b
Port fixes to lifting pass
keyboardDrummer Jun 18, 2026
6e0ce93
Merge branch 'reviewed-kbd-will-merge-to-main' into issue-924-contrac…
keyboardDrummer Jun 18, 2026
e1fc044
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 18, 2026
dcfb0e9
Some fixes for resolution during phases
keyboardDrummer Jun 18, 2026
a0ec015
Fixes to resolution so it throws fewer errors during passes
keyboardDrummer Jun 18, 2026
25f1d5d
Emit resolution errors during passes
keyboardDrummer Jun 18, 2026
fa7f844
Source fix
keyboardDrummer Jun 18, 2026
441ec1e
Improve well-formedness of the 3 heap passes
keyboardDrummer Jun 18, 2026
cf706b2
Fix for type hierarchy
keyboardDrummer Jun 18, 2026
66589c5
Refactoring
keyboardDrummer Jun 18, 2026
903f89e
Allow dangling addition in blocks
keyboardDrummer Jun 18, 2026
1a0e8ae
Fix diagnostic and test
keyboardDrummer Jun 18, 2026
018939b
Remove function return type expectation check
keyboardDrummer Jun 18, 2026
8d34b32
Update resolver check to use procedure instead of function
keyboardDrummer Jun 18, 2026
d9b39ac
Small improvements
keyboardDrummer Jun 18, 2026
a24cc86
Merge branch 'issue-924-contract-and-proof-pass' of github.com:keyboa…
keyboardDrummer Jun 18, 2026
c438d8c
Merge branch 'fixResolutionErrorsDuringPhases' into issue-924-contrac…
keyboardDrummer Jun 18, 2026
fcb3903
Fix merge
keyboardDrummer Jun 18, 2026
4efaebd
Remove obsolete test
keyboardDrummer Jun 18, 2026
8ce6fc6
Add multi output test-case to ImpureExpressions
keyboardDrummer Jun 19, 2026
157ec6b
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 19, 2026
d7a4c3c
Refactor liftImperative
keyboardDrummer Jun 19, 2026
0b03bff
Simplify lift pass
keyboardDrummer Jun 19, 2026
97e3146
Bring back test
keyboardDrummer Jun 19, 2026
ffcc39d
Recursion proof refactoring
keyboardDrummer Jun 19, 2026
1ef148f
Fix proofs
keyboardDrummer Jun 19, 2026
a279ff7
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 22, 2026
34c850f
Cleanup
keyboardDrummer Jun 22, 2026
12b5e59
Fix
keyboardDrummer Jun 22, 2026
bfc0b6c
Fix test
keyboardDrummer Jun 22, 2026
2e2a7b3
Fix tests
keyboardDrummer Jun 22, 2026
5f46178
Update expect file
keyboardDrummer Jun 22, 2026
16be9b5
Merge remote-tracking branch 'origin/reviewed-kbd-will-merge-to-main'…
keyboardDrummer Jun 23, 2026
97870a3
Undo added test and fix related to that
keyboardDrummer Jun 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Strata/Languages/Laurel/ConstrainedTypeElim.lean
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def constraintCallFor (ptMap : ConstrainedTypeMap) (ty : HighType)
For nested types, the function calls the parent's constraint function. -/
def mkConstraintFunc (ptMap : ConstrainedTypeMap) (ct : ConstrainedType) : Procedure :=
let baseType := resolveType ptMap ct.base
let bodyExpr := match ct.base.val with
let bodyExpr: StmtExprMd := match ct.base.val with
| .UserDefined parent =>
if ptMap.contains parent.text then
let paramId := { ct.valueName with uniqueId := none }
Expand All @@ -89,7 +89,7 @@ def mkConstraintFunc (ptMap : ConstrainedTypeMap) (ct : ConstrainedType) : Proce
{ name := mkId s!"{ct.name.text}$constraint"
inputs := [{ name := ct.valueName, type := baseType }]
outputs := [{ name := mkId "result", type := { val := .TBool, source := none } }]
body := .Transparent { val := .Block [bodyExpr] none, source := none }
body := .Transparent { val := .Return bodyExpr, source := none }
isFunctional := true
decreases := none
preconditions := [] }
Expand Down Expand Up @@ -244,11 +244,11 @@ public def constrainedTypeElim (model : SemanticModel) (program : Program)
funcDiags)

/-- Pipeline pass: constrained type elimination. -/
public def constrainedTypeElimPass : LaurelPass where
public def constrainedTypeElimPass : LoweringPass where
name := "ConstrainedTypeElim"
documentation := "Eliminates constrained types by replacing them with their base types and generating constraint-checking functions and witness procedures. Type tests against constrained types are rewritten to call the generated constraint function."
needsResolves := true
run := fun p m =>
run := fun p m _ =>
let (p', diags) := constrainedTypeElim m p
(p', diags, {})

Expand Down
461 changes: 461 additions & 0 deletions Strata/Languages/Laurel/ContractPass.lean

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions Strata/Languages/Laurel/CoreDefinitionsForLaurel.lean
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@ program Laurel;

datatype LaurelResolutionErrorPlaceholder {}
datatype Float64IsNotSupportedYet {}
datatype LaurelUnit { MkLaurelUnit() }

// The types for these Map functions are incorrect.
// We'll fix them when Laurel supports polymorphism
function select(map: int, key: int) : int
// And then we can remove the datatype Box as well
// And remove the hacky filter in HeapParameterization
datatype Box { MkBox() }

function select(map: int, key: int) : Box
external;

function update(map: int, key: int, value: int) : int
function update(map: int, key: int, value: int) : Box
external;

function const(value: int) : int
function const(value: int) : Box
external;

#end
Expand Down
34 changes: 25 additions & 9 deletions Strata/Languages/Laurel/CoreGroupingAndOrdering.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
-/

module
public import Strata.Languages.Laurel.TransparencyPass
public import Strata.Languages.Laurel.LaurelAST
public import Strata.Languages.Laurel.UnorderedCore
public import Strata.Languages.Laurel.LaurelPass
import Strata.DL.Lambda.LExpr
import StrataDDM.Util.Graph.Tarjan
import Strata.Languages.Laurel.Grammar.AbstractToConcreteTreeTranslator
Expand All @@ -27,6 +29,7 @@ declarations before they are emitted as Strata Core declarations.
namespace Strata.Laurel

open Lambda (LMonoTy LExpr)
open Std (Format ToFormat)

/-- Collect all `UserDefined` type names referenced in a `HighType`, including nested ones. -/
def collectTypeRefs : HighTypeMd → List String
Expand Down Expand Up @@ -54,7 +57,7 @@ def collectStaticCallNames (expr : StmtExprMd) : List String :=
match val with
| .StaticCall callee args =>
callee.text :: args.flatMap (fun a => collectStaticCallNames a)
| .PrimitiveOp _ args => args.flatMap (fun a => collectStaticCallNames a)
| .PrimitiveOp _ args _ => args.flatMap (fun a => collectStaticCallNames a)
| .IfThenElse cond t e =>
collectStaticCallNames cond ++
collectStaticCallNames t ++
Expand Down Expand Up @@ -113,18 +116,18 @@ Build the procedure call graph, run Tarjan's SCC algorithm, and return each SCC
as a list of procedures paired with a flag indicating whether the SCC is recursive.
Results are in reverse topological order: dependencies before dependents.

Procedures with `invokeOn` are placed as early as possible — before
Procedures with axioms are placed as early as possible — before
unrelated procedures without them — by stably partitioning them first before building
the graph. Tarjan then naturally assigns them lower indices, causing them to appear
earlier in the output.
-/
public def computeSccDecls (program : UnorderedCoreWithLaurelTypes) : List (List Procedure × Bool) :=
-- Stable partition: procedures with invokeOn come first, preserving relative
-- Stable partition: procedures with axioms come first, preserving relative
-- order within each group. Tarjan then places them earlier in the topological output.
let allProcs := program.functions ++ program.coreProcedures
let (withInvokeOn, withoutInvokeOn) :=
allProcs.partition (fun p => p.invokeOn.isSome)
let orderedProcs : List Procedure := withInvokeOn ++ withoutInvokeOn
let (withAxioms, withoutAxioms) :=
allProcs.partition (fun p => !p.axioms.isEmpty)
let orderedProcs : List Procedure := withAxioms ++ withoutAxioms

-- Build a call-graph over all procedures.
-- An edge proc → callee means proc's body/contracts contain a StaticCall to callee.
Expand All @@ -142,7 +145,8 @@ public def computeSccDecls (program : UnorderedCoreWithLaurelTypes) : List (List
| _ => []
let contractExprs : List StmtExprMd :=
proc.preconditions.map (·.condition) ++
proc.invokeOn.toList
proc.invokeOn.toList ++
proc.axioms
(bodyExprs ++ contractExprs).flatMap collectStaticCallNames

-- Build the OutGraph for Tarjan.
Expand Down Expand Up @@ -225,7 +229,7 @@ Functions are grouped into SCCs (for mutual recursion). Proofs are emitted
as individual `procedure` decls. Both participate in the topological ordering
so that axioms are available to functions that need them.
-/
public def orderFunctionsAndProcedures (program : UnorderedCoreWithLaurelTypes) : CoreWithLaurelTypes :=
def orderFunctionsAndProcedures (program : UnorderedCoreWithLaurelTypes) : CoreWithLaurelTypes :=
let datatypeDecls := (groupDatatypesByScc' program).map OrderedDecl.datatypes
let constantDecls := program.constants.map OrderedDecl.constant
let funcNames : Std.HashSet String :=
Expand Down Expand Up @@ -254,4 +258,16 @@ where
let members := comp.toList.filterMap fun idx => dtsArr[idx]?
if members.isEmpty then none else some members

public def orderingPass : LaurelPass UnorderedCoreWithLaurelTypes CoreWithLaurelTypes where
name := "OrderingPass"
comesBefore := []
documentation := "Produce a `CoreWithLaurelTypes` from a `UnorderedCoreWithLaurelTypes` by
computing a combined ordering of functions and proofs using the call graph,
then collecting datatypes and constants.
Functions are grouped into SCCs (for mutual recursion). Proofs are emitted
as individual `procedure` decls. Both participate in the topological ordering
so that axioms are available to functions that need them."
run := fun p _ _ =>
(orderFunctionsAndProcedures p, [], {})

end Strata.Laurel
25 changes: 13 additions & 12 deletions Strata/Languages/Laurel/DesugarShortCircuit.lean
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,43 @@ namespace Strata.Laurel

public section

private def bare (v : StmtExpr) : StmtExprMd := ⟨v, none⟩

/-- Local rewrite of a single short-circuit node. Recursion is handled by `mapStmtExpr`. -/
private def desugarShortCircuitNode (model : SemanticModel) (expr : StmtExprMd) : StmtExprMd :=
private def desugarShortCircuitNode (imperativeCallees : List String) (expr : StmtExprMd) : StmtExprMd :=
let source := expr.source
let wrap (v : StmtExpr) : StmtExprMd := ⟨v, source⟩
match expr.val with
| .PrimitiveOp op args _ =>
match op, args with
-- With bottom-up traversal, `a` and `b` are already desugared (nested
-- short-circuits converted to IfThenElse). The check still works because
-- `containsAssignmentOrImperativeCall` recurses into IfThenElse.
| .AndThen, [a, b] | .Implies, [a, b] =>
if containsAssignmentOrImperativeCall model b then
if containsAssignmentOrImperativeCall imperativeCallees b then
let elseVal := match op with | .AndThen => false | _ => true
⟨.IfThenElse a b (some (bare (.LiteralBool elseVal))), source⟩
⟨.IfThenElse a b (some (wrap (.LiteralBool elseVal))), source⟩
else expr
| .OrElse, [a, b] =>
if containsAssignmentOrImperativeCall model b then
⟨.IfThenElse a (bare (.LiteralBool true)) (some b), source⟩
if containsAssignmentOrImperativeCall imperativeCallees b then
⟨.IfThenElse a (wrap (.LiteralBool true)) (some b), source⟩
else expr
| _, _ => expr
| _ => expr

/-- Desugar short-circuit operators in a program. -/
def desugarShortCircuit (model : SemanticModel) (program : Program) : Program :=
mapProgram (mapStmtExpr (desugarShortCircuitNode model)) program
def desugarShortCircuit (program : Program) : Program :=
let imperativeCallees := (program.staticProcedures.filter (!·.isFunctional)).map (·.name.text)
mapProgram (mapStmtExpr (desugarShortCircuitNode imperativeCallees)) program

end -- public section

/-- Pipeline pass: desugar short-circuit operators. -/
public def desugarShortCircuitPass : LaurelPass where
public def desugarShortCircuitPass : LoweringPass where
name := "DesugarShortCircuit"
documentation := "Rewrites short-circuit boolean operators (`&&` and `||`) into equivalent conditional expressions. This simplifies subsequent passes and the final translation to Core, which does not have short-circuit semantics built in."
run := fun p m =>
(desugarShortCircuit m p, [], {})
run := fun p _ _ =>
(desugarShortCircuit p, [], {})
comesBefore := [
liftExpressionAssignmentsPass, "The desugar short circuit pass introduces if-then-else expressions whose control-flow must be taken into account by the lifting pass."⟩]
liftImperativeExpressionsPass.meta, "The desugar short circuit pass introduces if-then-else expressions whose control-flow must be taken into account by the lifting pass."⟩]

end Strata.Laurel
4 changes: 2 additions & 2 deletions Strata/Languages/Laurel/EliminateDeterministicHoles.lean
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ def eliminateDeterministicHoles (program : Program) : Program × Statistics :=
end -- public section

/-- Pipeline pass: eliminate deterministic holes. -/
public def eliminateDeterministicHolesPass : LaurelPass where
public def eliminateDeterministicHolesPass : LoweringPass where
name := "EliminateDeterministicHoles"
documentation := "Replaces every deterministic hole with a call to a freshly generated uninterpreted function. After this pass the program contains only non-deterministic holes. Assumes `InferHoleTypes` has already annotated holes with types."
run := fun p _m =>
run := fun p _m _ =>
let (p', stats) := eliminateDeterministicHoles p
(p', [], stats)

Expand Down
4 changes: 2 additions & 2 deletions Strata/Languages/Laurel/EliminateDoWhile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def eliminateDoWhile (program : Program) : Program :=
(mapProgramProceduresM rewrite program |>.run {}).fst

/-- Pipeline pass: eliminate post-test (`do … while`) loops. -/
public def eliminateDoWhilePass : LaurelPass where
public def eliminateDoWhilePass : LoweringPass where
name := "EliminateDoWhile"
documentation := "Lowers post-test `While` loops (the `do … while` form) into the pre-test loop `{ while(true) invariant I { BODY; if (!COND) exit L } } L`, with a fresh `$`-prefixed exit label `L`. Runs early so no later pass observes a post-test loop; the invariant is checked at the loop head, matching `while`."
run := fun p _m => (eliminateDoWhile p, [], {})
run := fun p _m _ => (eliminateDoWhile p, [], {})

end -- public section
end Strata.Laurel
4 changes: 2 additions & 2 deletions Strata/Languages/Laurel/EliminateIncrDecr.lean
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ def eliminateIncrDecr (program : Program) : Program :=
mapProgramProcedures lowerProcedure program

/-- Pipeline pass: eliminate increment/decrement operators. -/
public def eliminateIncrDecrPass : LaurelPass where
public def eliminateIncrDecrPass : LoweringPass where
name := "EliminateIncrDecr"
documentation := "Lowers Java-style increment/decrement operators (`++x`, `x++`, `--x`, `x--`) into existing Laurel assignment and arithmetic constructs. Prefix forms yield the new value; postfix forms yield the old value. Runs early so that no later pass observes an `.IncrDecr` node."
run := fun p _m => (eliminateIncrDecr p, [], {})
run := fun p _m _ => (eliminateIncrDecr p, [], {})

end -- public section
end Strata.Laurel
83 changes: 83 additions & 0 deletions Strata/Languages/Laurel/EliminateReturnStatements.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/-
Copyright Strata Contributors

SPDX-License-Identifier: Apache-2.0 OR MIT
-/
module

public import Strata.Languages.Laurel.MapStmtExpr
public import Strata.Languages.Laurel.LaurelPass

/-!
# Eliminate Return Statements

Replaces `return` statements in imperative procedure bodies with assignments
to the output parameters followed by an `exit` to a labelled block that wraps
the entire body. This ensures that code placed after the body block (e.g.,
postcondition assertions inserted by the contract pass) is always reached.

This pass should run after `EliminateReturnsInExpression` (which handles
functional/expression-position returns) and before the contract pass.
-/

namespace Strata.Laurel

public section

private def returnLabel : String := "$return"




/-- Transform a single procedure: wrap body in a labelled block and replace returns. -/
private def eliminateReturnStmts (proc : Procedure) : Procedure :=
match proc.body with
| .Opaque postconds (some impl) mods =>
let impl' := replaceReturn proc.outputs impl
let wrapped := match impl'.val with
| .Block stmts none => ⟨.Block stmts (some returnLabel), impl'.source⟩
| _ => ⟨ .Block [impl'] (some returnLabel), proc.name.source ⟩
{ proc with body := .Opaque postconds (some wrapped) mods }
| .Transparent body =>
let body' := replaceReturn proc.outputs body
let wrapped := match body'.val with
| .Block stmts none => ⟨.Block stmts (some returnLabel), body'.source⟩
| _ => ⟨ .Block [body'] (some returnLabel), proc.name.source ⟩
{ proc with body := .Transparent wrapped }
| _ => proc
where

/-- Replace `Return val` with `output := val; exit "$return"` (or just `exit`
for valueless returns). Uses `mapStmtExpr` for bottom-up traversal. -/
replaceReturn (outputs : List Parameter) (expr : StmtExprMd) : StmtExprMd :=
mapStmtExpr (fun e =>
match e.val with
| .Return (some val) =>
/- Handling valued return is required because the heap param pass introduces valued return in
Strata/Languages/Laurel/HeapParameterizationConstants.lean
We should change that so we can remove this case.
-/
match outputs with
| [out] =>
let assign := ⟨ .Assign [⟨ .Local out.name, expr.source ⟩] val, expr.source ⟩
let exit := ⟨ .Exit returnLabel, expr.source ⟩
⟨.Block [assign, exit] none, e.source⟩
| _ => ⟨ .Exit returnLabel, expr.source ⟩
| .Return none => ⟨ .Exit returnLabel, expr.source ⟩
| _ => e) expr

/-- Transform a program by eliminating return statements in all procedure bodies. -/
def eliminateReturnStatements (program : Program) : Program :=
{ program with staticProcedures := program.staticProcedures.map eliminateReturnStmts }

public def eliminateReturnStatementsPass : LoweringPass where
name := "EliminateReturnStatements"
documentation := "Lower return statements to exit statements. Wrap each procedure body with a 'return' block"
run := fun p _m _ =>
let p' := eliminateReturnStatements p
(p', [], {})
-- comesBefore := [contractPass]

end -- public section

end Strata.Laurel
Loading
Loading