Skip to content

Commit 6b8946e

Browse files
committed
changed eval to evaluate loops using continuations
1 parent d12d193 commit 6b8946e

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

Pullback/UnquoteTy/SSA.lean

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ theorem Array.find?_eq_getElem_findFinIdx? {α : Type u} (xs : Array α) (p : α
152152
· grind
153153

154154
#check ForIn
155-
-- (a : α, true) means break (a : α, false) means continue
156-
def SSA.loop {α : Type u} [Inhabited α] (init : α) (step : α → (α → α) → α) : α := sorry
155+
def SSA.loop {α β : Type u} {m : Type u → Type v} [Monad m] [Inhabited α] (init : α) (step : α → (α → m β) → m β) : m β := sorry
157156

158157
private def SSABaseType.decEq : (ty : SSABaseType) → DecidableEq ty.type
159158
| float => by
@@ -186,7 +185,7 @@ def SSAConst.interp : (e : SSAConst) → (e.inferType).type
186185
| ofInt i => i
187186
| ofUnit () => ()
188187
| ifthenelse ty => fun c t e => if (cast (by simp [SSAType.type, SSABaseType.type]) c : Int) != 0 then t else e
189-
| loop ty => SSA.loop (α := ty.type)
188+
| loop ty => SSA.loop (α := ty.type) (m := Id)
190189
| prod α β => (@Prod.mk α.type β.type)
191190
| prod₁ α β => fun ab => ab.1
192191
| prod₂ α β => fun ab => ab.2
@@ -414,6 +413,9 @@ inductive DoResult (α : Type) where
414413
/- early return-/
415414
| return (a : SSAConst) : DoResult α
416415
| pure (a : α) : DoResult α
416+
deriving Inhabited
417+
418+
instance : Inhabited (DoResult (if (!inloop) = true then SSAConst else LoopStep)) := sorry
417419

418420
#check StateT.run_modify
419421

@@ -444,16 +446,16 @@ def SSADo.eval (args : Array (Name × SSAConst)) (inloop : Bool := false) : SSAD
444446
set (mutvars.set idx (var, ← val.eval args))
445447
rest.eval args inloop
446448
| loop body rest => do
447-
while true do
449+
SSA.loop Unit.unit (fun x kcontinue => do
448450
match (← body.eval args true) with
449-
| .return x => return .return x
451+
| .return x => pure (DoResult.return x)
450452
| .pure x =>
451453
match x with
452-
| .continue x => set x
454+
| .continue x => set x; kcontinue ()
453455
| .break x =>
454456
set x
455-
break
456-
rest.eval args inloop
457+
rest.eval args inloop
458+
)
457459
| .break => do if h : inloop then some (DoResult.pure (cast (by grind) (LoopStep.break (← get)))) else none
458460
| .continue => do if h : inloop then some (DoResult.pure (cast (by grind) (LoopStep.continue (← get)))) else none
459461
| .return x => do some (DoResult.return (← x.eval args))

0 commit comments

Comments
 (0)