forked from jrh13/hol-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiz3.ml
1890 lines (1755 loc) · 67.8 KB
/
miz3.ml
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
needs "Examples/holby.ml";;
let horizon = ref 1;;
let timeout = ref 1;;
let default_prover = ref ("HOL_BY", CONV_TAC o HOL_BY);;
let renumber_labels = ref true;;
let extra_labels = ref 0;;
let start_label = ref 1;;
let growth_mode = ref true;;
let proof_indent = ref " ";;
let proof_width = ref 72;;
let grow_haves = ref true;;
let grow_duplicates = ref 0;;
let indent_continued = ref false;;
let sketch_mode = ref false;;
let silent_server = ref 1;;
let explain_errors = ref 1;;
let miz3_pid = ref "/tmp/miz3_pid";;
let miz3_filename = ref "/tmp/miz3_filename";;
let ERRORS =
["1: inference error";
"2: inference time-out";
"3: skeleton error";
"4: unknown label";
"5: error ocaml (or justification)";
"6: underspecified types hol";
"7: unbound free variables hol";
"8: syntax or type error hol";
"9: syntax error mizar"];;
let mizar_step_words =
["assume"; "cases"; "case"; "consider"; "end"; "let"; "now"; "proof";
"qed"; "set"; "suppose"; "take"; "thus"];;
let mizar_step_words = mizar_step_words @
["exec"];;
let mizar_words = mizar_step_words @
["be"; "being"; "by"; "from"; "such"; "that"];;
let mizar_skip_bracketed =
[","; ";"; "["];;
reserve_words (subtract mizar_words (reserved_words()));;
type by_item =
| Label of string
| Thm of string * thm
| Tactic of string * (thm list -> tactic)
| Grow of string * (thm list -> tactic)
| Hole;;
type step =
int * (string * lexcode * string) list list * substep
and substep =
| Have of term * string list * just
| Now of string list * just
| Let of term list
| Assume of term * string list
| Thus of term * string list * just
| Qed of just
| Bracket_proof
| Bracket_end
| Take of term
| Consider of term list * term * string list * just
| Set of term * string list
| Cases of just * just list
| Bracket_case
| Suppose of term * string list
| Exec of string * tactic
| Error of string * just
| Error_point
| Empty_step
and just =
| By of by_item list * by_item list * bool
| Proof of step option * step list * step option
| Proof_expected of bool
| No_steps;;
unset_jrh_lexer;;
let system_ok = Unix.WEXITED 0;;
let wronly = Unix.O_WRONLY;;
let usr2_handler = ref (fun () -> print_string "usr2_handler\n");;
Sys.signal Sys.sigusr2 (Sys.Signal_handle (fun _ -> !usr2_handler ()));;
set_jrh_lexer;;
let rawtoken =
let collect (h,t) = end_itlist (^) (h::t) in
let stringof p = atleast 1 p >> end_itlist (^) in
let simple_ident = stringof(some isalnum) ||| stringof(some issymb) in
let undertail = stringof (a "_") ++ possibly simple_ident >> collect in
let ident = (undertail ||| simple_ident) ++ many undertail >> collect in
let septok = stringof(some issep) in
let stringchar =
some (fun i -> i <> "\\" && i <> "\"")
||| (a "\\" ++ some (fun _ -> true) >> fun (_,x) -> "\\"^x) in
let string = a "\"" ++ many stringchar ++ ((a "\"" >> K 0) ||| finished) >>
(fun ((_,s),_) -> "\""^implode s^"\"") in
(string ||| some isbra ||| septok ||| ident ||| a "`");;
let rec whitespace e i =
let non_newline i =
if i <> [] && hd i <> "\n" then hd i,tl i else raise Noparse in
let rest_of_line = many non_newline ++ (a "\n" ||| (finished >> K "")) >>
fun x,y -> itlist (^) x y in
let comment_string =
match !comment_token with
| Resword t -> t
| Ident t -> t in
match i with
| [] -> if e then "",i else raise Noparse
| (" " as c)::rst | ("\t" as c)::rst | ("\r" as c)::rst ->
let s,rst1 = whitespace true rst in c^s,rst1
| ("\n" as c)::rst -> c,rst
| _ ->
let t,rst = rawtoken i in
if t = comment_string then (rest_of_line >> fun x -> t^x) rst
else if String.length t >= 2 && String.sub t 0 2 = "::" then
(rest_of_line >> fun x -> if e then t^x else "") rst
else if e then "",i else raise Noparse;;
let lex1 =
let reserve1 n =
if is_reserved_word n then Resword n else Ident n in
let rec tokens i =
try (many (whitespace false) ++ rawtoken ++ whitespace true
++ tokens >>
fun (((x,y),z),w) -> (implode x,reserve1 y,z)::w) i
with Noparse -> [],i in
fun l ->
let (toks,rst) = tokens l in
let rst',rst'' = many (whitespace false) rst in
if rst'' <> [] then failwith "lex1" else
if toks = [] then toks else
let (x,y,z) = last toks in
butlast toks@[x,y,z^implode rst'];;
let lex2 = lex1 o explode;;
let middle (_,x,_) = x;;
let a' t toks =
match toks with
| ((_,Resword t',_) as tok)::rst when t = t' -> tok,rst
| ((_,Ident t',_) as tok)::rst when t = t' -> tok,rst
| _ -> raise Noparse;;
let a_semi = a' ";";;
let ident' toks =
match toks with
| (_,Ident s,_)::rst -> s,rst
| (_,Resword "(",_)::(_,Ident s,_)::(_,Resword ")",_)::rst -> s,rst
| _ -> raise Noparse;;
let unident' s =
if parses_as_binder s || can get_infix_status s || is_prefix s
then ["",Resword "(",""; "",Ident s,""; "",Resword ")",""]
else ["",Ident s,""];;
let rec cut_to b n c l toks =
match toks with
| [] -> if b then [],[] else raise Noparse
| tok::rst ->
(match tok with
| _,Resword s,_ | _,Ident s,_ ->
let x = not (n > 0 && mem s mizar_skip_bracketed) in
if mem s c && x then [tok],rst else
if b && mem s l && x then [],toks else
let stp1,rst1 =
(match s with
| "(" | "[" -> cut_to true (n + 1) c l rst
| ")" | "]" -> cut_to true (if n > 0 then n - 1 else 0) c l rst
| _ -> cut_to true n c l rst) in
(tok::stp1),rst1);;
let cut_step toks =
match toks with
| (_,Resword "proof",_ as tok)::rst -> [tok],rst
| (_,Resword "now",_)::rst ->
(a' "now" ++
(many (a' "[" ++ cut_to false 0 ["]"] mizar_step_words >>
fun x,y -> x::y)) >> fun x,y -> x::(itlist (@) y [])) toks
| _ -> cut_to false 0 [";"] mizar_step_words toks;;
let rec cut_steps toks =
let steps,rst = many cut_step toks in
if rst = [] then steps else steps@[rst];;
let strings_of_toks toks =
let rec string_of_toks1 toks =
match toks with
| [] -> "",""
| [x,Ident y,z] | [x,Resword y,z] -> x^y,z
| (x,Ident y,z)::rst | (x,Resword y,z)::rst ->
let u,v = string_of_toks1 rst in x^y^z^u,v in
match toks with
| [] -> "","",""
| [x,Ident y,z] | [x,Resword y,z] -> x,y,z
| (x,Ident y,z)::rst | (x,Resword y,z)::rst ->
let u,v = string_of_toks1 rst in x,y^z^u,v;;
let string_of_toks = middle o strings_of_toks;;
let split_string = map string_of_toks o cut_steps o lex2;;
let tok_of_toks toks =
let x,y,z = strings_of_toks toks in
x,Ident y,z;;
let exec_phrase b s =
let lexbuf = Lexing.from_string s in
let ok = Toploop.execute_phrase b Format.std_formatter
(!Toploop.parse_toplevel_phrase lexbuf) in
Format.pp_print_flush Format.std_formatter ();
(ok,
let i = lexbuf.Lexing.lex_curr_pos in
String.sub lexbuf.Lexing.lex_buffer
i (lexbuf.Lexing.lex_buffer_len - i));;
let exec_thm_out = ref TRUTH;;
let exec_thm s =
try
let ok,rst = exec_phrase false
("exec_thm_out := (("^s^") : thm);;") in
if not ok || rst <> "" then raise Noparse;
!exec_thm_out
with _ -> raise Noparse;;
let exec_thmlist_tactic_out = ref REWRITE_TAC;;
let exec_thmlist_tactic s =
try
let ok,rst = exec_phrase false
("exec_thmlist_tactic_out := (("^s^") : thm list -> tactic);;") in
if not ok || rst <> "" then raise Noparse;
!exec_thmlist_tactic_out
with _ -> raise Noparse;;
let exec_thmtactic_out = ref MATCH_MP_TAC;;
let exec_thmtactic s =
try
let ok,rst = exec_phrase false
("exec_thmtactic_out := (("^s^") : thm -> tactic);;") in
if not ok || rst <> "" then raise Noparse;
!exec_thmtactic_out
with _ -> raise Noparse;;
let exec_tactic_out = ref ALL_TAC;;
let exec_tactic s =
try
let ok,rst = exec_phrase false
("exec_tactic_out := (("^s^") : tactic);;") in
if not ok || rst <> "" then raise Noparse;
!exec_tactic_out
with _ -> raise Noparse;;
let exec_conv_out = ref NUM_REDUCE_CONV;;
let exec_conv s =
try
let ok,rst = exec_phrase false
("exec_conv_out := (("^s^") : conv);;") in
if not ok || rst <> "" then raise Noparse;
!exec_conv_out
with _ -> raise Noparse;;
let (MP_ALL : tactic -> thm list -> tactic) =
fun tac ths ->
MAP_EVERY MP_TAC ths THEN tac;;
let use_thms tac =
fun ths -> tac ORELSE MP_ALL tac ths;;
let by_item_cache = ref undefined;;
let rec by_item_of_toks toks =
match toks with
| [_,Ident "#",_] -> Hole
| (_,Ident "#",_)::toks' ->
(match by_item_of_toks toks' with
| Tactic(s,tac) -> Grow(s,tac)
| _ -> failwith "by_item_of_toks")
| [_,Ident "*",_] -> Label "*"
| _ ->
let s = string_of_toks toks in
try apply (!by_item_cache) s with _ ->
let i =
try Thm (s, exec_thm s) with _ ->
try Tactic (s, exec_thmlist_tactic s) with _ ->
try Tactic (s, (exec_thmtactic s) o hd) with _ ->
try Tactic (s, use_thms (exec_tactic s)) with _ ->
try Tactic (s, use_thms (CONV_TAC (exec_conv s))) with _ ->
match toks with
| [_,Ident s,_] -> Label s
| _ -> failwith "by_item_of_toks" in
by_item_cache := (s |-> i) !by_item_cache;
i;;
let parse_by =
let parse_by_item toks =
match toks with
| (_,Ident "#",_ as tok1)::(_,Ident s,_ as tok2)::toks when s <> "," ->
[tok1;tok2],toks
| (_,Ident _,_ as tok)::toks -> [tok],toks
| _ -> raise Noparse in
let parse_by_part =
((a' "by" ++ many (parse_by_item ++ a' "," >> fst) >> snd) ++
parse_by_item) >>
(fun (x,y) -> x@[y])
||| (nothing >> K [])
and parse_from_part =
((a' "from" ++ many (parse_by_item ++ a' "," >> fst) >> snd) ++
parse_by_item) >>
(fun (x,y) -> (x@[y]),true)
||| (nothing >> K ([],false)) in
let rec will_grow l =
match l with
| [] -> false
| Tactic _::_ -> false
| Grow _::_ -> true
| _::l' -> will_grow l'
in
((parse_by_part ++ parse_from_part) ++ a_semi ++ finished >>
fun (((x,(y,z)),_),_) ->
let x' = map by_item_of_toks x in
let y' = map by_item_of_toks y in
By(x',y',z || will_grow (x'@y')))
||| (finished >> K (Proof_expected true));;
let rec parse_labels toks =
match toks with
| [] -> []
| (_,Resword "[",_)::(_,Ident s,_)::(_,Resword "]",_)::rst ->
s::(parse_labels rst)
| _ -> raise Noparse;;
let rec type_of_pretype1 ty =
match ty with
Stv n -> failwith "type_of_pretype1"
| Utv(v) -> mk_vartype(v)
| Ptycon(con,args) -> mk_type(con,map type_of_pretype1 args);;
let term_of_preterm1 =
let rec term_of_preterm1 ptm =
match ptm with
Varp(s,pty) -> mk_var(s,type_of_pretype1 pty)
| Constp(s,pty) -> mk_mconst(s,type_of_pretype1 pty)
| Combp(l,r) -> mk_comb(term_of_preterm1 l,term_of_preterm1 r)
| Absp(v,bod) -> mk_gabs(term_of_preterm1 v,term_of_preterm1 bod)
| Typing(ptm,pty) -> term_of_preterm1 ptm in
fun ptm -> term_of_preterm1 ptm;;
let term_of_hol b =
let error = mk_var("error",`:error`) in
let term_of_hol1 env toks =
let env' = ("thesis",Ptycon("bool",[]))::
(map ((fun (s,ty) -> s,pretype_of_type ty) o dest_var) env) in
try
let ptm,l = (parse_preterm o map middle) toks in
if l <> [] then (8,error) else
try
let tm = (term_of_preterm1 o retypecheck env') ptm in
if not (subset
(filter
(fun v -> not (mem (fst (dest_var v)) ["..."; "thesis"]))
(frees tm)) env)
then (7,error) else
if b && type_of tm <> bool_ty then (8,error) else
(0,tm)
with _ ->
let tiw = !type_invention_warning in
type_invention_warning := false;
let tm =
try (term_of_preterm o retypecheck env') ptm
with e -> type_invention_warning := tiw; raise e in
type_invention_warning := tiw;
if not (subset (frees tm) env) then (7,error) else (6,error)
with _ -> (8,error) in
fun env toks ->
match toks with
| (x,Ident ".=",y)::rest ->
term_of_hol1 env ((x,Ident "..."," ")::("",Ident "=",y)::rest)
| _ -> term_of_hol1 env toks;;
let type_of_hol =
let error = `:error` in
fun toks ->
try (0,(parse_type o middle o strings_of_toks) toks)
with _ -> (8,error);;
let split_step toks =
let cut_semi toks =
match toks with
| (_,Resword ";",_ as tok)::rst -> rev rst,[tok]
| _ -> rev toks,[] in
let rec cut_by_part rev_front toks =
match toks with
| [] | (_,Resword "by",_)::_ | (_,Resword "from",_)::_ -> rev_front,toks
| tok::rst -> cut_by_part (tok::rev_front) rst in
let rec group_by_items toks =
match toks with
| [] -> []
| (_,Resword "by",_ as tok)::rst
| (_,Resword "from",_ as tok)::rst
| (_,Ident ",",_ as tok)::rst
| (_,Resword ";",_ as tok)::rst -> tok::group_by_items rst
| (_,Ident "#",_ as tok)::toks' ->
let toks1,toks2 =
if toks' = [] then [],[]
else cut_to false 0 [] ([","; ";"]@mizar_words) toks' in
tok::(if toks1 = [] then [] else [tok_of_toks toks1])@
group_by_items toks2
| tok::rst ->
let toks1,toks2 = cut_to false 0 [] ([","; ";"]@mizar_words) toks in
if toks1 = [] then tok::group_by_items rst else
(tok_of_toks toks1)::group_by_items toks2 in
let rec cut_labs toks labs =
match toks with
| (_,Resword "]",_ as tok1)::(_,Ident _,_ as tok2)::
(_,Resword "[",_ as tok3)::rst ->
cut_labs rst (tok3::tok2::tok1::labs)
| _ -> toks,labs in
let rec cut_front toks tail =
match toks with
| [] -> [],tail
| (_,Resword s,_)::rst when mem s mizar_words -> rev toks,tail
| tok::rst -> cut_front rst (tok::tail) in
let toks1,semi_part = cut_semi (rev toks) in
let toks2,by_part = cut_by_part [] toks1 in
let toks3,labs_part = cut_labs toks2 [] in
let front_part,hol_part = cut_front toks3 [] in
if front_part <> [] && middle (hd front_part) = Resword "exec" then
let ml_tok = tok_of_toks ((tl front_part)@hol_part@labs_part@by_part) in
[[hd front_part]; [ml_tok]; []; []; semi_part]
else
[front_part; hol_part; labs_part; group_by_items by_part; semi_part];;
let parse_step env toks =
let src = split_step toks in
try
match src with
| [front_part; hol_part; labs_part; by_part; semi_part] ->
let labs = parse_labels labs_part in
let just,_ = parse_by (by_part@semi_part) in
(match front_part with
| [] ->
(match toks with
| [_,Resword ";",_] ->
-1,src,Empty_step
| _ ->
let n,t = term_of_hol true env hol_part in
if n <> 0 then n,src,Error(string_of_toks toks,just) else
-1,src,Have(t,labs,just))
| (_,Resword key,_)::_ ->
(match key,(tl front_part),(string_of_toks semi_part) with
| "now",[],"" ->
if hol_part <> [] || by_part <> [] then raise Noparse else
-1,src,Now(labs,Proof_expected false)
| "let",rst,";" ->
if labs_part <> [] || by_part <> [] then raise Noparse else
let x = (fst o fst o fst o
many ident' ++ a' "be" ++ finished) rst in
let n,t = type_of_hol hol_part in
if n <> 0 then n,src,Error(string_of_toks toks,No_steps) else
-1,src,Let(map (fun s -> mk_var(s,t)) x)
| "assume",[],";" ->
if by_part <> [] then raise Noparse else
let n,t = term_of_hol true env hol_part in
if n <> 0 then n,src,Error(string_of_toks toks,No_steps) else
-1,src,Assume(t,labs)
| "thus",[],_ ->
let n,t = term_of_hol true env hol_part in
if n <> 0 then n,src,Error(string_of_toks toks,just) else
-1,src,Thus(t,labs,just)
| "qed",[],_ ->
if hol_part <> [] || labs_part <> [] then raise Noparse else
-1,src,Qed just
| "proof",[],"" ->
if hol_part <> [] || labs_part <> [] || by_part <> [] then
raise Noparse else
-1,src,Bracket_proof
| "end",[],";" ->
if hol_part <> [] || labs_part <> [] || by_part <> [] then
raise Noparse else
-1,src,Bracket_end
| "take",[],";" ->
if labs_part <> [] || by_part <> [] then raise Noparse else
let n,t = term_of_hol false env hol_part in
if n <> 0 then n,src,Error(string_of_toks toks,No_steps) else
-1,src,Take t
| "consider",rst,_ ->
let cut_suchthat toks =
match toks with
| (_,Resword "that",_)::(_,Resword "such",_)::rst -> rst
| _ -> raise Not_found in
let rec cut_being toks tail =
match toks with
| [] -> raise Not_found
| (_,Resword "being",_)::rst -> (rev rst),(rev tail)
| tok::rst -> cut_being rst (tok::tail) in
(try
let rst1,rst2 = cut_being (cut_suchthat (rev rst)) [] in
let n,t = type_of_hol rst2 in
if n <> 0 then n,src,Error(string_of_toks toks,just) else
let x = (fst o fst o many ident' ++ finished) rst1 in
let vars = map (fun s -> mk_var(s,t)) x in
let n,tm' = term_of_hol true (vars@env) hol_part in
if n <> 0 then n,src,Error(string_of_toks toks,just) else
-1,src,Consider(vars,tm',labs,just)
with Not_found ->
let x = (fst o fst o fst o fst o
many ident' ++ a' "such" ++ a' "that" ++ finished) rst in
let xy = (("",Ident "?","")::((flat (map unident' x))@
(("",Resword ".","")::hol_part))) in
let n,tm = term_of_hol true env xy in
if n <> 0 then n,src,Error(string_of_toks toks,just) else
let vars,tm' = nsplit dest_exists x tm in
-1,src,Consider(vars,tm',labs,just))
| "set",[],";" ->
if by_part <> [] then raise Noparse else
let (w,_),rst = (ident' ++ a' "=") hol_part in
let n,t = term_of_hol false env rst in
if n <> 0 then n,src,Error(string_of_toks toks,No_steps) else
-1,src,Set(mk_eq(mk_var(w,type_of t),t),labs)
| "cases",[],_ ->
if hol_part <> [] || labs_part <> [] then raise Noparse else
-1,src,Cases(just,[])
| "case",[],";" ->
if hol_part <> [] || labs_part <> [] || by_part <> [] then
raise Noparse else
-1,src,Bracket_case
| "suppose",[],";" ->
if by_part <> [] then raise Noparse else
let n,t = term_of_hol true env hol_part in
if n <> 0 then
n,src,Error(string_of_toks toks,Proof_expected false) else
-1,src,Suppose(t,labs)
| "exec",[],";" ->
let s = string_of_toks hol_part in
-1,src,Exec(s,exec_tactic s)
| _ -> raise Noparse)
| _ -> raise Noparse)
| _ -> raise Noparse
with
| Failure "by_item_of_toks" -> 5,src,Error(string_of_toks toks,No_steps)
| _ -> 9,src,Error(string_of_toks toks,No_steps);;
let rec steps_of_toks1 q e env toks =
let prefix x (y,w,z) = (x@y),w,z in
if toks = [] then
if e then [9,[],Error_point],None,[] else [],None,[]
else
let stoks,rst = cut_step toks in
let (status,src,substep as step) = parse_step env stoks in
match substep with
| Have (tm, labs, Proof_expected _) ->
let just,rst1 = just_of_toks env rst in
let step,rst2 =
(match just with
| Proof(_, _, _) -> (status,src,Have (tm, labs, just)),rst1
| _ -> (9,src,Error(string_of_toks stoks, No_steps)),rst) in
prefix [step] (steps_of_toks1 q e env rst2)
| Thus (tm, labs, Proof_expected _) ->
let just,rst1 = just_of_toks env rst in
let step,rst2 =
(match just with
| Proof(_, _, _) -> (status,src,Thus (tm, labs, just)),rst1
| _ -> (9,src,Error(string_of_toks stoks, No_steps)),rst) in
prefix [step] (steps_of_toks1 q e env rst2)
| Let vars -> prefix [step] (steps_of_toks1 q e ((rev vars)@env) rst)
| Now (labs, Proof_expected _) ->
let just,rst1 = now_of_toks env rst in
prefix [status,src,Now (labs, just)] (steps_of_toks1 q e env rst1)
| Consider (vars, _, _, By _) ->
prefix [step] (steps_of_toks1 q e ((rev vars)@env) rst)
| Consider (vars, tm, labs, Proof_expected _) ->
let just,rst1 = just_of_toks env rst in
let step,rst2 =
(match just with
| Proof(_, _, _) -> (status,src,Consider(vars, tm, labs, just)),rst1
| _ -> (9,src,Error(string_of_toks stoks, No_steps)),rst) in
prefix [step] (steps_of_toks1 q e ((rev vars)@env) rst2)
| Set (tm, _) ->
prefix [step] (steps_of_toks1 q e ((fst (dest_eq tm))::env) rst)
| Cases ((By _ as just), []) ->
(try
let justs,rst1 = many (case_of_toks env q) rst in
let final,step1,rst2 = steps_of_toks1 false e env rst1 in
let cases = status,src,Cases(just, justs) in
if final <> [] then
prefix [cases; 9,[],Error_point]
(steps_of_toks1 q e env rst1)
else [cases],step1,rst2
with Noparse ->
prefix [9,src,Error(string_of_toks stoks, No_steps)]
(steps_of_toks1 q e env rst))
| Qed just ->
if q then [step],None,rst else
prefix [(if e then 3 else 9),src,Error(string_of_toks stoks, No_steps)]
(steps_of_toks1 q e env rst)
| Bracket_end ->
if e then [],Some step,rst else
prefix [9,src,Error(string_of_toks stoks, No_steps)]
(steps_of_toks1 q e env rst)
| Bracket_proof | Cases (_, _) | Bracket_case | Suppose (_, _) ->
prefix [9,src,Error(string_of_toks stoks, No_steps)]
(steps_of_toks1 q e env rst)
| Error (s, Proof_expected true) ->
let just,rst1 = just_of_toks env rst in
(match just with
| Proof(_, _, _) ->
prefix [status,src,Error(s, just)] (steps_of_toks1 q e env rst1)
| _ ->
prefix [status,src,Error(string_of_toks stoks, No_steps)]
(steps_of_toks1 q e env rst))
| Error (s, Proof_expected false) ->
let steps,step1,rst1 = steps_of_toks1 true true env rst in
prefix [status,src,Error(s, Proof(None,steps,step1))]
(steps_of_toks1 q e env rst)
| Error (_, By _) ->
prefix [status,src,Error(string_of_toks stoks, No_steps)]
(steps_of_toks1 q e env rst)
| _ -> prefix [step] (steps_of_toks1 q e env rst)
and just_of_toks env toks =
try
let stoks,rst = cut_step toks in
let (_,_,substep as step) = parse_step env stoks in
if substep = Bracket_proof then
let steps,step1,rst1 = steps_of_toks1 true true env rst in
(Proof(Some step,steps,step1)),rst1
else (No_steps),toks
with Noparse -> (No_steps),toks
and now_of_toks env toks =
let steps,step1,rst = steps_of_toks1 false true env toks in
(Proof(None,steps,step1)),rst
and case_of_toks env q toks =
let stoks,rst = cut_step toks in
let (_,_,substep as step) = parse_step env stoks in
match substep with
| Bracket_case ->
let steps,step1,rst1 = steps_of_toks1 q true env rst in
(Proof(Some step,steps,step1)),rst1
| Suppose (_, _) ->
let steps,step1,rst1 = steps_of_toks1 q true env rst in
(Proof(None,step::steps,step1)),rst1
| _ -> raise Noparse;;
let steps_of_toks toks =
let proof,_,rst = steps_of_toks1 false false [] toks in
if rst = [] then proof else
proof@[9,[rst],Error (string_of_toks rst, No_steps)];;
let fix_semi toks =
if toks = [] then toks else
match last toks with
| _,Resword ";",_ -> toks
| _ -> toks@["\n",Resword ";",""];;
let parse_proof = steps_of_toks o fix_semi o lex2;;
exception Timeout;;
Sys.set_signal Sys.sigalrm (Sys.Signal_handle (fun _ -> raise Timeout));;
let TIMED_TAC n tac g =
let _ = Unix.alarm n in
try
let gs = tac g in
let _ = Unix.alarm 0 in
gs
with x ->
let _ = Unix.alarm 0 in
raise x;;
let FAKE_TAC : bool -> thm list -> tactic =
fun fake thl (asl,w as g) ->
if fake then
let tm' = itlist (curry mk_imp) (map concl thl) w in
let vl = frees tm' in
let tm = itlist (curry mk_forall) vl tm' in
let th = itlist (C MP) (rev thl) (itlist SPEC (rev vl) (ASSUME tm)) in
null_meta,[],(fun i _ -> INSTANTIATE_ALL i th)
else NO_TAC g;;
let MIZAR_NEXT : (goal -> step * goalstate) -> (goal -> step * goalstate) =
let t = `T` in
fun tac (asl,_ as g) ->
let e,((mvs,insts),gls,just as gs) = tac g in
match gls with
| [] -> e,((mvs,insts),[asl,t],(fun _ _ -> just null_inst []))
| [gl] -> e,gs
| _ -> failwith "MIZAR_NEXT";;
let MIZAR_NEXT' : tactic -> tactic =
let t = `T` in
fun tac (asl,_ as g) ->
let ((mvs,insts),gls,just as gs) = tac g in
match gls with
| [] ->
((mvs,insts),[asl,t],(fun _ _ -> just null_inst []))
| [gl] -> gs
| _ -> failwith "MIZAR_NEXT'";;
let fix_dots prevs tm =
try
let lhs,_ = dest_eq (hd prevs) in
vsubst [lhs, mk_var("...",type_of lhs)] tm
with _ -> tm;;
let fix_dots' asl tm =
try
let th = snd (hd asl) in
let lhs,_ = dest_eq (concl th) in
let dots = mk_var("...",type_of lhs) in
let rec fix_dots1 tm =
(match tm with
| Var _ when tm = dots -> th
| Comb(t1,t2) -> MK_COMB(fix_dots1 t1,fix_dots1 t2)
| Abs(x,t) -> ABS x (fix_dots1 t)
| _ -> REFL tm) in
if vfree_in dots tm then fix_dots1 tm else REFL tm
with _ -> REFL tm;;
let rec terms_of_step prevs (_,_,substep) =
match substep with
| Have (tm, _, _) -> [fix_dots prevs tm]
| Now (_, just) -> [term_of_now just]
| Assume (tm, _) -> [fix_dots prevs tm]
| Thus (tm, _, _) -> [fix_dots prevs tm]
| Consider (_, tm, _, _) -> [fix_dots prevs tm]
| Set (tm, _) -> [fix_dots prevs tm]
| Suppose (tm, _) -> [fix_dots prevs tm]
| _ -> []
and term_of_now =
let t = `T` in
let rec term_of_steps prevs steps =
match steps with
| [] -> t
| (_,_,substep as step)::rst ->
let tm' = term_of_steps ((terms_of_step prevs step)@prevs) rst in
(match substep with
| Let vars -> list_mk_forall(vars,tm')
| Assume (tm, _) -> mk_imp(fix_dots prevs tm,tm')
| Thus (tm, _, _) -> mk_conj(fix_dots prevs tm,tm')
| Take tm ->
let var = genvar (type_of tm) in mk_exists(var,subst [var,tm] tm')
| Consider (vars, _, _, _) ->
if intersect (frees tm') vars <> [] then failwith "term_of_now"
else tm'
| Cases (_, _) -> failwith "term_of_now"
| _ -> tm') in
fun just ->
match just with
| Proof(_, steps, _) ->
(rand o concl o PURE_REWRITE_CONV[AND_CLAUSES])
(term_of_steps [] steps)
| _ -> failwith "term_of_now";;
let terms_of_cases =
let f = `F` in
let rec terms_of_cases cases =
match cases with
| [] -> [],f
| case::rst ->
let l',tm' = terms_of_cases rst in
(match case with
| (_,_,Suppose (tm, _))::_ -> (()::l'),mk_disj(tm,tm')
| _ -> failwith "terms_of_cases") in
terms_of_cases o (map
(fun just ->
match just with
| Proof(_, case, _) -> case
| _ -> failwith "terms_of_cases"));;
let print_to_string1 printer =
let sbuff = ref "" in
let output s m n = sbuff := (!sbuff)^(String.sub s m n) and flush() = () in
let fmt = make_formatter output flush in
ignore(pp_set_max_boxes fmt 100);
fun prefix' n i ->
let prefix = prefix'^(implode (replicate " " n)) in
let m = String.length prefix in
pp_set_margin fmt ((!proof_width) - m);
ignore(printer fmt i);
ignore(pp_print_flush fmt ());
let s = !sbuff in sbuff := "";
implode (map (fun x -> if x = "\n" then "\n"^prefix else x) (explode s));;
let string_of_term1 = print_to_string1 pp_print_term;;
let string_of_type1 = print_to_string1 pp_print_type;;
let string_of_substep prefix substep =
let string_of_vars tl = implode (map (fun v -> " "^fst (dest_var v)) tl) in
let string_of_labs l = implode (map (fun s -> " ["^s^"]") l) in
let rec string_of_by_items x l =
match l with
| [] -> ""
| i::l' -> x^(match i with
| Label s | Thm(s,_) | Tactic(s,_) | Grow(s,_) -> s
| Hole -> "#")^string_of_by_items "," l' in
let string_of_just just =
match just with
| By(l,l',_) ->
(if l = [] then "" else " by"^string_of_by_items " " l)^
(if l' = [] then "" else " from"^string_of_by_items " " l')^";"
| _ -> "" in
prefix^
(match substep with
| Have(tm,l,just) ->
string_of_term1 prefix
(if !indent_continued then String.length !proof_indent else 0) tm^
string_of_labs l^string_of_just just
| Now(l,just) -> "now"^string_of_labs l
| Let(tl) ->
let s = "let"^string_of_vars tl^" be " in
s^string_of_type1 prefix (String.length s) (type_of (hd tl))^";"
| Assume(tm,l) ->
let s = "assume " in
s^string_of_term1 prefix (String.length s) tm^string_of_labs l^";"
| Thus(tm,l,just) ->
let s = "thus " in
s^string_of_term1 prefix (String.length s) tm^string_of_labs l^
string_of_just just
| Qed(just) -> "qed"^string_of_just just
| Bracket_proof -> "proof"
| Bracket_end -> "end;"
| Take(tm) ->
let s = "take " in
s^string_of_term1 prefix (String.length s) tm^";"
| Consider(tl,tm,l,just) ->
let s = "consider"^string_of_vars tl^" such that " in
s^string_of_term1 prefix (String.length s) tm^
string_of_labs l^string_of_just just
| Set(tm,l) ->
let s = "set " in
s^string_of_term1 prefix (String.length s) tm^string_of_labs l^";"
| Cases(just,_) -> "cases"^string_of_just just
| Bracket_case -> "case;"
| Suppose(tm,l) ->
let s = "suppose " in
s^string_of_term1 prefix (String.length s) tm^string_of_labs l^";"
| Exec(s,_) -> "exec "^s^";"
| Error(s,_) -> s
| Empty_step -> ""
| Error_point -> "")^
"\n";;
let step_of_substep prefix substep =
(-1,split_step (lex2 (string_of_substep prefix substep)),substep :step);;
let step_of_obligation prefix lab tl ass tm =
let hole = By([Hole],[],false) in
let prefix' = prefix^ !proof_indent in
let rec lets l =
match l with
| [] -> []
| t::_ -> let l',l'' = partition ((=) (type_of t) o type_of) l in
step_of_substep prefix' (Let l')::lets l'' in
step_of_substep prefix
(if tl = [] && ass = [] then Have(tm,[lab],hole) else
let ll = lets tl in
let intros = ll@(map (fun a ->
step_of_substep prefix' (Assume(a,[]))) ass) in
if !grow_haves then
Have(list_mk_forall(flat
(map (function (_,_,Let l) -> l | _ -> []) ll),
itlist (curry mk_imp) ass tm), [lab],
Proof (Some (step_of_substep prefix Bracket_proof),
intros@
[step_of_substep prefix (Qed(hole))], None))
else
Now([lab], Proof (None,
intros@
[step_of_substep prefix' (Thus(tm,[],hole))],
Some (step_of_substep prefix Bracket_end))));;
let steps_of_goals (asl,w :goal) (_,gl,_ :goalstate) prefix n =
let ass = map (concl o snd) asl in
let fv = union (flat (map frees ass)) (frees w) in
let rec extra_ass l l' =
if subset l ass then l' else extra_ass (tl l) ((hd l)::l') in
let rec steps_of_goals1 n gl =
match gl with
| [] -> [],[],n
| (asl',w')::gl' ->
let ass' = map (concl o snd) asl' in
let steps',labs',n' = steps_of_goals1 (n + 1) gl' in
let lab = string_of_int n in
((step_of_obligation prefix lab
(subtract (union (flat (map frees ass')) (frees w')) fv)
(extra_ass ass' []) w')::steps'),lab::labs',n' in
steps_of_goals1 n gl;;
let next_growth_label = ref 0;;
let connect_step (step:step) labs =
let comma = "",Ident ",","" in
let from_key = " ",Resword "from"," " in
let rec ungrow_by src l =
match l with
| [] -> src,[]
| Grow(name,tac)::l' ->
(match src with
| tok1::(_,Ident "#",_)::tok2::src' ->
let src'',l'' = ungrow_by src' l' in
(tok1::tok2::src''),(Tactic(name,tac)::l')
| _ -> failwith "ungrow_by")
| x::l' -> let toks,src' = chop_list 2 src in
let src'',l'' = ungrow_by src' l' in
(toks@src''),(x::l'') in
let rec extra_from sep labs =
match labs with
| [] -> []
| lab::labs' -> sep::("",Ident lab,"")::extra_from comma labs' in
let connect_just src4 just =
match just with
| By(l,l',b) ->
let src4',l'' = ungrow_by src4 l in
let src4'',l''' = ungrow_by src4' l' in
(src4''@if labs = [] then [] else
extra_from (if l' = [] then from_key else comma) labs),
By(l'',(l'''@map (fun s -> Label s) labs),b)
| _ -> src4,just in
match step with
| (e,[src1; src2; src3; src4; src5],substep) ->
(match substep with
| Have(x,y,just) ->
let src4',just' = connect_just src4 just in
(e,[src1; src2; src3; src4'; src5],Have(x,y,just'))
| Thus(x,y,just) ->
let src4',just' = connect_just src4 just in
(e,[src1; src2; src3; src4'; src5],Thus(x,y,just'))
| Qed just ->
let src4',just' = connect_just src4 just in
(e,[src1; src2; src3; src4'; src5],Qed just')
| Consider(x,y,z,just) ->
let src4',just' = connect_just src4 just in
(e,[src1; src2; src3; src4'; src5],Consider(x,y,z,just'))
| Cases(just,x) ->
let src4',just' = connect_just src4 just in
(e,[src1; src2; src3; src4'; src5],Cases(just',x))
| _ -> failwith "connect_step" :step)
| _ -> failwith "connect_step";;
let add_width n s =
let rec add_width1 n s =
match s with
| [] -> n
| "\t"::s' -> add_width1 ((n/8 + 1)*8) s'
| "\n"::s' -> add_width1 0 s'
| _::s' -> add_width1 (n + 1) s' in
add_width1 n (explode s);;
let rewrap_step (e,src,substep as step:step) =
let rec rewrap_from x1 src4a src4b =
match src4b with
| [] -> rev src4a
| (x,y,z)::(x',(Resword "from" as y'),z')::rst ->
(rev src4a)@(x,y,"\n")::(x1,y',z')::rst
| tok::rst -> rewrap_from x1 (tok::src4a) rst in
match src with
| [src1; src2; src3; src4; src5] ->
if src4 = [] then step else
let src123 = src1@src2@src3 in
let x,y,z = strings_of_toks src123 in
let x',y',_ = strings_of_toks src4 in
if add_width 0 (x^y^z^x'^y') > !proof_width then
let a,b,_ = last src123 in
let src123' = (butlast src123)@[a,b,"\n"] in
let src1',src23' = chop_list (length src1) src123' in
let src2',src3' = chop_list (length src2) src23' in
let _,b',c' = hd src4 in
let x1 = x^ !proof_indent in
let src4' = (x1,b',c')::tl src4 in
let src4'' =
if add_width 0 (x1^y') > !proof_width then
rewrap_from x1 [] src4' else src4' in
(e,[src1'; src2'; src3'; src4''; src5],substep)
else (step:step)
| _ -> failwith "rewrap_step";;
let rec pp_step prefix step =
let (e,_,substep) = step in
let (_,src,substep') =
rewrap_step (step_of_substep prefix substep) in
let substep'' =
(match substep' with