-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompile.agda
More file actions
221 lines (212 loc) · 11.1 KB
/
Compile.agda
File metadata and controls
221 lines (212 loc) · 11.1 KB
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
module CC.Compile where
open import Data.Nat
open import Data.List
open import Data.Product using (_×_; ∃; ∃-syntax) renaming (_,_ to ⟨_,_⟩)
open import Data.Maybe
open import Relation.Nullary using (¬_; Dec; yes; no)
open import Relation.Binary.PropositionalEquality using (_≡_; refl; subst)
open import Function using (case_of_)
open import Syntax
open import Common.Utils
open import Common.BlameLabels
open import Common.Types
open import Common.TypeBasedCast
open import Surface.SurfaceLang
renaming (`_ to `ᴳ_;
$_of_ to $ᴳ_of_;
ƛ⟦_⟧_˙_of_ to ƛᴳ⟦_⟧_˙_of_;
!_ to !ᴳ_)
open import CC.CCStatics renaming (Term to CCTerm)
open import CC.SubstPreserve using (rename-↑1-pres)
compile : ∀ {Γ gc A} (M : Term) → Γ ; gc ⊢ᴳ M ⦂ A → CCTerm
compile ($ᴳ k of ℓ) ⊢const = $ k of ℓ
compile (`ᴳ x) (⊢var x∈Γ) = ` x
compile (ƛᴳ⟦ pc ⟧ A ˙ N of ℓ) (⊢lam ⊢N) =
let N′ = compile N ⊢N in
ƛ⟦ pc ⟧ A ˙ N′ of ℓ
compile (L · M at p) (⊢app {gc = gc} {gc′} {A = A} {A′} {B} {g = g} ⊢L ⊢M A′≲A g≾gc′ gc≾gc′) =
case ⟨ ≲-prop A′≲A , ≾-prop′ gc≾gc′ , ≾-prop′ g≾gc′ ⟩ of λ where
⟨ ⟨ C , A′~C , C<:A ⟩ , ⟨ g₁ , gc<:g₁ , g₁~gc′ ⟩ , ⟨ g₂ , g<:g₂ , g₂~gc′ ⟩ ⟩ →
let g₁⋎g₂~gc′ = subst (λ □ → _ ~ₗ □) g⋎̃g≡g (consis-join-~ₗ g₁~gc′ g₂~gc′)
c~ = ~-ty ~ₗ-refl (~-fun (~ₗ-sym g₁⋎g₂~gc′) ~-refl ~-refl)
c = cast (⟦ gc′ ⟧ A ⇒ B of g) (⟦ g₁ ⋎̃ g₂ ⟧ A ⇒ B of g) p c~ in
let L′ = case gc′ ==? g₁ ⋎̃ g₂ of λ where
(yes refl) → compile L ⊢L {- skip id cast -}
(no _ ) → compile L ⊢L ⟨ c ⟩ in
let M′ = case A′ ≡? C of λ where
(yes refl) → compile M ⊢M {- skip id cast -}
(no _ ) → compile M ⊢M ⟨ cast A′ C p A′~C ⟩ in
L′ · M′
compile (if L then M else N at p) (⊢if {A = A} {B} {C} ⊢L ⊢M ⊢N A∨̃B≡C) =
case consis-join-≲-inv {A} {B} A∨̃B≡C of λ where
⟨ A≲C , B≲C ⟩ →
case ⟨ ≲-prop A≲C , ≲-prop B≲C ⟩ of λ where
⟨ ⟨ A′ , A~A′ , A′<:C ⟩ , ⟨ B′ , B~B′ , B′<:C ⟩ ⟩ →
let L′ = compile L ⊢L in
let M′ = case A ≡? A′ of λ where
(yes refl) → compile M ⊢M {- skip id cast -}
(no _ ) → compile M ⊢M ⟨ cast A A′ p A~A′ ⟩ in
let N′ = case B ≡? B′ of λ where
(yes refl) → compile N ⊢N {- skip id cast -}
(no _ ) → compile N ⊢N ⟨ cast B B′ p B~B′ ⟩ in
if L′ C M′ N′
compile (M ∶ A at p) (⊢ann {A′ = A′} ⊢M A′≲A) =
case ≲-prop A′≲A of λ where
⟨ B , A′~B , B<:A ⟩ →
case A′ ≡? B of λ where
(yes refl) → compile M ⊢M {- skip id cast -}
(no _ ) → compile M ⊢M ⟨ cast A′ B p A′~B ⟩
compile (`let M `in N) (⊢let ⊢M ⊢N) =
let M′ = compile M ⊢M in
let N′ = compile N ⊢N in
`let M′ N′
compile (ref⟦ ℓ ⟧ M at p) (⊢ref {gc = gc} {T = T} {g} ⊢M Tg≲Tℓ gc≾ℓ) =
case ≲-prop Tg≲Tℓ of λ where
⟨ A , Tg~A , A<:Tℓ ⟩ →
let M′ = case (T of g) ≡? A of λ where
(yes refl) → compile M ⊢M {- skip id cast -}
(no _ ) → compile M ⊢M ⟨ cast (T of g) A p Tg~A ⟩ in
case gc≾ℓ of λ where
≾-⋆l → ref?⟦ ℓ ⟧ M′
(≾-l ℓᶜ≼ℓ) → ref⟦ ℓ ⟧ M′
compile (!ᴳ M) (⊢deref ⊢M) =
let M′ = compile M ⊢M in ! M′
compile (L := M at p) (⊢assign {gc = gc} {A = A} {T} {g} {g₁} ⊢L ⊢M A≲Tg1 g≾g1 gc≾g1) =
case ⟨ ≲-prop A≲Tg1 , ≾-prop g≾g1 ⟩ of λ where
⟨ ⟨ B , A~B , B<:Tg1 ⟩ , ⟨ g₂ , g~g₂ , g₂<:g₁ ⟩ ⟩ →
let c~ = ~-ty g~g₂ ~ᵣ-refl
c = cast (Ref (T of g₁) of g) (Ref (T of g₁) of g₂) p c~ in
let L′ = case g ==? g₂ of λ where
(yes refl) → compile L ⊢L {- skip id cast -}
(no _ ) → compile L ⊢L ⟨ c ⟩
M′ = case A ≡? B of λ where
(yes refl) → compile M ⊢M {- skip id cast -}
(no _ ) → compile M ⊢M ⟨ cast A B p A~B ⟩ in
case gc≾g1 of λ where
(≾-l ℓᶜ≼ℓ₁) → L′ := M′
_ → L′ :=? M′
compile-preserve : ∀ {Γ gc A} (M : Term)
→ (⊢M : Γ ; gc ⊢ᴳ M ⦂ A)
-------------------------------------------------
→ (∀ {pc} → Γ ; ∅ ; gc ; pc ⊢ compile M ⊢M ⦂ A)
compile-preserve ($ᴳ k of ℓ) ⊢const = ⊢const
compile-preserve (`ᴳ x) (⊢var Γ∋x) = ⊢var Γ∋x
compile-preserve (ƛᴳ⟦ pc ⟧ A ˙ N of ℓ) (⊢lam ⊢N) = ⊢lam (compile-preserve N ⊢N)
compile-preserve (L · M at p) (⊢app {gc = gc} {gc′} {A = A} {A′} {g = g} ⊢L ⊢M A′≲A g≾gc′ gc≾gc′)
with ≲-prop A′≲A | ≾-prop′ gc≾gc′ | ≾-prop′ g≾gc′
... | ⟨ B , A′~B , B<:A ⟩ | ⟨ g₁ , gc<:g₁ , g₁~gc′ ⟩ | ⟨ g₂ , g<:g₂ , g₂~gc′ ⟩
with gc′ ==? g₁ ⋎̃ g₂ | A′ ≡? B
... | yes refl | yes refl =
⊢app (⊢sub (compile-preserve L ⊢L)
(<:-ty <:ₗ-refl (<:-fun (consis-join-<:ₗ gc<:g₁ g<:g₂) <:-refl <:-refl)))
(⊢sub (compile-preserve M ⊢M) B<:A)
... | yes refl | no _ =
⊢app (⊢sub (compile-preserve L ⊢L)
(<:-ty <:ₗ-refl (<:-fun (consis-join-<:ₗ gc<:g₁ g<:g₂) <:-refl <:-refl)))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:A)
... | no _ | yes refl =
⊢app (⊢sub (⊢cast (compile-preserve L ⊢L))
(<:-ty <:ₗ-refl (<:-fun (consis-join-<:ₗ gc<:g₁ g<:g₂) <:-refl <:-refl)))
(⊢sub (compile-preserve M ⊢M) B<:A)
... | no _ | no _ =
⊢app (⊢sub (⊢cast (compile-preserve L ⊢L))
(<:-ty <:ₗ-refl (<:-fun (consis-join-<:ₗ gc<:g₁ g<:g₂) <:-refl <:-refl)))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:A)
compile-preserve (if L then M else N at p) (⊢if {A = A} {B} {C} ⊢L ⊢M ⊢N A∨̃B≡C)
with consis-join-≲-inv {A} {B} A∨̃B≡C
... | ⟨ A≲C , B≲C ⟩
with ≲-prop A≲C | ≲-prop B≲C
... | ⟨ A′ , A~A′ , A′<:C ⟩ | ⟨ B′ , B~B′ , B′<:C ⟩
with A ≡? A′ | B ≡? B′
... | yes refl | yes refl =
⊢if (compile-preserve L ⊢L)
(⊢sub (compile-preserve M ⊢M) A′<:C)
(⊢sub (compile-preserve N ⊢N) B′<:C)
... | yes refl | no _ =
⊢if (compile-preserve L ⊢L)
(⊢sub (compile-preserve M ⊢M) A′<:C)
(⊢sub (⊢cast (compile-preserve N ⊢N)) B′<:C)
... | no _ | yes refl =
⊢if (compile-preserve L ⊢L)
(⊢sub (⊢cast (compile-preserve M ⊢M)) A′<:C)
(⊢sub (compile-preserve N ⊢N) B′<:C)
... | no _ | no _ =
⊢if (compile-preserve L ⊢L)
(⊢sub (⊢cast (compile-preserve M ⊢M)) A′<:C)
(⊢sub (⊢cast (compile-preserve N ⊢N)) B′<:C)
compile-preserve {Γ} {Σ} {A = A} (M ∶ A at p) (⊢ann {A′ = A′} ⊢M A′≲A)
with ≲-prop A′≲A
... | ⟨ B , A′~B , B<:A ⟩
with A′ ≡? B
... | yes refl = ⊢sub (compile-preserve M ⊢M) B<:A
... | no _ = ⊢sub (⊢cast (compile-preserve M ⊢M)) B<:A
compile-preserve (`let M `in N) (⊢let ⊢M ⊢N) =
⊢let (compile-preserve M ⊢M) (compile-preserve N ⊢N)
compile-preserve (ref⟦ ℓ ⟧ M at p) (⊢ref {gc = gc} {T = T} {g} ⊢M Tg≲Tℓ gc≾ℓ)
with ≲-prop Tg≲Tℓ
... | ⟨ A , Tg~A , A<:Tℓ ⟩
with gc≾ℓ
... | ≾-⋆l
with (T of g) ≡? A
... | yes refl = ⊢ref? (⊢sub (compile-preserve M ⊢M) A<:Tℓ)
... | no _ = ⊢ref? (⊢sub (⊢cast (compile-preserve M ⊢M)) A<:Tℓ)
compile-preserve (ref⟦ ℓ ⟧ M at p) (⊢ref {gc = gc} {T = T} {g} ⊢M Tg≲Tℓ gc≾ℓ)
| ⟨ A , Tg~A , A<:Tℓ ⟩ | ≾-l ℓᶜ≼ℓ {- gc = ℓᶜ -}
with (T of g) ≡? A
... | yes refl = ⊢ref (⊢sub (compile-preserve M ⊢M) A<:Tℓ) ℓᶜ≼ℓ
... | no _ = ⊢ref (⊢sub (⊢cast (compile-preserve M ⊢M)) A<:Tℓ) ℓᶜ≼ℓ
compile-preserve (!ᴳ M) (⊢deref ⊢M) = ⊢deref (compile-preserve M ⊢M)
compile-preserve (L := M at p) (⊢assign {gc = gc} {A = A} {T} {g} {g₁} ⊢L ⊢M A≲Tg1 g≾g1 gc≾g1)
with ≲-prop A≲Tg1 | ≾-prop g≾g1
... | ⟨ B , A~B , B<:Tg1 ⟩ | ⟨ g₂ , g~g₂ , g₂<:g₁ ⟩
with gc≾g1
... | ≾-l ℓᶜ≼ℓ₁ {- gc = ℓᶜ and g₁ = ℓ₁ -}
with g ==? g₂ | A ≡? B
... | yes refl | yes refl =
⊢assign (⊢sub (compile-preserve L ⊢L) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (compile-preserve M ⊢M) B<:Tg1) ℓᶜ≼ℓ₁
... | yes refl | no _ =
⊢assign (⊢sub (compile-preserve L ⊢L) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:Tg1) ℓᶜ≼ℓ₁
... | no _ | yes refl =
⊢assign (⊢sub (⊢cast (compile-preserve L ⊢L)) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (compile-preserve M ⊢M) B<:Tg1) ℓᶜ≼ℓ₁
... | no _ | no _ =
⊢assign (⊢sub (⊢cast (compile-preserve L ⊢L)) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:Tg1) ℓᶜ≼ℓ₁
compile-preserve (L := M at p) (⊢assign {A = A} {g = g} ⊢L ⊢M A≲Tg1 g≾g1 gc≾g1)
| ⟨ B , A~B , B<:Tg1 ⟩ | ⟨ g₂ , g~g₂ , g₂<:g₁ ⟩ | ≾-⋆l
with g ==? g₂ | A ≡? B
... | yes refl | yes refl =
⊢assign? (⊢sub (compile-preserve L ⊢L) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (compile-preserve M ⊢M) B<:Tg1)
... | yes refl | no _ =
⊢assign? (⊢sub (compile-preserve L ⊢L) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:Tg1)
... | no _ | yes refl =
⊢assign? (⊢sub (⊢cast (compile-preserve L ⊢L)) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (compile-preserve M ⊢M) B<:Tg1)
... | no _ | no _ =
⊢assign? (⊢sub (⊢cast (compile-preserve L ⊢L)) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:Tg1)
compile-preserve (L := M at p) (⊢assign {A = A} {g = g} ⊢L ⊢M A≲Tg1 g≾g1 gc≾g1)
| ⟨ B , A~B , B<:Tg1 ⟩ | ⟨ g₂ , g~g₂ , g₂<:g₁ ⟩ | ≾-⋆r
with g ==? g₂ | A ≡? B
... | yes refl | yes refl =
⊢assign? (⊢sub (compile-preserve L ⊢L) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (compile-preserve M ⊢M) B<:Tg1)
... | yes refl | no _ =
⊢assign? (⊢sub (compile-preserve L ⊢L) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:Tg1)
... | no _ | yes refl =
⊢assign? (⊢sub (⊢cast (compile-preserve L ⊢L)) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (compile-preserve M ⊢M) B<:Tg1)
... | no _ | no _ =
⊢assign? (⊢sub (⊢cast (compile-preserve L ⊢L)) (<:-ty g₂<:g₁ <:ᵣ-refl))
(⊢sub (⊢cast (compile-preserve M ⊢M)) B<:Tg1)
{- Compilation from Surface to CC is type-preserving. -}
compilation-preserves-type : ∀ {Γ gc A} (M : Term)
→ (⊢M : Γ ; gc ⊢ᴳ M ⦂ A)
----------------------------------------------
→ Γ ; ∅ ; gc ; low ⊢ compile M ⊢M ⦂ A
compilation-preserves-type M ⊢M = compile-preserve M ⊢M {low}