Add post about the typesafe imperative language.
This commit is contained in:
47
code/typesafe-imperative/TypesafeImp.idr
Normal file
47
code/typesafe-imperative/TypesafeImp.idr
Normal file
@@ -0,0 +1,47 @@
|
||||
data Reg = A | B | R
|
||||
|
||||
data Ty = IntTy | BoolTy
|
||||
|
||||
RegState : Type
|
||||
RegState = (Ty, Ty, Ty)
|
||||
|
||||
getRegTy : Reg -> RegState -> Ty
|
||||
getRegTy A (a, _, _) = a
|
||||
getRegTy B (_, b, _) = b
|
||||
getRegTy R (_, _, r) = r
|
||||
|
||||
setRegTy : Reg -> Ty -> RegState -> RegState
|
||||
setRegTy A a (_, b, r) = (a, b, r)
|
||||
setRegTy B b (a, _, r) = (a, b, r)
|
||||
setRegTy R r (a, b, _) = (a, b, r)
|
||||
|
||||
data Expr : RegState -> Ty -> Type where
|
||||
Lit : Int -> Expr s IntTy
|
||||
Load : (r : Reg) -> Expr s (getRegTy r s)
|
||||
Add : Expr s IntTy -> Expr s IntTy -> Expr s IntTy
|
||||
Leq : Expr s IntTy -> Expr s IntTy -> Expr s BoolTy
|
||||
Not : Expr s BoolTy -> Expr s BoolTy
|
||||
|
||||
mutual
|
||||
data Stmt : RegState -> RegState -> Type where
|
||||
Store : (r : Reg) -> Expr s t -> Stmt s (setRegTy r t s)
|
||||
If : Expr s BoolTy -> Prog s n -> Prog s n -> Stmt s n
|
||||
Loop : Prog s s -> Stmt s s
|
||||
|
||||
data Prog : RegState -> RegState -> Type where
|
||||
Nil : Prog s s
|
||||
Break : Prog s s
|
||||
(::) : Stmt s n -> Prog n m -> Prog s m
|
||||
|
||||
initialState : RegState
|
||||
initialState = (IntTy, IntTy, IntTy)
|
||||
|
||||
testProg : Prog Main.initialState Main.initialState
|
||||
testProg =
|
||||
[ Store A (Lit 1 `Leq` Lit 2)
|
||||
, If (Load A)
|
||||
[ Store A (Lit 1) ]
|
||||
[ Store A (Lit 2) ]
|
||||
, Store B (Lit 2)
|
||||
, Store R (Add (Load A) (Load B))
|
||||
]
|
||||
Reference in New Issue
Block a user