-
Notifications
You must be signed in to change notification settings - Fork 0
/
STLC.agda
1395 lines (1082 loc) · 63.1 KB
/
STLC.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module STLC where
-- ma look no imports
{-
Semantics of Simply Typed Lambda Calculus in Agda
Zhenya Vinogradov · 6 July 2020
We describe operational semantics for simply typed lambda calculus by defining a function
which describes a single computation step (sections 1-11). Then we prove that operational
semantics are terminating by providing denotational-style semantics, i.e. interpreting
types of our calculus as corresponding meta-language types (sections 12-19). As the
termination proof is constructive, it allows us to define an interpreter for the calculus,
which computes resulting value for a term (section 20).
This file has 20 sections (also listed in the navigation bar in the rendered version):
Part I. Operational semantics
1. Standard library definitions
2. Types
3. Introduction and elimination rules
4. Boilerplate utensils for introduction and elimination rules
5. Terms
6. Examples of terms
7. Compiled term representation
8. Compilation
9. Run-time term representation
10. Operational semantics for elimination rules
11. Operational semantics for the whole calculus
Part II. Denotational semantics
12. Locality lemma
13. Examples of values
14. Definition of a computation trace
15. Definition of denotation for values
16. Definition of denotation for other objects
17. Denotational semantics for introduction rules
18. Denotational semantics for elimination rules
19. Denotational semantics for the whole calculus
20. Interpreter
This file is available in two versions:
* rendered: https://zhenyavinogradov.github.io/stlc/
* raw code on github: https://github.com/zhenyavinogradov/stlc/blob/barin/STLC.agda
-}
module 1:Library where
{-
1. Standard library definitions
-}
-- Pair
infixr 15 _×_
infixr 5 _,_
record _×_ (X Y : Set) : Set where
constructor _,_
field
fst : X
snd : Y
-- Dependent sum
infixr 5 _,,_
record Σ (Ω : Set) (X : Ω → Set) : Set where
constructor _,,_
field
first : Ω
second : X first
-- Equality
data Eq {X : Set} (x : X) : X → Set where
refl : Eq x x
-- Synonym for 'Eq'
_≡_ = Eq
-- List
infixr 5 _∷_
data List (X : Set) : Set where
ε : List X
_∷_ : X → List X → List X
-- Heterogeneous list
data All {Ω : Set} (X : Ω → Set) : List Ω → Set where
ε : All X ε
_∷_ : ∀ {ω ωs} → X ω → All X ωs → All X (ω ∷ ωs)
-- Heterogeneous list for Set₁
data All₁ {Ω : Set} (X : Ω → Set₁) : List Ω → Set₁ where
ε : All₁ X ε
_∷_ : ∀ {ω ωs} → X ω → All₁ X ωs → All₁ X (ω ∷ ωs)
-- 'Any X ωs' states that 'X' holds for some element of 'ωs'
data Any {Ω : Set} (X : Ω → Set) : List Ω → Set where
here : ∀ {ω ωs} → X ω → Any X (ω ∷ ωs)
there : ∀ {ω ωs} → Any X ωs → Any X (ω ∷ ωs)
-- 'Has ωs ω' points at a position of 'ω' in 'ωs'
Has : {Ω : Set} → List Ω → Ω → Set
Has ωs ω = Any (Eq ω) ωs
-- 'All× X Y (ω , ψ)' states that 'X' holds for 'ω' and 'Y' holds for 'ψ'
data All× {Ω Ψ : Set} (X : Ω → Set) (Y : Ψ → Set) : Ω × Ψ → Set where
_,_ : ∀ {ω ψ} → X ω → Y ψ → All× X Y (ω , ψ)
-- 'AllΣ P (ω ,, x)' states that 'P' holds for 'x'
data AllΣ {Ω : Set} {X : Ω → Set} (P : ∀ ω → X ω → Set) : Σ Ω X → Set where
_,,_ : (ω : Ω) → {x : X ω} → P ω x → AllΣ P (ω ,, x)
-- 'All2 P ωs xs' states that 'P' holds for all elements of 'xs'
data All2 {Ω : Set} {X : Ω → Set} (P : ∀ ω → X ω → Set) : ∀ ωs → All X ωs → Set where
ε : All2 P ε ε
_∷_ : ∀ {ω ωs x xs} → P ω x → All2 P ωs xs → All2 P (ω ∷ ωs) (x ∷ xs)
-- 'AllAny P ωs xᵢ' states that 'P' holds for the value which 'xᵢ' points to
data AllAny {Ω : Set} {X : Ω → Set} (P : ∀ ω → X ω → Set) : ∀ ωs → Any X ωs → Set where
here : ∀ {ω ωs x} → P ω x → AllAny P (ω ∷ ωs) (here x)
there : ∀ {ω ωs xᵢ} → AllAny P ωs xᵢ → AllAny P (ω ∷ ωs) (there xᵢ)
-- Functorial map for List
mapList : {X Y : Set} → (X → Y) → (List X → List Y)
mapList f ε = ε
mapList f (x ∷ xs) = f x ∷ mapList f xs
-- Functorial map for All
mapAll : {Ω : Set} {X Y : Ω → Set} → (∀ {ω} → X ω → Y ω) → (∀ {ωs} → All X ωs → All Y ωs)
mapAll f ε = ε
mapAll f (x ∷ xs) = f x ∷ mapAll f xs
-- Functorial map for Any
mapAny : {Ω : Set} {X Y : Ω → Set} → (∀ {ω} → X ω → Y ω) → (∀ {ωs} → Any X ωs → Any Y ωs)
mapAny f (here x) = here (f x)
mapAny f (there xᵢ) = there (mapAny f xᵢ)
-- Functorial map for All2
mapAll2
: {Ω : Set} {X Y : Ω → Set} {AllX : ∀ ω → X ω → Set} {AllY : ∀ ω → Y ω → Set}
→ (f : ∀ {ω} → X ω → Y ω)
→ (allF : ∀ {ω x} → AllX ω x → AllY ω (f x))
→ ∀ {ωs xs} → All2 AllX ωs xs → All2 AllY ωs (mapAll f xs)
mapAll2 f allF ε = ε
mapAll2 f allF (p ∷ ps) = allF p ∷ mapAll2 f allF ps
-- Functorial map for AllAny
mapAllAny
: {Ω : Set} {X Y : Ω → Set} {AllX : ∀ ω → X ω → Set} {AllY : ∀ ω → Y ω → Set}
→ (f : ∀ {ω} → X ω → Y ω)
→ (allF : ∀ {ω x} → AllX ω x → AllY ω (f x))
→ ∀ {ωs xs} → AllAny AllX ωs xs → AllAny AllY ωs (mapAny f xs)
mapAllAny f allF (here p) = here (allF p)
mapAllAny f allF (there pᵢ) = there (mapAllAny f allF pᵢ)
-- Identity function
identity : {X : Set} → X → X
identity x = x
-- Proves that function application preserves equality
cong : {X Y : Set} → (f : X → Y) → ∀ {x x'} → x ≡ x' → f x ≡ f x'
cong f refl = refl
-- Proves that index of a predicate can be replaced with an equal one
transport : {Ω : Set} → (X : Ω → Set) → ∀ {ω ω'} → ω ≡ ω' → X ω → X ω'
transport X refl x = x
-- Shorthands for 'Has' for several first elements
$0 : ∀ {Ω ω₀ ωs} → Has {Ω} (ω₀ ∷ ωs) ω₀
$1 : ∀ {Ω ω₀ ω₁ ωs} → Has {Ω} (ω₀ ∷ ω₁ ∷ ωs) ω₁
$2 : ∀ {Ω ω₀ ω₁ ω₂ ωs} → Has {Ω} (ω₀ ∷ ω₁ ∷ ω₂ ∷ ωs) ω₂
$3 : ∀ {Ω ω₀ ω₁ ω₂ ω₃ ωs} → Has {Ω} (ω₀ ∷ ω₁ ∷ ω₂ ∷ ω₃ ∷ ωs) ω₃
$4 : ∀ {Ω ω₀ ω₁ ω₂ ω₃ ω₄ ωs} → Has {Ω} (ω₀ ∷ ω₁ ∷ ω₂ ∷ ω₃ ∷ ω₄ ∷ ωs) ω₄
$0 = here refl
$1 = there $0
$2 = there $1
$3 = there $2
$4 = there $3
-- Returns ith element of 'All'
get : {Ω : Set} {X : Ω → Set} → ∀ {ω ωs} → All X ωs → Has ωs ω → X ω
get (x ∷ xs) (here refl) = x
get (x ∷ xs) (there i) = get xs i
-- Returns ith element of 'All2'
get2 : {Ω : Set} {X : Ω → Set} {P : ∀ ω → X ω → Set} → ∀ {ω ωs xs} → All2 P ωs xs → (i : Has ωs ω) → P ω (get xs i)
get2 (p ∷ ps) (here refl) = p
get2 (p ∷ ps) (there i) = get2 ps i
-- List concatenation
infixr 5 _++_
_++_ : {X : Set} → List X → List X → List X
ε ++ ys = ys
(x ∷ xs) ++ ys = x ∷ (xs ++ ys)
{-
Some properties of 'Has'
-}
has-skip : {Ω : Set} → {τs : List Ω} → {τ : Ω} → (ρs : List Ω) → Has τs τ → Has (ρs ++ τs) τ
has-skip ε i = i
has-skip (ρ ∷ ρs) i = there (has-skip ρs i)
has-append : {Ω : Set} → {τs : List Ω} → {τ : Ω} → (ρs : List Ω) → Has τs τ → Has (τs ++ ρs) τ
has-append ρs (here e) = here e
has-append ρs (there i) = there (has-append ρs i)
has-splice : {Ω : Set} → {τ : Ω} → (τs τs' ρs : List Ω) → Has (τs ++ τs') τ → Has (τs ++ ρs ++ τs') τ
has-splice ε τs' ρs i = has-skip ρs i
has-splice (τ ∷ τs) τs' ρs (here e) = here e
has-splice (τ ∷ τs) τs' ρs (there i) = there (has-splice τs τs' ρs i)
has-abs : {Ω : Set} → {τ : Ω} → (τ' : Ω) → (τs ρs : List Ω) → Has (τ' ∷ τs) τ → Has (τ' ∷ ρs ++ τs) τ
has-abs τ' τs ρs i = has-splice (τ' ∷ ε) τs ρs i
has-cons : {Ω : Set} → {τs : List Ω} → {τ' τ : Ω} → Has τs τ' → Has (τ' ∷ τs) τ → Has τs τ
has-cons i (here refl) = i
has-cons i (there j) = j
has-prepend : {Ω : Set} → {τs τs' : List Ω} → (∀ {τ} → Has τs τ → Has τs' τ) → (σs : List Ω) → (∀ {τ} → Has (σs ++ τs) τ → Has (σs ++ τs') τ)
has-prepend f ε x = f x
has-prepend f (σ ∷ σs) (here x) = here x
has-prepend f (σ ∷ σs) (there x) = there (has-prepend f σs x)
open 1:Library
module 2:Types where
{-
2. Types
-}
-- Types defined in our calculus. We have functions, finite coproducts, finite products, and
-- a couple of inductive and coinductive types
infixr 5 _⇒_
data Type : Set where
_⇒_ : Type → Type → Type -- function
#Sum : List Type → Type -- sum of a list of types
#Product : List Type → Type -- product of a list of types
#Nat : Type -- natural number
#Conat : Type -- conatural number (potentially infinite number)
#Stream : Type → Type -- stream (infinite sequence)
-- Empty sum
#Void : Type
#Void = #Sum ε
-- Empty product
#Unit : Type
#Unit = #Product ε
-- Sum of two types
#Either : Type → Type → Type
#Either σ τ = #Sum (σ ∷ τ ∷ ε)
-- Product of two types
#Pair : Type → Type → Type
#Pair σ τ = #Product (σ ∷ τ ∷ ε)
-- Bool
#Bool : Type
#Bool = #Either #Unit #Unit
-- Maybe
#Maybe : Type → Type
#Maybe τ = #Either #Unit τ
open 2:Types
module 3:Rules where
{-
3. Introduction and elimination rules
-}
{-
We define introduction and elimination rules for our calculus, parameterized by two
special types: '%Abstraction' and '%Value'. In next sections we use different
instantiatons of these two types to define different representations for terms
of our calculus.
-}
-- Introduction rules
data Intr (%Abstraction : Type → Type → Set) (%Value : Type → Set) : Type → Set where
-- A function is defined by a lambda abstraction
intrArrow : ∀ {ρ τ} → %Abstraction ρ τ → Intr %Abstraction %Value (ρ ⇒ τ)
-- To construct '#Sum τs', provide a value for one of the types in τs
intrSum : ∀ {τs} → Any %Value τs → Intr %Abstraction %Value (#Sum τs)
-- To construct '#Product τs', provide a value for each type in τs
intrProduct : ∀ {τs} → All %Value τs → Intr %Abstraction %Value (#Product τs)
-- To construct '#Nat', provide a value of type '#Maybe #Nat'. 'nothing' corresponds to
-- zero, 'just n' corresponds to the successor of 'n'
intrNat : %Value (#Maybe #Nat) → Intr %Abstraction %Value #Nat
-- Conatural number is defined by a triple '(ρ ,, v , f)', where ρ is a type, 'v' is a
-- value of type ρ and 'f' is a function of type 'ρ ⇒ #Maybe ρ'. 'v' represents current
-- state, 'f' returns 'nothing' if the number is zero, or 'just u' if the number is the
-- successor of 'u'
intrConat : Σ Type (\ρ → %Value ρ × %Value (ρ ⇒ #Maybe ρ)) → Intr %Abstraction %Value #Conat
-- Stream of elements of type τ is defined by a triple (ρ ,, v , f), where ρ is a type,
-- 'v' is a value of type ρ and 'f' is a function of type 'ρ ⇒ #Pair τ ρ'. 'v' represents
-- current state, 'f' returns a pair containing stream head and stream tail
intrStream : ∀ {τ} → Σ Type (\ρ → %Value ρ × %Value (ρ ⇒ #Pair τ ρ)) → Intr %Abstraction %Value (#Stream τ)
-- Elimination rules
data Elim (%Value : Type → Set) : Type → Type → Set where
-- To eliminate 'ρ ⇒ τ', provide a value of type ρ; the result will be of type τ
elimArrow : ∀ {ρ τ} → %Value ρ → Elim %Value (ρ ⇒ τ) τ
-- To eliminate '#Sum τs', provide a function 'τ ⇒ ϕ' for each type τ in τs; the result
-- will be of type ϕ
elimSum : ∀ {τs ϕ} → All (\τ → %Value (τ ⇒ ϕ)) τs → Elim %Value (#Sum τs) ϕ
-- If τ is a member of τs, we can eliminate '#Product τs' to get a value of type τ
elimProduct : ∀ {τs τ} → Has τs τ → Elim %Value (#Product τs) τ
-- Elimination for '#Nat' is recursion
elimNat : ∀ {ϕ} → %Value (#Maybe ϕ ⇒ ϕ) → Elim %Value #Nat ϕ
-- '#Conat' can be deconstructed into '#Maybe #Conat', giving 'nothing' for zero, or
-- 'just n' for the successor of 'n'
elimConat : Elim %Value #Conat (#Maybe #Conat)
-- '#Stream τ' can be deconstructed into '#Pair τ (#Stream τ)', giving a pair containing
-- stream head and stream tail
elimStream : ∀ {τ} → Elim %Value (#Stream τ) (#Pair τ (#Stream τ))
-- An expression is either an introduction rule, or an elimination rule with a value it applies to
data Expr (%Abstraction : Type → Type → Set) (%Value : Type → Set) (τ : Type) : Set where
intr : Intr %Abstraction %Value τ → Expr %Abstraction %Value τ
elim : ∀ {ϕ} → %Value ϕ → Elim %Value ϕ τ → Expr %Abstraction %Value τ
open 3:Rules
module 4:Utensils where
{-
4. Boilerplate utensils for introduction and elimination rules
-}
-- Functorial map for Intr
mapIntr
: ∀ {%A1 %A2 %V1 %V2}
→ (%mapA : ∀ {ρ τ} → %A1 ρ τ → %A2 ρ τ) → (%mapV : ∀ {τ} → %V1 τ → %V2 τ) → (∀ {τ} → Intr %A1 %V1 τ → Intr %A2 %V2 τ)
mapIntr %mapA %mapV (intrArrow abs) = intrArrow (%mapA abs)
mapIntr %mapA %mapV (intrSum vᵢ) = intrSum (mapAny %mapV vᵢ)
mapIntr %mapA %mapV (intrProduct vs) = intrProduct (mapAll %mapV vs)
mapIntr %mapA %mapV (intrNat v) = intrNat (%mapV v)
mapIntr %mapA %mapV (intrConat (ρ ,, v , f)) = intrConat (ρ ,, %mapV v , %mapV f)
mapIntr %mapA %mapV (intrStream (ρ ,, v , f)) = intrStream (ρ ,, %mapV v , %mapV f)
-- Functorial map for Elim
mapElim : ∀ {%V1 %V2} → (%mapV : ∀ {τ} → %V1 τ → %V2 τ) → (∀ {τ ϕ} → Elim %V1 τ ϕ → Elim %V2 τ ϕ)
mapElim %mapV (elimArrow v) = elimArrow (%mapV v)
mapElim %mapV (elimSum fs) = elimSum (mapAll %mapV fs)
mapElim %mapV (elimProduct i) = elimProduct i
mapElim %mapV (elimNat f) = elimNat (%mapV f)
mapElim %mapV elimConat = elimConat
mapElim %mapV elimStream = elimStream
-- Functorial map for Expr
mapExpr
: ∀ {%A1 %A2 %V1 %V2}
→ (%mapA : ∀ {ρ τ} → %A1 ρ τ → %A2 ρ τ) → (%mapV : ∀ {τ} → %V1 τ → %V2 τ) → (∀ {τ} → Expr %A1 %V1 τ → Expr %A2 %V2 τ)
mapExpr %mapA %mapV (intr rule) = intr (mapIntr %mapA %mapV rule)
mapExpr %mapA %mapV (elim value rule) = elim (%mapV value) (mapElim %mapV rule)
-- 'AllIntr %AllA %AllV τ rule' states that:
-- 1. all instances of '%A' in 'rule' satisfy '%AllA'
-- 2. all instances of '%V' in 'rule' satisfy '%AllV'
data AllIntr
{%A : Type → Type → Set} {%V : Type → Set}
(%AllA : (ρ τ : Type) → %A ρ τ → Set) (%AllV : (τ : Type) → %V τ → Set)
: ∀ τ → Intr %A %V τ → Set where
mkAllIntrArrow : ∀ {ρ τ abs} → %AllA ρ τ abs → AllIntr _ _ (ρ ⇒ τ) (intrArrow abs)
mkAllIntrSum : ∀ {τs vᵢ} → AllAny %AllV τs vᵢ → AllIntr _ _ (#Sum τs) (intrSum vᵢ)
mkAllIntrProduct : ∀ {τs vs} → All2 %AllV τs vs → AllIntr _ _ (#Product τs) (intrProduct vs)
mkAllIntrNat : ∀ {v} → %AllV (#Maybe #Nat) v → AllIntr _ _ #Nat (intrNat v)
mkAllIntrConat : ∀ {r} → AllΣ (\ρ → All× (%AllV ρ) (%AllV (ρ ⇒ #Maybe ρ))) r → AllIntr _ _ #Conat (intrConat r)
mkAllIntrStream : ∀ {τ r} → AllΣ (\ρ → All× (%AllV ρ) (%AllV (ρ ⇒ #Pair τ ρ))) r → AllIntr _ _ (#Stream τ) (intrStream r)
-- 'AllElim %AllV τ ϕ rule' states that all instances of '%V' in 'rule' satisfy '%AllV'
data AllElim {%V : Type → Set} (%AllV : (τ : Type) → %V τ → Set) : ∀ τ ϕ → Elim %V τ ϕ → Set where
mkAllElimArrow : ∀ {ρ τ v} → %AllV ρ v → AllElim _ (ρ ⇒ τ) τ (elimArrow v)
mkAllElimSum : ∀ {τs ϕ fs} → All2 (\τ → %AllV (τ ⇒ ϕ)) τs fs → AllElim _ (#Sum τs) ϕ (elimSum fs)
mkAllElimProduct : ∀ {τs τ} → (i : Has τs τ) → AllElim _ (#Product τs) τ (elimProduct i)
mkAllElimNat : ∀ {ϕ f} → %AllV (#Maybe ϕ ⇒ ϕ) f → AllElim _ #Nat ϕ (elimNat f)
mkAllElimConat : AllElim _ #Conat (#Maybe #Conat) elimConat
mkAllElimStream : ∀ {τ} → AllElim _ (#Stream τ) (#Pair τ (#Stream τ)) elimStream
-- Functorial map for AllIntr
mapAllIntr
: {%A1 %A2 : Type → Type → Set} → {%V1 %V2 : Type → Set}
→ {%AllA1 : ∀ ρ τ → %A1 ρ τ → Set} {%AllA2 : ∀ ρ τ → %A2 ρ τ → Set} {%AllV1 : ∀ τ → %V1 τ → Set} {%AllV2 : ∀ τ → %V2 τ → Set}
→ (%mapA : ∀ {ρ τ} → %A1 ρ τ → %A2 ρ τ)
→ (%mapV : ∀ {τ} → %V1 τ → %V2 τ)
→ (%mapAllA : ∀ {ρ τ abs} → %AllA1 ρ τ abs → %AllA2 ρ τ (%mapA abs))
→ (%mapAllV : ∀ {τ v} → %AllV1 τ v → %AllV2 τ (%mapV v))
→ ∀ {τ rule} → AllIntr %AllA1 %AllV1 τ rule → AllIntr %AllA2 %AllV2 τ (mapIntr %mapA %mapV rule)
mapAllIntr %mapA %mapV %mapAllA %mapAllV (mkAllIntrArrow abs) = mkAllIntrArrow (%mapAllA abs)
mapAllIntr %mapA %mapV %mapAllA %mapAllV (mkAllIntrSum vᵢ) = mkAllIntrSum (mapAllAny %mapV %mapAllV vᵢ)
mapAllIntr %mapA %mapV %mapAllA %mapAllV (mkAllIntrProduct vs) = mkAllIntrProduct (mapAll2 %mapV %mapAllV vs)
mapAllIntr %mapA %mapV %mapAllA %mapAllV (mkAllIntrNat v) = mkAllIntrNat (%mapAllV v)
mapAllIntr %mapA %mapV %mapAllA %mapAllV (mkAllIntrConat (ρ ,, v , f)) = mkAllIntrConat (ρ ,, %mapAllV v , %mapAllV f)
mapAllIntr %mapA %mapV %mapAllA %mapAllV (mkAllIntrStream (ρ ,, v , f)) = mkAllIntrStream (ρ ,, %mapAllV v , %mapAllV f)
-- Functorial map for AllElim
mapAllElim
: {%V1 %V2 : Type → Set} {%AllV1 : ∀ τ → %V1 τ → Set} {%AllV2 : ∀ τ → %V2 τ → Set}
→ (%mapV : ∀ {τ} → %V1 τ → %V2 τ)
→ (%mapAllV : ∀ {τ v} → %AllV1 τ v → %AllV2 τ (%mapV v))
→ ∀ {τ ϕ rule} → AllElim %AllV1 τ ϕ rule → AllElim %AllV2 τ ϕ (mapElim %mapV rule)
mapAllElim %mapV %mapAllV (mkAllElimArrow v) = mkAllElimArrow (%mapAllV v)
mapAllElim %mapV %mapAllV (mkAllElimSum fs) = mkAllElimSum (mapAll2 %mapV %mapAllV fs)
mapAllElim %mapV %mapAllV (mkAllElimProduct i) = mkAllElimProduct i
mapAllElim %mapV %mapAllV (mkAllElimNat f) = mkAllElimNat (%mapAllV f)
mapAllElim %mapV %mapAllV mkAllElimConat = mkAllElimConat
mapAllElim %mapV %mapAllV mkAllElimStream = mkAllElimStream
open 4:Utensils
module 5:Term where
{-
5. Terms
-}
mutual
-- Regular de Bruijn representation of a term
data Term (Γ : List Type) (τ : Type) : Set where
-- a variable
var : Has Γ τ → Term Γ τ
-- an expression recursively containing other terms
wrap : Expr (AbsTerm Γ) (Term Γ) τ → Term Γ τ
AbsTerm : List Type → (Type → Type → Set)
AbsTerm Γ ρ τ = Term (ρ ∷ Γ) τ
-- Maps a function to each variable in a term
{-# TERMINATING #-} -- terminating because it preserves structure, inlining 'mapExpr' would convince Agda
mapTerm : ∀ {Γ Δ} → (∀ {τ} → Has Γ τ → Has Δ τ) → (∀ {τ} → Term Γ τ → Term Δ τ)
mapTerm f (var x) = var (f x)
mapTerm f (wrap expr) = wrap (mapExpr (mapTerm (has-prepend f _)) (mapTerm f) expr)
-- Expands context with one ignored variable
↑_ : ∀ {Γ ρ τ} → Term Γ τ → Term (ρ ∷ Γ) τ
↑ term = mapTerm there term
open 5:Term
module 6:SomeTerms where
{-
6. Examples of terms
-}
#lambda : ∀ {Γ σ τ} → Term (σ ∷ Γ) τ → Term Γ (σ ⇒ τ)
#lambda f = wrap (intr (intrArrow f))
#apply : ∀ {Γ σ τ} → Term Γ (σ ⇒ τ) → Term Γ σ → Term Γ τ
#apply f v = wrap (elim f (elimArrow v))
#compose : ∀ {Γ ρ σ τ} → Term Γ (σ ⇒ τ) → Term Γ (ρ ⇒ σ) → Term Γ (ρ ⇒ τ)
#compose f g = #lambda (#apply (↑ f) (#apply (↑ g) (var $0)))
#inl : ∀ {Γ σ τ} → Term Γ σ → Term Γ (#Either σ τ)
#inl v = wrap (intr (intrSum (here v)))
#inr : ∀ {Γ σ τ} → Term Γ τ → Term Γ (#Either σ τ)
#inr v = wrap (intr (intrSum (there (here v))))
#either : ∀ {Γ σ τ ϕ} → Term Γ (σ ⇒ ϕ) → Term Γ (τ ⇒ ϕ) → Term Γ (#Either σ τ) → Term Γ ϕ
#either f₁ f₂ v = wrap (elim v (elimSum (f₁ ∷ f₂ ∷ ε)))
#unit : ∀ {Γ} → Term Γ #Unit
#unit = wrap (intr (intrProduct ε))
#pair : ∀ {Γ σ τ} → Term Γ σ → Term Γ τ → Term Γ (#Pair σ τ)
#pair v₁ v₂ = wrap (intr (intrProduct (v₁ ∷ v₂ ∷ ε)))
#fst : ∀ {Γ σ τ} → Term Γ (#Pair σ τ) → Term Γ σ
#fst v = wrap (elim v (elimProduct $0))
#snd : ∀ {Γ σ τ} → Term Γ (#Pair σ τ) → Term Γ τ
#snd v = wrap (elim v (elimProduct $1))
#mapPair : ∀ {Γ ρ σ τ} → Term Γ (σ ⇒ τ) → Term Γ (#Pair ρ σ ⇒ #Pair ρ τ)
#mapPair f = #lambda (#pair (#fst (var $0)) (#apply (↑ f) (#snd (var $0))))
#nothing : ∀ {Γ τ} → Term Γ (#Maybe τ)
#nothing = #inl #unit
#just : ∀ {Γ τ} → Term Γ τ → Term Γ (#Maybe τ)
#just v = #inr v
#maybe : ∀ {Γ τ ϕ} → Term Γ ϕ → Term Γ (τ ⇒ ϕ) → Term Γ (#Maybe τ) → Term Γ ϕ
#maybe f₁ f₂ v = #either (#lambda (↑ f₁)) f₂ v
#mapMaybe : ∀ {Γ σ τ} → Term Γ (σ ⇒ τ) → Term Γ (#Maybe σ ⇒ #Maybe τ)
#mapMaybe f = #lambda (#maybe #nothing (#lambda (#just (#apply (↑ ↑ f) (var $0)))) (var $0))
#elimNat : ∀ {Γ ϕ} → Term Γ (#Maybe ϕ ⇒ ϕ) → Term Γ (#Nat ⇒ ϕ)
#elimNat f = #lambda (wrap (elim (var $0) (elimNat (↑ f))))
#buildConat : ∀ {Γ ρ} → Term Γ (ρ ⇒ #Maybe ρ) → Term Γ (ρ ⇒ #Conat)
#buildConat f = #lambda (wrap (intr (intrConat (_ ,, var $0 , ↑ f))))
#buildStream : ∀ {Γ τ ρ} → Term Γ (ρ ⇒ #Pair τ ρ) → Term Γ (ρ ⇒ #Stream τ)
#buildStream f = #lambda (wrap (intr (intrStream (_ ,, var $0 , ↑ f))))
open 6:SomeTerms
module 7:CompiledTerm where
{-
7. Compiled term representation
-}
infixr 5 _▸_
mutual
-- Compiled representation of a term
data TermC (Γ : List Type) (τ : Type) : Set where
-- 'return x': return the value of variable 'x'
return : Has Γ τ → TermC Γ τ
-- 'expr ▸ term': compute 'expr', push the result, then proceed to compute 'term'
_▸_ : ∀ {ρ} → Expr (AbsTermC Γ) (Has Γ) ρ → TermC (ρ ∷ Γ) τ → TermC Γ τ
AbsTermC : List Type → (Type → Type → Set)
AbsTermC Γ σ τ = TermC (σ ∷ Γ) τ
-- Compile-time introduction rule
IntrC : List Type → Type → Set
IntrC Γ τ = Intr (AbsTermC Γ) (Has Γ) τ
-- Compile-time elimination rule
ElimC : List Type → Type → Type → Set
ElimC Γ τ ϕ = Elim (Has Γ) τ ϕ
-- Maps a function to each variable in a term
{-# TERMINATING #-} -- terminating because it preserves structure, inlining 'mapExpr' would convince Agda
mapTermC : ∀ {Γ Δ τ} → (∀ {ϕ} → Has Γ ϕ → Has Δ ϕ) → (TermC Γ τ → TermC Δ τ)
mapTermC f (return x) = return (f x)
mapTermC f (expr ▸ term) = mapExpr (mapTermC (has-prepend f _)) f expr ▸ mapTermC (has-prepend f _) term
-- Term computing a single expression
pure : ∀ {Γ τ} → Expr (AbsTermC Γ) (Has Γ) τ → TermC Γ τ
pure expr = expr ▸ return $0
open 7:CompiledTerm
module 8:Compilation where
{-
8. Compilation
-}
infixr 5 _∷ₗ_
data Linear {Ω : Set} (%V : Ω → Set) (%E : List Ω → Set) : List Ω → Set where
εₗ : ∀ {ρs} → %E ρs → Linear %V %E ρs
_∷ₗ_ : ∀ {ρ ρs} → %V ρ → Linear %V %E (ρ ∷ ρs) → Linear %V %E ρs
mapLinear
: {Ω : Set} {%V : Ω → Set} {%E1 %E2 : List Ω → Set}
→ (∀ {τs} → %E1 τs → %E2 τs) → (∀ {τs} → Linear %V %E1 τs → Linear %V %E2 τs)
mapLinear f (εₗ x) = εₗ (f x)
mapLinear f (v ∷ₗ l) = v ∷ₗ mapLinear f l
mapLinear'
: {Ω : Set} {%V : Ω → Set} {%E1 %E2 : List Ω → Set} {Γ : List Ω}
→ (∀ {τs} → %E1 τs → %E2 (τs ++ Γ)) → (∀ {τs} → Linear %V %E1 τs → Linear %V %E2 (τs ++ Γ))
mapLinear' f (εₗ x) = εₗ (f x)
mapLinear' f (v ∷ₗ l) = v ∷ₗ mapLinear' f l
linizeAny
: {Ω : Set} {%V : Ω → Set} {τs : List Ω}
→ (κ : Ω → Ω) → Any (\τ → %V (κ τ)) τs → Linear %V (\ρs → Any (\τ → Has ρs (κ τ)) τs) ε
linizeAny κ (here v) = v ∷ₗ εₗ (here $0)
linizeAny κ (there vᵢ) = mapLinear there (linizeAny κ vᵢ)
linizeAll
: {Ω : Set} {%V : Ω → Set} {τs : List Ω}
→ (κ : Ω → Ω) → All (\τ → %V (κ τ)) τs → Linear %V (\ρs → All (\τ → Has ρs (κ τ)) τs) ε
linizeAll κ ε = εₗ ε
linizeAll κ (v ∷ vs) = v ∷ₗ mapLinear' (\vs' → has-skip _ $0 ∷ mapAll (has-append _) vs') (linizeAll κ vs)
linizeIntr : ∀ {%A %V τ} → Intr %A %V τ → Linear %V (\ρs → Intr %A (Has ρs) τ) ε
linizeIntr (intrArrow e) = εₗ (intrArrow e)
linizeIntr (intrSum vᵢ) = mapLinear intrSum (linizeAny identity vᵢ)
linizeIntr (intrProduct vs) = mapLinear intrProduct (linizeAll identity vs)
linizeIntr (intrNat v) = v ∷ₗ εₗ (intrNat $0)
linizeIntr (intrConat (ρ ,, v , f)) = v ∷ₗ f ∷ₗ εₗ (intrConat (ρ ,, $1 , $0))
linizeIntr (intrStream (ρ ,, v , f)) = v ∷ₗ f ∷ₗ εₗ (intrStream (ρ ,, $1 , $0))
linizeElim : ∀ {%V τ ϕ} → Elim %V τ ϕ → Linear %V (\ρs → Elim (Has ρs) τ ϕ) ε
linizeElim (elimArrow v) = v ∷ₗ εₗ (elimArrow $0)
linizeElim (elimSum f) = mapLinear elimSum (linizeAll (\τ → τ ⇒ _) f)
linizeElim (elimProduct i) = εₗ (elimProduct i)
linizeElim (elimNat v) = v ∷ₗ εₗ (elimNat $0)
linizeElim elimConat = εₗ elimConat
linizeElim elimStream = εₗ elimStream
linizeExpr : ∀ {%A %V τ} → Expr %A %V τ → Linear %V (\ρs → Expr %A (Has ρs) τ) ε
linizeExpr (intr rule) = mapLinear intr (linizeIntr rule)
linizeExpr (elim value rule) = value ∷ₗ mapLinear' (\rule' → elim (has-skip _ $0) (mapElim (has-append _) rule')) (linizeElim rule)
combine2 : ∀ {Γ ρ τ} → TermC Γ ρ → TermC (ρ ∷ Γ) τ → TermC Γ τ
combine2 (return x) term2 = mapTermC (has-cons x) term2
combine2 (expr ▸ term1) term2 = expr ▸ combine2 term1 (mapTermC (has-abs _ _ _) term2)
combineL : ∀ {Γ Δ τ} → Linear (TermC Γ) (\ρs → Expr (AbsTermC Γ) (Has ρs) τ) Δ → TermC (Δ ++ Γ) τ
combineL (εₗ expr) = pure (mapExpr (mapTermC (has-abs _ _ _)) (has-append _) expr)
combineL (term ∷ₗ l) = combine2 (mapTermC (has-skip _) term) (combineL l)
seqize : ∀ {Γ τ} → Expr (AbsTermC Γ) (TermC Γ) τ → TermC Γ τ
seqize expr = combineL (linizeExpr expr)
-- Transforms regular representation of a term into compiled representation
{-# TERMINATING #-} -- terminating because 'mapExpr' preserves structure, inlining 'mapExpr' would convince Agda
compile : ∀ {Γ τ} → Term Γ τ → TermC Γ τ
compile (var x) = return x
compile (wrap expr) = seqize (mapExpr compile compile expr)
open 8:Compilation
module 9:Runtime where
{-
9. Run-time term representation
-}
mutual
-- Result of a computation. Simillar to 'Term', but constructed using only introduction rules
data Value (τ : Type) : Set where
construct : Intr Closure Value τ → Value τ
-- Run-time representation of a function 'ρ ⇒ τ'. Consists of a term in context 'ρ ∷ Γ' and
-- a list of values for all types in Γ
data Closure (ρ τ : Type) : Set where
_&_ : ∀ {Γ} → Env Γ → TermC (ρ ∷ Γ) τ → Closure ρ τ
-- A list containing a value for each type in Γ
Env : List Type → Set
Env Γ = All Value Γ
-- Run-time introduction rule
IntrR : Type → Set
IntrR τ = Intr Closure Value τ
-- Run-time elimination rule
ElimR : Type → Type → Set
ElimR τ ϕ = Elim Value τ ϕ
-- Unevaluated thunk. Consists of a term and a list of values for all variables of the term
data Thunk (τ : Type) : Set where
_&_ : ∀ {Γ} → Env Γ → TermC Γ τ → Thunk τ
-- A composable sequence of closures
data CallStack : Type → Type → Set where
ε : ∀ {τ} → CallStack τ τ
_∷_ : ∀ {ρ σ τ} → Closure ρ σ → CallStack σ τ → CallStack ρ τ
-- Computation state
-- * a thunk that is currently being evaluated
-- * and a continuation which will be applied when we finish evaluating the thunk
data Machine : Type → Set where
_▹_ : ∀ {σ τ} → Thunk σ → CallStack σ τ → Machine τ
-- Result of a single computation step
-- * final value if the computation finishes
-- * next computation state if it doesn't
data Step (τ : Type) : Set where
finish : Value τ → Step τ
continue : Machine τ → Step τ
-- Plugs a value into a closure, producing a thunk
composeValueClosure : ∀ {σ τ} → Value σ → Closure σ τ → Thunk τ
composeValueClosure value (env & term) = (value ∷ env) & term
-- Composes two callstacks
composeStackStack : ∀ {ρ σ τ} → CallStack ρ σ → CallStack σ τ → CallStack ρ τ
composeStackStack ε stack2 = stack2
composeStackStack (closure ∷ stack1) stack2 = closure ∷ composeStackStack stack1 stack2
-- Appends a callstack to the current callstack of a machine
composeMachineStack : ∀ {σ τ} → Machine σ → CallStack σ τ → Machine τ
composeMachineStack (thunk ▹ stack1) stack2 = thunk ▹ composeStackStack stack1 stack2
-- Applies a value to a callstack
composeValueStack : ∀ {σ τ} → Value σ → CallStack σ τ → Step τ
composeValueStack value ε = finish value
composeValueStack value (closure ∷ stack) = continue (composeValueClosure value closure ▹ stack)
-- Composes a computation step and a callstack
-- * for a finished computation: applies the callstack to the result
-- * for an unfinished computation: append the callstack to the current callstack of the machine
composeStepStack : ∀ {σ τ} → Step σ → CallStack σ τ → Step τ
composeStepStack (finish value) stack = composeValueStack value stack
composeStepStack (continue machine) stack = continue (composeMachineStack machine stack)
-- Transforms compiled representation of a closed term into run-time representation
-- * initial environment is empty
-- * initial continuation is empty as well
load : ∀ {τ} → TermC ε τ → Machine τ
load term = (ε & term) ▹ ε
open 9:Runtime
module 10:OperationalElimination where
{-
10. Operational semantics for elimination rules
-}
-- Arrow elimination: plug the supplied value into the closure describing the function
eliminateArrow : ∀ {ρ τ ϕ} → Elim Value (ρ ⇒ τ) ϕ → Value (ρ ⇒ τ) → Thunk ϕ
eliminateArrow (elimArrow value) (construct (intrArrow closure)) = composeValueClosure value closure
-- Sum elimination: apply corresponding function to the contained element
eliminateSum : ∀ {τs ϕ} → Elim Value (#Sum τs) ϕ → Value (#Sum τs) → Thunk ϕ
eliminateSum (elimSum (f ∷ fs)) (construct (intrSum (here v))) = (f ∷ v ∷ ε) & compile (#apply (var $0) (var $1))
eliminateSum (elimSum (f ∷ fs)) (construct (intrSum (there vᵢ))) = eliminateSum (elimSum fs) (construct (intrSum vᵢ))
-- Product elimination: extract the element with the corresponding index
eliminateProduct : ∀ {τs ϕ} → Elim Value (#Product τs) ϕ → Value (#Product τs) → Thunk ϕ
eliminateProduct (elimProduct i) (construct (intrProduct vs)) = (get vs i ∷ ε) & compile (var $0)
-- Natural number elimination: given 'f : #Maybe ϕ ⇒ ϕ' and 'w : #Nat', unpack 'w' to get
-- 'v : #Maybe #Nat', apply elimination to 'v' recursively to get a value of type '#Maybe ϕ',
-- then apply 'f' to the result
eliminateNat : ∀ {ϕ} → Elim Value #Nat ϕ → Value #Nat → Thunk ϕ
eliminateNat (elimNat f) (construct (intrNat v)) =
(f ∷ v ∷ ε) & compile (#apply (#compose (var $0) (#mapMaybe (#elimNat (var $0)))) (var $1))
-- Conatural number elimination: given 'w : #Conat', unpack it to get
-- '(ρ : Type) × (v : ρ) × (f : ρ ⇒ #Maybe ρ)', apply 'f' to 'v' to get a value
-- of type '#Maybe ρ', and pack the result into a new Conat
eliminateConat : ∀ {ϕ} → Elim Value #Conat ϕ → Value #Conat → Thunk ϕ
eliminateConat elimConat (construct (intrConat (ρ ,, v , f))) =
(f ∷ v ∷ ε) & compile (#apply (#compose (#mapMaybe (#buildConat (var $0))) (var $0)) (var $1))
-- Stream elimination: given 'w : #Stream τ', unpack it to get
-- '(ρ : Type) × (v : ρ) × (f : ρ ⇒ #Pair τ ρ)', apply 'f' to 'v' to get a value
-- of type '#Pair τ ρ', and pack the result into a new Stream
eliminateStream : ∀ {τ ϕ} → Elim Value (#Stream τ) ϕ → Value (#Stream τ) → Thunk ϕ
eliminateStream elimStream (construct (intrStream (ρ ,, v , f))) =
(f ∷ v ∷ ε) & compile (#apply (#compose (#mapPair (#buildStream (var $0))) (var $0)) (var $1))
{-
We don't actually use 'eliminate' definitions for Nat, Conat and Stream given above,
because they make typechecking in next sections unbearingly slow. Instead we use
optimized equivalent definitions given below, which compute to the same value but in
less steps.
-}
eliminateNat' : ∀ {ϕ} → Elim Value #Nat ϕ → Value #Nat → Thunk ϕ
eliminateNat' (elimNat f) (construct (intrNat v)) =
(f ∷ v ∷ ε) &
( intr (intrArrow (intr (intrProduct ε) ▸ pure (intr (intrSum (here $0)))))
▸ intr (intrArrow (elim $0 (elimNat $2) ▸ pure (intr (intrSum (there (here $0))))))
▸ elim $3 (elimSum ($1 ∷ $0 ∷ ε))
▸ pure (elim $3 (elimArrow $0))
)
eliminateConat' : ∀ {ϕ} → Elim Value #Conat ϕ → Value #Conat → Thunk ϕ
eliminateConat' elimConat (construct (intrConat (ρ ,, v , f))) =
(f ∷ v ∷ ε) &
( elim $0 (elimArrow $1)
▸ intr (intrArrow (intr (intrProduct ε) ▸ pure (intr (intrSum (here $0)))))
▸ intr (intrArrow (intr (intrConat (ρ ,, $0 , $3)) ▸ pure (intr (intrSum (there (here $0))))))
▸ pure (elim $2 (elimSum ($1 ∷ $0 ∷ ε)))
)
eliminateStream' : ∀ {τ ϕ} → Elim Value (#Stream τ) ϕ → Value (#Stream τ) → Thunk ϕ
eliminateStream' elimStream (construct (intrStream (ρ ,, v , f))) =
(f ∷ v ∷ ε) &
( elim $0 (elimArrow $1)
▸ elim $0 (elimProduct $0)
▸ elim $1 (elimProduct $1)
▸ intr (intrStream (ρ ,, $0 , $3))
▸ pure (intr (intrProduct ($2 ∷ $0 ∷ ε)))
)
-- Given a run-time elimination rule and a value, produces a thunk computing the result
-- of applying the rule to the value
eliminate : ∀ {τ ϕ} → Elim Value τ ϕ → Value τ → Thunk ϕ
eliminate {ρ ⇒ τ} = eliminateArrow
eliminate {#Sum τs} = eliminateSum
eliminate {#Product τs} = eliminateProduct
eliminate {#Nat} = eliminateNat'
eliminate {#Conat} = eliminateConat'
eliminate {#Stream τ} = eliminateStream'
open 10:OperationalElimination
module 11:Operational where
{-
11. Operational semantics for the whole calculus
-}
-- Given an environment, transforms compile-time introduction rule into a run-time introduction rule
plugEnvIntr : ∀ {Γ τ} → Env Γ → Intr (AbsTermC Γ) (Has Γ) τ → Intr Closure Value τ
plugEnvIntr env rule = mapIntr (\term → env & term) (\x → get env x) rule
-- Given an environment, transforms compile-time elimination rule into a run-time elimination rule
plugEnvElim : ∀ {Γ τ ϕ} → Env Γ → Elim (Has Γ) τ ϕ → Elim Value τ ϕ
plugEnvElim env rule = mapElim (\x → get env x) rule
-- Performs a single computation step by matching on the current term
reduce : ∀ {τ} → Machine τ → Step τ
-- * 'return x': supply value of variable 'x' to the continuation stack
reduce ((env & return x) ▹ stack) = composeValueStack (get env x) stack
-- * introduction rule: compute value constructed by the introduction rule, push it into
-- the environment, and continue with computing the rest of the term
reduce ((env & (intr rule ▸ term')) ▹ stack) = continue (((value ∷ env) & term') ▹ stack)
where
value : Value _
value = construct (plugEnvIntr env rule)
-- * elimination rule: build a thunk computing the result of the elimination rule, push
-- current computation into the continuation stack, and start evaluating the new thunk
reduce ((env & (elim x rule ▸ term')) ▹ stack) = continue (thunk ▹ ((env & term') ∷ stack))
where
thunk : Thunk _
thunk = eliminate (plugEnvElim env rule) (get env x)
open 11:Operational
module 12:Locality where
{-
12. Locality lemma
-}
-- We can append a callstack to a machine either before, or after performing a reduction
-- step, the result will be the same
locality-lem
: ∀ {σ τ} → (machine : Machine σ) → (stack : CallStack σ τ)
→ composeStepStack (reduce machine) stack ≡ reduce (composeMachineStack machine stack)
locality-lem ((env & return x) ▹ ε) stack' = refl
locality-lem ((env & return x) ▹ (closure ∷ stack)) stack' = refl
locality-lem ((env & (intr rule ▸ term')) ▹ stack) stack' = refl
locality-lem ((env & (elim x rule ▸ term')) ▹ stack) stack' = refl
open 12:Locality
module 13:SomeValues where
{-
13. Examples of values
-}
^apply : ∀ {τ ϕ} → Value (τ ⇒ ϕ) → Value τ → Thunk ϕ
^apply f v = eliminateArrow (elimArrow v) f
^here : ∀ {τ τs} → Value τ → Value (#Sum (τ ∷ τs))
^here v = construct (intrSum (here v))
^there : ∀ {τ τs} → Value (#Sum τs) → Value (#Sum (τ ∷ τs))
^there (construct (intrSum vᵢ)) = construct (intrSum (there vᵢ))
^nil : Value (#Product ε)
^nil = construct (intrProduct ε)
^cons : ∀ {τ τs} → Value τ → Value (#Product τs) → Value (#Product (τ ∷ τs))
^cons v (construct (intrProduct vs)) = construct (intrProduct (v ∷ vs))
^pair : ∀ {σ τ} → Value σ → Value τ → Value (#Pair σ τ)
^pair v₁ v₂ = construct (intrProduct (v₁ ∷ v₂ ∷ ε))
^nothing : ∀ {τ} → Value (#Maybe τ)
^nothing = construct (intrSum (here ^nil))
^just : ∀ {τ} → Value τ → Value (#Maybe τ)
^just v = construct (intrSum (there (here v)))
^mkNat : Value (#Maybe #Nat) → Value #Nat
^mkNat v = construct (intrNat v)
^mkConat : (ρ : Type) → Value ρ → Value (ρ ⇒ #Maybe ρ) → Value #Conat
^mkConat ρ v f = construct (intrConat (ρ ,, v , f))
^mkStream : ∀ {τ} → (ρ : Type) → Value ρ → Value (ρ ⇒ #Pair τ ρ) → Value (#Stream τ)
^mkStream ρ v f = construct (intrStream (ρ ,, v , f))
open 13:SomeValues
module 14:Trace where
{-
14. Definition of a computation trace
-}
{-
As a convention, we prefix names related to denotational semantics with '!'.
-}
-- 'TraceStep !τ step' states that 'step' is either a value satisfying
-- predicate '!τ', or 'step' is a machine, and applying 'reduce' repeatedly
-- to that machine will eventually terminate with a value satisfying '!τ'
data TraceStep {τ} (!τ : Value τ → Set) : Step τ → Set where
!finish : {value : Value τ} → !τ value → TraceStep !τ (finish value)
!continue : {machine : Machine τ} → TraceStep !τ (reduce machine) → TraceStep !τ (continue machine)
-- 'TraceMachine !τ machine' states that applying 'reduce' repeatedly to
-- 'machine' will eventually terminate, and the final value will satisfy '!τ'
data TraceMachine {τ} (!τ : Value τ → Set) : Machine τ → Set where
!continueM : {machine : Machine τ} → TraceStep !τ (reduce machine) → TraceMachine !τ machine
-- Trace for a thunk is a trace for the machine consisting of this thunk and
-- an empty continuation
TraceThunk : ∀ {τ} → (!τ : Value τ → Set) → Thunk τ → Set
TraceThunk !τ thunk = TraceMachine !τ (thunk ▹ ε)
-- Functorial map for TraceStep
mapTraceStep
: ∀ {τ} { !τ !τ' : Value τ → Set }
→ (∀ {value} → !τ value → !τ' value) → (∀ {step} → TraceStep !τ step → TraceStep !τ' step)
mapTraceStep f (!finish !v) = !finish (f !v)
mapTraceStep f (!continue !step) = !continue (mapTraceStep f !step)
-- Functorial map for TraceMachine
mapTraceMachine
: ∀ {τ} { !τ !τ' : Value τ → Set }
→ (∀ {value} → !τ value → !τ' value) → (∀ {machine} → TraceMachine !τ machine → TraceMachine !τ' machine)
mapTraceMachine f (!continueM !step) = !continueM (mapTraceStep f !step)
-- Neat aliases for !finish, !continue and !continueM
infix 10 ◽_ ∗_ ∗ₘ_
◽_ : ∀ {τ value} { !τ : Value τ → Set } → !τ value → TraceStep !τ (finish value)
◽_ = !finish
∗_ : ∀ {τ machine} { !τ : Value τ → Set } → TraceStep !τ (reduce machine) → TraceStep !τ (continue machine)
∗_ = !continue
∗ₘ_ : ∀ {τ machine} { !τ : Value τ → Set } → TraceStep !τ (reduce machine) → TraceMachine !τ machine
∗ₘ_ = !continueM
-- Composes trace of a step and trace of a callstack
!composeStepStack
: ∀ {σ τ step stack} { !σ : Value σ → Set } { !τ : Value τ → Set }
→ TraceStep !σ step
→ (∀ {value} → !σ value → TraceStep !τ (composeValueStack value stack))
→ TraceStep !τ (composeStepStack step stack)
!composeStepStack {σ} {τ} {finish value} {stack} { !σ } { !τ } (!finish !value) !stack = !stack !value
!composeStepStack {σ} {τ} {continue machine} {stack} { !σ } { !τ } (!continue !step') !stack =
!continue (transport (TraceStep !τ) (locality-lem machine stack) (!composeStepStack !step' !stack))
-- Composes trace of a thunk and trace of a callstack
_▹!_
: ∀ {σ τ thunk stack} { !σ : Value σ → Set } { !τ : Value τ → Set }
→ TraceThunk !σ thunk
→ (∀ {value} → !σ value → TraceStep !τ (composeValueStack value stack))
→ TraceStep !τ (reduce (thunk ▹ stack))
_▹!_ {σ} {τ} {thunk} {stack} { !σ } { !τ } (!continueM !step) !stack =
transport (TraceStep !τ) (locality-lem (thunk ▹ ε) stack) (!composeStepStack !step !stack)
-- Returns final value for TraceStep
resultStep : ∀ {τ step} → { !τ : Value τ → Set } → TraceStep !τ step → Value τ
resultStep {step = finish value} (!finish _) = value
resultStep {step = continue machine} (!continue trace') = resultStep trace'
-- Returns final value for TraceMachine
result : ∀ {τ machine} → { !τ : Value τ → Set } → TraceMachine !τ machine → Value τ
result (!continueM traceStep) = resultStep traceStep
open 14:Trace
module 15:DenotationValue where
{-
15. Definition of denotation for values
-}
-- We define denotations on type τ as predicates on values of type τ
Val : Type → Set₁
Val τ = Value τ → Set
-- Denotations for a list of types
Vals : List Type → Set₁