-
Notifications
You must be signed in to change notification settings - Fork 0
/
While.agda
308 lines (244 loc) · 10.1 KB
/
While.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
{-# OPTIONS --sized-types #-}
------------------------------------------------------------------------
-- Calculation for a simple arithmetic expression language extended
-- with While loops.
------------------------------------------------------------------------
module While where
open import Code public
open import Partial
open import Data.Nat
open import Data.Fin hiding (_+_)
open import Data.Fin.Patterns
open import Data.Bool
open import Data.List hiding (head)
open import Data.Vec hiding (_>>=_)
open import Relation.Binary.PropositionalEquality hiding ([_])
---------------------
-- Source language --
---------------------
data Expr : Set where
Val : ℕ → Expr
Add : Expr → Expr → Expr
While : Expr → Expr → Expr
syntax Add x y = x ⊕ y
mutual
eval : ∀ {i} → Expr → Partial ℕ i
eval (Val x) = return x
eval (Add x y) =
do n ← eval x
m ← eval y
return (n + m)
eval (While x y) = do n ← eval x
if n ≡ᵇ 0 then return 0
else eval y >> later (∞eval (While x y))
∞eval : ∀ {i} → Expr → ∞Partial ℕ i
force (∞eval x) = eval x
--------------------------------
-- Tree-based target language --
--------------------------------
data Instr : ℕ → Set where
PUSH' : ℕ → Instr 0
ADD' : Instr 0
POP' : Instr 0
JPZ' : Instr 1
-- Constructors for `Code` type
pattern PUSH n c = PUSH' n ! ▸ c
pattern ADD c = ADD' ! ▸ c
pattern POP c = POP' ! ▸ c
pattern JPZ l c = JPZ' ! @ l ▸ c
Stack : Set
Stack = List ℕ
mutual
exec : ∀ {i} → Code Instr ∞ → Stack → Partial Stack i
exec (PUSH n c) s = exec c (n ∷ s)
exec (ADD c) (n ∷ m ∷ s) = exec c ((m + n) ∷ s)
exec (POP c) (_ ∷ s) = exec c s
exec (JPZ c' c) (n ∷ s) = if n ≡ᵇ 0 then exec c' (0 ∷ s) else exec c s
exec (REC c) s = later (∞exec c s)
exec HALT s = return s
exec _ _ = return []
∞exec : ∀ {i} → ∞Code Instr ∞ → Stack → ∞Partial Stack i
force (∞exec c x) = exec (cforce c) x
-------------------------
-- Tree-based compiler --
-------------------------
mutual
comp : ∀ {i} → Expr → Code Instr i → Code Instr i
comp (Val n) c = PUSH n c
comp (Add x y) c = comp x (comp y (ADD c))
comp (While x y) c = comp x (JPZ c (comp y (POP (REC (∞comp (While x y) c)))))
∞comp : ∀ {i} → Expr → Code Instr i → ∞Code Instr i
cforce (∞comp x c) = comp x c
compile : ∀ {i} → Expr → Code Instr i
compile e = comp e HALT
----------------------------------------
-- Calculation of tree-based compiler --
----------------------------------------
-- lemma for the calculation
ifzero-cong : ∀ {i A} {p1 p2 q1 q2 : Partial A ∞} n
→ p1 ~[ i ] q1 → p2 ~[ i ] q2
→ (if n ≡ᵇ 0 then p1 else p2) ~[ i ] (if n ≡ᵇ 0 then q1 else q2)
ifzero-cong zero b1 b2 = b1
ifzero-cong (suc n) b1 b2 = b2
module TreeComp where
open ~i-Calculation
-- specification and calculation of comp
spec-comp : ∀ i x {s c} →
(do v ← eval x; exec c (v ∷ s)) ~[ i ] exec (comp x c) s
spec-comp zero _ = ~izero
spec-comp i (Val x) {s} {c} =
(do v ← eval (Val x); exec c (v ∷ s))
≡⟨⟩
exec (PUSH x c) s
∎
spec-comp i (Add x y) {s} {c} =
(do v ← eval (Add x y); exec c (v ∷ s))
≡⟨⟩
(do v <- (do n ← eval x
m ← eval y
return (n + m))
exec c (v ∷ s))
~⟨ ~i>>=-assoc (eval x) ⟩
(do n ← eval x
v <- (do m ← eval y
return (n + m))
exec c (v ∷ s))
~⟨ ~i>>=-cong-r (eval x) (\ n -> ~i>>=-assoc (eval y)) ⟩
(do n ← eval x
m ← eval y
v <- (return (n + m))
exec c (v ∷ s))
≡⟨⟩
(do n ← eval x
m ← eval y
exec c ((n + m) ∷ s))
≡⟨⟩
(do n ← eval x
m ← eval y
exec (ADD c) (m ∷ n ∷ s))
~⟨ ~i>>=-cong-r (eval x) (\ n' → spec-comp i y) ⟩
(do n ← eval x
exec (comp y (ADD c)) (n ∷ s))
~⟨ spec-comp i x ⟩
exec (comp x (comp y (ADD c))) s
∎
spec-comp i@(suc j) (While x y) {s} {c} =
(do v ← eval (While x y) ; exec c (v ∷ s))
≡⟨⟩
(do v ← do n ← eval x ; if n ≡ᵇ 0 then return 0 else eval y >> later (∞eval (While x y))
exec c (v ∷ s))
~⟨ (~i>>=-assoc' (eval x) λ { zero → ~irefl ; (suc n) → ~i>>=-assoc (eval y)})⟩
(do n ← eval x
if n ≡ᵇ 0 then exec c (0 ∷ s) else do eval y ; later (∞eval (While x y) ∞>>= λ m → exec c (m ∷ s)))
~⟨ (~i>>=-cong-r (eval x) λ n → ifzero-cong n ~irefl (~i>>=-cong-r (eval y) λ _ → ~ilater (spec-comp j (While x y) {s} {c}))) ⟩
(do n ← eval x
if n ≡ᵇ 0 then exec c (0 ∷ s) else do eval y ; later (∞exec (∞comp (While x y) c) s))
~⟨ (((~i>>=-cong-r (eval x) λ { zero → ~irefl ; (suc n) → ~i>>=-cong-r (eval y) λ _ → ~ilater ~irefl}))) ⟩
(do n ← eval x
if n ≡ᵇ 0 then exec c (0 ∷ s) else do eval y ; exec (REC (∞comp (While x y) c)) s)
≡⟨⟩
(do n ← eval x
if n ≡ᵇ 0 then exec c (0 ∷ s) else do m ← eval y ; exec (POP (REC (∞comp (While x y) c))) (m ∷ s))
~⟨ ((~i>>=-cong-r (eval x) λ { zero → ~irefl ; (suc n) → spec-comp i y })) ⟩
(do n ← eval x
if n ≡ᵇ 0 then exec c (0 ∷ s) else do exec (comp y (POP (REC (∞comp (While x y) c)))) s)
~⟨ (((~i>>=-cong-r (eval x) λ { zero → ~irefl ; (suc n) → ~irefl }))) ⟩
(do n ← eval x
exec (JPZ c (comp y (POP (REC (∞comp (While x y) c))))) (n ∷ s))
~⟨ spec-comp i x ⟩
(exec (comp x (JPZ c (comp y (POP (REC (∞comp (While x y) c)))))) s)
∎
-- specification and calculation of compile
spec-compile : ∀ i x {s} →
(do v ← eval x; return (v ∷ s)) ~[ i ] exec (compile x) s
spec-compile i x {s} =
(do v ← eval x; return (v ∷ s))
≡⟨⟩
(do v ← eval x; exec HALT (v ∷ s))
~⟨ spec-comp i x ⟩
exec (comp x HALT) s
∎
---------------------------------
-- Graph-based target language --
---------------------------------
-- Constructors for `Codeᵍ` type
pattern PUSHᵍ n c = PUSH' n ! ▸ᵍ c
pattern ADDᵍ c = ADD' ! ▸ᵍ c
pattern POPᵍ c = POP' ! ▸ᵍ c
pattern JPZᵍ l c = JPZ' ! @ l ▸ᵍ c
execᵍ : ∀ {i} → (∀ {l} → Codeᵍ Instr l) → Stack → Partial Stack i
execᵍ c = exec ⦅ c ⦆
--------------------------
-- Graph-based compiler --
--------------------------
compᵍ : ∀ {l} → Expr → Codeᵍ Instr l → Codeᵍ Instr l
compᵍ (Val x) c = PUSHᵍ x c
compᵍ (Add x y) c = compᵍ x (compᵍ y ( ADDᵍ c ))
compᵍ (While x y) c = LABᵍ← (λ l → compᵍ x (LABᵍ→ (λ l' → JPZᵍ l' (compᵍ y (POPᵍ (JMPᵍ l)))) c ))
compileᵍ : ∀ {l} → Expr → Codeᵍ Instr l
compileᵍ e = compᵍ e HALTᵍ
-- ≈ is a congruence for `comp`
mutual
≈comp : ∀ {i} {c d : Code Instr ∞} (x : Expr) → c ≈[ i ] d → comp x c ≈[ i ] comp x d
≈comp (Val x) b = ≈cong1 b
≈comp (Add x y) b = ≈comp x (≈comp y (≈cong1 b))
≈comp {i} (While x y) b = ≈comp x (≈cong2 b (≈comp y (≈cong1 (≈REC (∞≈comp {i} _ b)))))
∞≈comp : ∀ {i} {c d : Code Instr ∞} (x : Expr) → c ≈[ i ] d → ∞comp x c ∞≈[ i ] ∞comp x d
≈force (∞≈comp x b) {j} = ≈comp {j} x (≈down b)
-----------------------------------------
-- Calculation of graph-based compiler --
-----------------------------------------
open ≈-Calculation
mutual
-- specification and calculation of compᵍ
spec-compᵍ : ∀ {i} x {c} → comp x ⦅ c ⦆ ≈[ i ] ⦅ 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 ⦆)
≈⟨ ≈comp x (spec-compᵍ y) ⟩
comp x ⦅ compᵍ y ( ADDᵍ c )⦆
≈⟨ spec-compᵍ x ⟩
⦅ compᵍ x (compᵍ y ( ADDᵍ c ))⦆
∎
spec-compᵍ {i} (While x y) {c} =
comp x (JPZ ⦅ c ⦆ (comp y (POP (REC (∞comp (While x y) ⦅ c ⦆)))))
≈⟨ ≈comp x ((≈cong1 (≈comp y (≈cong1 (≈REC (∞spec-compᵍ {i} (While x y))))))) ⟩
comp x (JPZ ⦅ c ⦆ (comp y (POP (REC ∞⦅ compᵍ (While x y) c ⦆))))
≡⟨⟩
(λ l → comp x (JPZ ⦅ c ⦆ (comp y (POP l)))) (REC ∞⦅ compᵍ (While x y) c ⦆)
≡⟨⟩
(λ l → comp x (JPZ ⦅ c ⦆ (comp y (POP ⦅ JMPᵍ l ⦆)))) (REC ∞⦅ compᵍ (While x y) c ⦆)
≡⟨⟩
(λ l → comp x (JPZ ⦅ c ⦆ (comp y ⦅ POPᵍ (JMPᵍ l) ⦆))) (REC ∞⦅ compᵍ (While x y) c ⦆)
≈⟨ ≈comp x (≈cong1 (spec-compᵍ y)) ⟩
(λ l → comp x (JPZ ⦅ c ⦆ ⦅ compᵍ y (POPᵍ (JMPᵍ l))⦆)) (REC ∞⦅ compᵍ (While x y) c ⦆)
≡⟨⟩
(λ l → comp x ((λ l' → JPZ l' ⦅ compᵍ y (POPᵍ (JMPᵍ l))⦆) ⦅ c ⦆)) (REC ∞⦅ compᵍ (While x y) c ⦆)
≡⟨⟩
(λ l → comp x ((λ l' → ⦅ JPZᵍ l' (compᵍ y (POPᵍ (JMPᵍ l))) ⦆) ⦅ c ⦆ )) (REC ∞⦅ compᵍ (While x y) c ⦆)
≡⟨⟩
(λ l → comp x ⦅ LABᵍ→ (λ l' → JPZᵍ l' (compᵍ y (POPᵍ (JMPᵍ l)))) c ⦆) (REC ∞⦅ compᵍ (While x y) c ⦆)
≈⟨ spec-compᵍ x ⟩
(λ l → ⦅ compᵍ x (LABᵍ→ (λ l' → JPZᵍ l' (compᵍ y (POPᵍ (JMPᵍ l)))) c )⦆) (REC ∞⦅ compᵍ (While x y) c ⦆)
≡⟨⟩
⦅ LABᵍ← (λ l → compᵍ x (LABᵍ→ (λ l' → JPZᵍ l' (compᵍ y (POPᵍ (JMPᵍ l)))) c ))⦆
∎
∞spec-compᵍ : ∀ {i} x {c} → ∞comp x ⦅ c ⦆ ∞≈[ i ] ∞⦅ compᵍ x c ⦆
≈force (∞spec-compᵍ x) = spec-compᵍ x
-- specification and calculation of compileᵍ
spec-compileᵍ : ∀ {i} x → compile x ≈[ i ] ⦅ compileᵍ x ⦆
spec-compileᵍ x =
compile x
≡⟨⟩
comp x HALT
≡⟨⟩
comp x ⦅ HALTᵍ ⦆
≈⟨ spec-compᵍ x ⟩
⦅ compᵍ x HALTᵍ ⦆
∎