-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVarForm-Coq.V8.v
2641 lines (2134 loc) · 93.8 KB
/
VarForm-Coq.V8.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Require Import Coq.Arith.Peano_dec
Coq.Arith.EqNat
Coq.Lists.List.
Import ListNotations.
Notation "a && b" := (andb a b).
Notation "a || b" := (orb a b).
Notation "! a" := (negb a) (at level 5).
Definition Object := nat.
Definition ObjectSet := list Object.
Inductive Link := link : nat -> Object -> Object -> Link.
Definition LinkSet := list Link.
Notation "id ¤ src --> tgt" := (link id src tgt) (at level 66).
Inductive Graph := graph : ObjectSet -> LinkSet -> Graph.
Notation "gObj ** gLnk" := (graph gObj gLnk) (at level 64).
Definition Model := Graph.
Definition Fragment := Graph.
Inductive FragSubst :=
fragsubst : Fragment -> Fragment -> LinkSet -> FragSubst.
Notation "p ,. r ., bdg" := (fragsubst p r bdg) (at level 65).
Definition FragSubstSet := list FragSubst.
Module OBJECT.
Module SET.
(* Contains *)
Inductive Contains : ObjectSet -> Object -> Prop :=
| Contains_h : forall o t, Contains (o::t) o
| Contains_t : forall o h t, Contains t o -> Contains (h::t) o.
Definition Contains_dec (s : ObjectSet) (o : Object) :=
{ Contains s o } + { ~Contains s o }.
Definition contains: forall s o, Contains_dec s o.
refine (fix contains s o: Contains_dec s o :=
match s with
| [] => right _
| h::t => if eq_nat_dec h o
then left _
else if contains t o
then left _
else right _
end).
Proof.
unfold not. intro. inversion H.
rewrite _H. apply Contains_h.
apply Contains_t. apply _H0.
unfold not. intro. inversion H. apply _H. apply H0.
unfold not in _H0. apply _H0. apply H3.
Defined.
Definition bContains (s : ObjectSet) (o : Object) : bool :=
if contains s o then true else false.
Lemma Contains_bProp : forall s o,
(bContains s o) = true <-> Contains s o.
Proof.
intros. split.
unfold bContains. destruct (contains s o) as [T | F].
intro. apply T.
intro. inversion H.
intro. unfold bContains. destruct (contains s o) as [T | F].
reflexivity.
exfalso. unfold not in F. apply F. apply H.
Qed.
Lemma NotContains_bProp : forall s o,
(bContains s o) = false <-> ~Contains s o.
Proof.
intros. split.
unfold bContains. destruct (contains s o) as [T | F].
intro. inversion H.
intro. auto.
intro. unfold bContains. destruct (contains s o) as [T | F].
contradiction.
reflexivity.
Qed.
(* IsSet *)
Inductive IsSet : ObjectSet -> Prop :=
| IsSet_nil : IsSet []
| IsSet_cons : forall s o, ~Contains s o /\ IsSet s -> IsSet (o::s).
Definition IsSet_dec (s : ObjectSet) :=
{ IsSet s } + { ~IsSet s }.
Definition isSet: forall s, IsSet_dec s.
refine (fix isSet s : IsSet_dec s :=
match s with
| [] => left _
| h::t => if contains t h
then right _
else if isSet t
then left _
else right _
end).
Proof.
apply IsSet_nil.
unfold not. intro. inversion H. inversion H1. unfold not in H3. apply H3. apply _H.
apply IsSet_cons. split. apply _H. apply _H0.
unfold not. intro. inversion H. inversion H1. unfold not in _H0. apply _H0. apply H4.
Defined.
Definition bIsSet (s : ObjectSet) : bool :=
if isSet s then true else false.
Lemma IsSet_bProp : forall s,
(bIsSet s) = true <-> IsSet s.
Proof.
intros. split.
unfold bIsSet. destruct (isSet s) as [T | F].
intro. apply T.
intro. inversion H.
intro. unfold bIsSet. destruct (isSet s) as [T | F].
reflexivity.
exfalso. unfold not in F. apply F. apply H.
Qed.
(* Subset *)
Inductive Subset : ObjectSet -> ObjectSet -> Prop :=
| Subset_nil : forall s, Subset [] s
| Subset_cons : forall h t s, Subset t s /\ Contains s h -> Subset (h::t) s.
Definition Subset_dec (s1 s2 : ObjectSet) :=
{ Subset s1 s2 } + { ~Subset s1 s2 }.
Definition subset: forall s1 s2, Subset_dec s1 s2.
refine (fix subset s1 s2 : Subset_dec s1 s2 :=
match s1 with
| [] => left _
| h::t => if subset t s2
then (if contains s2 h
then left _
else right _)
else right _
end).
Proof.
apply Subset_nil.
apply Subset_cons. split. apply _H. apply _H0.
unfold not. intro. inversion H. inversion H3. unfold not in _H0. apply _H0. apply H5.
unfold not. intro. inversion H. inversion H3. unfold not in _H. apply _H. apply H4.
Defined.
Definition bSubset (s1 s2 : ObjectSet) : bool :=
if subset s1 s2 then true else false.
Lemma Subset_consR : forall s t h,
Subset s t -> Subset s (h::t).
Proof.
intros. induction s as [|h' s'].
constructor.
constructor. split. apply IHs'. inversion_clear H. inversion_clear H0. auto.
constructor. inversion_clear H. inversion_clear H0. auto.
Qed.
Lemma Subset_Contains_trans : forall h s1 s2,
Contains s1 h /\ Subset s1 s2 -> Contains s2 h.
Proof.
intros. induction s1 as [|h1' s1'].
inversion_clear H. inversion_clear H0.
inversion_clear H. inversion H0. inversion_clear H0. inversion_clear H1.
inversion_clear H. rewrite <- H4. auto.
apply IHs1'. split. auto. inversion_clear H1. inversion_clear H0. auto.
apply IHs1'. inversion_clear H1. inversion_clear H5. auto.
Qed.
Lemma Subset_refl : forall s, Subset s s.
Proof.
intro. induction s as [|h t].
constructor.
constructor. split. apply Subset_consR. auto. constructor.
Qed.
Lemma Subset_trans : forall s1 s2 s3,
Subset s1 s2 /\ Subset s2 s3 -> Subset s1 s3.
Proof.
intros. induction s1 as [|h1' s1'].
constructor.
inversion_clear H. inversion_clear H0. inversion_clear H. constructor. split.
apply IHs1'. auto.
apply Subset_Contains_trans with (s1 := s2). auto.
Qed.
Lemma SubsetRight : forall o s1 s2,
Subset s1 s2 -> Subset s1 (o :: s2).
Proof.
intros. induction s1 as [|h t]. constructor. constructor. split. apply IHt.
inversion_clear H. inversion_clear H0. auto. constructor. inversion_clear H.
inversion_clear H0. auto.
Qed.
Lemma SubsetH : forall o s1 s2,
Subset s1 s2 -> Subset (o::s1) (o::s2).
Proof.
intros. induction s1 as [|h t].
constructor. split. constructor. constructor.
constructor. split. apply SubsetRight. auto. constructor.
Qed.
(* Equal *)
Inductive Equal : ObjectSet -> ObjectSet -> Prop :=
| Equal_ : forall s1 s2, Subset s1 s2 /\ Subset s2 s1 -> Equal s1 s2.
Definition Equal_dec (s1 s2 : ObjectSet) :=
{ Equal s1 s2 } + { ~Equal s1 s2 }.
Definition equal: forall s1 s2, Equal_dec s1 s2.
refine (fix equal s1 s2 : Equal_dec s1 s2 :=
if subset s1 s2
then (if subset s2 s1
then left _
else right _)
else right _
).
Proof.
apply Equal_. split. apply _H. apply _H0.
unfold not. intro. inversion H. inversion H0. unfold not in _H0. apply _H0. apply H4.
unfold not. intro. inversion H. inversion H0. unfold not in _H. apply _H. apply H3.
Defined.
Definition bEqual (s1 s2 : ObjectSet) : bool :=
if equal s1 s2 then true else false.
Lemma Equal_refl: forall s,
Equal s s.
Proof.
intro. constructor. split. apply Subset_refl. apply Subset_refl.
Qed.
Lemma Equal_sym : forall s1 s2,
Equal s1 s2 -> Equal s2 s1.
Proof.
intros. inversion_clear H. inversion_clear H0. constructor. auto.
Qed.
Lemma Equal_trans : forall s1 s2 s3,
Equal s1 s2 /\ Equal s2 s3 -> Equal s1 s3.
Proof.
intros. inversion_clear H. inversion_clear H0. inversion_clear H1. inversion_clear H.
inversion_clear H0. constructor. split. apply Subset_trans with (s2 := s2). auto.
apply Subset_trans with (s2 := s2). auto.
Qed.
Lemma EqualNil : forall s,
Equal [] s -> s = [].
Proof.
intros. inversion H. inversion H0. inversion H4. reflexivity. inversion H5. inversion H9.
Qed.
Lemma SubsetContainsSameObjects : forall o s1 s2,
Subset s1 s2 /\ Contains s1 o -> Contains s2 o.
Proof.
intros.
induction s1 as [|h t]. inversion H. inversion H1.
inversion_clear H. inversion H1. rewrite H4 in H0. inversion_clear H0. inversion_clear H.
auto. apply IHt. split. inversion_clear H0. inversion_clear H5. auto. auto.
Qed.
Lemma EqualSetsContainSameObjects : forall o s1 s2,
Equal s1 s2 /\ Contains s1 o -> Contains s2 o.
Proof.
intros. inversion_clear H. inversion_clear H0. inversion_clear H.
apply SubsetContainsSameObjects with (s1 := s1). split. auto. auto.
Qed.
Fixpoint union (s1 s2 : ObjectSet) : ObjectSet :=
match s1 with
| [] => s2
| h::t => if contains s2 h
then union t s2
else h::(union t s2)
end.
Theorem unionContains : forall o s1 s2,
Contains s1 o \/ Contains s2 o -> Contains (union s1 s2) o.
Proof.
intros. induction s1 as [|h t].
(* s1 = [] *)
simpl. inversion H. inversion H0. apply H0.
(* s1 = h::t *)
inversion_clear H.
(* Contains (h :: t) o *)
inversion H0.
(* h = o *)
simpl.
destruct (contains s2 o).
(* Contains s2 o *)
apply IHt. right. auto.
(* ~Contains s2 o *)
apply Contains_h.
(* Contains t o *)
simpl.
destruct (contains s2 h).
(* Contains s2 h *)
apply IHt. left. apply H3.
(* ~ Contains s2 h *)
constructor. apply IHt. left. auto.
(* Contains s2 o *)
simpl.
destruct (contains s2 h).
(* Contains s2 h *)
apply IHt. right. auto.
(* ~Contains s2 h *)
constructor. apply IHt. right. auto.
Qed.
Theorem SubsetsUnionLeft : forall s1 s2 s3,
Subset s1 s2 -> Subset s1 (union s2 s3).
Proof.
intros. induction s1 as [|h t]. constructor. constructor. split. apply IHt.
inversion_clear H. inversion H0. apply H. apply unionContains. left. inversion_clear H.
inversion_clear H0. auto.
Qed.
Theorem SubsetsUnionRight : forall s1 s2 s3,
Subset s1 s2 -> Subset s1 (union s3 s2).
Proof.
intros. induction s1 as [|h t]. constructor. constructor. split. apply IHt.
inversion_clear H. inversion H0. apply H. apply unionContains. right. inversion_clear H.
inversion_clear H0. auto.
Qed.
Theorem UnionSubset : forall s1 s2 s,
Subset s1 s /\ Subset s2 s -> Subset (union s1 s2) s.
Proof.
intros. induction s1 as [|h1 t1]. simpl. inversion_clear H. auto.
unfold union. fold union. destruct (contains s2 h1) as [T|F].
apply IHt1. split. inversion_clear H. inversion_clear H0. inversion_clear H. auto.
inversion_clear H. auto.
constructor. split. apply IHt1. split. inversion_clear H. inversion_clear H0.
inversion_clear H. auto. inversion_clear H. auto. inversion_clear H. inversion_clear H0.
inversion_clear H. auto.
Qed.
Definition Consistent := Subset.
Lemma SubsetUnionOfObjectSubsets : forall o1 o1' o2 o2',
Subset o1 o1' /\ Subset o2 o2' -> Subset (union o1 o2) (union o1' o2').
Proof.
intros. inversion_clear H. induction o1 as [|h1 t1].
simpl. apply SubsetsUnionRight. auto.
inversion_clear H0. inversion_clear H. unfold union. fold union.
destruct (contains o2 h1) as [T|F].
apply IHt1. auto.
constructor. split. apply IHt1. auto. apply unionContains. auto.
Qed.
Lemma EqualUnionOfEqualObjectSets : forall o1 o1' o2 o2',
Equal o1 o1' /\ Equal o2 o2' -> Equal (union o1 o2) (union o1' o2').
Proof.
intros. inversion_clear H. inversion_clear H0. inversion_clear H1. inversion_clear H.
inversion_clear H0. constructor. split.
apply SubsetUnionOfObjectSubsets. auto.
apply SubsetUnionOfObjectSubsets. auto.
Qed.
End SET.
End OBJECT.
Module LINK.
(* Equal *)
Inductive Equal : Link -> Link -> Prop :=
| Equal_ : forall id1 src1 tgt1 id2 src2 tgt2,
id1 = id2 /\ src1 = src2 /\ tgt1 = tgt2
-> Equal (id1¤src1-->tgt1) (id2¤src2-->tgt2).
Definition Equal_dec (l1 l2 : Link) :=
{ Equal l1 l2 } + { ~Equal l1 l2 }.
Definition equal: forall l1 l2, Equal_dec l1 l2.
refine (fix equal l1 l2 : Equal_dec l1 l2 :=
match l1, l2 with
| (id1¤src1-->tgt1),
(id2¤src2-->tgt2) => if eq_nat_dec id1 id2
then (if eq_nat_dec src1 src2
then (if eq_nat_dec tgt1 tgt2
then left _
else right _)
else right _)
else right _
end).
Proof.
apply Equal_. split. apply _H. split. apply _H0. apply _H1.
unfold not. intro. inversion_clear H. inversion_clear H0. inversion_clear H1.
contradiction.
unfold not. intro. inversion_clear H. inversion_clear H0. inversion_clear H1.
contradiction.
unfold not. intro. inversion_clear H. inversion_clear H0. contradiction.
Defined.
Definition bEqual (l1 l2 : Link) : bool :=
if equal l1 l2 then true else false.
Lemma Equal_refl : forall l, Equal l l.
Proof.
intro. destruct l as [id src tgt]. apply Equal_. split. reflexivity. split. reflexivity.
reflexivity.
Qed.
Lemma Equal_sym : forall l1 l2,
Equal l1 l2 -> Equal l2 l1.
Proof.
intros. inversion_clear H. inversion_clear H0. inversion_clear H1.
apply Equal_. split. symmetry. apply H. split. symmetry. apply H0. symmetry. apply H2.
Qed.
Lemma Equal_trans : forall l1 l2 l3,
Equal l1 l2 /\ Equal l2 l3 -> Equal l1 l3.
Proof.
intros. inversion_clear H. destruct l1 as [id1 src1 tgt1]. destruct l2 as [id2 src2 tgt2].
destruct l3 as [id3 src3 tgt3]. inversion_clear H0. inversion_clear H1.
inversion_clear H. inversion_clear H2. inversion_clear H0. inversion_clear H4.
constructor. split. rewrite H1. rewrite H2. reflexivity. split. rewrite H. rewrite H0.
reflexivity. rewrite H3. rewrite H5. reflexivity.
Qed.
Lemma Equal_eq : forall l1 l2,
Equal l1 l2 <-> l1 = l2.
Proof.
split.
intro. inversion_clear H. inversion_clear H0. inversion_clear H1. rewrite H. rewrite H0.
rewrite H2. reflexivity.
intro. rewrite H. apply Equal_refl.
Qed.
(* EqualId *)
Inductive EqualId : Link -> Link -> Prop :=
| EqualId_ : forall id1 src1 tgt1 id2 src2 tgt2,
id1 = id2 -> EqualId (id1¤src1-->tgt1) (id2¤src2-->tgt2).
Definition EqualId_dec (l1 l2 : Link) :=
{ EqualId l1 l2 } + { ~EqualId l1 l2 }.
Definition equalId: forall l1 l2, EqualId_dec l1 l2.
refine (fix equalId l1 l2 : EqualId_dec l1 l2 :=
match l1, l2 with
| (id1¤src1-->tgt1), (id2¤src2-->tgt2) => if eq_nat_dec id1 id2
then left _
else right _
end).
Proof.
apply EqualId_. apply _H.
unfold not. intro. inversion_clear H. contradiction.
Defined.
Definition bEqualId (l1 l2 : Link) : bool :=
if equalId l1 l2 then true else false.
Lemma EqualId_refl : forall l,
EqualId l l.
Proof.
intro. destruct l. constructor. reflexivity.
Qed.
Lemma EqualId_sym : forall l1 l2,
EqualId l1 l2 -> EqualId l2 l1.
Proof.
intros. inversion_clear H.
apply EqualId_. symmetry. apply H0.
Qed.
Lemma EqualId_trans : forall l1 l2 l3,
EqualId l1 l2 /\ EqualId l2 l3 -> EqualId l1 l3.
Proof.
intros. inversion_clear H. destruct l1 as [id1 src1 tgt1].
destruct l2 as [id2 src2 tgt2]. destruct l3 as [id3 src3 tgt3].
inversion_clear H0. inversion_clear H1.
constructor. rewrite H. rewrite H0. reflexivity.
Qed.
Module SET.
(* Contains *)
Inductive Contains : LinkSet -> Link -> Prop :=
| Contains_h : forall l h t, LINK.Equal l h -> Contains (h::t) l
| Contains_t : forall l h t, Contains t l -> Contains (h::t) l.
Definition Contains_dec (s : LinkSet) (l : Link) :=
{ Contains s l } + { ~Contains s l }.
Definition contains: forall s l, Contains_dec s l.
refine (fix contains s l: Contains_dec s l :=
match s with
| [] => right _
| h::t => if LINK.equal h l
then left _
else if contains t l
then left _
else right _
end).
Proof.
unfold not. intro. inversion H.
apply Contains_h. apply LINK.Equal_sym. apply _H.
apply Contains_t. apply _H0.
unfold not. intro. inversion_clear H. apply LINK.Equal_sym in H0. contradiction.
contradiction.
Defined.
Definition bContains (s : LinkSet) (l : Link) : bool :=
if contains s l then true else false.
Lemma Contains_bProp : forall s l,
(bContains s l) = true <-> Contains s l.
Proof.
intros. split.
unfold bContains. destruct (contains s l) as [T | F].
intro. apply T.
intro. inversion H.
intro. unfold bContains. destruct (contains s l) as [T | F].
reflexivity.
exfalso. unfold not in F. apply F. apply H.
Qed.
Lemma NotContains_bProp : forall s l,
(bContains s l) = false <-> ~Contains s l.
Proof.
intros. split.
unfold bContains. destruct (contains s l) as [T | F].
intro. inversion H.
intro. auto.
intro. unfold bContains. destruct (contains s l) as [T | F].
contradiction.
reflexivity.
Qed.
(* ContainsId *)
Inductive ContainsId : LinkSet -> Link -> Prop :=
| ContainsId_h : forall l h t, LINK.EqualId l h -> ContainsId (h::t) l
| ContainsId_t : forall l h t, ContainsId t l -> ContainsId (h::t) l.
Definition ContainsId_dec (s : LinkSet) (l : Link) :=
{ ContainsId s l } + { ~ContainsId s l }.
Definition containsId: forall s l, ContainsId_dec s l.
refine (fix containsId s l: ContainsId_dec s l :=
match s with
| [] => right _
| h::t => if LINK.equalId h l
then left _
else if containsId t l
then left _
else right _
end).
Proof.
unfold not. intro. inversion H.
apply ContainsId_h. apply LINK.EqualId_sym. apply _H.
apply ContainsId_t. apply _H0.
unfold not. intro. inversion_clear H. apply LINK.EqualId_sym in H0. contradiction.
contradiction.
Defined.
Definition bContainsId (s : LinkSet) (l : Link) : bool :=
if containsId s l then true else false.
Lemma ContainsId_bProp : forall s l,
(bContainsId s l) = true <-> ContainsId s l.
Proof.
intros. split.
unfold bContainsId. destruct (containsId s l) as [T | F].
intro. apply T.
intro. inversion H.
intro. unfold bContainsId. destruct (containsId s l) as [T | F].
reflexivity.
exfalso. unfold not in F. apply F. apply H.
Qed.
Lemma NotContainsId_bProp : forall s l,
(bContainsId s l) = false <-> ~ContainsId s l.
Proof.
intros. split.
unfold bContainsId. destruct (containsId s l) as [T | F].
intro. inversion H.
intro. auto.
intro. unfold bContainsId. destruct (containsId s l) as [T | F].
contradiction.
reflexivity.
Qed.
Theorem ContainsImpliesContainsId : forall l s,
Contains s l -> ContainsId s l.
Proof.
intros. destruct l as [id src tgt]. induction s as [|h t].
inversion H.
inversion_clear H. inversion_clear H0. inversion_clear H. rewrite H0. apply ContainsId_h.
apply EqualId_. reflexivity.
apply ContainsId_t. apply IHt. apply H0.
Qed.
(* IsSet *)
Inductive IsSet : LinkSet -> Prop :=
| IsSet_nil : IsSet []
| IsSet_cons : forall s l, ~ContainsId s l /\ IsSet s -> IsSet (l::s).
Definition IsSet_dec (s : LinkSet) :=
{ IsSet s } + { ~IsSet s }.
Definition isSet: forall s, IsSet_dec s.
refine (fix isSet s : IsSet_dec s :=
match s with
| [] => left _
| h::t => if containsId t h
then right _
else if isSet t
then left _
else right _
end).
Proof.
apply IsSet_nil.
unfold not. intro. inversion H. inversion H1. unfold not in H3. apply H3. apply _H.
apply IsSet_cons. split. apply _H. apply _H0.
unfold not. intro. inversion H. inversion H1. unfold not in _H0. apply _H0. apply H4.
Defined.
Definition bIsSet (s : LinkSet) : bool :=
if isSet s then true else false.
(* Subset *)
Inductive Subset : LinkSet -> LinkSet -> Prop :=
| Subset_nil : forall s, Subset [] s
| Subset_cons : forall h t s, Subset t s /\ ContainsId s h -> Subset (h::t) s.
Definition Subset_dec (s1 s2 : LinkSet) :=
{ Subset s1 s2 } + { ~Subset s1 s2 }.
Definition subset: forall s1 s2, Subset_dec s1 s2.
refine (fix subset s1 s2 : Subset_dec s1 s2 :=
match s1 with
| [] => left _
| h::t => if subset t s2
then (if containsId s2 h
then left _
else right _)
else right _
end).
Proof.
apply Subset_nil.
apply Subset_cons. split. apply _H. apply _H0.
unfold not. intro. inversion H. inversion H3. unfold not in _H0. apply _H0. apply H5.
unfold not. intro. inversion H. inversion H3. unfold not in _H. apply _H. apply H4.
Defined.
Definition bSubset (s1 s2 : LinkSet) : bool :=
if subset s1 s2 then true else false.
Eval compute in bSubset [] [].
Eval compute in bSubset [1¤1-->2] [].
Eval compute in bSubset [] [1¤1-->2].
Eval compute in bSubset [1¤1-->2] [1¤1-->2].
Eval compute in bSubset [1¤1-->2] [1¤3-->2].
Eval compute in bSubset [1¤1-->2] [1¤1-->3].
Eval compute in bSubset [1¤1-->2] [3¤3-->2; 1¤1-->2].
Eval compute in bSubset [1¤1-->2] [1¤1-->2; 4¤1-->3].
Lemma Subset_consR : forall s t h,
Subset s t -> Subset s (h::t).
Proof.
intros. induction s as [|h' s'].
constructor.
constructor. split. apply IHs'. inversion_clear H. inversion_clear H0. auto.
apply ContainsId_t. inversion_clear H. inversion_clear H0. auto.
Qed.
Lemma ContainsEqualIds : forall l1 l2 s,
EqualId l1 l2 /\ ContainsId s l1 -> ContainsId s l2.
Proof.
intros. induction s as [|h t].
inversion_clear H. inversion_clear H1.
inversion_clear H. destruct l1 as [id1 src1 tgt1]. destruct l2 as [id2 src2 tgt2].
destruct h as [idh srch tgth]. inversion_clear H0. inversion_clear H1.
inversion_clear H0. constructor. rewrite <- H. rewrite <- H1. constructor. reflexivity.
apply ContainsId_t. apply IHt. split. rewrite H. constructor. reflexivity. auto.
Qed.
Lemma Subset_Contains_trans : forall h s1 s2,
ContainsId s1 h /\ Subset s1 s2 -> ContainsId s2 h.
Proof.
intros. induction s1 as [|h1' s1'].
inversion_clear H. inversion_clear H0.
inversion_clear H. inversion_clear H0. inversion_clear H1. inversion_clear H0.
apply ContainsEqualIds with (l1 := h1'). apply EqualId_sym in H. auto.
apply IHs1'. inversion_clear H1. inversion_clear H0. auto.
Qed.
Lemma Subset_refl : forall s, Subset s s.
Proof.
intro. induction s as [|h t].
constructor.
constructor. split. apply Subset_consR. auto. constructor. apply EqualId_refl.
Qed.
Lemma Subset_trans : forall s1 s2 s3,
Subset s1 s2 /\ Subset s2 s3 -> Subset s1 s3.
Proof.
intros. induction s1 as [|h1' s1'].
constructor.
inversion_clear H. inversion_clear H0. inversion_clear H. constructor. split.
apply IHs1'. auto.
apply Subset_Contains_trans with (s1 := s2). auto.
Qed.
(* Equal *)
Inductive Equal : LinkSet -> LinkSet -> Prop :=
| Equal_ : forall s1 s2, Subset s1 s2 /\ Subset s2 s1 -> Equal s1 s2.
Definition Equal_dec (s1 s2 : LinkSet) :=
{ Equal s1 s2 } + { ~Equal s1 s2 }.
Definition equal: forall s1 s2, Equal_dec s1 s2.
refine (fix equal s1 s2 : Equal_dec s1 s2 :=
if subset s1 s2
then (if subset s2 s1
then left _
else right _)
else right _
).
Proof.
apply Equal_. split. apply _H. apply _H0.
unfold not. intro. inversion H. inversion H0. unfold not in _H0. apply _H0. apply H4.
unfold not. intro. inversion H. inversion H0. unfold not in _H. apply _H. apply H3.
Defined.
Definition bEqual (s1 s2 : LinkSet) : bool :=
if equal s1 s2 then true else false.
Lemma Equal_refl: forall s,
Equal s s.
Proof.
intro. constructor. split. apply Subset_refl. apply Subset_refl.
Qed.
Lemma Equal_sym : forall s1 s2,
Equal s1 s2 -> Equal s2 s1.
Proof.
intros. inversion_clear H. inversion_clear H0. constructor. auto.
Qed.
Lemma Equal_trans : forall s1 s2 s3,
Equal s1 s2 /\ Equal s2 s3 -> Equal s1 s3.
Proof.
intros. inversion_clear H. inversion_clear H0. inversion_clear H1. inversion_clear H.
inversion_clear H0. constructor. split. apply Subset_trans with (s2 := s2). auto.
apply Subset_trans with (s2 := s2). auto.
Qed.
Fixpoint union (s1 s2 : LinkSet) : LinkSet :=
match s1 with
| [] => s2
| h::t => if containsId s2 h
then union t s2
else h::(union t s2)
end.
Theorem setContainsLinksWithSameId : forall l1 l2 s,
EqualId l1 l2 /\ ContainsId s l1 -> ContainsId s l2.
Proof.
intros. induction s as [|h t].
(* s = [] *)
inversion H. inversion H1.
(* s = h::t *)
inversion_clear H. inversion_clear H1.
(* EqualId l1 h *)
apply ContainsId_h. destruct l1 as [id1 src1 tgt1].
destruct l2 as [id2 src2 tgt2]. destruct h as [idh srch tgth]. inversion_clear H0.
inversion_clear H. apply EqualId_. rewrite <- H0. rewrite H1. reflexivity.
(* ContainsId t l1 *)
apply ContainsId_t. apply IHt. split. apply H0. apply H.
Qed.
Theorem unionContains : forall l s1 s2,
ContainsId s1 l \/ ContainsId s2 l <-> ContainsId (union s1 s2) l.
Proof.
intros. split.
(* left *)
intros. induction s1 as [|h t].
(* s1 = [] *)
simpl. inversion H. inversion H0. apply H0.
(* s1 = h::t *)
inversion_clear H.
(* Contains (h :: t) l *)
inversion_clear H0.
(* EqualId l h *)
simpl.
destruct (containsId s2 h).
(* ContainsId s2 h *)
apply IHt. right.
apply setContainsLinksWithSameId with (l1 := h) (l2 := l) (s := s2).
split. apply EqualId_sym. apply H. auto.
(* ~ContainsId s2 h *)
apply ContainsId_h. apply H.
(* ContainsId t l *)
simpl.
destruct (containsId s2 h).
(* ContainsId s2 h *)
apply IHt. left. apply H.
(* ~ContainsId s2 h *)
apply ContainsId_t. apply IHt. left. apply H.
(* ContainsId s2 l *)
simpl.
destruct (containsId s2 h).
(* ContainsId s2 h *)
apply IHt. right. auto.
(* ~ContainsId s2 h *)
apply ContainsId_t. apply IHt. right. auto.
(* right *)
intro.
induction s1 as [|h t].
simpl in H. right. auto.
simpl in H. destruct (containsId s2 h).
apply IHt in H. inversion_clear H. left. constructor 2. auto. right. auto.
inversion_clear H. left. constructor. auto. apply IHt in H0. inversion_clear H0.
left. constructor 2. auto. right. auto.
Qed.
Theorem unionDoesNotContain : forall l s1 s2,
~ContainsId s1 l /\ ~ContainsId s2 l -> ~ContainsId (union s1 s2) l.
Proof.
intros. induction s1 as [|h t].
- simpl. inversion H. apply H1.
- inversion_clear H. simpl. destruct (containsId s2 h).
+ apply IHt. split.
* unfold not in H0. unfold not. intro. apply H0. apply ContainsId_t. apply H.
* apply H1.
+ unfold not. intro. inversion H.
* unfold not in H0. apply H0. apply ContainsId_h. auto.
* generalize dependent H5. apply IHt. { split.
- unfold not. intro. unfold not in H0. apply H0. apply ContainsId_t. auto.
- auto. }
Qed.
Theorem unionProducesSet : forall s1 s2,
IsSet s1 /\ IsSet s2 -> IsSet (union s1 s2).
Proof.
intros. induction s1 as [|h t].
(* s1 = [] *)
simpl. inversion H. apply H1.
(* s1 = h::t *)
simpl. destruct (containsId s2 h).
(* ContainsId s2 h *)
apply IHt. inversion_clear H. inversion_clear H0. inversion_clear H. split. auto. auto.
(* ~ContainsId s2 h *)
apply IsSet_cons. split.
(* ~ ContainsId (union t s2) h *)
apply unionDoesNotContain. split. inversion_clear H. inversion_clear H0.
inversion_clear H. apply H0. apply n.
(* IsSet (union t s2) *)
apply IHt. inversion_clear H. inversion_clear H0. inversion_clear H. split. auto.
auto.
Qed.
Theorem SubsetsUnionLeft : forall s1 s2 s3,
Subset s1 s2 -> Subset s1 (union s2 s3).
Proof.
intros. induction s1 as [|h t]. constructor. constructor. split. apply IHt.
inversion_clear H. inversion H0. apply H. apply unionContains. left. inversion_clear H.
inversion_clear H0. auto.
Qed.
Theorem SubsetsUnionRight : forall s1 s2 s3,
Subset s1 s2 -> Subset s1 (union s3 s2).
Proof.
intros. induction s1 as [|h t]. constructor. constructor. split. apply IHt.
inversion_clear H. inversion H0. apply H. apply unionContains. right. inversion_clear H.
inversion_clear H0. auto.
Qed.
Theorem UnionSubset : forall s1 s2 s,
Subset s1 s /\ Subset s2 s -> Subset (union s1 s2) s.
Proof.
intros. induction s1 as [|h1 t1]. simpl. inversion_clear H. auto.
unfold union. fold union. destruct (containsId s2 h1) as [T|F].
apply IHt1. split. inversion_clear H. inversion_clear H0. inversion_clear H. auto.
inversion_clear H. auto.
constructor. split. apply IHt1. split. inversion_clear H. inversion_clear H0.
inversion_clear H. auto. inversion_clear H. auto. inversion_clear H. inversion_clear H0.
inversion_clear H. auto.
Qed.
Lemma ContainsIdForInconsistentLinks : forall s id src1 tgt1 src2 tgt2,
ContainsId s (id¤src1-->tgt1) -> ContainsId s (id¤src2-->tgt2).
Proof.
intros. induction s as [|h t]. inversion H.
destruct h as [idh srch tgth]. inversion_clear H. inversion_clear H0.
constructor. constructor. auto. apply ContainsId_t. apply IHt. auto.
Qed.