-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpreters.agda
574 lines (446 loc) · 15 KB
/
Interpreters.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
{-
Juan S. Domandl
2022
Semantics of Arith exps, stack machine compiling
-}
{-# OPTIONS --allow-exec #-}
{-# OPTIONS --guardedness #-}
module Interpreters where
open import ADT.Stack.Stack
open import Data.List
open import Data.Maybe
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl;sym ; cong; cong-app)
open Eq.≡-Reasoning
open import Data.Product using (_×_;_,_)
open import Data.Nat using (ℕ;zero;suc; _≡ᵇ_;_+_; _*_;_∸_)
open import Data.Bool using (Bool; not; _∧_)
open import Data.String using (String; _≟_)
open import Relation.Nullary using (yes; no)
--open import Relation.Binary.PropositionalEquality using (_≡_; refl; sym)
open import Function.Base using (case_of_; case_return_of_)
open import SMT.Theories.Nats as Nats
open import SMT.Backend.Z3 Nats.theory using (solveZ3)
Var = String
Σ = Var → ℕ
∅ : Σ
∅ = (λ x → 0)
record Semantical (Exp Input Domain : Set) : Set₁ where
field
⟦_⟧ : Exp → Input → Domain
open Semantical {{...}} public
data BinOp : Set where
_PLUS_ : BinOp
_TIMES_ : BinOp
_MINUS_ : BinOp
data Arith : Set where
CONST : ℕ → Arith
VAR : Var → Arith
_PLUS_ : Arith → Arith → Arith
_TIMES_ : Arith → Arith → Arith
_MINUS_ : Arith → Arith → Arith
_[_/_] : Arith → Arith → Var → Arith
CONST n [ e / x ] = CONST n
VAR y [ e / x ] with y ≟ x
... | yes _ = e
... | no _ = VAR y
(n PLUS m) [ e / x ] = (n [ e / x ]) PLUS (m [ e / x ])
(n TIMES m) [ e / x ] = (n [ e / x ]) TIMES (m [ e / x ])
(n MINUS m) [ e / x ] = (n [ e / x ]) MINUS (m [ e / x ])
data ⊥ : Set where
_≢_ : {A : Set} → A → A → Set
a ≢ b = a ≡ b → ⊥
_[_↦_] : Σ → Var → ℕ → Σ
(σ [ X ↦ n ]) Y with Y ≟ X
... | yes _ = n
... | no _ = σ Y
postulate
axiom1 : ∀ (σ : Σ) (x : Var) (v : ℕ)
-------------------------------
→ (σ [ x ↦ v ]) x ≡ v
axiom2 : ∀ (σ : Σ) (x y : Var) (v : ℕ)
→ x ≢ y
-------------------------------
→ (σ [ x ↦ v ]) y ≡ σ y
proof1 : "x" ≢ "y"
proof1 = λ ()
proof2 : "x" ≡ "x"
proof2 = refl
ℕ⟦_⟧ : Arith → Σ → ℕ
ℕ⟦ CONST n ⟧ σ = n
ℕ⟦ VAR x ⟧ σ = σ x
ℕ⟦ n PLUS m ⟧ σ = ℕ⟦ n ⟧ σ + ℕ⟦ m ⟧ σ
ℕ⟦ n TIMES m ⟧ σ = ℕ⟦ n ⟧ σ * ℕ⟦ m ⟧ σ
ℕ⟦ n MINUS m ⟧ σ = ℕ⟦ n ⟧ σ ∸ ℕ⟦ m ⟧ σ
instance
NatSemantical : Semantical Arith Σ ℕ
NatSemantical = record { ⟦_⟧ = ℕ⟦_⟧ }
{-
(e , σ) ----------------[e'/x]---------→ ( e [e'/x], σ)
| |
| |
[ x ↦ ⟦ e' ⟧ σ ] ⟦_⟧
| |
| |
↓ ↓
(e , σ [ x ↦ ⟦ e' ⟧ σ ]) ----⟦_⟧-------→ ⟦ e [ e' / x ] ⟧ σ
-}
subst-theorem : ∀ (e : Arith) {x : Var} {e' : Arith} {σ : Σ}
------------------------------------------------------
→ ℕ⟦ e [ e' / x ] ⟧ σ ≡ ℕ⟦ e ⟧ (σ [ x ↦ ℕ⟦ e' ⟧ σ ])
subst-theorem (CONST x) = refl
subst-theorem (VAR y) {x} with y ≟ x
... | yes _ = refl
... | no _ = refl
subst-theorem (n PLUS m) {x} {e'} {σ}
rewrite subst-theorem n {x} {e'} {σ}
| subst-theorem m {x} {e'} {σ} = refl
subst-theorem (n TIMES m) {x} {e'} {σ}
rewrite subst-theorem n {x} {e'} {σ}
| subst-theorem m {x} {e'} {σ} = refl
subst-theorem (n MINUS m) {x} {e'} {σ}
rewrite subst-theorem n {x} {e'} {σ}
| subst-theorem m {x} {e'} {σ} = refl
{--
Notes about class types in agda.
data List (A : Set) : Set where
nil : List A
cons : A → List A → List A
data Maybe (A : Set) : Set where
nothing : Maybe A
just : A → Maybe A
record Functor (F : Set → Set) : Set₁ where
field
fmap : ∀ {A B} → (A → B) → F A → F B
open Functor {{...}} public
map-list : {A B : Set} → (A → B) → List A → List B
map-list f nil = nil
map-list f (cons x xs) = cons (f x) (map-list f xs)
map-maybe : {A B : Set} → (A → B) → Maybe A → Maybe B
map-maybe f nothing = nothing
map-maybe f (just x) = just (f x)
instance
ListFunctor : Functor (List)
ListFunctor = record { fmap = map-list}
MaybeFunctor : Functor (Maybe)
MaybeFunctor = record { fmap = map-maybe}
--}
data Instruction : Set where
PUSH : ℕ → Instruction
ADD : Instruction
MULT : Instruction
SUB : Instruction
Program = List Instruction
-- this is just the (++) of lists
-- but lest prove some props about it
_⋈_ : Program → Program → Program
[] ⋈ p = p
(x ∷ p') ⋈ p = x ∷ ( p' ⋈ p)
infixr 5 _⋈_
⋈-neutral : ∀ (p : Program) → p ≡ p ⋈ []
⋈-neutral [] = refl
⋈-neutral (x ∷ p')
rewrite sym (⋈-neutral p') = refl
⋈-assoc : ∀ (p1 p2 p3 : Program) → p1 ⋈ (p2 ⋈ p3) ≡ (p1 ⋈ p2) ⋈ p3
⋈-assoc [] p2 p3 = refl
⋈-assoc (x ∷ p1) p2 p3
rewrite ⋈-assoc p1 p2 p3 = refl
pushN : ℕ → Stack ℕ → Stack ℕ
pushN = push ℕ
exec : Program → Stack ℕ → Stack ℕ
exec [] s = s
exec (PUSH x ∷ c) s = exec c (pushN x s)
exec (ADD ∷ c) (m ∷ n ∷ s) = exec c (pushN (n + m) s)
exec (ADD ∷ c) _ = empty ℕ
exec (MULT ∷ c) (m ∷ n ∷ s) = exec c (pushN (n * m) s)
exec (MULT ∷ c) _ = empty ℕ
exec (SUB ∷ c) (m ∷ n ∷ s) = exec c (pushN (n ∸ m) s)
exec (SUB ∷ c) _ = empty ℕ
compile' : Arith → Σ → Program → Program
compile' (CONST n) σ c = PUSH n ∷ c
compile' (VAR x) σ c = PUSH (σ x) ∷ c
compile' (e1 PLUS e2) σ c = compile' e1 σ (compile' e2 σ (ADD ∷ c))
compile' (e1 TIMES e2) σ c = compile' e1 σ (compile' e2 σ (MULT ∷ c))
compile' (e1 MINUS e2) σ c = compile' e1 σ (compile' e2 σ (SUB ∷ c))
compile : Arith → Σ → Program
compile e σ = compile' e σ []
-- Example
program1 : Program
program1 =
PUSH 2 ∷
PUSH 5 ∷
MULT ∷
PUSH 1 ∷
ADD ∷
[]
exp1 : Arith
exp1 = ((CONST 2) TIMES (CONST 5)) PLUS (CONST 1)
-- means
exp1-means = ℕ⟦ exp1 ⟧ ∅
-- compiled
p : Program
p = compile exp1 ∅
exec-p : Stack ℕ
exec-p = exec p (empty ℕ)
-- exec-p ≡ [ exp1-means ]
-- exec (compile exp1 ∅) (empty ℕ) ≡ [ ℕ⟦ exp1 ⟧ ∅ ]
-- This fact is general.
compile'-correctness : ∀ (e : Arith) (σ : Σ) (s : Stack ℕ) (c : Program)
------------------------------------------------------
→ exec (compile' e σ c) s ≡ exec c (pushN (⟦ e ⟧ σ) s)
compile'-correctness (CONST n) σ s c =
begin
exec (compile' (CONST n) σ c) s
≡⟨⟩
exec (PUSH n ∷ c) s
≡⟨⟩
exec c (pushN n s)
≡⟨⟩
exec c (pushN (⟦ (CONST n) ⟧ σ) s)
∎
compile'-correctness (VAR x) σ s c =
begin
exec (compile' (VAR x) σ c) s
≡⟨⟩
exec (PUSH ( σ x ) ∷ c) s
≡⟨⟩
exec c (pushN (σ x) s)
≡⟨⟩
exec c (pushN (⟦ (CONST (σ x)) ⟧ σ) s )
∎
compile'-correctness (e1 PLUS e2) σ s c =
begin
exec (compile' (e1 PLUS e2) σ c) s
≡⟨⟩
exec (compile' e1 σ (compile' e2 σ (ADD ∷ c))) s
≡⟨ compile'-correctness e1 σ s (compile' e2 σ (ADD ∷ c)) ⟩
exec (compile' e2 σ (ADD ∷ c)) (pushN (⟦ e1 ⟧ σ) s )
≡⟨ compile'-correctness e2 σ (pushN (⟦ e1 ⟧ σ) s ) (ADD ∷ c) ⟩
exec (ADD ∷ c) (pushN (⟦ e2 ⟧ σ) (pushN (⟦ e1 ⟧ σ) s ) )
≡⟨⟩
exec c (pushN (⟦ e1 ⟧ σ + ⟦ e2 ⟧ σ) s )
≡⟨⟩
exec c (pushN (⟦ (e1 PLUS e2) ⟧ σ) s )
∎
compile'-correctness (e1 TIMES e2) σ s c =
begin
exec (compile' (e1 TIMES e2) σ c) s
≡⟨⟩
exec (compile' e1 σ (compile' e2 σ (MULT ∷ c))) s
≡⟨ compile'-correctness e1 σ s (compile' e2 σ (MULT ∷ c)) ⟩
exec (compile' e2 σ (MULT ∷ c)) (pushN (⟦ e1 ⟧ σ) s )
≡⟨ compile'-correctness e2 σ (pushN (⟦ e1 ⟧ σ) s ) (MULT ∷ c) ⟩
exec (MULT ∷ c) (pushN (⟦ e2 ⟧ σ) (pushN (⟦ e1 ⟧ σ) s ) )
≡⟨⟩
exec c (pushN (⟦ e1 ⟧ σ * ℕ⟦ e2 ⟧ σ) s )
≡⟨⟩
exec c (pushN (⟦ (e1 TIMES e2) ⟧ σ) s )
∎
compile'-correctness (e1 MINUS e2) σ s c =
begin
exec (compile' (e1 MINUS e2) σ c) s
≡⟨⟩
exec (compile' e1 σ (compile' e2 σ (SUB ∷ c))) s
≡⟨ compile'-correctness e1 σ s (compile' e2 σ (SUB ∷ c)) ⟩
exec (compile' e2 σ (SUB ∷ c)) (pushN (⟦ e1 ⟧ σ) s )
≡⟨ compile'-correctness e2 σ (pushN (⟦ e1 ⟧ σ) s ) (SUB ∷ c) ⟩
exec (SUB ∷ c) (pushN (⟦ e2 ⟧ σ) (pushN (⟦ e1 ⟧ σ) s ) )
≡⟨⟩
exec c (pushN (⟦ e1 ⟧ σ ∸ ⟦ e2 ⟧ σ) s )
≡⟨⟩
exec c (pushN (⟦ (e1 MINUS e2) ⟧ σ) s )
∎
compile-correctness : ∀ (e : Arith) (σ : Σ)
---------------------------------------------
→ exec (compile e σ) (empty ℕ) ≡ [ ⟦ e ⟧ σ ]
compile-correctness e σ =
begin
exec (compile e σ) (empty ℕ)
≡⟨ compile'-correctness e σ (empty ℕ) [] ⟩
exec [] (pushN (⟦ e ⟧ σ) (empty ℕ))
≡⟨⟩
pushN (⟦ e ⟧ σ) (empty ℕ)
≡⟨⟩
[ ⟦ e ⟧ σ ]
∎
{-
compile-correctness as a commutative diagram
compile
Arith --------→ Stack ℕ
\ |
\ |
[⟦_⟧] \ | exec
\ |
\ |
\ ↓
→ Stack ℕ
-}
data Cmd : Set where
SKIP : Cmd
_::=_ : Var → Arith → Cmd
_::_ : Cmd → Cmd → Cmd
REPEAT_DO_DONE : Arith → Cmd → Cmd
id : {A : Set} → A → A
id x = x
_^_ : {A : Set } → (A → A) → ℕ → (A → A)
f ^ zero = id
f ^ (suc n) = λ x → (f ^ n) (f x)
_∘_ : {A B C : Set } → (B → C) → (A → B) → (A → C)
g ∘ f = λ x → g (f x)
C⟦_⟧ : Cmd → Σ → Σ
C⟦ SKIP ⟧ σ = σ
C⟦ x ::= e ⟧ σ = σ [ x ↦ ⟦ e ⟧ σ ]
C⟦ c :: c' ⟧ σ = C⟦ c' ⟧ (C⟦ c ⟧ σ )
C⟦ REPEAT e DO c DONE ⟧ σ = (C⟦ c ⟧ ^ n) σ
where
n = ⟦ e ⟧ σ
infixr 5 _::_
instance
CmdSemantical : Semantical Cmd Σ Σ
CmdSemantical = record { ⟦_⟧ = C⟦_⟧ }
fact-body : Cmd
fact-body =
"fact" ::= ((VAR "fact") TIMES (VAR "n")) ::
"n" ::= ((VAR "n") MINUS (CONST 1))
fact : Cmd
fact =
"fact" ::= (CONST 1) ::
REPEAT (VAR "n") DO
fact-body
DONE
input : Σ
input "n" = 5
input _ = 0
emp = C⟦ fact ⟧ input
j = emp "fact"
_! : ℕ → ℕ
zero ! = suc zero
(suc n) ! = (suc n) * n !
n*1≡n : ∀ (n : ℕ) → n * 1 ≡ n
n*1≡n zero = refl
n*1≡n (suc n)
rewrite n*1≡n n = refl
1*n≡n : ∀ (n : ℕ) → 1 * n ≡ n
1*n≡n zero = refl
1*n≡n (suc n)
rewrite 1*n≡n n = refl
postulate
σ-eq : ∀ {σ σ' : Σ} (x y : Var)
→ σ x ≡ σ' x → σ y ≡ σ' y
-----------------------------------
→ σ ≡ σ'
-- HELP FROM Z3
auxbyZ3 : ∀ ( x y z : ℕ) → x * (suc y) * z ≡ x * (z + y * z)
auxbyZ3 = solveZ3
body-correct : ∀ ( n fact : ℕ ) (σ : Σ)
→ σ "n" ≡ n → σ "fact" ≡ fact
------------------------------------------------------------------------
→ (⟦ fact-body ⟧ ^ n) σ ≡ (σ [ "n" ↦ 0 ]) [ "fact" ↦ (fact * (n !)) ]
body-correct zero fact σ σn≡n σfact≡fact
rewrite (n*1≡n fact) =
begin
(⟦ fact-body ⟧ ^ zero) σ
≡⟨ σ-eq "n" "fact" σn≡n σfact≡fact ⟩
((σ [ "n" ↦ 0 ]) [ "fact" ↦ fact ])
∎
body-correct (suc n) fact σ σsucn≡sucn σfact≡fact =
begin
(⟦ fact-body ⟧ ^ (suc n)) σ
≡⟨⟩
(⟦ fact-body ⟧ ^ n) (⟦ fact-body ⟧ σ)
≡⟨⟩
(⟦ fact-body ⟧ ^ n) σ'
≡⟨ body-correct n (fact * suc n) σ' prop1 prop2 ⟩
(σ' [ "n" ↦ 0 ]) [ "fact" ↦ ((fact * suc n) * (n !)) ]
≡⟨ prop3 σ' σ ⟩
(σ [ "n" ↦ 0 ]) [ "fact" ↦ fact * (suc n !) ]
∎
where
σ' = (σ [ "fact" ↦ σ "fact" * σ "n" ])
[ "n" ↦ (σ [ "fact" ↦ σ "fact" * σ "n" ]) "n" ∸ 1 ]
fact≢n : "fact" ≢ "n"
fact≢n = λ ()
prop0 : σ' ≡ (σ [ "fact" ↦ σ "fact" * σ "n" ])
[ "n" ↦ σ "n" ∸ 1 ]
prop0
rewrite axiom2 σ "fact" "n" (σ "fact" * σ "n") fact≢n = refl
prop1 : σ' "n" ≡ n
prop1
rewrite prop0
| σsucn≡sucn = refl
prop2 : σ' "fact" ≡ fact * suc n
prop2
rewrite prop0
| σfact≡fact
| σsucn≡sucn = refl
arith-lhs = fact * suc n * (n !)
arith-rhs = fact * ((n !) + n * (n !))
arith : arith-lhs ≡ arith-rhs
arith rewrite
auxbyZ3 fact n (n !) = refl
eqn : ∀ (σ1 σ2 : Σ) →
((σ1 [ "n" ↦ 0 ]) [ "fact" ↦ arith-lhs ]) "n"
≡ ((σ2 [ "n" ↦ 0 ]) [ "fact" ↦ arith-rhs ]) "n"
eqn σ1 σ2
rewrite axiom2 σ1 "fact" "n" arith-lhs fact≢n
| axiom2 σ2 "fact" "n" arith-lhs fact≢n = refl
eqfact : ∀ (σ1 σ2 : Σ) →
((σ1 [ "n" ↦ 0 ]) [ "fact" ↦ arith-lhs ]) "fact"
≡ ((σ2 [ "n" ↦ 0 ]) [ "fact" ↦ arith-rhs ]) "fact"
eqfact σ1 σ2
rewrite axiom2 σ1 "fact" "n" arith-lhs fact≢n
| axiom2 σ2 "fact" "n" arith-lhs fact≢n
| arith = refl
prop3 : ∀ (σ1 σ2 : Σ) →
((σ1 [ "n" ↦ 0 ]) [ "fact" ↦ arith-lhs ])
≡ ((σ2 [ "n" ↦ 0 ]) [ "fact" ↦ arith-rhs ])
prop3 σ1 σ2
rewrite σ-eq
{((σ1 [ "n" ↦ 0 ]) [ "fact" ↦ arith-lhs ])}
{((σ2 [ "n" ↦ 0 ]) [ "fact" ↦ arith-rhs ])}
"n" "fact" (eqn σ1 σ2) (eqfact σ1 σ2) = refl
factorial-correct : ∀ ( n : ℕ ) (σ : Σ)
→ σ "n" ≡ n
--------------------------------
→ (⟦ fact ⟧ σ) "fact" ≡ n !
factorial-correct n σ σn≡n =
begin
(⟦ fact ⟧ σ) "fact"
≡⟨⟩
(⟦ "fact" ::= (CONST 1) ::
REPEAT (VAR "n") DO
fact-body
DONE ⟧ σ) "fact"
≡⟨⟩
((⟦ REPEAT (VAR "n") DO fact-body DONE ⟧)
(⟦ "fact" ::= (CONST 1) ⟧ σ )) "fact"
≡⟨⟩
((⟦ REPEAT (VAR "n") DO fact-body DONE ⟧)
(σ [ "fact" ↦ ⟦ (CONST 1) ⟧ σ ] )) "fact"
≡⟨⟩
((⟦ REPEAT (VAR "n") DO fact-body DONE ⟧) (σ [ "fact" ↦ 1 ] )) "fact"
≡⟨⟩
((⟦ fact-body ⟧ ^ (⟦ VAR "n" ⟧ σ')) σ') "fact"
≡⟨⟩
((⟦ fact-body ⟧ ^ (σ' "n")) σ') "fact"
≡⟨ prop2 ⟩
((⟦ fact-body ⟧ ^ n) σ') "fact"
≡⟨ prop3 ⟩
n !
∎
where
σ' = σ [ "fact" ↦ 1 ]
prop0 : (σ' "fact") ≡ 1
prop0
rewrite axiom1 σ' "fact" 1 = refl
prop1 : (σ' "n") ≡ n
prop1
rewrite σn≡n = refl
prop2 : ((⟦ fact-body ⟧ ^ (⟦ VAR "n" ⟧ σ')) σ') "fact"
≡ ((⟦ fact-body ⟧ ^ n) σ') "fact"
prop2
rewrite prop1 = refl
prop3 : ((⟦ fact-body ⟧ ^ n) σ') "fact" ≡ n !
prop3
rewrite body-correct n 1 σ' prop1 prop0
| 1*n≡n (n !) = refl