421 in |
332 in |
422 play [] reconstrs |
333 play [] reconstrs |
423 end |
334 end |
424 end |
335 end |
425 |
336 |
426 |
337 val canonical_isar_supported_prover = eN |
427 (* generic TPTP-based ATPs *) |
338 |
428 |
339 fun isar_supported_prover_of thy name = |
429 (* Too general means, positive equality literal with a variable X as one |
340 if is_atp thy name then name |
430 operand, when X does not occur properly in the other operand. This rules out |
341 else the_default name (remotify_atp_if_not_installed thy canonical_isar_supported_prover) |
431 clearly inconsistent facts such as X = a | X = b, though it by no means |
|
432 guarantees soundness. *) |
|
433 |
|
434 fun get_facts_of_filter _ [(_, facts)] = facts |
|
435 | get_facts_of_filter fact_filter factss = |
|
436 case AList.lookup (op =) factss fact_filter of |
|
437 SOME facts => facts |
|
438 | NONE => snd (hd factss) |
|
439 |
|
440 (* Unwanted equalities are those between a (bound or schematic) variable that |
|
441 does not properly occur in the second operand. *) |
|
442 val is_exhaustive_finite = |
|
443 let |
|
444 fun is_bad_equal (Var z) t = |
|
445 not (exists_subterm (fn Var z' => z = z' | _ => false) t) |
|
446 | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j)) |
|
447 | is_bad_equal _ _ = false |
|
448 fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1 |
|
449 fun do_formula pos t = |
|
450 case (pos, t) of |
|
451 (_, @{const Trueprop} $ t1) => do_formula pos t1 |
|
452 | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) => |
|
453 do_formula pos t' |
|
454 | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) => |
|
455 do_formula pos t' |
|
456 | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) => |
|
457 do_formula pos t' |
|
458 | (_, @{const "==>"} $ t1 $ t2) => |
|
459 do_formula (not pos) t1 andalso |
|
460 (t2 = @{prop False} orelse do_formula pos t2) |
|
461 | (_, @{const HOL.implies} $ t1 $ t2) => |
|
462 do_formula (not pos) t1 andalso |
|
463 (t2 = @{const False} orelse do_formula pos t2) |
|
464 | (_, @{const Not} $ t1) => do_formula (not pos) t1 |
|
465 | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2] |
|
466 | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2] |
|
467 | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2 |
|
468 | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2 |
|
469 | _ => false |
|
470 in do_formula true end |
|
471 |
|
472 fun has_bound_or_var_of_type pred = |
|
473 exists_subterm (fn Var (_, T as Type _) => pred T |
|
474 | Abs (_, T as Type _, _) => pred T |
|
475 | _ => false) |
|
476 |
|
477 (* Facts are forbidden to contain variables of these types. The typical reason |
|
478 is that they lead to unsoundness. Note that "unit" satisfies numerous |
|
479 equations like "?x = ()". The resulting clauses will have no type constraint, |
|
480 yielding false proofs. Even "bool" leads to many unsound proofs, though only |
|
481 for higher-order problems. *) |
|
482 |
|
483 (* Facts containing variables of type "unit" or "bool" or of the form |
|
484 "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types |
|
485 are omitted. *) |
|
486 fun is_dangerous_prop ctxt = |
|
487 transform_elim_prop |
|
488 #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf |
|
489 is_exhaustive_finite) |
|
490 |
|
491 (* Important messages are important but not so important that users want to see |
|
492 them each time. *) |
|
493 val atp_important_message_keep_quotient = 25 |
|
494 |
|
495 fun choose_type_enc strictness best_type_enc format = |
|
496 the_default best_type_enc |
|
497 #> type_enc_of_string strictness |
|
498 #> adjust_type_enc format |
|
499 |
|
500 fun isar_proof_reconstructor ctxt name = |
|
501 let val thy = Proof_Context.theory_of ctxt in |
|
502 if is_atp thy name then name |
|
503 else remotify_prover_if_not_installed ctxt eN |> the_default name |
|
504 end |
|
505 |
342 |
506 (* FIXME: See the analogous logic in the function "maybe_minimize" in |
343 (* FIXME: See the analogous logic in the function "maybe_minimize" in |
507 "sledgehammer_prover_minimize.ML". *) |
344 "sledgehammer_prover_minimize.ML". *) |
508 fun choose_minimize_command ctxt (params as {isar_proofs, ...}) minimize_command name preplay = |
345 fun choose_minimize_command thy (params as {isar_proofs, ...}) minimize_command name preplay = |
509 let |
346 let |
510 val maybe_isar_name = name |> isar_proofs = SOME true ? isar_proof_reconstructor ctxt |
347 val maybe_isar_name = name |> isar_proofs = SOME true ? isar_supported_prover_of thy |
511 val (min_name, override_params) = |
348 val (min_name, override_params) = |
512 (case preplay of |
349 (case preplay of |
513 (reconstr, Played _) => |
350 (reconstr, Played _) => |
514 if isar_proofs = SOME true then (maybe_isar_name, []) |
351 if isar_proofs = SOME true then (maybe_isar_name, []) |
515 else extract_reconstructor params reconstr |
352 else extract_reconstructor params reconstr |
516 | _ => (maybe_isar_name, [])) |
353 | _ => (maybe_isar_name, [])) |
517 in minimize_command override_params min_name end |
354 in minimize_command override_params min_name end |
518 |
355 |
519 val max_fact_instances = 10 (* FUDGE *) |
356 val max_fact_instances = 10 (* FUDGE *) |
520 |
357 |
521 fun repair_monomorph_context max_iters best_max_iters max_new_instances |
358 fun repair_monomorph_context max_iters best_max_iters max_new_instances best_max_new_instances = |
522 best_max_new_instances = |
|
523 Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters) |
359 Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters) |
524 #> Config.put Monomorph.max_new_instances |
360 #> Config.put Monomorph.max_new_instances |
525 (max_new_instances |> the_default best_max_new_instances) |
361 (max_new_instances |> the_default best_max_new_instances) |
526 #> Config.put Monomorph.max_thm_instances max_fact_instances |
362 #> Config.put Monomorph.max_thm_instances max_fact_instances |
527 |
363 |
528 fun suffix_of_mode Auto_Try = "_try" |
364 fun run_reconstructor mode name (params as {debug, verbose, timeout, type_enc, lam_trans, ...}) |
529 | suffix_of_mode Try = "_try" |
|
530 | suffix_of_mode Normal = "" |
|
531 | suffix_of_mode MaSh = "" |
|
532 | suffix_of_mode Auto_Minimize = "_min" |
|
533 | suffix_of_mode Minimize = "_min" |
|
534 |
|
535 (* Give the ATPs some slack before interrupting them the hard way. "z3_tptp" on |
|
536 Linux appears to be the only ATP that does not honor its time limit. *) |
|
537 val atp_timeout_slack = seconds 1.0 |
|
538 |
|
539 val mono_max_privileged_facts = 10 |
|
540 |
|
541 (* For low values of "max_facts", this fudge value ensures that most slices are |
|
542 invoked with a nontrivial amount of facts. *) |
|
543 val max_fact_factor_fudge = 5 |
|
544 |
|
545 fun run_atp mode name |
|
546 ({exec, arguments, proof_delims, known_failures, prem_role, best_slices, best_max_mono_iters, |
|
547 best_max_new_mono_instances, ...} : atp_config) |
|
548 (params as {debug, verbose, overlord, type_enc, strict, lam_trans, uncurried_aliases, |
|
549 fact_filter, max_facts, max_mono_iters, max_new_mono_instances, isar_proofs, compress_isar, |
|
550 try0_isar, slice, timeout, preplay_timeout, ...}) |
|
551 minimize_command |
365 minimize_command |
552 ({comment, state, goal, subgoal, subgoal_count, factss, ...} : prover_problem) = |
366 ({state, subgoal, subgoal_count, factss = (_, facts) :: _, ...} : prover_problem) = |
553 let |
|
554 val thy = Proof.theory_of state |
|
555 val ctxt = Proof.context_of state |
|
556 val atp_mode = |
|
557 if Config.get ctxt completish then Sledgehammer_Completish |
|
558 else Sledgehammer |
|
559 val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal ctxt |
|
560 val (dest_dir, problem_prefix) = |
|
561 if overlord then overlord_file_location_of_prover name |
|
562 else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix) |
|
563 val problem_file_name = |
|
564 Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^ |
|
565 suffix_of_mode mode ^ "_" ^ string_of_int subgoal) |
|
566 val prob_path = |
|
567 if dest_dir = "" then |
|
568 File.tmp_path problem_file_name |
|
569 else if File.exists (Path.explode dest_dir) then |
|
570 Path.append (Path.explode dest_dir) problem_file_name |
|
571 else |
|
572 error ("No such directory: " ^ quote dest_dir ^ ".") |
|
573 val exec = exec () |
|
574 val command0 = |
|
575 case find_first (fn var => getenv var <> "") (fst exec) of |
|
576 SOME var => |
|
577 let |
|
578 val pref = getenv var ^ "/" |
|
579 val paths = map (Path.explode o prefix pref) (snd exec) |
|
580 in |
|
581 case find_first File.exists paths of |
|
582 SOME path => path |
|
583 | NONE => error ("Bad executable: " ^ Path.print (hd paths) ^ ".") |
|
584 end |
|
585 | NONE => error ("The environment variable " ^ quote (List.last (fst exec)) ^ |
|
586 " is not set.") |
|
587 fun split_time s = |
|
588 let |
|
589 val split = String.tokens (fn c => str c = "\n") |
|
590 val (output, t) = |
|
591 s |> split |> (try split_last #> the_default ([], "0")) |
|
592 |>> cat_lines |
|
593 fun as_num f = f >> (fst o read_int) |
|
594 val num = as_num (Scan.many1 Symbol.is_ascii_digit) |
|
595 val digit = Scan.one Symbol.is_ascii_digit |
|
596 val num3 = as_num (digit ::: digit ::: (digit >> single)) |
|
597 val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b) |
|
598 val as_time = |
|
599 raw_explode #> Scan.read Symbol.stopper time #> the_default 0 |
|
600 in (output, as_time t |> Time.fromMilliseconds) end |
|
601 fun run () = |
|
602 let |
|
603 (* If slicing is disabled, we expand the last slice to fill the entire |
|
604 time available. *) |
|
605 val all_slices = best_slices ctxt |
|
606 val actual_slices = get_slices slice all_slices |
|
607 fun max_facts_of_slices f slices = fold (Integer.max o slice_max_facts o f) slices 0 |
|
608 val num_actual_slices = length actual_slices |
|
609 val max_fact_factor = |
|
610 Real.fromInt (case max_facts of |
|
611 NONE => max_facts_of_slices I all_slices |
|
612 | SOME max => max) |
|
613 / Real.fromInt (max_facts_of_slices snd actual_slices) |
|
614 fun monomorphize_facts facts = |
|
615 let |
|
616 val ctxt = |
|
617 ctxt |
|
618 |> repair_monomorph_context max_mono_iters |
|
619 best_max_mono_iters max_new_mono_instances |
|
620 best_max_new_mono_instances |
|
621 (* pseudo-theorem involving the same constants as the subgoal *) |
|
622 val subgoal_th = |
|
623 Logic.list_implies (hyp_ts, concl_t) |> Skip_Proof.make_thm thy |
|
624 val rths = |
|
625 facts |> chop mono_max_privileged_facts |
|
626 |>> map (pair 1 o snd) |
|
627 ||> map (pair 2 o snd) |
|
628 |> op @ |
|
629 |> cons (0, subgoal_th) |
|
630 in |
|
631 Monomorph.monomorph atp_schematic_consts_of ctxt rths |
|
632 |> tl |> curry ListPair.zip (map fst facts) |
|
633 |> maps (fn (name, rths) => |
|
634 map (pair name o zero_var_indexes o snd) rths) |
|
635 end |
|
636 fun run_slice time_left (cache_key, cache_value) |
|
637 (slice, (time_frac, |
|
638 (key as ((best_max_facts, best_fact_filter), format, |
|
639 best_type_enc, best_lam_trans, |
|
640 best_uncurried_aliases), |
|
641 extra))) = |
|
642 let |
|
643 val effective_fact_filter = |
|
644 fact_filter |> the_default best_fact_filter |
|
645 val facts = get_facts_of_filter effective_fact_filter factss |
|
646 val num_facts = |
|
647 Real.ceil (max_fact_factor * Real.fromInt best_max_facts) + |
|
648 max_fact_factor_fudge |
|
649 |> Integer.min (length facts) |
|
650 val strictness = if strict then Strict else Non_Strict |
|
651 val type_enc = |
|
652 type_enc |> choose_type_enc strictness best_type_enc format |
|
653 val sound = is_type_enc_sound type_enc |
|
654 val real_ms = Real.fromInt o Time.toMilliseconds |
|
655 val slice_timeout = |
|
656 (real_ms time_left |
|
657 |> (if slice < num_actual_slices - 1 then |
|
658 curry Real.min (time_frac * real_ms timeout) |
|
659 else |
|
660 I)) |
|
661 * 0.001 |
|
662 |> seconds |
|
663 val generous_slice_timeout = |
|
664 if mode = MaSh then one_day else Time.+ (slice_timeout, atp_timeout_slack) |
|
665 val _ = |
|
666 if debug then |
|
667 quote name ^ " slice #" ^ string_of_int (slice + 1) ^ |
|
668 " with " ^ string_of_int num_facts ^ " fact" ^ |
|
669 plural_s num_facts ^ " for " ^ string_of_time slice_timeout ^ "..." |
|
670 |> Output.urgent_message |
|
671 else |
|
672 () |
|
673 val readable_names = not (Config.get ctxt atp_full_names) |
|
674 val lam_trans = |
|
675 case lam_trans of |
|
676 SOME s => s |
|
677 | NONE => best_lam_trans |
|
678 val uncurried_aliases = |
|
679 case uncurried_aliases of |
|
680 SOME b => b |
|
681 | NONE => best_uncurried_aliases |
|
682 val value as (atp_problem, _, fact_names, _, _) = |
|
683 if cache_key = SOME key then |
|
684 cache_value |
|
685 else |
|
686 facts |
|
687 |> not sound |
|
688 ? filter_out (is_dangerous_prop ctxt o prop_of o snd) |
|
689 |> take num_facts |
|
690 |> not (is_type_enc_polymorphic type_enc) ? monomorphize_facts |
|
691 |> map (apsnd prop_of) |
|
692 |> prepare_atp_problem ctxt format prem_role type_enc atp_mode |
|
693 lam_trans uncurried_aliases |
|
694 readable_names true hyp_ts concl_t |
|
695 fun sel_weights () = atp_problem_selection_weights atp_problem |
|
696 fun ord_info () = atp_problem_term_order_info atp_problem |
|
697 val ord = effective_term_order ctxt name |
|
698 val full_proof = isar_proofs |> the_default (mode = Minimize) |
|
699 val args = |
|
700 arguments ctxt full_proof extra slice_timeout (File.shell_path prob_path) |
|
701 (ord, ord_info, sel_weights) |
|
702 val command = |
|
703 "(exec 2>&1; " ^ File.shell_path command0 ^ " " ^ args ^ " " ^ ")" |
|
704 |> enclose "TIMEFORMAT='%3R'; { time " " ; }" |
|
705 val _ = |
|
706 atp_problem |
|
707 |> lines_of_atp_problem format ord ord_info |
|
708 |> cons ("% " ^ command ^ "\n" ^ (if comment = "" then "" else "% " ^ comment ^ "\n")) |
|
709 |> File.write_list prob_path |
|
710 val ((output, run_time), (atp_proof, outcome)) = |
|
711 TimeLimit.timeLimit generous_slice_timeout Isabelle_System.bash_output command |
|
712 |>> (if overlord then prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n") else I) |
|
713 |> fst |> split_time |
|
714 |> (fn accum as (output, _) => |
|
715 (accum, |
|
716 extract_tstplike_proof_and_outcome verbose proof_delims known_failures output |
|
717 |>> atp_proof_of_tstplike_proof atp_problem |
|
718 handle UNRECOGNIZED_ATP_PROOF () => ([], SOME ProofIncomplete))) |
|
719 handle TimeLimit.TimeOut => (("", slice_timeout), ([], SOME TimedOut)) |
|
720 val outcome = |
|
721 (case outcome of |
|
722 NONE => |
|
723 (case used_facts_in_unsound_atp_proof ctxt fact_names atp_proof |
|
724 |> Option.map (sort string_ord) of |
|
725 SOME facts => |
|
726 let val failure = UnsoundProof (is_type_enc_sound type_enc, facts) in |
|
727 if debug then (warning (string_of_atp_failure failure); NONE) else SOME failure |
|
728 end |
|
729 | NONE => NONE) |
|
730 | _ => outcome) |
|
731 in |
|
732 ((SOME key, value), (output, run_time, facts, atp_proof, outcome)) |
|
733 end |
|
734 val timer = Timer.startRealTimer () |
|
735 fun maybe_run_slice slice |
|
736 (result as (cache, (_, run_time0, _, _, SOME _))) = |
|
737 let |
|
738 val time_left = Time.- (timeout, Timer.checkRealTimer timer) |
|
739 in |
|
740 if Time.<= (time_left, Time.zeroTime) then |
|
741 result |
|
742 else |
|
743 run_slice time_left cache slice |
|
744 |> (fn (cache, (output, run_time, used_from, atp_proof, outcome)) => |
|
745 (cache, (output, Time.+ (run_time0, run_time), used_from, atp_proof, outcome))) |
|
746 end |
|
747 | maybe_run_slice _ result = result |
|
748 in |
|
749 ((NONE, ([], Symtab.empty, Vector.fromList [], [], Symtab.empty)), |
|
750 ("", Time.zeroTime, [], [], SOME InternalError)) |
|
751 |> fold maybe_run_slice actual_slices |
|
752 end |
|
753 (* If the problem file has not been exported, remove it; otherwise, export |
|
754 the proof file too. *) |
|
755 fun clean_up () = |
|
756 if dest_dir = "" then (try File.rm prob_path; ()) else () |
|
757 fun export (_, (output, _, _, _, _)) = |
|
758 if dest_dir = "" then () |
|
759 else File.write (Path.explode (Path.implode prob_path ^ "_proof")) output |
|
760 val ((_, (_, pool, fact_names, lifted, sym_tab)), |
|
761 (output, run_time, used_from, atp_proof, outcome)) = |
|
762 with_cleanup clean_up run () |> tap export |
|
763 val important_message = |
|
764 if mode = Normal andalso random_range 0 (atp_important_message_keep_quotient - 1) = 0 |
|
765 then |
|
766 extract_important_message output |
|
767 else |
|
768 "" |
|
769 val (used_facts, preplay, message, message_tail) = |
|
770 (case outcome of |
|
771 NONE => |
|
772 let |
|
773 val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof |
|
774 val needs_full_types = is_typed_helper_used_in_atp_proof atp_proof |
|
775 val reconstrs = |
|
776 bunch_of_reconstructors needs_full_types (lam_trans_of_atp_proof atp_proof |
|
777 o (fn desperate => if desperate then hide_lamsN else default_metis_lam_trans)) |
|
778 in |
|
779 (used_facts, |
|
780 Lazy.lazy (fn () => |
|
781 let val used_pairs = used_from |> filter_used_facts false used_facts in |
|
782 play_one_line_proof mode debug verbose preplay_timeout used_pairs state subgoal |
|
783 (hd reconstrs) reconstrs |
|
784 end), |
|
785 fn preplay => |
|
786 let |
|
787 val _ = if verbose then Output.urgent_message "Generating proof text..." else () |
|
788 fun isar_params () = |
|
789 let |
|
790 val metis_type_enc = |
|
791 if is_typed_helper_used_in_atp_proof atp_proof then full_typesN |
|
792 else partial_typesN |
|
793 val metis_lam_trans = lam_trans_of_atp_proof atp_proof default_metis_lam_trans |
|
794 val atp_proof = |
|
795 atp_proof |
|
796 |> termify_atp_proof ctxt pool lifted sym_tab |
|
797 |> introduce_spass_skolem |
|
798 |> factify_atp_proof fact_names hyp_ts concl_t |
|
799 in |
|
800 (verbose, metis_type_enc, metis_lam_trans, preplay_timeout, compress_isar, |
|
801 try0_isar, atp_proof, goal) |
|
802 end |
|
803 val one_line_params = |
|
804 (preplay, proof_banner mode name, used_facts, |
|
805 choose_minimize_command ctxt params minimize_command name preplay, |
|
806 subgoal, subgoal_count) |
|
807 val num_chained = length (#facts (Proof.goal state)) |
|
808 in |
|
809 proof_text ctxt debug isar_proofs isar_params num_chained one_line_params |
|
810 end, |
|
811 (if verbose then "\nATP real CPU time: " ^ string_of_time run_time ^ "." else "") ^ |
|
812 (if important_message <> "" then |
|
813 "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^ important_message |
|
814 else |
|
815 "")) |
|
816 end |
|
817 | SOME failure => |
|
818 ([], Lazy.value (plain_metis, Play_Failed), fn _ => string_of_atp_failure failure, "")) |
|
819 in |
|
820 {outcome = outcome, used_facts = used_facts, used_from = used_from, run_time = run_time, |
|
821 preplay = preplay, message = message, message_tail = message_tail} |
|
822 end |
|
823 |
|
824 (* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until |
|
825 these are sorted out properly in the SMT module, we have to interpret these |
|
826 ourselves. *) |
|
827 val remote_smt_failures = |
|
828 [(2, NoLibwwwPerl), |
|
829 (22, CantConnect)] |
|
830 val z3_failures = |
|
831 [(101, OutOfResources), |
|
832 (103, MalformedInput), |
|
833 (110, MalformedInput), |
|
834 (112, TimedOut)] |
|
835 val unix_failures = |
|
836 [(138, Crashed), |
|
837 (139, Crashed)] |
|
838 val smt_failures = remote_smt_failures @ z3_failures @ unix_failures |
|
839 |
|
840 fun failure_of_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) = |
|
841 if is_real_cex then Unprovable else GaveUp |
|
842 | failure_of_smt_failure SMT_Failure.Time_Out = TimedOut |
|
843 | failure_of_smt_failure (SMT_Failure.Abnormal_Termination code) = |
|
844 (case AList.lookup (op =) smt_failures code of |
|
845 SOME failure => failure |
|
846 | NONE => UnknownError ("Abnormal termination with exit code " ^ |
|
847 string_of_int code ^ ".")) |
|
848 | failure_of_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources |
|
849 | failure_of_smt_failure (SMT_Failure.Other_Failure s) = UnknownError s |
|
850 |
|
851 (* FUDGE *) |
|
852 val smt_max_slices = |
|
853 Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8) |
|
854 val smt_slice_fact_frac = |
|
855 Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} |
|
856 (K 0.667) |
|
857 val smt_slice_time_frac = |
|
858 Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.333) |
|
859 val smt_slice_min_secs = |
|
860 Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 3) |
|
861 |
|
862 val is_boring_builtin_typ = |
|
863 not o exists_subtype (member (op =) [@{typ nat}, @{typ int}, HOLogic.realT]) |
|
864 |
|
865 fun smt_filter_loop name ({debug, overlord, max_mono_iters, max_new_mono_instances, timeout, slice, |
|
866 ...} : params) state goal i = |
|
867 let |
|
868 fun repair_context ctxt = |
|
869 ctxt |> select_smt_solver name |
|
870 |> Config.put SMT_Config.verbose debug |
|
871 |> (if overlord then |
|
872 Config.put SMT_Config.debug_files |
|
873 (overlord_file_location_of_prover name |
|
874 |> (fn (path, name) => path ^ "/" ^ name)) |
|
875 else |
|
876 I) |
|
877 |> Config.put SMT_Config.infer_triggers |
|
878 (Config.get ctxt smt_triggers) |
|
879 |> not (Config.get ctxt smt_builtins) |
|
880 ? (SMT_Builtin.filter_builtins is_boring_builtin_typ |
|
881 #> Config.put SMT_Config.datatypes false) |
|
882 |> repair_monomorph_context max_mono_iters default_max_mono_iters |
|
883 max_new_mono_instances default_max_new_mono_instances |
|
884 val state = Proof.map_context (repair_context) state |
|
885 val ctxt = Proof.context_of state |
|
886 val max_slices = if slice then Config.get ctxt smt_max_slices else 1 |
|
887 fun do_slice timeout slice outcome0 time_so_far |
|
888 (weighted_factss as (fact_filter, weighted_facts) :: _) = |
|
889 let |
|
890 val timer = Timer.startRealTimer () |
|
891 val slice_timeout = |
|
892 if slice < max_slices then |
|
893 let val ms = Time.toMilliseconds timeout in |
|
894 Int.min (ms, |
|
895 Int.max (1000 * Config.get ctxt smt_slice_min_secs, |
|
896 Real.ceil (Config.get ctxt smt_slice_time_frac |
|
897 * Real.fromInt ms))) |
|
898 |> Time.fromMilliseconds |
|
899 end |
|
900 else |
|
901 timeout |
|
902 val num_facts = length weighted_facts |
|
903 val _ = |
|
904 if debug then |
|
905 quote name ^ " slice " ^ string_of_int slice ^ " with " ^ string_of_int num_facts ^ |
|
906 " fact" ^ plural_s num_facts ^ " for " ^ string_of_time slice_timeout |
|
907 |> Output.urgent_message |
|
908 else |
|
909 () |
|
910 val birth = Timer.checkRealTimer timer |
|
911 val _ = |
|
912 if debug then Output.urgent_message "Invoking SMT solver..." else () |
|
913 val (outcome, used_facts) = |
|
914 SMT_Solver.smt_filter_preprocess ctxt [] goal weighted_facts i |
|
915 |> SMT_Solver.smt_filter_apply slice_timeout |
|
916 |> (fn {outcome, used_facts} => (outcome, used_facts)) |
|
917 handle exn => if Exn.is_interrupt exn then |
|
918 reraise exn |
|
919 else |
|
920 (ML_Compiler.exn_message exn |
|
921 |> SMT_Failure.Other_Failure |> SOME, []) |
|
922 val death = Timer.checkRealTimer timer |
|
923 val outcome0 = if is_none outcome0 then SOME outcome else outcome0 |
|
924 val time_so_far = Time.+ (time_so_far, Time.- (death, birth)) |
|
925 val too_many_facts_perhaps = |
|
926 case outcome of |
|
927 NONE => false |
|
928 | SOME (SMT_Failure.Counterexample _) => false |
|
929 | SOME SMT_Failure.Time_Out => slice_timeout <> timeout |
|
930 | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *) |
|
931 | SOME SMT_Failure.Out_Of_Memory => true |
|
932 | SOME (SMT_Failure.Other_Failure _) => true |
|
933 val timeout = Time.- (timeout, Timer.checkRealTimer timer) |
|
934 in |
|
935 if too_many_facts_perhaps andalso slice < max_slices andalso num_facts > 0 andalso |
|
936 Time.> (timeout, Time.zeroTime) then |
|
937 let |
|
938 val new_num_facts = |
|
939 Real.ceil (Config.get ctxt smt_slice_fact_frac * Real.fromInt num_facts) |
|
940 val weighted_factss as (new_fact_filter, _) :: _ = |
|
941 weighted_factss |
|
942 |> (fn (x :: xs) => xs @ [x]) |
|
943 |> app_hd (apsnd (take new_num_facts)) |
|
944 val show_filter = fact_filter <> new_fact_filter |
|
945 fun num_of_facts fact_filter num_facts = |
|
946 string_of_int num_facts ^ |
|
947 (if show_filter then " " ^ quote fact_filter else "") ^ |
|
948 " fact" ^ plural_s num_facts |
|
949 val _ = |
|
950 if debug then |
|
951 quote name ^ " invoked with " ^ |
|
952 num_of_facts fact_filter num_facts ^ ": " ^ |
|
953 string_of_atp_failure (failure_of_smt_failure (the outcome)) ^ |
|
954 " Retrying with " ^ num_of_facts new_fact_filter new_num_facts ^ |
|
955 "..." |
|
956 |> Output.urgent_message |
|
957 else |
|
958 () |
|
959 in |
|
960 do_slice timeout (slice + 1) outcome0 time_so_far weighted_factss |
|
961 end |
|
962 else |
|
963 {outcome = if is_none outcome then NONE else the outcome0, used_facts = used_facts, |
|
964 used_from = map (apsnd snd) weighted_facts, run_time = time_so_far} |
|
965 end |
|
966 in |
|
967 do_slice timeout 1 NONE Time.zeroTime |
|
968 end |
|
969 |
|
970 fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...}) minimize_command |
|
971 ({state, goal, subgoal, subgoal_count, factss, ...} : prover_problem) = |
|
972 let |
|
973 val ctxt = Proof.context_of state |
|
974 fun weight_facts facts = |
|
975 let val num_facts = length facts in |
|
976 facts ~~ (0 upto num_facts - 1) |
|
977 |> map (weight_smt_fact ctxt num_facts) |
|
978 end |
|
979 val weighted_factss = factss |> map (apsnd weight_facts) |
|
980 val {outcome, used_facts = used_pairs, used_from, run_time} = |
|
981 smt_filter_loop name params state goal subgoal weighted_factss |
|
982 val used_facts = used_pairs |> map fst |
|
983 val outcome = outcome |> Option.map failure_of_smt_failure |
|
984 val (preplay, message, message_tail) = |
|
985 case outcome of |
|
986 NONE => |
|
987 (Lazy.lazy (fn () => |
|
988 play_one_line_proof mode debug verbose preplay_timeout used_pairs |
|
989 state subgoal SMT |
|
990 (bunch_of_reconstructors false (fn desperate => |
|
991 if desperate then liftingN else default_metis_lam_trans))), |
|
992 fn preplay => |
|
993 let |
|
994 val one_line_params = |
|
995 (preplay, proof_banner mode name, used_facts, |
|
996 choose_minimize_command ctxt params minimize_command name preplay, |
|
997 subgoal, subgoal_count) |
|
998 val num_chained = length (#facts (Proof.goal state)) |
|
999 in |
|
1000 one_line_proof_text num_chained one_line_params |
|
1001 end, |
|
1002 if verbose then "\nSMT solver real CPU time: " ^ string_of_time run_time ^ "." else "") |
|
1003 | SOME failure => |
|
1004 (Lazy.value (plain_metis, Play_Failed), |
|
1005 fn _ => string_of_atp_failure failure, "") |
|
1006 in |
|
1007 {outcome = outcome, used_facts = used_facts, used_from = used_from, |
|
1008 run_time = run_time, preplay = preplay, message = message, |
|
1009 message_tail = message_tail} |
|
1010 end |
|
1011 |
|
1012 fun run_reconstructor mode name |
|
1013 (params as {debug, verbose, timeout, type_enc, lam_trans, ...}) |
|
1014 minimize_command |
|
1015 ({state, subgoal, subgoal_count, factss = (_, facts) :: _, ...} |
|
1016 : prover_problem) = |
|
1017 let |
367 let |
1018 val reconstr = |
368 val reconstr = |
1019 if name = metisN then |
369 if name = metisN then |
1020 Metis (type_enc |> the_default (hd partial_type_encs), |
370 Metis (type_enc |> the_default (hd partial_type_encs), |
1021 lam_trans |> the_default default_metis_lam_trans) |
371 lam_trans |> the_default default_metis_lam_trans) |