Skip to content

Instantly share code, notes, and snippets.

@dunhamsteve
Last active September 6, 2022 13:37
Show Gist options
  • Save dunhamsteve/9338f18df1a58b64f3376b5ac2cca9cd to your computer and use it in GitHub Desktop.
Save dunhamsteve/9338f18df1a58b64f3376b5ac2cca9cd to your computer and use it in GitHub Desktop.

Revisions

  1. dunhamsteve revised this gist Apr 13, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion DTComp2.idr
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,6 @@ excludedMiddle b with (eval b)
    _ | True = Left Refl
    _ | False = Right Refl

    partial
    correct : (e : Exp t) -> (s : Stack ts) -> exec (compile e) s = eval e |> s
    correct (Val x) s = Refl
    correct (Add x y) s =
  2. dunhamsteve revised this gist Apr 13, 2022. 1 changed file with 59 additions and 0 deletions.
    59 changes: 59 additions & 0 deletions DTComp2.idr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    -- https://www.cs.nott.ac.uk/~psymp4/files/dependently-typed-compilers.pdf
    data Exp : Type -> Type where
    Val : t -> Exp t
    Add : Exp Nat -> Exp Nat -> Exp Nat
    If : Eq t => Exp Bool -> Exp t -> Exp t -> Exp t

    eval : Exp t -> t
    eval (Val x) = x
    eval (Add x y) = eval x + eval y
    eval (If x y z) = if eval x then eval y else eval z

    data Stack : List Type -> Type where
    Eps : Stack []
    (|>) : t -> Stack ts -> Stack (t :: ts)

    infixr 7 |>

    data Code : List Type -> List Type -> Type where
    PUSH : t -> Code ts (t :: ts)
    IF : Code s s' -> Code s s' -> Code (Bool :: s) s'
    ADD : Code (Nat :: Nat :: s) (Nat :: s)
    (+++) : Code s s' -> Code s' s'' -> Code s s''

    infixl 7 +++

    exec : Code s s' -> Stack s -> Stack s'
    exec (PUSH x) s = x |> s
    exec (IF c1 c2) (b |> s) = if b then exec c1 s else exec c2 s
    exec ADD (m |> n |> s) = (n + m) |> s
    exec (c1 +++ c2) s = exec c2 (exec c1 s)

    compile : Exp t -> Code s (t :: s)
    compile (Val x) = PUSH x
    compile (Add x y) = compile x +++ compile y +++ ADD
    compile (If b x y) = compile b +++ IF (compile x) (compile y)

    distributeIf : (b : Bool ) -> (t : ty) -> (e : ty) -> (f : ty -> ty2) -> f (if b then t else e) = if b then f t else f e
    distributeIf False t e f = Refl
    distributeIf True t e f = Refl

    excludedMiddle : (b : _) -> Either (eval b === True) (eval b === False)
    excludedMiddle b with (eval b)
    _ | True = Left Refl
    _ | False = Right Refl

    partial
    correct : (e : Exp t) -> (s : Stack ts) -> exec (compile e) s = eval e |> s
    correct (Val x) s = Refl
    correct (Add x y) s =
    rewrite correct x s in
    rewrite correct y (eval x |> s) in
    Refl
    correct (If b x y) s =
    rewrite correct b s in
    case excludedMiddle b of
    Left prf => rewrite prf in correct x s
    Right prf => rewrite prf in correct y s


  3. dunhamsteve revised this gist Mar 13, 2022. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions DTComp.idr
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    -- https://www.cs.nott.ac.uk/~psymp4/files/dependently-typed-compilers.pdf

    data Exp : Type -> Type where
    Val : t -> Exp t
    Add : Exp Nat -> Exp Nat -> Exp Nat
    @@ -7,7 +8,7 @@ data Exp : Type -> Type where
    eval : Exp t -> t
    eval (Val x) = x
    eval (Add x y) = eval x + eval y
    eval (If x y z) = if eval x then eval y else eval z
    eval (If x y z) = ifThenElse (eval x) (eval y) (eval z)

    data Stack : List Type -> Type where
    Eps : Stack []
    @@ -25,7 +26,7 @@ infixl 7 +++

    exec : Code s s' -> Stack s -> Stack s'
    exec (PUSH x) s = x |> s
    exec (IF c1 c2) (b |> s) = if b then exec c1 s else exec c2 s
    exec (IF c1 c2) (b |> s) = ifThenElse b (exec c1 s) (exec c2 s)
    exec ADD (m |> n |> s) = (n + m) |> s
    exec (c1 +++ c2) s = exec c2 (exec c1 s)

    @@ -34,11 +35,20 @@ compile (Val x) = PUSH x
    compile (Add x y) = compile x +++ compile y +++ ADD
    compile (If b x y) = compile b +++ IF (compile x) (compile y)

    correct : (e : Exp t) -> (s : Stack ts) -> exec (compile e) s = eval e |> s
    distributeIf : (f : t1 -> t2) -> (b : Bool) -> (x : t1) -> (y : t1) ->
    f (ifThenElse b x y) = ifThenElse b (f x) (f y)
    distributeIf _ False x y = Refl
    distributeIf _ True x y = Refl

    correct : (e : Exp t) -> (s : Stack ts) -> exec (compile e) s = eval e |> s
    correct (Val x) s = Refl
    correct (Add x y) s =
    rewrite correct x s in
    rewrite correct y (eval x |> s) in
    Refl
    correct (If b x y) s =
    rewrite correct b s in ?foo
    rewrite correct b s in
    rewrite distributeIf (|> s) (eval b) (eval x) (eval y) in
    rewrite correct x s in
    rewrite correct y s in
    Refl
  4. dunhamsteve created this gist Mar 13, 2022.
    44 changes: 44 additions & 0 deletions DTComp.idr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    -- https://www.cs.nott.ac.uk/~psymp4/files/dependently-typed-compilers.pdf
    data Exp : Type -> Type where
    Val : t -> Exp t
    Add : Exp Nat -> Exp Nat -> Exp Nat
    If : Eq t => Exp Bool -> Exp t -> Exp t -> Exp t

    eval : Exp t -> t
    eval (Val x) = x
    eval (Add x y) = eval x + eval y
    eval (If x y z) = if eval x then eval y else eval z

    data Stack : List Type -> Type where
    Eps : Stack []
    (|>) : t -> Stack ts -> Stack (t :: ts)

    infixr 7 |>

    data Code : List Type -> List Type -> Type where
    PUSH : t -> Code ts (t :: ts)
    IF : Code s s' -> Code s s' -> Code (Bool :: s) s'
    ADD : Code (Nat :: Nat :: s) (Nat :: s)
    (+++) : Code s s' -> Code s' s'' -> Code s s''

    infixl 7 +++

    exec : Code s s' -> Stack s -> Stack s'
    exec (PUSH x) s = x |> s
    exec (IF c1 c2) (b |> s) = if b then exec c1 s else exec c2 s
    exec ADD (m |> n |> s) = (n + m) |> s
    exec (c1 +++ c2) s = exec c2 (exec c1 s)

    compile : Exp t -> Code s (t :: s)
    compile (Val x) = PUSH x
    compile (Add x y) = compile x +++ compile y +++ ADD
    compile (If b x y) = compile b +++ IF (compile x) (compile y)

    correct : (e : Exp t) -> (s : Stack ts) -> exec (compile e) s = eval e |> s
    correct (Val x) s = Refl
    correct (Add x y) s =
    rewrite correct x s in
    rewrite correct y (eval x |> s) in
    Refl
    correct (If b x y) s =
    rewrite correct b s in ?foo