Repository navigation
Expand file tree
/
Copy pathsyntax_helperScript.sml
More file actions
822 lines (738 loc) · 21.6 KB
/
Copy pathsyntax_helperScript.sml
File metadata and controls
822 lines (738 loc) · 21.6 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
(*
Syntactic print/parse helper files
*)
Theory syntax_helper
Ancestors
misc cnf mlint mlstring mllist input_lex
Libs
preamble
(* The concrete formats number variables from 1, reserving 0 as the clause
terminator. var_lit is polymorphic, so the type is pinned here. *)
Overload nz_lit = ``(λ(l:num lit). var_lit l ≠ 0)``
(* Parse an integer as a literal. *)
Definition mk_lit_def:
mk_lit l =
let n = Num (ABS l) in
if l > 0 then Pos n else Neg n
End
(* The inverse conversion, from a literal to its integer encoding. *)
Definition to_ilit_def:
to_ilit (l : num lit) =
case l of
Pos n => (&n):int
| Neg n => -&n
End
Theorem to_ilit_NEQ_0:
var_lit l ≠ 0 ⇒
to_ilit l ≠ 0
Proof
Cases_on`l`>>rw[to_ilit_def]
QED
(* to_ilit is inverted by the parser's literal reader *)
Theorem mk_lit_to_ilit:
var_lit l ≠ 0 ⇒
mk_lit (to_ilit l) = l
Proof
Cases_on`l`>>rw[mk_lit_def,to_ilit_def]>>
intLib.ARITH_TAC
QED
Theorem to_ilit_mk_lit:
to_ilit (mk_lit l) = l
Proof
rw[mk_lit_def,to_ilit_def]>>
intLib.ARITH_TAC
QED
(* Canonical form for a clause: repeated literals removed, and sorted
in decreasing lit_le order. The contract exported here is
MEM_canon_clause and canon_clause_ALL_DISTINCT.
lit_le orders literals by variable, with Pos before Neg. to_ilit is not
usable as the sort key: it maps Pos 0 and Neg 0 to the same integer. *)
Definition lit_le_def[simp]:
(lit_le (Pos m) (Pos n) ⇔ m ≤ (n:num)) ∧
(lit_le (Pos m) (Neg n) ⇔ m ≤ n) ∧
(lit_le (Neg m) (Pos n) ⇔ m < n) ∧
(lit_le (Neg m) (Neg n) ⇔ m ≤ n)
End
Definition sorted_nub_aux_def:
(sorted_nub_aux h [] acc = (h::acc)) ∧
(sorted_nub_aux h (i::is) acc =
if i = h then sorted_nub_aux h is acc
else sorted_nub_aux i is (h::acc))
End
Definition sorted_nub_def:
(sorted_nub [] = []) ∧
(sorted_nub (x::xs) =
sorted_nub_aux x xs [])
End
Definition canon_clause_def:
canon_clause (ls:num lit list) =
sorted_nub (sort lit_le ls)
End
Overload lit_gt = ``λx y. lit_le y x ∧ x ≠ y``
Theorem lit_le_total:
total lit_le
Proof
rw[total_def]>>
Cases_on`x`>>Cases_on`y`>>gvs[]>>
decide_tac
QED
Theorem lit_le_transitive:
transitive lit_le
Proof
rw[transitive_def]>>
Cases_on`x`>>Cases_on`y`>>Cases_on`z`>>gvs[]>>
decide_tac
QED
Theorem lit_le_antisym:
lit_le x y ∧ lit_le y x ⇒ x = y
Proof
Cases_on`x`>>Cases_on`y`>>gvs[]>>
decide_tac
QED
Theorem sorted_nub_aux_SORTED_strict:
∀ls h acc.
SORTED lit_gt (h::acc) ∧
SORTED lit_le (h::ls) ⇒
SORTED lit_gt (sorted_nub_aux h ls acc)
Proof
Induct>>rw[sorted_nub_aux_def]>>
first_x_assum irule>>
gvs[]>>
metis_tac[lit_le_transitive,transitive_def]
QED
Theorem sorted_nub_SORTED_strict:
SORTED lit_le ls ⇒
SORTED lit_gt (sorted_nub ls)
Proof
rw[oneline sorted_nub_def]>>
TOP_CASE_TAC>>gvs[]>>
irule sorted_nub_aux_SORTED_strict>>
simp[]
QED
Theorem MEM_sorted_nub_aux:
∀ls x h acc.
MEM x ls ∨ x = h ∨ MEM x acc ⇔
MEM x (sorted_nub_aux h ls acc)
Proof
Induct>>rw[sorted_nub_aux_def]>>
metis_tac[MEM]
QED
Theorem MEM_sorted_nub:
MEM x (sorted_nub ls) ⇔
MEM x ls
Proof
rw[oneline sorted_nub_def]>>
Cases_on`ls`>>gvs[]>>
metis_tac[MEM_sorted_nub_aux,MEM]
QED
Theorem canon_clause_ALL_DISTINCT:
ALL_DISTINCT (canon_clause ls)
Proof
rw[canon_clause_def]>>
irule SORTED_ALL_DISTINCT>>
irule_at Any sorted_nub_SORTED_strict>>
irule_at Any sort_SORTED>>
simp[lit_le_total,lit_le_transitive,irreflexive_def,transitive_def]>>
metis_tac[lit_le_transitive,transitive_def,lit_le_antisym]
QED
Theorem MEM_canon_clause[simp]:
MEM x (canon_clause ls) ⇔ MEM x ls
Proof
rw[canon_clause_def,MEM_sorted_nub]
QED
Theorem canon_clause_eq_nil[simp]:
canon_clause ls = [] ⇔ ls = []
Proof
`∀l:num lit list. l = [] ⇔ ∀x. ¬MEM x l` by
(Cases>>simp[]>>metis_tac[])>>
metis_tac[MEM_canon_clause]
QED
(* Force literals to be in maxvar *)
Definition check_maxvar_def:
check_maxvar maxvar ls =
EVERY (λl. var_lit l ≤ maxvar:num) ls
End
(* A single line of literals, forcing maxvar *)
Definition parse_lits_def:
parse_lits maxvar ls =
case parse_until_zero ls of
SOME (ls,[]) =>
let ls = MAP mk_lit ls in
if check_maxvar maxvar ls
then SOME ls
else NONE
| _ => NONE
End
(* Everything the parser accepts uses non-zero literals *)
Theorem nz_lit_mk_lit:
i ≠ 0 ⇒ nz_lit (mk_lit i)
Proof
rw[mk_lit_def]>>
intLib.ARITH_TAC
QED
Theorem parse_lits_nz_lit:
parse_lits maxvar ls = SOME c ⇒
EVERY nz_lit c
Proof
rw[parse_lits_def]>>
gvs[AllCaseEqs()]>>
drule parse_until_zero_nz>>
simp[EVERY_MAP,EVERY_MEM]>>
metis_tac[nz_lit_mk_lit]
QED
(***
Printing helpers.
***)
Definition print_lit_def:
(print_lit (Pos n) = toString n) ∧
(print_lit (Neg n) = «-» ^ toString n)
End
(* Print a list of literals with optional terminator char. *)
Definition print_lits_def:
print_lits (term:char) ls =
let ls = SNOC («0» ^ toString term) (MAP print_lit ls) in
concatWith « » ls
End
Theorem print_lits_nil:
print_lits term [] = «0» ^ toString term
Proof
rw[print_lits_def,concatWith_def,mllistTheory.intersperse_def]>>
EVAL_TAC
QED
Theorem print_lits_cons:
print_lits term (l::ls) =
print_lit l ^ « » ^ print_lits term ls
Proof
rw[print_lits_def,concatWith_def]>>
Cases_on`ls`>>
simp[mllistTheory.intersperse_def]>>
EVAL_TAC>>
simp[STRCAT_ASSOC]>>
simp[STRCAT_ASSOC]
QED
(* Maximum of a list of numbers *)
Definition max_list_def:
(max_list k [] = k) ∧
(max_list k (x::xs) = max_list (MAX k x) xs)
End
Theorem max_list_max:
∀ls k.
k ≤ max_list k ls ∧
EVERY (λn. n ≤ max_list k ls) ls
Proof
Induct>>rw[max_list_def]>>
first_x_assum(qspec_then`MAX k h` mp_tac)>>
simp[]
QED
Theorem le_max_list:
(∃l. v ≤ l ∧ MEM l ls) ⇒
v ≤ max_list k ls
Proof
rw[]>>
assume_tac (SPEC_ALL max_list_max)>>
rw[EVERY_MEM]>>
first_x_assum drule>>fs[]
QED
Definition print_header_line_def:
print_header_line v len =
«p cnf » ^ toString v ^ « » ^ toString len ^ «\n»
End
(* The printers produce single newline-terminated lines *)
Theorem toString_not_newline[local]:
¬MEM #"\n" (explode (toString (n:num)))
Proof
Cases_on`toString n`>>
drule num_to_str_every>>
rw[EVERY_MEM]>>
strip_tac>>first_x_assum drule>>
EVAL_TAC
QED
Theorem print_lits_newline:
∃t. print_lits #"\n" ls = t ^ «\n» ∧ ¬MEM #"\n" (explode t)
Proof
Induct_on`ls`>>rw[print_lits_nil,print_lits_cons]
>- (qexists`«0»`>>EVAL_TAC)>>
qexists`print_lit h ^ « » ^ t`>>
Cases_on`h`>>simp[print_lit_def,toString_not_newline]
QED
Theorem print_header_line_newline:
∃t. print_header_line v len = t ^ «\n» ∧ ¬MEM #"\n" (explode t)
Proof
rw[print_header_line_def,toString_not_newline]
QED
(***
Round trip: parsing a printed line returns it unchanged.
***)
Theorem tokens_unchanged:
EVERY ($~ o P) (explode ls) ∧ ¬ NULL (explode ls) ⇒
tokens P ls = [ls]
Proof
rw[] >> drule TOKENS_unchanged>>
simp[]>>
simp[GSYM mlstringTheory.TOKENS_eq_tokens]
QED
Theorem isDigit_not_blanks[local]:
isDigit c ==> ~ blanks c
Proof
CCONTR_TAC>>fs [blanks_def]>>fs [isDigit_def]
QED
Theorem blanks_thms[simp]:
(blanks #"\n") ∧
(blanks #" ") ∧
(¬blanks #"-")
Proof
EVAL_TAC
QED
Theorem tokens_blanks_print_lit:
tokens blanks (print_lit l) = [print_lit l]
Proof
match_mp_tac tokens_unchanged>>
Cases_on`l`>>
simp[print_lit_def]>>
rename1`toString n`>>
Cases_on`toString n`>>
`~NULL s` by
(drule num_to_str_imp_cons>>rw[]>>fs[])>>
simp[]>>
irule listTheory.EVERY_MONOTONIC >>
irule_at Any num_to_str_every>>
asm_exists_tac>>simp[GSYM isDigit_def, isDigit_not_blanks]
QED
Theorem print_lit_alt:
n ≠ 0 ⇒
(print_lit (Pos n) = int_to_string (#"-") (&n)) ∧
(print_lit (Neg n) = int_to_string (#"-") (-&n))
Proof
rw[print_lit_def,int_to_string_thm,num_to_str_thm]>>
simp[strcat_def,concat_def]
QED
Theorem fromString_print_lit:
nz_lit h ⇒
fromString (print_lit h) = SOME (to_ilit h)
Proof
Cases_on`h`>>rw[]>>
drule print_lit_alt>>simp[]>>
rw[to_ilit_def]
QED
Theorem tokenize_print_lit[simp]:
nz_lit n ⇒
tokenize (print_lit n) = INR (to_ilit n)
Proof
rw[tokenize_def]>>
drule fromString_print_lit>>rw[]
QED
Theorem parse_until_zero_aux_print_lits:
∀ys acc.
EVERY nz_lit ys ∧ blanks c
⇒
parse_until_zero_aux (toks (print_lits c ys ^ rest)) acc =
SOME (REVERSE acc ++ MAP to_ilit ys,toks rest)
Proof
simp[toks_def]>>
Induct>>rw[print_lits_nil,print_lits_cons]
>- (
drule mlstringTheory.tokens_append>>simp[]>>
PURE_REWRITE_TAC[GSYM strcat_assoc]>>
disch_then (fn th => simp[th])>>
EVAL_TAC)>>
`blanks #" " ∧ toString #" " = « »` by EVAL_TAC>>
drule mlstringTheory.tokens_append>>simp[]>>
PURE_REWRITE_TAC[GSYM strcat_assoc]>>
disch_then (fn th => simp[th])>>
simp[tokens_blanks_print_lit]>>
gvs[parse_until_zero_aux_def]>>
DEP_REWRITE_TAC[tokenize_print_lit]>>
simp[]>>
`to_ilit h ≠ 0` by metis_tac[to_ilit_NEQ_0]>>
simp[]
QED
Theorem parse_lits_print_lits:
EVERY nz_lit ys ∧
EVERY (λl. var_lit l ≤ maxvar) ys
⇒
parse_lits maxvar (toks (print_lits #"\n" ys)) = SOME ys
Proof
strip_tac>>
simp[parse_lits_def,parse_until_zero_def]>>
`print_lits #"\n" ys = print_lits #"\n" ys ^ «»` by simp[strcat_nil]>>
pop_assum (fn th => PURE_ONCE_REWRITE_TAC[th])>>
DEP_REWRITE_TAC[parse_until_zero_aux_print_lits]>>
simp[]>>
`toks «» = []` by EVAL_TAC>>
simp[]>>
`MAP mk_lit (MAP to_ilit ys) = ys` by (
simp[MAP_MAP_o,o_DEF,MAP_EQ_ID]>>
gvs[EVERY_MEM]>>
metis_tac[mk_lit_to_ilit])>>
simp[check_maxvar_def]
QED
Theorem FILTER_keep_line_print_lits:
EVERY (EVERY nz_lit) ls ⇒
FILTER keep_line
(MAP toks (MAP (print_lits #"\n") ls)) =
(MAP toks (MAP (print_lits #"\n") ls))
Proof
simp[FILTER_EQ_ID,EVERY_MAP,EVERY_MEM]>>
rw[]>>
Cases_on`x`>>simp[print_lits_nil,print_lits_cons]
>- EVAL_TAC >>
`blanks #" " ∧ toString #" " = « »` by EVAL_TAC>>
first_x_assum drule>>
simp[DISJ_IMP_THM,FORALL_AND_THM]>>rw[]>>
simp[toks_def]>>
drule mlstringTheory.tokens_append>>simp[]>>
PURE_REWRITE_TAC[GSYM strcat_assoc]>>
disch_then (fn th => simp[th])>>
simp[tokens_blanks_print_lit]>>
DEP_REWRITE_TAC[tokenize_print_lit]>>
simp[keep_line_def]
QED
Theorem tokens_blanks_toString:
tokens blanks (toString h) = [toString h]
Proof
match_mp_tac tokens_unchanged>>
Cases_on`toString h`>>
`~NULL s` by
(drule num_to_str_imp_cons>>rw[]>>fs[])>>
simp[]>>
irule listTheory.EVERY_MONOTONIC >>
irule_at Any num_to_str_every>>
asm_exists_tac>>simp[GSYM isDigit_def, isDigit_not_blanks]
QED
Theorem tokenize_toString[simp]:
tokenize (toString n) = INR (&n)
Proof
rw[tokenize_def,num_to_str_def]
QED
Theorem fromString_toString_num:
mlint$fromString ((toString (n:num)):mlstring) = SOME (&n)
Proof
rw[num_to_str_def]
QED
Theorem parse_header_line_print_header_line:
parse_header_line (toks (print_header_line v len)) = SOME(v,len)
Proof
rw[print_header_line_def, toks_def]>>
qmatch_goalsub_abbrev_tac`aa ^ bb ^ _ ^ cc ^ dd`>>
`blanks #" " ∧ toString #" " = « »` by EVAL_TAC>>
drule mlstringTheory.tokens_append>>simp[]>>
`aa = «p» ^ « » ^ «cnf» ^ « »` by
(fs[Abbr`aa`]>>EVAL_TAC)>>
strip_tac>>
first_assum(qspecl_then[`aa ^ bb`,`cc ^ dd`] assume_tac)>>fs[]>>
`cc ^ dd = cc ^ dd ^ «»` by EVAL_TAC>>
pop_assum SUBST_ALL_TAC>>
`blanks #"\n" ∧ toString #"\n" = «\n»` by EVAL_TAC>>
drule mlstringTheory.tokens_append>>simp[]>>
unabbrev_all_tac>>
rw[]>>
`tokens blanks «p» = [«p»]` by EVAL_TAC>>
`tokens blanks «cnf» = [«cnf»]` by EVAL_TAC>>
`tokens blanks «» = []` by EVAL_TAC>>
simp[tokens_blanks_toString]>>
simp[tokenize_def,parse_header_line_def]>>
CONJ_TAC >- EVAL_TAC>>
simp[fromString_toString_num]>>
intLib.ARITH_TAC
QED
Theorem print_header_line_first:
∃ls. tokens blanks (print_header_line a b) =
«p»::ls
Proof
rw[print_header_line_def]>>
qmatch_goalsub_abbrev_tac`aa ^ bb ^ _ ^ dd ^ ee`>>
`aa = «p» ^ « » ^ «cnf» ^ « »` by
(fs[Abbr`aa`]>>EVAL_TAC)>>
simp[]>>
PURE_REWRITE_TAC[GSYM mlstringTheory.strcat_assoc]>>
PURE_REWRITE_TAC[Once mlstringTheory.strcat_assoc]>>
`blanks #" " ∧ toString #" " = « »` by EVAL_TAC>>
drule mlstringTheory.tokens_append>>simp[]>>
`tokens blanks «p» = [«p»]` by EVAL_TAC>>
simp[]
QED
(***
Binary format parser
***)
Definition parse_vb_num_aux_def:
parse_vb_num_aux (s:mlstring) (i:num) (len:num) (ex:num) (n:num) =
if i < len then
let v = ORD (strsub_unsafe s i) in
if v >= 128 then (* msb is set *)
parse_vb_num_aux s (i+1) len (ex*128) ((v-128)*ex+n)
else
((v*ex+n),i + 1)
else (0, i) (* should not happen *)
Termination
WF_REL_TAC` measure (λ(x,s,i,r). i-s)`
End
Theorem parse_vb_num_aux_i:
!s i len ex n m i'.
m <> 0 /\
(m,i') = parse_vb_num_aux s i len ex n ==>
i < len /\ i < i'
Proof
ho_match_mp_tac parse_vb_num_aux_ind >>
rpt GEN_TAC >> strip_tac >>
simp[Once parse_vb_num_aux_def] >>
rw[] >> fs[] >>
first_x_assum (drule_at (Pos last)) >>
rw[] >>
fs[Once parse_vb_num_aux_def,AllCaseEqs()]
QED
(* Decoding never moves the read position past the end of the record *)
Theorem parse_vb_num_aux_le:
∀s i len ex n m j.
parse_vb_num_aux s i len ex n = (m,j) ∧ i ≤ len ⇒ j ≤ len
Proof
ho_match_mp_tac parse_vb_num_aux_ind>>
rpt gen_tac>>strip_tac>>
simp[Once parse_vb_num_aux_def]>>
rw[]
QED
Definition parse_vb_num_def:
parse_vb_num s offset len =
parse_vb_num_aux s offset len 1 0
End
Theorem parse_vb_num_le:
parse_vb_num s i len = (m,j) ∧ i ≤ len ⇒ j ≤ len
Proof
rw[parse_vb_num_def]>>
metis_tac[parse_vb_num_aux_le]
QED
Definition parse_vb_int_def:
parse_vb_int s offset len =
let (m,i) = parse_vb_num s offset len in
let v =
(if m = 0 then 0i
else if m MOD 2 = 0n
then (&(m DIV 2):int)
else (-&(m DIV 2):int)) in
(v,i)
End
Theorem parse_vb_int_le:
parse_vb_int s i len = (v,j) ∧ i ≤ len ⇒ j ≤ len
Proof
rw[parse_vb_int_def]>>
pairarg_tac>>gvs[]>>
metis_tac[parse_vb_num_le]
QED
(* Decoding a non-zero value moves the read position forward *)
Theorem parse_vb_int_i:
(v,j) = parse_vb_int s i len ⇒ v = 0 ∨ i < len ∧ i < j
Proof
rw[parse_vb_int_def,parse_vb_num_def]>>
pairarg_tac>>gvs[]>>
Cases_on`m = 0`>>simp[]>>
qpat_x_assum`parse_vb_num_aux _ _ _ _ _ = _`(assume_tac o GSYM)>>
drule_all parse_vb_num_aux_i>>
simp[]
QED
(* An unrolled reading of parse_vb_int.
A doubled number m = p0 + p1*128 + p2*128^2 + ... has m MOD 2 = p0 MOD 2 and
m DIV 2 = p0 DIV 2 + p1*64 + p2*8192 + ..., so the sign and the halving are
settled by the first byte alone and every later byte contributes its payload
scaled by 128^k DIV 2. The first byte's parity and half are taken with word8
masks and a shift on char_to_word8 c, which translates to a single
char-to-byte conversion (Eval_char_to_word8, translator/ml_translatorScript.sml);
n2w of a num bound separately translates to Word8.fromInt, a division. The
four unrolled bytes cover every number below 2^28, and the rest is left to
the general loop. *)
Definition vb_sgn_def:
vb_sgn neg (h:num) = if neg then -&h else (&h:int)
End
Definition vb_int_lo_def:
vb_int_lo (s:mlstring) (i:num) (len:num) =
if i < len then
(let c = strsub_unsafe s i in
let b = ORD c in
let w = char_to_word8 c in
let neg = (w2n (w && 1w) = 1) in
let h = w2n ((w && 127w) >>> 1) in
if b < 128 then (vb_sgn neg h, i + 1)
else if i + 1 < len then
(let b = ORD (strsub_unsafe s (i + 1)) in
if b < 128 then (vb_sgn neg (h + b * 64), i + 2)
else if i + 2 < len then
(let h = h + (b - 128) * 64 in
let b = ORD (strsub_unsafe s (i + 2)) in
if b < 128 then (vb_sgn neg (h + b * 8192), i + 3)
else if i + 3 < len then
(let h = h + (b - 128) * 8192 in
let b = ORD (strsub_unsafe s (i + 3)) in
if b < 128 then (vb_sgn neg (h + b * 1048576), i + 4)
else
(let h = h + (b - 128) * 1048576 in
let (m,j) =
parse_vb_num_aux s (i + 4) len 268435456
(2 * h + if neg then 1 else 0) in
((if m MOD 2 = 0n then (&(m DIV 2):int)
else (-&(m DIV 2):int)), j)))
else (0, i + 3))
else (0, i + 2))
else (0, i + 1))
else (0, i)
End
Theorem vb_half_bits[local]:
b < 256 ==>
w2n ((n2w b :word8) && 1w) = b MOD 2 /\
w2n (((n2w b :word8) && 127w) >>> 1) = (b MOD 128) DIV 2
Proof
simp[SIMP_RULE (srw_ss()) [] (Q.SPEC `1n` WORD_AND_EXP_SUB1),
SIMP_RULE (srw_ss()) [] (Q.SPEC `7n` WORD_AND_EXP_SUB1),
w2n_lsr, w2n_n2w] >>
`b MOD 2 < 2 /\ b MOD 128 < 128` by simp[MOD_LESS] >>
simp[LESS_MOD]
QED
Theorem vb_sgn_decode[local]:
(if m MOD 2 = 0n then (&(m DIV 2):int) else (-&(m DIV 2):int)) =
vb_sgn (m MOD 2 = 1) (m DIV 2)
Proof
`m MOD 2 < 2` by simp[MOD_LESS] >>
`m MOD 2 = 0 \/ m MOD 2 = 1` by simp[] >>
gvs[vb_sgn_def]
QED
(* One byte of the general loop, run at an even multiplier so that the
accumulator stays of the form 2 * half + parity. *)
Theorem vb_int_step[local]:
!ex s i len g par.
par < 2 ==>
(let (m,j) = parse_vb_num_aux s i len (2 * ex) (2 * g + par) in
((if m MOD 2 = 0n then (&(m DIV 2):int) else (-&(m DIV 2):int)), j)) =
if i < len then
(let b = ORD (strsub_unsafe s i) in
if b < 128 then (vb_sgn (par = 1) (g + b * ex), i + 1)
else
(let (m,j) =
parse_vb_num_aux s (i + 1) len (2 * (ex * 128))
(2 * (g + (b - 128) * ex) + par) in
((if m MOD 2 = 0n then (&(m DIV 2):int) else (-&(m DIV 2):int)), j)))
else (0, i)
Proof
rpt strip_tac >>
CONV_TAC (LHS_CONV (ONCE_REWRITE_CONV [parse_vb_num_aux_def])) >>
rw[]
>- fs[]
>- (AP_TERM_TAC >> AP_TERM_TAC >> simp[]) >>
`par = 0 \/ par = 1` by simp[] >>
gvs[vb_sgn_def]
QED
Theorem parse_vb_int_eq:
parse_vb_int s i len = vb_int_lo s i len
Proof
simp[parse_vb_int_def, parse_vb_num_def] >>
CONV_TAC (LHS_CONV (ONCE_REWRITE_CONV [parse_vb_num_aux_def])) >>
`!m:num. (if m = 0 then 0i
else if m MOD 2 = 0 then (&(m DIV 2):int) else (-&(m DIV 2):int)) =
(if m MOD 2 = 0 then (&(m DIV 2):int) else (-&(m DIV 2):int))` by rw[] >>
Cases_on `i < len` >> Cases_on `ORD (strsub_unsafe s i) < 128` >>
simp[vb_int_lo_def, ORD_BOUND, vb_half_bits]
>- simp[vb_sgn_decode] >>
`?r. r < 128 /\ ORD (strsub_unsafe s i) = 128 + r` by
(qexists_tac `ORD (strsub_unsafe s i) - 128` >> simp[ORD_BOUND]) >>
gvs[] >>
`r = 2 * (r DIV 2) + r MOD 2` by
simp[Once (Q.SPEC `r` (MATCH_MP DIVISION (DECIDE ``0n < 2``))), MULT_COMM] >>
pop_assum (fn th => CONV_TAC (LHS_CONV (ONCE_REWRITE_CONV [th]))) >>
`r MOD 2 < 2` by simp[MOD_LESS] >>
`(if r MOD 2 = 1 then 1n else 0) = r MOD 2` by rw[] >>
simp[SIMP_RULE (srw_ss()) [LET_THM] (Q.SPEC `64n` vb_int_step),
SIMP_RULE (srw_ss()) [LET_THM] (Q.SPEC `8192n` vb_int_step),
SIMP_RULE (srw_ss()) [LET_THM] (Q.SPEC `1048576n` vb_int_step)]
QED
(* Reads doubled literals, decoding as it goes, until a value that
decodes to 0, which is the terminating zero of a well-formed record.
The result is in reverse order with respect to the input and never
contains the literal 0. *)
Definition parse_vb_ilits_def:
parse_vb_ilits (s:mlstring) (i:num) (len:num) (acc:int list) =
let (v,j) = parse_vb_int s i len in
if v <> 0
then parse_vb_ilits s j len (v::acc)
else acc
Termination
WF_REL_TAC` measure (λ(x,s,i,r). i-s)`>>
rw[]>>
drule_then strip_assume_tac parse_vb_int_i>>
gvs[]
End
Theorem parse_vb_ilits_nz:
∀s i len acc.
¬MEM 0 acc ⇒ ¬MEM 0 (parse_vb_ilits s i len acc)
Proof
ho_match_mp_tac parse_vb_ilits_ind>>
rw[]>>
simp[Once parse_vb_ilits_def]>>
pairarg_tac>>
rw[]
QED
(* The doubled literals of a binary record from offset i, in file order,
up to the value that decodes to 0 *)
(* TMC *)
Definition parse_vb_ilits_fwd_def:
parse_vb_ilits_fwd (s:mlstring) (i:num) (len:num) =
let (v,j) = parse_vb_int s i len in
if v ≠ 0 then v :: parse_vb_ilits_fwd s j len else []
Termination
WF_REL_TAC` measure (λ(x,s,i). i-s)`>>
rw[]>>
drule_then strip_assume_tac parse_vb_int_i>>
gvs[]
End
Theorem parse_vb_ilits_fwd:
∀s i len acc.
parse_vb_ilits s i len acc = REVERSE (parse_vb_ilits_fwd s i len) ++ acc
Proof
ho_match_mp_tac parse_vb_ilits_ind>>
rw[]>>
simp[Once parse_vb_ilits_def,Once parse_vb_ilits_fwd_def]>>
pairarg_tac>>
rw[]
QED
Theorem parse_vb_ilits_fwd_nz:
∀s i len. ¬MEM 0 (parse_vb_ilits_fwd s i len)
Proof
ho_match_mp_tac parse_vb_ilits_fwd_ind>>
rw[]>>
simp[Once parse_vb_ilits_fwd_def]>>
pairarg_tac>>
rw[]
QED
(* Other ASCII syntax parsing tools *)
(* Parse nums until the next zero and returns. *)
Definition parse_until_zero_nn_aux_def:
(parse_until_zero_nn_aux [] acc = NONE) ∧
(parse_until_zero_nn_aux (x::xs) acc =
case x of
INL _ => NONE
| INR l =>
if l = 0:int then
SOME (REVERSE acc, xs)
else
if l > 0 then parse_until_zero_nn_aux xs (Num (ABS l)::acc)
else NONE
)
End
Definition parse_until_zero_nn_def:
parse_until_zero_nn ls =
parse_until_zero_nn_aux ls []
End
(* If a line starts with the character,
return INR without that char
otherwise INL or line unchanged *)
Definition starts_with_def:
starts_with s ls =
case ls of
x::rest => if x = s then INR rest else INL ls
| [] => INL ls
End
(* The RUP hint format in ASCII: (int list) 0 (num list) 0 *)
Definition parse_rup_def:
parse_rup rest =
case parse_until_zero rest of
NONE => NONE
| SOME (c ,rest) =>
(case parse_until_zero_nn rest of
SOME (hints, []) => SOME (c,hints)
| _ => NONE)
End