-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLambda.agda
173 lines (123 loc) · 4.94 KB
/
Lambda.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
{-
This is an example of using Abstract Binding Trees to define the
simply-typed lambda calculus and prove type safety via progress and
preservation.
-}
import Syntax
open import Level using (lift)
open import Data.List using (List; []; _∷_; length)
open import Data.Nat using (ℕ; zero; suc)
open import Data.Product using (_×_; proj₁; proj₂) renaming (_,_ to ⟨_,_⟩ )
open import Data.Unit.Polymorphic using (⊤; tt)
open import Data.Vec using (Vec) renaming ([] to []̌; _∷_ to _∷̌_)
open import Relation.Binary.PropositionalEquality using (_≡_; refl; sym)
module examples.Lambda where
open Syntax using (Sig; Rename; _•_; id; ↑; Shiftable; GSubst; ⟰; ν; ■)
data Op : Set where
op-lam : Op
op-app : Op
sig : Op → List Sig
sig op-lam = (ν ■) ∷ []
sig op-app = ■ ∷ ■ ∷ []
open Syntax.OpSig Op sig
using (`_; _⦅_⦆; cons; nil; bind; ast; _[_]; Subst; ⟪_⟫;
rename; ABT-is-Shiftable; Var-is-Quotable; ABT-is-Quotable)
renaming (ABT to Term)
pattern ƛ N = op-lam ⦅ cons (bind (ast N)) nil ⦆
infixl 7 _·_
pattern _·_ L M = op-app ⦅ cons (ast L) (cons (ast M) nil) ⦆
{------------- Examples regarding substitution -------------}
sub-app : ∀ (L M : Term) (σ : Subst) → ⟪ σ ⟫ (L · M) ≡ (⟪ σ ⟫ L) · (⟪ σ ⟫ M)
sub-app = λ L M σ → refl
sub-lam : ∀ (N : Term) (σ : Subst) → ⟪ σ ⟫ (ƛ N) ≡ ƛ (⟪ ` 0 • ⟰ σ ⟫ N)
sub-lam N σ = refl
ren-lam : ∀ (N : Term) (ρ : Rename) → rename ρ (ƛ N) ≡ ƛ (rename (0 • ⟰ ρ) N)
ren-lam N σ = refl
_ : ∀ (M L : Term) → (M • L • id) 0 ≡ M
_ = λ M L → refl
_ : ∀ (M L : Term) → (M • L • id) 1 ≡ L
_ = λ M L → refl
_ : ∀ (M L : Term) → (M • L • id) 2 ≡ ` 0
_ = λ M L → refl
_ : ∀ (M L : Term) → (M • L • id) 3 ≡ ` 1
_ = λ M L → refl
_ : ∀ (M L : Term) → ⟪ M • L • id ⟫ (` 1 · ` 0) ≡ L · M
_ = λ M L → refl
_ : ∀ (M : Term) → ⟪ M • id ⟫ (` 1 · ` 0) ≡ ` 0 · M
_ = λ M → refl
_ : ∀ (N L : Term) → ((` 1 · ` 0) [ N ] ) [ L ] ≡ (L · N [ L ])
_ = λ N L → refl
{------------- Reduction Semantics -------------}
infix 2 _—→_
data _—→_ : Term → Term → Set where
ξ-·₁ : ∀ {L L′ M : Term}
→ L —→ L′
---------------
→ L · M —→ L′ · M
ξ-·₂ : ∀ {L M M′ : Term}
→ M —→ M′
---------------
→ L · M —→ L · M′
ξ-ƛ : ∀ {N N′ : Term}
→ N —→ N′
---------------
→ (ƛ N) —→ (ƛ N′)
β-ƛ : ∀ {N M : Term}
--------------------
→ (ƛ N) · M —→ N [ M ]
_ : ∀ L M → (ƛ ((ƛ (` 0 · ` 1)) · M)) · L
—→ (ƛ (M · ` 0)) · L
_ = λ L M → ξ-·₁ (ξ-ƛ β-ƛ)
{------------- Type System -------------}
data Type : Set where
Bot : Type
_⇒_ : Type → Type → Type
open import Var
{-open import MapPreserve Op sig-}
𝑉 : List Type → Var → Type → Type → Set
𝑉 Γ x A B = A ≡ B
𝑃 : (op : Op) → Vec Type (length (sig op)) → BTypes Type (sig op) → Type → Set
𝑃 op-lam (B ∷̌ []̌) ⟨ ⟨ A , tt ⟩ , tt ⟩ A→B = A→B ≡ A ⇒ B
𝑃 op-app (A→B ∷̌ A ∷̌ []̌) ⟨ tt , ⟨ tt , tt ⟩ ⟩ B = A→B ≡ A ⇒ B
open import ABTPredicate Op sig 𝑉 𝑃
pattern ⊢` ∋x = var-p ∋x refl
pattern ⊢ƛ ⊢N eq = op-p {op = op-lam} (cons-p (bind-p (ast-p ⊢N)) nil-p) eq
pattern ⊢· ⊢L ⊢M eq = op-p {op = op-app}
(cons-p (ast-p ⊢L) (cons-p (ast-p ⊢M) nil-p)) eq
{------------- Proof of Progress -------------}
data Value : Term → Set where
V-ƛ : ∀ {N : Term}
---------------------------
→ Value (ƛ N)
data Progress (M : Term) : Set where
step : ∀ {N}
→ M —→ N
----------
→ Progress M
done :
Value M
----------
→ Progress M
progress : ∀ {M A}
→ [] ⊢ M ⦂ A
----------
→ Progress M
progress (⊢` (lift ()))
progress (⊢ƛ ⊢N _) = done V-ƛ
progress (⊢· ⊢L ⊢M _)
with progress ⊢L
... | step L—→L′ = step (ξ-·₁ L—→L′)
... | done V-ƛ = step β-ƛ
{------------- Proof of Preservation -------------}
open import SubstPreserve Op sig Type 𝑉 𝑃 (λ x → refl) (λ { refl refl → refl })
(λ x → x) (λ { refl ⊢M → ⊢M }) using (preserve-substitution)
preserve : ∀ {Γ M N A}
→ Γ ⊢ M ⦂ A
→ M —→ N
----------
→ Γ ⊢ N ⦂ A
preserve (⊢· ⊢L ⊢M refl) (ξ-·₁ L—→L′) = ⊢· (preserve ⊢L L—→L′) ⊢M refl
preserve (⊢· ⊢L ⊢M refl) (ξ-·₂ M—→M′) = ⊢· ⊢L (preserve ⊢M M—→M′) refl
preserve (⊢ƛ ⊢M refl) (ξ-ƛ M—→N) = ⊢ƛ (preserve ⊢M M—→N) refl
preserve {Γ}{(ƛ N) · M}{_}{B} (⊢· (⊢ƛ ⊢N refl) ⊢M refl) β-ƛ =
preserve-substitution N M ⊢N ⊢M