Skip to content

Commit

Permalink
Mem
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiiyya committed Oct 30, 2024
1 parent 5e26488 commit d27ca84
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions LeanBoogie/Boog.lean
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Std
import Aesop

namespace Boogie

open Std (HashSet HashMap)

/-
Expand Down
2 changes: 1 addition & 1 deletion LeanBoogie/BoogieDsl.lean
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Std
import Qq
import LeanBoogie.Boog

namespace Boog
namespace Boogie
open Lean Elab Meta Qq
open Std (HashSet HashMap)

Expand Down
21 changes: 21 additions & 0 deletions LeanBoogie/ITree/Mem.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import LeanBoogie.ITree.ITree
import LeanBoogie.ITree.Eutt
import LeanBoogie.ITree.Monad

/-
# Memory effects
-/

example : Ans = Int := rfl -- currently our ITrees have the answer type hardcoded as Int.

/-- Memory events: Reading or writing of a variable. -/
inductive MemEv
| read : String -> MemEv
| write : String -> Int -> MemEv

abbrev Mem : Type -> Type := ITree MemEv

def Mem.set (a : String) (b : Int) : Mem Unit := .vis (.write a b) (fun _unit => .ret ())
def Mem.get (v : String) : Mem Int := .vis (.read v) (fun ans => .ret ans)

-- TODO: interpreting memory events away into the boogie state monad.
16 changes: 16 additions & 0 deletions LeanBoogie/ITree/Monad.lean
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import LeanBoogie.ITree.ITree
import LeanBoogie.ITree.Eutt

/-
# `ITree` forms a monad
-/

def ITree.pure (a : A) : ITree E A := .ret a

def ITree.bind (ta : ITree E A) (tb : A -> ITree E B) : ITree E B :=
Expand All @@ -15,3 +19,15 @@ instance : Monad (ITree E) where
bind := ITree.bind

#check LawfulMonad

/- ## Helpers for writing imperative programs -/

def ITree.skip : ITree E Unit := .ret ()
def ITree.seq (a b : ITree E Unit) : ITree E Unit := bind a (fun () => b)
def ITree.trigger (e : E) : ITree E Int := .vis e (fun ans => .ret ans)

def ITree.ite (c : ITree E Bool) (t e : ITree E Unit) : ITree E Unit
:= bind c (fun c => if c then t else e)

abbrev ITree.ifthen (c : ITree E Bool) (t : ITree E Unit) : ITree E Unit
:= ite c t skip

0 comments on commit d27ca84

Please sign in to comment.