-
Notifications
You must be signed in to change notification settings - Fork 4
/
eq_rel.v
623 lines (514 loc) · 16.3 KB
/
eq_rel.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
Require Import universe.
Inductive void : [univ] := .
(* ========= Here is a constuctive iff ======== *)
(*Definition t_iff A B := (A -> B) # (B -> A).*)
Inductive t_iff (A B : [univ]) : [univ] :=
| t_iff_cons : (A -> B) -> (B -> A) -> t_iff A B.
Notation "x <=> y" := (iff x y) (at level 95, no associativity).
Lemma tiff_trans :
forall a b c, (a <=> b) -> (b <=> c) -> (a <=> c).
Proof.
intros a b c i1 i2.
destruct i1 as [i11 i12].
destruct i2 as [i21 i22].
split; intro k.
apply i11 in k; apply i21 in k; auto.
apply i22 in k; apply i12 in k; auto.
Qed.
Ltac dtiff :=
match goal with
[ H : ?x <=> ?y |- _ ] => destruct H
end.
Ltac dtiffs := repeat dtiff.
Ltac dprod :=
match goal with
[ H : ?x # ?y |- _ ] => destruct H
end.
Ltac dprods := repeat dprod.
Ltac dsum :=
match goal with
[ H : ?x [+] ?y |- _ ] => destruct H
end.
Ltac dsums := repeat dsums.
Ltac dall :=
repeat match goal with
| [ H : ?x <=> ?y |- _ ] => destruct H
| [ H : ?x # ?y |- _ ] => destruct H
| [ H : ?x [+] ?y |- _ ] => destruct H
end.
Definition tiff_fst :=
fun {A B : [univ]} (p : A <=> B) => let (x, _) := p in x.
Definition tiff_snd :=
fun {A B : [univ]} (p : A <=> B) => let (_, y) := p in y.
Lemma tiff_is_prod_implies1 :
forall A B,
(A <=> B) <=> ((A -> B) # (B -> A)).
Proof.
intros; split; intros k; split; intros; destruct k; auto.
Qed.
Lemma tiff_is_prod_implies2 :
forall A B : [univ],
((A -> B) # (B -> A)) <=> (A <=> B).
Proof.
intros; split; intros k; split; intros; destruct k; auto.
Qed.
Lemma combine_t_iff_proofs_imp :
forall {A B A' B' : [univ]},
(A <=> A') -> (B <=> B') -> ((A -> B) <=> (A' -> B')).
Proof.
intros; dall; constructor; auto.
Qed.
Lemma combine_t_iff_proofs_t_iff :
forall {A B A' B'},
(A <=> A')
-> (B <=> B')
-> ((A <=> B) <=> (A' <=> B')).
Proof.
intros.
dall; constructor; intro; dall; constructor; auto.
Qed.
Lemma combine_t_iff_proofs_prod :
forall {A B A' B'},
(A <=> A')
-> (B <=> B')
-> ((A # B) <=> (A' # B')).
Proof.
intros.
dall; constructor; intro; dall; auto.
Qed.
Lemma combine_t_iff_proofs_sum :
forall {A B A' B'},
(A <=> A')
-> (B <=> B')
-> ((A [+] B) <=> (A' [+] B')).
Proof.
intros.
dall; constructor; intro; dall; auto.
Qed.
Lemma combine_t_iff_proofs_not :
forall {A A'},
(A <=> A')
-> (!A <=> !A').
Proof.
intros.
dall; constructor; repeat intro; auto.
Qed.
Lemma t_iff_refl :
forall A, A <=> A.
Proof.
intros; constructor; auto.
Qed.
Lemma t_iff_sym :
forall {A B}, (A <=> B) -> (B <=> A).
Proof.
intros.
dall; constructor; auto.
Qed.
(* this build a proof object of T <=> T[b/a] where p is the proof of a<=>b.
*)
Ltac build_tiff_term T a b p :=
match T with
| a => p
| ?x -> ?y =>
let l := build_tiff_term x a b p in
let r := build_tiff_term y a b p in
constr:(combine_t_iff_proofs_imp l r)
| ?x <=> ?y =>
let l := build_tiff_term x a b p in
let r := build_tiff_term y a b p in
constr:(combine_t_iff_proofs_t_iff l r)
| ?x # ?y =>
let l := build_tiff_term x a b p in
let r := build_tiff_term y a b p in
constr:(combine_t_iff_proofs_prod l r)
| ?x [+] ?y =>
let l := build_tiff_term x a b p in
let r := build_tiff_term y a b p in
constr:(combine_t_iff_proofs_sum l r)
| !?x =>
let l := build_tiff_term x a b p in
constr:(combine_t_iff_proofs_not l)
| _ => constr:(t_iff_refl T)
end.
Tactic Notation "thin_last" :=
match goal with H: ?T |- _ => clear H end.
Ltac apply' H1 H2 :=
let H3 := fresh in
(pose proof (H1 H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ _ _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ _ _ _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ _ _ _ _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3) ||
(pose proof (H1 _ _ _ _ _ _ _ _ _ _ H2) as H3; clear H2; pose proof H3 as H2; clear H3).
Ltac build_and_rewrite H :=
let T := type of H in
match goal with
| [ |- ?C ] =>
match T with
| ?A <=> ?B =>
let d := build_tiff_term C A B H in
let name := fresh H in
remember d as name;
thin_last;
apply name;
clear name
end
end.
Ltac build_and_rewrite_hyp H H2 :=
let T := type of H in
let C := type of H2 in
match T with
| ?A <=> ?B =>
let d := build_tiff_term C A B H in
let name := fresh H in
remember d as name;
thin_last;
apply' (tiff_fst name) H2;
clear name
end.
Ltac build_and_rewrite_rev H :=
let T := type of H in
match goal with
| [ |- ?C ] =>
match T with
| ?A <=> ?B =>
let d := build_tiff_term C B A (t_iff_sym H) in
let name := fresh H in
remember d as name;
thin_last;
apply name;
clear name
end
end.
Ltac build_and_rewrite_hyp_rev H H2 :=
let T := type of H in
let C := type of H2 in
match T with
| ?A <=> ?B =>
let d := build_tiff_term C B A (t_iff_sym H) in
let name := fresh H in
remember d as name;
thin_last;
apply' (tiff_fst name) H2;
clear name
end.
Tactic Notation "trewrite" ident(H) :=
build_and_rewrite H.
Tactic Notation "trewrite" ident(H) "in" ident (H') :=
build_and_rewrite_hyp H H'.
Tactic Notation "trewrite" "<-" ident(H) :=
build_and_rewrite_rev H.
Tactic Notation "trewrite" "<-" ident(H) "in" ident(H') :=
build_and_rewrite_hyp_rev H H'.
Ltac get_instance_of T H Hn :=
match H with
| _ =>
let name:= fresh "Htemp" in
(* idtac "trying gio" H;*)
progress (
(pose proof (fun h:H => (tiff_fst T) h) as name)||
(pose proof (fun h:H => (tiff_fst (T _)) h) as name) ||
(pose proof (fun h:H => (tiff_fst (T _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_fst (T _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_fst (T _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_fst (T _ _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_fst (T _ _ _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_fst (T _ _ _ _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_fst (T _ _ _ _ _ _ _ _)) h) as name)
); (* idtac "succeded" H; *)
let H2 := type of name in
match H2 with
| ?H -> ?H => fail 1 (* both sides are the same, we won't rewrite anything *)
| ?H -> ?H2 =>
clear name;
assert (H <=> H2) as Hn by (apply T)
(*; idtac "really succeded" H *)
end
| ?Hl <=> ?Hr => get_instance_of T Hl Hn || get_instance_of T Hr Hn
| ?Hl # ?Hr => get_instance_of T Hl Hn || get_instance_of T Hr Hn
| ?Hl [+] ?Hr => get_instance_of T Hl Hn || get_instance_of T Hr Hn
| !?Hl => get_instance_of T Hl Hn
| ?Hl -> ?Hr => get_instance_of T Hl Hn || get_instance_of T Hr Hn
end.
Ltac get_instance_of_rev T H Hn :=
match H with
| _ =>
let name:= fresh "Htemp" in
(* idtac "trying gio" H;*)
progress (
(pose proof (fun h:H => (tiff_snd T) h) as name)||
(pose proof (fun h:H => (tiff_snd (T _)) h) as name) ||
(pose proof (fun h:H => (tiff_snd (T _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_snd (T _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_snd (T _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_snd (T _ _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_snd (T _ _ _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_snd (T _ _ _ _ _ _ _)) h) as name) ||
(pose proof (fun h:H => (tiff_snd (T _ _ _ _ _ _ _ _)) h) as name)
); (* idtac "succeded" H;*)
let H2 := type of name in
match H2 with
| ?H -> ?H => fail 1 (* both sides are the same, we won't rewrite anything *)
| ?H -> ?H2 =>
clear name;
assert (H2 <=> H) as Hn by (apply T)
(** H2 in LHS; apply does not automatically take care
of symmetry *)
(*; idtac "really succeded" H*)
end
| ?Hl <=> ?Hr => get_instance_of_rev T Hl Hn || get_instance_of_rev T Hr Hn
| ?Hl # ?Hr => get_instance_of_rev T Hl Hn || get_instance_of_rev T Hr Hn
| ?Hl [+] ?Hr => get_instance_of_rev T Hl Hn || get_instance_of_rev T Hr Hn
| !?Hl => get_instance_of_rev T Hl Hn
| ?Hl -> ?Hr => get_instance_of_rev T Hl Hn || get_instance_of_rev T Hr Hn
end.
(** rewrite using a universally quantified t_iff relation T in the conclusion *)
Ltac trw T :=
match goal with
[ |- ?C ] =>
let name:= fresh "Hget_instance_of_in_concl" in
get_instance_of T C name;
build_and_rewrite name; clear name
end.
Ltac trw_rev T :=
match goal with
[ |- ?C ] =>
let name:= fresh "Hget_instance_of_in_concl" in
get_instance_of_rev T C name;
build_and_rewrite_rev name ; clear name
end.
Ltac trw_h T h :=
let C := type of h in
let name:= fresh "Hget_instance_of_in_concl" in
get_instance_of T C name;
build_and_rewrite_hyp name h; clear name.
Ltac trw_rev_h T h :=
let C := type of h in
let name:= fresh "Hget_instance_of_in_concl" in
get_instance_of_rev T C name;
build_and_rewrite_hyp_rev name h; clear name.
(* Some general notation to use the above tactics *)
Tactic Notation "rw" constr(T) :=
trw T || rewrite T.
Tactic Notation "rw" "<-" constr(T) :=
trw_rev T || rewrite <- T.
Tactic Notation "rw" constr(T) "in" ident(H) :=
trw_h T H || rewrite T in H.
Tactic Notation "rw" "<-" constr(T) "in" ident(H) :=
trw_rev_h T H || rewrite <- T in H.
Tactic Notation "onerw" :=
match goal with
| [ H : _ |- _ ] => progress (trw H || rewrite H)
end.
Tactic Notation "allrw" := repeat onerw.
Tactic Notation "allrw" "<-" :=
repeat match goal with
| [ H : _ |- _ ] => progress (trw_rev H || rewrite <- H)
end.
(*
Tactic Notation "onerw" constr(T) :=
match goal with
| [ H : _ |- _ ] =>
progress (trw_h T H || trw T || rewrite T in H || rewrite T)
end.
Tactic Notation "allrw" constr(T) := repeat (onerw T).
*)
Tactic Notation "onerw" constr(T) :=
let t := type of T in
match goal with
| [ H : _, H' : t |- _ ] =>
progress (trw_h T H || trw T || rewrite T in H || rewrite T)
| [ H : t |- _ ] => fail 1
| [ H : _ |- _ ] =>
progress (trw_h T H || trw T || rewrite T in H || rewrite T)
end.
Tactic Notation "allrw" constr(T) := repeat (onerw T).
Tactic Notation "rww" ident(T) :=
let t := type of T in
repeat match goal with
| [ H1 : t, H : _ |- _ ] =>
progress (trw_h T H || trw T || rewrite T in H || rewrite T)
end.
Tactic Notation "allrw" "<-" constr(T) :=
repeat match goal with
| [ H : _ |- _ ] =>
progress (trw_rev_h T H || trw_rev T || rewrite <- T in H || rewrite <- T)
end.
Tactic Notation "rww" "<-" ident(T) :=
let t := type of T in
repeat match goal with
| [ H1 : t, H : _ |- _ ] =>
progress (trw_rev_h T H || trw_rev T || rewrite <- T in H || rewrite <- T)
end.
(* ------------------------ *)
(*
Theorem trw_demo: forall (P Q: nat -> Type),
(forall n, P n <=> Q n )
-> ((P 1 * void -> void ) <=> (Q 1 * void -> void )).
Proof.
intros ? ? Hiff.
assert (P 1) by aXdmit.
trw_h Hiff X.
trw Hiff . apply t_iff_refl.
Qed.
(*
Theorem trw_demo2:
forall (P Q: nat -> Type),
(forall n, P n <=> forall m, Q m )
-> ((P 1 * void -> void ) <=> ((forall m, Q m) * void -> void )).
Proof.
intros ? ? Hiff.
constructor; intros.
destruct X; auto.
destruct X; auto.
Qed.
*)
Theorem trw_demo2:
forall (P Q: nat -> Type),
(forall n, P n <=> forall m, Q m )
-> ((P 1 * void -> void ) <=> ((forall m, Q m) * void -> void )).
Proof.
intros ? ? Hiff.
trw Hiff.
apply t_iff_refl.
Qed.
*)
(** --- setoid stuff -- delete? *)
Require Import Coq.Setoids.Setoid.
Inductive Cast (t : [univ]) : Prop :=
| cast : t -> Cast t.
Hint Constructors Cast.
Inductive Cast2 (p : Prop) : [univ] :=
| cast2 : p -> Cast2 p.
Hint Constructors Cast2.
Definition c_t_iff : relation [univ] :=
fun A B : [univ] => Cast (A <=> B).
Notation "x <==> y" := (c_t_iff x y) (at level 95, no associativity).
Definition t_c_iff : relation [univ] :=
fun A B : [univ] => Cast (A -> B) /\ Cast (B -> A).
Notation "x <~> y" := (t_c_iff x y) (at level 95, no associativity).
Lemma CType_S : Setoid_Theory [univ] c_t_iff.
Proof.
split.
repeat constructor; auto.
unfold Symmetric; intros.
inversion H; subst.
dall.
repeat constructor; auto.
unfold Transitive; intros.
inversion H; inversion H0; subst.
dall.
repeat constructor; auto.
Qed.
Lemma TypeC_S : Setoid_Theory [univ] t_c_iff.
Proof.
split.
repeat constructor; auto.
unfold Symmetric; intros.
destruct H.
inversion H; subst.
inversion H0; subst.
repeat constructor; auto.
unfold Transitive; intros.
destruct H; destruct H0.
inversion H; inversion H0; inversion H1; inversion H2; subst.
repeat constructor; auto.
Qed.
Add Setoid [univ] c_t_iff CType_S as Type_iff_reg.
Add Setoid [univ] t_c_iff TypeC_S as Type_iff_reg2.
Hint Resolve (Seq_refl [univ] c_t_iff CType_S): setoid.
Hint Resolve (Seq_sym [univ] c_t_iff CType_S): setoid.
Hint Resolve (Seq_trans [univ] c_t_iff CType_S): setoid.
Hint Resolve (Seq_refl [univ] t_c_iff TypeC_S): setoid.
Hint Resolve (Seq_sym [univ] t_c_iff TypeC_S): setoid.
Hint Resolve (Seq_trans [univ] t_c_iff TypeC_S): setoid.
(* ============================================== *)
(*
Lemma test :
forall a b (*c f*), (a <=> b) -> Cast a. (*. # { x : c | f x }.*)
Proof.
intros.
rewrite X.
Qed.
*)
(** should not be required anymore? *)
Tactic Notation "trewrite" "<-" ident(H) ident(p1) :=
let name := fresh H in
generalize (H p1);
intro name;
build_and_rewrite_rev name;
clear name.
Tactic Notation "trewrite" "<-" ident(H) ident(p1) ident(p2) :=
let name := fresh H in
generalize (H p1 p2);
intro name;
build_and_rewrite_rev name;
clear name.
Tactic Notation "trewrite" ident(H) ident(p1) :=
let name := fresh H in
generalize (H p1);
intro name;
build_and_rewrite name;
clear name.
Tactic Notation "trewrite" ident(H) ident(p1) ident(p2) :=
let name := fresh H in
generalize (H p1 p2);
intro name;
build_and_rewrite name;
clear name.
Tactic Notation "trewrite" ident(H) ident(p1) "in" ident (H') :=
let name := fresh H in
generalize (H p1);
intro name;
build_and_rewrite_hyp name H';
clear name.
Tactic Notation "trewrite" ident(H) ident(p1) ident(p2) "in" ident (H') :=
let name := fresh H in
generalize (H p1 p2);
intro name;
build_and_rewrite_hyp name H';
clear name.
Tactic Notation "trewrite" "<-" ident(H) ident(p1) "in" ident(H') :=
let name := fresh H in
generalize (H p1);
intro name;
build_and_rewrite_hyp_rev name H';
clear name.
Tactic Notation "trewrite" "<-" ident(H) ident(p1) ident(p2) "in" ident(H') :=
let name := fresh H in
generalize (H p1 p2);
intro name;
build_and_rewrite_hyp_rev name H';
clear name.
Theorem dont_touch_forall :
forall (P Q : (nat : Set) -> [univ]),
(forall (n : (nat : Set)), P n <=> (forall (m : (nat : Set)), Q (m+n)))
-> P (1 : (nat : Set))
-> forall (m : (nat : Set)), Q (m+1).
Proof.
intros ? ? Hif Hp1.
rw Hif in Hp1; trivial.
Qed.
Theorem dont_touch_forall2 :
forall (P Q : nat -> [univ]),
(forall n, P n <=> (forall m, Q (m+n)))
-> P 1
-> forall m, Q (m+1).
Proof.
intros ? ? Hif Hp1.
trw_rev Hif. trivial.
Qed.
Theorem dont_touch_forall3 :
forall (P Q: nat -> [univ]),
(forall n, P n <=> (forall m, Q (m+n)))
-> (forall m, Q (m+1))
-> P 1.
Proof.
intros ? ? Hif Hq .
trw_rev_h Hif Hq. trivial.
Qed.