-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSimpleCoercions.agda
185 lines (153 loc) · 7.13 KB
/
SimpleCoercions.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
module SimpleCoercions where
open import Data.Nat
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 Data.Sum using (_⊎_; inj₁; inj₂)
open import Data.Product using (_×_; proj₁; proj₂; Σ; Σ-syntax)
renaming (_,_ to ⟨_,_⟩)
open import Relation.Binary.PropositionalEquality
using (_≡_;_≢_; refl; trans; sym; cong; cong₂; cong-app)
data Cast : Type → Set where
id : ∀ {A : Type} {a : Atomic A} → Cast (A ⇒ A)
inj : (A : Type) → ∀ {i : A ≢ ⋆} → Cast (A ⇒ ⋆)
proj : (B : Type) → Label → ∀ {j : B ≢ ⋆} → Cast (⋆ ⇒ B)
cfun : ∀ {A B A' B'}
→ (c : Cast (B ⇒ A)) → (d : Cast (A' ⇒ B'))
-----------------------------------------
→ Cast ((A ⇒ A') ⇒ (B ⇒ B'))
cpair : ∀ {A B A' B'}
→ (c : Cast (A ⇒ B)) → (d : Cast (A' ⇒ B'))
-----------------------------------------
→ Cast ((A `× A') ⇒ (B `× B'))
csum : ∀ {A B A' B'}
→ (c : Cast (A ⇒ B)) → (d : Cast (A' ⇒ B'))
-----------------------------------------
→ Cast ((A `⊎ A') ⇒ (B `⊎ B'))
coerce : (A : Type) → (B : Type) → ∀ {c : A ~ B} → Label → Cast (A ⇒ B)
coerce .⋆ B {unk~L} ℓ with eq-unk B
... | yes eq rewrite eq = id {⋆} {A-Unk}
... | no neq = proj B ℓ {neq}
coerce A .⋆ {unk~R} ℓ with eq-unk A
... | yes eq rewrite eq = id {⋆} {A-Unk}
... | no neq = inj A {neq}
coerce (` ι) (` ι) {base~} ℓ = id {` ι} {A-Base}
coerce (A ⇒ B) (A' ⇒ B') {fun~ c c₁} ℓ =
cfun (coerce A' A {c} (flip ℓ) ) (coerce B B' {c₁} ℓ)
coerce (A `× B) (A' `× B') {pair~ c c₁} ℓ =
cpair (coerce A A' {c} ℓ ) (coerce B B' {c₁} ℓ)
coerce (A `⊎ B) (A' `⊎ B') {sum~ c c₁} ℓ =
csum (coerce A A' {c} ℓ ) (coerce B B' {c₁} ℓ)
data Inert : ∀ {A} → Cast A → Set where
I-inj : ∀{A i} → Inert (inj A {i})
InertNotRel : ∀{A}{c : Cast A} (i1 : Inert c)(i2 : Inert c) → i1 ≡ i2
InertNotRel I-inj I-inj = refl
data Active : ∀ {A} → Cast A → Set where
A-proj : ∀{ B ℓ j} → Active (proj B ℓ {j})
A-fun : ∀{A B A' B' c d} → Active (cfun {A}{B}{A'}{B'} c d)
A-pair : ∀{A B A' B' c d} → Active (cpair {A}{B}{A'}{B'} c d)
A-sum : ∀{A B A' B' c d} → Active (csum {A}{B}{A'}{B'} c d)
A-id : ∀{A a} → Active (id {A}{a})
ActiveNotRel : ∀{A}{c : Cast A} (a1 : Active c) (a2 : Active c) → a1 ≡ a2
ActiveNotRel A-proj A-proj = refl
ActiveNotRel A-fun A-fun = refl
ActiveNotRel A-pair A-pair = refl
ActiveNotRel A-sum A-sum = refl
ActiveNotRel A-id A-id = refl
open import ParamCastCalculus Cast Inert public
ActiveOrInert : ∀{A} → (c : Cast A) → Active c ⊎ Inert c
ActiveOrInert id = inj₁ A-id
ActiveOrInert (inj A) = inj₂ I-inj
ActiveOrInert (proj B x) = inj₁ A-proj
ActiveOrInert (cfun c c₁) = inj₁ A-fun
ActiveOrInert (cpair c c₁) = inj₁ A-pair
ActiveOrInert (csum c c₁) = inj₁ A-sum
ActiveNotInert : ∀ {A} {c : Cast A} → Active c → ¬ Inert c
ActiveNotInert A-proj ()
ActiveNotInert A-fun ()
ActiveNotInert A-pair ()
ActiveNotInert A-sum ()
ActiveNotInert A-id ()
data Cross : ∀ {A} → Cast A → Set where
C-fun : ∀{A B A' B' c d} → Cross (cfun {A}{B}{A'}{B'} c d)
C-pair : ∀{A B A' B' c d} → Cross (cpair {A}{B}{A'}{B'} c d)
C-sum : ∀{A B A' B' c d} → Cross (csum {A}{B}{A'}{B'} c d)
Inert-Cross⇒ : ∀{A C D} → (c : Cast (A ⇒ (C ⇒ D))) → (i : Inert c)
→ Cross c × Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ ⇒ A₂
Inert-Cross⇒ c ()
Inert-Cross× : ∀{A C D} → (c : Cast (A ⇒ (C `× D))) → (i : Inert c)
→ Cross c × Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ `× A₂
Inert-Cross× c ()
Inert-Cross⊎ : ∀{A C D} → (c : Cast (A ⇒ (C `⊎ D))) → (i : Inert c)
→ Cross c × Σ[ A₁ ∈ Type ] Σ[ A₂ ∈ Type ] A ≡ A₁ `⊎ A₂
Inert-Cross⊎ c ()
dom : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ ⇒ A₂) ⇒ (A' ⇒ B'))) → .(Cross c)
→ Cast (A' ⇒ A₁)
dom (cfun c d) x = c
cod : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ ⇒ A₂) ⇒ (A' ⇒ B'))) → .(Cross c)
→ Cast (A₂ ⇒ B')
cod (cfun c d) x = d
fstC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `× A₂) ⇒ (A' `× B'))) → .(Cross c)
→ Cast (A₁ ⇒ A')
fstC (cpair c d) x = c
sndC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `× A₂) ⇒ (A' `× B'))) → .(Cross c)
→ Cast (A₂ ⇒ B')
sndC (cpair c d) x = d
inlC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `⊎ A₂) ⇒ (A' `⊎ B'))) → .(Cross c)
→ Cast (A₁ ⇒ A')
inlC (csum c d) x = c
inrC : ∀{A₁ A₂ A' B'} → (c : Cast ((A₁ `⊎ A₂) ⇒ (A' `⊎ B'))) → .(Cross c)
→ Cast (A₂ ⇒ B')
inrC (csum c d) x = d
baseNotInert : ∀ {A ι} → (c : Cast (A ⇒ ` ι)) → ¬ Inert c
baseNotInert c ()
idNotInert : ∀ {A} → Atomic A → (c : Cast (A ⇒ A)) → ¬ Inert c
idNotInert a (inj ⋆ {i}) I-inj = contradiction refl i
projNotInert : ∀ {B} → B ≢ ⋆ → (c : Cast (⋆ ⇒ B)) → ¬ Inert c
projNotInert j id = contradiction refl j
projNotInert j (inj .⋆) = contradiction refl j
projNotInert j (proj B x) = ActiveNotInert A-proj
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
applyCast : ∀ {Γ A B} → (M : Γ ⊢ A) → (Value M) → (c : Cast (A ⇒ B))
→ ∀ {a : Active c} → Γ ⊢ B
applyCast M v id {a} = M
applyCast M v (inj A) {()}
applyCast M v (proj B ℓ) {a} with canonical⋆ M v
... | ⟨ A' , ⟨ M' , ⟨ c , ⟨ _ , meq ⟩ ⟩ ⟩ ⟩ rewrite meq with A' `~ B
... | yes cns = M' ⟨ coerce A' B {cns} ℓ ⟩
... | no incns = blame ℓ
applyCast M v (cfun c d) {a} = eta⇒ M (cfun c d) C-fun
applyCast M v (cpair c d) {a} = eta× M (cpair c d) C-pair
applyCast M v (csum c d) {a} = eta⊎ M (csum c d) C-sum
open import CastStructure
cs : CastStruct
cs = record { precast = pcs ; applyCast = applyCast }
open import ParamCastReduction cs public
open import ParamCastDeterministic cs public
open import GTLC2CC Cast Inert (λ A B ℓ {c} → coerce A B {c} ℓ) public