-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGroundInertX.agda
More file actions
227 lines (195 loc) · 10.4 KB
/
GroundInertX.agda
File metadata and controls
227 lines (195 loc) · 10.4 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
222
223
224
225
226
227
module GroundInertX where
open import Data.Nat
open import Data.Bool
open import Types
open import Variables
open import Labels
open import Relation.Nullary using (¬_; Dec; yes; no)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Binary.PropositionalEquality
using (_≡_;_≢_; refl; trans; sym; cong; cong₂; cong-app)
open import Data.Product
using (_×_; proj₁; proj₂; ∃; ∃-syntax; Σ; Σ-syntax)
renaming (_,_ to [_,_])
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Data.Empty using (⊥; ⊥-elim)
open import Data.Empty.Irrelevant renaming (⊥-elim to ⊥-elimi)
{- Definitions: Cast, Active, and Inert -}
data Cast : Type → Set where
cast : (A : Type) → (B : Type) → Label → A ~ B → Cast (A ⇒ B)
data Inert : ∀ {A} → Cast A → Set where
I-inj : ∀{A} → Ground A → (c : Cast (A ⇒ ⋆)) → Inert c
-- All cross casts are inert!
I-fun : ∀{A B A' B'} → (c : Cast ((A ⇒ B) ⇒ (A' ⇒ B'))) → Inert c
I-pair : ∀{A B A' B'} → (c : Cast ((A `× B) ⇒ (A' `× B'))) → Inert c
I-sum : ∀{A B A' B'} → (c : Cast ((A `⊎ B) ⇒ (A' `⊎ B'))) → Inert c
InertNotRel : ∀{A}{c : Cast A} (i1 : Inert c)(i2 : Inert c) → i1 ≡ i2
InertNotRel (I-inj g1 _) (I-inj g2 _)
with GroundNotRel g1 g2
... | refl = refl
InertNotRel (I-fun _) (I-fun _) = refl
InertNotRel (I-pair _) (I-pair _) = refl
InertNotRel (I-sum _) (I-sum _) = refl
data Active : ∀ {A} → Cast A → Set where
A-id : ∀{A} → {a : Atomic A} → (c : Cast (A ⇒ A)) → Active c
A-inj : ∀{A} → (c : Cast (A ⇒ ⋆)) → .(¬ Ground A) → .(A ≢ ⋆) → Active c
A-proj : ∀{B} → (c : Cast (⋆ ⇒ B)) → .(B ≢ ⋆) → Active c
ActiveNotRel : ∀{A}{c : Cast A} (a1 : Active c) (a2 : Active c) → a1 ≡ a2
ActiveNotRel (A-id {a = a1} _) (A-id {a = a2} _)
with AtomicNotRel a1 a2
... | refl = refl
ActiveNotRel (A-id _) (A-inj _ x x₁) = ⊥-elimi (x₁ refl)
ActiveNotRel (A-id _) (A-proj _ x) = ⊥-elimi (x refl)
ActiveNotRel (A-inj _ x x₁) (A-id _) = ⊥-elimi (x₁ refl)
ActiveNotRel (A-inj _ x x₁) (A-inj _ x₂ x₃) = refl
ActiveNotRel (A-inj _ x x₁) (A-proj _ x₂) = ⊥-elimi (x₁ refl)
ActiveNotRel (A-proj _ x) (A-id _) = ⊥-elimi (x refl)
ActiveNotRel (A-proj _ x) (A-inj _ x₁ x₂) = ⊥-elimi (x refl)
ActiveNotRel (A-proj _ x) (A-proj _ x₁) = refl
open import ParamCastCalculus Cast Inert public
open import GTLC2CC Cast Inert (λ A B ℓ {c} → cast A B ℓ c) public
base-consis-eq : ∀ {ι ι' : Base} → .(` ι ~ ` ι') → ι ≡ ι'
base-consis-eq {Nat} {Nat} c = refl
base-consis-eq {Int} {Int} c = refl
base-consis-eq {𝔹} {𝔹} c = refl
base-consis-eq {Unit} {Unit} c = refl
base-consis-eq {Base.Void} {Base.Void} _ = refl
base-consis-eq {Blame} {Blame} _ = refl
ActiveOrInert : ∀{A} → (c : Cast A) → Active c ⊎ Inert c
ActiveOrInert {.(⋆ ⇒ ⋆)} (cast ⋆ ⋆ ℓ A~B) = inj₁ (A-id {a = A-Unk} (cast ⋆ ⋆ ℓ A~B))
ActiveOrInert {.(⋆ ⇒ ` ι)} (cast ⋆ (` ι) ℓ A~B) = inj₁ (A-proj (cast ⋆ (` ι) ℓ A~B) (λ ()))
ActiveOrInert {.(⋆ ⇒ (B ⇒ B₁))} (cast ⋆ (B ⇒ B₁) ℓ A~B) = inj₁ (A-proj (cast ⋆ (B ⇒ B₁) ℓ A~B) (λ ()))
ActiveOrInert {.(⋆ ⇒ B `× B₁)} (cast ⋆ (B `× B₁) ℓ A~B) = inj₁ (A-proj (cast ⋆ (B `× B₁) ℓ A~B) (λ ()))
ActiveOrInert {.(⋆ ⇒ B `⊎ B₁)} (cast ⋆ (B `⊎ B₁) ℓ A~B) = inj₁ (A-proj (cast ⋆ (B `⊎ B₁) ℓ A~B) (λ ()))
ActiveOrInert {.(` ι ⇒ ⋆)} (cast (` ι) ⋆ ℓ A~B) = inj₂ (I-inj G-Base (cast (` ι) ⋆ ℓ A~B))
ActiveOrInert {.(` ι ⇒ ` ι')} (cast (` ι) (` ι') ℓ A~B)
with base-consis-eq A~B
... | refl = inj₁ (A-id {a = A-Base} (cast (` ι) (` ι) ℓ A~B))
ActiveOrInert {.((A ⇒ A₁) ⇒ ⋆)} (cast (A ⇒ A₁) ⋆ ℓ A~B)
with ground? (A ⇒ A₁)
... | yes g = inj₂ (I-inj g (cast (A ⇒ A₁) ⋆ ℓ A~B))
... | no ng = inj₁ (A-inj (cast (A ⇒ A₁) ⋆ ℓ A~B) ng (λ ()))
ActiveOrInert {.((A ⇒ A₁) ⇒ (B ⇒ B₁))} (cast (A ⇒ A₁) (B ⇒ B₁) ℓ A~B) = inj₂ (I-fun (cast (A ⇒ A₁) (B ⇒ B₁) ℓ A~B))
ActiveOrInert {.(A `× A₁ ⇒ ⋆)} (cast (A `× A₁) ⋆ ℓ A~B)
with ground? (A `× A₁)
... | yes g = inj₂ (I-inj g (cast (A `× A₁) ⋆ ℓ A~B))
... | no ng = inj₁ (A-inj (cast (A `× A₁) ⋆ ℓ A~B) ng (λ ()))
ActiveOrInert {.(A `× A₁ ⇒ B `× B₁)} (cast (A `× A₁) (B `× B₁) ℓ A~B) = inj₂ (I-pair (cast (A `× A₁) (B `× B₁) ℓ A~B))
ActiveOrInert {.(A `⊎ A₁ ⇒ ⋆)} (cast (A `⊎ A₁) ⋆ ℓ A~B)
with ground? (A `⊎ A₁)
... | yes g = inj₂ (I-inj g (cast (A `⊎ A₁) ⋆ ℓ A~B))
... | no ng = inj₁ (A-inj (cast (A `⊎ A₁) ⋆ ℓ A~B) ng (λ ()))
ActiveOrInert {.(A `⊎ A₁ ⇒ B `⊎ B₁)} (cast (A `⊎ A₁) (B `⊎ B₁) ℓ A~B) = inj₂ (I-sum (cast (A `⊎ A₁) (B `⊎ B₁) ℓ A~B))
ActiveNotInert : ∀ {A} {c : Cast A} → Active c → ¬ Inert c
ActiveNotInert (A-id c) (I-inj () .c)
ActiveNotInert (A-id {a = ()} c) (I-fun .c)
ActiveNotInert (A-inj c ¬g _) (I-inj g .c) = ⊥-elimi (¬g g)
ActiveNotInert (A-proj c neq) (I-inj _ .c) = ⊥-elimi (neq refl)
{- Cross casts: -}
data Cross : ∀ {A} → Cast A → Set where
C-fun : ∀{A B A' B' ℓ} {cn} → Cross (cast (A ⇒ B) (A' ⇒ B') ℓ cn)
C-pair : ∀{A B A' B' ℓ} {cn} → Cross (cast (A `× B) (A' `× B') ℓ cn)
C-sum : ∀{A B A' B' ℓ} {cn} → Cross (cast (A `⊎ B) (A' `⊎ B') ℓ cn)
Inert-Cross⇒ : ∀{A C D} → (c : Cast (A ⇒ (C ⇒ D))) → (i : Inert c)
→ Cross c × Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ ⇒ A₂
Inert-Cross⇒ (cast (A ⇒ B) (C ⇒ D) ℓ cn) (I-fun _) =
[ C-fun , [ A , [ B , refl ] ] ]
Inert-Cross× : ∀{A C D} → (c : Cast (A ⇒ (C `× D))) → (i : Inert c)
→ Cross c × Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ `× A₂
Inert-Cross× (cast (A `× B) (C `× D) _ _) (I-pair _) = [ C-pair , [ A , [ B , refl ] ] ]
Inert-Cross⊎ : ∀{A C D} → (c : Cast (A ⇒ (C `⊎ D))) → (i : Inert c)
→ Cross c × Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ `⊎ A₂
Inert-Cross⊎ (cast (A `⊎ B) (C `⊎ D) _ _) (I-sum _) = [ C-sum , [ A , [ B , refl ] ] ]
dom : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ ⇒ A₂) ⇒ (A' ⇒ B'))) → .(Cross c)
→ Cast (A' ⇒ A₁)
dom (cast (A₁ ⇒ A₂) (A' ⇒ B') ℓ c') x
with ~-relevant c'
... | fun~ c d = cast A' A₁ ℓ c
cod : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ ⇒ A₂) ⇒ (A' ⇒ B'))) → .(Cross c)
→ Cast (A₂ ⇒ B')
cod (cast (A₁ ⇒ A₂) (A' ⇒ B') ℓ c') x
with ~-relevant c'
... | fun~ c d = cast A₂ B' ℓ d
fstC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `× A₂) ⇒ (A' `× B'))) → .(Cross c)
→ Cast (A₁ ⇒ A')
fstC (cast (A `× B) (C `× D) ℓ c') x
with ~-relevant c'
... | pair~ c d = cast A C ℓ c
sndC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `× A₂) ⇒ (A' `× B'))) → .(Cross c)
→ Cast (A₂ ⇒ B')
sndC (cast (A `× B) (C `× D) ℓ c') x
with ~-relevant c'
... | pair~ c d = cast B D ℓ d
inlC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `⊎ A₂) ⇒ (A' `⊎ B'))) → .(Cross c)
→ Cast (A₁ ⇒ A')
inlC (cast (A `⊎ B) (C `⊎ D) ℓ c') x
with ~-relevant c'
... | sum~ c d = cast A C ℓ c
inrC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `⊎ A₂) ⇒ (A' `⊎ B'))) → .(Cross c)
→ Cast (A₂ ⇒ B')
inrC (cast (A `⊎ B) (C `⊎ D) ℓ c') x
with ~-relevant c'
... | sum~ c d = cast B D ℓ d
baseNotInert : ∀ {A ι} → (c : Cast (A ⇒ ` ι)) → ¬ Inert c
baseNotInert c ()
idNotInert : ∀ {A} → Atomic A → (c : Cast (A ⇒ A)) → ¬ Inert c
idNotInert a c = ActiveNotInert (A-id {a = a} c)
projNotInert : ∀ {B} → B ≢ ⋆ → (c : Cast (⋆ ⇒ B)) → ¬ Inert c
projNotInert j c = ActiveNotInert (A-proj c j)
open import PreCastStructure
pcs : PreCastStruct
pcs = record
{ Cast = Cast
; Inert = Inert
; Active = Active
; ActiveOrInert = ActiveOrInert
; ActiveNotInert = ActiveNotInert
; Cross = Cross
; Inert-Cross⇒ = Inert-Cross⇒
; Inert-Cross× = Inert-Cross×
; Inert-Cross⊎ = Inert-Cross⊎
; dom = dom
; cod = cod
; fstC = fstC
; sndC = sndC
; inlC = inlC
; inrC = inrC
; baseNotInert = baseNotInert
; idNotInert = idNotInert
; projNotInert = projNotInert
; InertNotRel = InertNotRel
; ActiveNotRel = ActiveNotRel
}
open import ParamCastAux pcs public
inert-ground : ∀{A} → (c : Cast (A ⇒ ⋆)) → Inert c → Ground A
inert-ground c (I-inj g .c) = g
applyCast : ∀ {Γ A B} → (M : Γ ⊢ A) → (Value M) → (c : Cast (A ⇒ B))
→ ∀ {a : Active c} → Γ ⊢ B
{- V : ι ⇒ ι —→ V -}
applyCast M v c {A-id c} = M
{- V : A ⇒ ⋆ —→ V : A ⇒ G ⇒ ⋆ -}
applyCast M v (cast A ⋆ ℓ cn) {A-inj c a-ng a-nd}
with ground A {a-nd}
... | [ G , cns ] = (M ⟨ cast A G ℓ (proj₂ cns) ⟩) ⟨ cast G ⋆ ℓ unk~R ⟩
{- V : G ⇒p ⋆ ⇒q G —→ V
V : G ⇒p ⋆ ⇒q H —→ blame q -}
applyCast M v (cast ⋆ B ℓ cn) {A-proj c b-nd}
with ground? B
... | yes b-g
with canonical⋆ M v
... | [ G , [ V , [ c' , [ i , meq ] ] ] ] rewrite meq
with gnd-eq? G B {inert-ground c' i} {b-g}
... | yes ap-b rewrite ap-b = V
... | no ap-b = blame ℓ
{- V : ⋆ ⇒ B —→ V : ⋆ ⇒ H ⇒ B -}
applyCast M v (cast ⋆ B ℓ cn) {A-proj c b-nd}
| no b-ng with ground B {b-nd}
... | [ H , [ h-g , cns ] ] =
(M ⟨ cast ⋆ H ℓ unk~L ⟩) ⟨ cast H B ℓ (Sym~ cns) ⟩
{- Since cross casts are all inert we don't have cases for them here. -}
open import CastStructure
cs : CastStruct
cs = record { precast = pcs ; applyCast = applyCast }
{- We now instantiate the module ParamCastReduction and thereby prove type safety. -}
open import ParamCastReduction cs public
open import ParamCastDeterministic cs public