-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpermutations.v
566 lines (508 loc) · 14.9 KB
/
permutations.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
(* Copyright (c) 2014, Robert Dockins *)
Require Import Setoid.
Require Import basics.
Require Import categories.
Require Import preord.
Require Import sets.
Require Import finsets.
Require Import atoms.
Require Import List.
(** * Support and finite permutations
*)
(** The [supported] record equips types with an operation to get a
finite set of atoms; this finite set is called the "support."
This record contains no axioms about the support, but is inteded
to be used with nominal types, as defined in nominal.v. We define
[supported] here because it is convenient to have notation for the
support of a finite permuatation and disjointness of support
before we can define the nominal types.
When [x] is an element of a supported type, we write [‖x‖] for
the support of [x], and when [y] is also an element of a possibly
different supported type, we write [x♯y] to mean that the
support of [x] and [y] are disjoint.
*)
Module Support.
Record supported :=
Supported
{ carrier :> Type
; support : carrier -> finset atom
}.
Definition disjoint (A B:supported) (a:A) (b:B) :=
forall v, v ∈ support A a -> v ∈ support B b -> False.
End Support.
Arguments Support.support : simpl never.
Notation supported := Support.supported.
Notation Supported := Support.Supported.
Notation "‖ x ‖" := (Support.support _ x) (at level 20, format "‖ x ‖").
Notation "a ♯ b" := (Support.disjoint _ _ a b)
(at level 25, no associativity, format "a ♯ b").
Canonical Structure atom_supp :=
Supported atom (fun x => x::nil).
Definition concat A := fold_right (@app A) nil.
Notation "'fresh' '[' x , .. , z ']'" :=
(fresh_atom (concat atom
(cons (Support.support _ x) .. (cons (Support.support _ z) nil) ..))).
(** Here we define finitely supported permuatations of atoms. Such
permutations play an important role in the developement of nominal types.
The support of a permutation is a finite set that contains all the
atoms _not_ fixed by the permutation. Note: the support may be larger
than necessary; that is, it may contain atoms that are nonetheless
fixed by the permutation. In other words, for a permutation [p] every
atom [v] is either in the support of [p] or else [p v = v]; but it
is possible that [v] may both be in the support of [p] and also that [p v = v].
Permutations form a groupoid category (with a single object; hence a group).
When [p] and [q] are permuatations, we write [p ∘ q] for the composition,
and [p⁻¹] represents the inverse permuatation. [u ⇋ v] is the permuatation
that swaps atoms [u] and [v].
Because we only deal with the finitely supported permutations, we can
prove interesting "swap induction" principles, where the inductive
step must only consider only swapping of two variables. These induction
principles witness the fact that every finitely supported permutation
is equivelant to a finite composition of swappings.
*)
Module Perm.
Record perm (A B:unit) :=
Perm
{ f : atom -> atom
; g : atom -> atom
; support : finset atom
; gf : forall x, g (f x) = x
; fg : forall x, f (g x) = x
; support_axiom : forall v, v ∈ support \/ f v = v
}.
Arguments f [A B] _ _.
Arguments g [A B] _ _.
Arguments support [A B] _.
Arguments fg [A B] _ _.
Arguments gf [A B] _ _.
Arguments support_axiom [A B] _ _.
Canonical Structure perm_support (A B:unit) : supported :=
Supported (perm A B) (@support A B).
Program Definition eq_mixin (A B:unit) : Eq.mixin_of (perm A B) :=
Eq.Mixin (perm A B) (fun a b => forall x, f a x = f b x) _ _ _.
Solve Obligations with intuition eauto.
Next Obligation.
simpl; intros. rewrite H. auto.
Qed.
Canonical Structure eq (A B:unit) : Eq.type :=
Eq.Pack (perm A B) (eq_mixin A B).
Definition ident A : perm A A :=
Perm A A (fun x => x) (fun x => x) nil
(fun x => Logic.eq_refl x)
(fun x => Logic.eq_refl x)
(fun x => or_intror (Logic.eq_refl x)).
Program Definition compose A B C (p1:perm B C) (p2:perm A B) : perm A C :=
Perm A C
(fun x => f p1 (f p2 x))
(fun x => g p2 (g p1 x))
(‖p1‖ ++ ‖p2‖)
_ _ _.
Next Obligation.
simpl; intros.
rewrite (gf p1).
rewrite (gf p2). auto.
Qed.
Next Obligation.
simpl; intros.
rewrite (fg p2).
rewrite (fg p1). auto.
Qed.
Next Obligation.
simpl; intros.
destruct (support_axiom p2 v).
left. apply app_elem; auto.
rewrite H.
destruct (support_axiom p1 v).
left. apply app_elem; auto.
right; auto.
Qed.
Definition comp_mixin : Comp.mixin_of unit perm
:= Comp.Mixin unit perm ident compose.
Canonical Structure comp : Comp.type :=
Comp.Pack unit perm comp_mixin.
Lemma category_axioms : Category.axioms unit perm eq_mixin comp_mixin.
Proof.
constructor; simpl; intros; auto.
hnf; simpl; auto.
hnf; simpl; auto.
hnf; simpl; auto.
hnf; simpl; intro.
rewrite H. rewrite H0. auto.
Qed.
Canonical Structure PERM : category
:= Category unit perm eq_mixin comp_mixin category_axioms.
Program Definition inv A B (p:perm A B) : perm B A:=
Perm B A (g p) (f p) (support p) (fg p) (gf p) _.
Next Obligation.
intros.
destruct (support_axiom p v); auto.
right. rewrite <- H at 1.
apply gf.
Qed.
Lemma inv_id1 : forall A B (p:perm A B),
p ∘ (inv A B p) ≈ id(B).
Proof.
repeat intro. simpl.
apply fg.
Qed.
Lemma inv_id2 : forall A B (p:perm A B),
inv A B p ∘ p ≈ id(A).
Proof.
repeat intro. simpl.
apply gf.
Qed.
Program Definition groupoid_mixin
:= Groupoid.Mixin
unit perm
eq_mixin
comp_mixin
inv _.
Next Obligation.
constructor.
apply inv_id1.
apply inv_id2.
Qed.
Definition groupoid :=
Groupoid
unit perm
eq_mixin
comp_mixin
category_axioms
groupoid_mixin.
Lemma f_inj : forall (p:perm tt tt) x y,
f p x = f p y -> x = y.
Proof.
intros.
rewrite <- (gf p x).
rewrite <- (gf p y).
rewrite H; auto.
Qed.
Lemma g_inj : forall (p:perm tt tt) x y,
g p x = g p y -> x = y.
Proof.
intros.
rewrite <- (fg p x).
rewrite <- (fg p y).
rewrite H; auto.
Qed.
Program Definition swap (x y:atom) : perm tt tt :=
Perm tt tt
(fun z => if string_dec x z then y
else if string_dec y z then x else z)
(fun z => if string_dec x z then y
else if string_dec y z then x else z)
(x::y::nil)
_ _ _.
Next Obligation.
simpl; intros. rename x0 into z.
destruct (string_dec x z).
destruct (string_dec x y). congruence.
destruct (string_dec y y); congruence.
destruct (string_dec y z); try congruence.
destruct (string_dec x x); congruence.
destruct (string_dec x z). congruence.
destruct (string_dec y z); try congruence.
Qed.
Next Obligation.
simpl; intros. rename x0 into z.
destruct (string_dec x z).
destruct (string_dec x y).
congruence.
destruct (string_dec y y); congruence.
destruct (string_dec y z).
destruct (string_dec x x); congruence.
destruct (string_dec x z). congruence.
destruct (string_dec y z); congruence.
Qed.
Next Obligation.
simpl; intros.
destruct (string_dec x v).
left.
apply cons_elem; auto.
left. rewrite e; auto.
destruct (string_dec y v).
left.
rewrite e.
apply cons_elem; right.
apply cons_elem; auto.
auto.
Qed.
Lemma swap_swap (p q:atom) :
swap p q ≈ swap q p.
Proof.
hnf; simpl; intros.
destruct (string_dec p x).
destruct (string_dec q x); auto. congruence.
destruct (string_dec q x); auto.
Qed.
Lemma assoc (f g h : perm tt tt) :
f ∘ (g ∘ h) ≈ (f ∘ g) ∘ h.
Proof.
hnf; simpl; intros. auto.
Qed.
Lemma swap_self_inv p q :
swap p q ∘ swap p q ≈ id(tt).
Proof.
hnf; simpl; intros.
apply (fg (swap p q)).
Qed.
Lemma swap_same_id p :
swap p p ≈ id(tt).
Proof.
hnf; simpl; intros.
destruct (string_dec p x); auto.
Qed.
Lemma swap_swizzle (p q r:atom)
(Hrp : r <> p) (Hrq : r <> q) (Hpq : p <> q) :
swap p r ∘ swap r q ≈
swap r q ∘ swap p q.
Proof.
intros. hnf. simpl. intros.
destruct (string_dec r x). subst x.
destruct (string_dec p q); auto.
elim Hpq; auto.
destruct (string_dec r q). elim Hrq; auto.
destruct (string_dec p r). elim Hrp; auto.
destruct (string_dec p r). elim Hrp; auto.
destruct (string_dec q r). elim Hrq; auto.
destruct (string_dec r r); auto. elim n4; auto.
destruct (string_dec q x). subst x.
destruct (string_dec r r). 2: elim n0.
destruct (string_dec p q).
elim Hpq; auto.
destruct (string_dec p r). elim Hrp; auto.
destruct (string_dec r p). elim Hrp; auto.
destruct (string_dec q p). elim Hpq; auto.
auto.
auto.
destruct (string_dec p x). subst x.
destruct (string_dec r q); auto.
destruct (string_dec q q); auto. elim n2; auto.
destruct (string_dec r x). contradiction.
destruct (string_dec q x); auto. contradiction.
Qed.
Lemma support_perm (p:perm tt tt) (v:atom) :
v ∈ support p <-> f p v ∈ support p.
Proof.
split; intros.
destruct (support_axiom p (f p v)); auto.
apply f_inj in H0. rewrite H0. auto.
destruct (support_axiom p v); auto.
rewrite <- H0. auto.
Qed.
Lemma support_perm' (p:perm tt tt) (v:atom) :
v ∈ support p <-> f (inv tt tt p) v ∈ support p.
Proof.
split; intros.
apply support_perm.
simpl. rewrite fg. auto.
apply support_perm in H.
simpl in H. rewrite fg in H. auto.
Qed.
End Perm.
Canonical Structure Perm.eq.
Canonical Structure Perm.comp.
Canonical Structure Perm.perm_support.
Canonical Structure Perm.PERM.
Canonical Structure Perm.groupoid.
Notation perm := (Perm.perm tt tt).
Notation PERM := Perm.PERM.
Coercion Perm.f : Perm.perm >-> Funclass.
Notation "u ⇋ v" := (Perm.swap u v) (at level 20, no associativity).
(** Permutations with disjoint support commute. *)
Lemma perm_commute (p q:perm) :
p♯q -> p ∘ q ≈ q ∘ p.
Proof.
intro.
hnf; simpl; intros.
destruct (Perm.support_axiom p x).
destruct (Perm.support_axiom q x).
elim (H x); auto.
rewrite H1.
destruct (Perm.support_axiom q (p x)).
elim (H (p x)); auto.
rewrite <- Perm.support_perm; auto.
auto.
rewrite H0.
destruct (Perm.support_axiom p (q x)).
destruct (Perm.support_axiom q x).
elim (H (q x)); auto.
rewrite <- Perm.support_perm; auto.
rewrite H2. auto.
auto.
Qed.
(** Swappings commute with any permutation by
applying the permuatation to the swapped atoms.
*)
Lemma swap_commute u w (p:perm) :
(p u ⇋ p w) ∘ p ≈ p ∘ (u ⇋ w).
Proof.
hnf; simpl; intros.
destruct (string_dec u x).
destruct (string_dec (p u) (p x)); auto.
elim n. rewrite e; auto.
destruct (string_dec (p u) (p x)); auto.
elim n.
apply (Perm.f_inj p) in e. auto.
destruct (string_dec w x).
destruct (string_dec (p w) (p x)); auto.
elim n1. rewrite e; auto.
destruct (string_dec (p w) (p x)); auto.
elim n1.
apply (Perm.f_inj p) in e. auto.
Qed.
Lemma swap_commute' u w (p:perm) :
(u ⇋ w) ∘ p ≈ p ∘ (p⁻¹ u ⇋ p⁻¹ w).
Proof.
rewrite <- swap_commute.
simpl.
rewrite Perm.fg.
rewrite Perm.fg.
reflexivity.
Qed.
(** Here we prove the swapping induction principles. The first
decomposes a permutation on the left; the second decomposes
on the right.
These induction principles are proved by induction on the
size of the support set of a permutation. At each step
we can reduce the support by applying a swap.
*)
Require Import Arith.
Lemma swap_induction (P:perm -> Prop) :
(forall p p', p ≈ p' -> P p -> P p') ->
P id ->
(forall u v (p:perm), p u = u -> u <> v -> P p -> P (u ⇋ v ∘ p)) ->
forall p, P p.
Proof.
intros Heq Hbase Hind.
induction p using (induction_ltof1 perm (fun p => length (Perm.support p))).
case_eq (Perm.support p); intros.
apply Heq with id(tt).
hnf; simpl.
intro x.
destruct (Perm.support_axiom p x); auto.
rewrite H0 in H1.
apply nil_elem in H1. elim H1.
apply Hbase.
set (f' x := (c ⇋ (p c)) (p x)).
set (g' x := Perm.g p ((c ⇋ (p c)) x)).
assert (Hgf : forall x, g' (f' x) = x).
unfold g'; unfold f'. simpl.
intro x.
destruct (string_dec c (p x)).
destruct (string_dec c (p c)).
rewrite <- e0.
rewrite e.
apply Perm.gf.
destruct (string_dec (p c) (p c)); auto.
rewrite e.
apply Perm.gf.
elim n0; auto.
destruct (string_dec (p c) (p x)); auto.
destruct (string_dec c c); auto.
rewrite e.
apply Perm.gf.
elim n0. auto.
destruct (string_dec c (p x)).
contradiction.
destruct (string_dec (p c) (p x)); auto.
contradiction.
apply Perm.gf.
assert (Hfg : forall x, f' (g' x) = x).
unfold g'; unfold f'. simpl.
intro x.
destruct (string_dec c x).
subst x.
rewrite Perm.fg.
destruct (string_dec (p c) (p c)).
destruct (string_dec c (p c)); auto.
elim n; auto.
rewrite Perm.fg.
destruct (string_dec (p c) x).
rewrite e.
destruct (string_dec c c); auto.
elim n0. auto.
destruct (string_dec c x); auto.
contradiction.
destruct (string_dec (p c) x); auto.
contradiction.
assert (Hsupport : forall v, v ∈ (l:finset atom) \/ f' v = v).
intros.
destruct (Perm.support_axiom p v); auto.
rewrite H0 in H1.
apply cons_elem in H1.
destruct H1.
right.
apply atom_strong_eq in H1. subst v.
unfold f'. simpl.
destruct (string_dec (p c) (p c)).
destruct (string_dec c (p c)); auto.
elim n; auto.
auto.
right. unfold f'.
simpl. rewrite H1.
destruct (string_dec c v).
subst v; auto.
destruct (string_dec (p c) v).
rewrite <- H1 in e.
apply Perm.f_inj in e. auto.
auto.
set (p' := Perm.Perm tt tt f' g' l Hgf Hfg Hsupport : perm).
destruct (string_dec c (p c)).
apply Heq with p'.
hnf; simpl.
unfold f'; simpl; intros.
destruct (string_dec c (p x)).
rewrite <- e. rewrite <- e0. auto.
destruct (string_dec (p c) (p x)).
rewrite <- e0; auto. auto.
apply H.
hnf; simpl.
rewrite H0. simpl. auto.
apply Heq with (c ⇋ (p c) ∘ p').
hnf; simpl.
intro x. unfold f'. simpl.
destruct (string_dec c (p x)).
destruct (string_dec c (p c)).
rewrite <- e. rewrite <- e0. auto.
rewrite <- e.
destruct (string_dec (p c) (p c)); auto.
elim n1; auto.
destruct (string_dec (p c) (p x)); auto.
destruct (string_dec c c); auto.
elim n1. auto.
destruct (string_dec (p c) (p x)); auto.
contradiction.
destruct (string_dec c (p x)).
contradiction.
auto.
apply Hind.
simpl. unfold f'.
simpl.
destruct (string_dec c (p c)); auto.
destruct (string_dec (p c) (p c)); auto.
elim n1; auto.
auto.
apply H.
hnf; simpl.
rewrite H0. simpl. auto.
Qed.
Lemma swap_induction' (P:perm -> Prop) :
(forall p p', p ≈ p' -> P p -> P p') ->
P id ->
(forall u v (p:perm), p u = u -> u <> v -> P p -> P (p ∘ u ⇋ v)) ->
forall p, P p.
Proof.
intros.
apply swap_induction; auto.
intros.
apply H with
(p0 ∘ (Perm.g p0 u) ⇋ (Perm.g p0 v)).
2: apply H1; auto.
symmetry; apply swap_commute'.
rewrite Perm.fg.
rewrite <- H2 at 2.
rewrite Perm.gf.
auto.
intro. apply Perm.g_inj in H5. elim H3; auto.
Qed.