forked from CakeML/cakeml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_implScript.sml
633 lines (587 loc) · 24.5 KB
/
lexer_implScript.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
(*
Definition of the lexer: code for consuming tokens until a top-level
semicolon is found (semicolons can be hidden in `let`-`in`-`end` blocks,
structures, signatures, and between parentheses).
TODO: update this description if it is incorrect.
*)
open preamble tokensTheory lexer_funTheory
val _ = temp_delsimps ["NORMEQ_CONV"]
val _ = new_theory "lexer_impl";
val _ = set_grammar_ancestry ["misc", "tokens", "lexer_fun", "ASCIInumbers", "location"]
val tac =
full_simp_tac (srw_ss()) [char_le_def, char_lt_def] >>
Cases_on `t` >>
rw [get_token_def, processIdent_def, isAlphaNum_def, isAlpha_def, isDigit_def,
isLower_def, isUpper_def];
Theorem get_token_eqn:
!s.
get_token s =
case s of
[] => LexErrorT
| [c] =>
if c ≤ #";" then
if c ≤ #")" then
if c ≤ #"'" then
if c = #"#" then HashT else
if c = #"'" then TyvarT s else
SymbolT s
else
if c = #"(" then LparT else
if c = #")" then RparT else
SymbolT s
else
if c ≤ #"," then
if c = #"*" then StarT else
if c = #"," then CommaT else
SymbolT s
else
if c = #":" then ColonT else
if c = #";" then SemicolonT else
SymbolT s
else
if c ≤ #"]" then
if c ≤ #"Z" then
if #"A" ≤ c ∧ c ≤ #"Z" then AlphaT s else
if c = #"=" then EqualsT else
SymbolT s
else
if c = #"[" then LbrackT else
if c = #"]" then RbrackT else
SymbolT s
else
if c ≤ #"{" then
if c = #"_" then UnderbarT else
if #"a" ≤ c ∧ c ≤ #"z" then AlphaT s else
if c = #"{" then LbraceT else
SymbolT s
else
if c = #"|" then BarT else
if c = #"}" then RbraceT else
SymbolT s
| c::s' =>
if c < #"a" then
if c ≤ #"." then
if c = #"'" then TyvarT s else
if s = "->" then ArrowT else
if s = "..." then DotsT else
SymbolT s
else
if s = ":>" then SealT else
if s = "=>" then DarrowT else
if #"A" ≤ c ∧ c ≤ #"Z" then AlphaT s else
SymbolT s
else if c ≤ #"z" then
if c ≤ #"i" then
if c ≤ #"e" then
if c < #"e" then
if s = "and" then AndT else
if s = "andalso" then AndalsoT else
if s = "as" then AsT else
if s = "case" then CaseT else
if s = "datatype" then DatatypeT else
AlphaT s
else
if s = "else" then ElseT else
if s = "end" then EndT else
if s = "eqtype" then EqtypeT else
if s = "exception" then ExceptionT else
AlphaT s
else
if c ≤ #"h" then
if s = "fn" then FnT else
if s = "fun" then FunT else
if s = "handle" then HandleT else
AlphaT s
else
if s = "if" then IfT else
if s = "in" then InT else
if s = "include" then IncludeT else
AlphaT s
else
if c ≤ #"r" then
if c = #"l" then
if s = "let" then LetT else
if s = "local" then LocalT else
AlphaT s
else if c = #"o" then
if s = "of" then OfT else
if s = "op" then OpT else
if s = "open" then OpenT else
if s = "orelse" then OrelseT else
AlphaT s
else
if s = "raise" then RaiseT else
if s = "rec" then RecT else
AlphaT s
else
if c = #"s" then
if s = "sharing" then SharingT else
if s = "sig" then SigT else
if s = "signature" then SignatureT else
if s = "struct" then StructT else
if s = "structure" then StructureT else
AlphaT s
else if c < #"w" then
if s = "then" then ThenT else
if s = "type" then TypeT else
if s = "val" then ValT else
AlphaT s
else
if s = "where" then WhereT else
if s = "with" then WithT else
if s = "withtype" then WithtypeT else
AlphaT s
else
SymbolT s
Proof
strip_tac >>
Cases_on `s` >>
simp_tac (srw_ss()) []
>- srw_tac [] [processIdent_def, get_token_def] >>
MAP_EVERY (fn c =>
Cases_on `h = ^c` >-
tac >>
full_simp_tac (srw_ss()) [])
[``#"a"``, ``#"c"``, ``#"d"``, ``#"e"``, ``#"f"``, ``#"h"``,
“#"i"”, ``#"l"``, ``#"o"``, ``#"r"``, ``#"s"``, ``#"t"``, ``#"w"``,
“#"v"”, ``#"'"``, ``#"."``, ``#":"``, ``#"-"``, ``#"="``, ``#"#"``,
“#"("”, ``#")"``, ``#"*"``, ``#","``, ``#";"``, ``#"|"``, ``#"["``,
“#"]"”, ``#"_"``, ``#"{"``, ``#"}"``] >>
full_simp_tac (srw_ss()) [get_token_def] >>
rw [processIdent_def, isAlphaNum_def, isAlpha_def, isDigit_def,
isLower_def, isUpper_def] >>
full_simp_tac (srw_ss()++ARITH_ss) [char_le_def, char_lt_def] >>
Cases_on `t` >>
rw []
QED
val _ = computeLib.add_persistent_funs(["get_token_eqn"]);
val unhex_alt_def = Define`
unhex_alt x = (if isHexDigit x then UNHEX x else 0n)`
val num_from_dec_string_alt_def = Define `num_from_dec_string_alt = s2n 10 unhex_alt`;
val num_from_hex_string_alt_def = Define `num_from_hex_string_alt = s2n 16 unhex_alt`;
val next_sym_alt_def = tDefine "next_sym_alt" `
(next_sym_alt "" _ = NONE) /\
(next_sym_alt (c::str) loc =
if c = #"\n" then (* skip new line *)
next_sym_alt str (next_line loc)
else if isSpace c then (* skip blank space *)
next_sym_alt str (next_loc 1 loc)
else if isDigit c then (* read number *)
if str ≠ "" ∧ c = #"0" ∧ HD str = #"w" then
if TL str = "" then SOME (ErrorS, Locs loc loc, "")
else if isDigit (HD (TL str)) then
let (n,rest) = read_while isDigit (TL str) [] in
SOME (WordS (num_from_dec_string_alt n),
Locs loc (next_loc (LENGTH n + 1) loc),
rest)
else if HD(TL str) = #"x" then
let (n,rest) = read_while isHexDigit (TL (TL str)) [] in
SOME (WordS (num_from_hex_string_alt n),
Locs loc (next_loc (LENGTH n + 2) loc),
rest)
else SOME (ErrorS, Locs loc loc, TL str)
else
if str ≠ "" ∧ c = #"0" ∧ HD str = #"x" then
let (n,rest) = read_while isHexDigit (TL str) [] in
SOME (NumberS (& num_from_hex_string_alt n),
Locs loc (next_loc (LENGTH n) loc),
rest)
else
let (n,rest) = read_while isDigit str [] in
SOME (NumberS (&(num_from_dec_string_alt (c::n))),
Locs loc (next_loc (LENGTH n) loc),
rest)
else if c = #"~" /\ str <> "" /\ isDigit (HD str) then (* read negative number *)
let (n,rest) = read_while isDigit str [] in
SOME (NumberS (0- &(num_from_dec_string_alt n)),
Locs loc (next_loc (LENGTH n) loc),
rest)
else if c = #"'" then (* read type variable *)
let (n,rest) = read_while isAlphaNumPrime str [c] in
SOME (OtherS n,
Locs loc (next_loc (LENGTH n - 1) loc),
rest)
else if c = #"\"" then (* read string *)
let (t, loc', rest) = read_string str "" (next_loc 1 loc) in
SOME (t, Locs loc loc', rest)
else if isPREFIX "*)" (c::str) then
SOME (ErrorS, Locs loc (next_loc 2 loc), TL str)
else if isPREFIX "#\"" (c::str) then
let (t, loc', rest) = read_string (TL str) "" (next_loc 2 loc) in
SOME (mkCharS t, Locs loc loc', rest)
else if isPREFIX "#(" (c::str) then
let (t, loc', rest) =
read_FFIcall (TL str) "" (next_loc 2 loc)
in
SOME (t, Locs loc loc', rest)
else if isPREFIX "(*" (c::str) then
case skip_comment (TL str) (0:num) (next_loc 2 loc) of
| NONE => SOME (ErrorS, Locs loc (next_loc 2 loc), "")
| SOME (rest, loc') => next_sym_alt rest loc'
else if is_single_char_symbol c then (* single character tokens, i.e. delimiters *)
SOME (OtherS [c], Locs loc loc, str)
else if isSymbol c then
let (n,rest) = read_while isSymbol str [c] in
SOME (OtherS n,
Locs loc (next_loc (LENGTH n - 1) loc),
rest)
else if isAlpha c then (* read identifier *)
let (n,rest) = read_while isAlphaNumPrime str [c] in
case rest of
#"."::rest' =>
(case rest' of
c'::rest' =>
if isAlpha c' then
let (n', rest'') = read_while isAlphaNumPrime rest' [c'] in
SOME (LongS (n ++ "." ++ n'),
Locs loc
(next_loc (LENGTH n + LENGTH n') loc),
rest'')
else if isSymbol c' then
let (n', rest'') = read_while isSymbol rest' [c'] in
SOME (LongS (n ++ "." ++ n'),
Locs loc
(next_loc (LENGTH n + LENGTH n') loc),
rest'')
else
SOME (ErrorS,
Locs loc (next_loc (LENGTH n) loc),
rest')
| "" => SOME (ErrorS,
Locs loc (next_loc (LENGTH n) loc),
[]))
| _ => SOME (OtherS n,
Locs loc (next_loc (LENGTH n - 1) loc),
rest)
else if c = #"_" then SOME (OtherS "_", Locs loc loc, str)
else (* input not recognised *)
SOME (ErrorS, Locs loc loc, str))`
( WF_REL_TAC `measure (LENGTH o FST) ` THEN REPEAT STRIP_TAC
THEN IMP_RES_TAC (GSYM read_while_thm)
THEN IMP_RES_TAC (GSYM read_string_thm)
THEN IMP_RES_TAC skip_comment_thm THEN Cases_on `str`
THEN FULL_SIMP_TAC (srw_ss()) [LENGTH] THEN DECIDE_TAC);
val EVERY_isDigit_imp = Q.prove(`
EVERY isDigit x ⇒
MAP UNHEX x = MAP unhex_alt x`,
rw[]>>match_mp_tac LIST_EQ>>fs[EL_MAP,EVERY_EL,unhex_alt_def,isDigit_def,isHexDigit_def])
val toNum_rw = Q.prove(`
∀x. EVERY isDigit x ⇒
toNum x = num_from_dec_string_alt x`,
rw[ASCIInumbersTheory.s2n_def,ASCIInumbersTheory.num_from_dec_string_def,num_from_dec_string_alt_def]>>
AP_TERM_TAC>>
match_mp_tac EVERY_isDigit_imp>>
metis_tac[rich_listTheory.EVERY_REVERSE])
val EVERY_isHexDigit_imp = Q.prove(`
EVERY isHexDigit x ⇒
MAP UNHEX x = MAP unhex_alt x`,
rw[]>>match_mp_tac LIST_EQ>>fs[EL_MAP,EVERY_EL,unhex_alt_def])
val num_from_hex_string_rw = Q.prove(`
∀x. EVERY isHexDigit x ⇒
num_from_hex_string x = num_from_hex_string_alt x`,
rw[ASCIInumbersTheory.s2n_def,ASCIInumbersTheory.num_from_hex_string_def,num_from_hex_string_alt_def]>>
AP_TERM_TAC>>
match_mp_tac EVERY_isHexDigit_imp>>
metis_tac[rich_listTheory.EVERY_REVERSE])
val EVERY_IMPLODE = Q.prove(`
∀ls P.
EVERY P (IMPLODE ls) ⇔ EVERY P ls`,
Induct>>fs[])
val read_while_P_lem = Q.prove(`
∀ls rest P x y.
EVERY P rest ∧
read_while P ls rest = (x,y) ⇒
EVERY P x`,
Induct>>fs[read_while_def]>>rw[]>>
fs[EVERY_IMPLODE,rich_listTheory.EVERY_REVERSE]>>
first_assum match_mp_tac>>fs[]>>
qexists_tac`STRING h rest`>>fs[])
Theorem read_while_P[local]:
∀ls P x y. read_while P ls "" = (x,y) ⇒ EVERY P x
Proof
rw[]>>ho_match_mp_tac read_while_P_lem>>
MAP_EVERY qexists_tac [`ls`,`""`,`y`]>>fs[]
QED
Theorem next_sym_eq:
∀x l. next_sym x l = next_sym_alt x l
Proof
ho_match_mp_tac next_sym_ind>>fs[next_sym_def,next_sym_alt_def]>>rw[]>>
TRY(BasicProvers.TOP_CASE_TAC>>fs[]>>NO_TAC)>>
TRY(rpt(pop_assum mp_tac)>> EVAL_TAC>> simp[]>>NO_TAC)>>
TRY(pairarg_tac) >>fs[]>>
TRY(qmatch_goalsub_abbrev_tac`num_from_hex_string _ = _` >>
match_mp_tac num_from_hex_string_rw)>>
TRY(qmatch_goalsub_abbrev_tac`toNum _ = _` >>match_mp_tac toNum_rw)>>
TRY(fs[]>>
ho_match_mp_tac read_while_P>>
metis_tac[]) >>
every_case_tac >> metis_tac[]
QED
(* lex_until_toplevel_semicolon *)
Definition lex_aux_def:
lex_aux acc (d:num) input loc =
case next_token input loc of
| (* case: end of input *)
NONE => NONE
| (* case: token found *)
SOME (token, Locs loc' loc'', rest) =>
let new_acc = ((token, Locs loc' loc'')::acc) in
let newloc = next_loc 1 loc'' in
if token = SemicolonT /\ (d = 0) then SOME (REVERSE new_acc, newloc, rest)
else
if token = LetT then lex_aux new_acc (d + 1) rest newloc
else if token = StructT then lex_aux new_acc (d + 1) rest newloc
else if token = SigT then lex_aux new_acc (d + 1) rest newloc
else if token = LparT then lex_aux new_acc (d + 1) rest newloc
else if token = EndT then lex_aux new_acc (d - 1) rest newloc
else if token = RparT then lex_aux new_acc (d - 1) rest newloc
else lex_aux new_acc d rest newloc
Termination
WF_REL_TAC `measure (LENGTH o FST o SND o SND)` >> rw[] >>
imp_res_tac next_token_LESS
End
val lex_until_toplevel_semicolon_def = Define `
lex_until_toplevel_semicolon input = lex_aux [] 0 input`;
val lex_aux_LESS = Q.prove(
`!acc d input l.
(lex_aux acc d input l = SOME (ts, l', rest)) ==>
if acc = [] then LENGTH rest < LENGTH input
else LENGTH rest <= LENGTH input`,
HO_MATCH_MP_TAC (fetch "-" "lex_aux_ind")
THEN REPEAT STRIP_TAC THEN POP_ASSUM MP_TAC
THEN ONCE_REWRITE_TAC [lex_aux_def]
THEN Cases_on `next_token input l` THEN FULL_SIMP_TAC (srw_ss()) []
THEN every_case_tac
THEN FULL_SIMP_TAC (srw_ss()) [LET_DEF]
THEN SRW_TAC [] [] THEN IMP_RES_TAC next_token_LESS
THEN FULL_SIMP_TAC std_ss [] THEN RES_TAC
THEN IMP_RES_TAC arithmeticTheory.LESS_TRANS
THEN TRY (Cases_on `h`)
THEN FULL_SIMP_TAC (srw_ss()) []
THEN RES_TAC THEN IMP_RES_TAC arithmeticTheory.LESS_EQ_LESS_TRANS
THEN IMP_RES_TAC (DECIDE ``n < m ==> n <= m:num``)
THEN DECIDE_TAC);
Theorem lex_until_toplevel_semicolon_LESS:
(lex_until_toplevel_semicolon input l = SOME (ts, l', rest)) ==>
LENGTH rest < LENGTH input
Proof
SIMP_TAC std_ss [lex_until_toplevel_semicolon_def]
THEN REPEAT STRIP_TAC THEN IMP_RES_TAC lex_aux_LESS
THEN FULL_SIMP_TAC std_ss []
QED
(* lex_until_toplevel_semicolon_alt *)
open rich_listTheory
Definition lex_aux_alt_def:
lex_aux_alt acc (d:num) input l =
case next_sym input l of
| (* case: end of input *)
NONE => NONE
| (* case: token found *)
SOME (token, Locs loc' loc'', rest) =>
let new_acc = ((token, Locs loc' loc'')::acc) in
let newloc = next_loc 1 loc'' in
if (token = OtherS ";") /\ (d = 0) then SOME (REVERSE new_acc, newloc, rest)
else if MEM token [OtherS "let"; OtherS "struct";
OtherS "sig"; OtherS "("] then
lex_aux_alt new_acc (d + 1) rest newloc
else if MEM token [OtherS ")"; OtherS "end"] then
lex_aux_alt new_acc (d - 1) rest newloc
else lex_aux_alt new_acc d rest newloc
Termination
WF_REL_TAC `measure (LENGTH o FST o SND o SND)` >> rw[] >>
imp_res_tac next_sym_LESS
End
val lex_until_top_semicolon_alt_def = Define `
lex_until_top_semicolon_alt input = lex_aux_alt [] 0 input`
val lex_aux_alt_LESS = Q.prove(
`!acc d input l.
(lex_aux_alt acc d input l = SOME (ts, l', rest)) ==>
if acc = [] then LENGTH rest < LENGTH input
else LENGTH rest <= LENGTH input`,
HO_MATCH_MP_TAC (fetch "-" "lex_aux_alt_ind")
THEN REPEAT STRIP_TAC THEN POP_ASSUM MP_TAC
THEN ONCE_REWRITE_TAC [lex_aux_alt_def]
THEN Cases_on `next_sym inputi l` THEN FULL_SIMP_TAC (srw_ss()) []
THEN every_case_tac
THEN FULL_SIMP_TAC (srw_ss()) [LET_DEF]
THEN SRW_TAC [] [] THEN IMP_RES_TAC next_sym_LESS
THEN FULL_SIMP_TAC std_ss [] THEN RES_TAC
THEN IMP_RES_TAC arithmeticTheory.LESS_TRANS
THEN TRY (Cases_on `h`) THEN FULL_SIMP_TAC (srw_ss()) []
THEN RES_TAC THEN IMP_RES_TAC arithmeticTheory.LESS_EQ_LESS_TRANS
THEN IMP_RES_TAC (DECIDE ``n < m ==> n <= m:num``)
THEN DECIDE_TAC);
Theorem lex_until_top_semicolon_alt_LESS:
(lex_until_top_semicolon_alt input l = SOME (ts, l', rest)) ==>
LENGTH rest < LENGTH input
Proof
SIMP_TAC std_ss [lex_until_top_semicolon_alt_def]
THEN REPEAT STRIP_TAC THEN IMP_RES_TAC lex_aux_alt_LESS
THEN FULL_SIMP_TAC std_ss []
QED
Theorem token_of_sym_EQ_LEMMA[local]:
((token_of_sym q = LetT) = (q = OtherS "let")) /\
((token_of_sym q = EndT) = (q = OtherS "end")) /\
((token_of_sym q = SigT) = (q = OtherS "sig")) /\
((token_of_sym q = StructT) = (q = OtherS "struct")) /\
((token_of_sym q = SemicolonT) = (q = OtherS ";")) /\
((token_of_sym q = RparT) = (q = OtherS ")")) /\
((token_of_sym q = LparT) = (q = OtherS "("))
Proof
REPEAT STRIP_TAC THEN
simp[token_of_sym_def,get_token_def,processIdent_def,LET_DEF,
AllCaseEqs(), UNCURRY]
QED
val token_of_sym_loc_def = Define`
token_of_sym_loc (t, l) = (token_of_sym t, l)`;
val lex_aux_alt_thm = Q.prove(
`!acc d input l.
case lex_aux_alt acc d input l of
| NONE => (lex_aux (MAP token_of_sym_loc acc) d input l = NONE)
| SOME (ts, rest) => (lex_aux (MAP token_of_sym_loc acc) d input l =
SOME (MAP token_of_sym_loc ts,rest))`,
HO_MATCH_MP_TAC (fetch "-" "lex_aux_alt_ind") THEN REPEAT STRIP_TAC
THEN ONCE_REWRITE_TAC [lex_aux_alt_def,lex_aux_def]
THEN SIMP_TAC std_ss [next_token_def]
THEN Cases_on `next_sym input l` THEN1 EVAL_TAC
THEN every_case_tac THEN FULL_SIMP_TAC (srw_ss()) [LET_DEF]
THEN FULL_SIMP_TAC std_ss [token_of_sym_EQ_LEMMA]
THEN SRW_TAC [] [] THEN FULL_SIMP_TAC (srw_ss()) [rich_listTheory.MAP_REVERSE]
THEN FULL_SIMP_TAC (srw_ss()) [token_of_sym_loc_def,token_of_sym_def,get_token_def])
|> Q.SPECL [`[]`,`0`] |> SIMP_RULE std_ss [MAP] ;
Theorem lex_until_top_semicolon_alt_thm:
case lex_until_top_semicolon_alt input l of
| NONE => (lex_until_toplevel_semicolon input l = NONE)
| SOME (ts,rest) =>
(lex_until_toplevel_semicolon input l = SOME (MAP token_of_sym_loc ts,rest))
Proof
SIMP_TAC std_ss [lex_until_top_semicolon_alt_def,
lex_until_toplevel_semicolon_def,lex_aux_alt_thm]
QED
(* lex_impl_all *)
val lex_impl_all_def = tDefine "lex_impl_all" `
lex_impl_all input l =
case lex_until_toplevel_semicolon input l of
| NONE => []
| SOME (t, loc, input') => t ::lex_impl_all input' loc`
(WF_REL_TAC `measure (LENGTH o FST)` >>
rw [] >>
metis_tac [lex_until_toplevel_semicolon_LESS]);
val lex_aux_tokens_def = Define `
lex_aux_tokens acc (d:num) input =
case input of
[] => NONE
| (token, locs)::rest =>
let new_acc = (token, locs)::acc in
if token = SemicolonT /\ (d = 0) then
SOME (REVERSE (new_acc),rest)
else
if token = LetT then
lex_aux_tokens new_acc (d+1) rest
else if token = StructT then
lex_aux_tokens new_acc (d+1) rest
else if token = SigT then
lex_aux_tokens new_acc (d+1) rest
else if token = LparT then
lex_aux_tokens new_acc (d+1) rest
else if token = EndT then
lex_aux_tokens new_acc (d-1) rest
else if token = RparT then
lex_aux_tokens new_acc (d-1) rest
else lex_aux_tokens new_acc d rest`
val lex_until_toplevel_semicolon_tokens_def = Define `
lex_until_toplevel_semicolon_tokens input = lex_aux_tokens [] 0 input`;
val lex_aux_tokens_LESS = Q.prove(
`!acc d input.
(lex_aux_tokens acc d input = SOME (t,rest)) ==>
(if acc = [] then LENGTH rest < LENGTH input
else LENGTH rest <= LENGTH input)`,
Induct_on `input`
THEN1 (EVAL_TAC >> SRW_TAC [] [LENGTH] >> SRW_TAC [] [LENGTH])
>> SIMP_TAC (srw_ss()) [Once lex_aux_tokens_def,LET_DEF]
>> SRW_TAC [] [] >> RES_TAC
>> FULL_SIMP_TAC std_ss [NOT_CONS_NIL]
>> TRY (Cases_on `h`) >> RES_TAC
>> FULL_SIMP_TAC (srw_ss()) [] >> RES_TAC
>> FULL_SIMP_TAC (srw_ss()) [] >> RES_TAC
>> Cases_on `q` >> Cases_on `d` >> fs[] >> res_tac >> fs[]);
val lex_impl_all_tokens_def = tDefine "lex_impl_all_tokens" `
lex_impl_all_tokens input =
case lex_until_toplevel_semicolon_tokens input of
NONE => []
| SOME (t,input) => t::lex_impl_all_tokens input`
(WF_REL_TAC `measure LENGTH`
>> SIMP_TAC std_ss [lex_until_toplevel_semicolon_tokens_def]
>> METIS_TAC [lex_aux_tokens_LESS])
val lex_aux_tokens_thm = Q.prove(
`!input l acc d res1 res2.
(lex_aux_tokens acc d (lexer_fun_aux input l) = res1) /\
(lex_aux acc d input l = res2) ==>
(case res2 of NONE => (res1 = NONE)
| SOME (ts, l', rest) =>
(res1 = SOME (ts, lexer_fun_aux rest l')))`,
HO_MATCH_MP_TAC lexer_fun_aux_ind >> SIMP_TAC std_ss []
>> REPEAT STRIP_TAC >> SIMP_TAC std_ss [Once lex_aux_def]
>> ONCE_REWRITE_TAC [lexer_fun_aux_def]
>> ONCE_REWRITE_TAC [lex_aux_tokens_def]
>> Cases_on `next_token input l ` >> ASM_SIMP_TAC (srw_ss()) []
>> Cases_on `x`
>> Q.MATCH_ASSUM_RENAME_TAC `next_token input l = SOME (t,rest)`
>> Cases_on `rest`
>> Cases_on `q`
>> FULL_SIMP_TAC (srw_ss()) []
>> SRW_TAC [] [] >> SRW_TAC [] []
>> ASM_SIMP_TAC std_ss [GSYM lexer_fun_aux_def]) |> SIMP_RULE std_ss [];
val lex_impl_all_tokens_thm = Q.prove(
`!input l. lex_impl_all input l =
lex_impl_all_tokens (lexer_fun_aux input l)`,
HO_MATCH_MP_TAC (fetch "-" "lex_impl_all_ind") >> REPEAT STRIP_TAC
>> SIMP_TAC std_ss [Once lex_impl_all_def,Once lex_impl_all_tokens_def]
>> fs [lex_until_toplevel_semicolon_tokens_def]
>> fs [lex_until_toplevel_semicolon_def]
>> MP_TAC (lex_aux_tokens_thm |> Q.SPECL [`input`, `l`, `[]`,`0`])
>> Cases_on `lex_aux [] 0 input l` >> fs[]
>> Cases_on `x` >> Cases_on `r` >> fs[]);
val lex_aux_tokens_thm = Q.prove(
`!input d acc.
(res = lex_aux_tokens acc d input) ==>
case res of
NONE => (toplevel_semi_dex (LENGTH acc) d input = NONE)
| SOME (toks,rest) =>
(toplevel_semi_dex (LENGTH acc) d input = SOME (LENGTH toks)) /\
(REVERSE acc ++ input = toks ++ rest)`,
Induct
>> SIMP_TAC (srw_ss()) [Once lex_aux_tokens_def]
>> ONCE_REWRITE_TAC [toplevel_semi_dex_def]
>> SIMP_TAC std_ss [LET_DEF] >> Cases
>> FULL_SIMP_TAC (srw_ss()) []
>> REPEAT STRIP_TAC >> RES_TAC
>> POP_ASSUM MP_TAC
>> POP_ASSUM (ASSUME_TAC o GSYM)
>> ASM_REWRITE_TAC []
>> Cases_on `res` >> SIMP_TAC (srw_ss()) [arithmeticTheory.ADD1]
>> Cases_on `d = 0` >> ASM_SIMP_TAC (srw_ss()) [arithmeticTheory.ADD1]
>> Cases_on `q` >> fs[arithmeticTheory.ADD1]
>> TRY (Cases_on `x`) >> fs [arithmeticTheory.ADD1]
>> SIMP_TAC std_ss [Once EQ_SYM_EQ]
>> FULL_SIMP_TAC std_ss []
>> REPEAT STRIP_TAC >> RES_TAC
>> fs [Once toplevel_semi_dex_def,LENGTH,arithmeticTheory.ADD1])
|> Q.SPECL [`input`,`0`,`[]`] |> Q.GEN `res` |> SIMP_RULE std_ss [LENGTH];
val split_top_level_semi_thm = Q.prove(
`!input. split_top_level_semi input = lex_impl_all_tokens input`,
HO_MATCH_MP_TAC split_top_level_semi_ind >> REPEAT STRIP_TAC
>> SIMP_TAC std_ss [Once split_top_level_semi_def,Once lex_impl_all_tokens_def,
Once lex_until_toplevel_semicolon_tokens_def]
>> MP_TAC lex_aux_tokens_thm
>> Cases_on `lex_aux_tokens [] 0 input` >> FULL_SIMP_TAC std_ss []
>> Cases_on `x` >> FULL_SIMP_TAC (srw_ss()) []
>> FULL_SIMP_TAC std_ss [TAKE_LENGTH_APPEND,DROP_LENGTH_APPEND]
>> STRIP_TAC >> RES_TAC >> POP_ASSUM MP_TAC
>> FULL_SIMP_TAC std_ss [TAKE_LENGTH_APPEND,DROP_LENGTH_APPEND]);
Theorem lexer_correct:
!input. split_top_level_semi (lexer_fun_aux input l) = lex_impl_all input l
Proof
SIMP_TAC std_ss [lex_impl_all_tokens_thm,split_top_level_semi_thm]
QED
val _ = export_theory();