Repository navigation
Expand file tree
/
Copy pathPathN.v
More file actions
3036 lines (2817 loc) · 119 KB
/
Copy pathPathN.v
File metadata and controls
3036 lines (2817 loc) · 119 KB
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
From Stdlib Require Import List Utf8
BinNatDef Lia.
From Semiring Require Import OrelN Structures.
Import ListNotations SemiringNotations.
Section Generic.
Context {A : Type}
{hdec : ∀ (x y : A), {x = y} + {x ≠ y}}.
(** c covers l, i.e., every element of l appears in c *)
Definition covers {A : Type} (l c : list A) : Prop :=
forall (x : A), In x l -> In x c.
Lemma covers_list_elem :
forall (c l : list A), (forall y : A, List.In y c) ->
covers l c.
Proof using Type.
unfold covers.
destruct c as [|a c].
+ intros ? Hy ? Hl.
specialize (Hy x).
simpl in Hy.
inversion Hy.
+ intros ? Hy ? Hl.
simpl in *.
exact (Hy x).
Qed.
(** Pigeonhole principle: if every element of [l] appears in [c]
(i.e., [covers l c]) and [c] is shorter than [l], then some
element [a] occurs at least twice in [l]. The proof uses
to decide membership on the type [A] with decidable equality. *)
Lemma covers_pigenhole :
forall (l c : list A),
covers l c -> (length c < List.length l) ->
exists a l₁ l₂ l₃, l = (l₁ ++ [a] ++ l₂ ++ [a] ++ l₃).
Proof.
(** [In a l] implies [l = l₁ ++ a :: l₂] without needing decidable equality. *)
assert (in_split' : forall (a : A) (l : list A), In a l ->
exists l₁ l₂, l = l₁ ++ a :: l₂).
{ induction l as [|b l IH]; intros Hin.
- inversion Hin.
- destruct Hin as [Heq | Hin].
+ subst b. exists [], l. reflexivity.
+ destruct (IH Hin) as [l₁ [l₂ Hl]].
exists (b :: l₁), l₂. cbn. f_equal. exact Hl. }
induction l as [|a l' IH]; intros c Hcov Hlen.
- (* l = [] *)
cbn in Hlen. lia.
- (* l = a :: l' *)
cbn in Hlen.
unfold covers in Hcov.
assert (Ha_c : In a c) by (apply Hcov; left; reflexivity).
apply in_split' in Ha_c. destruct Ha_c as [c₁ [c₂ Hc]].
destruct (@in_dec A hdec a l') as [Hin | Hnotin].
+ (* a ∈ l': found the duplicate *)
apply in_split' in Hin. destruct Hin as [l₁ [l₂ Hl']].
exists a, [], l₁, l₂.
cbn. f_equal. exact Hl'.
+ (* a ∉ l' *)
assert (Hcov' : covers l' (c₁ ++ c₂)).
{ unfold covers. intros x Hx.
pose proof (Hcov x (or_intror Hx)) as Hx_c.
rewrite Hc in Hx_c.
apply in_app_or in Hx_c.
destruct Hx_c as [Hx_c | [Heq | Hx_c]].
- apply in_or_app. left. exact Hx_c.
- subst x. exfalso. apply Hnotin. exact Hx.
- apply in_or_app. right. exact Hx_c. }
(** Length inequality *)
assert (Hlen' : length (c₁ ++ c₂) < length l').
{ rewrite Hc in Hlen.
rewrite !length_app in Hlen. cbn in Hlen.
assert (Htemp : length c₁ + length c₂ < length l') by lia.
rewrite length_app. exact Htemp. }
apply IH in Hcov'; [| exact Hlen'].
destruct Hcov' as [a' [l₁ [l₂ [l₃ Hl']]]].
exists a', (a :: l₁), l₂, l₃.
cbn. f_equal. exact Hl'.
Qed.
End Generic.
Section Path.
Context
{Node : FinType.type}.
(** A matrix over semiring [R] indexed by finite type [Node]. *)
Let Matrix {R : Semiring.type} := @OrelN.Matrix Node R.
(** a path is a triple *)
Definition Path {R : Semiring.type} : Type :=
Node * Node * list (Node * Node * R).
Definition source {R : Semiring.type} (c : Node)
(l : list (Node * Node * R)) : bool :=
match l with
| [] => false
| (x, _, _) :: _ =>
match fin_eq_dec c x with
| left _ => true
| right _ => false
end
end.
Fixpoint target {R : Semiring.type} (d : Node)
(l : list (Node * Node * R)) : bool :=
match l with
| [] => false
| (x, y, r) :: t => match t with
| [] => match fin_eq_dec d y with
| left _ => true
| right _ => false
end
| hs :: ts => target d t
end
end.
(** path strength between c and d *)
Fixpoint measure_of_path {R : Semiring.type}
(l : list (Node * Node * R)) : R :=
match l with
| [] => 1
| (_, _, v) :: t => v * measure_of_path t
end.
Fixpoint well_formed_path_aux {R : Semiring.type}
(m : @Matrix R) (l : list (Node * Node * R)) : Prop :=
match l with
| [] => True
| (c, x, v) :: tl => (m c x = v) ∧ match tl with
| [] => True
| (y, _, _) :: _ => (x = y) ∧ well_formed_path_aux m tl
end
end.
Definition t_proj {R : Semiring.type} (p : @Path R) :
list (Node * Node * R):=
match p with
|(_, _, l) => l
end.
(** stick a node 'c' in all the paths, represented by l *)
Fixpoint append_node_in_paths {R : Semiring.type}
(m : @Matrix R) (c : Node) (l : list (list (Node * Node * R))) :
list (list (Node * Node * R)) :=
match l with
| [] => []
| h :: t => match h with
| [] => append_node_in_paths m c t
| (x, _, _) :: ht =>
((c, x, m c x) :: h) :: append_node_in_paths m c t
end
end.
(** list of all paths of lenghth k from c to d.
xs is list of all candidates *)
Fixpoint all_paths_klength {R : Semiring.type} (xs : list Node)
(m : @Matrix R) (k : nat)
(c d : Node) : list (list (Node * Node * R)) :=
match k with
| O => if fin_eq_dec c d then [[(c, d, 1)]] else []
| S k' =>
let lf := List.flat_map
(fun x => all_paths_klength xs m k' x d) xs
in append_node_in_paths m c lf
end.
Definition construct_all_paths {R : Semiring.type}
(xs : list Node) (m : Node -> Node -> R) (k : nat)
(c d : Node) : list Path :=
let lp := all_paths_klength xs m k c d in
List.map (fun l => (c, d, l)) lp.
(** get all the R values from path *)
Definition get_all_rvalues {R : Semiring.type}
(pl : list Path) : list R :=
List.map (fun '(_, _, l) => measure_of_path l) pl.
Definition sum_all_rvalues {R : Semiring.type} (pl : list R) :=
List.fold_right (fun b a => b + a) 0 pl.
Definition cyclic_path {R : Semiring.type} (c : Node)
(l : list (Node * Node * R)) : Prop :=
l <> [] /\ source c l = true /\
target c l = true.
(** assume that path is well_founded *)
Fixpoint collect_nodes_from_a_path {R : Semiring.type}
(l : list (Node * Node * R)) : list Node :=
match l with
| [] => []
| (a, b, _) :: t => match t with
| [] => [a; b]
| _ :: _ => a :: collect_nodes_from_a_path t
end
end.
(** Checks if au is second element of path or not *)
Fixpoint elem_path_triple_tail {R : Semiring.type}
(au : Node) (l : list (Node * Node * R)) : bool :=
match l with
| [] => false
| (bu, bv, _) :: t =>
if (fin_eq_dec au bv) then true
else elem_path_triple_tail au t
end.
Fixpoint keep_collecting {R : Semiring.type} (au : Node) (l : list (Node * Node * R)) :=
match l with
| [] => []
| (bu, bv, bw) :: t => if fin_eq_dec au bv then [(bu, bv, bw)] else
(bu, bv, bw) :: keep_collecting au t
end.
Fixpoint keep_dropping {R : Semiring.type} (au : Node) (l : list (Node * Node * R)) :=
match l with
| [] => []
| (bu, bv, bw) :: t => if (fin_eq_dec au bv) then t else
keep_dropping au t
end.
(** computes the loop in a path *)
Fixpoint elem_path_triple_compute_loop {R : Semiring.type} (l : list (Node * Node * R)) :=
match l with
| [] => None
| (au, av, aw) :: t => if fin_eq_dec au av then Some [(au, av, aw)] (* loop at the head, 1 length *)
else
if elem_path_triple_tail au t then Some ((au, av, aw) :: keep_collecting au t)
else elem_path_triple_compute_loop t
end.
(** This function is very similar to the above one, except it returns the
left over from the front ++ loop ++ rest of the list *)
Fixpoint elem_path_triple_compute_loop_triple {R : Semiring.type}
(l : list (Node * Node * R)) :=
match l with
| [] => ([], None, [])
| (au, av, aw) :: t => if fin_eq_dec au av then ([], Some [(au, av, aw)], t)
else
if elem_path_triple_tail au t then
([], Some ((au, av, aw) :: keep_collecting au t), keep_dropping au t)
else match elem_path_triple_compute_loop_triple t with
| (fp, sp, tp) => ((au, av, aw) :: fp, sp, tp)
end
end.
(** elem_path_triple l = true means l does not have any cycle *)
Fixpoint elem_path_triple {R : Semiring.type} (l : list (Node * Node * R)) : bool :=
match l with
| [] => true
| (au, av, _) :: t =>
negb (if fin_eq_dec au av then true else false) &&
negb (elem_path_triple_tail au t) &&
elem_path_triple t
end.
Fixpoint partial_sum_paths {R : Semiring.type} (l : list Node)
(m : @Matrix R) (n : nat) (c d : Node) : R :=
match n with
| O => if fin_eq_dec c d then 1 else 0
| S n' => partial_sum_paths l m n' c d +
sum_all_rvalues (get_all_rvalues (construct_all_paths l m n c d))
end.
(** Get all the paths in one big list *)
Fixpoint enum_all_paths_flat {R : Semiring.type} (l : list Node)
(m : @Matrix R) (n : nat) (c d : Node) : list Path :=
match n with
| O => construct_all_paths l m O c d
| S n' =>
construct_all_paths l m n c d ++
enum_all_paths_flat l m n' c d
end.
Fixpoint sum_all_flat_paths {R : Semiring.type} (l : list Path) : R :=
match l with
| [] => 0
| (_, _, h) :: t => measure_of_path h +
sum_all_flat_paths t
end.
(** Proofs start from here *)
(** If [xs] is produced by [append_node_in_paths m c l], then [xs]
begins with the edge [(c, y, m c y)] for some [y], its tail [ys]
is non-empty, and both [source c xs] and [source y ys] hold. *)
Lemma append_node_in_paths_shape {R : Semiring.type} :
∀ (l : list (list (Node * Node * R)))
(m : @Matrix R) (c : Node)
(xs : list (Node * Node * R)),
List.In xs (append_node_in_paths m c l) ->
∃ (y : Node) (ys : list (Node * Node * R)),
xs = ((c, y, m c y) :: ys) ∧
source c xs = true ∧
source y ys = true ∧
ys ≠ [].
Proof.
induction l as [|h t IH]; intros m c xs Hin.
- inversion Hin.
- simpl in Hin.
destruct h as [|ha ht].
+ apply (IH m c xs Hin).
+ destruct ha as [[x y] r].
destruct Hin as [Hin | Hin].
* subst xs.
exists x, ((x, y, r) :: ht).
unfold source. simpl.
destruct (fin_eq_dec c c) as [_|Hc]; [|exfalso; apply Hc; reflexivity].
destruct (fin_eq_dec x x) as [_|Hx]; [|exfalso; apply Hx; reflexivity].
split; [reflexivity|].
split; [reflexivity|].
split; [reflexivity|].
intro Hnil. inversion Hnil.
* apply (IH m c xs Hin).
Qed.
(** If [xs] is in [append_node_in_paths m c l], then [xs] is exactly
[(c, y, m c y)] prepended to some [ys] that was already in [l]. *)
Lemma append_node_in_paths_In {R : Semiring.type} (m : @Matrix R) (c : Node)
(l : list (list (Node * Node * R))) (xs : list (Node * Node * R)) :
In xs (append_node_in_paths m c l) ->
exists (y : Node) (ys : list (Node * Node * R)),
xs = (c, y, m c y) :: ys /\ In ys l.
Proof.
induction l as [|h t IH]; intros Hin.
- inversion Hin.
- simpl in Hin.
destruct h as [|ha ht].
+ apply IH in Hin. destruct Hin as (y & ys & Heq & Hin').
exists y, ys. split; [exact Heq | right; exact Hin'].
+ destruct ha as [[x y'] r].
destruct Hin as [Hin | Hin].
* subst xs. exists x, ((x, y', r) :: ht).
split; [reflexivity | left; reflexivity].
* apply IH in Hin. destruct Hin as (y & ys & Heq & Hin').
exists y, ys. split; [exact Heq | right; exact Hin'].
Qed.
(** Every path returned by [all_paths_klength elements m n c d] is
non-empty, starts at [c], and ends at [d]. *)
Lemma non_empty_paths_in_kpath {R : Semiring.type} :
∀ (n : nat) (m : @Matrix R)
(c d : Node) (xs : list (Node * Node * R)),
List.In xs (all_paths_klength elements m n c d) ->
xs ≠ [] ∧ source c xs = true ∧ target d xs = true.
Proof.
induction n as [|n' IH]; intros m c d xs Hin.
- (* n = 0 *)
simpl in Hin.
destruct (fin_eq_dec c d) as [Heq | Hneq].
+ subst d. destruct Hin as [Hin | []].
subst xs.
split; [intro Hnil; inversion Hnil | ].
unfold source. simpl.
destruct (fin_eq_dec c c) as [_|Hc]; [|exfalso; apply Hc; reflexivity].
split; [reflexivity | ].
unfold target. simpl.
destruct (fin_eq_dec c c) as [_|Hc]; [|exfalso; apply Hc; reflexivity].
reflexivity.
+ inversion Hin.
- (* n = S n' *)
simpl in Hin.
apply append_node_in_paths_In in Hin.
destruct Hin as (y & ys & Heq & Hin_flat).
subst xs.
apply in_flat_map in Hin_flat.
destruct Hin_flat as (x & Hin_x & Hin_ys).
apply (IH m x d) in Hin_ys.
destruct Hin_ys as (Hys_ne & Hsrc & Htgt).
split; [intro Hnil; inversion Hnil | ].
unfold source. simpl.
destruct (fin_eq_dec c c) as [_|Hc]; [|exfalso; apply Hc; reflexivity].
split; [reflexivity | ].
cbn [target].
destruct ys as [|h ys']; [exfalso; apply Hys_ne; reflexivity | ].
exact Htgt.
Qed.
(** If we prepend a well-formed edge [(c, y, m c y)] to a well-formed
path [ys], the result is well-formed. The condition [source y ys]
ensures the new edge connects properly to the first edge of [ys]. *)
Lemma well_formed_by_extending {R : Semiring.type} :
forall (xs ys : list (Node * Node * R)) (c y : Node) (m : @Matrix R),
ys <> [] -> xs = ((c, y, m c y) :: ys)->
source y ys = true ->
well_formed_path_aux m (List.tl xs) ->
well_formed_path_aux m xs.
Proof.
intros xs ys c y m Hys_ne Heq Hsrc_ys Htl.
subst xs. cbn.
split; [reflexivity | ].
destruct ys as [|h ys']; [exfalso; apply Hys_ne; reflexivity | ].
destruct h as [[z w] r].
unfold source in Hsrc_ys. simpl in Hsrc_ys.
destruct (fin_eq_dec y z) as [Heq_yz | Hneq].
- subst z. split; [reflexivity | exact Htl].
- discriminate Hsrc_ys.
Qed.
(** Every path returned by [all_paths_klength elements m n c d] is
well-formed with respect to [m], provided the diagonal of [m] is 1. *)
Lemma all_paths_well_formed_in_kpaths {R : Semiring.type} :
forall (n : nat) (m : @Matrix R)
(c d : Node) (xs : list (Node * Node * R)),
(forall c d, c = d -> m c d = 1) ->
List.In xs (all_paths_klength elements m n c d) ->
well_formed_path_aux m xs.
Proof.
induction n as [|n' IH]; intros m c d xs Hdiag Hin.
- (* n = 0 *)
simpl in Hin.
destruct (fin_eq_dec c d) as [Heq | Hneq].
+ subst d. destruct Hin as [Hin | []].
subst xs. unfold well_formed_path_aux. cbn.
split; [apply Hdiag; reflexivity | exact I].
+ inversion Hin.
- (* n = S n' *)
simpl in Hin.
(** Keep a copy of Hin for the membership lemma *)
assert (Hin_copy := Hin).
(** Extract structural properties *)
apply (append_node_in_paths_shape
(List.flat_map (fun x => all_paths_klength elements m n' x d) elements)
m c xs) in Hin.
destruct Hin as (y & ys & Heq & Hsrc_xs & Hsrc_ys & Hys_ne).
subst xs.
(** Extract membership: ys is from the flat_map *)
apply append_node_in_paths_In in Hin_copy.
destruct Hin_copy as (y2 & ys2 & Heq2 & Hin_flat).
(** Heq2: (c,y,m c y)::ys = (c,y2,m c y2)::ys2 *)
inversion Heq2. subst y2 ys2.
apply in_flat_map in Hin_flat.
destruct Hin_flat as (x & Hin_el & Hin_ys).
(** Get well-formedness of the tail via IH *)
apply (IH m x d ys Hdiag) in Hin_ys.
(** Assemble using the extension lemma *)
apply (well_formed_by_extending ((c, y, m c y) :: ys) ys c y m
Hys_ne eq_refl Hsrc_ys Hin_ys).
Qed.
(** Every path in [all_paths_klength elements m k c d] ends with the
unit loop [(d, d, 1)], i.e., it can be written as a prefix followed
by that loop. *)
Lemma path_end_unit_loop {R : Semiring.type} :
forall k l (m : @Matrix R) (c d : Node),
List.In l (all_paths_klength elements m k c d) ->
exists l', l = (l' ++ [(d, d, 1)]).
Proof.
induction k as [|k' IH]; intros l m c d Hin.
- (* k = 0 *)
simpl in Hin.
destruct (fin_eq_dec c d) as [Heq | Hneq].
+ subst d. destruct Hin as [Hin | []].
subst l. exists []. reflexivity.
+ inversion Hin.
- (* k = S k' *)
simpl in Hin.
apply append_node_in_paths_In in Hin.
destruct Hin as (y & ys & Heq & Hin_flat).
subst l.
apply in_flat_map in Hin_flat.
destruct Hin_flat as (x & Hin_el & Hin_ys).
apply (IH ys m x d) in Hin_ys.
destruct Hin_ys as [l' Hl'].
exists ((c, y, m c y) :: l').
rewrite Hl'. reflexivity.
Qed.
(** Every path in [all_paths_klength elements m k c d] has length
[S k] (i.e., [k+1] edges). *)
Lemma all_paths_in_klength {R : Semiring.type} :
∀ (k : nat) (m : @Matrix R) (c d : Node) xs,
List.In xs (all_paths_klength elements m k c d) ->
List.length xs = S k.
Proof.
induction k as [|k' IH]; intros m c d xs Hin.
- (* k = 0 *)
simpl in Hin.
destruct (fin_eq_dec c d) as [Heq | Hneq].
+ subst d. destruct Hin as [Hin | []].
subst xs. reflexivity.
+ inversion Hin.
- (* k = S k' *)
simpl in Hin.
apply (append_node_in_paths_In m c
(List.flat_map (fun x => all_paths_klength elements m k' x d) elements)
xs) in Hin.
destruct Hin as (y & ys & Heq & Hin_flat).
subst xs.
apply in_flat_map in Hin_flat.
destruct Hin_flat as (x & Hin_el & Hin_ys).
apply (IH m x d ys) in Hin_ys.
simpl. rewrite Hin_ys. reflexivity.
Qed.
(** Combined property: every path in [all_paths_klength] is non-empty,
starts at [c], ends at [d], is well-formed, has length [S n], and
ends with the unit loop [(d, d, 1)]. *)
Lemma source_target_non_empty_kpath_and_well_formed {R : Semiring.type} :
∀ (n : nat) (m : @Matrix R)
(c d : Node) (xs : list (Node * Node * R)),
(forall c d, c = d-> m c d = 1) ->
List.In xs (all_paths_klength elements m n c d) ->
xs ≠ [] ∧ source c xs = true ∧ target d xs = true ∧
well_formed_path_aux m xs ∧ (List.length xs = S n)%nat ∧
exists xs', xs = (xs' ++ [(d, d, 1)]).
Proof.
intros n m c d xs Hdiag Hin.
assert (Hin1 := Hin). assert (Hin2 := Hin).
assert (Hin3 := Hin). assert (Hin4 := Hin).
apply non_empty_paths_in_kpath in Hin as (Hne & Hsrc & Htgt).
apply all_paths_well_formed_in_kpaths in Hin1 as Hwf; [| exact Hdiag].
apply all_paths_in_klength in Hin2 as Hlen.
apply path_end_unit_loop in Hin3 as Hend.
split; [exact Hne | ].
split; [exact Hsrc | ].
split; [exact Htgt | ].
split; [exact Hwf | ].
split; [exact Hlen | ].
exact Hend.
Qed.
(** [target] also depends only on the last element of a non-empty list. *)
Lemma target_end {R : Semiring.type} :
forall (l : list (Node * Node * R))
(x : Node * Node * R) (d : Node),
target d (l ++ [x]) =
target d [x].
Proof.
induction l.
- simpl; intros ? ?. reflexivity.
- intros ? ?.
assert (Ht : target d ((a :: l) ++ [x]) =
target d (l ++ [x])).
simpl. destruct a. destruct p.
destruct (l ++ [x]) eqn:Hv.
pose proof app_eq_nil l [x] Hv as Hw.
destruct Hw as [Hwl Hwr].
congruence. reflexivity.
rewrite Ht. apply IHl.
Qed.
(** If a well-formed path [xs] starts at [c] and its non-empty tail
belongs to [l], then [xs] belongs to [append_node_in_paths m c l].
This is the reverse direction of [append_node_in_paths_In]. *)
Lemma In_append_node_in_paths_rev {R : Semiring.type} :
forall l (m : @Matrix R) c xs,
source c xs = true ->
List.tl xs <> [] ->
well_formed_path_aux m xs ->
List.In (List.tl xs) l ->
List.In xs (append_node_in_paths m c l).
Proof.
intros l m c xs Hsrc Htl_ne Hwf Hin_tl.
(** Extract the structure of xs from the hypotheses *)
unfold source in Hsrc.
destruct xs as [|h rest]; [discriminate Hsrc | ].
destruct h as [[c' x] v].
simpl in Hsrc.
destruct (fin_eq_dec c c') as [Heq | Hneq]; [| discriminate Hsrc].
subst c'.
(** xs = (c, x, v) :: rest *)
cbn [List.tl] in Htl_ne, Hin_tl.
(** Htl_ne : rest ≠ [], Hin_tl : In rest l *)
unfold well_formed_path_aux in Hwf. cbn in Hwf.
destruct Hwf as [Hmv Hrest].
destruct rest as [|h2 rest']; [exfalso; apply Htl_ne; reflexivity | ].
destruct h2 as [[au av] aw].
destruct Hrest as [Heq_x Hwf_rest].
(** Hmv: m c x = v, Heq_x: x = au *)
subst x. subst v.
(** xs = (c, au, m c au) :: (au, av, aw) :: rest' *)
(** Goal: In ((c, au, m c au) :: (au, av, aw) :: rest')
(append_node_in_paths m c l) *)
induction l as [|h t IH].
- inversion Hin_tl.
- simpl.
destruct h as [|h0 ht].
+ (* h = []: skip empty paths, recurse *)
apply IH. destruct Hin_tl as [Hin | Hin].
* discriminate Hin.
* exact Hin.
+ (* h = (x0, y0, r0) :: ht *)
destruct h0 as [[x0 y0] r0].
destruct Hin_tl as [Hin | Hin].
* (* rest = h: the prepended path is exactly xs *)
inversion Hin. subst. left. reflexivity.
* (* rest in t: recurse *)
right. apply IH. exact Hin.
Qed.
(** Completeness: if a path ending with [(d, d, 1)] satisfies
source, target, and well-formedness, then it belongs to
[all_paths_klength] with the appropriate length. *)
Lemma all_paths_klength_complete
{R : Semiring.type} :
∀ (xs : list (Node * Node * R))
(m : @Matrix R) (c d : Node),
source c (xs ++ [(d, d, 1)]) = true ->
target d (xs ++ [(d, d, 1)]) = true ->
well_formed_path_aux m (xs ++ [(d, d, 1)]) ->
List.In (xs ++ [(d, d, 1)])
(all_paths_klength elements m (List.length xs) c d).
Proof.
induction xs as [|h ys IH]; intros m c d Hsrc Htgt Hwf.
- (* xs = []: path is [(d, d, 1)] *)
simpl in Hsrc. unfold source in Hsrc.
destruct (fin_eq_dec c d) as [Heq | Hneq]; [| discriminate Hsrc].
subst c.
simpl. destruct (fin_eq_dec d d) as [_ | Hc]; [| exfalso; apply Hc; reflexivity].
left. reflexivity.
- (* xs = h :: ys: path = h :: (ys ++ [(d, d, 1)]) *)
destruct h as [[c' x] v].
simpl in Hsrc. unfold source in Hsrc. simpl in Hsrc.
destruct (fin_eq_dec c c') as [Heq | Hneq]; [| discriminate Hsrc].
subst c'.
(** path = (c, x, v) :: (ys ++ [(d, d, 1)]), use definitional equality *)
set (tail := ys ++ [(d, d, 1)]).
assert (Htail_eq : ((c, x, v) :: ys) ++ [(d, d, 1)] = (c, x, v) :: tail).
{ cbn. reflexivity. }
rewrite Htail_eq.
simpl.
(** Match the S (length ys) case: append_node_in_paths m c (flat_map ...) *)
apply (In_append_node_in_paths_rev
(List.flat_map (fun z => all_paths_klength elements m (length ys) z d) elements)
m c ((c, x, v) :: tail)).
+ (* source c ((c, x, v) :: tail) = true *)
unfold source. simpl. destruct (fin_eq_dec c c) as [_|Hc]; [reflexivity | exfalso; apply Hc; reflexivity].
+ (* tl of the path is non-empty: tail ≠ [] *)
unfold tail. intro H. cbn in H.
eapply app_eq_nil in H.
destruct H as (Ha & Hb).
congruence.
+ (* well_formed_path_aux holds *)
rewrite <- Htail_eq. exact Hwf.
+ (* tail ∈ flat_map *)
apply in_flat_map. exists x.
split.
* apply elements_complete.
* (* tail = ys ++ [(d, d, 1)] is in all_paths_klength ... x d *)
unfold tail. unfold well_formed_path_aux in Hwf. cbn in Hwf.
destruct Hwf as [Hmv Hwf_tail].
destruct ys as [|h2 ys'].
{ (* ys = []: tail = [(d, d, 1)] *)
destruct Hwf_tail as [Heq_x _]. subst x.
simpl. destruct (fin_eq_dec d d) as [_|Hc]; [|exfalso; apply Hc; reflexivity].
left. reflexivity. }
{ (* ys = h2 :: ys' *)
destruct h2 as [[au av] aw].
destruct Hwf_tail as [Heq_x Hwf_rest]. subst x.
cbn. eapply IH.
- unfold source. simpl. destruct (fin_eq_dec au au) as [_|Hc]; [reflexivity | exfalso; apply Hc; reflexivity].
- rewrite Htail_eq in Htgt. simpl in Htgt. exact Htgt.
- exact Hwf_rest. }
Qed.
(** The measure of a concatenated path equals the product of the
measures of its parts: [measure (l₁ ++ l₂) = measure l₁ * measure l₂]. *)
Lemma measure_of_path_app {R : Semiring.type} :
forall (l l₁ l₂ : list (Node * Node * R)), l = (l₁ ++ l₂) ->
measure_of_path l = measure_of_path l₁ * measure_of_path l₂.
Proof.
intros * ha. subst.
induction l₁ as [|((la, lb), lp) lh ihn].
+ cbn. rewrite mul1r. reflexivity.
+ cbn. rewrite ihn. rewrite mulA. reflexivity.
Qed.
(** Factoring a scalar out of a mapped fold: [Σ w·f(y) = w·Σ f(y)]. *)
Lemma fold_map_factor {R : Semiring.type} :
forall (l : list (list (Node * Node * R))) w,
fold_right (fun a b => a + b) 0
(map (fun y => w * measure_of_path y) l) =
w * fold_right (fun a b => a + b) 0
(map measure_of_path l).
Proof.
induction l as [|lh lt ihn]; intro w.
+ cbn. setoid_rewrite mulr0. reflexivity.
+ cbn. setoid_rewrite mulDl.
f_equal. eapply ihn.
Qed.
(** When every path in [l] starts at [a], prepending the edge
[(c, a, m c a)] to each path multiplies each measure by [m c a]. *)
Lemma map_measure_append_node {R : Semiring.type} :
forall (l : list (list (Node * Node * R)))
(m : @Matrix R) (c a : Node),
(forall xs, List.In xs l -> xs <> [] /\ source a xs = true) ->
(map measure_of_path (append_node_in_paths m c l)) =
(map (fun y => m c a * measure_of_path y) l).
Proof.
induction l as [|h t IH]; intros m c a Hprop.
- reflexivity.
- pose proof (Hprop h (or_introl eq_refl)) as (Hh_ne & Hh_src).
destruct h as [|h0 ht]; [exfalso; apply Hh_ne; reflexivity | ].
destruct h0 as [[x y] r].
(** From source a h = true, we get x = a *)
unfold source in Hh_src. simpl in Hh_src.
destruct (fin_eq_dec a x) as [Heq_ax | Hneq]; [| discriminate Hh_src].
subst x.
(** Now h = (a, y, r) :: ht *)
cbn [append_node_in_paths map measure_of_path].
f_equal.
apply IH. intros xs Hin. apply Hprop. right. exact Hin.
Qed.
(** Specialization of [map_measure_append_node] to paths from
[all_paths_klength]. *)
Lemma map_measure_append_node_kpaths {R : Semiring.type} :
forall (n : nat) (m : @Matrix R) c d a,
(map measure_of_path
(append_node_in_paths m c (all_paths_klength elements m n a d))) =
(map (fun y => m c a * measure_of_path y)
(all_paths_klength elements m n a d)).
Proof.
intros *.
eapply map_measure_append_node.
intros * ha.
eapply non_empty_paths_in_kpath in ha.
firstorder.
Qed.
(** The sum of measures through [append_node_in_paths] distributes
over list concatenation: [Σ_{l₁++l₂} = Σ_{l₁} + Σ_{l₂}]. *)
Lemma fold_measure_append_node_app {R : Semiring.type} :
forall (l₁ l₂ : list (list (Node * Node * R)))
(m : @Matrix R) (c : Node),
fold_right (λ u v : R, u + v) 0
(map measure_of_path (append_node_in_paths m c (l₁ ++ l₂)))
=
fold_right (λ u v : R, u + v) 0
(map measure_of_path
(append_node_in_paths m c l₁ ++
append_node_in_paths m c l₂)).
Proof.
induction l₁ as [|la lt ihl]; intros *.
+ cbn. reflexivity.
+ cbn. destruct la as [|((pa, pb), pc) laa].
++ eapply ihl.
++ cbn. rewrite ihl. reflexivity.
Qed.
(** Factoring [m c a] out of the sum of measures of paths from
[append_node_in_paths] specialized to [all_paths_klength]. *)
Lemma fold_measure_append_node_kpaths {R : Semiring.type} :
forall n (m : @Matrix R) c a d,
fold_right (λ u₁ v₁ : R, u₁ + v₁) 0
(map measure_of_path
(append_node_in_paths m c (all_paths_klength elements m n a d)))
=
m c a * fold_right (λ b v : R, b + v) 0
(map measure_of_path (all_paths_klength elements m n a d)).
Proof.
intros *.
rewrite map_measure_append_node_kpaths.
setoid_rewrite fold_map_factor.
reflexivity.
Qed.
(** If a concatenated path [ll ++ lr] is well-formed, then both
[ll] and [lr] are individually well-formed. *)
Lemma well_formed_path_snoc {R : Semiring.type} :
forall ll lr (m : @Matrix R),
well_formed_path_aux m (ll ++ lr)->
well_formed_path_aux m ll ∧
well_formed_path_aux m lr .
Proof.
induction ll as [|((c, x), v) ll' IH]; intros lr m Hwf.
- (* ll = [] *)
cbn in Hwf. split; [exact I | exact Hwf].
- (* ll = (c, x, v) :: ll' *)
cbn [app] in Hwf.
(** Hwf : well_formed_path_aux m ((c, x, v) :: ll' ++ lr) *)
cbn [well_formed_path_aux] in Hwf.
destruct Hwf as [Hmv Hconn].
(** Hmv : m c x = v *)
(** Hconn : match (ll' ++ lr) with
| [] => True
| (y, _, _) :: _ => (x = y) ∧ well_formed_path_aux m (ll' ++ lr)
end *)
destruct (ll' ++ lr) as [|h t] eqn:Happ.
+ (* ll' ++ lr = [] → ll' = [] ∧ lr = [] *)
apply app_eq_nil in Happ. destruct Happ as [Hll' Hlr].
subst ll' lr.
cbn. split; [| exact I].
cbn [well_formed_path_aux]. split; [exact Hmv | exact I].
+ (* ll' ++ lr = h :: t, so ll' ++ lr is non-empty *)
destruct h as [[y z] w].
destruct Hconn as [Heq Hwf_tail].
(** Heq : x = y, Hwf_tail : well_formed_path_aux m ((y, z, w) :: t)
Rewrite using Happ to get the form the IH expects *)
rewrite <- Happ in Hwf_tail.
apply IH in Hwf_tail.
destruct Hwf_tail as [Hwf_ll' Hwf_lr].
split; [| exact Hwf_lr].
(** prove well_formed_path_aux m ((c, x, v) :: ll') *)
cbn [well_formed_path_aux]. split; [exact Hmv |].
destruct ll' as [|h2 ll''].
* (* ll' = []: no connection condition needed *)
exact I.
* (* ll' = h2 :: ll'' *)
destruct h2 as [[y2 z2] w2].
cbn. split.
-- (* x = y2 *)
rewrite Heq.
simpl in Happ.
inversion Happ. subst. reflexivity.
-- (* well_formed_path_aux m ll' *)
exact Hwf_ll'.
Qed.
(** Splitting a path at a chosen node returns the collected prefix and the remainder. *)
Lemma keep_collecting_dropping_dual {R : Semiring.type} :
forall (l : list (Node * Node * R)) au,
l = (keep_collecting au l ++ keep_dropping au l).
Proof.
induction l as [|((la, lb), lc) lt ihl]; intros *.
+reflexivity.
+cbn. destruct (fin_eq_dec au lb) as [ha | ha].
++reflexivity.
++cbn. f_equal. now setoid_rewrite <-ihl.
Qed.
(** If [elem_path_triple_tail av l] holds, then [l] decomposes as
[ll ++ [(au, av, aw)] ++ lr] where [(au, av, aw)] is the first
element whose second component matches [av]. The prefix [ll]
contains no such element, and [keep_collecting av l] returns
exactly [ll ++ [(au, av, aw)]]. *)
Lemma elem_path_triple_tail_true {R : Semiring.type} :
forall (l : list (Node * Node * R)) av,
elem_path_triple_tail av l = true ->
exists ll au aw lr,
l = (ll ++ [(au, av, aw)] ++ lr) /\
elem_path_triple_tail av ll = false /\
(ll ++ [(au, av, aw)]) = (keep_collecting av l).
Proof.
induction l as [|((bu, bv), bw) t IH]; intros av Htrue.
- (* l = [] *)
cbn in Htrue. discriminate Htrue.
- (* l = (bu, bv, bw) :: t *)
cbn [elem_path_triple_tail] in Htrue.
destruct (fin_eq_dec av bv) as [Heq_av_bv | Hneq_av_bv].
+ (* av = bv: the current element is the first match *)
subst bv.
exists [], bu, bw, t.
cbn [app elem_path_triple_tail keep_collecting].
destruct (fin_eq_dec av av) as [_ | Hc]; [| exfalso; apply Hc; reflexivity].
split; [reflexivity | split; [reflexivity | reflexivity]].
+ (* av ≠ bv: the first match is in t *)
apply IH in Htrue.
destruct Htrue as (ll' & au & aw & lr' & Hl & Hfalse & Hcoll).
exists ((bu, bv, bw) :: ll'), au, aw, lr'.
cbn [app elem_path_triple_tail keep_collecting].
destruct (fin_eq_dec av bv) as [Heq' | _]; [exfalso; apply Hneq_av_bv; exact Heq' | ].
split.
* (* equality of lists *)
rewrite Hl. reflexivity.
* split.
-- (* elem_path_triple_tail on the extended prefix *)
exact Hfalse.
-- (* keep_collecting equality *)
rewrite Hcoll. reflexivity.
Qed.
(** If [elem_path_triple_tail au l] holds, then [keep_collecting au l]
is non-empty (it contains at least the matching element). *)
Lemma keep_collecting_nonempty {R : Semiring.type} :
forall (l : list (Node * Node * R)) au,
elem_path_triple_tail au l = true ->
keep_collecting au l <> [].
Proof.
intros l au H.
apply elem_path_triple_tail_true in H.
destruct H as (ll & au' & aw' & lr & _ & _ & Hcoll).
rewrite <- Hcoll. intro Hnil.
apply app_eq_nil in Hnil. destruct Hnil as [_ Hnil'].
inversion Hnil'.
Qed.
(** If [elem_path_triple_tail au l] holds, then the last element of
[keep_collecting au l] has [au] as its second component, so
[target au (keep_collecting au l)] holds. *)
Lemma keep_collecting_target {R : Semiring.type} :
forall (l : list (Node * Node * R)) au,
elem_path_triple_tail au l = true ->
target au (keep_collecting au l) = true.
Proof.
intros l au H.
apply elem_path_triple_tail_true in H.
destruct H as (ll & au' & aw' & lr & _ & _ & Hcoll).
rewrite <- Hcoll.
rewrite target_end. cbn.
destruct (fin_eq_dec au au) as [_ | Hc]; [reflexivity | exfalso; apply Hc; reflexivity].
Qed.
(** When the tail of a cons is non-empty, [target d (h :: t)] reduces
to [target d t] — the head's second component is skipped. *)
Lemma target_cons_nonempty_tail {R : Semiring.type} :
forall (h : Node * Node * R) (t : list (Node * Node * R)) (d : Node),
t <> [] -> target d (h :: t) = target d t.
Proof.
intros h t d Hne.
destruct h as [[x y] r].
cbn. destruct t as [|h2 t2]; [exfalso; apply Hne; reflexivity | ].
reflexivity.
Qed.
(** If [elem_path_triple_compute_loop l] returns [Some lc], then
[lc] is of the form [(au, av, aw) :: lcc] and is a cyclic path
starting and ending at [au]. *)
Lemma compute_loop_cycle {R : Semiring.type} :
forall (l : list (Node * Node * R)) lc,
Some lc = elem_path_triple_compute_loop l ->
exists au av aw lcc, Some ((au, av, aw) :: lcc) = Some lc /\
cyclic_path au lc.
Proof.
induction l as [|((au, av), aw) t IH]; intros lc H.
- (* l = [] *)
cbn in H. discriminate H.
- (* l = (au, av, aw) :: t *)
cbn [elem_path_triple_compute_loop] in H.
destruct (fin_eq_dec au av) as [Heq_au_av | Hneq_au_av].
+ (* au = av: self-loop [(au, av, aw)] *)
inversion H. subst lc.
exists au, av, aw, [].
split; [reflexivity | ].
unfold cyclic_path.
split; [intro Hnil; inversion Hnil | ].
split.
* unfold source. cbn.
destruct (fin_eq_dec au au) as [_ | Hc]; [reflexivity | exfalso; apply Hc; reflexivity].
* unfold target. cbn.
destruct (fin_eq_dec au av) as [_ | Hc]; [reflexivity | exfalso; apply Hc; exact Heq_au_av].
+ (* au ≠ av: check tail for a match *)
destruct (elem_path_triple_tail au t) eqn:Htail.
* (* match found in tail *)
inversion H. subst lc.
exists au, av, aw, (keep_collecting au t).
split; [reflexivity | ].
unfold cyclic_path.
split; [intro Hnil; inversion Hnil | ].
split.
-- unfold source. cbn.
destruct (fin_eq_dec au au) as [_ | Hc]; [reflexivity | exfalso; apply Hc; reflexivity].
-- assert (Hkeep_ne : keep_collecting au t <> [])
by (eapply keep_collecting_nonempty; eauto).
rewrite (target_cons_nonempty_tail (au, av, aw) (keep_collecting au t) au Hkeep_ne).
apply keep_collecting_target. exact Htail.
* (* no match in tail: recurse *)
apply IH in H.
destruct H as (au' & av' & aw' & lcc' & Hsome & Hcyclic).
exists au', av', aw', lcc'. split; [exact Hsome | exact Hcyclic].
Qed.
(** If no element in [l] has second component [au], and [l = ll ++ lr],
then neither [ll] nor [lr] contains such an element either. *)
Lemma elem_path_triple_tail_false {R : Semiring.type} :
forall (l : list (Node * Node * R)) ll lr au,
elem_path_triple_tail au l = false -> l = ll ++ lr ->
elem_path_triple_tail au ll = false /\
elem_path_triple_tail au lr = false.
Proof.
intros l ll lr au Hfalse Heq. subst l.
revert lr Hfalse.
induction ll as [|((bu, bv), bw) ll' IH]; intros lr Hfalse.