-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLMNtalSyntax.v
2267 lines (2126 loc) · 68.1 KB
/
LMNtalSyntax.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 String.
Open Scope string_scope.
Require Import List.
Import ListNotations.
Open Scope list_scope.
Require Import Multiset.
Definition Link := string.
Inductive Atom : Type :=
| AAtom (name:string) (links:list Link)
| AConn (X: Link) (Y: Link).
Inductive Functor : Type :=
| FFunctor (name:string) (arity:nat).
Notation "p / n" := (FFunctor p n).
Definition Feq_dec: forall x y : Functor, {x=y} +{x<>y}.
Proof. repeat decide equality. Defined.
Definition get_functor (a:Atom) : Functor :=
match a with
| AAtom p ls => p / length ls
| AConn x y => "=" / 2
end.
Inductive Graph : Type :=
| GZero
| GAtom (atom:Atom)
| GMol (g1 g2:Graph).
Inductive Rule : Type :=
| React (lhs rhs:Graph).
Inductive RuleSet : Type :=
| RZero
| RRule (rule:Rule)
| RMol (r1 r2:RuleSet).
Coercion RRule : Rule >-> RuleSet.
Coercion GAtom : Atom >-> Graph.
Declare Custom Entry lmntal.
Declare Scope lmntal_scope.
Notation "{{ e }}" := e (at level 0, e custom lmntal at level 99) : lmntal_scope.
Notation "( x )" := x (in custom lmntal, x at level 2) : lmntal_scope.
Notation "x" := x (in custom lmntal at level 0, x constr at level 0) : lmntal_scope.
Notation "p ( x , .. , y )" := (AAtom p (cons x .. (cons y nil) .. ))
(in custom lmntal at level 0,
p constr at level 0, x constr at level 9,
y constr at level 9) : lmntal_scope.
Notation "p ()" := (AAtom p nil) (in custom lmntal at level 0,
p constr at level 0) : lmntal_scope.
Notation "x = y" := (AConn x y) (in custom lmntal at level 40, left associativity) : lmntal_scope.
Notation "x , y" := (GMol x y) (in custom lmntal at level 90, left associativity) : lmntal_scope.
Notation "x ':-' y" := (React x y) (in custom lmntal at level 91, no associativity) : lmntal_scope.
Notation "x ';' y" := (RMol x y) (in custom lmntal at level 92, left associativity) : lmntal_scope.
Open Scope lmntal_scope.
Check {{ "Y" }} : Link.
Check {{ "p"("X","Y"),"q"("Y","X") }} : Graph.
Check {{ "p"("X","Y"),"q"("Y","X") :- GZero }} : Rule.
Check {{ "p"("X","Y"),"q"("Y","X") :- GZero; RZero }} : RuleSet.
Example get_functor_example: get_functor (AAtom "p" ["L";"L";"M";"M"]) = "p"/4.
Proof. reflexivity. Qed.
Fixpoint remove_one (x: Link) (l: list Link) : bool * list Link :=
match l with
| [] => (false, [])
| h::t => if h =? x then (true, t)
else match (remove_one x t) with
| (b, ls) => (b, h::ls)
end
end.
Fixpoint links (g: Graph) : list Link :=
match g with
| GZero => []
| GAtom a => match a with
| AAtom p args => args
| AConn x y => [x;y]
end
| {{P,Q}} => links P ++ links Q
end.
Definition Leq_dec : forall x y : Link, {x=y} + {x<>y} := string_dec.
Definition unique_links (g: Graph): list Link := nodup Leq_dec (links g).
Definition list_to_multiset (l:list Link) : multiset Link :=
fold_right (fun x a => munion (SingletonBag eq Leq_dec x) a) (EmptyBag Link) l.
Definition link_multiset (g: Graph) : multiset Link :=
list_to_multiset (links g).
Definition freelinks (g: Graph) : list Link := filter
(fun x => Nat.eqb (multiplicity (link_multiset g) x) 1) (unique_links g).
Definition locallinks (g: Graph) : list Link := filter
(fun x => Nat.eqb (multiplicity (link_multiset g) x) 2) (unique_links g).
Compute freelinks {{ "p"("X","Y"),"q"("Y","X","F") }}.
Compute locallinks {{ "p"("X","Y"),"q"("Y","X","F") }}.
Lemma in_unique_links:
forall g x, In x (unique_links g) <-> In x (links g).
Proof.
intros g x.
unfold unique_links.
apply nodup_In.
Qed.
Require Import Coq.Logic.Eqdep_dec.
Lemma Leq_dec_refl:
forall X, Leq_dec X X = left eq_refl.
Proof.
intros X. destruct (Leq_dec X X).
- apply f_equal, UIP_dec, Leq_dec.
- congruence.
Qed.
Lemma Leq_dec_eq:
forall X Y, X = Y <-> exists p, Leq_dec X Y = left p.
Proof.
intros. destruct (Leq_dec X Y); split; intro H.
- exists e. auto.
- auto.
- congruence.
- destruct H. congruence.
Qed.
Lemma Leq_dec_neq:
forall X Y, X <> Y <-> exists p, Leq_dec X Y = right p.
Proof.
intros. destruct (Leq_dec X Y); split; intro H.
- congruence.
- destruct H. congruence.
- exists n. auto.
- auto.
Qed.
(* A graph is well-formed if each link name occurs at most twice in it *)
Definition wellformed_g (g:Graph) : Prop :=
forallb (fun x => Nat.leb (multiplicity (link_multiset g) x) 2) (unique_links g) = true.
Lemma wellformed_g_forall: forall g, wellformed_g g <->
forall x, In x (links g) -> (multiplicity (link_multiset g) x) <= 2.
Proof.
intros g.
split.
- intros H1 x H2.
unfold wellformed_g in H1. rewrite forallb_forall in H1.
apply in_unique_links in H2.
apply H1 in H2. rewrite PeanoNat.Nat.leb_le in H2.
apply H2.
- intros H1. unfold wellformed_g. rewrite forallb_forall.
intros x H2. rewrite PeanoNat.Nat.leb_le.
apply H1. apply in_unique_links. apply H2.
Qed.
Fixpoint link_list_eqb (l1 l2 : list Link) : bool :=
match l1,l2 with
| [],[] => true
| [],_ => false
| h1::t1,_ => match (remove_one h1 l2) with
| (true, l) => link_list_eqb t1 l
| (false, _) => false
end
end.
Definition link_list_eq (l1 l2 : list Link) : Prop :=
meq (list_to_multiset l1) (list_to_multiset l2).
Definition wellformed_r (r:Rule) : Prop :=
match r with
| {{lhs :- rhs}} => link_list_eq (freelinks lhs) (freelinks rhs)
end.
Definition substitute_link (Y X L : Link) :=
if L =? X then Y else L.
(* P[Y/X] *)
Fixpoint substitute (Y X:Link) (P:Graph) : Graph :=
match P with
| GZero => GZero
| GAtom a => GAtom (match a with
| AAtom p args => AAtom p (map (substitute_link Y X) args)
| AConn a b => AConn (substitute_link Y X a) (substitute_link Y X b)
end)
| {{P,Q}} => GMol (substitute Y X P) (substitute Y X Q)
end.
Notation "P [ Y / X ]" := (substitute Y X P) (in custom lmntal at level 40, left associativity) : lmntal_scope.
Example substitute_example :
{{ ( "p"("X", "Y"), "q"("Y", "X") ) [ "L" / "X" ] }} = {{ "p"("L", "Y"), "q"("Y", "L") }}.
Proof. reflexivity. Qed.
Reserved Notation "p == q" (at level 40).
Inductive cong : Graph -> Graph -> Prop :=
| cong_E1 : forall P, wellformed_g P ->
{{GZero, P}} == P
| cong_E2 : forall P Q, wellformed_g {{P, Q}} ->
{{P, Q}} == {{Q, P}}
| cong_E3 : forall P Q R, wellformed_g {{P, (Q, R)}} ->
{{P, (Q, R)}} == {{(P, Q), R}}
| cong_E4 : forall P X Y, wellformed_g P -> wellformed_g {{ P[Y/X] }} ->
In X (locallinks P) -> P == {{ P[Y/X] }}
| cong_E5 : forall P P' Q, wellformed_g {{ P,Q }} -> wellformed_g {{ P',Q }} ->
P == P' -> {{ P,Q }} == {{ P',Q }}
| cong_E7 : forall X, {{ X = X }} == GZero
| cong_E8 : forall X Y, {{ X = Y }} == {{ Y = X }}
| cong_E9 : forall X Y (A:Atom),
wellformed_g {{ X = Y, A }} -> wellformed_g {{ A[Y/X] }} ->
In X (freelinks A) ->
{{ X = Y, A }} == {{ A[Y/X] }}
| cong_refl : forall P, wellformed_g P ->
P == P
| cong_trans : forall P Q R, wellformed_g P -> wellformed_g Q -> wellformed_g R ->
P == Q -> Q == R -> P == R
| cong_sym : forall P Q, wellformed_g P -> wellformed_g Q ->
P == Q -> Q == P
where "p '==' q" := (cong p q).
Example cong_example : {{ "p"("X","X") }} == {{ "p"("Y","Y") }}.
Proof.
replace ({{ "p"("Y","Y") }}:Graph) with {{ "p"("X","X")["Y"/"X"] }}; auto.
apply cong_E4; unfold wellformed_g; auto.
simpl. auto.
Qed.
Ltac solve_refl :=
repeat (
unfold wellformed_g, wellformed_r,
freelinks, locallinks,
unique_links, substitute_link
|| rewrite Leq_dec_refl
|| rewrite eqb_refl
|| simpl); auto.
Example cong_example_var : forall p X Y, {{ p(X,X) }} == {{ p(Y,Y) }}.
Proof.
intros p X Y.
replace ({{ p(Y,Y) }}:Graph) with {{ p(X,X)[Y/X] }}.
- apply cong_E4; solve_refl.
- solve_refl.
Qed.
Reserved Notation "p '-[' r ']->' q" (at level 40, r custom lmntal at level 99, p constr, q constr at next level).
Inductive rrel : Rule -> Graph -> Graph -> Prop :=
| rrel_R1 : forall G1 G1' G2 r,
wellformed_g {{G1,G2}} -> wellformed_g {{G1',G2}} ->
wellformed_r r ->
G1 -[ r ]-> G1' -> {{G1,G2}} -[ r ]-> {{G1',G2}}
| rrel_R3 : forall G1 G1' G2 G2' r,
wellformed_r r ->
G2 == G1 -> G1' == G2' ->
G1 -[ r ]-> G1' -> G2 -[ r ]-> G2'
| rrel_R6 : forall T U,
wellformed_g T -> wellformed_g U ->
wellformed_r {{ T :- U }} ->
T -[ T :- U ]-> U
where "p '-[' r ']->' q" := (rrel r p q).
Reserved Notation "p '-[' r ']->*' q" (at level 40, r custom lmntal at level 99, p constr, q constr at next level).
Inductive rrel_rep : Rule -> Graph -> Graph -> Prop :=
| rrel_rep_refl : forall r a b, a == b -> a -[ r ]->* b
| rrel_rep_step : forall r a b c, a -[ r ]->* b -> b -[ r ]-> c -> a -[ r ]->* c
where "p '-[' r ']->*' q" := (rrel_rep r p q).
Reserved Notation "p '=[' r ']=>' q" (at level 40, r custom lmntal at level 99, p constr, q constr at next level).
Fixpoint rrel_ruleset (rs : RuleSet) (p q : Graph) : Prop :=
match rs with
| RZero => False
| RRule r => p -[ r ]-> q
| RMol a b => p =[ a ]=> q \/ p =[ b ]=> q
end
where "p '=[' rs ']=>' q" := (rrel_ruleset rs p q).
Reserved Notation "p '=[' r ']=>*' q" (at level 40, r custom lmntal at level 99, p constr, q constr at next level).
Inductive rrel_ruleset_rep : RuleSet -> Graph -> Graph -> Prop :=
| rrel_ruleset_rep_refl : forall rs a b, a == b -> a =[ rs ]=>* b
| rrel_ruleset_rep_step : forall rs a b c, a =[ rs ]=>* b -> b =[ rs ]=> c -> a =[ rs ]=>* c
where "p '=[' r ']=>*' q" := (rrel_ruleset_rep r p q).
Example rrel_example :
{{ "a"(), "b"("Z"), "c"("Z") }}
-[ "b"("X"),"c"("X") :- "d"() ]->
{{ "a"(), "d"() }}.
Proof.
apply rrel_R3 with (G1:={{"b" ("X"), "c" ("X"), "a" ()}}) (G1':={{"d" (), "a" ()}}).
- unfold wellformed_r. unfold link_list_eq. simpl. apply meq_refl.
- apply cong_trans with (Q:={{"a"(),("b"("Z"),"c"("Z"))}}); unfold wellformed_g; auto.
+ apply cong_sym; unfold wellformed_g; auto.
apply cong_E3; unfold wellformed_g; auto.
+ apply cong_trans with (Q:={{"b" ("Z"), "c" ("Z"), "a" ()}}); unfold wellformed_g; auto.
* apply cong_E2; unfold wellformed_g; auto.
* assert (H1: {{"b"("X"), "c"("X"), "a"()}}={{("b"("Z"), "c"("Z"),"a"())["X"/"Z"] }}).
{ reflexivity. }
rewrite H1.
apply cong_E4; unfold wellformed_g; auto.
simpl. auto.
- apply cong_E2; unfold wellformed_g; auto.
- apply rrel_R1; unfold wellformed_g; auto.
+ unfold wellformed_r. unfold link_list_eq. simpl. apply meq_refl.
+ apply rrel_R6; unfold wellformed_g; auto.
unfold wellformed_r. unfold link_list_eq. simpl. apply meq_refl.
Qed.
Example rrel_example_var : forall a b c d X Z,
{{ a(), b(Z), c(Z) }}
-[ b(X), c(X) :- d() ]->
{{ a(), d() }}.
Proof.
intros a b c d X Z.
apply rrel_R3 with (G1:={{b(X), c(X), a()}}) (G1':={{d(), a()}}).
- unfold wellformed_r. unfold link_list_eq. simpl.
solve_refl.
apply meq_refl.
- apply cong_trans with (Q:={{a(),(b(Z),c(Z))}}); solve_refl.
+ apply cong_sym; solve_refl.
apply cong_E3; solve_refl.
+ apply cong_trans with (Q:={{b(Z), c(Z), a()}}); solve_refl.
* apply cong_E2; solve_refl.
* assert (H1: {{b(X), c(X), a()}}={{(b(Z), c(Z), a())[X/Z] }}); solve_refl.
rewrite H1.
apply cong_E4; solve_refl.
- apply cong_E2; solve_refl.
- apply rrel_R1; solve_refl.
+ unfold link_list_eq. simpl. apply meq_refl.
+ apply rrel_R6; solve_refl. unfold link_list_eq. simpl. apply meq_refl.
Qed.
Fixpoint ruleset_to_list rs :=
match rs with
| RZero => []
| RRule r => [r]
| RMol a b => (ruleset_to_list a) ++ (ruleset_to_list b)
end.
Lemma rrel_ruleset_In :
forall p q rs, p =[ rs ]=> q <->
(exists r, In r (ruleset_to_list rs)
/\ p -[ r ]-> q).
Proof.
intros p q rs.
generalize dependent q.
generalize dependent p.
induction rs; split; intros H.
- simpl in H. destruct H.
- simpl in H. destruct H.
destruct H as [[] _].
- simpl in H. exists rule.
simpl. auto.
- simpl in H. destruct H.
destruct H as [[H1|[]] H2].
simpl. rewrite H1. auto.
- destruct H;
[ rewrite IHrs1 in H | rewrite IHrs2 in H ];
destruct H; destruct H as [H1 H2];
exists x; simpl;
rewrite in_app_iff; auto.
- destruct H. simpl.
simpl in H. rewrite in_app_iff in H.
destruct H as [[H1|H1] H2];
[ left | right ];
[ rewrite IHrs1 | rewrite IHrs2 ];
exists x; auto.
Qed.
Definition inv (r:Rule) : Rule :=
match r with
| {{ lhs:-rhs }} => {{ rhs :- lhs }}
end.
Lemma link_list_eq_commut : forall l1 l2,
link_list_eq l1 l2 <-> link_list_eq l2 l1.
Proof.
intros l1 l2.
unfold link_list_eq.
split.
- apply meq_sym.
- apply meq_sym.
Qed.
Lemma list_to_multiset_app:
forall l1 l2, meq (list_to_multiset (l1 ++ l2)) (munion (list_to_multiset l1) (list_to_multiset l2)).
Proof.
intros l1 l2.
induction l1.
- simpl. apply munion_empty_left.
- simpl. apply meq_trans
with (munion (SingletonBag eq Leq_dec a) (munion (list_to_multiset l1) (list_to_multiset l2))).
+ apply meq_right. apply IHl1.
+ apply meq_sym. apply munion_ass.
Qed.
Lemma link_multiset_mol:
forall G1 G2, meq (link_multiset {{G1,G2}}) (munion (link_multiset G1) (link_multiset G2)).
Proof.
intros G1. destruct G1.
- intros G2. unfold link_multiset.
replace (links GZero) with ([]:list Link).
+ simpl. apply munion_empty_left.
+ reflexivity.
- intros G2. unfold link_multiset. destruct atom; simpl.
{ apply list_to_multiset_app. }
apply meq_trans with (munion
(munion (SingletonBag eq Leq_dec X)
(SingletonBag eq Leq_dec Y))
(list_to_multiset (links G2))).
{ apply meq_sym, munion_ass. }
apply meq_left, meq_right, munion_empty_right.
- intros G2. unfold link_multiset.
replace (links {{G1_1, G1_2}}) with (links {{G1_1}} ++ links {{G1_2}}).
+ apply list_to_multiset_app.
+ reflexivity.
Qed.
Lemma multiplicity_munionL: forall {X:Type} (m m1 m2:multiset X) (x:X),
meq m (munion m1 m2) -> multiplicity m1 x <= multiplicity m x.
Proof.
intros X m m1 m2 x H.
unfold meq in H.
rewrite H.
unfold munion. simpl.
apply PeanoNat.Nat.le_add_r.
Qed.
Lemma multiplicity_munionR: forall {X:Type} (m m1 m2:multiset X) (x:X),
meq m (munion m1 m2) -> multiplicity m2 x <= multiplicity m x.
Proof.
intros X m m1 m2 x H.
unfold meq in H.
rewrite H.
unfold munion. simpl. rewrite PeanoNat.Nat.add_comm.
apply PeanoNat.Nat.le_add_r.
Qed.
Lemma links_mol:
forall G1 G2 x, In x (links {{G1,G2}}) <-> In x (links G1) \/ In x (links G2).
Proof.
intros G1 G2 x.
replace (links {{G1,G2}}) with (links G1 ++ links G2).
- apply in_app_iff.
- reflexivity.
Qed.
Lemma wellformed_g_inj:
forall G1 G2, wellformed_g {{G1,G2}} -> wellformed_g G1 /\ wellformed_g G2.
Proof.
intros G1 G2 H.
rewrite wellformed_g_forall in H.
split.
- rewrite wellformed_g_forall. intros x H1.
apply PeanoNat.Nat.le_trans with (multiplicity (link_multiset {{G1, G2}}) x).
+ apply multiplicity_munionL with (link_multiset G2).
apply link_multiset_mol.
+ apply H. rewrite links_mol. left. apply H1.
- rewrite wellformed_g_forall. intros x H1.
apply PeanoNat.Nat.le_trans with (multiplicity (link_multiset {{G1, G2}}) x).
+ apply multiplicity_munionR with (link_multiset G1).
apply link_multiset_mol.
+ apply H. rewrite links_mol. right. apply H1.
Qed.
Lemma connector_wellformed_g :
forall X Y, wellformed_g {{ X = Y }}.
Proof.
intros X Y.
unfold wellformed_g.
unfold unique_links.
unfold nodup.
simpl.
destruct (Leq_dec Y X) eqn:EYX; simpl.
- rewrite e. rewrite Leq_dec_refl. auto.
- destruct (Leq_dec X Y) eqn:EXY; simpl.
+ rewrite Leq_dec_refl. auto.
+ rewrite Leq_dec_refl. rewrite EYX.
rewrite Leq_dec_refl. auto.
Qed.
Lemma in_links_link_multiset:
forall P x, In x (links P) <-> 1 <= multiplicity (link_multiset P) x.
Proof.
intros P x.
unfold link_multiset.
induction (links P).
- simpl. split.
+ intros C. destruct C.
+ intros C. apply Compare_dec.nat_compare_ge in C.
simpl in C. destruct C. reflexivity.
- simpl. split; intros H.
+ destruct H.
* rewrite H. rewrite Leq_dec_refl.
apply le_n_S.
apply le_0_n.
* apply IHl in H.
rewrite PeanoNat.Nat.add_comm.
apply Plus.le_plus_trans.
apply H.
+ destruct (Leq_dec a x).
* left. auto.
* right. simpl in H. apply IHl.
apply H.
Qed.
Lemma wellformed_g_link_multiset:
forall P Q,
meq (link_multiset P) (link_multiset Q) ->
wellformed_g P -> wellformed_g Q.
Proof.
intros P Q H.
rewrite wellformed_g_forall.
rewrite wellformed_g_forall.
intros HP x Hx.
unfold meq in H. rewrite <- H.
apply HP.
apply in_links_link_multiset.
rewrite H.
apply in_links_link_multiset.
apply Hx.
Qed.
Lemma link_multiset_inj :
forall P1 Q1 P2 Q2,
meq (link_multiset P1) (link_multiset P2) ->
meq (link_multiset Q1) (link_multiset Q2) ->
meq (link_multiset {{P1, Q1}}) (link_multiset {{P2, Q2}}).
Proof.
intros P1 Q1 P2 Q2 H1 H2.
apply meq_trans with (munion (link_multiset P1) (link_multiset Q1)).
{ apply link_multiset_mol. }
apply meq_trans with (munion (link_multiset P2) (link_multiset Q2)).
{ apply meq_congr; auto. }
apply meq_sym. apply link_multiset_mol.
Qed.
Lemma link_multiset_swap :
forall P Q, meq (link_multiset {{P, Q}}) (link_multiset {{Q, P}}).
Proof.
intros P Q.
apply meq_trans with (munion (link_multiset P) (link_multiset Q)).
{ apply link_multiset_mol. }
apply meq_trans with (munion (link_multiset Q) (link_multiset P)).
{ apply munion_comm. }
apply meq_sym.
apply link_multiset_mol.
Qed.
Lemma link_multiset_assoc:
forall P Q R,
meq (link_multiset {{P, (Q, R)}}) (link_multiset {{P, Q, R}}).
Proof.
intros P Q R.
apply meq_trans with (munion (link_multiset P) (link_multiset {{Q,R}})).
{ apply link_multiset_mol. }
apply meq_trans with (munion (link_multiset P) (munion (link_multiset Q) (link_multiset R))).
{ apply meq_right. apply link_multiset_mol. }
apply meq_trans with (munion (munion (link_multiset P) (link_multiset Q)) (link_multiset R)).
{ apply meq_sym. apply munion_ass. }
apply meq_trans with (munion (link_multiset {{P,Q}}) (link_multiset R)).
{ apply meq_left. apply meq_sym. apply link_multiset_mol. }
apply meq_sym. apply link_multiset_mol.
Qed.
Lemma cong_wellformed_g :
forall P Q, P == Q -> wellformed_g P /\ wellformed_g Q.
Proof.
intros P Q H.
induction H; auto; split; auto.
- apply wellformed_g_link_multiset with {{P,Q}}; auto.
apply link_multiset_swap.
- apply wellformed_g_link_multiset with {{P,(Q,R)}}; auto.
apply link_multiset_assoc.
- apply connector_wellformed_g.
- unfold wellformed_g.
simpl. auto.
- apply connector_wellformed_g.
- apply connector_wellformed_g.
Qed.
Lemma rrel_wellformed :
forall P Q r, P -[ r ]-> Q ->
wellformed_g P /\ wellformed_g Q /\ wellformed_r r.
Proof.
intros P Q r H.
induction H; auto.
apply cong_wellformed_g in H0.
apply cong_wellformed_g in H1.
destruct H0. destruct H1.
auto.
Qed.
Theorem inv_rrel: forall r G G', G' -[r]-> G
-> let inv_r := inv r in G -[ inv_r ]-> G'.
Proof.
intros r G G' H. simpl.
assert (A: wellformed_r r -> wellformed_r (inv r)).
{ simpl. destruct r. apply link_list_eq_commut. }
induction H.
- apply rrel_R1; auto.
- apply rrel_R3 with (G1') (G1); auto.
+ apply cong_sym; auto; apply cong_wellformed_g in H1; destruct H1; auto.
+ apply cong_sym; auto; apply cong_wellformed_g in H0; destruct H0; auto.
- apply rrel_R6; auto.
Qed.
Lemma inv_inv : forall r, inv (inv r) = r.
Proof.
intros [lhs rhs]. reflexivity.
Qed.
Corollary inv_rrel_iff: forall r G G', G' -[r]-> G
<-> let inv_r := inv r in G -[ inv_r ]-> G'.
Proof.
intros r G G'.
split.
- apply inv_rrel.
- simpl. intros H. apply inv_rrel in H.
rewrite inv_inv in H.
apply H.
Qed.
Reserved Notation "p '==m' q" (at level 40).
Inductive congm : Graph -> Graph -> Prop :=
| congm_E1 : forall P, wellformed_g P ->
{{GZero, P}} ==m P
| congm_E2 : forall P Q, wellformed_g {{P, Q}} ->
{{P, Q}} ==m {{Q, P}}
| congm_E3 : forall P Q R, wellformed_g {{P, (Q, R)}} ->
{{P, (Q, R)}} ==m {{(P, Q), R}}
| congm_E5 : forall P P' Q, wellformed_g {{ P,Q }} -> wellformed_g {{ P',Q }} ->
P ==m P' -> {{ P,Q }} ==m {{ P',Q }}
| congm_E7 : forall X, {{ X = X }} ==m GZero
| congm_E9 : forall X Y (A:Atom),
wellformed_g {{ X = Y, A }} -> wellformed_g {{ A[Y/X] }} ->
In X (freelinks A) ->
{{ X = Y, A }} ==m {{ A[Y/X] }}
| congm_refl : forall P, wellformed_g P ->
P ==m P
| congm_trans : forall P Q R, wellformed_g P -> wellformed_g Q -> wellformed_g R ->
P ==m Q -> Q ==m R -> P ==m R
| congm_sym : forall P Q, wellformed_g P -> wellformed_g Q ->
P ==m Q -> Q ==m P
where "p '==m' q" := (congm p q).
Lemma in_freelinks:
forall X P, In X (freelinks P) <-> multiplicity (link_multiset P) X = 1.
Proof.
intros X P. unfold freelinks.
rewrite filter_In.
rewrite PeanoNat.Nat.eqb_eq.
split.
- intros [H1 H2]. auto.
- intros H. split; auto.
apply in_unique_links.
apply in_links_link_multiset.
rewrite H. auto.
Qed.
Lemma multiplicity_mol:
forall P Q X,
multiplicity (link_multiset {{P, Q}}) X
= multiplicity (link_multiset P) X + multiplicity (link_multiset Q) X.
Proof.
apply link_multiset_mol.
Qed.
Lemma multiplicity_not_in:
forall P X,
multiplicity (link_multiset P) X = 0
<-> ~ In X (links P).
Proof.
intros P X.
split; intros H.
- unfold not. intros H1.
apply in_links_link_multiset in H1.
rewrite H in H1.
apply Compare_dec.nat_compare_ge in H1.
auto.
- unfold link_multiset.
induction (links P); auto.
simpl. simpl in H.
destruct (Leq_dec a X); auto.
exfalso. auto.
Qed.
Lemma in_freelinks_mol:
forall X P Q, In X (freelinks {{P,Q}}) <->
In X (freelinks P) /\ ~ In X (links Q) \/
In X (freelinks Q) /\ ~ In X (links P).
Proof.
intros X P Q.
repeat rewrite in_freelinks.
rewrite multiplicity_mol.
split; intros H.
- apply PeanoNat.Nat.eq_add_1 in H.
destruct H as [[H1 H2]|[H1 H2]]; [left | right]; split; auto;
apply multiplicity_not_in; auto.
- destruct H as [[H1 H2]|[H1 H2]]; apply multiplicity_not_in in H2;
rewrite H1, H2; auto.
Qed.
Lemma in_locallinks:
forall X P, In X (locallinks P) <-> multiplicity (link_multiset P) X = 2.
Proof.
intros X P. unfold locallinks.
rewrite filter_In.
rewrite PeanoNat.Nat.eqb_eq.
split.
- intros [H1 H2]. auto.
- intros H. split; auto.
apply in_unique_links.
apply in_links_link_multiset.
rewrite H. auto.
Qed.
Lemma congm_E8_sub :
forall X Y Z, X <> Z -> Y <> Z ->
wellformed_g {{ Z=X, Z=Y }} ->
{{ Z=X, Z=Y }} ==m {{ X=Y }}.
Proof.
intros X Y Z Hxz Hyz H.
destruct (Leq_dec Y Z) eqn:EYZ.
{ congruence. }
apply congm_trans with {{ (Z=Y)[X/Z] }}; auto.
- apply connector_wellformed_g.
- apply connector_wellformed_g.
- apply congm_E9; auto.
{ simpl. apply connector_wellformed_g. }
unfold freelinks.
simpl. unfold unique_links.
simpl. rewrite EYZ.
simpl. repeat rewrite Leq_dec_refl.
rewrite EYZ. simpl. auto.
- simpl. solve_refl.
replace (Y =? Z) with false.
+ apply congm_refl.
apply connector_wellformed_g.
+ symmetry. apply eqb_neq. apply n.
Qed.
Lemma get_fresh_link_X_Y:
forall (X Y: Link), exists Z, X <> Z /\ Y <> Z.
Proof.
intros X Y.
destruct (Leq_dec X "X").
- destruct (Leq_dec Y "X").
+ rewrite e, e0. exists "Y".
rewrite <- eqb_neq. auto.
+ rewrite e. destruct (Leq_dec Y "Y").
* rewrite e0. exists "Z".
repeat rewrite <- eqb_neq. auto.
* exists "Y".
rewrite <- eqb_neq. auto.
- destruct (Leq_dec Y "X").
+ rewrite e. destruct (Leq_dec X "Y").
* rewrite e0. exists "Z".
repeat rewrite <- eqb_neq. auto.
* exists "Y".
split; auto.
rewrite <- eqb_neq. auto.
+ exists "X". auto.
Qed.
Lemma wellformed_g_swap :
forall P Q, wellformed_g {{P,Q}} -> wellformed_g {{Q,P}}.
Proof.
intros P Q H.
apply wellformed_g_link_multiset with {{P,Q}}.
- apply link_multiset_swap.
- auto.
Qed.
Lemma wellformed_g_zx_zy:
forall X Y Z,
X <> Z -> Y <> Z -> wellformed_g {{Z = X, Z = Y}}.
Proof.
intros X Y Z Hxz Hyz.
apply wellformed_g_forall.
intros L H1. simpl in H1. simpl.
destruct H1 as [H1|[H1|[H1|[H1|[]]]]]; rewrite <- H1;
rewrite Leq_dec_refl.
- destruct (Leq_dec X Z);
destruct (Leq_dec Y Z);
try congruence; auto.
- destruct (Leq_dec Z X);
destruct (Leq_dec Y X);
try congruence; auto.
- destruct (Leq_dec X Z);
destruct (Leq_dec Y Z);
try congruence; auto.
- destruct (Leq_dec Z Y);
destruct (Leq_dec X Y);
try congruence; auto.
Qed.
Lemma congm_E8 :
forall X Y, {{X = Y}} ==m {{Y = X}}.
Proof.
intros X Y.
assert (AZ: exists Z, X <> Z /\ Y <> Z).
{ apply get_fresh_link_X_Y. }
destruct AZ as [Z].
destruct H.
assert (WF: wellformed_g {{Z = X, Z = Y}}).
{ apply wellformed_g_zx_zy; auto. }
apply congm_trans with {{ Z=X, Z=Y }}.
- apply connector_wellformed_g.
- apply WF.
- apply connector_wellformed_g.
- apply congm_sym; auto.
+ apply connector_wellformed_g.
+ apply congm_E8_sub; auto.
- apply congm_trans with {{ Z=Y,Z=X }}; auto.
+ apply wellformed_g_swap. auto.
+ apply connector_wellformed_g.
+ apply congm_E2. auto.
+ apply congm_E8_sub; auto.
apply wellformed_g_swap. auto.
Qed.
Lemma sum_2 :
forall a b,
a + b = 2 <->
a = 2 /\ b = 0 \/
a = 1 /\ b = 1 \/
a = 0 /\ b = 2.
Proof.
intros a b.
split.
- intros H.
destruct a; auto.
destruct a; auto.
simpl in H. left.
inversion H.
apply PeanoNat.Nat.eq_add_0 in H1.
destruct H1.
rewrite !H0, !H1.
auto.
- intros [[H1 H2]|[[H1 H2]|[H1 H2]]];
rewrite H1,H2; auto.
Qed.
Lemma in_locallinks_mol :
forall P Q X,
In X (locallinks {{P,Q}}) <->
In X (locallinks P) /\ ~ In X (links Q) \/
In X (locallinks Q) /\ ~ In X (links P) \/
In X (freelinks P) /\ In X (freelinks Q).
Proof.
intros P Q X.
repeat rewrite in_locallinks.
repeat rewrite in_freelinks.
repeat rewrite <- multiplicity_not_in.
rewrite multiplicity_mol.
rewrite sum_2.
split; intros [[H1 H2]|[[H1 H2]|[H1 H2]]];
rewrite !H1,!H2; auto.
Qed.
Lemma subst_mol:
forall P Q X Y,
{{(P,Q)[Y/X]}} = {{P[Y/X],Q[Y/X]}}.
Proof.
intros P Q X Y.
induction P; auto.
Qed.
Lemma subst_multiplicity_X:
forall P X Y,
X <> Y ->
multiplicity (link_multiset {{P[Y/X]}}) X = 0.
Proof.
intros P.
induction P; intros X Y H; auto.
- destruct atom as [n ls|a b]; simpl.
{ induction ls as [|h t IH]; auto.
simpl. unfold link_multiset in IH.
simpl in IH. rewrite IH. solve_refl.
destruct (h =? X) eqn:E1.
+ destruct (Leq_dec Y X); auto.
destruct H. auto.
+ destruct (Leq_dec h X); auto.
apply eqb_neq in E1. destruct E1.
auto. }
solve_refl. destruct (a =? X) eqn:e1.
+ destruct (Leq_dec Y X).
* destruct H. auto.
* destruct (b =? X) eqn:e2.
{ destruct (Leq_dec Y X); auto. destruct n. auto. }
destruct (Leq_dec b X); auto.
apply eqb_neq in e2. destruct e2.
auto.
+ destruct (Leq_dec a X).
{ apply eqb_neq in e1. destruct e1. auto. }
destruct (b =? X) eqn:e2.
{ destruct (Leq_dec Y X); auto.
destruct H. auto. }
destruct (Leq_dec b X); auto.
apply eqb_neq in e2. destruct e2. auto.
- rewrite subst_mol.
rewrite link_multiset_mol. simpl.
apply PeanoNat.Nat.eq_add_0.
auto.
Qed.
Lemma subst_multiplicity_Y:
forall P X Y, X <> Y ->
multiplicity (link_multiset {{P[Y/X]}}) Y =
multiplicity (link_multiset P) X + multiplicity (link_multiset P) Y.
Proof.
intros P X Y H.
induction P; auto.
- destruct atom as [n ls|x y].
{ simpl. induction ls as [|h t IH]; auto.
unfold link_multiset in IH.
simpl in IH.
simpl. destruct (Leq_dec h X).
+ rewrite e. solve_refl.
unfold substitute_link in IH.
f_equal. rewrite IH.
f_equal. destruct (Leq_dec X Y); auto.
destruct H. auto.
+ solve_refl. unfold substitute_link in IH.
apply eqb_neq in n0.
rewrite n0. simpl.
destruct (Leq_dec h Y); auto.
simpl. rewrite IH. auto. }
simpl. solve_refl.
destruct (x =? X) eqn:e1.
+ apply eqb_eq in e1. rewrite e1. solve_refl.
destruct (y =? X) eqn:e2.
* apply eqb_eq in e2. rewrite e2. solve_refl.
repeat f_equal.
apply Leq_dec_neq in H.
destruct H. rewrite H. auto.
* apply eqb_neq in e2. apply Leq_dec_neq in e2.
destruct e2. rewrite H0.
apply Leq_dec_neq in H.
destruct H. rewrite H. simpl. auto.
+ apply eqb_neq in e1.
apply Leq_dec_neq in e1. destruct e1.
rewrite H0. destruct (y =? X) eqn:e2.
* apply eqb_eq in e2. rewrite e2. solve_refl.
apply Leq_dec_neq in H. destruct H.
rewrite H. auto.
* apply eqb_neq in e2. apply Leq_dec_neq in e2.
destruct e2. rewrite H1. auto.
- rewrite subst_mol, !multiplicity_mol.
rewrite IHP1, IHP2.
apply PeanoNat.Nat.add_shuffle1.
Qed.
Lemma subst_id :
forall P X, {{P[X/X]}} = P.
Proof.
intros P X.
induction P; auto.
- destruct atom as [n ls|x y].
{ simpl. induction ls as [|h t IH]; auto.
simpl. replace (map (substitute_link X X) t) with t.
+ solve_refl. destruct (h =? X) eqn:E; auto.
apply eqb_eq in E.
rewrite E. auto.
+ inversion IH. repeat rewrite H0. auto. }
simpl. solve_refl.
destruct (x =? X) eqn:E1; destruct (y =? X) eqn:E2.
+ apply eqb_eq in E1,E2. rewrite E1,E2. auto.
+ apply eqb_eq in E1. rewrite E1. auto.
+ apply eqb_eq in E2. rewrite E2. auto.
+ auto.
- rewrite subst_mol.
f_equal; auto.
Qed.
Lemma subst_none :
forall P X Y,
P = {{ P[Y/X] }} <-> X = Y \/ ~ In X (links P).
Proof.
intros P X Y.
split; intros H.
- destruct (Leq_dec X Y); auto.
right. induction P; auto.
+ destruct atom as [name ls|x y].
{ simpl in H. simpl. inversion H.
rewrite <- H1. induction ls as [|h t IH]; auto.
simpl in H1. inversion H1. solve_refl.
unfold substitute_link in H1,IH,H2,H3.
destruct (h =? X) eqn:E.
{ apply eqb_eq in E. congruence. }
rewrite <- H3. simpl.
intros C.
destruct C.
{ apply eqb_neq in E. congruence. }
apply IH; auto.
rewrite <- H3.
reflexivity. }