-
Notifications
You must be signed in to change notification settings - Fork 0
/
NbE.lagda.md
2432 lines (1987 loc) · 91.7 KB
/
NbE.lagda.md
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
---
title: "Normalization by Evaluation"
author: Emmanuel Suárez Acevedo
---
### Background
This site is both an overview of normalization by evaluation and a formalization
in Agda of the algorithm for the simply typed lambda calculus, based largely on
its presentation in Chapter 2 of Andreas Abel's habilitation thesis,
"Normalization by Evaluation: Dependent Types and Impredicativity" [@nbe]. It
was compiled from a literate Agda file available
[here](https://github.com/emmanueljs1/nbe/blob/main/NbE.lagda.md) by following
the helpful advice in [this](https://jesper.sikanda.be/posts/literate-agda.html)
blog post by Jesper Cockx.
For clarity and readability, some parts of the source file are left out in this
rendering, and this will be called out when possible.
Some familiarity with Agda (e.g. such as having worked through the first part of
[Programming Languages Foundations in Agda](https://plfa.inf.ed.ac.uk/22.08/))
is assumed along with some knowledge of programming language foundations, though
the content is mostly self contained.
### Introduction
Consider the following term in the lambda calculus:
λx. (λy. y) x
This term is not in its *normal form*, that is, it can still undergo some
reductions. In this case, we can apply a beta reduction under the first binder
and arrive at:
`λx. x`
This new term is now the normal form of `λx. (λy. y) x`. What we've just done,
believe it or not, is normalization by evaluation!
Normalization by evaluation is a technique for deriving the normal form of a
term in an object language by *evaluating* the term in a meta language (a
language we are using to describe the object language). In this case, our
object language was the untyped lambda calculus, and our meta language was,
well, just plain English.
In essence, you can reduce a term by evaluating the parts that _can_ be
evaluated while leaving the parts that _can't_ untouched. That is the intuition
behind normalization by evaluation.
To actually formalize normalization by evaluation and prove its correctness in
Agda, the algorithm may seem to become less intuitive, but it will still be
based on this initial idea.
<!---
### Imports
```agda
import Relation.Binary.PropositionalEquality as Eq
open import Data.Empty using (⊥; ⊥-elim)
open import Data.Unit using (⊤; tt)
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Data.Product using (_×_; proj₁; ∃; ∃-syntax) renaming (_,_ to ⟨_,_⟩)
open import Relation.Nullary using (Dec; yes; no; ¬_)
open Eq using (refl; trans; sym; _≡_; cong; cong₂; cong-app)
open Eq.≡-Reasoning using (_≡⟨⟩_; step-≡; begin_; _∎)
module NbE where
postulate
extensionality : ∀ {A B : Set} {f g : A → B}
→ (∀ (x : A) → f x ≡ g x)
-----------------------
→ f ≡ g
```
--->
### STLC
The object language that will be used here to define normalization by evaluation
will be the simply-typed lambda calculus with `𝟙` ("unit") as a base type. It
has variables, functions, application, and a single value of type `𝟙`: `unit`.
```agda
data Type : Set where
-- unit
𝟙 : Type
-- functions
_⇒_ : ∀ (S T : Type) → Type
```
<!---
```agda
infixr 7 _⇒_
```
--->
A typing context (for which the metavariable `Γ` will be used) is either the
empty context `∅` or an extension to a context `Γ , x:S` mapping an object
language variable to a type (here, `Γ` is extended with the variable `x` mapped
to the type `S`).
The Agda definition does not actually mention variable names at all, and is
really just a list of types. This is because a de Brujin representation will be
used for variables, so instead of a name, a variable will be an index into the
list of types making up the context (i.e. a de Brujin index). For example, the
variable `x` in the context `Γ, x:S` would be represented simply as the zeroth
index.
```agda
data Ctx : Set where
∅ : Ctx
_,_ : Ctx → Type → Ctx
```
<!---
```
infixl 5 _,_
```
--->
The de Brujin index representing a variable will not actually be a natural
number "index", but rather a lookup judgements into a context. These lookup
judgements are very similar to natural numbers (and their contructors have been
named suggestively based on this similarity: `𝑍` for zero and `𝑆` for
successor). Defining them this way instead of simply using Agda's natural
numbers will allow for an index into a context to always be well-defined (and
for variables to be intrinsically typed, as will be shown in a moment).
```agda
data _∋_ : Ctx → Type → Set where
𝑍 : ∀ {Γ : Ctx} {T : Type}
---------
→ Γ , T ∋ T
𝑆_ : ∀ {Γ : Ctx} {S T : Type}
→ Γ ∋ T
---------
→ Γ , S ∋ T
```
<!---
```
infix 4 _∋_
infix 9 𝑆_
```
--->
Using these, the context `∅, x:S, y:T` can be represented along with the
variable names `"x"` and `"y"` as is done in the following example.
```agda
module Example (S T : Type) where
∅,x:S,y:T = ∅ , S , T
x : ∅,x:S,y:T ∋ S
x = 𝑆 𝑍
y : ∅,x:S,y:T ∋ T
y = 𝑍
```
STLC terms will be embedded in Agda using an *intrinsically* typed
representation. Types are defined first, and terms are only every considered
with respect to their type.
Using this representation, terms that are not well-typed don't even have to be
considered, as they are impossible to represent. An STLC term `t` embedded in
Agda as an expression of type `Γ ⊢ T` is, by definition, a well-typed STLC
term of type `T` in the context `Γ` (written as `Γ ⊢ t : T`).
For clarity, when talking about terms their representation in the STLC will be
used over their intrinsically typed representation using de Brujin indices, and
all terms will be assumed to be well-typed. (e.g. the variable `# 𝑍` will be
referred to as `Γ, x:T ⊢ x : T`, or just `x`.)
```agda
data _⊢_ (Γ : Ctx) : Type → Set where
-- unit term
unit : Γ ⊢ 𝟙
-- variable
#_ : ∀ {S : Type}
→ Γ ∋ S
-----
→ Γ ⊢ S
-- abstraction
ƛ_ : ∀ {S T : Type}
→ Γ , S ⊢ T
---------
→ Γ ⊢ S ⇒ T
-- application
_·_ : ∀ {S T : Type}
→ Γ ⊢ S ⇒ T
→ Γ ⊢ S
---------
→ Γ ⊢ T
```
<!---
```agda
infix 4 _⊢_
infix 5 ƛ_
infixl 7 _·_
infix 9 #_
```
--->
Here are some sample programs in STLC embedded here using these constructors:
```agda
module SamplePrograms where
-- Γ ⊢ λx. x : T → T
ex0 : ∀ {Γ : Ctx} {T : Type} → Γ ⊢ T ⇒ T
ex0 = ƛ # 𝑍
-- ∅ ⊢ (λx. x) unit : 𝟙
ex1 : ∅ ⊢ 𝟙
ex1 = ex0 · unit
```
With this embedding of the simply typed lambda calculus in Agda, an algorithm
for normalization by evaluation can actually be written. However, to prove
properties about the algorithm (e.g. that it is actually correct), a few more
language constructs are still needed. They are: context extension,
substitutions, and definitional equality. These will be defined before getting
into the details of the algorithm itself.
#### Context extension
When defining the algorithm for normalization by evaluation, it will be
necessary to determine whether or not a context is an extension of another. A
context `Γ′` extends another context `Γ` if every mapping in `Γ` is also in
`Γ′`.
Since contexts are really just lists in their Agda representation, `Γ′` will be
an extension of `Γ` if `Γ` is a "sublist" of `Γ′`. The relation for context
extension is defined inductively based somewhat on this idea: a context extends
itself, and given that a context `Γ′` extends another context `Γ`, an extension
of `Γ′` is still an extension of `Γ′`.
```agda
data _≤_ : Ctx → Ctx → Set where
≤-id : ∀ {Γ : Ctx} → Γ ≤ Γ
≤-ext : ∀ {Γ Γ′ : Ctx} {T : Type}
→ Γ′ ≤ Γ
----------
→ Γ′ , T ≤ Γ
```
<!---
```agda
infix 4 _≤_
```
--->
<!---
The relation is invertible: if `Γ′ ≤ Γ , T`, then `Γ′ ≤ Γ`.
```agda
invert-≤ : ∀ {Γ Γ′ : Ctx} {T : Type}
→ Γ′ ≤ Γ , T
----------
→ Γ′ ≤ Γ
invert-≤ ≤-id = ≤-ext ≤-id
invert-≤ (≤-ext x) = ≤-ext (invert-≤ x)
```
--->
The relation is antisymmetric, meaning that if `Γ′ ≤ Γ` and `Γ ≤ Γ′`, then
`Γ′` and `Γ` must be the same context. This proof is left out in the rendering,
though it is proven mutually with the fact that `Γ` is never an extension of
`Γ, x:T`.
```agda
≤-antisym : ∀ {Γ Γ′ : Ctx}
→ Γ ≤ Γ′
→ Γ′ ≤ Γ
------
→ Γ ≡ Γ′
Γ≰Γ,T : ∀ {Γ : Ctx} {T : Type} → ¬ (Γ ≤ Γ , T)
```
<!---
```agda
≤-ext-uniq-T : ∀ {Γ Γ′ : Ctx} {S T : Type}
→ Γ′ ≤ Γ , T
→ Γ′ ≤ Γ , S
----------
→ T ≡ S
≤-ext-uniq-T ≤-id ≤-id = refl
≤-ext-uniq-T ≤-id (≤-ext c) = ⊥-elim (Γ≰Γ,T c)
≤-ext-uniq-T (≤-ext c) ≤-id = ⊥-elim (Γ≰Γ,T c)
≤-ext-uniq-T (≤-ext p₁) (≤-ext p₂)
rewrite ≤-ext-uniq-T p₁ p₂ = refl
≤-antisym ≤-id Γ′≤Γ = refl
≤-antisym (≤-ext Γ≤Γ′) ≤-id = refl
≤-antisym (≤-ext {T = T₁} p₁) (≤-ext {T = T₂} p₂)
with invert-≤ p₁ | invert-≤ p₂
... | ≤→ | ≤←
with ≤-antisym ≤→ ≤←
... | refl
rewrite ≤-ext-uniq-T p₁ p₂ = refl
Γ≰Γ,T {Γ} {T} Γ≤Γ,T
with ≤-ext {T = T} (≤-id {Γ})
... | Γ,T≤Γ
with ≤-antisym Γ≤Γ,T Γ,T≤Γ
... | ()
```
--->
This relation is also transitive, a proof that follows by induction:
```agda
≤-trans : ∀ {Γ″ Γ′ Γ : Ctx}
→ Γ″ ≤ Γ′
→ Γ′ ≤ Γ
-------
→ Γ″ ≤ Γ
≤-trans ≤-id ≤-id = ≤-id
≤-trans ≤-id (≤-ext pf) = ≤-ext pf
≤-trans (≤-ext pf) ≤-id = ≤-ext pf
≤-trans (≤-ext pf₁) (≤-ext pf₂) = ≤-ext (≤-trans pf₁ (≤-ext pf₂))
```
Finally, this relation can be made decidable. Its decidability requires that
equality between contexts (and by extension, type) also be decidable; these
proofs are also left out in the rendering.
```agda
_≟Tp_ : ∀ (S T : Type) → Dec (S ≡ T)
```
<!---
```agda
𝟙 ≟Tp 𝟙 = yes refl
𝟙 ≟Tp (S ⇒ T) = no λ()
(S₁ ⇒ T₁) ≟Tp 𝟙 = no λ()
(S₁ ⇒ T₁) ≟Tp (S₂ ⇒ T₂) with S₁ ≟Tp S₂ | T₁ ≟Tp T₂
... | no ¬pf | no _ = no λ{refl → ¬pf refl}
... | no ¬pf | yes _ = no λ{refl → ¬pf refl}
... | yes _ | no ¬pf = no λ{refl → ¬pf refl}
... | yes refl | yes refl = yes refl
```
--->
```agda
_≟Ctx_ : (Γ Γ′ : Ctx) → Dec (Γ ≡ Γ′)
```
<!---
```agda
∅ ≟Ctx ∅ = yes refl
∅ ≟Ctx (_ , _) = no λ()
(_ , _) ≟Ctx ∅ = no λ()
(Γ′ , S) ≟Ctx (Γ , T) with Γ′ ≟Ctx Γ | S ≟Tp T
... | no ¬pf | no _ = no λ{refl → ¬pf refl}
... | no ¬pf | yes _ = no λ{refl → ¬pf refl}
... | yes _ | no ¬pf = no λ{refl → ¬pf refl}
... | yes refl | yes refl = yes refl
```
--->
With these, the relation can be made decidable in Agda:
```agda
_≤?_ : ∀ (Γ′ Γ : Ctx) → Dec (Γ′ ≤ Γ)
∅ ≤? ∅ = yes ≤-id
∅ ≤? (_ , _) = no λ()
(_ , _) ≤? ∅ = yes Γ≤∅ where
Γ≤∅ : ∀ {Γ : Ctx} → Γ ≤ ∅
Γ≤∅ {∅} = ≤-id
Γ≤∅ {Γ , _} = ≤-ext Γ≤∅
(Γ′ , T) ≤? Γ@(_ , _)
with (Γ′ , T) ≟Ctx Γ
... | yes refl = yes ≤-id
... | no Γ′≢Γ
with Γ′ ≤? Γ
... | yes pf = yes (≤-ext pf)
... | no ¬pf = no λ where
≤-id → Γ′≢Γ refl
(≤-ext pf) → ¬pf pf
```
#### Substitution
A parallel substitution `Γ ⊢ σ : Δ` provides a term in `Γ` to substitute for
each variable in the context `Δ`. In other words, a substitution `σ` is a
function from variables in a context to terms in another context.
```agda
Sub : Ctx → Ctx → Set
Sub Γ Δ = ∀ {T : Type} → Δ ∋ T → Γ ⊢ T
```
To actually use a substitution, an operation is needed to apply the substitution
to a term in the context being used by the substitution:
Δ ⊢ σ : Γ Δ ⊢ t : T
------------------------
Γ ⊢ t[σ] : T
This operation would allow for transforming a term `t` that is well-typed in the
context `Δ` using a substitution `σ` that maps every variable in `Δ` to a term
that is well-typed in `Γ`.
Defining this operation is actually a little tricky in Agda, because the
typical definition of the application of a substitution `σ` is not obviously
terminating. To solve this problem, it is necessary to separately define a
smaller subset of substitution: renaming.
A renaming is a specialized substitution where the only terms that can be
substituted for variables are other variables (i.e. a renaming `Γ ⊢ ρ : Δ`
provides a variable in `Γ`, not a term in `Γ`, to replace for every variable
in `Δ`).
```agda
Ren : Ctx → Ctx → Set
Ren Γ Δ = ∀ {T : Type} → Δ ∋ T → Γ ∋ T
```
Any renaming can be transformed to the more general definition for
substitutions:
```agda
ren : ∀ {Γ Δ : Ctx} → Ren Γ Δ → Sub Γ Δ
ren ρ x = # ρ x
```
Two renamings that will be especially relevant are the identity renaming (which
substitutes variables for themselves) and the shifting renaming (which shifts
all variables by 1). To indicate that these are renamings specifically (as they
will also be defined for the more general definition of substitutions), the
superscript `ʳ` is used.
```agda
idʳ : ∀ {Γ : Ctx} → Ren Γ Γ
idʳ x = x
↥ʳ : ∀ {Γ : Ctx} {T : Type} → Ren (Γ , T) Γ
↥ʳ x = 𝑆 x
```
Any two renamings can also be composed. For a renaming substitution, this is
really just function compostition. Agda's own symbol for function composition,
`∘`, is used for this reason (though again with the superscript `ʳ`).
```agda
_∘ʳ_ : ∀ {Γ Δ Σ : Ctx} → Ren Δ Γ → Ren Σ Δ → Ren Σ Γ
ρ ∘ʳ ω = λ x → ω (ρ x)
```
<!---
```agda
infixr 9 _∘ʳ_
```
--->
It is possible to define a renaming for a context `Γ′` using a context `Γ` that
it extends by composing many shifting renamings, ending finally at the identity
renaming.
```
ρ-≤ : ∀ {Γ′ Γ : Ctx} → Γ′ ≤ Γ → Ren Γ′ Γ
ρ-≤ ≤-id = idʳ
ρ-≤ (≤-ext pf) = ρ-≤ pf ∘ʳ ↥ʳ
```
The application of a renaming substituion `Γ ⊢ ρ : Δ` to a term `Δ ⊢ t : T`
rebases the term to the context `Γ`. This is done by "distributing" the
renaming substitution across all subterms of the term, renaming all variables
used in the term with their corresponding variable in `Γ`. When going under a
binder, the renaming substitution has to be "extended" (`ext`) to now use the
newly bound variable `𝑍`.
```agda
ext : ∀ {Γ Δ : Ctx} → Ren Γ Δ → ∀ {T : Type} → Ren (Γ , T) (Δ , T)
ext ρ 𝑍 = 𝑍
ext ρ (𝑆 x) = 𝑆 ρ x
_[_]ʳ : ∀ {Γ Δ : Ctx} {T : Type}
→ Δ ⊢ T
→ Ren Γ Δ
-------
→ Γ ⊢ T
unit [ _ ]ʳ = unit
# x [ ρ ]ʳ = # ρ x
(ƛ t) [ ρ ]ʳ = ƛ t [ ext ρ ]ʳ
(r · s) [ ρ ]ʳ = r [ ρ ]ʳ · s [ ρ ]ʳ
```
<!---
```agda
infix 8 _[_]ʳ
```
--->
With the application of a renaming substitution, it is now possible to define
the application of any general substitution such that it is guaranteed to
terminate. Extending the terms in the substitution under a binder requires
applying a shifting substitution to every term in the binder. By defining
the application of renaming substitutions separately, extending a substitution
can be done such that the overall definition of the application `Γ ⊢ t [ σ ]: T`
of a substitution `Γ ⊢ σ : Δ` is guaranteed to be terminating. The definition is
very similar to the more specific application of a renaming substitution, except
variables are now being replcaed with entire terms.
```agda
exts : ∀ {Γ Δ : Ctx} → Sub Γ Δ → ∀ {T : Type} → Sub (Γ , T) (Δ , T)
exts σ 𝑍 = # 𝑍
exts σ (𝑆 x) = (σ x) [ ↥ʳ ]ʳ
_[_] : ∀ {Γ Δ : Ctx} {T : Type}
→ Δ ⊢ T
→ Sub Γ Δ
-------
→ Γ ⊢ T
unit [ _ ] = unit
# x [ σ ] = σ x
(ƛ t) [ σ ] = ƛ t [ exts σ ]
(r · s) [ σ ] = r [ σ ] · s [ σ ]
```
<!---
```
infix 8 _[_]
```
--->
The more general identity and shifting substitutions are defined exactly as they
were for renamings, except now the variable term is used. Parallel substitutions
can also be composed, by applying a substitution `Γ ⊢ τ : Δ` to every term in a
substitution `Δ ⊢ σ : Σ`.
```agda
id : ∀ {Γ : Ctx} → Sub Γ Γ
id x = # x
↥ : ∀ {Γ : Ctx} {T : Type} → Sub (Γ , T) Γ
↥ x = # 𝑆 x
_∘_ : ∀ {Γ Δ Σ : Ctx} → Sub Δ Γ → Sub Σ Δ → Sub Σ Γ
(σ ∘ τ) x = (σ x) [ τ ]
```
Any substitution `Γ ⊢ σ : Δ` can be have a well-typed term `Γ ⊢ s : S` added to
it as well, which will be written as `Γ ⊢ σ ∷ s : Δ, x:S` (with `∷` used for
"cons"). This operation is similar to the extension `exts` of a substitution,
except that terms in the substitution do not need to be shifted (and in fact,
`exts σ` is equivalent to `(σ ∘ ↥) ∷ # 𝑍`).
```agda
_∷_ : ∀ {Γ Δ : Ctx} {S : Type} → Sub Γ Δ → Γ ⊢ S → Sub Γ (Δ , S)
(_ ∷ s) 𝑍 = s
(σ ∷ _) (𝑆 x) = σ x
```
<!---
```agda
infix 8 _∷_
infixr 9 _∘_
```
--->
Using the renaming substitution for context extension, any well-typed term in
`Γ` can be "weakened" to a well-typed term in a context `Γ′`. `≤⊢` will be used
as shorthand for the application of a weakening substitution (and in Agda, this
will look a lot like an extended judgement: `Γ′≤Γ ≤⊢ t`).
```agda
weaken : ∀ {Γ′ Γ : Ctx}
→ Γ′ ≤ Γ
--------
→ Sub Γ′ Γ
weaken pf x = # ρ-≤ pf x
_≤⊢_ : ∀ {Γ′ Γ : Ctx} {T : Type} → Γ′ ≤ Γ → Γ ⊢ T → Γ′ ⊢ T
pf ≤⊢ t = t [ weaken pf ]
```
It will similarly be useful to introduce shorthand for the application of a
shifting substitution:
```agda
_↥⊢_ : ∀ {Γ : Ctx} {T : Type} → (S : Type) → Γ ⊢ T → Γ , S ⊢ T
_ ↥⊢ t = t [ ↥ ]
```
<!---
```
infixr 5 _↥⊢_
infixr 5 _≤⊢_
```
--->
To actually prove results about terms, a number of substitution lemmas will be
necessary. Their proofs are omitted, though they are directly inspired from the
[Substitution](https://plfa.github.io/20.07/Substitution/) chapter of PLFA. The
most import ones are `sub-sub` (substitutions can be composed) and
`[id]-identity` (the application of the identity substitution is an identity).
<!---
```agda
sub-tail : ∀ {Γ Δ : Ctx} {S T : Type} {s : Γ ⊢ S} {σ : Sub Γ Δ}
→ (↥ ∘ (σ ∷ s)) {T = T} ≡ σ
sub-tail = refl
sub-dist : ∀ {Γ Δ Σ : Ctx} {S T : Type} {τ : Sub Γ Δ} {σ : Sub Δ Σ} {s : Δ ⊢ S}
→ (σ ∷ s) ∘ τ ≡ (σ ∘ τ ∷ (s [ τ ])) {T}
sub-dist {Σ = Σ} {S} {T} {τ} {σ} {s} = extensionality lemma where
lemma : ∀ (x : Σ , S ∋ T) → ((σ ∷ s) ∘ τ) x ≡ ((σ ∘ τ) ∷ (s [ τ ])) x
lemma 𝑍 = refl
lemma (𝑆 x) = refl
cong-ext : ∀ {Γ Δ : Ctx} {ρ ρ′ : Ren Γ Δ} {T : Type}
→ (∀ {S : Type} → ρ ≡ ρ′ {S})
→ ∀ {S : Type} → ext ρ {T} {S} ≡ ext ρ′
cong-ext {Δ = Δ} {ρ} {ρ′} {T} ρ≡ρ′ {S} = extensionality lemma where
lemma : ∀ (x : Δ , T ∋ S) → ext ρ x ≡ ext ρ′ x
lemma 𝑍 = refl
lemma (𝑆 x) rewrite ρ≡ρ′ {S} = refl
cong-rename : ∀ {Γ Δ : Ctx} {ρ ρ′ : Ren Γ Δ} {T : Type} {t : Δ ⊢ T}
→ (∀ {S : Type} → ρ ≡ ρ′ {S})
→ t [ ρ ]ʳ ≡ t [ ρ′ ]ʳ
cong-rename {t = unit} _ = refl
cong-rename {T = T} {# x} ρ≡ρ′ rewrite ρ≡ρ′ {T} = refl
cong-rename {ρ = ρ} {ρ′} {t = ƛ t} ρ≡ρ′
rewrite cong-rename {ρ = ext ρ} {ρ′ = ext ρ′} {t = t} (cong-ext ρ≡ρ′) = refl
cong-rename {t = r · s} ρ≡ρ′
rewrite cong-rename {t = r} ρ≡ρ′ | cong-rename {t = s} ρ≡ρ′ = refl
cong-exts : ∀ {Γ Δ : Ctx} {σ σ′ : Sub Γ Δ} {T : Type}
→ (∀ {S : Type} → σ ≡ σ′ {S})
→ (∀ {S : Type} → exts σ {T} {S} ≡ exts σ′)
cong-exts {Δ = Δ} {σ} {σ′} {T} σ≡σ′ {S} = extensionality lemma where
lemma : ∀ (x : Δ , T ∋ S) → exts σ x ≡ exts σ′ x
lemma 𝑍 = refl
lemma (𝑆 x) rewrite σ≡σ′ {S} = refl
cong-sub : ∀ {Γ Δ : Ctx} {σ σ′ : Sub Γ Δ} {T : Type} {t t′ : Δ ⊢ T}
→ (∀ {S : Type} → σ ≡ σ′ {S})
→ t ≡ t′
→ t [ σ ] ≡ t′ [ σ′ ]
cong-sub {t = unit} {unit} _ _ = refl
cong-sub {T = T} {t = # x} σ≡σ′ refl rewrite σ≡σ′ {T} = refl
cong-sub {σ = σ} {σ′} {t = ƛ t} σ≡σ′ refl
rewrite cong-sub {σ = exts σ} {exts σ′} {t = t} (cong-exts σ≡σ′) refl = refl
cong-sub {σ = σ} {σ′} {t = r · s} σ≡σ′ refl
rewrite cong-sub {σ = σ} {σ′} {t = r} σ≡σ′ refl
| cong-sub {σ = σ} {σ′} {t = s} σ≡σ′ refl = refl
cong-cons : ∀ {Γ Δ : Ctx} {S : Type} {s s′ : Γ ⊢ S} {σ τ : Sub Γ Δ}
→ s ≡ s′
→ (∀ {T : Type} → σ {T} ≡ τ {T})
→ ∀ {T : Type} → σ ∷ s ≡ (τ ∷ s′) {T}
cong-cons {Δ = Δ} {S} {s} {s′} {σ} {τ} refl σ≡τ {T} = extensionality lemma where
lemma : ∀ (x : Δ , S ∋ T) → (σ ∷ s) x ≡ (τ ∷ s′) x
lemma 𝑍 = refl
lemma (𝑆 x) rewrite σ≡τ {T} = refl
cong-seq : ∀ {Γ Δ Σ : Ctx} {τ τ′ : Sub Γ Δ} {σ σ′ : Sub Δ Σ}
→ (∀ {T : Type} → σ {T} ≡ σ′)
→ (∀ {T : Type} → τ {T} ≡ τ′)
→ (∀ {T : Type} → (σ ∘ τ) {T} ≡ σ′ ∘ τ′)
cong-seq {Σ = Σ} {τ} {τ′} {σ} {σ′} σ≡σ′ τ≡τ′ {T} = extensionality lemma where
lemma : ∀ (x : Σ ∋ T) → (σ ∘ τ) x ≡ (σ′ ∘ τ′) x
lemma x rewrite σ≡σ′ {T} | cong-sub {t = σ′ x} τ≡τ′ refl = refl
ren-ext : ∀ {Γ Δ : Ctx} {S T : Type} {ρ : Ren Γ Δ}
→ ren (ext ρ) ≡ exts (ren ρ) {S} {T}
ren-ext {Δ = Δ} {S} {T} {ρ} = extensionality lemma where
lemma : ∀ (x : Δ , S ∋ T) → (ren (ext ρ)) x ≡ exts (ren ρ) x
lemma 𝑍 = refl
lemma (𝑆 x) = refl
rename-subst-ren : ∀ {Γ Δ : Ctx} {T : Type} {ρ : Ren Γ Δ} {t : Δ ⊢ T}
→ t [ ρ ]ʳ ≡ t [ ren ρ ]
rename-subst-ren {t = unit} = refl
rename-subst-ren {t = # x} = refl
rename-subst-ren {ρ = ρ} {ƛ t}
rewrite rename-subst-ren {ρ = ext ρ} {t}
| cong-sub {t = t} (ren-ext {ρ = ρ}) refl = refl
rename-subst-ren {ρ = ρ} {r · s}
rewrite rename-subst-ren {ρ = ρ} {r} | rename-subst-ren {ρ = ρ} {s} = refl
ren-shift : ∀ {Γ : Ctx} {S T : Type}
→ ren ↥ʳ ≡ ↥ {Γ} {S} {T}
ren-shift {Γ} {S} {T} = extensionality lemma where
lemma : ∀ (x : Γ ∋ T) → ren ↥ʳ x ≡ ↥ x
lemma 𝑍 = refl
lemma (𝑆 x) = refl
rename-shift : ∀ {Γ : Ctx} {S T : Type} {s : Γ ⊢ S}
→ s [ ↥ʳ {T = T} ]ʳ ≡ s [ ↥ ]
rename-shift {Γ} {S} {T} {s}
rewrite rename-subst-ren {ρ = ↥ʳ {T = T}} {s}
| ren-shift {Γ} {T} {S} = refl
exts-cons-shift : ∀ {Γ Δ : Ctx} {S T : Type} {σ : Sub Γ Δ}
→ exts σ {S} {T} ≡ (σ ∘ ↥) ∷ # 𝑍
exts-cons-shift {Δ = Δ} {S} {T} {σ} = extensionality lemma where
lemma : ∀ (x : Δ , S ∋ T) → exts σ x ≡ ((σ ∘ ↥) ∷ # 𝑍) x
lemma 𝑍 = refl
lemma (𝑆 x) = rename-subst-ren
exts-ids : ∀ {Γ : Ctx} {S T : Type}
→ exts id ≡ id {Γ , S} {T}
exts-ids {Γ} {S} {T} = extensionality lemma where
lemma : ∀ (x : Γ , S ∋ T) → exts id x ≡ id x
lemma 𝑍 = refl
lemma (𝑆 x) = refl
```
--->
```agda
[id]-identity : ∀ {Γ : Ctx} {T : Type} {t : Γ ⊢ T}
→ t [ id ] ≡ t
```
<!---
```agda
[id]-identity {t = unit} = refl
[id]-identity {t = # x} = refl
[id]-identity {T = S ⇒ T} {ƛ t}
rewrite cong-sub {t = t} exts-ids refl
| [id]-identity {t = t} = refl
[id]-identity {t = r · s}
rewrite [id]-identity {t = r} | [id]-identity {t = s} = refl
sub-idR : ∀ {Γ Δ : Ctx} {σ : Sub Γ Δ} {T : Type}
→ (σ ∘ id) ≡ σ {T}
sub-idR = extensionality (λ _ → [id]-identity)
compose-ext : ∀ {Γ Δ Σ : Ctx} {ρ : Ren Δ Σ} {ω : Ren Γ Δ} {S T : Type}
→ (ext ρ) ∘ʳ (ext ω) ≡ ext (ρ ∘ʳ ω) {S} {T}
compose-ext {Σ = Σ} {ρ} {ω} {S} {T} = extensionality lemma where
lemma : ∀ (x : Σ , S ∋ T) → ((ext ρ) ∘ʳ (ext ω)) x ≡ ext (ρ ∘ʳ ω) x
lemma 𝑍 = refl
lemma (𝑆 x) = refl
compose-rename : ∀ {Γ Δ Σ : Ctx} {T : Type} {t : Σ ⊢ T} {ω : Ren Γ Δ}
{ρ : Ren Δ Σ}
→ t [ ρ ]ʳ [ ω ]ʳ ≡ t [ ρ ∘ʳ ω ]ʳ
compose-rename {t = unit} = refl
compose-rename {t = # x} = refl
compose-rename {T = S ⇒ T} {ƛ t} {ω} {ρ}
rewrite compose-rename {t = t} {ext ω} {ext ρ}
| cong-rename {t = t} (compose-ext {ρ = ρ} {ω}) = refl
compose-rename {t = r · s} {ω} {ρ}
rewrite compose-rename {t = r} {ω} {ρ}
| compose-rename {t = s} {ω} {ρ} = refl
commute-subst-rename : ∀ {Γ Δ Σ Φ : Ctx} {T : Type} {t : Σ ⊢ T}
{σ : Sub Γ Δ} {ρ : Ren Δ Σ}
{ρ′ : Ren Γ Φ } {σ′ : Sub Φ Σ}
→ (∀ {S : Type} {x : Σ ∋ S} → σ (ρ x) ≡ σ′ x [ ρ′ ]ʳ)
→ t [ ρ ]ʳ [ σ ] ≡ t [ σ′ ] [ ρ′ ]ʳ
commute-subst-rename {t = unit} pf = refl
commute-subst-rename {t = # x} pf = pf
commute-subst-rename {Σ = Σ} {T = S ⇒ T} {ƛ t} {σ} {ρ} {ρ′} {σ′} pf =
cong ƛ_ (commute-subst-rename {t = t} (λ {_} {x} → H x))
where
H : ∀ {U : Type} (x : Σ , S ∋ U) → exts σ (ext ρ x) ≡ exts σ′ x [ ext ρ′ ]ʳ
H 𝑍 = refl
H (𝑆 x) rewrite pf {x = x}
| compose-rename {t = σ′ x} {↥ʳ {T = S}} {ρ′}
| compose-rename {t = σ′ x} {ext ρ′ {S}} {↥ʳ} = refl
commute-subst-rename {t = r · s} {σ} {ρ} {ρ′} {σ′} pf
rewrite commute-subst-rename {t = r} {σ} {ρ} {ρ′} {σ′} pf
| commute-subst-rename {t = s} {σ} {ρ} {ρ′} {σ′} pf = refl
sub-𝑆-shift : ∀ {Γ Δ : Ctx} {S T : Type} {σ : Sub Γ (Δ , S)} {x : Δ ∋ T}
→ σ (𝑆_ {T = T} x) ≡ (↥ ∘ σ) x
sub-𝑆-shift = refl
exts-seq : ∀ {Γ Δ Σ : Ctx} {τ : Sub Γ Δ} {σ : Sub Δ Σ} {S : Type}
→ ∀ {T : Type} → (exts σ ∘ exts τ) ≡ exts (σ ∘ τ) {S} {T}
exts-seq {Σ = Σ} {τ} {σ} {S} {T} = extensionality lemma where
lemma : ∀ (x : Σ , S ∋ T) → (exts σ ∘ exts τ) x ≡ exts (σ ∘ τ) x
lemma 𝑍 = refl
lemma (𝑆 x) rewrite sub-𝑆-shift {S = S} {σ = exts σ ∘ exts τ} {x} =
commute-subst-rename {t = σ x} refl
```
--->
```agda
sub-sub : ∀ {Γ Δ Σ : Ctx} {T : Type} {τ : Sub Γ Δ} {σ : Sub Δ Σ} {t : Σ ⊢ T}
→ t [ σ ] [ τ ] ≡ t [ σ ∘ τ ]
```
<!---
```agda
sub-sub {t = unit} = refl
sub-sub {t = # x} = refl
sub-sub {τ = τ} {σ} {ƛ t} =
begin
(ƛ t) [ σ ] [ τ ]
≡⟨⟩
ƛ t [ exts σ ] [ exts τ ]
≡⟨ cong ƛ_ (sub-sub {τ = exts τ} {exts σ} {t}) ⟩
ƛ t [ exts σ ∘ exts τ ]
≡⟨ cong ƛ_ (cong-sub {t = t} exts-seq refl) ⟩
ƛ t [ exts (σ ∘ τ) ]
∎
sub-sub {τ = τ} {σ} {r · s}
rewrite sub-sub {τ = τ} {σ} {r} | sub-sub {τ = τ} {σ} {s} = refl
sub-assoc : ∀ {Γ Δ Σ Φ : Ctx} {σ : Sub Δ Γ} {τ : Sub Σ Δ} {θ : Sub Φ Σ}
→ ∀ {T : Type} → (σ ∘ τ) ∘ θ ≡ (σ ∘ τ ∘ θ) {T}
sub-assoc {Γ} {σ = σ} {τ} {θ} {T} = extensionality lemma where
lemma : ∀ (x : Γ ∋ T) → ((σ ∘ τ) ∘ θ) x ≡ (σ ∘ τ ∘ θ) x
lemma x rewrite sub-sub {τ = θ} {τ} {t = σ x} = refl
subst-zero-exts-cons : ∀ {Γ Δ : Ctx} {S T : Type} {σ : Sub Γ Δ} {s : Γ ⊢ S}
→ exts σ ∘ (id ∷ s) ≡ (σ ∷ s) {T}
subst-zero-exts-cons {S = S} {T} {σ} {s} =
begin
exts σ ∘ (id ∷ s)
≡⟨ cong-seq exts-cons-shift refl ⟩
((σ ∘ ↥) ∷ # 𝑍) ∘ (id ∷ s)
≡⟨ sub-dist ⟩
((σ ∘ ↥) ∘ (id ∷ s)) ∷ s
≡⟨ cong-cons refl (sub-assoc {σ = σ}) ⟩
(σ ∘ ↥ ∘ (id ∷ s)) ∷ s
≡⟨ cong-cons refl (cong-seq {σ = σ} refl (sub-tail {s = s} {σ = id})) ⟩
(σ ∘ id) ∷ s
≡⟨ cong-cons refl (sub-idR {σ = σ}) ⟩
σ ∷ s
∎
```
--->
#### Definitional Equality
There is still one language construct left to define ─ equality. To explain why
an embedding of equality in Agda is needed, we can begin discussing
normalization by evaluation in more detail.
Normalization by evaluation is an algorithm for normalization, the process of
transforming a term into its normal form. The normal form of a term is *unique*,
being the term with all possible reductions (i.e. "computations") applied to it.
For any normalization algorithm `nf` such that `nf(t)` is the normal form of a
term `Γ ⊢ t : T`, we would expect the following properties to hold.
- `Γ ⊢ nf(t) : T` (well-typedness of normal form)
A normalization algorithm should still produce a term that is well-typed
under the context `Γ` (and with the same type)
- `⟦ nf(t) ⟧ = ⟦ t ⟧` (preservation of meaning)
The `⟦ t ⟧` notation here indicates the *denotation* of `t`, which is
equivalent to its meaning (in some meta-language).
We want an algorithm for normalization by evaluation to ensure that the
normal form of a term that is obtained is _semantically equal_ to the
original term. Put simply, this means that the two terms must have the
same meaning.
- `nf(nf(t)) = nf(t)` (idempotency)
This property refers to the "normalization" part of the algorithm ─ it
should actually produce a term that has been fully normalized, i.e. it
cannot undergo any more normalization.
Equality of functions is undecidable, so the last property is especially tricky
to prove for any algorithm in general. Instead, we will want to use
βη-equivalence, or _definitional equality_. In STLC, we have that two terms are
definitionally equal if and only if they have the same meaning. By proving that
`Γ ⊢ nf(t) = t : T`, that the normal form of a term is definitionally equal to
the original term, we will be proving that the normal form of a term preserves
the meaning of the original term.
To actually define βη-equivalence, we need to first define operations for
β-reductions and η-expansions.
A β-reduction will be the application of a substitution `t[s/x]` that
substitutes the term `s` for the variable `x` in the term `t`. In Agda, this
substitution will be the identity substitution with the term `s` added as the
first term in the substitution. For convenience, we will use `t [ s ]₀` (as we
are replacing the zeroth term in the identity substitution).
```agda
_[_]₀ : ∀ {Γ : Ctx} {S T : Type}
→ Γ , S ⊢ T
→ Γ ⊢ S
---------
→ Γ ⊢ T
_[_]₀ {Γ} {S} t s = t [ id ∷ s ]
```
<!---
```
infix 8 _[_]₀
```
--->
η-expansion for a functional term `Γ ⊢ t : S → T` will be an abstraction
`Γ ⊢ λx . t x : S → T` containing the application of a variable `Γ, x:S ⊢ x :
S` to the term `t`. The term `t` needs to have a shifting substitution applied
to it as we are using an intrinsically-typed representation (in changing the
context from `Γ` to `Γ, x:S`, the expression `t` itself also changes).
```agda
η-expand : ∀ {Γ : Ctx} {S T : Type}
→ Γ ⊢ S ⇒ T
→ Γ ⊢ S ⇒ T
η-expand {S = S} t = ƛ (S ↥⊢ t) · # 𝑍
```
With these defined, we can actually introduce definitional equality in Agda.
We use `t == t′` in Agda instead of `Γ ⊢ t = t′ : T`, though we will refer to
two terms that are definitionally equal with the latter.
```agda
data _==_ : ∀ {Γ : Ctx} {T : Type} → Γ ⊢ T → Γ ⊢ T → Set where
-- computation rule: beta reduction
β : ∀ {Γ : Ctx} {S T : Type}
{t : Γ , S ⊢ T}
{s : Γ ⊢ S}
----------------------
→ (ƛ t) · s == t [ s ]₀
-- η-expansion / function extensionality, i.e. Γ ⊢ t = Γ ⊢ λx. t x : S ⇒ T
η : ∀ {Γ : Ctx} {S T : Type}
{t : Γ ⊢ S ⇒ T}
----------------------
→ t == η-expand t
-- compatibility rules
abs-compatible : ∀ {Γ : Ctx} {S T : Type} {t t′ : Γ , S ⊢ T}
→ t == t′
-----------
→ ƛ t == ƛ t′
app-compatible : ∀ {Γ : Ctx} {S T : Type}
{r r′ : Γ ⊢ S ⇒ T} {s s′ : Γ ⊢ S}
→ r == r′
→ s == s′
----------------
→ r · s == r′ · s′
-- equivalence rules
refl⁼⁼ : ∀ {Γ : Ctx} {T : Type} {t : Γ ⊢ T}
------
→ t == t
sym⁼⁼ : ∀ {Γ : Ctx} {T : Type} {t t′ : Γ ⊢ T}
→ t == t′
-------
→ t′ == t
trans⁼⁼ : ∀ {Γ : Ctx} {T : Type} {t₁ t₂ t₃ : Γ ⊢ T}
→ t₁ == t₂
→ t₂ == t₃
--------
→ t₁ == t₃
```
<!---
```
infix 3 _==_
```
--->
For the readability of some of the proofs that will follow, it will be helpful
to have equational reasoning defined with respect to definitional equality. Its
definition is almost identical to Agda's own equational reasoning for
propositional equality, so it is left out in the rendering.
<!---
```agda
module ==-Reasoning where
infix 1 begin==_
infixr 2 _==⟨_⟩_
infix 3 _==∎
begin==_ : ∀ {Γ : Ctx} {T : Type} {t t′ : Γ ⊢ T}
→ t == t′
-------
→ t == t′
begin== pf = pf
_==⟨_⟩_ : ∀ {Γ : Ctx} {T : Type} {t₂ t₃ : Γ ⊢ T}
→ (t₁ : Γ ⊢ T)
→ t₁ == t₂
→ t₂ == t₃
--------