-
Notifications
You must be signed in to change notification settings - Fork 86
/
mlstringScript.sml
1415 lines (1247 loc) · 37.4 KB
/
mlstringScript.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
(*
Pure functions for the String module.
Defines mlstring as a separate type from string in HOL's standard library (a
synonym for char list).
*)
open preamble totoTheory mllistTheory
val _ = new_theory"mlstring"
val cpn_distinct = TypeBase.distinct_of ``:ordering``
val cpn_nchotomy = TypeBase.nchotomy_of ``:ordering``
(* Defines strings as a separate type from char list. This theory should be
moved into HOL, either as its own theory, or as an addendum to stringTheory *)
Datatype:
mlstring = strlit string
End
val _ = add_strliteral_form{inj=``strlit``, ldelim = "«"};
Definition implode_def:
implode = strlit
End
Definition strlen_def:
strlen (strlit s) = LENGTH s
End
Definition strsub_def:
strsub (strlit s) n = EL n s
End
(* the test here is because underspecification is annoying (and SEG is underspecified) *)
(* the underlying primitive (CopyStrStr) raises an exception if the test is false *)
Definition substring_def:
substring (strlit s) off len = strlit (if off + len ≤ LENGTH s then SEG len off s
else if off <= LENGTH s then DROP off s
else "")
End
Definition concat_def:
concat l = strlit (FLAT (MAP (λs. case s of strlit x => x) l))
End
Theorem concat_nil[simp]:
concat [] = strlit ""
Proof
EVAL_TAC
QED
val _ = export_rewrites["strlen_def","strsub_def"];
Definition explode_aux_def:
(explode_aux s n 0 = []) ∧
(explode_aux s n (SUC len) =
strsub s n :: (explode_aux s (n + 1) len))
End
val _ = export_rewrites["explode_aux_def"];
Theorem explode_aux_thm:
∀max n ls.
(n + max = LENGTH ls) ⇒
(explode_aux (strlit ls) n max = DROP n ls)
Proof
Induct \\ rw[] \\ fs[LENGTH_NIL_SYM,DROP_LENGTH_TOO_LONG]
\\ match_mp_tac (GSYM rich_listTheory.DROP_EL_CONS)
\\ simp[]
QED
Definition explode_def:
explode s = explode_aux s 0 (strlen s)
End
Theorem explode_thm[simp]:
explode (strlit ls) = ls
Proof
rw[explode_def,SIMP_RULE std_ss [] explode_aux_thm]
QED
Theorem explode_implode[simp]:
∀x. explode (implode x) = x
Proof
rw[implode_def]
QED
Theorem implode_explode[simp]:
∀x. implode (explode x) = x
Proof
Cases >> rw[implode_def]
QED
Theorem explode_11[simp]:
∀s1 s2. (explode s1 = explode s2) ⇔ (s1 = s2)
Proof
Cases >> Cases >> simp[]
QED
Theorem implode_BIJ:
BIJ implode UNIV UNIV
Proof
rw[BIJ_IFF_INV] >>
qexists_tac`explode` >>
rw[implode_explode,
explode_implode]
QED
Theorem explode_BIJ:
BIJ explode UNIV UNIV
Proof
rw[BIJ_IFF_INV] >>
qexists_tac`implode` >>
rw[implode_explode,
explode_implode]
QED
Theorem LENGTH_explode[simp]:
LENGTH (explode s) = strlen s
Proof
Cases_on`s` \\ simp[]
QED
Theorem concat_thm:
concat l = implode (FLAT (MAP explode l))
Proof
rw[concat_def,implode_def] \\
rpt (AP_TERM_TAC ORELSE AP_THM_TAC) \\
rw[FUN_EQ_THM] \\ CASE_TAC \\ simp[]
QED
Theorem strlen_implode[simp]:
strlen (implode s) = LENGTH s
Proof
EVAL_TAC
QED
Theorem strlen_substring:
strlen (substring s i j) = if i + j <= strlen s then j
else if i <= strlen s then strlen s - i
else 0
Proof
Cases_on`s` \\ rw[substring_def,LENGTH_SEG]
QED
Definition extract_def:
extract s i opt =
if strlen s <= i
then implode []
else case opt of
SOME x => substring s i (MIN (strlen s - i) x)
| NONE => substring s i (strlen s - i)
End
Theorem strlen_extract_le:
!s x y. strlen (extract s x y) <= strlen s - x
Proof
rw[extract_def] >> CASE_TAC >> fs[strlen_substring]
QED
Theorem strsub_substring_0_thm:
∀m n l. m < n ⇒ strsub (substring l 0 n) m = strsub l m
Proof
Cases_on`l` \\ rw[strsub_def,substring_def]
\\ rw[SEG_TAKE_DROP,EL_TAKE]
QED
Theorem substring_full[simp]:
substring s 0 (strlen s) = s
Proof
Cases_on`s` \\ rw[substring_def,SEG_LENGTH_ID]
QED
Theorem substring_too_long:
strlen s <= i ==> substring s i j = strlit ""
Proof
Cases_on`s` \\ rw[substring_def,DROP_NIL] \\
`j = 0` by decide_tac \\ fs[SEG]
QED
Definition strcat_def:
strcat s1 s2 = concat [s1; s2]
End
val _ = Parse.add_infix("^",480,Parse.LEFT)
Overload "^" = ``λx y. strcat x y``
Theorem concat_cons:
concat (h::t) = strcat h (concat t)
Proof
rw[strcat_def,concat_def]
QED
Theorem strcat_thm:
strcat s1 s2 = implode (explode s1 ++ explode s2)
Proof
rw[strcat_def,concat_def]
\\ CASE_TAC \\ rw[] \\ CASE_TAC \\ rw[implode_def]
QED
Theorem strcat_assoc[simp]:
!s1 s2 s3.
s1 ^ (s2 ^ s3) = s1 ^ s2 ^ s3
Proof
rw[strcat_def,concat_def]
QED
Theorem strcat_nil[simp]:
(strcat (strlit "") s = s) ∧
(strcat s (strlit "") = s)
Proof
rw[strcat_def,concat_def] \\ CASE_TAC \\ rw[]
QED
Theorem concat_append:
concat (xs ++ ys) = concat xs ^ concat ys
Proof
Induct_on `xs` \\ simp [concat_cons]
QED
Theorem implode_STRCAT:
!l1 l2.
implode(STRCAT l1 l2) = implode l1 ^ implode l2
Proof
rw[implode_def, strcat_def, concat_def]
QED
Theorem explode_strcat[simp]:
explode (strcat s1 s2) = explode s1 ++ explode s2
Proof
rw[strcat_thm]
QED
Theorem strlen_strcat[simp]:
strlen (strcat s1 s2) = strlen s1 + strlen s2
Proof
rw[strcat_thm]
QED
Definition concatWith_aux_def:
(concatWith_aux s [] bool = implode []) /\
(concatWith_aux s (h::t) T = strcat h (concatWith_aux s t F)) /\
(concatWith_aux s (h::t) F = strcat s (concatWith_aux s (h::t) T))
Termination
wf_rel_tac `inv_image ($< LEX $<) (\(s,l,b). (LENGTH l, if b then 0n else 1))` \\
rw[]
End
Definition concatWith_def:
concatWith s l = concatWith_aux s l T
End
Triviality concatWith_CONCAT_WITH_aux:
!s l fl. (CONCAT_WITH_aux s l fl = REVERSE fl ++ explode (concatWith (implode s) (MAP implode l)))
Proof
ho_match_mp_tac CONCAT_WITH_aux_ind
\\ rw[CONCAT_WITH_aux_def, concatWith_def, implode_def, concatWith_aux_def, strcat_thm]
>-(Induct_on `l` \\ rw[MAP, implode_def, concatWith_aux_def, strcat_thm]
\\ Cases_on `l` \\ rw[concatWith_aux_def, explode_implode, strcat_thm, implode_def])
QED
Theorem concatWith_CONCAT_WITH:
!s l. CONCAT_WITH s l = explode (concatWith (implode s) (MAP implode l))
Proof
rw[concatWith_def, CONCAT_WITH_def, concatWith_CONCAT_WITH_aux]
QED
Definition str_def:
str (c: char) = implode [c]
End
Theorem explode_str[simp]:
explode (str c) = [c]
Proof
rw[str_def]
QED
Theorem strlen_str[simp]:
strlen (str c) = 1
Proof
rw[str_def]
QED
Definition translate_aux_def:
(translate_aux f s n 0 = []) /\
(translate_aux f s n (SUC len) = f (strsub s n)::translate_aux f s (n + 1) len)
End
Definition translate_def:
translate f s = implode (translate_aux f s 0 (strlen s))
End
Triviality translate_aux_thm:
!f s n len. (n + len = strlen s) ==> (translate_aux f s n len = MAP f (DROP n (explode s)))
Proof
Cases_on `s` \\ Induct_on `len` \\ rw [translate_aux_def, strlen_def, explode_def] \\
rw [DROP_LENGTH_NIL] \\
rw [strsub_def, DROP_EL_CONS]
QED
Theorem translate_thm:
!f s. translate f s = implode (MAP f (explode s))
Proof
rw [translate_def, translate_aux_thm]
QED
Definition splitl_aux_def:
splitl_aux P s i =
if i < strlen s ∧ P (strsub s i) then
splitl_aux P s (i+1)
else (extract s 0 (SOME i), extract s i NONE)
Termination
WF_REL_TAC`inv_image $< (λ(x,s,i). strlen s - i)`
End
val splitl_aux_ind = theorem"splitl_aux_ind";
Definition splitl_def:
splitl P s = splitl_aux P s 0
End
Theorem splitl_aux_SPLITP:
∀P s i.
splitl_aux P s i =
(implode o ((++)(TAKE i (explode s))) ## implode)
(SPLITP ((~) o P) (DROP i (explode s)))
Proof
recInduct splitl_aux_ind
\\ rw[]
\\ Cases_on`SPLITP P (DROP i (explode s))` \\ fs[]
\\ simp[Once splitl_aux_def]
\\ Cases_on`strlen s ≤ i` \\ fs[DROP_LENGTH_TOO_LONG,LENGTH_explode]
>- (
fs[SPLITP] \\ rveq
\\ simp[TAKE_LENGTH_TOO_LONG,LENGTH_explode]
\\ simp[extract_def]
\\ Cases_on`s` \\ fs[substring_def]
\\ rw[implode_def]
\\ qmatch_goalsub_rename_tac`MIN (LENGTH s) i`
\\ `MIN (LENGTH s) i = LENGTH s` by rw[MIN_DEF]
\\ rw[SEG_LENGTH_ID] )
\\ Cases_on`DROP i (explode s)` \\ fs[DROP_NIL,LENGTH_explode]
\\ fs[SPLITP]
\\ `strsub s i = h` by ( Cases_on`s` \\ rfs[strsub_def,DROP_EL_CONS] )
\\ rveq \\ fs[]
\\ IF_CASES_TAC \\ fs[]
>- (
rveq \\ fs[]
\\ rfs[DROP_EL_CONS,LENGTH_explode]
\\ rveq
\\ Cases_on`SPLITP ($~ o P) (DROP (i+1) (explode s))` \\ fs[]
\\ AP_TERM_TAC
\\ simp[LIST_EQ_REWRITE,LENGTH_TAKE,LENGTH_explode]
\\ rw[]
\\ Cases_on`x < i` \\ simp[EL_APPEND1,EL_APPEND2,LENGTH_explode,EL_TAKE]
\\ Cases_on`x < i+1` \\ simp[EL_APPEND1,EL_APPEND2,LENGTH_explode,EL_TAKE,EL_CONS,PRE_SUB1]
\\ `x = i` by DECIDE_TAC
\\ rw[] )
\\ Cases_on`s`
\\ rw[extract_def,substring_def,implode_def] \\ fs[MIN_DEF]
\\ simp[TAKE_SEG] \\ rfs[]
\\ rfs[DROP_SEG]
QED
Theorem splitl_SPLITL:
splitl P s = (implode ## implode) (SPLITL P (explode s))
Proof
rw[splitl_def,splitl_aux_SPLITP,SPLITL_def]
\\ Cases_on`SPLITP((~)o P)(explode s)` \\ fs[]
QED
Definition tokens_aux_def:
(tokens_aux f s [] n 0 = []) /\
(tokens_aux f s (h::t) n 0 = [implode (REVERSE (h::t))]) /\
(tokens_aux f s [] n (SUC len) =
if f (strsub s n)
then tokens_aux f s [] (n + 1) len
else tokens_aux f s [strsub s n] (n + 1) len) /\
(tokens_aux f s (h::t) n (SUC len) =
if f (strsub s n)
then (implode (REVERSE (h::t)))::(tokens_aux f s [] (n + 1) len)
else tokens_aux f s (strsub s n::(h::t)) (n + 1) len)
End
val tokens_aux_ind = theorem"tokens_aux_ind";
Definition tokens_def:
tokens f s = tokens_aux f s [] 0 (strlen s)
End
Triviality tokens_aux_filter:
!f s ss n len. (n + len = strlen s) ==> (concat (tokens_aux f s ss n len) =
implode (REVERSE ss++FILTER ($~ o f) (DROP n (explode s))))
Proof
Cases_on `s` \\ Induct_on `len` \\
rw [strlen_def, tokens_aux_def, concat_cons, DROP_LENGTH_NIL, strcat_thm, implode_def] \\
Cases_on `ss` \\ rw [tokens_aux_def, DROP_EL_CONS, concat_cons, strcat_thm, implode_def]
QED
Theorem tokens_filter:
!f s. concat (tokens f s) = implode (FILTER ($~ o f) (explode s))
Proof
rw [tokens_def, tokens_aux_filter]
QED
Theorem TOKENS_eq_tokens_aux:
!P ls ss n len. (n + len = LENGTH (explode ls)) ==>
(MAP explode (tokens_aux P ls ss n len) = case ss of
| (h::t) => if (len <> 0) /\ ($~ (P (EL n (explode ls)))) then
(REVERSE (h::t) ++ HD (TOKENS P (DROP n (explode ls))))::TL (TOKENS P (DROP n (explode ls)))
else if (len <> 0) then
REVERSE (h::t)::(TOKENS P (DROP n (explode ls)))
else [REVERSE(h::t)]
| [] => (TOKENS P (DROP n (explode ls))))
Proof
ho_match_mp_tac tokens_aux_ind \\ rw[] \\ Cases_on `s`
\\ rw[explode_thm, tokens_aux_def, TOKENS_def, implode_def, strlen_def, strsub_def]
\\ fs[strsub_def, DROP_LENGTH_TOO_LONG, TOKENS_def]
>-(rw[EQ_SYM_EQ, Once DROP_EL_CONS] \\ rw[TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[]
\\ imp_res_tac SPLITP_NIL_FST_IMP \\ fs[SPLITP] \\ rfs[])
>-(rw[EQ_SYM_EQ, Once DROP_EL_CONS]
\\ rw[TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[]
>-(fs[SPLITP] \\ rfs[] \\ Cases_on `DROP (n + 1) s'`)
>-(fs[SPLITP] \\ rfs[] \\ Cases_on `DROP (n + 1) s'`
>-(imp_res_tac DROP_EMPTY \\ fs[ADD1])
\\ Cases_on `f h` \\ rw[]
>-(`n + 1 < LENGTH s'` by fs[]
\\ `h = EL (n + 1) s'` by metis_tac[HD_DROP, HD] \\ fs[])
\\ rw[TOKENS_def, SPLITP]
) (*this is a copy*)
>-(fs[SPLITP] \\ rfs[] \\ Cases_on `DROP (n + 1) s'`
>-(imp_res_tac DROP_EMPTY \\ fs[ADD1])
\\ Cases_on `f h` \\ rw[]
>-(`n + 1 < LENGTH s'` by fs[]
\\ `h = EL (n + 1) s'` by metis_tac[HD_DROP, HD] \\ fs[])
\\ rw[TOKENS_def, SPLITP]))
>-(rw[DROP_EL_CONS, TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[]
\\ rw[TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[] \\ metis_tac[TL])
(*This is a copy *)
>-(rw[DROP_EL_CONS, TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[]
\\ rw[TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[] \\ metis_tac[TL])
>-(`n = LENGTH s' - 1` by DECIDE_TAC
\\ rw[DROP_EL_CONS, DROP_LENGTH_TOO_LONG, TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[]
\\ `LENGTH r = 1` by rw[]
\\ Cases_on `TL r` >-(rw[TOKENS_def])
\\ rw[] \\ fs[])
>-(fs[ADD1]
\\ `x0 = implode [EL n s']` by fs[implode_explode] \\ rw[explode_implode]
\\ rw[DROP_EL_CONS, DROP_LENGTH_TOO_LONG, TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[] \\ rw[TOKENS_def])
\\(rw[DROP_EL_CONS, DROP_LENGTH_TOO_LONG, TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[] \\ rw[TOKENS_def]
\\ pairarg_tac \\ fs[NULL_EQ] \\ rw[] \\ fs[SPLITP] \\ rfs[] \\ metis_tac[TL])
QED
(*
>> TRY (
recogniser (e.g., rename1_tac or qmatch_goalsub_rename_tac ...) >>
... >> NO_TAC)
>> TRY (
... >> NO_TAC)
>> TRY (
... >> NO_TAC)
*)
Theorem TOKENS_eq_tokens:
!P ls.(MAP explode (tokens P ls) = TOKENS P (explode ls))
Proof
Cases_on `ls` \\ rw[tokens_def, TOKENS_eq_tokens_aux]
QED
(*
Theorem TOKENS_eq_tokens_sym
`!P ls. tokens P ls = MAP implode (TOKENS P (explode ls))`
(rw[]
\\ Q.ISPEC_THEN`explode`match_mp_tac INJ_MAP_EQ
\\ simp[MAP_MAP_o,INJ_DEF,explode_11,o_DEF,explode_implode,TOKENS_eq_tokens]
*)
Theorem TOKENS_eq_tokens_sym =
TOKENS_eq_tokens
|> SPEC_ALL
|> Q.AP_TERM`MAP implode`
|> SIMP_RULE(srw_ss())[MAP_MAP_o,implode_explode,o_DEF]
Theorem tokens_append:
!P s1 x s2.
P x ==>
(tokens P (strcat (strcat s1 (str x)) s2) = tokens P s1 ++ tokens P s2)
Proof
rw[TOKENS_eq_tokens_sym] \\ Cases_on `s1` \\ Cases_on `s2`
\\ rewrite_tac[GSYM MAP_APPEND] \\ AP_TERM_TAC
\\ rw[explode_thm]
\\ rewrite_tac[GSYM APPEND_ASSOC,APPEND]
\\ match_mp_tac TOKENS_APPEND \\ rw[]
QED
Definition tokens_alt_aux_def:
(tokens_alt_aux f s i j n =
if j < n then
(if f (strsub s j)
then
(if i = j then tokens_alt_aux f s (i+1) (j+1) n
else
substring s i (j-i)::
(tokens_alt_aux f s (j+1) (j+1) n))
else
tokens_alt_aux f s i (j+1) n)
else
if i = j then []
else [substring s i (j-i)])
Termination
WF_REL_TAC`measure (λf,s,i,j,n. n - j)`>>rw[]
End
Theorem substring_1_strsub:
i < strlen s ⇒
substring s i 1 = str (strsub s i)
Proof
Cases_on`s`>>rw[substring_def]>>
DEP_REWRITE_TAC[SEG1]>>
gvs[str_def,implode_def]
QED
Theorem substring_0[simp]:
substring s i 0 = strlit ""
Proof
Cases_on`s`>>rw[substring_def]>>
EVAL_TAC
QED
Theorem substring_add:
i + x + y ≤ strlen s ⇒
substring s i (x + y) =
substring s i x ^ substring s (i + x) y
Proof
Cases_on`s`>>rw[substring_def]>>
simp[TAKE_SUM]>>
DEP_REWRITE_TAC[SEG_TAKE_DROP]>>
rw[TAKE_SUM,DROP_DROP]>>
simp[strcat_def]>>
EVAL_TAC
QED
Theorem tokens_alt_tokens_alt_aux:
∀f s acc j l i.
i ≤ j ∧ j ≤ strlen s ∧
strlen s - j = l ∧
acc = REVERSE (explode (substring s i (j - i))) ⇒
tokens_aux f s acc j l =
tokens_alt_aux f s i j (strlen s)
Proof
ho_match_mp_tac tokens_aux_ind>>rw[]
>- (
simp[Once tokens_alt_aux_def]>>
rw[tokens_aux_def]>>
qpat_x_assum`_ _ = ""`
(mp_tac o Q.AP_TERM `LENGTH`)>>
simp[strlen_substring]>>rw[])
>- (
qpat_assum`STRING _ _ = _` (SUBST1_TAC o SYM)>>
simp[Once tokens_alt_aux_def,tokens_aux_def]>>
rw[]>>gvs[]>>
qpat_x_assum`STRING _ _ = REVERSE _`
(mp_tac o Q.AP_TERM `implode o REVERSE`)>>
simp[])
>- (
simp[Once tokens_alt_aux_def]>>
rw[tokens_aux_def]>>gvs[]
>- (
qpat_x_assum`_ _ = ""`
(mp_tac o Q.AP_TERM `LENGTH`)>>
simp[strlen_substring]>>rw[])
>- (
first_x_assum(qspec_then`i` mp_tac)>> simp[]>>
impl_keep_tac >-
simp[substring_1_strsub]>>
rw[])>>
first_x_assum(qspec_then`i` mp_tac)>> simp[]>>
impl_keep_tac >- (
qpat_x_assum`_ _ = ""`
(mp_tac o Q.AP_TERM `LENGTH`)>>
simp[strlen_substring]>>rw[])>>
rw[])>>
gvs[]>>
pop_assum (assume_tac o SYM)>>
simp[Once tokens_alt_aux_def]>>
rw[tokens_aux_def]>>gvs[]
>- (
qpat_x_assum`REVERSE _ = STRING _ _`
(mp_tac o Q.AP_TERM `implode o REVERSE`)>>
simp[])
>- (
first_x_assum(qspec_then`i` mp_tac)>> simp[]>>
impl_keep_tac >- (
`j + 1 - i = (j - i) + 1` by simp[]>>
pop_assum SUBST1_TAC>>
DEP_REWRITE_TAC[substring_add]>>
simp[]>>
DEP_REWRITE_TAC[substring_1_strsub]>>rw[])>>
rw[])
QED
Theorem tokens_alt:
tokens f s =
tokens_alt_aux f s 0 0 (strlen s)
Proof
rw[tokens_def]>>
match_mp_tac tokens_alt_tokens_alt_aux>>
simp[]
QED
Definition fields_aux_def:
(fields_aux f s ss n 0 = [implode (REVERSE ss)]) /\
(fields_aux f s ss n (SUC len) =
if f (strsub s n)
then implode (REVERSE ss)::(fields_aux f s [] (n + 1) len)
else fields_aux f s (strsub s n::ss) (n + 1) len)
End
Definition fields_def:
fields f s = fields_aux f s [] 0 (strlen s)
End
Triviality fields_aux_filter:
!f s ss n len. (n + len = strlen s) ==> (concat (fields_aux f s ss n len) =
implode (REVERSE ss++FILTER ($~ o f) (DROP n (explode s))))
Proof
Cases_on `s` \\ Induct_on `len` \\ rw [strlen_def, fields_aux_def, concat_cons,
implode_def, explode_thm, DROP_LENGTH_NIL, strcat_thm] \\
rw [DROP_EL_CONS]
QED
Theorem fields_filter:
!f s. concat (fields f s) = implode (FILTER ($~ o f) (explode s))
Proof
rw [fields_def, fields_aux_filter]
QED
Triviality fields_aux_length:
!f s ss n len. (n + len = strlen s) ==>
(LENGTH (fields_aux f s ss n len) = LENGTH (FILTER f (DROP n (explode s))) + 1)
Proof
Cases_on `s` \\ Induct_on `len` \\
rw [strlen_def, fields_aux_def, explode_thm, DROP_LENGTH_NIL, ADD1, DROP_EL_CONS]
QED
Theorem fields_length:
!f s. LENGTH (fields f s) = (LENGTH (FILTER f (explode s)) + 1)
Proof
rw [fields_def, fields_aux_length]
QED
Definition fields_alt_aux_def:
(fields_alt_aux f s i j n =
if j < n then
(if f (strsub s j)
then
substring s i (j-i)::
(fields_alt_aux f s (j+1) (j+1) n)
else
fields_alt_aux f s i (j+1) n)
else
[substring s i (j-i)])
Termination
WF_REL_TAC`measure (λf,s,i,j,n. n - j)`>>rw[]
End
Theorem fields_alt_fields_alt_aux:
∀l acc j i.
i ≤ j ∧ j ≤ strlen s ∧
strlen s - j = l ∧
acc = REVERSE (explode (substring s i (j - i))) ⇒
fields_aux f s acc j l =
fields_alt_aux f s i j (strlen s)
Proof
Induct>>rw[]
>- (
simp[Once fields_alt_aux_def]>>
rw[fields_aux_def])
>- (
simp[Once fields_alt_aux_def]>>
rw[fields_aux_def]>>gvs[]>>
first_x_assum(qspecl_then [`j+1`,`i`] mp_tac)>> simp[]>>
`j + 1 - i = (j - i) + 1` by simp[]>>
pop_assum SUBST1_TAC>>
DEP_REWRITE_TAC[substring_add]>>
simp[]>>
DEP_REWRITE_TAC[substring_1_strsub]>>rw[])
QED
Theorem fields_alt:
fields f s =
fields_alt_aux f s 0 0 (strlen s)
Proof
rw[fields_def]>>
match_mp_tac fields_alt_fields_alt_aux>>
simp[]
QED
Definition str_findi_def:
str_findi P i s = if i < strlen s
then if P (strsub s i) then SOME i else str_findi P (i + 1) s
else NONE
Termination
WF_REL_TAC `measure (\(P, i, s). strlen s - i)`
End
Theorem str_findi_range:
!P i s. str_findi P i s = SOME j ==> i <= j /\ j < strlen s
Proof
recInduct str_findi_ind
\\ rpt gen_tac
\\ disch_tac
\\ simp [Once str_findi_def]
\\ rw []
\\ fs []
QED
Theorem OLEAST_LE_STEP:
(OLEAST j. i <= j /\ P j) = (if P i then SOME i
else (OLEAST j. i + 1 <= j /\ P j))
Proof
rw []
\\ simp [whileTheory.OLEAST_EQ_SOME]
\\ qmatch_goalsub_abbrev_tac `opt1 = $OLEAST _`
\\ Cases_on `opt1`
\\ fs [whileTheory.OLEAST_EQ_SOME]
\\ rw []
\\ fs [LESS_EQ |> REWRITE_RULE [ADD1] |> GSYM, arithmeticTheory.LT_LE]
\\ CCONTR_TAC
\\ fs []
\\ metis_tac []
QED
Theorem str_findi_OLEAST:
!P i s. str_findi P i s = (OLEAST j. i <= j /\ j < strlen s /\ P (strsub s j))
Proof
recInduct str_findi_ind
\\ rw []
\\ simp [Once OLEAST_LE_STEP]
\\ simp [Once str_findi_def]
\\ rw []
\\ fs []
\\ CCONTR_TAC
\\ fs []
QED
Definition isStringThere_aux_def:
(isStringThere_aux s1 s2 s1i s2i 0 = T) /\
(isStringThere_aux s1 s2 s1i s2i (SUC len) =
if strsub s1 s1i = strsub s2 s2i
then isStringThere_aux s1 s2 (s1i + 1) (s2i + 1) len
else F)
End
(*
val isStringThere_thm = Q.prove (
`!s1 s2 s1i s2i len. (s2i + len <= strlen s2) /\ (s1i + len = strlen s1) /\
(strlen s1 <= strlen s2) /\ (s1i <= s2i) /\ (isStringThere_aux s1 s2 0 s2i (strlen s1)) ==>
(SEG len s2i (explode s2) = TAKE len (explode s1))`
Cases_on `s1` \\ Cases_on `s2` \\
rw [strlen_def, explode_thm, SEG, SEG_TAKE_DROP] \\
Cases_on `len` \\ rw [SEG] \\ `s2i < STRLEN s'` by DECIDE_TAC \\
);
*)
Definition isPrefix_def:
isPrefix s1 s2 =
if strlen s1 <= strlen s2
then isStringThere_aux s1 s2 0 0 (strlen s1)
else F
End
Definition isSuffix_def:
isSuffix s1 s2 =
if strlen s1 <= strlen s2
then isStringThere_aux s1 s2 0 (strlen s2 - strlen s1) (strlen s1)
else F
End
Definition isSubstring_aux_def:
(isSubstring_aux s1 s2 lens1 n 0 = F) /\
(isSubstring_aux s1 s2 lens1 n (SUC len) =
if (isStringThere_aux s1 s2 0 n lens1)
then T
else isSubstring_aux s1 s2 lens1 (n + 1) len)
End
Definition isSubstring_def:
isSubstring s1 s2 =
if strlen s1 <= strlen s2
then isSubstring_aux s1 s2 (strlen s1) 0 ((strlen s2) - (strlen s1) + 1)
else F
End
(* proof that isSubstring has the right sort of properties *)
Theorem isStringThere_SEG:
∀i1 i2.
i1 + n ≤ LENGTH s1 ∧ i2 + n ≤ LENGTH s2 ⇒
(isStringThere_aux (strlit s1) (strlit s2) i1 i2 n <=>
(SEG n i1 s1 = SEG n i2 s2))
Proof
Induct_on `n`
>- simp[SEG, isStringThere_aux_def]
>- simp[isStringThere_aux_def, SEG_SUC_EL]
QED
Theorem isSubstring_aux_lemma:
∀i len.
i + len ≤ strlen s2 ==>
(isSubstring_aux s1 s2 lens1 i len ⇔
∃n. n < len ∧ isStringThere_aux s1 s2 0 (n+i) lens1)
Proof
Induct_on `len`
>- simp[isSubstring_aux_def] >>
fs[isSubstring_aux_def] >> rw[EQ_IMP_THM]
>- (qexists_tac ‘0’ >> simp[])
>- (rename [‘n < len’, ‘i + (n + 1)’] >> qexists_tac ‘n + 1’ >> simp[]) >>
rename [‘isStringThere_aux _ _ 0 (i + n)’] >>
Cases_on ‘n’ >> fs[] >> metis_tac[ADD1]
QED
Theorem isSubstring_SEG:
isSubstring (strlit s1) (strlit s2) <=>
∃i. i + LENGTH s1 ≤ LENGTH s2 ∧ SEG (LENGTH s1) i s2 = s1
Proof
rw[isSubstring_def] >> Cases_on `s1` >> simp[]
>- (fs[isSubstring_aux_def, isStringThere_aux_def, GSYM ADD1] >>
qexists_tac `0` >> simp[SEG])
>- (simp[] >>
rename [‘SUC (STRLEN s0) ≤ STRLEN s2’, ‘STRING h s0’] >>
Cases_on ‘SUC(STRLEN s0) ≤ STRLEN s2’ >> fs[] >>
csimp[isSubstring_aux_lemma, isStringThere_SEG, SUB_LEFT_LESS,
DECIDE “x < y + 1n ⇔ x ≤ y”] >>
‘STRLEN (STRING h s0) = SUC (STRLEN s0)’ by simp[] >>
metis_tac[SEG_LENGTH_ID])
QED
Theorem strlit_STRCAT:
strlit a ^ strlit b = strlit (a ++ b)
Proof
fs[strcat_def, concat_def]
QED
Theorem isSubString_spec:
isSubstring s1 s2 ⇔ ∃p s. s2 = p ^ s1 ^ s
Proof
map_every Cases_on [`s1`,`s2`] >> rw[isSubstring_SEG, EQ_IMP_THM]
>- (rename [‘SEG (STRLEN s1) i s2 = s1’] >>
map_every qexists_tac [
‘strlit (TAKE i s2)’, ‘strlit (DROP (i + STRLEN s1) s2)’
] >> simp[strlit_STRCAT] >> metis_tac[TAKE_SEG_DROP, ADD_COMM]) >>
rename [‘strlit s2 = px ^ strlit s1 ^ sx’] >>
qexists_tac `strlen px` >> Cases_on `px` >> simp[strlit_STRCAT] >>
Cases_on `sx` >> fs[strlit_STRCAT] >>
simp[SEG_APPEND1, SEG_APPEND2, SEG_LENGTH_ID]
QED
(* String orderings *)
Definition compare_aux_def:
compare_aux (s1: mlstring) s2 ord start len =
if len = 0n then
ord
else if strsub s2 start < strsub s1 start
then GREATER
else if strsub s1 start < strsub s2 start
then LESS
else compare_aux s1 s2 ord (start + 1) (len - 1)
End
Definition compare_def:
compare s1 s2 = if (strlen s1) < (strlen s2)
then compare_aux s1 s2 LESS 0 (strlen s1)
else if (strlen s2) < (strlen s1)
then compare_aux s1 s2 GREATER 0 (strlen s2)
else compare_aux s1 s2 EQUAL 0 (strlen s2)
End
Definition mlstring_lt_def:
mlstring_lt s1 s2 ⇔ (compare s1 s2 = LESS)
End
Definition mlstring_le_def:
mlstring_le s1 s2 ⇔ (compare s1 s2 ≠ GREATER)
End
Definition mlstring_gt_def:
mlstring_gt s1 s2 ⇔ (compare s1 s2 = GREATER)
End
Definition mlstring_ge_def:
mlstring_ge s1 s2 ⇔ (compare s1 s2 <> LESS)
End
Overload "<" = ``λx y. mlstring_lt x y``
Overload "<=" = ``λx y. mlstring_le x y``
Overload ">" = ``λx y. mlstring_gt x y``
Overload ">=" = ``λx y. mlstring_ge x y``
(* Properties of string orderings *)
val flip_ord_def = ternaryComparisonsTheory.invert_comparison_def
Overload flip_ord = ``invert_comparison``
Triviality compare_aux_spec:
!s1 s2 ord_in start len.
len + start ≤ strlen s1 ∧ len + start ≤ strlen s2 ⇒
(compare_aux s1 s2 ord_in start len =
if TAKE len (DROP start (explode s1)) = TAKE len (DROP start (explode s2)) then
ord_in
else if string_lt (TAKE len (DROP start (explode s1))) (TAKE len (DROP start (explode s2))) then
LESS
else
GREATER)
Proof
Induct_on `len` >>
rw [] >>
ONCE_REWRITE_TAC [compare_aux_def] >>
simp [] >>
Cases_on `s1` >>
Cases_on `s2` >>
fs [] >>
full_simp_tac (srw_ss()) [TAKE_SUM, DECIDE ``!n. SUC n = 1 + n``] >>
fs [TAKE1_DROP, DROP_DROP_T, char_lt_def] >>
fs [string_lt_def] >>
simp [] >>
rw [] >>
fs [char_lt_def, CHAR_EQ_THM]
QED
Triviality compare_aux_refl:
!s1 s2 start len.
start + len ≤ strlen s1 ∧ start + len ≤ strlen s2
⇒
((compare_aux s1 s2 EQUAL start len = EQUAL)
⇔
(TAKE len (DROP start (explode s1)) = TAKE len (DROP start (explode s2))))
Proof
rw [compare_aux_spec]
QED
Triviality compare_aux_equal:
!s1 s2 ord_in start len.
(compare_aux s1 s2 ord_in start len = EQUAL) ⇒ (ord_in = EQUAL)
Proof
Induct_on `len` >>
rw []
>- fs [Once compare_aux_def] >>
pop_assum mp_tac >>
ONCE_REWRITE_TAC [compare_aux_def] >>
simp_tac (std_ss++ARITH_ss) [] >>
rw [] >>
metis_tac []
QED
Triviality compare_aux_sym:
!s1 s2 ord_in start len ord_out.
(compare_aux s1 s2 ord_in start len = ord_out)
⇔
(compare_aux s2 s1 (flip_ord ord_in) start len = flip_ord ord_out)
Proof
Induct_on `len` >>
rw [] >>
ONCE_REWRITE_TAC [compare_aux_def] >>
simp []
>- (
Cases_on `ord_in` >>
Cases_on `ord_out` >>
simp [flip_ord_def]) >>
simp [char_lt_def, CHAR_EQ_THM] >>
`ORD (strsub s2 start) < ORD (strsub s1 start) ∨
ORD (strsub s1 start) < ORD (strsub s2 start) ∨
(ORD (strsub s1 start) = ORD (strsub s2 start))`
by decide_tac
>- (
Cases_on `ord_out` >>
simp [flip_ord_def])
>- (
simp [] >>
Cases_on `ord_out` >>
simp [flip_ord_def]) >>
ASM_REWRITE_TAC [] >>
simp_tac (std_ss++ARITH_ss) [] >>
metis_tac []