-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathParamCastAuxOrig.agda
182 lines (138 loc) · 5.15 KB
/
ParamCastAuxOrig.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
open import Types
open import PreCastStructure
open import Labels
open import Data.Nat
open import Data.Product using (_×_; proj₁; proj₂; Σ; Σ-syntax) renaming (_,_ to ⟨_,_⟩)
open import Data.Sum using (_⊎_; inj₁; inj₂)
open import Data.Bool
open import Variables
open import Relation.Nullary using (¬_)
open import Relation.Nullary.Negation using (contradiction)
open import Relation.Binary.PropositionalEquality using (_≡_;_≢_; refl; trans; sym; cong; cong₂; cong-app)
open import Data.Empty using (⊥; ⊥-elim)
{-
This modules defines reduction for the Parameterized Cast Calculus
and provides a proof of progress. Preservation is guaranteed in the
way the reduction relation is defined and checked by Agda.
-}
module ParamCastAuxOrig (pcs : PreCastStruct) where
open PreCastStruct pcs
import ParamCastCalculusOrig
open ParamCastCalculusOrig Cast
{-
Before defining reduction, we first need to define Value. In cast
calculi, whether a cast forms a value or not depends on the shape of
the cast. But here we have parameterized over casts. So we must add
more parameters to tell us whether a cast is a value-forming cast or
not. So we add the parameter Inert to identify the later, and the
parameter Active to identify casts that need to be reduced. Further,
we require that all casts (at least, all the well-typed ones) can be
categorized one of these two ways, which is given by the
ActiveOrInert parameter.
The following is the definition of Value. The case for casts, M ⟨ c ⟩,
requires M to be a value and c to be an inert cast.
-}
data Value : ∀ {Γ A} → Γ ⊢ A → Set where
V-ƛ : ∀ {Γ A B} {N : Γ , A ⊢ B}
-----------
→ Value (ƛ N)
V-const : ∀ {Γ} {A : Type} {k : rep A} {f : Prim A}
------------------------
→ Value {Γ} {A} (($ k){f})
V-pair : ∀ {Γ A B} {V : Γ ⊢ A} {W : Γ ⊢ B}
→ Value V → Value W
-----------------
→ Value (cons V W)
V-inl : ∀ {Γ A B} {V : Γ ⊢ A}
→ Value V
--------------------------
→ Value {Γ} {A `⊎ B} (inl V)
V-inr : ∀ {Γ A B} {V : Γ ⊢ B}
→ Value V
--------------------------
→ Value {Γ} {A `⊎ B} (inr V)
V-cast : ∀ {Γ : Context} {A B : Type} {V : Γ ⊢ A} {c : Cast (A ⇒ B)}
{i : Inert c}
→ Value V
---------------
→ Value (V ⟨ c ⟩)
{-
A value of type ⋆ must be of the form M ⟨ c ⟩ where c is inert cast.
-}
canonical⋆ : ∀ {Γ} → (M : Γ ⊢ ⋆) → (Value M)
→ Σ[ A ∈ Type ] Σ[ M' ∈ (Γ ⊢ A) ] Σ[ c ∈ (Cast (A ⇒ ⋆)) ]
Inert c × (M ≡ (M' ⟨ c ⟩))
canonical⋆ .($ _) (V-const {k = ()})
canonical⋆ .(_ ⟨ _ ⟩) (V-cast{Γ}{A}{B}{V}{c}{i} v) =
⟨ A , ⟨ V , ⟨ c , ⟨ i , refl ⟩ ⟩ ⟩ ⟩
{-
We shall use a kind of shallow evaluation context, called a Frame,
to collapse all of the ξ rules into a single rule.
-}
data Frame : {Γ : Context} → Type → Type → Set where
F-·₁ : ∀ {Γ A B}
→ Γ ⊢ A
→ Frame {Γ} (A ⇒ B) B
F-·₂ : ∀ {Γ A B}
→ (M : Γ ⊢ A ⇒ B) → ∀{v : Value {Γ} M}
→ Frame {Γ} A B
F-if : ∀ {Γ A}
→ Γ ⊢ A
→ Γ ⊢ A
→ Frame {Γ} (` 𝔹) A
F-×₁ : ∀ {Γ A B}
→ Γ ⊢ A
→ Frame {Γ} B (A `× B)
F-×₂ : ∀ {Γ A B}
→ Γ ⊢ B
→ Frame {Γ} A (A `× B)
F-fst : ∀ {Γ A B}
→ Frame {Γ} (A `× B) A
F-snd : ∀ {Γ A B}
→ Frame {Γ} (A `× B) B
F-inl : ∀ {Γ A B}
→ Frame {Γ} A (A `⊎ B)
F-inr : ∀ {Γ A B}
→ Frame {Γ} B (A `⊎ B)
F-case : ∀ {Γ A B C}
→ Γ ⊢ A ⇒ C
→ Γ ⊢ B ⇒ C
→ Frame {Γ} (A `⊎ B) C
F-cast : ∀ {Γ A B}
→ Cast (A ⇒ B)
→ Frame {Γ} A B
{-
The plug function inserts an expression into the hole of a frame.
-}
plug : ∀{Γ A B} → Γ ⊢ A → Frame {Γ} A B → Γ ⊢ B
plug L (F-·₁ M) = L · M
plug M (F-·₂ L) = L · M
plug L (F-if M N) = if L M N
plug L (F-×₁ M) = cons M L
plug M (F-×₂ L) = cons M L
plug M (F-fst) = fst M
plug M (F-snd) = snd M
plug M (F-inl) = inl M
plug M (F-inr) = inr M
plug L (F-case M N) = case L M N
plug M (F-cast c) = M ⟨ c ⟩
eta⇒ : ∀ {Γ A B C D} → (M : Γ ⊢ A ⇒ B)
→ (c : Cast ((A ⇒ B) ⇒ (C ⇒ D)))
→ (x : Cross c)
→ Γ ⊢ C ⇒ D
eta⇒ M c x =
ƛ (((rename S_ M) · ((` Z) ⟨ dom c x ⟩)) ⟨ cod c x ⟩)
eta× : ∀ {Γ A B C D} → (M : Γ ⊢ A `× B)
→ (c : Cast ((A `× B) ⇒ (C `× D)))
→ (x : Cross c)
→ Γ ⊢ C `× D
eta× M c x =
cons (fst M ⟨ fstC c x ⟩) (snd M ⟨ sndC c x ⟩)
eta⊎ : ∀ {Γ A B C D} → (M : Γ ⊢ A `⊎ B)
→ (c : Cast ((A `⊎ B) ⇒ (C `⊎ D)))
→ (x : Cross c)
→ Γ ⊢ C `⊎ D
eta⊎ M c x =
let l = inl ((` Z) ⟨ inlC c x ⟩) in
let r = inr ((` Z) ⟨ inrC c x ⟩) in
case M (ƛ l) (ƛ r)