-
Notifications
You must be signed in to change notification settings - Fork 6
/
Hutton.agda
74 lines (54 loc) · 1.5 KB
/
Hutton.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Hutton where
open import Ex1Prelude
open import Ex2Prelude
{-
data Nat : Set where
zero : Nat
suc : Nat -> Nat
{-# BUILTIN NATURAL Nat #-}
data List (X : Set) : Set where
[] : List X
_:>_ : X -> List X -> List X
infixr 5 _:>_
_+_ : Nat -> Nat -> Nat
zero + y = y
suc x + y = suc (x + y)
-}
data HExp : Set where
val : Nat -> HExp
_+++_ : HExp -> HExp -> HExp
{-
id : {X : Set} -> X -> X
id x = x
-}
eval : HExp -> Nat
eval (val n) = id n
eval (d +++ e) = eval d + eval e
leaves : HExp -> List Nat
leaves (val n) = n :> []
leaves (d +++ e) = {!leaves d ++ leaves e!}
data HCode : List One -> List One -> Set where
PUSH : {i : List One} -> Nat -> HCode i (<> :> i)
ADD : {i : List One} -> HCode (<> :> <> :> i) (<> :> i)
_-SEQ-_ : {i j k : List One} -> HCode i j -> HCode j k -> HCode i k
infixr 3 _-SEQ-_
{-
data Vec (X : Set) : Nat -> Set where
[] : Vec X zero
_,_ : forall {n} -> X -> Vec X n -> Vec X (suc n)
-}
Stk : List One -> Set
Stk xs = All (\ _ -> Nat) xs
exec : {i j : List One} -> HCode i j -> Stk i -> Stk j
exec (PUSH x) s = x , s
exec ADD (x , (y , s)) = (y + x) , s
exec (h -SEQ- k) s = exec k (exec h s)
exec' : (i j : List One) -> HCode i j -> Stk i -> Stk j
exec' i .(<> :> i) (PUSH x) s = x , s
exec' .(<> :> <> :> i) .(<> :> i) (ADD {i}) (y , (x , s)) = (x + y) , s
exec' i j (c -SEQ- c') s = {!exec' _ j c' (exec' i _ c s)!}
{-
compile : HExp -> {i : Nat} -> HCode i (suc i)
compile (val n) = PUSH n
compile (d +++ e) = compile d -SEQ- compile e -SEQ- ADD
-}