forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcsScript.sml
1261 lines (1130 loc) · 43.6 KB
/
lcsScript.sml
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
(*
Verification of longest common subsequence algorithms.
*)
open preamble;
val _ = new_theory "lcs";
(* Miscellaneous lemmas that may belong elsewhere *)
val sub_suc_0 = Q.prove(`x - SUC x = 0`,
Induct_on `x` >> fs[SUB]);
val take_suc_length = Q.prove(`TAKE (SUC (LENGTH l)) l = l`,
Induct_on `l` >> fs[])
val is_suffix_length = Q.prove(`IS_SUFFIX l1 l2 ==> LENGTH l1 >= LENGTH l2`,
rpt strip_tac
>> first_assum(assume_tac o MATCH_MP IS_SUFFIX_IS_SUBLIST)
>> fs[IS_SUBLIST_APPEND]);
val is_suffix_take = Q.prove(`
IS_SUFFIX l (h::r) ==>
(TAKE (LENGTH l − LENGTH r) l = TAKE (LENGTH l − SUC(LENGTH r)) l ++ [h])`,
fs[IS_SUFFIX_APPEND]
>> rpt strip_tac
>> fs[]
>> fs[TAKE_APPEND, GSYM ADD1, take_suc_length, sub_suc_0]);
val is_suffix_drop = Q.prove(`
IS_SUFFIX l (h::r) ==> (DROP (LENGTH l − SUC(LENGTH r)) l = h::r)`,
fs[IS_SUFFIX_APPEND]
>> rpt strip_tac
>> fs[]
>> fs[GSYM ADD1]
>> PURE_REWRITE_TAC [GSYM APPEND_ASSOC, DROP_LENGTH_APPEND]
>> fs[]);
val suc_ge = Q.prove(`(SUC x >= SUC y) = (x >= y)`, fs[]);
val take_singleton_one = Q.prove(`
(TAKE n r = [e]) ==> (TAKE 1 r = [e])`,
Cases_on `r` >> Cases_on `n` >> fs[]);
val sub_le_suc = Q.prove(`n ≥ SUC m ==> (n + SUC l − SUC m = n + l − m)`, fs[]);
val if_length_lemma = Q.prove(`TAKE (if LENGTH r <= 1 then 1 else LENGTH r) r = r`,
Induct_on `r` >> rw[] >> Cases_on `r` >> fs[]);
(* The predicate
lcs l1 l2 l3
is true iff l1 is the longest common subsequence of l2 and l3. *)
val is_subsequence_def =
Define `(is_subsequence [] l = T) ∧
(is_subsequence (f::r) l =
case l of
| [] => F
| f'::r' =>
(((f = f') /\ is_subsequence r r') \/ is_subsequence (f::r) r'))`
val common_subsequence_def =
Define `common_subsequence s t u =
(is_subsequence s t ∧ is_subsequence s u)`
val lcs_def =
Define `lcs s t u =
(common_subsequence s t u ∧
(∀s'. common_subsequence s' t u ⇒ LENGTH s' <= LENGTH s))`
(* Properties of lcs and its auxiliary functions *)
Theorem is_subsequence_nil:
(is_subsequence l [] = (l = [])) /\ (is_subsequence [] l = T)
Proof
Induct_on `l` >> fs[is_subsequence_def]
QED
Theorem is_subsequence_cons:
is_subsequence (f::r) (h::r') =
(((f = h) /\ is_subsequence r r') \/ is_subsequence (f::r) r')
Proof
fs[Once is_subsequence_def]
QED
Theorem is_subsequence_single:
is_subsequence s [h] = ((s = [h]) \/ (s = []))
Proof
Cases_on `s`
>> fs[is_subsequence_nil,is_subsequence_cons]
QED
Theorem is_subsequence_refl:
is_subsequence l l = T
Proof
Induct_on `l` >> fs[is_subsequence_nil,is_subsequence_cons]
QED
Theorem prefix_is_subsequence:
! s l s'.
(is_subsequence (s ++ s') l ==> is_subsequence s l)
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>- fs[is_subsequence_nil,is_subsequence_cons]
>> Cases_on `l`
>- fs[is_subsequence_nil]
>> fs[is_subsequence_cons]
>> rw[]
>> metis_tac[]
QED
Theorem suffix_is_subsequence:
! s l s'.
(is_subsequence (f::s) l ==> is_subsequence s l)
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>- fs[is_subsequence_nil,is_subsequence_cons]
>> Cases_on `l`
>- fs[is_subsequence_nil]
>> fs[is_subsequence_cons]
QED
Theorem suffix_is_subsequence':
!s l. is_subsequence (s' ++ s) l ==> is_subsequence s l
Proof
Induct_on `s'`
>> fs[] >> metis_tac[suffix_is_subsequence]
QED
Theorem cons_is_subsequence:
is_subsequence s l ==> is_subsequence s (f::l)
Proof
Induct_on `s`
>> rw[is_subsequence_cons,is_subsequence_nil]
QED
Theorem is_subsequence_snoc:
!s l f. is_subsequence (s ++ [f]) (l ++ [f]) = is_subsequence s l
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>- (Induct_on `l` >> fs[is_subsequence_nil,is_subsequence_cons])
>> fs[is_subsequence_nil,is_subsequence_cons]
>> Cases_on `l`
>> rfs[is_subsequence_nil,is_subsequence_cons] >> metis_tac[]
QED
Theorem is_subsequence_snoc':
!r r'. is_subsequence (r ++ [f]) (r' ++ [h]) =
(((f = h) /\ is_subsequence r r') \/ is_subsequence (r ++ [f]) r')
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>> fs[is_subsequence_cons,is_subsequence_nil]
>> Induct_on `r'` >> rpt strip_tac >> fs[is_subsequence_nil,is_subsequence_cons]
>> metis_tac[]
QED
Theorem snoc_is_subsequence:
!s l f. is_subsequence s l ==> is_subsequence s (l++[f])
Proof
ho_match_mp_tac SNOC_INDUCT
>> rw[is_subsequence_snoc',is_subsequence_nil,SNOC_APPEND]
QED
Theorem is_subsequence_appendr:
!l' s l. is_subsequence s l ==> is_subsequence s (l++l')
Proof
Induct
>> rpt strip_tac >> fs[]
>> drule snoc_is_subsequence
>> disch_then(qspec_then `h` assume_tac)
>> first_x_assum drule
>> FULL_SIMP_TAC bool_ss [GSYM APPEND_ASSOC,GSYM CONS_APPEND]
QED
Theorem is_subsequence_appendl:
!l' s l. is_subsequence s l ==> is_subsequence s (l'++l)
Proof
Induct
>> rpt strip_tac >> fs[]
>> match_mp_tac cons_is_subsequence >> metis_tac[]
QED
Theorem is_subsequence_append:
!l l' r r'. is_subsequence l l' /\ is_subsequence r r' ==> is_subsequence(l++r) (l'++r')
Proof
ho_match_mp_tac (fetch "lcs" "is_subsequence_ind")
>> rpt strip_tac
>- (fs[is_subsequence_def] >> metis_tac[is_subsequence_appendl])
>> Cases_on `l'`
>- fs[is_subsequence_def]
>> fs[is_subsequence_cons]
QED
Theorem is_subsequence_length:
!l l'. is_subsequence l l' ==> LENGTH l <= LENGTH l'
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>- fs[is_subsequence_nil]
>> Cases_on `l'`
>- fs[is_subsequence_nil]
>> fs[is_subsequence_cons]
>> metis_tac [suffix_is_subsequence]
QED
Theorem is_subsequence_cons':
!s l f. is_subsequence s (f::l)
==> ((((s = []) \/ f ≠ HD s) /\ is_subsequence s l)
\/ (((s ≠ []) /\ (f = HD s)) /\ is_subsequence (TL s) l))
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>- fs[is_subsequence_nil]
>> Cases_on `l`
>- fs[is_subsequence_nil, Once is_subsequence_def]
>- (Cases_on `f' = f`
>> fs[is_subsequence_cons] >> rfs[]
>> metis_tac [cons_is_subsequence])
QED
Theorem is_subsequence_snoc'':
!s l f. is_subsequence s (l ++ [f])
==> ((((s = []) \/ f ≠ LAST s) /\ is_subsequence s l)
\/ (((s ≠ []) /\ (f = LAST s)) /\ is_subsequence (FRONT s) l))
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>- fs[is_subsequence_nil]
>> Cases_on `l`
>- fs[is_subsequence_nil, Once is_subsequence_def]
>- (Cases_on `f' = f`
>> fs[is_subsequence_snoc'] >> fs[is_subsequence_cons]
>> rpt(first_x_assum(ASSUME_TAC o Q.SPEC `f'`))
>> rfs[is_subsequence_nil] >> Cases_on `s` >> fs[is_subsequence_nil,is_subsequence_cons])
QED
Theorem common_subsequence_append:
common_subsequence a b c /\ common_subsequence a' b' c' ==> common_subsequence(a++a') (b++b') (c++c')
Proof
fs[common_subsequence_def,is_subsequence_append]
QED
Theorem common_subsequence_sym:
common_subsequence s u t = common_subsequence s t u
Proof
fs[common_subsequence_def,EQ_IMP_THM]
QED
Theorem lcs_refl:
lcs l l l
Proof
fs[lcs_def,common_subsequence_def,is_subsequence_refl,is_subsequence_length]
QED
val is_subsequence_greater = Q.prove(
`!l' l. is_subsequence l' l /\ LENGTH l ≤ LENGTH l'
==> l = l'`,
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>- fs[quantHeuristicsTheory.LIST_LENGTH_0]
>> Cases_on `l`
>> fs[is_subsequence_cons,is_subsequence_nil]
>> rfs[]);
Theorem lcs_refl':
lcs l' l l = (l = l')
Proof
fs[lcs_def,common_subsequence_def,EQ_IMP_THM,is_subsequence_refl,is_subsequence_length]
>> rpt strip_tac
>> first_x_assum(assume_tac o Q.SPEC `l`)
>> fs[is_subsequence_refl,is_subsequence_greater]
QED
Theorem lcs_sym:
lcs l l' l'' = lcs l l'' l'
Proof
metis_tac[lcs_def,common_subsequence_sym]
QED
val lcs_empty = Q.prove(`lcs [] l [] /\ lcs [] [] l`,
fs[lcs_def,common_subsequence_def,is_subsequence_nil]);
Theorem lcs_empty':
(lcs l l' [] = (l = [])) /\ (lcs l [] l' = (l = []))
Proof
fs[lcs_def,common_subsequence_def,is_subsequence_nil,EQ_IMP_THM]
QED
Theorem common_subsequence_empty':
(common_subsequence l l' [] = (l = [])) /\ (common_subsequence l [] l' = (l = []))
Proof
fs[common_subsequence_def,is_subsequence_nil,EQ_IMP_THM]
QED
Theorem cons_lcs_optimal_substructure:
lcs (f::l) (f::l') (f::l'') = lcs l l' l''
Proof
fs[lcs_def,common_subsequence_def, Once is_subsequence_def, EQ_IMP_THM]
>> rpt strip_tac
>> first_assum(ASSUME_TAC o Q.SPEC `f::s'`)
>> fs[is_subsequence_cons]
>> TRY(metis_tac[suffix_is_subsequence])
>> rpt(first_x_assum(assume_tac o MATCH_MP is_subsequence_cons'))
>> fs[]
>- metis_tac[cons_is_subsequence, LESS_EQ_SUC_REFL, LESS_EQ_TRANS]
>- (`LENGTH(TL s') ≤ LENGTH l` by metis_tac[cons_is_subsequence, LESS_EQ_SUC_REFL, LESS_EQ_TRANS]
>> Cases_on `s'`
>> fs[])
QED
Theorem cons_common_subsequence:
common_subsequence (f::l) (f::l') (f::l'') = common_subsequence l l' l''
Proof
fs[common_subsequence_def, Once is_subsequence_def, EQ_IMP_THM]
>> rpt strip_tac
>> fs[is_subsequence_cons]
>> metis_tac[suffix_is_subsequence]
QED
Theorem snoc_lcs_optimal_substructure:
lcs (l ++ [f]) (l' ++ [f]) (l'' ++ [f]) = lcs l l' l''
Proof
fs[lcs_def,common_subsequence_def, Once is_subsequence_def, EQ_IMP_THM]
>> rpt strip_tac
>> first_assum(ASSUME_TAC o Q.SPEC `s' ++ [f]`)
>> rpt(first_x_assum(assume_tac o MATCH_MP is_subsequence_snoc''))
>> fs[is_subsequence_snoc,FRONT_APPEND]
>> TRY(metis_tac[prefix_is_subsequence])
>- (`LENGTH s' ≤ LENGTH l` by fs[] >> fs[])
>- (`LENGTH(FRONT s') ≤ LENGTH l` by fs[] >> Cases_on `s'` >> fs[])
QED
Theorem cons_lcs_optimal_substructure_left:
f ≠ f' /\ lcs l (f::l') l''
/\ lcs l''' l' (f'::l'')
/\ LENGTH l >= LENGTH l'''
==> lcs l (f::l') (f'::l'')
Proof
fs[lcs_def,common_subsequence_def, Once is_subsequence_def, EQ_IMP_THM]
>> rpt strip_tac
>- metis_tac[cons_is_subsequence]
>> PAT_ASSUM ``is_subsequence s' (f'::l'')`` (assume_tac o MATCH_MP is_subsequence_cons')
>> PAT_ASSUM ``is_subsequence s' (f::l')`` (assume_tac o MATCH_MP is_subsequence_cons')
>> fs[is_subsequence_cons]
>> Cases_on `s'`
>> fs[is_subsequence_cons]
>> `LENGTH(h::t) ≤ LENGTH l'''` by(first_assum match_mp_tac >> fs[is_subsequence_cons])
>> fs[]
QED
Theorem snoc_lcs_optimal_substructure_left:
f ≠ f' /\ lcs l (l' ++ [f]) l''
/\ lcs l''' l' (l''++[f'])
/\ LENGTH l >= LENGTH l'''
==> lcs l (l'++[f]) (l''++[f'])
Proof
fs[lcs_def,common_subsequence_def, is_subsequence_snoc', EQ_IMP_THM]
>> rpt strip_tac
>- metis_tac[snoc_is_subsequence]
>> PAT_ASSUM ``is_subsequence s' (l''++[f'])`` (assume_tac o MATCH_MP is_subsequence_snoc'')
>> PAT_ASSUM ``is_subsequence s' (l'++[f])`` (assume_tac o MATCH_MP is_subsequence_snoc'')
>> fs[is_subsequence_cons]
>> FULL_STRUCT_CASES_TAC (Q.SPEC `s'` SNOC_CASES)
>> fs[is_subsequence_snoc',SNOC_APPEND]
>> `LENGTH(l''''++[x]) ≤ LENGTH l'''` by(first_assum match_mp_tac >> fs[is_subsequence_snoc'])
>> fs[]
QED
Theorem cons_lcs_optimal_substructure_right:
f ≠ f' /\ lcs l (f::l') l''
/\ lcs l''' l' (f'::l'')
/\ LENGTH l''' >= LENGTH l
==> lcs l''' (f::l') (f'::l'')
Proof
metis_tac[cons_lcs_optimal_substructure_left,lcs_sym]
QED
Theorem snoc_lcs_optimal_substructure_right:
f ≠ f' /\ lcs l (l'++[f]) l''
/\ lcs l''' l' (l''++[f'])
/\ LENGTH l''' >= LENGTH l
==> lcs l''' (l'++[f]) (l''++[f'])
Proof
metis_tac[snoc_lcs_optimal_substructure_left,lcs_sym]
QED
Theorem lcs_length_left:
(lcs xl yl zl /\ lcs xl' (yl ++ [y]) zl)
==> SUC(LENGTH xl) >= LENGTH xl'
Proof
fs[lcs_def,common_subsequence_def] >> rpt strip_tac
>> first_assum(assume_tac o MATCH_MP is_subsequence_snoc'')
>> fs[]
>- (`LENGTH xl' <= LENGTH xl` by metis_tac[] >> fs[])
>> FULL_STRUCT_CASES_TAC (Q.SPEC `xl'` SNOC_CASES)
>> fs[SNOC_APPEND]
>> rpt(first_x_assum(assume_tac o MATCH_MP prefix_is_subsequence))
>> `LENGTH l <= LENGTH xl` by metis_tac[] >> fs[]
QED
Theorem lcs_length_right:
(lcs xl yl zl /\ lcs xl' (yl) (zl ++ [z]))
==> SUC(LENGTH xl) >= LENGTH xl'
Proof
metis_tac[lcs_sym,lcs_length_left]
QED
Theorem lcs_length:
!l l' r r'. lcs l r r' /\ lcs l' r r' ==> LENGTH l = LENGTH l'
Proof
rpt strip_tac >> fs[lcs_def]
>> metis_tac[EQ_LESS_EQ]
QED
Theorem is_subsequence_rev:
!l r. is_subsequence (REVERSE l) (REVERSE r) = is_subsequence l r
Proof
ho_match_mp_tac (theorem "is_subsequence_ind")
>> rpt strip_tac
>> fs[is_subsequence_nil]
>> Cases_on `r`
>> fs[is_subsequence_nil,is_subsequence_snoc',is_subsequence_cons]
QED
Theorem is_subsequence_rev':
!l r. is_subsequence l (REVERSE r) = is_subsequence (REVERSE l) r
Proof
ho_match_mp_tac SNOC_INDUCT
>> strip_tac
>- fs[is_subsequence_nil]
>> rpt strip_tac
>> Induct_on `r`
>> fs[is_subsequence_nil,is_subsequence_cons,is_subsequence_snoc',SNOC_APPEND,REVERSE_APPEND]
QED
Theorem common_subsequence_rev:
!l r s. common_subsequence (REVERSE l) (REVERSE r) (REVERSE s) = common_subsequence l r s
Proof
rw[common_subsequence_def,is_subsequence_rev]
QED
Theorem common_subsequence_rev':
!l r s. common_subsequence l (REVERSE r) (REVERSE s) = common_subsequence (REVERSE l) r s
Proof
rw[common_subsequence_def,is_subsequence_rev']
QED
Theorem lcs_rev:
!l r s. lcs (REVERSE l) (REVERSE r) (REVERSE s) = lcs l r s
Proof
rw[common_subsequence_rev',lcs_def,EQ_IMP_THM]
>> metis_tac[LENGTH_REVERSE,REVERSE_REVERSE]
QED
Theorem lcs_rev':
!l r s. lcs l (REVERSE r) (REVERSE s) = lcs (REVERSE l) r s
Proof
rw[common_subsequence_rev',lcs_def,EQ_IMP_THM]
>> metis_tac[LENGTH_REVERSE,REVERSE_REVERSE]
QED
Theorem lcs_drop_ineq:
(lcs (f::r) (h::l) l' /\ f ≠ h) ==> lcs (f::r) l l'
Proof
rpt strip_tac
>> fs[lcs_def,common_subsequence_def,Once is_subsequence_cons]
>> metis_tac[cons_is_subsequence]
QED
Theorem common_subsequence_drop_ineq:
(common_subsequence (f::r) (h::l) l' /\ f ≠ h) ==> common_subsequence (f::r) l l'
Proof
rpt strip_tac
>> fs[common_subsequence_def,Once is_subsequence_cons]
>> metis_tac[cons_is_subsequence]
QED
Theorem lcs_split:
lcs (f::r) l l' ==> ?ll lr. SPLITP ($= f) l = (ll,f::lr)
Proof
Induct_on `l`
>> rw[lcs_empty',SPLITP]
>> fs[SPLITP]
>> metis_tac[lcs_drop_ineq,SND]
QED
Theorem common_subsequence_split:
common_subsequence (f::r) l l' ==> ?ll lr. SPLITP ($= f) l = (ll,f::lr)
Proof
Induct_on `l`
>> rw[common_subsequence_empty',SPLITP]
>> fs[SPLITP]
>> metis_tac[common_subsequence_drop_ineq,SND]
QED
Theorem lcs_split2:
lcs (f::r) l l' ==> ?ll lr. SPLITP ($= f) l' = (ll,f::lr)
Proof
metis_tac[lcs_split,lcs_sym]
QED
Theorem common_subsequence_split2:
common_subsequence (f::r) l l' ==> ?ll lr. SPLITP ($= f) l' = (ll,f::lr)
Proof
metis_tac[common_subsequence_split,common_subsequence_sym]
QED
Theorem lcs_split_lcs:
lcs (f::r) l l' ==> lcs (f::r) (SND(SPLITP ($= f) l)) l'
Proof
Induct_on `l`
>> rw[lcs_empty',SPLITP]
>> metis_tac[lcs_drop_ineq,SND]
QED
Theorem common_subsequence_split_css:
common_subsequence (f::r) l l' ==> common_subsequence (f::r) (SND(SPLITP ($= f) l)) l'
Proof
Induct_on `l`
>> rw[common_subsequence_empty',SPLITP]
>> metis_tac[common_subsequence_drop_ineq,SND]
QED
Theorem lcs_split_lcs2:
lcs (f::r) l l' ==> lcs (f::r) l (SND(SPLITP ($= f) l'))
Proof
metis_tac[lcs_split_lcs,lcs_sym]
QED
Theorem common_subsequence_split_css2:
common_subsequence (f::r) l l' ==> common_subsequence (f::r) l (SND(SPLITP ($= f) l'))
Proof
metis_tac[common_subsequence_split_css,common_subsequence_sym]
QED
Theorem split_lcs_optimal_substructure:
lcs (f::r) l l' ==> lcs r (TL(SND(SPLITP ($= f) l))) (TL(SND(SPLITP ($= f) l')))
Proof
rpt strip_tac >>
drule lcs_split >> drule lcs_split2 >>
pop_assum (assume_tac o MATCH_MP lcs_split_lcs2 o MATCH_MP lcs_split_lcs)
>> rpt strip_tac
>> fs[cons_lcs_optimal_substructure]
QED
Theorem split_common_subsequence:
common_subsequence (f::r) l l' ==> common_subsequence r (TL(SND(SPLITP ($= f) l))) (TL(SND(SPLITP ($= f) l')))
Proof
rpt strip_tac >>
drule common_subsequence_split >> drule common_subsequence_split2 >>
pop_assum (assume_tac o MATCH_MP common_subsequence_split_css2 o MATCH_MP common_subsequence_split_css)
>> rpt strip_tac
>> fs[cons_common_subsequence]
QED
Theorem lcs_max_length:
!l t t'. lcs l t t' ==> 2 * LENGTH l <= LENGTH t + LENGTH t'
Proof
rpt strip_tac >> fs[lcs_def,common_subsequence_def]
>> drule is_subsequence_length >> qpat_x_assum `is_subsequence _ _` kall_tac
>> drule is_subsequence_length >> fs[]
QED
(* A naive, exponential-time LCS algorithm that's easy to verify *)
val longest_def =
Define `longest l l' = if LENGTH l >= LENGTH l' then l else l'`
val naive_lcs_def =
Define
`(naive_lcs l [] = []) ∧
(naive_lcs [] l = []) ∧
(naive_lcs (f::r) (f'::r') =
if f = f' then
f::naive_lcs r r'
else
longest(naive_lcs (f::r) r') (naive_lcs r (f'::r')))`
(* Properties of the naive lcs algorithm *)
Theorem longest_tail:
longest (l ++ [e]) (l' ++ [e]) = longest l l' ++ [e]
Proof
rw[longest_def,GSYM ADD1] >> fs[]
QED
Theorem longest_cons:
longest (e::l) (e::l') = e::longest l l'
Proof
rw[longest_def,GSYM ADD1] >> fs[]
QED
Theorem naive_lcs_clauses:
(naive_lcs l [] = []) ∧
(naive_lcs [] l = []) ∧
(naive_lcs (f::r) (f'::r') =
if f = f' then
f::naive_lcs r r'
else
longest(naive_lcs (f::r) r') (naive_lcs r (f'::r')))
Proof
Cases_on `l` >> fs[naive_lcs_def]
QED
Theorem naive_lcs_tail:
!prevh fullr h. naive_lcs (prevh ++ [h]) (fullr ++ [h]) = naive_lcs prevh fullr ++ [h]
Proof
ho_match_mp_tac (theorem "naive_lcs_ind")
>> rpt strip_tac
>- (fs[naive_lcs_clauses]
>> Induct_on `prevh`
>> rw[naive_lcs_clauses, longest_def])
>- (rw[naive_lcs_clauses,longest_def]
>> Induct_on `v3` (*TODO: generated name *)
>> rw[naive_lcs_clauses,longest_def])
>> rw[naive_lcs_clauses]
>> fs[longest_tail]
QED
Theorem naive_lcs_length_bound:
!l l'. LENGTH (naive_lcs l l') <= MIN (LENGTH l) (LENGTH l')
Proof
ho_match_mp_tac (theorem "naive_lcs_ind")
>> rw[naive_lcs_clauses, MIN_DEF, longest_def]
QED
val naive_lcs_length = Q.prove(
`!l l' h. LENGTH(naive_lcs l l') + 1 >= LENGTH(naive_lcs l (l' ++ [h]))`,
ho_match_mp_tac (theorem "naive_lcs_ind")
>> rpt strip_tac
>> fs[naive_lcs_clauses]
>- (ASSUME_TAC(Q.SPECL [`l`,`[h]`] naive_lcs_length_bound)
>> fs[])
>> rw[longest_def] >> fs[GSYM ADD1, suc_ge]
>> rpt(first_x_assum(assume_tac o Q.SPEC `h`))
>> fs[]);
val naive_lcs_length' = Q.prove(
`!l l' h. LENGTH(naive_lcs l l') + 1 >= LENGTH(naive_lcs (l ++ [h]) l')`,
ho_match_mp_tac (theorem "naive_lcs_ind")
>> rpt strip_tac
>> fs[naive_lcs_clauses]
>> rw[]
>- (ASSUME_TAC(Q.SPECL [`[h]`,`v3`] naive_lcs_length_bound)
>> fs[longest_def])
>> rw[longest_def] >> fs[GSYM ADD1, suc_ge]
>> rpt(first_x_assum(assume_tac o Q.SPEC `h`))
>> fs[]);
(* Main correctness theorem for the naive lcs algorithm *)
Theorem naive_lcs_correct:
∀l l'. lcs (naive_lcs l l') l l'
Proof
ho_match_mp_tac (theorem "naive_lcs_ind")
>> rpt strip_tac
(* Base cases *)
>- fs[naive_lcs_def,lcs_def,common_subsequence_def,is_subsequence_nil]
>- fs[naive_lcs_def,lcs_def,common_subsequence_def,is_subsequence_nil]
(* Inductive step *)
>> Cases_on `f = f'`
>- fs[naive_lcs_def,cons_lcs_optimal_substructure]
>> rw[naive_lcs_def, longest_def]
>- metis_tac[cons_lcs_optimal_substructure_left]
>> `LENGTH (naive_lcs l (f'::l')) ≥ (LENGTH (naive_lcs (f::l) l'))` by fs[]
>> metis_tac[cons_lcs_optimal_substructure_right]
QED
(* A quadratic-time LCS algorithm in dynamic programming style *)
val longest'_def =
Define `longest' (l,n) (l',n') = if n:num >= n' then (l,n) else (l',n')`
val longest'_thm = Q.prove(
`!l l'. longest' l l' = if SND l >= SND l' then l else l'`,
Cases >> Cases >> fs[longest'_def]);
val dynamic_lcs_row_def = Define `
(dynamic_lcs_row h [] previous_col previous_row ddl = [])
∧ (dynamic_lcs_row h (f::r) previous_col previous_row (diagonal,dl) =
if f = h then
(let current = longest' (f::diagonal,dl+1) (longest' (HD previous_row) previous_col) in
current::dynamic_lcs_row h r current (TL previous_row) (HD previous_row))
else
(let current = longest' (HD previous_row) previous_col in
current::dynamic_lcs_row h r current (TL previous_row) (HD previous_row))
)`;
val dynamic_lcs_rows_def = Define `
(dynamic_lcs_rows [] r previous_row =
if previous_row = [] then [] else FST(LAST previous_row)) ∧
(dynamic_lcs_rows (h::l) r previous_row =
dynamic_lcs_rows l r (dynamic_lcs_row h r ([],0) previous_row ([],0)))
`;
val dynamic_lcs_def = Define `
dynamic_lcs l r = REVERSE(dynamic_lcs_rows l r (REPLICATE (LENGTH r) ([],0)))`;
val dynamic_lcs_no_rev_def = Define `
dynamic_lcs_no_rev l r = dynamic_lcs_rows l r (REPLICATE (LENGTH r) ([],0))`;
(* Verification of dynamic LCS algorithm *)
val dynamic_lcs_row_invariant_def = Define `
dynamic_lcs_row_invariant h r previous_col previous_row diagonal prevh fullr =
((LENGTH r = LENGTH previous_row) ∧ (IS_SUFFIX fullr r) ∧
(SND previous_col = LENGTH(FST previous_col)) ∧
(SND diagonal = LENGTH(FST diagonal)) ∧
(!n. 0 <= n /\ n < LENGTH previous_row ==> (lcs (REVERSE(FST((EL n previous_row)))) prevh (TAKE (SUC n + (LENGTH fullr - LENGTH r)) fullr))) ∧
(!n. 0 <= n /\ n < LENGTH previous_row ==> (SND(EL n previous_row) = LENGTH(FST(EL n previous_row)))) ∧
(lcs (REVERSE(FST diagonal)) prevh (TAKE (LENGTH fullr - LENGTH r) fullr)) ∧
(lcs (REVERSE(FST previous_col)) (SNOC h prevh) (TAKE (LENGTH fullr - LENGTH r) fullr)))`;
val dynamic_lcs_rows_invariant_def = Define `
dynamic_lcs_rows_invariant h r previous_row fullh =
((LENGTH r = LENGTH previous_row) ∧ (IS_SUFFIX fullh h) ∧
(!n. 0 <= n /\ n < LENGTH previous_row ==> (lcs (REVERSE(FST(EL n previous_row))) (TAKE (LENGTH fullh - LENGTH h) fullh) (TAKE (SUC n) r))) ∧
(!n. 0 <= n /\ n < LENGTH previous_row ==> (SND(EL n previous_row) = LENGTH(FST(EL n previous_row)))))`;
Theorem dynamic_lcs_row_invariant_pres1:
dynamic_lcs_row_invariant h (h::r) previous_col previous_row (diagonal,dl) prevh fullr
==> dynamic_lcs_row_invariant h r (longest' (h::diagonal,dl+1)
(longest' (HD previous_row) previous_col))
(TL previous_row) (HD previous_row) prevh fullr
Proof
Cases_on `previous_col`
>> rename1 `(previous_col,pcl)`
>> fs[dynamic_lcs_row_invariant_def]
>> rpt strip_tac
>- (Cases_on `previous_row` >> fs[])
>- metis_tac[IS_SUFFIX_CONS2_E]
>- (fs[longest'_thm] >> every_case_tac
>> fs[] >> first_x_assum(qspec_then `0` assume_tac)
>> rfs[])
>- (first_x_assum(qspec_then `0` assume_tac)
>> rfs[])
>- (last_x_assum(assume_tac o Q.SPEC `SUC n`)
>> Cases_on `previous_row` >> fs[]
>> first_x_assum(assume_tac o MATCH_MP is_suffix_length)
>> fs[ADD_CLAUSES,SUB_LEFT_SUC] >> every_case_tac
>> Cases_on `n`
>> TRY(`LENGTH fullr = SUC(LENGTH t)` by(fs[]>>NO_TAC))
>> fs[SUB,ADD_CLAUSES] >> rfs[])
>- (first_x_assum(qspec_then `SUC n` assume_tac)
>> Cases_on `previous_row` >> fs[])
>- (last_x_assum(assume_tac o Q.SPEC `0`)
>> first_x_assum(assume_tac o MATCH_MP is_suffix_length)
>> Cases_on `previous_row`
>> fs[ADD_CLAUSES,SUB_LEFT_SUC]
>> every_case_tac
>> TRY(`LENGTH fullr = SUC(LENGTH t)` by(fs[]>>NO_TAC))
>> fs[] >> rfs[])
>- (rw[longest'_thm]
>> PAT_ASSUM ``IS_SUFFIX fullr (h::r)`` (assume_tac o MATCH_MP is_suffix_take)
>> fs[SNOC_APPEND,REVERSE_SNOC,snoc_lcs_optimal_substructure] >> rfs[]
(* longest is from previous row *)
>- (last_x_assum (assume_tac o Q.SPEC `0`)
>> first_x_assum (assume_tac o Q.SPEC `0`)
>> rfs[] >> fs[Q.SPEC `1` ADD_SYM] >> fs[TAKE_SUM]
>> PAT_ASSUM ``IS_SUFFIX fullr (h::r)`` (assume_tac o MATCH_MP is_suffix_drop)
>> rfs[] >> fs[] >> metis_tac[ADD1,lcs_length_right,LENGTH_REVERSE])
(* longest is from previous column *)
>- metis_tac[ADD1,lcs_length_left,LENGTH_REVERSE])
QED
Theorem dynamic_lcs_row_invariant_pres2:
h ≠ f ∧ dynamic_lcs_row_invariant h (f::r) previous_col previous_row diagonal fullh fullr
==> dynamic_lcs_row_invariant h r (longest' (HD previous_row) previous_col) (TL previous_row)
(HD previous_row) fullh fullr
Proof
fs[dynamic_lcs_row_invariant_def]
>> rpt strip_tac
>- (Cases_on `previous_row` >> fs[])
>- metis_tac[IS_SUFFIX_CONS2_E]
>- (first_x_assum(qspec_then `0` assume_tac)
>> Cases_on `previous_row` >> fs[longest'_thm] >> rw[])
>- (first_x_assum(qspec_then `0` assume_tac) >> fs[])
>- (last_x_assum(assume_tac o Q.SPEC `SUC n`)
>> Cases_on `previous_row` >> fs[]
>> first_x_assum(assume_tac o MATCH_MP is_suffix_length)
>> fs[ADD_CLAUSES,SUB_LEFT_SUC] >> every_case_tac
>> Cases_on `n`
>> TRY(`LENGTH fullr = SUC(LENGTH t)` by(fs[]>>NO_TAC))
>> fs[SUB,ADD_CLAUSES] >> rfs[])
>- (first_x_assum(qspec_then `SUC n` assume_tac) >>
Cases_on `previous_row` >> rfs[])
>- (last_x_assum(assume_tac o Q.SPEC `0`)
>> first_x_assum(assume_tac o MATCH_MP is_suffix_length)
>> Cases_on `previous_row`
>> fs[ADD_CLAUSES,SUB_LEFT_SUC]
>> every_case_tac
>> TRY(`LENGTH fullr = SUC(LENGTH t)` by(fs[]>>NO_TAC))
>> fs[] >> rfs[])
>- (rw[longest'_thm]
>> PAT_ASSUM ``IS_SUFFIX fullr (f::r)`` (assume_tac o MATCH_MP is_suffix_take)
>> fs[SNOC_APPEND,snoc_lcs_optimal_substructure,REVERSE_SNOC] >> rfs[]
(* longest is from previous row *)
>- (MATCH_MP_TAC (Q.INST [`l`|->`REVERSE(FST (previous_col:'a list#num))`] snoc_lcs_optimal_substructure_right)
>> rpt strip_tac >> fs[]
>> last_x_assum (assume_tac o Q.SPEC `0`)
>> first_x_assum (assume_tac o Q.SPEC `0`)
>> rfs[] >> fs[Q.SPEC `1` ADD_SYM] >> fs[TAKE_SUM]
>> PAT_ASSUM ``IS_SUFFIX fullr (f::r)`` (assume_tac o MATCH_MP is_suffix_drop)
>> rfs[] >> fs[]) (*TODO: cleanup *)
(* longest is from previous column *)
>- (MATCH_MP_TAC (Q.INST [`l'''`|->`REVERSE(FST(HD (previous_row:('a list # num) list)))`] snoc_lcs_optimal_substructure_left)
>> rpt strip_tac >> fs[]
>> last_x_assum (assume_tac o Q.SPEC `0`)
>> first_x_assum (assume_tac o Q.SPEC `0`)
>> rfs[] >> fs[Q.SPEC `1` ADD_SYM] >> fs[TAKE_SUM]
>> PAT_ASSUM ``IS_SUFFIX fullr (f::r)`` (assume_tac o MATCH_MP is_suffix_drop)
>> rfs[] >> fs[]))
QED
Theorem dynamic_lcs_length:
!h r previous_col previous_row diagonal.
LENGTH(dynamic_lcs_row h r previous_col previous_row diagonal) = LENGTH r
Proof
Induct_on `r` >> Cases_on `diagonal` >> rw[dynamic_lcs_row_def]
QED
Theorem dynamic_lcs_row_invariant_pres:
!h r previous_col previous_row diagonal prevh fullr l n.
(dynamic_lcs_row_invariant h r previous_col previous_row diagonal prevh fullr
/\ (dynamic_lcs_row h r previous_col previous_row diagonal = l)
/\ (0 <= n) /\ (n < LENGTH l))
==> (lcs (REVERSE (FST(EL n l))) (prevh ++ [h]) (TAKE (SUC n + (LENGTH fullr - (LENGTH l))) fullr))
Proof
Induct_on `r`
>> rpt strip_tac
>> Cases_on `diagonal`
>> rename1 `(diagonal,dl)`
>- (fs[dynamic_lcs_row_def]
>> metis_tac[LENGTH,prim_recTheory.NOT_LESS_0])
>> `IS_SUFFIX fullr (h::r)` by fs[dynamic_lcs_row_invariant_def]
>> first_assum(assume_tac o MATCH_MP is_suffix_length)
>> first_assum(assume_tac o MATCH_MP is_suffix_take)
>> Cases_on `n`
(* 0 requires special treatment since it's outside the range of the inductive hypothesis *)
>- (rw[dynamic_lcs_row_def]
(* first element of r is a match *)
>- (first_x_assum(ASSUME_TAC o MATCH_MP dynamic_lcs_row_invariant_pres1)
>> fs[dynamic_lcs_row_invariant_def,SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM]
>> qpat_x_assum `LENGTH x = LENGTH y` (assume_tac o GSYM)
>> fs[SUB_LEFT_SUC] >> rw[] >>
`LENGTH fullr = SUC (LENGTH r)` by fs[] >> fs[])
(* first element of r is NOT a match *)
>- (`dynamic_lcs_row_invariant h' r
(longest' (HD previous_row) previous_col)
(TL previous_row) (HD previous_row) prevh fullr`
by (MATCH_MP_TAC(GEN_ALL dynamic_lcs_row_invariant_pres2) >> metis_tac[])
>> fs[dynamic_lcs_row_invariant_def,SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM]
>> qpat_x_assum `LENGTH x = LENGTH y` (assume_tac o GSYM) >> rfs[]
>> fs[SUB_LEFT_SUC] >> rw[]
>- (`LENGTH fullr = LENGTH previous_row` by fs[]
>> fs[] >> rfs[]
>> first_assum (assume_tac o MATCH_MP take_singleton_one) >> fs[])
>- (qpat_x_assum `SUC(LENGTH x) = LENGTH y` (assume_tac o GSYM)
>> fs[] >> rfs[])))
(* SUC n -- inductive case *)
>> rw[dynamic_lcs_row_def]
(* first element of r is a match *)
>- (first_x_assum(ASSUME_TAC o MATCH_MP dynamic_lcs_row_invariant_pres1)
>> fs[SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM]
>> first_x_assum(assume_tac o Q.SPECL[`h`,
`longest' (h::diagonal,SUC dl)
(longest' (HD previous_row) previous_col)`,
`TL previous_row`, `HD previous_row`, `prevh`,
`fullr`,`n'`])
>> rfs[] >> metis_tac[sub_le_suc])
(* first element of r is NOT a match *)
>- (`dynamic_lcs_row_invariant h' r
(longest' (HD previous_row) previous_col)
(TL previous_row) (HD previous_row) prevh fullr`
by (MATCH_MP_TAC (GEN_ALL dynamic_lcs_row_invariant_pres2)
>> metis_tac[])
>> fs[SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM]
>> first_x_assum(assume_tac o Q.SPECL[`h'`,
`longest' (HD previous_row) previous_col`,
`TL previous_row`, `HD previous_row`, `prevh`,
`fullr`,`n'`])
>> rfs[] >> metis_tac[sub_le_suc])
QED
Theorem dynamic_lcs_row_invariant_pres2[allow_rebind]:
!h r previous_col previous_row diagonal prevh fullr l n.
(dynamic_lcs_row_invariant h r previous_col previous_row diagonal prevh fullr
/\ (dynamic_lcs_row h r previous_col previous_row diagonal = l)
/\ (0 <= n) /\ (n < LENGTH l))
==> (SND (EL n l) = LENGTH (FST (EL n l)))
Proof
Induct_on `r`
>> rpt strip_tac
>> Cases_on `diagonal`
>> rename1 `(diagonal,dl)`
>- (fs[dynamic_lcs_row_def]
>> metis_tac[LENGTH,prim_recTheory.NOT_LESS_0])
>> `IS_SUFFIX fullr (h::r)` by fs[dynamic_lcs_row_invariant_def]
>> first_assum(assume_tac o MATCH_MP is_suffix_length)
>> first_assum(assume_tac o MATCH_MP is_suffix_take)
>> Cases_on `n`
(* 0 requires special treatment since it's outside the range of the inductive hypothesis *)
>- (rw[dynamic_lcs_row_def]
(* first element of r is a match *)
>- (first_x_assum(ASSUME_TAC o MATCH_MP dynamic_lcs_row_invariant_pres1)
>> fs[dynamic_lcs_row_invariant_def,SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM])
(* first element of r is NOT a match *)
>- (`dynamic_lcs_row_invariant h' r
(longest' (HD previous_row) previous_col)
(TL previous_row) (HD previous_row) prevh fullr`
by (MATCH_MP_TAC(GEN_ALL dynamic_lcs_row_invariant_pres2) >> metis_tac[])
>> fs[dynamic_lcs_row_invariant_def,SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM]))
(* SUC n -- inductive case *)
>> rw[dynamic_lcs_row_def]
(* first element of r is a match *)
>- (first_x_assum(ASSUME_TAC o MATCH_MP dynamic_lcs_row_invariant_pres1)
>> fs[SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM]
>> first_x_assum(assume_tac o Q.SPECL[`h`,
`longest' (h::diagonal,SUC dl)
(longest' (HD previous_row) previous_col)`,
`TL previous_row`, `HD previous_row`, `prevh`,
`fullr`,`n'`])
>> rfs[])
(* first element of r is NOT a match *)
>- (`dynamic_lcs_row_invariant h' r
(longest' (HD previous_row) previous_col)
(TL previous_row) (HD previous_row) prevh fullr`
by (MATCH_MP_TAC (GEN_ALL dynamic_lcs_row_invariant_pres2)
>> metis_tac[])
>> fs[SNOC_APPEND,GSYM ADD1,
dynamic_lcs_length,Q.SPEC `1` ADD_SYM]
>> first_x_assum(assume_tac o Q.SPECL[`h'`,
`longest' (HD previous_row) previous_col`,
`TL previous_row`, `HD previous_row`, `prevh`,
`fullr`,`n'`])
>> rfs[])
QED
Theorem dynamic_lcs_rows_invariant_pres:
dynamic_lcs_rows_invariant (h::l) r previous_row fullh
==> dynamic_lcs_rows_invariant l r (dynamic_lcs_row h r ([],0) previous_row ([],0)) fullh
Proof
fs[dynamic_lcs_rows_invariant_def]
>> rpt strip_tac
>- fs[dynamic_lcs_length]
>- metis_tac[IS_SUFFIX_CONS2_E]
>- (first_assum(assume_tac o MATCH_MP is_suffix_take)
>> fs[dynamic_lcs_length]
>> `lcs (REVERSE (FST(EL n (dynamic_lcs_row h r ([],0) previous_row ([],0)))))
(TAKE (LENGTH fullh − SUC (LENGTH l)) fullh ++ [h])
(TAKE (SUC n + (LENGTH r -
(LENGTH (dynamic_lcs_row h r ([],0) previous_row ([],0)))))
r)` suffices_by fs[dynamic_lcs_length]
>> MATCH_MP_TAC dynamic_lcs_row_invariant_pres
>> Q.EXISTS_TAC `r` >> Q.EXISTS_TAC `([],0)`
>> Q.EXISTS_TAC `previous_row` >> Q.EXISTS_TAC `([],0)`
>> rpt strip_tac >> fs[dynamic_lcs_length]
>> fs[dynamic_lcs_row_invariant_def]
>> fs[lcs_def,common_subsequence_def,is_subsequence_nil])
>- (first_assum(assume_tac o MATCH_MP is_suffix_take)
>> fs[dynamic_lcs_length]
>> MATCH_MP_TAC dynamic_lcs_row_invariant_pres2
>> Q.EXISTS_TAC `h` >> Q.EXISTS_TAC `r`
>> Q.EXISTS_TAC `([],0)` >> Q.EXISTS_TAC `previous_row`
>> Q.EXISTS_TAC `([],0)`
>> Q.EXISTS_TAC `(TAKE (LENGTH fullh − SUC (LENGTH l)) fullh)` >> Q.EXISTS_TAC `r`
>> fs[] >> fs[dynamic_lcs_row_invariant_def,lcs_empty]
>> fs[dynamic_lcs_length])
QED
Theorem dynamic_lcs_rows_correct:
!l r previous_row fullh.
dynamic_lcs_rows_invariant l r previous_row fullh
==> lcs (REVERSE (dynamic_lcs_rows l r previous_row)) fullh r
Proof
Induct
>> rpt strip_tac
(* nil *)
>- (fs[dynamic_lcs_rows_invariant_def]
>> rw[dynamic_lcs_rows_def]
>> last_x_assum(assume_tac o Q.SPEC `LENGTH r - 1`)
>> rfs[quantHeuristicsTheory.LIST_LENGTH_0,lcs_empty]
>> qpat_x_assum `LENGTH x = LENGTH y` (assume_tac o GSYM)
>> `0 < LENGTH previous_row` by(Cases_on `previous_row` >> fs[])
>> fs[LAST_EL,PRE_SUB1,SUB_LEFT_SUC,if_length_lemma])
(* cons *)
>> first_assum(assume_tac o MATCH_MP dynamic_lcs_rows_invariant_pres)
>> first_x_assum(assume_tac o Q.SPECL [`r`,
`dynamic_lcs_row h r ([],0) previous_row ([],0)`,
`fullh`])