-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfree.v
316 lines (288 loc) · 7.07 KB
/
free.v
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
Require Import Coq.Unicode.Utf8.
Require Import Coq.Setoids.Setoid.
Require Import Coq.Classes.SetoidClass.
Require Import Coq.Strings.String.
Require Import Coq.Logic.FunctionalExtensionality.
Reserved Notation "⟨ x , y , .. , z ⟩".
Reserved Notation "'some' x .. y , P"
(at level 200, x binder, y binder, right associativity,
format "'[ ' '[ ' 'some' x .. y ']' , '/' P ']'").
Reserved Notation "'Σ' x .. y , P"
(at level 200, x binder, y binder, right associativity,
format "'[ ' '[ ' 'Σ' x .. y ']' , '/' P ']'").
Record someT [A] (P: A → Type) := some_intro { head: A ; tail: P head ; }.
Arguments some_intro [A P].
Arguments head [A P].
Arguments tail [A P].
Module Import SomeNotations.
Add Printing Let someT.
Notation "'some' x .. y , P" := (someT (λ x, .. (someT (λ y, P)) .. )) : type_scope.
Notation "'Σ' x .. y , P" := (someT (λ x, .. (someT (λ y, P)) .. )) : type_scope.
Notation "⟨ x , y , .. , z ⟩" := (some_intro .. (some_intro x y) .. z) : core_scope.
End SomeNotations.
Record iso A B := {
to: A → B ;
from: B → A ;
to_from x: to (from x) = x ;
from_to x: from (to x) = x ;
}.
Arguments to {A B}.
Arguments from {A B}.
Inductive sort :=
| empty | unit
| sum (A B: sort)
| prod (A B: sort)
| exp (A B: sort)
| Forall (κ: string) (A: sort)
| Forsome (κ: string) (A: sort)
| diag (κ μ: string)
| basechange (p: (string → Type) → (string → Type)) (x: sort)
.
Inductive term: sort → sort → Type :=
| id A: term A A
| compose {A B C}: term B C → term A B → term A C
| bang {A}: term A unit
| fanout {A B C}: term C A → term C B → term C (prod A B)
| π1 {A B}: term (prod A B) A
| π2 {A B}: term (prod A B) B
| absurd {A}: term empty A
| fanin {A B C}: term A C → term B C → term (sum A B) C
| i1 {A B}: term A (sum A B)
| i2 {A B}: term B (sum A B)
| curry {A B C}: term (prod A B) C → term B (exp A C)
| ev {A B}: term (prod A (exp A B)) B
| map_Forsome {A B κ}: term A B → term (Forsome κ A) (Forsome κ B)
| pure {A κ}: term A (Forsome κ A)
| join {A κ}: term (Forsome κ (Forsome κ A)) (Forsome κ A)
| swap_Forsome {A κ μ}: κ ≠ μ → term (Forsome κ (Forsome μ A)) (Forsome μ (Forsome κ A))
| necessity {A κ}: term unit A → term unit (Forall κ A)
| map_Forall {A B κ}: term A B → term (Forall κ A) (Forall κ B)
| mon {A B κ}: term (prod (Forall κ A) (Forall κ B)) (Forall κ (prod A B))
| extract {A κ}: term (Forall κ A) A
| dup {A κ}: term (Forall κ A) (Forall κ (Forall κ A))
| swap_Forall {A κ μ}:
κ ≠ μ → term (Forall κ (Forall μ A)) (Forall μ (Forall κ A))
| refl κ: term unit (diag κ κ)
| trans {i j k}: term (prod (diag i j) (diag j k)) (diag i k)
.
Infix "∘" := compose (at level 30).
Definition put (Γ: string → Type) (κ: string) (v: Type) (μ: string): Type :=
if string_dec κ μ
then
v
else
Γ μ.
Lemma put_redundant {κ Γ}: put Γ κ (Γ κ) = Γ.
Proof.
extensionality x.
unfold put.
destruct (string_dec κ x).
all: subst.
all: reflexivity.
Qed.
Lemma put_put {κ Γ x y}: put (put Γ κ x) κ y = put Γ κ y.
Proof.
extensionality μ.
unfold put.
destruct (string_dec κ μ).
all: subst.
all: reflexivity.
Qed.
Lemma put_swap {κ μ Γ x y}:
κ ≠ μ → put (put Γ κ x) μ y = put (put Γ μ y) κ x.
Proof.
intro p.
extensionality ν.
unfold put.
destruct (string_dec μ ν) eqn:q, (string_dec κ ν) eqn:r.
all: subst.
all: try reflexivity.
contradiction.
Qed.
(*
Sorts are interpreted as multi-profunctors over a groupoid [xs,V] → Set ?
Dealing with variance is a massive pain.
*)
Fixpoint op (S: sort) (Γ: string → Type): Type :=
match S with
| empty => False
| unit => True
| sum A B => op A Γ + op B Γ
| prod A B => op A Γ * op B Γ
| exp A B => ∀ Δ, (∀ s, Δ s → Γ s) → op A Δ → op B Δ
(* Still really confused about this part. I think other sources
might complicate it because they have to do a relation variant of
put? *)
| Forall κ A => ∀ v, op A (put Γ κ v)
| Forsome κ A => Σ v, op A (put Γ κ v)
| diag κ μ => ∀ Δ, (∀ s, Δ s → Γ s) → Δ κ → Δ μ
| basechange p x => ∀ Δ, (∀ s, p Δ s → Γ s) → op x Δ
end.
(* Not even so important ? *)
Definition map (S: sort) {Γ Δ: string → Type} (f: ∀ κ, Δ κ → Γ κ): op S Γ → op S Δ.
Proof.
generalize dependent Δ.
generalize dependent Γ.
induction S.
all: intros Γ Δ f x.
- cbn in *.
contradiction.
- cbn in *.
exists.
- cbn in *.
destruct x as [x'|x'].
+ left.
apply (IHS1 Γ Δ f x').
+ right.
apply (IHS2 Γ Δ f x').
- cbn in *.
apply (IHS1 Γ Δ f (fst x), IHS2 Γ Δ f (snd x)).
- cbn in *.
intros Δ' p y.
apply (x Δ').
+ intros ? ?.
apply f.
apply p.
auto.
+ auto.
- cbn in *.
intros ?.
apply (IHS (put Γ κ v)).
+ intros.
unfold put in *.
destruct (string_dec κ κ0).
* auto.
* apply f.
auto.
+ apply x.
- cbn in *.
destruct x as [h x].
exists h.
apply (IHS (put Γ κ h)).
+ intros.
unfold put in *.
destruct (string_dec κ κ0).
* auto.
* apply f.
auto.
+ apply x.
- cbn in *.
intros ? p y.
apply x.
+ intros ? z.
apply f.
apply p.
auto.
+ auto.
- cbn in *.
intros.
apply (IHS Δ0).
+ intros.
auto.
+ apply x.
intros.
apply f.
apply X.
auto.
Defined.
Definition eval {A B} (t: term A B) {κ}: op A κ → op B κ.
Proof.
generalize dependent κ.
induction t.
all: intros Γ x.
- apply x.
- apply IHt1.
apply IHt2.
auto.
- cbn in *.
exists.
- cbn in *.
split.
+ apply IHt1.
auto.
+ apply IHt2.
auto.
- apply x.
- apply x.
- cbn in x.
contradiction.
- cbn in *.
destruct x as [x' | x'].
+ apply IHt1.
auto.
+ apply IHt2.
auto.
- cbn in *.
left.
auto.
- cbn in *.
right.
auto.
- cbn in *.
intros Δ p y.
apply (IHt Δ).
refine (y, _).
apply (map _ p x).
- destruct x as [x f].
cbn in f.
refine (f _ _ x).
intros.
auto.
- cbn in *.
exists (head x).
apply IHt.
apply (tail x).
- cbn in *.
exists (Γ κ).
rewrite put_redundant.
auto.
- destruct x as [h [h' t]].
cbn in *.
exists h'.
rewrite put_put in t.
auto.
- destruct x as [h [h' t]].
cbn in *.
exists h'.
exists h.
auto.
rewrite put_swap.
1: auto.
auto.
- cbn in *.
intro v.
apply IHt.
apply x.
- cbn in *.
intro v.
apply IHt.
apply x.
- cbn in *.
destruct x as [x y].
intro v.
apply (x v, y v).
- cbn in x.
set (x' := x (Γ κ)).
rewrite put_redundant in x'.
auto.
- cbn in *.
intros.
rewrite put_put.
apply x.
- cbn in *.
intros.
rewrite put_swap.
2:auto.
apply x.
- cbn.
intros.
apply X0.
- cbn in *.
destruct x as [f g].
intros Δ p y.
apply g.
1: apply p.
apply f.
1: apply p.
auto.
Defined.