-
Notifications
You must be signed in to change notification settings - Fork 0
/
try.agda
428 lines (342 loc) · 18.1 KB
/
try.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
module try where
import Data.String as S
open import Relation.Binary.PropositionalEquality hiding (inspect)
-------------------------------------------------------------
-- Prelude
-------------------------------------------------------------
data Nat : Set where
Zero : Nat
Succ : Nat -> Nat
_+_ : Nat -> Nat -> Nat
Zero + y = y
Succ x + y = Succ (x + y)
showNat : Nat -> S.String
showNat Zero = "0"
showNat (Succ n) = "S(" S.++ (showNat n) S.++ ")"
data List (A : Set) : Set where
[] : List A
_::_ : A -> List A -> List A
data _≤_ : Nat -> Nat -> Set where
Base : ∀ {n} -> n ≤ n
Step : ∀ {n m} -> n ≤ m -> n ≤ (Succ m)
data _<_ : Nat -> Nat -> Set where
Base : ∀ {n} -> Zero < (Succ n)
Step : ∀ {n m} -> n < m -> (Succ n) < (Succ m)
data Unit : Set where
unit : Unit
data Fin : Nat -> Set where
FZero : ∀ {n} -> Fin (Succ n)
FSucc : ∀ {n} -> Fin n -> Fin (Succ n)
data _×_ (A B : Set) : Set where
_,_ : A -> B -> A × B
fst : {A B : Set} -> A × B -> A
fst (x , _) = x
snd : {A B : Set} -> A × B -> B
snd (_ , x) = x
≡-symm : ∀ {A : Set} {a b : A} -> a ≡ b -> b ≡ a
≡-symm refl = refl
≡-trans : ∀ {A : Set} {a b c : A} -> a ≡ b -> b ≡ c -> a ≡ c
≡-trans refl refl = refl
+-comm : ∀ {n m p} -> n + (m + p) ≡ (n + m) + p
+-comm {Zero} = refl
+-comm {Succ n} {m} {p} = cong Succ (+-comm {n} {m} {p})
data Stack : Nat -> Set where
Empty : Stack Zero
_▻_ : {n : Nat} -> Nat -> Stack n -> Stack (Succ n)
pop : ∀ {n} -> Stack (Succ n) -> Nat
pop (x ▻ s) = x
-- Initializes a heap of a given size with Zero-values
initHeap : (n : Nat) -> Stack n
initHeap Zero = Empty
initHeap (Succ n) = Zero ▻ initHeap n
lookup : ∀ {n} -> Fin n -> Stack n -> Nat
lookup FZero (x ▻ s) = x
lookup (FSucc n) (x ▻ s) = lookup n s
update : ∀ {n} -> Fin n -> Nat -> Stack n -> Stack n
update FZero n (x ▻ s) = n ▻ s
update (FSucc f) n (x ▻ s) = x ▻ (update f n s)
fog : ∀ {n m} -> Fin n -> n ≤ m -> Fin m
fog n₁ Base = n₁
fog FZero (Step p) = FZero
fog (FSucc n₁) (Step p) = FSucc (fog n₁ (lem p))
where lem : ∀ {n m} -> Succ n ≤ m -> n ≤ m
lem Base = Step Base
lem (Step p₁) = Step (lem p₁)
predFin : ∀ {n} -> Fin n -> Fin n
predFin FZero = FZero
predFin (FSucc x) = fog x (Step Base)
fromNat : (n : Nat) -> Fin (Succ n)
fromNat Zero = FZero
fromNat (Succ n) = FSucc (fromNat n)
-------------------------------------------------------------
-- Definitions for expressions
-------------------------------------------------------------
data Expr (a : Set) : Set where
Val : Nat -> Expr a
Add : Expr a -> Expr a -> Expr a
Var : a -> Expr a
Let : Expr a -> (a -> Expr a) -> Expr a
ClosedExpr : Set₁
ClosedExpr = {a : Set} -> Expr a
data _⊢_≡_ {A B : Set} : (P : A -> B -> Set) -> Expr A -> Expr B -> Set₁ where
Val : ∀ {P} {x : Nat} -> P ⊢ (Val x) ≡ (Val x)
Add : ∀ {P} {l₁ r₁ l₂ r₂}
-> P ⊢ l₁ ≡ l₂ -> P ⊢ r₁ ≡ r₂
-> P ⊢ Add l₁ r₁ ≡ Add l₂ r₂
Var : ∀ {P x y} -> P x y -> P ⊢ Var x ≡ Var y
Let : ∀ {P b₁ b₂ e₁ e₂} -> P ⊢ b₁ ≡ b₂
-> (∀ {x₁} {x₂} -> (P x₁ x₂
-> P ⊢ (e₁ x₁) ≡ (e₂ x₂)))
-> P ⊢ (Let b₁ e₁) ≡ (Let b₂ e₂)
postulate
e≡e : ∀ {A B P} {e : ClosedExpr} -> P ⊢ e {A} ≡ e {B}
-------------------------------------------------------------
-- Basic functions over expressions
-------------------------------------------------------------
-- Evaluation
eval : Expr Nat -> Nat
eval (Val x) = x
eval (Add e₁ e₂) = eval e₁ + eval e₂
eval (Var x) = x
eval (Let e₁ e₂) = eval (e₂ (eval e₁))
-- Number of distinct variables in an expression
#vars : Expr Unit -> Nat
#vars (Val x) = Zero
#vars (Add e e₁) = #vars e + #vars e₁
#vars (Var x) = Zero
#vars (Let e x) = Succ Zero + (#vars e + #vars (x unit))
#vars' : Expr Nat -> Nat
#vars' (Val x) = Zero
#vars' (Add e e₁) = #vars' e + #vars' e₁
#vars' (Var x) = Zero
#vars' (Let e x) = Succ (#vars' e + #vars' (x Zero))
#vars'' : ∀ {m : Nat} -> Expr Nat -> Nat
#vars'' (Val x) = Zero
#vars'' {m} (Add e e₁) = #vars'' {m} e + #vars'' {m} e₁
#vars'' (Var x) = Zero
#vars'' {m} (Let e x) = Succ (#vars'' {m} e + #vars'' {m} (x m))
-- Building a tree
tree : Nat -> ClosedExpr
tree Zero = Val Zero
tree (Succ n) = Let (tree n) (λ shared -> Add (Var shared) (Var shared))
-- Observe sharing
text : ClosedExpr -> S.String
text e = go e Zero
where
go : Expr S.String -> Nat -> S.String
go (Val x) _ = showNat x
go (Add e₁ e₂) n = "(" S.++ (go e₁ n) S.++ " + " S.++ (go e₂ n) S.++ ")"
go (Var x) n = x
go (Let e₁ e₂) n with ("v" S.++ showNat n)
... | v = "let " S.++ v S.++ " = " S.++ (go e₁ (Succ n)) S.++
" in " S.++ (go (e₂ v) (Succ n)) S.++ ")"
-- Remove sharing
inline : {a : Set} -> Expr (Expr a) -> Expr a
inline (Val i) = Val i
inline (Add e₁ e₂) = Add (inline e₁) (inline e₂)
inline (Var x) = x
inline (Let e₁ e₂) = inline (e₂ (inline e₁))
-- Constant folding
remAdd : Expr Nat -> Expr Nat
remAdd (Val x) = Val x
remAdd (Add e₁ e₂) = Val (eval (remAdd e₁) + eval (remAdd e₂))
remAdd (Var x) = Var x
remAdd (Let e x) = Let (remAdd e) (λ x₁ → remAdd (x x₁))
-------------------------------------------------------------
-- Relations on Var-types
-------------------------------------------------------------
data _≃_ : Nat -> Expr Nat -> Set where
Eq : ∀ {e n} -> n ≡ eval e -> n ≃ e
data _⋍_ : ∀ {n} -> Fin n -> Unit -> Set where
Eq : ∀ {n} {f : Fin n} -> f ⋍ unit
--data _≂_ : ∀ {n} -> Fin n -> Nat -> Set where
-- Eq : ∀ {n m} {f : Fin n} -> (∀ {h : Stack n} -> lookup f h ≡ m) -> f ≂ m
_≂_ : ∀ {n} -> {s : Stack n} -> Fin n -> Nat -> Set
_≂_ {n} {s} i m = lookup i s ≡ m
-------------------------------------------------------------
-- Proofs
-------------------------------------------------------------
lem1 : {A B C D : Nat} -> A ≡ B -> C ≡ D -> A + C ≡ B + D
lem1 refl refl = refl
-- Constant folding does not change the value of an expression
remAddEquality' : {e : Expr Nat} -> eval e ≡ eval (remAdd e)
remAddEquality' {Val x} = refl
remAddEquality' {Add e e₁} with lem1 (remAddEquality' {e}) (remAddEquality' {e₁})
... | l = l
remAddEquality' {Var x} = refl
remAddEquality' {Let e x} with (cong x (remAddEquality' {e}))
... | q rewrite q = remAddEquality' {x (eval (remAdd e))}
remAddEquality : {e : ClosedExpr} -> eval e ≡ eval (remAdd e)
remAddEquality {e} = remAddEquality' {e {Nat}}
-- Removing sharing does not change the value of an expression
inlineEquality' : ∀ {eA : Expr Nat} {eB : Expr (Expr Nat)}
-> _≃_ ⊢ eA ≡ eB -> (eval eA) ≡ (eval (inline eB))
inlineEquality' Val = refl
inlineEquality' (Add p p₁) = lem (inlineEquality' p) (inlineEquality' p₁)
where lem : {a b a' b' : Nat} -> a ≡ a' -> b ≡ b' -> a + b ≡ a' + b'
lem refl refl = refl
inlineEquality' (Var (Eq x)) = x
inlineEquality' {Let x₁ e₁} {Let x₂ e₂} (Let p₁ p₂) with (p₂ {eval x₁} {inline (x₂)})
... | q = inlineEquality' (q (Eq (inlineEquality' p₁)))
inlineEquality : {e : ClosedExpr} -> eval e ≡ eval (inline e)
inlineEquality {e} = inlineEquality' ( e≡e {Nat} {Expr Nat} {_} {e})
-------------------------------------------------------------
-- Compiler
-------------------------------------------------------------
-- Data type for operations
data Op : Nat -> Nat -> Nat -> Nat -> Set where
Stop : ∀ {n m} -> Op m m n n
Push : ∀ {n m v w} -> Nat -> Op v w (Succ n) m -> Op v w n m
Add : ∀ {n m v w} -> Op v w (Succ n) m -> Op v w (Succ (Succ n)) m
Load : ∀ {n m v w} -> Fin v -> Op v w (Succ n) m -> Op v w n m
Store : ∀ {n m v w} -> Op (Succ v) w n m -> Op v w (Succ n) m
test : Op Zero (Succ Zero) Zero (Succ Zero)
test = Push (Succ Zero) (Store (Load FZero (Load FZero (Add Stop) )))
-- Concatenating operations
_++_ : ∀ {n m p v w z} -> Op v w n m -> Op w z m p -> Op v z n p
Stop ++ o₂ = o₂
Push x o₁ ++ o₂ = Push x (o₁ ++ o₂)
Add o₁ ++ o₂ = Add (o₁ ++ o₂)
Load x o₁ ++ o₂ = Load x (o₁ ++ o₂)
Store o₁ ++ o₂ = Store (o₁ ++ o₂)
++-assoc : ∀ {v w z q n m p r : Nat} {op₁ : Op v w n m} {op₂ : Op w z m p} {op₃ : Op z q p r} -> op₁ ++ (op₂ ++ op₃) ≡ (op₁ ++ op₂) ++ op₃
++-assoc {op₁ = Stop} = refl
++-assoc {op₁ = Push x op₁} {op₂} {op₃} = cong (λ x₁ → Push x x₁) (++-assoc {op₁ = op₁})
++-assoc {op₁ = Add op₁} = cong (λ x → Add x) (++-assoc {op₁ = op₁})
++-assoc {op₁ = Load x op₁} = cong (λ x₁ → Load x x₁) (++-assoc {op₁ = op₁})
++-assoc {op₁ = Store op₁} = cong (λ x₁ → Store x₁) (++-assoc {op₁ = op₁})
-- Executing an operation
exec : ∀ {n m v w } -> Op v w n m -> Stack n × Stack v -> Stack m × Stack w
exec Stop s = s
exec (Push x o) (x₁ , x₂) = exec o ((x ▻ x₁) , x₂)
exec (Add o) ((x ▻ (x₁ ▻ x₂)) , x₃) = exec o (((x + x₁) ▻ x₂) , x₃)
exec (Load x o) (x₁ , x₂) = exec o (((lookup x x₂) ▻ x₁ ), x₂ )
exec (Store o) ((x₁ ▻ x₂) , x₃) = exec o (x₂ , (x₁ ▻ x₃))
--------------------------------------------
-- Temp
--------------------------------------------
vw : ∀ {v w n m} -> Op v w n m -> v ≤ w
vw Stop = Base
vw (Push x o) = vw o
vw (Add o) = vw o
vw (Load x o) = vw o
vw (Store o) = lem (vw o)
where lem : ∀ {n m} -> Succ n ≤ m -> n ≤ m
lem Base = Step Base
lem (Step p) = Step (lem p)
asd : ∀ {n : Nat} -> #vars (Val n) ≡ Zero
asd = refl
dfg : ∀ {n : Nat} -> n + Zero ≡ n
dfg {Zero} = refl
dfg {Succ n} = cong Succ dfg
pff : ∀ {n m} -> n + Succ m ≡ Succ (n + m)
pff {Zero} = refl
pff {Succ n} {m} = cong Succ (pff {n} {m})
prt : ∀ {m n p} -> m ≤ n -> m ≤ (n + p)
prt {m} {p = Zero} Base rewrite (dfg {m}) = Base
prt {m} {p = Succ p} Base rewrite (pff {m} {p}) = Step (prt Base)
prt (Step p₁) = Step (prt p₁)
pe : ∀ {n m p} -> n ≤ m -> (n + p) ≤ (m + p)
pe Base = Base
pe (Step p₁) = Step (pe p₁)
bla : ∀ {e e₁} -> #vars' (Add e e₁) ≡ #vars' e + #vars' e₁
bla = refl
--(Let e x) ->
compile' : ∀ {v n} -> (m : Nat) -> m ≤ v -> (e : Expr Nat) -> Op v (v + (#vars' e)) n (Succ n)
compile' {v} m p (Val x) rewrite (dfg {v}) = Push x Stop
compile' {v} {n} m p (Add e e₁) rewrite (+-comm {v} {#vars' e} {#vars' e₁}) = compile' {v} {n} m p e ++ (compile' {v + #vars' e} {Succ n} m (prt p) e₁ ++ Add Stop )
compile' {v} m p (Var x) rewrite (dfg {v}) = Load {!fromNat x!} Stop
compile' {v} {n} m p (Let e x) rewrite (pff {v} { (#vars' e + #vars' (x m))} ) | (pff {v} { (#vars' e + #vars' (x Zero))} )| +-comm {v} {#vars' e} {#vars' (x m)} | +-comm {v} {#vars' e} {#vars' (x Zero)} = compile' m p e ++ Store (compile' {Succ (v + #vars' e)} {n} (Succ m) {!!} {!x m!}) -- {!compile' {Succ (v + #vars' e)} {n} (Succ (m + #vars' e)) ? (x m) !}
{-
compile' : ∀ {v n} -> (e : Expr (Fin (Succ v))) -> Op v (v + (#vars' e)) n (Succ n)
compile' {v} (Val x) rewrite (dfg {v}) = Push x Stop
compile' {v} (Add e e₁) = compile' e ++ ({!compile' {v + #vars' e} e₁ ++ Add Stop!}) --({!compile' e₁ ++ Add Stop!})
compile' (Var x) = {!!}
compile' (Let e x) = {!!}
compile' : ∀ {v n} {eU : Expr Unit} {eF : Expr (Fin v)} -> (_⋍_ ⊢ eF ≡ eU) -> Op v (v + (#vars eU)) n (Succ n)
compile' {v = v} {eU = Val .i} {eF = Val i} Val rewrite (asd {i}) with (dfg {v})
... | p rewrite p = Push i Stop
compile' {v = v} {eU = Add uₗ uᵣ} {eF = Add fₗ fᵣ} (Add p p₁) = compile' p ++ ({!compile' {v = v + #vars uₗ} p₁!} ++ {!!})
compile' {v = v} {eF = Var x} (Var x₁) rewrite (dfg {v}) = Load x Stop
compile' (Let p x) = {!!}
compile' (Val m) = {!!} -- Push m Stop
compile' (Add e₁ e₂) = compile' e₁ ++ (compile' e₂ ++ Add Stop)
compile' (Var i) = {!!} -- Load i Stop
compile' (Let x e) = compile' x ++ Store {!!} -- (compile' x) (e ?)
compile : ∀ {v n} -> Expr (Fin (Succ v)) -> Op (Succ v) n (Succ n)
compile {v} e = compile' (fromNat v) e
-------------------------------------------------------------
-- Example experssions
-------------------------------------------------------------
expr1 : ClosedExpr
expr1 = Let (Val (Succ Zero)) (λ i -> Add (Var i) (Var i))
expr2 : ClosedExpr
expr2 = Let (Val (Succ Zero)) (λ i -> Let (Val (Succ (Succ Zero)))(λ ii -> Add (Var i) (Var ii)))
expr3 : ClosedExpr
expr3 = Add (Let (Val (Succ Zero)) (λ i -> Var i)) (Let (Val (Succ (Succ Zero))) (λ ii → Var ii))
test : ClosedExpr -> Nat
test e with (#vars e)
... | v with exec (compile {#vars e} (e )) (Empty , initHeap (Succ (#vars e)))
test e | v | x , x₁ = pop x
-------------------------------------------------------------
-- Compiler correctness
-------------------------------------------------------------
lem : ∀ {n m p v} {op₁ : Op v n m} {op₂ : Op v m p} {s : Stack n} {h : Stack v} -> exec op₂ (exec op₁ (s , h)) ≡ exec (op₁ ++ op₂) (s , h)
lem {op₁ = Stop} = refl
lem {op₁ = Push x op₁} = lem {op₁ = op₁}
lem {op₁ = Add op₁} {s = x ▻ (x₁ ▻ s)} = lem {op₁ = op₁}
lem {op₁ = Load x op₁} = lem {op₁ = op₁}
lem {op₁ = Store x op₁} {s = x₁ ▻ s} = lem {op₁ = op₁}
lem₁ : ∀ {n v n₁ n₂} {op₁ : Op v n (Succ n)} {op₂ : Op v (Succ n) (Succ (Succ n))} {s : Stack n} {h h₁ h₂ : Stack v}
-> exec op₁ (s , h) ≡ (n₁ ▻ s) , h₁ -> exec op₂ ((n₁ ▻ s) , h₁) ≡ (n₂ ▻ (n₁ ▻ s)) , h₂ -> exec (op₁ ++ op₂) (s , h) ≡ (n₂ ▻ (n₁ ▻ s)) , h₂
lem₁ {op₁ = op₁} {op₂ = op₂} {s = s} {h = h} p₁ p₂ rewrite ≡-symm (lem {op₁ = op₁} {op₂ = op₂} {s = s} {h = h}) with p₂
... | r rewrite ≡-symm p₁ = p₂
data Exists (A : Set) (B : A -> Set) : Set where
∃_,_ : (x : A) -> B x -> Exists A B
+-comm : ∀ {a b} -> a + b ≡ b + a
+-comm {b = Zero} = +Zero
where +Zero : ∀ {a} -> a + Zero ≡ a
+Zero {Zero} = refl
+Zero {Succ a} = cong Succ +Zero
+-comm {a} {b = Succ b} rewrite +-comm {b} {a} = +-Succ {a} {b}
where +-Succ : ∀ {a b} -> a + Succ b ≡ Succ (a + b)
+-Succ {Zero} = refl
+-Succ {Succ a₁} = cong Succ (+-Succ {a₁})
cC : ∀ {v n : Nat} {h h₁ : Stack (Succ v)} {s : Stack n} {eF : Expr (Fin (Succ v))} {eN : Expr Nat} {f : Fin (Succ v)}
-> (_≂_ {Succ v} {h}) ⊢ eF ≡ eN -> Exists (Stack (Succ v)) (λ h₁ -> exec (compile' f eF) (s , h) ≡ (eval eN ▻ s) , h₁)
cC {h = h} Val = ∃ h , refl
cC {v} {n} {h = h} {s = s} {eF = Add fₗ fᵣ} {eN = Add nₗ nᵣ} {f = f} (Add p p₁)
rewrite ++-assoc {v = Succ v} {n = n} {m = Succ n} {op₁ = compile' f fₗ} {op₂ = compile' f fᵣ} {op₃ = Add Stop}
with lem {op₁ = compile' {n = n} f fₗ ++ compile' f fᵣ} {op₂ = Add Stop} {s = s} {h = h}
... | lemApp rewrite ≡-symm lemApp with (exec (compile' f fₗ) (s , h))
... | (sₗ , hₗ) with (exec (compile' f fᵣ) (sₗ , hₗ))
... | (sᵣ , hᵣ) with (cC {v} {h = h} {h₁ = hₗ} {s = s} {f = f} p)
... | ∃ ehₗ , ccₗ with cC {v} {h = ehₗ} {h₁ = hᵣ} {s = eval nₗ ▻ s} {f = f} {!p₁!} -- p₁
... | ∃ ehᵣ , ccᵣ rewrite lem₁ {op₁ = compile' f fₗ} {op₂ = compile' f fᵣ} {s = s} {h = h} {h₁ = ehₗ} {h₂ = ehᵣ} ccₗ {!!} with +-comm {eval nᵣ} {eval nₗ}
... | comm rewrite comm = ∃ ehᵣ , {!!} --refl
cC {h = h} {eF = Var xf} {eN = Var xn} (Var x₁) rewrite x₁ = ∃ h , refl
cC {v} {n} {h = h} {s = s} {eF = Let b₁ e₁} {eN = Let b₂ e₂} {f = f} (Let p x)
rewrite ≡-symm (lem {op₁ = compile' {n = n} f b₁} {op₂ = Store f (compile' (predFin f) (e₁ f))} {s = s} {h = h})
with (cC {v} {h = h} {h₁ = snd (exec (compile' f b₁) (s , h))} {s = s} {f = f} p)
... | ∃ eh₁ , cC₁ rewrite cC₁ with x {f} {eval b₂} ({!!}) -- ?! Fix ≂!
... | nope with cC {v} {h = update f (eval b₂) eh₁} {h₁ = snd (exec (compile' (predFin f) (e₁ f)) (s , update f (eval b₂) eh₁))} {s = s} {f = predFin f} {!!} --nope
... | ∃ eh₂ , cC₂ = ∃ eh₂ , cC₂
compilerCorrectness : ∀ {e : ClosedExpr} {n : Nat} {s : Stack n} {h : Stack (Succ (#vars e))} -> (fst (exec (compile e) (s , h))) ≡ eval e ▻ s
compilerCorrectness {e} {n} {s} {h} with cC {#vars e} {n} {h} {snd (exec (compile e) (s , h))} {s} {e} {e} {fromNat (#vars e)} (e≡e {Fin (Succ (#vars e))} {Nat} {_≂_} {e})
compilerCorrectness | ∃ x , x₁ rewrite x₁ = refl
-}
{-
cC : ∀ {v n : Nat} {h h₁ : Stack (Succ v)} {s : Stack n} {eF : Expr (Fin (Succ v))} {eN : Expr Nat} -> _≂_ ⊢ eF ≡ eN -> Exists (Stack (Succ v)) (λ h₁ -> exec (compile eF) (s , h) ≡ (eval eN ▻ s) , h₁)
cC {h = h} Val = ∃ h , refl
cC {v} {n} {h = h} {s = s} {eF = Add fₗ fᵣ} {eN = Add nₗ nᵣ} (Add p p₁) rewrite ++-assoc {v = Succ v} {n = n} {m = Succ n} {op₁ = compile' (fromNat v) fₗ} {op₂ = compile' (fromNat v) fᵣ} {op₃ = Add Stop} with lem {op₁ = compile' {n = n} (fromNat v) fₗ ++ compile' (fromNat v) fᵣ} {op₂ = Add Stop} {s = s} {h = h}
... | lemApp rewrite ≡-symm lemApp with (exec (compile' (fromNat v) fₗ) (s , h))
... | (sₗ , hₗ) with (exec (compile' (fromNat v) fᵣ) (sₗ , hₗ))
... | (sᵣ , hᵣ) with (cC {v} {h = h} {h₁ = hₗ} {s = s} p)
... | ∃ ehₗ , ccₗ with cC {v} {h = ehₗ} {h₁ = hᵣ} {s = eval nₗ ▻ s} p₁
... | ∃ ehᵣ , ccᵣ rewrite lem₁ {op₁ = compile' (fromNat v) fₗ} {op₂ = compile' (fromNat v)fᵣ} {s = s} {h = h} {h₁ = ehₗ} {h₂ = ehᵣ} ccₗ ccᵣ with +-comm {eval nᵣ} {eval nₗ}
... | comm rewrite comm = ∃ ehᵣ , refl
cC {h = h} {eF = Var xf} {eN = Var xn} (Var (Eq x₁)) = {!!} --rewrite x₁ {h} = ∃ h , refl
cC {v} {n} {h = h} {s = s} {eF = Let b₁ e₁} {eN = Let b₂ e₂} (Let p x) rewrite ≡-symm (lem {op₁ = compile' {n = n} (fromNat v) b₁} {op₂ = Store (fromNat v) (compile' (predFin (fromNat v)) (e₁ (fromNat v)))} {s = s} {h = h}) with (cC {v} {h = h} {h₁ = snd (exec (compile' (fromNat v) b₁) (s , h))} {s = s} p)
... | ∃ eh₁ , cC₁ rewrite cC₁ with x {fromNat v} {eval b₂} (Eq {!!}) -- update (fromNat v) (eval b₂) (eh₁)
... | nope with cC {v} {h = update (fromNat v) (eval b₂) eh₁} {h₁ = {!!}} {s = s} nope
... | ∃ eh₂ , cC₂ = {!s₁!} -}