-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCond.agda
213 lines (171 loc) · 6.07 KB
/
Cond.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
------------------------------------------------------------------------
-- Calculation for a simple arithmetic expression language extended
-- with if-then-else.
------------------------------------------------------------------------
module Cond where
open import Data.Nat
open import Data.Bool
open import Data.List hiding (head)
open import Relation.Binary.PropositionalEquality hiding ([_])
---------------------
-- Source language --
---------------------
data Expr : Set where
Val : ℕ → Expr
Add : Expr → Expr → Expr
If : Expr → Expr → Expr → Expr
eval : Expr → ℕ
eval (Val n) = n
eval (Add x y) = eval x + eval y
eval (If x y z) = if eval x ≡ᵇ 0 then eval z else eval y
--------------------------------
-- Tree-based target language --
--------------------------------
Stack : Set
Stack = List ℕ
data Code : Set where
HALT : Code
PUSH : ℕ → Code → Code
ADD : Code → Code
JPZ : Code → Code → Code
exec : Code → Stack → Stack
exec HALT s = s
exec (PUSH n c) s = exec c (n ∷ s)
exec (ADD c) (n ∷ m ∷ s) = exec c ((m + n) ∷ s)
exec (JPZ c' c) (n ∷ s) = if n ≡ᵇ 0 then exec c' s else exec c s
exec _ _ = []
-------------------------
-- Tree-based compiler --
-------------------------
comp : Expr → Code → Code
comp (Val n) c = PUSH n c
comp (Add x y) c = comp x (comp y (ADD c))
comp (If x y z) c = comp x (JPZ (comp z c) (comp y c))
compile : Expr → Code
compile e = comp e HALT
----------------------------------------
-- Calculation of tree-based compiler --
----------------------------------------
open ≡-Reasoning
-- specification and calculation of comp
spec-comp : ∀ x {s c} →
exec c (eval x ∷ s) ≡ exec (comp x c) s
spec-comp (Val x) {s} {c} =
exec c (eval (Val x) ∷ s)
≡⟨⟩
(exec (PUSH x c) s)
∎
spec-comp (Add x y) {s} {c} =
exec c (eval (Add x y) ∷ s)
≡⟨⟩
exec c ((eval x + eval y) ∷ s)
≡⟨⟩
exec (ADD c) (eval y ∷ eval x ∷ s)
≡⟨ spec-comp y ⟩
exec (comp y (ADD c)) (eval x ∷ s)
≡⟨ spec-comp x ⟩
exec (comp x (comp y (ADD c))) s
∎
spec-comp (If x y z) {s} {c} =
exec c (eval (If x y z) ∷ s)
≡⟨⟩
exec c ((if eval x ≡ᵇ 0 then eval z else eval y) ∷ s)
≡⟨ exec-if {c} ⟩
(if eval x ≡ᵇ 0 then exec c (eval z ∷ s) else exec c (eval y ∷ s))
≡⟨ cong₂ (if_then_else_ (eval x ≡ᵇ 0)) (spec-comp z) (spec-comp y) ⟩
(if eval x ≡ᵇ 0 then exec (comp z c) s else exec (comp y c) s)
≡⟨⟩
(exec (JPZ (comp z c) (comp y c)) (eval x ∷ s))
≡⟨ spec-comp x ⟩
(exec (comp x (JPZ (comp z c) (comp y c))) s)
∎
where
exec-if : ∀ {c b x y s} → exec c ((if b then x else y) ∷ s) ≡ (if b then exec c (x ∷ s) else exec c (y ∷ s))
exec-if {c} {false} = refl
exec-if {c} {true} = refl
-- specification and calculation of compile
spec-compile : ∀ x {s} →
eval x ∷ s ≡ exec (compile x) s
spec-compile x {s} =
eval x ∷ s
≡⟨⟩
exec HALT (eval x ∷ s)
≡⟨ spec-comp x ⟩
exec (comp x HALT) s
∎
---------------------------------
-- Graph-based target language --
---------------------------------
data Codeᵍ (l : Set) : Set where
PUSHᵍ : ℕ → Codeᵍ l → Codeᵍ l
ADDᵍ : Codeᵍ l → Codeᵍ l
JPZᵍ : l → Codeᵍ l → Codeᵍ l
HALTᵍ : Codeᵍ l
JMPᵍ : l → Codeᵍ l
LABᵍ : (l → Codeᵍ l) → Codeᵍ l → Codeᵍ l
⦅_⦆ : Codeᵍ Code → Code
⦅ HALTᵍ ⦆ = HALT
⦅ PUSHᵍ n c ⦆ = PUSH n ⦅ c ⦆
⦅ ADDᵍ c ⦆ = ADD ⦅ c ⦆
⦅ JPZᵍ l c ⦆ = JPZ l ⦅ c ⦆
⦅ JMPᵍ l ⦆ = l
⦅ LABᵍ f c ⦆ = ⦅ f ⦅ c ⦆ ⦆
--------------------------
-- Graph-based compiler --
--------------------------
compᵍ : ∀ {l} → Expr → Codeᵍ l → Codeᵍ l
compᵍ (Val x) c = PUSHᵍ x c
compᵍ (Add x y) c = compᵍ x (compᵍ y ( ADDᵍ c ))
compᵍ (If x y z) c = compᵍ x (LABᵍ (λ l → LABᵍ (λ l' →
JPZᵍ l' (compᵍ y (JMPᵍ l))) (compᵍ z (JMPᵍ l))) c)
compileᵍ : Expr → (∀ {l} → Codeᵍ l)
compileᵍ e = compᵍ e HALTᵍ
-----------------------------------------
-- Calculation of graph-based compiler --
-----------------------------------------
-- specification and calculation of compᵍ
spec-compᵍ : ∀ x {c} → comp x ⦅ c ⦆ ≡ ⦅ compᵍ x c ⦆
spec-compᵍ (Val x) {c} =
(PUSH x ⦅ c ⦆)
≡⟨⟩
⦅ PUSHᵍ x c ⦆
∎
spec-compᵍ (Add x y) {c} =
comp x (comp y (ADD ⦅ c ⦆))
≡⟨⟩
comp x (comp y ⦅ ADDᵍ c ⦆)
≡⟨ cong (comp x) (spec-compᵍ y) ⟩
comp x ⦅ compᵍ y ( ADDᵍ c )⦆
≡⟨ spec-compᵍ x ⟩
⦅ compᵍ x (compᵍ y ( ADDᵍ c ))⦆
∎
spec-compᵍ (If x y z) {c} =
comp x (JPZ (comp z ⦅ c ⦆) (comp y ⦅ c ⦆))
≡⟨⟩
comp x ((λ l → (JPZ (comp z l) (comp y l))) ⦅ c ⦆)
≡⟨⟩
comp x ((λ l → (JPZ (comp z ⦅ JMPᵍ l ⦆) (comp y ⦅ JMPᵍ l ⦆))) ⦅ c ⦆)
≡⟨ cong₂ (λ h h' → comp x ((λ l → JPZ h h') ⦅ c ⦆)) (spec-compᵍ z) (spec-compᵍ y) ⟩
comp x ((λ l → (JPZ ⦅ compᵍ z (JMPᵍ l) ⦆ ⦅ compᵍ y (JMPᵍ l) ⦆)) ⦅ c ⦆)
≡⟨⟩
comp x ((λ l → (λ l' → (JPZ l' ⦅ compᵍ y (JMPᵍ l) ⦆)) ⦅ compᵍ z (JMPᵍ l) ⦆) ⦅ c ⦆)
≡⟨⟩
comp x ((λ l → (λ l' → ⦅ JPZᵍ l' (compᵍ y (JMPᵍ l)) ⦆) ⦅ compᵍ z (JMPᵍ l) ⦆) ⦅ c ⦆)
≡⟨⟩
comp x ((λ l → ⦅ LABᵍ (λ l' → JPZᵍ l' (compᵍ y (JMPᵍ l))) (compᵍ z (JMPᵍ l))⦆) ⦅ c ⦆)
≡⟨⟩
(comp x ⦅ LABᵍ (λ l → LABᵍ (λ l' → JPZᵍ l' (compᵍ y (JMPᵍ l))) (compᵍ z (JMPᵍ l))) c ⦆)
≡⟨ spec-compᵍ x ⟩
⦅ compᵍ x (LABᵍ (λ l → (LABᵍ (λ l' → (JPZᵍ l' (compᵍ y (JMPᵍ l)))) (compᵍ z (JMPᵍ l)))) c) ⦆
∎
-- specification and calculation of compileᵍ
spec-compileᵍ : ∀ x → compile x ≡ ⦅ compileᵍ x ⦆
spec-compileᵍ x =
compile x
≡⟨⟩
comp x HALT
≡⟨⟩
comp x HALT
≡⟨ spec-compᵍ x ⟩
⦅ compᵍ x HALTᵍ ⦆
∎