1 (* Title: HOL/Tools/Sledgehammer/sledgehammer_mash.ML
2 Author: Jasmin Blanchette, TU Muenchen
3 Author: Cezary Kaliszyk, University of Innsbruck
5 Sledgehammer's machine-learning-based relevance filter (MaSh).
8 signature SLEDGEHAMMER_MASH =
10 type stature = ATP_Problem_Generate.stature
11 type raw_fact = Sledgehammer_Fact.raw_fact
12 type fact = Sledgehammer_Fact.fact
13 type fact_override = Sledgehammer_Fact.fact_override
14 type params = Sledgehammer_Prover.params
15 type prover_result = Sledgehammer_Prover.prover_result
17 val trace : bool Config.T
18 val duplicates : bool Config.T
26 val learn_isarN : string
27 val learn_proverN : string
28 val relearn_isarN : string
29 val relearn_proverN : string
30 val fact_filters : string list
31 val encode_str : string -> string
32 val encode_strs : string list -> string
33 val decode_str : string -> string
34 val decode_strs : string -> string list
36 datatype mash_algorithm =
43 val is_mash_enabled : unit -> bool
44 val the_mash_algorithm : unit -> mash_algorithm
45 val str_of_mash_algorithm : mash_algorithm -> string
47 val mesh_facts : ('a list -> 'a list) -> ('a * 'a -> bool) -> int ->
48 (real * (('a * real) list * 'a list)) list -> 'a list
49 val nickname_of_thm : thm -> string
50 val find_suggested_facts : Proof.context -> ('b * thm) list -> string list -> ('b * thm) list
51 val crude_thm_ord : Proof.context -> thm ord
52 val thm_less : thm * thm -> bool
53 val goal_of_thm : theory -> thm -> thm
54 val run_prover_for_mash : Proof.context -> params -> string -> string -> fact list -> thm ->
56 val features_of : Proof.context -> string -> stature -> term list -> string list
57 val trim_dependencies : string list -> string list option
58 val isar_dependencies_of : string Symtab.table * string Symtab.table -> thm -> string list option
59 val prover_dependencies_of : Proof.context -> params -> string -> int -> raw_fact list ->
60 string Symtab.table * string Symtab.table -> thm -> bool * string list
61 val attach_parents_to_facts : ('a * thm) list -> ('a * thm) list ->
62 (string list * ('a * thm)) list
63 val num_extra_feature_facts : int
64 val extra_feature_factor : real
65 val weight_facts_smoothly : 'a list -> ('a * real) list
66 val weight_facts_steeply : 'a list -> ('a * real) list
67 val find_mash_suggestions : Proof.context -> int -> string list -> ('a * thm) list ->
68 ('a * thm) list -> ('a * thm) list -> ('a * thm) list * ('a * thm) list
69 val mash_suggested_facts : Proof.context -> string -> params -> int -> term list -> term ->
70 raw_fact list -> fact list * fact list
72 val mash_unlearn : Proof.context -> unit
73 val mash_learn_proof : Proof.context -> params -> term -> thm list -> unit
74 val mash_learn_facts : Proof.context -> params -> string -> int -> bool -> Time.time ->
75 raw_fact list -> string
76 val mash_learn : Proof.context -> params -> fact_override -> thm list -> bool -> unit
77 val mash_can_suggest_facts : Proof.context -> bool
78 val mash_can_suggest_facts_fast : Proof.context -> bool
80 val generous_max_suggestions : int -> int
81 val mepo_weight : real
82 val mash_weight : real
83 val relevant_facts : Proof.context -> params -> string -> int -> fact_override -> term list ->
84 term -> raw_fact list -> (string * fact list) list
87 structure Sledgehammer_MaSh : SLEDGEHAMMER_MASH =
91 open ATP_Problem_Generate
92 open Sledgehammer_Util
93 open Sledgehammer_Fact
94 open Sledgehammer_Prover
95 open Sledgehammer_Prover_Minimize
96 open Sledgehammer_MePo
98 val anonymous_proof_prefix = "."
100 val trace = Attrib.setup_config_bool \<^binding>\<open>sledgehammer_mash_trace\<close> (K false)
101 val duplicates = Attrib.setup_config_bool \<^binding>\<open>sledgehammer_fact_duplicates\<close> (K false)
103 fun trace_msg ctxt msg = if Config.get ctxt trace then tracing (msg ()) else ()
105 fun gen_eq_thm ctxt = if Config.get ctxt duplicates then Thm.eq_thm_strict else Thm.eq_thm_prop
115 val fact_filters = [meshN, mepoN, mashN]
117 val unlearnN = "unlearn"
118 val learn_isarN = "learn_isar"
119 val learn_proverN = "learn_prover"
120 val relearn_isarN = "relearn_isar"
121 val relearn_proverN = "relearn_prover"
123 fun map_array_at ary f i = Array.update (ary, i, f (Array.sub (ary, i)))
125 type xtab = int * int Symtab.table
127 val empty_xtab = (0, Symtab.empty)
129 fun add_to_xtab key (next, tab) = (next + 1, Symtab.update_new (key, next) tab)
130 fun maybe_add_to_xtab key = perhaps (try (add_to_xtab key))
132 fun state_file () = Path.expand (Path.explode "$ISABELLE_HOME_USER/mash_state")
133 val remove_state_file = try File.rm o state_file
135 datatype mash_algorithm =
142 fun mash_algorithm () =
143 (case Options.default_string \<^system_option>\<open>MaSh\<close> of
144 "yes" => SOME MaSh_NB_kNN
145 | "sml" => SOME MaSh_NB_kNN
146 | "nb" => SOME MaSh_NB
147 | "knn" => SOME MaSh_kNN
148 | "nb_knn" => SOME MaSh_NB_kNN
149 | "nb_ext" => SOME MaSh_NB_Ext
150 | "knn_ext" => SOME MaSh_kNN_Ext
153 | algorithm => (warning ("Unknown MaSh algorithm: " ^ quote algorithm); NONE))
155 val is_mash_enabled = is_some o mash_algorithm
156 val the_mash_algorithm = the_default MaSh_NB_kNN o mash_algorithm
158 fun str_of_mash_algorithm MaSh_NB = "nb"
159 | str_of_mash_algorithm MaSh_kNN = "knn"
160 | str_of_mash_algorithm MaSh_NB_kNN = "nb_knn"
161 | str_of_mash_algorithm MaSh_NB_Ext = "nb_ext"
162 | str_of_mash_algorithm MaSh_kNN_Ext = "knn_ext"
164 fun scaled_avg [] = 0
165 | scaled_avg xs = Real.ceil (100000000.0 * fold (curry (op +)) xs 0.0) div length xs
168 | avg xs = fold (curry (op +)) xs 0.0 / Real.fromInt (length xs)
170 fun normalize_scores _ [] = []
171 | normalize_scores max_facts xs =
172 map (apsnd (curry (op *) (1.0 / avg (map snd (take max_facts xs))))) xs
174 fun mesh_facts maybe_distinct _ max_facts [(_, (sels, unks))] =
175 map fst (take max_facts sels) @ take (max_facts - length sels) unks
177 | mesh_facts _ fact_eq max_facts mess =
179 val mess = mess |> map (apsnd (apfst (normalize_scores max_facts)))
181 fun score_in fact (global_weight, (sels, unks)) =
182 let val score_at = try (nth sels) #> Option.map (fn (_, score) => global_weight * score) in
183 (case find_index (curry fact_eq fact o fst) sels of
184 ~1 => if member fact_eq unks fact then NONE else SOME 0.0
185 | rank => score_at rank)
188 fun weight_of fact = mess |> map_filter (score_in fact) |> scaled_avg
190 fold (union fact_eq o map fst o take max_facts o fst o snd) mess []
191 |> map (`weight_of) |> sort (int_ord o apply2 fst o swap)
192 |> map snd |> take max_facts
195 fun smooth_weight_of_fact rank = Math.pow (1.3, 15.5 - 0.2 * Real.fromInt rank) + 15.0 (* FUDGE *)
196 fun steep_weight_of_fact rank = Math.pow (0.62, log2 (Real.fromInt (rank + 1))) (* FUDGE *)
198 fun weight_facts_smoothly facts = facts ~~ map smooth_weight_of_fact (0 upto length facts - 1)
199 fun weight_facts_steeply facts = facts ~~ map steep_weight_of_fact (0 upto length facts - 1)
201 fun sort_array_suffix cmp needed a =
203 exception BOTTOM of int
205 val al = Array.length a
208 let val i31 = i + i + i + 1 in
210 let val x = Unsynchronized.ref i31 in
211 if is_less (cmp (Array.sub (a, i31), Array.sub (a, i31 + 1))) then x := i31 + 1 else ();
212 if is_less (cmp (Array.sub (a, !x), Array.sub (a, i31 + 2))) then x := i31 + 2 else ();
216 if i31 + 1 < l andalso is_less (cmp (Array.sub (a, i31), Array.sub (a, i31 + 1)))
217 then i31 + 1 else if i31 < l then i31 else raise BOTTOM i
220 fun trickledown l i e =
221 let val j = maxson l i in
222 if is_greater (cmp (Array.sub (a, j), e)) then
223 (Array.update (a, i, Array.sub (a, j)); trickledown l j e)
225 Array.update (a, i, e)
228 fun trickle l i e = trickledown l i e handle BOTTOM i => Array.update (a, i, e)
231 let val j = maxson l i in
232 Array.update (a, i, Array.sub (a, j));
236 fun bubble l i = bubbledown l i handle BOTTOM i => i
239 let val father = (i - 1) div 3 in
240 if is_less (cmp (Array.sub (a, father), e)) then
241 (Array.update (a, i, Array.sub (a, father));
242 if father > 0 then trickleup father e else Array.update (a, 0, e))
244 Array.update (a, i, e)
247 fun for i = if i < 0 then () else (trickle al i (Array.sub (a, i)); for (i - 1))
250 if i < Integer.max 2 (al - needed) then
253 let val e = Array.sub (a, i) in
254 Array.update (a, i, Array.sub (a, 0));
255 trickleup (bubble i 0) e;
259 for (((al + 1) div 3) - 1);
262 let val e = Array.sub (a, 1) in
263 Array.update (a, 1, Array.sub (a, 0));
264 Array.update (a, 0, e)
270 fun rev_sort_list_prefix cmp needed xs =
271 let val ary = Array.fromList xs in
272 sort_array_suffix cmp needed ary;
273 Array.foldl (op ::) [] ary
277 (*** Convenience functions for synchronized access ***)
279 fun synchronized_timed_value var time_limit =
280 Synchronized.timed_access var time_limit (fn value => SOME (value, value))
281 fun synchronized_timed_change_result var time_limit f =
282 Synchronized.timed_access var time_limit (SOME o f)
283 fun synchronized_timed_change var time_limit f =
284 synchronized_timed_change_result var time_limit (fn x => ((), f x))
286 fun mash_time_limit _ = SOME (seconds 0.1)
289 (*** Isabelle-agnostic machine learning ***)
294 fun select_fact_idxs (big_number : real) recommends =
296 let val (j, ov) = Array.sub (recommends, at) in
297 Array.update (recommends, at, (j, big_number + ov))
300 fun wider_array_of_vector init vec =
301 let val ary = Array.array init in
302 Array.copyVec {src = vec, dst = ary, di = 0};
306 val nb_def_prior_weight = 1000 (* FUDGE *)
308 fun learn_facts (tfreq0, sfreq0, dffreq0) num_facts0 num_facts num_feats depss featss =
310 val tfreq = wider_array_of_vector (num_facts, 0) tfreq0
311 val sfreq = wider_array_of_vector (num_facts, Inttab.empty) sfreq0
312 val dffreq = wider_array_of_vector (num_feats, 0) dffreq0
314 fun learn_one th feats deps =
316 fun add_th weight t =
318 val im = Array.sub (sfreq, t)
319 fun fold_fn s = Inttab.map_default (s, 0) (Integer.add weight)
321 map_array_at tfreq (Integer.add weight) t;
322 Array.update (sfreq, t, fold fold_fn feats im)
325 val add_sym = map_array_at dffreq (Integer.add 1)
327 add_th nb_def_prior_weight th;
328 List.app (add_th 1) deps;
329 List.app add_sym feats
333 if i = num_facts then ()
334 else (learn_one i (Vector.sub (featss, i)) (Vector.sub (depss, i)); for (i + 1))
337 (Array.vector tfreq, Array.vector sfreq, Array.vector dffreq)
340 fun naive_bayes (tfreq, sfreq, dffreq) num_facts max_suggs fact_idxs goal_feats =
342 val tau = 0.2 (* FUDGE *)
343 val pos_weight = 5.0 (* FUDGE *)
344 val def_val = ~18.0 (* FUDGE *)
345 val init_val = 30.0 (* FUDGE *)
347 val ln_afreq = Math.ln (Real.fromInt num_facts)
348 val idf = Vector.map (fn i => ln_afreq - Math.ln (Real.fromInt i)) dffreq
350 fun tfidf feat = Vector.sub (idf, feat)
352 fun log_posterior i =
354 val tfreq = Real.fromInt (Vector.sub (tfreq, i))
356 fun add_feat (f, fw0) (res, sfh) =
357 (case Inttab.lookup sfh f of
359 (res + fw0 * tfidf f * Math.ln (pos_weight * Real.fromInt sf / tfreq),
361 | NONE => (res + fw0 * tfidf f * def_val, sfh))
363 val (res, sfh) = fold add_feat goal_feats (init_val * Math.ln tfreq, Vector.sub (sfreq, i))
365 fun fold_sfh (f, sf) sow =
366 sow + tfidf f * Math.ln (1.0 - Real.fromInt (sf - 1) / tfreq)
368 val sum_of_weights = Inttab.fold fold_sfh sfh 0.0
370 res + tau * sum_of_weights
373 val posterior = Array.tabulate (num_facts, (fn j => (j, log_posterior j)))
376 if at = num_facts then acc else ret (at + 1) (Array.sub (posterior, at) :: acc)
378 select_fact_idxs 100000.0 posterior fact_idxs;
379 sort_array_suffix (Real.compare o apply2 snd) max_suggs posterior;
380 ret (Integer.max 0 (num_facts - max_suggs)) []
385 fun k_nearest_neighbors dffreq num_facts num_feats depss featss max_suggs fact_idxs goal_feats =
387 exception EXIT of unit
389 val ln_afreq = Math.ln (Real.fromInt num_facts)
390 fun tfidf feat = ln_afreq - Math.ln (Real.fromInt (Vector.sub (dffreq, feat)))
392 val overlaps_sqr = Array.tabulate (num_facts, rpair 0.0)
394 val feat_facts = Array.array (num_feats, [])
395 val _ = Vector.foldl (fn (feats, fact) =>
396 (List.app (map_array_at feat_facts (cons fact)) feats; fact + 1)) 0 featss
398 fun do_feat (s, sw0) =
400 val sw = sw0 * tfidf s
401 val w6 = Math.pow (sw, 6.0 (* FUDGE *))
404 let val (_, ov) = Array.sub (overlaps_sqr, j) in
405 Array.update (overlaps_sqr, j, (j, w6 + ov))
408 List.app inc_overlap (Array.sub (feat_facts, s))
411 val _ = List.app do_feat goal_feats
412 val _ = sort_array_suffix (Real.compare o apply2 snd) num_facts overlaps_sqr
413 val no_recommends = Unsynchronized.ref 0
414 val recommends = Array.tabulate (num_facts, rpair 0.0)
415 val age = Unsynchronized.ref 500000000.0
417 fun inc_recommend v j =
418 let val (_, ov) = Array.sub (recommends, j) in
420 (no_recommends := !no_recommends + 1; Array.update (recommends, j, (j, !age + ov)))
422 Array.update (recommends, j, (j, v + ov))
425 val k = Unsynchronized.ref 0
427 if k >= num_facts then
431 val deps_factor = 2.7 (* FUDGE *)
432 val (j, o2) = Array.sub (overlaps_sqr, num_facts - k - 1)
433 val _ = inc_recommend o2 j
434 val ds = Vector.sub (depss, j)
435 val l = Real.fromInt (length ds)
437 List.app (inc_recommend (deps_factor * o2 / l)) ds
441 if !k = initial_k + 1 then () else (do_k (!k); k := !k + 1; while1 ())
445 if !no_recommends >= max_suggs then ()
446 else (do_k (!k); k := !k + 1; age := !age - 10000.0; while2 ())
450 if at = num_facts then acc else ret (Array.sub (recommends, at) :: acc) (at + 1)
454 select_fact_idxs 1000000000.0 recommends fact_idxs;
455 sort_array_suffix (Real.compare o apply2 snd) max_suggs recommends;
456 ret [] (Integer.max 0 (num_facts - max_suggs))
460 fun external_tool tool max_suggs learns goal_feats =
462 val ser = string_of_int (serial ()) (* poor person's attempt at thread-safety *)
463 val ocs = TextIO.openOut ("adv_syms" ^ ser)
464 val ocd = TextIO.openOut ("adv_deps" ^ ser)
465 val ocq = TextIO.openOut ("adv_seq" ^ ser)
466 val occ = TextIO.openOut ("adv_conj" ^ ser)
468 fun os oc s = TextIO.output (oc, s)
472 | ol oc f sep (h :: t) = (f h; os oc sep; ol oc f sep t)
474 fun do_learn (name, feats, deps) =
475 (os ocs name; os ocs ":"; ol ocs (os ocs o quote) ", " feats; os ocs "\n";
476 os ocd name; os ocd ":"; ol ocd (os ocd) " " deps; os ocd "\n"; os ocq name; os ocq "\n")
481 "~/misc/" ^ tool ^ " adv_syms" ^ ser ^ " adv_deps" ^ ser ^ " " ^ string_of_int no ^
482 " adv_seq" ^ ser ^ " < adv_conj" ^ ser
484 fst (Isabelle_System.bash_output cmd)
486 |> filter_out (curry (op =) "")
489 (List.app do_learn learns; ol occ (os occ o quote) ", " (map fst goal_feats);
490 TextIO.closeOut ocs; TextIO.closeOut ocd; TextIO.closeOut ocq; TextIO.closeOut occ;
494 fun k_nearest_neighbors_ext max_suggs =
495 external_tool ("newknn/knn" ^ " " ^ string_of_int initial_k) max_suggs
496 fun naive_bayes_ext max_suggs = external_tool "predict/nbayes" max_suggs
498 fun query_external ctxt algorithm max_suggs learns goal_feats =
499 (trace_msg ctxt (fn () => "MaSh query external " ^ commas (map fst goal_feats));
501 MaSh_NB_Ext => naive_bayes_ext max_suggs learns goal_feats
502 | MaSh_kNN_Ext => k_nearest_neighbors_ext max_suggs learns goal_feats))
504 fun query_internal ctxt algorithm num_facts num_feats (fact_names, featss, depss)
505 (freqs as (_, _, dffreq)) fact_idxs max_suggs goal_feats int_goal_feats =
508 naive_bayes freqs num_facts max_suggs fact_idxs int_goal_feats
511 k_nearest_neighbors dffreq num_facts num_feats depss featss max_suggs fact_idxs int_goal_feats
514 (trace_msg ctxt (fn () => "MaSh query internal " ^ commas (map fst goal_feats) ^ " from {" ^
515 elide_string 1000 (space_implode " " (Vector.foldr (op ::) [] fact_names)) ^ "}");
520 mesh_facts I (op =) max_suggs
521 [(0.5 (* FUDGE *), (weight_facts_steeply (nb ()), [])),
522 (0.5 (* FUDGE *), (weight_facts_steeply (knn ()), []))])
523 |> map (curry Vector.sub fact_names))
529 (*** Persistent, stringly-typed state ***)
532 if Char.isAlphaNum c orelse c = #"_" orelse c = #"." orelse c = #"(" orelse c = #")" orelse
533 c = #"," orelse c = #"'" then
536 (* fixed width, in case more digits follow *)
537 "%" ^ stringN_of_int 3 (Char.ord c)
539 fun unmeta_chars accum [] = String.implode (rev accum)
540 | unmeta_chars accum (#"%" :: d1 :: d2 :: d3 :: cs) =
541 (case Int.fromString (String.implode [d1, d2, d3]) of
542 SOME n => unmeta_chars (Char.chr n :: accum) cs
543 | NONE => "" (* error *))
544 | unmeta_chars _ (#"%" :: _) = "" (* error *)
545 | unmeta_chars accum (c :: cs) = unmeta_chars (c :: accum) cs
547 val encode_str = String.translate meta_char
548 val encode_strs = map encode_str #> space_implode " "
551 if String.isSubstring "%" s then unmeta_chars [] (String.explode s) else s;
554 space_explode " " s |> String.isSubstring "%" s ? map decode_str;
556 datatype proof_kind = Isar_Proof | Automatic_Proof | Isar_Proof_wegen_Prover_Flop
558 fun str_of_proof_kind Isar_Proof = "i"
559 | str_of_proof_kind Automatic_Proof = "a"
560 | str_of_proof_kind Isar_Proof_wegen_Prover_Flop = "x"
562 fun proof_kind_of_str "a" = Automatic_Proof
563 | proof_kind_of_str "x" = Isar_Proof_wegen_Prover_Flop
564 | proof_kind_of_str _ (* "i" *) = Isar_Proof
566 fun add_edge_to name parent =
567 Graph.default_node (parent, (Isar_Proof, [], []))
568 #> Graph.add_edge (parent, name)
570 fun add_node kind name parents feats deps (accum as (access_G, (fact_xtab, feat_xtab), learns)) =
571 let val fact_xtab' = add_to_xtab name fact_xtab in
572 ((Graph.new_node (name, (kind, feats, deps)) access_G
573 handle Graph.DUP _ => Graph.map_node name (K (kind, feats, deps)) access_G)
574 |> fold (add_edge_to name) parents,
575 (fact_xtab', fold maybe_add_to_xtab feats feat_xtab),
576 (name, feats, deps) :: learns)
578 handle Symtab.DUP _ => accum (* robustness (in case the state file violates the invariant) *)
580 fun try_graph ctxt when def f =
583 Graph.CYCLES (cycle :: _) =>
584 (trace_msg ctxt (fn () => "Cycle involving " ^ commas cycle ^ " when " ^ when); def)
586 (trace_msg ctxt (fn () => "Duplicate fact " ^ quote name ^ " when " ^ when); def)
587 | Graph.UNDEF name =>
588 (trace_msg ctxt (fn () => "Unknown fact " ^ quote name ^ " when " ^ when); def)
590 if Exn.is_interrupt exn then
593 (trace_msg ctxt (fn () => "Internal error when " ^ when ^ ":\n" ^ Runtime.exn_message exn);
597 string_of_int (length (Graph.keys G)) ^ " node(s), " ^
598 string_of_int (fold (Integer.add o length o snd) (Graph.dest G) 0) ^ " edge(s), " ^
599 string_of_int (length (Graph.maximals G)) ^ " maximal"
601 type ffds = string vector * int list vector * int list vector
602 type freqs = int vector * int Inttab.table vector * int vector
605 {access_G : (proof_kind * string list * string list) Graph.T,
609 dirty_facts : string list option}
611 val empty_xtabs = (empty_xtab, empty_xtab)
612 val empty_ffds = (Vector.fromList [], Vector.fromList [], Vector.fromList []) : ffds
613 val empty_freqs = (Vector.fromList [], Vector.fromList [], Vector.fromList []) : freqs
616 {access_G = Graph.empty,
620 dirty_facts = SOME []} : mash_state
622 fun recompute_ffds_freqs_from_learns (learns : (string * string list * string list) list)
623 ((num_facts, fact_tab), (num_feats, feat_tab)) num_facts0 (fact_names0, featss0, depss0) freqs0 =
625 val fact_names = Vector.concat [fact_names0, Vector.fromList (map #1 learns)]
626 val featss = Vector.concat [featss0,
627 Vector.fromList (map (map_filter (Symtab.lookup feat_tab) o #2) learns)]
628 val depss = Vector.concat [depss0,
629 Vector.fromList (map (map_filter (Symtab.lookup fact_tab) o #3) learns)]
631 ((fact_names, featss, depss),
632 MaSh.learn_facts freqs0 num_facts0 num_facts num_feats depss featss)
635 fun reorder_learns (num_facts, fact_tab) learns =
636 let val ary = Array.array (num_facts, ("", [], [])) in
637 List.app (fn learn as (fact, _, _) =>
638 Array.update (ary, the (Symtab.lookup fact_tab fact), learn))
640 Array.foldr (op ::) [] ary
643 fun recompute_ffds_freqs_from_access_G access_G (xtabs as (fact_xtab, _)) =
646 Graph.schedule (fn _ => fn (fact, (_, feats, deps)) => (fact, feats, deps)) access_G
647 |> reorder_learns fact_xtab
649 recompute_ffds_freqs_from_learns learns xtabs 0 empty_ffds empty_freqs
654 val version = "*** MaSh version 20190121 ***"
656 exception FILE_VERSION_TOO_NEW of unit
658 fun extract_node line =
659 (case space_explode ":" line of
661 (case (space_explode " " head, map (unprefix " ") (space_explode ";" tail)) of
662 ([kind, name], [parents, feats, deps]) =>
663 SOME (proof_kind_of_str kind, decode_str name, decode_strs parents, decode_strs feats,
668 fun would_load_state (memory_time, _) =
669 let val path = state_file () in
670 (case try OS.FileSys.modTime (File.platform_path path) of
672 | SOME disk_time => memory_time < disk_time)
675 fun load_state ctxt (time_state as (memory_time, _)) =
676 let val path = state_file () in
677 (case try OS.FileSys.modTime (File.platform_path path) of
680 if memory_time >= disk_time then
684 (case try File.read_lines path of
685 SOME (version' :: node_lines) =>
687 fun extract_line_and_add_node line =
688 (case extract_node line of
689 NONE => I (* should not happen *)
690 | SOME (kind, name, parents, feats, deps) => add_node kind name parents feats deps)
692 val empty_G_etc = (Graph.empty, empty_xtabs, [])
694 val (access_G, xtabs, rev_learns) =
695 (case string_ord (version', version) of
697 try_graph ctxt "loading state" empty_G_etc
698 (fn () => fold extract_line_and_add_node node_lines empty_G_etc)
699 | LESS => (remove_state_file (); empty_G_etc) (* cannot parse old file *)
700 | GREATER => raise FILE_VERSION_TOO_NEW ())
703 recompute_ffds_freqs_from_learns (rev rev_learns) xtabs 0 empty_ffds empty_freqs
705 trace_msg ctxt (fn () => "Loaded fact graph (" ^ graph_info access_G ^ ")");
706 {access_G = access_G, xtabs = xtabs, ffds = ffds, freqs = freqs, dirty_facts = SOME []}
708 | _ => empty_state)))
711 fun str_of_entry (kind, name, parents, feats, deps) =
712 str_of_proof_kind kind ^ " " ^ encode_str name ^ ": " ^ encode_strs parents ^ "; " ^
713 encode_strs feats ^ "; " ^ encode_strs deps ^ "\n"
715 fun save_state _ (time_state as (_, {dirty_facts = SOME [], ...})) = time_state
716 | save_state ctxt (memory_time, {access_G, xtabs, ffds, freqs, dirty_facts}) =
718 fun append_entry (name, ((kind, feats, deps), (parents, _))) =
719 cons (kind, name, Graph.Keys.dest parents, feats, deps)
721 val path = state_file ()
723 (case try OS.FileSys.modTime (File.platform_path path) of
725 | SOME disk_time => if disk_time <= memory_time then dirty_facts else NONE)
726 val (banner, entries) =
727 (case dirty_facts' of
728 SOME names => (NONE, fold (append_entry o Graph.get_entry access_G) names [])
729 | NONE => (SOME (version ^ "\n"), Graph.fold append_entry access_G []))
731 (case banner of SOME s => File.write path s | NONE => ();
732 entries |> chunk_list 500 |> List.app (File.append path o implode o map str_of_entry))
733 handle IO.Io _ => ();
734 trace_msg ctxt (fn () =>
735 "Saved fact graph (" ^ graph_info access_G ^
737 SOME dirty_facts => "; " ^ string_of_int (length dirty_facts) ^ " dirty fact(s)"
740 {access_G = access_G, xtabs = xtabs, ffds = ffds, freqs = freqs, dirty_facts = SOME []})
743 val global_state = Synchronized.var "Sledgehammer_MaSh.global_state" (Time.zeroTime, empty_state)
747 fun map_state ctxt f =
748 (trace_msg ctxt (fn () => "Changing MaSh state");
749 synchronized_timed_change global_state mash_time_limit
750 (load_state ctxt ##> f #> save_state ctxt))
752 handle FILE_VERSION_TOO_NEW () => ()
754 fun peek_state ctxt =
755 (trace_msg ctxt (fn () => "Peeking at MaSh state");
756 (case synchronized_timed_value global_state mash_time_limit of
758 | SOME state => if would_load_state state then NONE else SOME state))
761 (trace_msg ctxt (fn () => "Retrieving MaSh state");
762 synchronized_timed_change_result global_state mash_time_limit
763 (perhaps (try (load_state ctxt)) #> `snd))
765 fun clear_state ctxt =
766 (trace_msg ctxt (fn () => "Clearing MaSh state");
767 Synchronized.change global_state (fn _ => (remove_state_file (); (Time.zeroTime, empty_state))))
772 (*** Isabelle helpers ***)
774 fun crude_printed_term size t =
776 fun term _ (res, 0) = (res, 0)
777 | term (t $ u) (res, size) =
779 val (res, size) = term t (res ^ "(", size)
780 val (res, size) = term u (res ^ " ", size)
784 | term (Abs (s, _, t)) (res, size) = term t (res ^ "%" ^ s ^ ".", size - 1)
785 | term (Bound n) (res, size) = (res ^ "#" ^ string_of_int n, size - 1)
786 | term (Const (s, _)) (res, size) = (res ^ Long_Name.base_name s, size - 1)
787 | term (Free (s, _)) (res, size) = (res ^ s, size - 1)
788 | term (Var ((s, _), _)) (res, size) = (res ^ s, size - 1)
790 fst (term t ("", size))
793 fun nickname_of_thm th =
794 if Thm.has_name_hint th then
795 let val hint = Thm.get_name_hint th in
796 (* There must be a better way to detect local facts. *)
797 (case Long_Name.dest_local hint of
799 Long_Name.implode [Thm.theory_name th, suf, crude_printed_term 25 (Thm.prop_of th)]
803 crude_printed_term 50 (Thm.prop_of th)
805 fun find_suggested_facts ctxt facts =
807 fun add (fact as (_, th)) = Symtab.default (nickname_of_thm th, fact)
808 val tab = fold add facts Symtab.empty
810 Symtab.lookup tab nick
811 |> tap (fn NONE => trace_msg ctxt (fn () => "Cannot find " ^ quote nick) | _ => ())
812 in map_filter lookup end
814 fun free_feature_of s = "f" ^ s
815 fun thy_feature_of s = "y" ^ s
816 fun type_feature_of s = "t" ^ s
817 fun class_feature_of s = "s" ^ s
818 val local_feature = "local"
820 fun crude_thm_ord ctxt =
822 val ancestor_lengths =
823 fold (fn thy => Symtab.update (Context.theory_name thy, length (Context.ancestors_of thy)))
824 (Theory.nodes_of (Proof_Context.theory_of ctxt)) Symtab.empty
825 val ancestor_length = Symtab.lookup ancestor_lengths o Context.theory_id_name
827 fun crude_theory_ord p =
828 if Context.eq_thy_id p then EQUAL
829 else if Context.proper_subthy_id p then LESS
830 else if Context.proper_subthy_id (swap p) then GREATER
832 (case apply2 ancestor_length p of
834 (case int_ord (m, n) of
835 EQUAL => string_ord (apply2 Context.theory_id_name p)
837 | _ => string_ord (apply2 Context.theory_id_name p))
840 (case crude_theory_ord (apply2 Thm.theory_id p) of
842 (* The hack below is necessary because of odd dependencies that are not reflected in the theory
844 let val q = apply2 nickname_of_thm p in
845 (* Hack to put "xxx_def" before "xxxI" and "xxxE" *)
846 (case bool_ord (apply2 (String.isSuffix "_def") (swap q)) of
847 EQUAL => string_ord q
853 val thm_less_eq = Context.subthy_id o apply2 Thm.theory_id
854 fun thm_less p = thm_less_eq p andalso not (thm_less_eq (swap p))
856 val freezeT = Type.legacy_freeze_type
858 fun freeze (t $ u) = freeze t $ freeze u
859 | freeze (Abs (s, T, t)) = Abs (s, freezeT T, freeze t)
860 | freeze (Var ((s, _), T)) = Free (s, freezeT T)
861 | freeze (Const (s, T)) = Const (s, freezeT T)
862 | freeze (Free (s, T)) = Free (s, freezeT T)
865 fun goal_of_thm thy = Thm.prop_of #> freeze #> Thm.global_cterm_of thy #> Goal.init
867 fun run_prover_for_mash ctxt params prover goal_name facts goal =
870 {comment = "Goal: " ^ goal_name, state = Proof.init ctxt, goal = goal, subgoal = 1,
871 subgoal_count = 1, factss = [("", facts)], found_proof = I}
873 get_minimizing_prover ctxt MaSh (K ()) prover params problem
876 val bad_types = [\<^type_name>\<open>prop\<close>, \<^type_name>\<open>bool\<close>, \<^type_name>\<open>fun\<close>]
878 val crude_str_of_sort = space_implode "," o map Long_Name.base_name o subtract (op =) \<^sort>\<open>type\<close>
880 fun crude_str_of_typ (Type (s, [])) = Long_Name.base_name s
881 | crude_str_of_typ (Type (s, Ts)) = Long_Name.base_name s ^ implode (map crude_str_of_typ Ts)
882 | crude_str_of_typ (TFree (_, S)) = crude_str_of_sort S
883 | crude_str_of_typ (TVar (_, S)) = crude_str_of_sort S
885 fun maybe_singleton_str "" = []
886 | maybe_singleton_str s = [s]
888 val max_pat_breadth = 5 (* FUDGE *)
890 fun term_features_of ctxt thy_name term_max_depth type_max_depth ts =
892 val thy = Proof_Context.theory_of ctxt
894 val fixes = map snd (Variable.dest_fixes ctxt)
895 val classes = Sign.classes_of thy
897 fun add_classes \<^sort>\<open>type\<close> = I
899 fold (`(Sorts.super_classes classes)
901 #> subtract (op =) \<^sort>\<open>type\<close>
902 #> map class_feature_of
905 fun pattify_type 0 _ = []
906 | pattify_type _ (Type (s, [])) = if member (op =) bad_types s then [] else [s]
907 | pattify_type depth (Type (s, U :: Ts)) =
910 val ps = take max_pat_breadth (pattify_type depth T)
911 val qs = take max_pat_breadth ("" :: pattify_type (depth - 1) U)
913 map_product (fn p => fn "" => p | q => p ^ "(" ^ q ^ ")") ps qs
915 | pattify_type _ (TFree (_, S)) = maybe_singleton_str (crude_str_of_sort S)
916 | pattify_type _ (TVar (_, S)) = maybe_singleton_str (crude_str_of_sort S)
918 fun add_type_pat depth T =
919 union (op =) (map type_feature_of (pattify_type depth T))
921 fun add_type_pats 0 _ = I
922 | add_type_pats depth t = add_type_pat depth t #> add_type_pats (depth - 1) t
925 add_type_pats type_max_depth T
926 #> fold_atyps_sorts (add_classes o snd) T
928 fun add_subtypes (T as Type (_, Ts)) = add_type T #> fold add_subtypes Ts
929 | add_subtypes T = add_type T
931 fun pattify_term _ 0 _ = []
932 | pattify_term _ _ (Const (s, _)) =
933 if is_widely_irrelevant_const s then [] else [s]
934 | pattify_term _ _ (Free (s, T)) =
935 maybe_singleton_str (crude_str_of_typ T)
936 |> (if member (op =) fixes s then cons (free_feature_of (Long_Name.append thy_name s))
938 | pattify_term _ _ (Var (_, T)) =
939 maybe_singleton_str (crude_str_of_typ T)
940 | pattify_term Ts _ (Bound j) =
941 maybe_singleton_str (crude_str_of_typ (nth Ts j))
942 | pattify_term Ts depth (t $ u) =
944 val ps = take max_pat_breadth (pattify_term Ts depth t)
945 val qs = take max_pat_breadth ("" :: pattify_term Ts (depth - 1) u)
947 map_product (fn p => fn "" => p | q => p ^ "(" ^ q ^ ")") ps qs
949 | pattify_term _ _ _ = []
951 fun add_term_pat Ts = union (op =) oo pattify_term Ts
953 fun add_term_pats _ 0 _ = I
954 | add_term_pats Ts depth t = add_term_pat Ts depth t #> add_term_pats Ts (depth - 1) t
956 fun add_term Ts = add_term_pats Ts term_max_depth
958 fun add_subterms Ts t =
959 (case strip_comb t of
960 (Const (s, T), args) =>
961 (not (is_widely_irrelevant_const s) ? add_term Ts t)
962 #> add_subtypes T #> fold (add_subterms Ts) args
965 Free (_, T) => add_term Ts t #> add_subtypes T
966 | Var (_, T) => add_subtypes T
967 | Abs (_, T, body) => add_subtypes T #> add_subterms (T :: Ts) body
969 #> fold (add_subterms Ts) args)
971 fold (add_subterms []) ts []
974 val term_max_depth = 2
975 val type_max_depth = 1
977 (* TODO: Generate type classes for types? *)
978 fun features_of ctxt thy_name (scope, _) ts =
979 thy_feature_of thy_name ::
980 term_features_of ctxt thy_name term_max_depth type_max_depth ts
981 |> scope <> Global ? cons local_feature
983 (* Too many dependencies is a sign that a decision procedure is at work. There is not much to learn
985 val max_dependencies = 20 (* FUDGE *)
987 val prover_default_max_facts = 25 (* FUDGE *)
989 (* "type_definition_xxx" facts are characterized by their use of "CollectI". *)
990 val typedef_dep = nickname_of_thm @{thm CollectI}
991 (* Mysterious parts of the class machinery create lots of proofs that refer exclusively to
992 "someI_ex" (and to some internal constructions). *)
993 val class_some_dep = nickname_of_thm @{thm someI_ex}
996 @{thms fundef_ex1_existence fundef_ex1_uniqueness fundef_ex1_iff fundef_default_value}
997 |> map nickname_of_thm
999 (* "Rep_xxx_inject", "Abs_xxx_inverse", etc., are derived using these facts. *)
1001 @{thms type_definition.Abs_inverse type_definition.Rep_inverse type_definition.Rep
1002 type_definition.Rep_inject type_definition.Abs_inject type_definition.Rep_cases
1003 type_definition.Abs_cases type_definition.Rep_induct type_definition.Abs_induct
1004 type_definition.Rep_range type_definition.Abs_image}
1005 |> map nickname_of_thm
1007 fun is_size_def [dep] th =
1008 (case first_field ".rec" dep of
1010 (case first_field ".size" (nickname_of_thm th) of
1011 SOME (pref', _) => pref = pref'
1014 | is_size_def _ _ = false
1016 fun trim_dependencies deps =
1017 if length deps > max_dependencies then NONE else SOME deps
1019 fun isar_dependencies_of name_tabs th =
1020 thms_in_proof max_dependencies (SOME name_tabs) th
1021 |> Option.map (fn deps =>
1022 if deps = [typedef_dep] orelse deps = [class_some_dep] orelse
1023 exists (member (op =) fundef_ths) deps orelse exists (member (op =) typedef_ths) deps orelse
1024 is_size_def deps th then
1029 fun prover_dependencies_of ctxt (params as {verbose, max_facts, ...}) prover auto_level facts
1031 (case isar_dependencies_of name_tabs th of
1032 SOME [] => (false, [])
1035 val isar_deps = these isar_deps0
1036 val thy = Proof_Context.theory_of ctxt
1037 val goal = goal_of_thm thy th
1038 val name = nickname_of_thm th
1039 val (_, hyp_ts, concl_t) = ATP_Util.strip_subgoal goal 1 ctxt
1040 val facts = facts |> filter (fn (_, th') => thm_less (th', th))
1042 fun nickify ((_, stature), th) = ((nickname_of_thm th, stature), th)
1044 fun is_dep dep (_, th) = (nickname_of_thm th = dep)
1046 fun add_isar_dep facts dep accum =
1047 if exists (is_dep dep) accum then
1050 (case find_first (is_dep dep) facts of
1051 SOME ((_, status), th) => accum @ [(("", status), th)]
1052 | NONE => accum (* should not happen *))
1056 |> mepo_suggested_facts ctxt params (max_facts |> the_default prover_default_max_facts) NONE
1060 |> fold (add_isar_dep facts) isar_deps
1062 val num_isar_deps = length isar_deps
1064 if verbose andalso auto_level = 0 then
1065 writeln ("MaSh: " ^ quote prover ^ " on " ^ quote name ^ " with " ^
1066 string_of_int num_isar_deps ^ " + " ^ string_of_int (length facts - num_isar_deps) ^
1070 (case run_prover_for_mash ctxt params prover name facts goal of
1071 {outcome = NONE, used_facts, ...} =>
1072 (if verbose andalso auto_level = 0 then
1073 let val num_facts = length used_facts in
1074 writeln ("Found proof with " ^ string_of_int num_facts ^ " fact" ^
1079 (true, map fst used_facts))
1080 | _ => (false, isar_deps))
1084 (*** High-level communication with MaSh ***)
1086 (* In the following functions, chunks are risers w.r.t. "thm_less_eq". *)
1088 fun chunks_and_parents_for chunks th =
1090 fun insert_parent new parents =
1091 let val parents = parents |> filter_out (fn p => thm_less_eq (p, new)) in
1092 parents |> forall (fn p => not (thm_less_eq (new, p))) parents ? cons new
1095 fun rechunk seen (rest as th' :: ths) =
1096 if thm_less_eq (th', th) then (rev seen, rest)
1097 else rechunk (th' :: seen) ths
1099 fun do_chunk [] accum = accum
1100 | do_chunk (chunk as hd_chunk :: _) (chunks, parents) =
1101 if thm_less_eq (hd_chunk, th) then
1102 (chunk :: chunks, insert_parent hd_chunk parents)
1103 else if thm_less_eq (List.last chunk, th) then
1104 let val (front, back as hd_back :: _) = rechunk [] chunk in
1105 (front :: back :: chunks, insert_parent hd_back parents)
1108 (chunk :: chunks, parents)
1110 fold_rev do_chunk chunks ([], [])
1112 ||> map nickname_of_thm
1115 fun attach_parents_to_facts _ [] = []
1116 | attach_parents_to_facts old_facts (facts as (_, th) :: _) =
1118 fun do_facts _ [] = []
1119 | do_facts (_, parents) [fact] = [(parents, fact)]
1120 | do_facts (chunks, parents)
1121 ((fact as (_, th)) :: (facts as (_, th') :: _)) =
1123 val chunks = app_hd (cons th) chunks
1124 val chunks_and_parents' =
1125 if thm_less_eq (th, th') andalso
1126 Thm.theory_name th = Thm.theory_name th'
1127 then (chunks, [nickname_of_thm th])
1128 else chunks_and_parents_for chunks th'
1130 (parents, fact) :: do_facts chunks_and_parents' facts
1134 |> do_facts (chunks_and_parents_for [[]] th)
1135 |> drop (length old_facts)
1138 fun is_fact_in_graph access_G = can (Graph.get_node access_G) o nickname_of_thm
1140 val chained_feature_factor = 0.5 (* FUDGE *)
1141 val extra_feature_factor = 0.1 (* FUDGE *)
1142 val num_extra_feature_facts = 10 (* FUDGE *)
1144 val max_proximity_facts = 100 (* FUDGE *)
1146 fun find_mash_suggestions ctxt max_facts suggs facts chained raw_unknown =
1148 val inter_fact = inter (eq_snd Thm.eq_thm_prop)
1149 val raw_mash = find_suggested_facts ctxt facts suggs
1150 val proximate = take max_proximity_facts facts
1151 val unknown_chained = inter_fact raw_unknown chained
1152 val unknown_proximate = inter_fact raw_unknown proximate
1154 [(0.9 (* FUDGE *), (map (rpair 1.0) unknown_chained, [])),
1155 (0.4 (* FUDGE *), (weight_facts_smoothly unknown_proximate, [])),
1156 (0.1 (* FUDGE *), (weight_facts_steeply raw_mash, raw_unknown))]
1157 val unknown = raw_unknown
1158 |> fold (subtract (eq_snd Thm.eq_thm_prop)) [unknown_chained, unknown_proximate]
1160 (mesh_facts (fact_distinct (op aconv)) (eq_snd (gen_eq_thm ctxt)) max_facts mess, unknown)
1163 fun mash_suggested_facts ctxt thy_name ({debug, ...} : params) max_suggs hyp_ts concl_t facts =
1165 val algorithm = the_mash_algorithm ()
1168 |> rev_sort_list_prefix (crude_thm_ord ctxt o apply2 snd)
1169 (Int.max (num_extra_feature_facts, max_proximity_facts))
1171 val chained = filter (fn ((_, (scope, _)), _) => scope = Chained) facts
1173 fun fact_has_right_theory (_, th) = thy_name = Thm.theory_name th
1175 fun chained_or_extra_features_of factor (((_, stature), th), weight) =
1177 |> features_of ctxt (Thm.theory_name th) stature
1178 |> map (rpair (weight * factor))
1180 (case get_state ctxt of
1182 | SOME {access_G, xtabs = ((num_facts, fact_tab), (num_feats, feat_tab)), ffds, freqs, ...} =>
1185 features_of ctxt thy_name (Local, General) (concl_t :: hyp_ts)
1186 val chained_feats = chained
1188 |> map (chained_or_extra_features_of chained_feature_factor)
1189 |> rpair [] |-> fold (union (eq_fst (op =)))
1190 val extra_feats = facts
1191 |> take (Int.max (0, num_extra_feature_facts - length chained))
1192 |> filter fact_has_right_theory
1193 |> weight_facts_steeply
1194 |> map (chained_or_extra_features_of extra_feature_factor)
1195 |> rpair [] |-> fold (union (eq_fst (op =)))
1198 fold (union (eq_fst (op =))) [chained_feats, extra_feats] (map (rpair 1.0) goal_feats0)
1199 |> debug ? sort (Real.compare o swap o apply2 snd)
1201 val fact_idxs = map_filter (Symtab.lookup fact_tab o nickname_of_thm o snd) facts
1204 if algorithm = MaSh_NB_Ext orelse algorithm = MaSh_kNN_Ext then
1207 Graph.schedule (fn _ => fn (fact, (_, feats, deps)) => (fact, feats, deps))
1210 MaSh.query_external ctxt algorithm max_suggs learns goal_feats
1214 val int_goal_feats =
1215 map_filter (fn (s, w) => Option.map (rpair w) (Symtab.lookup feat_tab s)) goal_feats
1217 MaSh.query_internal ctxt algorithm num_facts num_feats ffds freqs fact_idxs max_suggs
1218 goal_feats int_goal_feats
1221 val unknown = filter_out (is_fact_in_graph access_G o snd) facts
1223 find_mash_suggestions ctxt max_suggs suggs facts chained unknown
1224 |> apply2 (map fact_of_raw_fact)
1228 fun mash_unlearn ctxt = (clear_state ctxt; writeln "Reset MaSh")
1230 fun learn_wrt_access_graph ctxt (name, parents, feats, deps)
1231 (accum as (access_G, (fact_xtab, feat_xtab))) =
1233 fun maybe_learn_from from (accum as (parents, access_G)) =
1234 try_graph ctxt "updating graph" accum (fn () =>
1235 (from :: parents, Graph.add_edge_acyclic (from, name) access_G))
1237 val access_G = access_G |> Graph.default_node (name, (Isar_Proof, feats, deps))
1238 val (parents, access_G) = ([], access_G) |> fold maybe_learn_from parents
1239 val (deps, _) = ([], access_G) |> fold maybe_learn_from deps
1241 val fact_xtab = add_to_xtab name fact_xtab
1242 val feat_xtab = fold maybe_add_to_xtab feats feat_xtab
1244 (SOME (name, parents, feats, deps), (access_G, (fact_xtab, feat_xtab)))
1246 handle Symtab.DUP _ => (NONE, accum) (* facts sometimes have the same name, confusingly *)
1248 fun relearn_wrt_access_graph ctxt (name, deps) access_G =
1250 fun maybe_relearn_from from (accum as (parents, access_G)) =
1251 try_graph ctxt "updating graph" accum (fn () =>
1252 (from :: parents, Graph.add_edge_acyclic (from, name) access_G))
1254 access_G |> Graph.map_node name (fn (_, feats, _) => (Automatic_Proof, feats, deps))
1255 val (deps, _) = ([], access_G) |> fold maybe_relearn_from deps
1257 ((name, deps), access_G)
1260 fun flop_wrt_access_graph name =
1261 Graph.map_node name (fn (_, feats, deps) => (Isar_Proof_wegen_Prover_Flop, feats, deps))
1263 val learn_timeout_slack = 20.0
1265 fun launch_thread timeout task =
1267 val hard_timeout = time_mult learn_timeout_slack timeout
1268 val birth_time = Time.now ()
1269 val death_time = birth_time + hard_timeout
1270 val desc = ("Machine learner for Sledgehammer", "")
1272 Async_Manager_Legacy.thread MaShN birth_time death_time desc task
1275 fun anonymous_proof_name () =
1276 Date.fmt (anonymous_proof_prefix ^ "%Y%m%d.%H%M%S.") (Date.fromTimeLocal (Time.now ())) ^
1279 fun mash_learn_proof ctxt ({timeout, ...} : params) t used_ths =
1280 if not (null used_ths) andalso is_mash_enabled () then
1281 launch_thread timeout (fn () =>
1283 val thy = Proof_Context.theory_of ctxt
1284 val feats = features_of ctxt (Context.theory_name thy) (Local, General) [t]
1287 (fn {access_G, xtabs as ((num_facts0, _), _), ffds, freqs, dirty_facts} =>
1290 |> filter (is_fact_in_graph access_G)
1291 |> map nickname_of_thm
1293 val name = anonymous_proof_name ()
1294 val (access_G', xtabs', rev_learns) =
1295 add_node Automatic_Proof name [] (* ignore parents *) feats deps
1296 (access_G, xtabs, [])
1298 val (ffds', freqs') =
1299 recompute_ffds_freqs_from_learns (rev rev_learns) xtabs' num_facts0 ffds freqs
1301 {access_G = access_G', xtabs = xtabs', ffds = ffds', freqs = freqs',
1302 dirty_facts = Option.map (cons name) dirty_facts}
1309 fun sendback sub = Active.sendback_markup_command (sledgehammerN ^ " " ^ sub)
1311 val commit_timeout = seconds 30.0
1313 (* The timeout is understood in a very relaxed fashion. *)
1314 fun mash_learn_facts ctxt (params as {debug, verbose, ...}) prover auto_level run_prover
1315 learn_timeout facts =
1317 val timer = Timer.startRealTimer ()
1318 fun next_commit_time () = Timer.checkRealTimer timer + commit_timeout
1320 (case get_state ctxt of
1321 NONE => "MaSh is busy\nPlease try again later"
1322 | SOME {access_G, ...} =>
1324 val is_in_access_G = is_fact_in_graph access_G o snd
1325 val no_new_facts = forall is_in_access_G facts
1327 if no_new_facts andalso not run_prover then
1328 if auto_level < 2 then
1329 "No new " ^ (if run_prover then "automatic" else "Isar") ^ " proofs to learn" ^
1330 (if auto_level = 0 andalso not run_prover then
1331 "\n\nHint: Try " ^ sendback learn_proverN ^ " to learn from an automatic prover"
1338 val name_tabs = build_name_tables nickname_of_thm facts
1340 fun deps_of status th =
1341 if status = Non_Rec_Def orelse status = Rec_Def then
1343 else if run_prover then
1344 prover_dependencies_of ctxt params prover auto_level facts name_tabs th
1345 |> (fn (false, _) => NONE | (true, deps) => trim_dependencies deps)
1347 isar_dependencies_of name_tabs th
1349 fun do_commit [] [] [] state = state
1350 | do_commit learns relearns flops
1351 {access_G, xtabs as ((num_facts0, _), _), ffds, freqs, dirty_facts} =
1353 val was_empty = Graph.is_empty access_G
1355 val (learns, (access_G', xtabs')) =
1356 fold_map (learn_wrt_access_graph ctxt) learns (access_G, xtabs)
1358 val (relearns, access_G'') =
1359 fold_map (relearn_wrt_access_graph ctxt) relearns access_G'
1361 val access_G''' = access_G'' |> fold flop_wrt_access_graph flops
1363 (case (was_empty, dirty_facts) of
1364 (false, SOME names) => SOME (map #1 learns @ map #1 relearns @ names)
1367 val (ffds', freqs') =
1368 if null relearns then
1369 recompute_ffds_freqs_from_learns
1370 (map (fn (name, _, feats, deps) => (name, feats, deps)) learns) xtabs'
1371 num_facts0 ffds freqs
1373 recompute_ffds_freqs_from_access_G access_G''' xtabs'
1375 {access_G = access_G''', xtabs = xtabs', ffds = ffds', freqs = freqs',
1376 dirty_facts = dirty_facts'}
1379 fun commit last learns relearns flops =
1380 (if debug andalso auto_level = 0 then writeln "Committing..." else ();
1381 map_state ctxt (do_commit (rev learns) relearns flops);
1382 if not last andalso auto_level = 0 then
1383 let val num_proofs = length learns + length relearns in
1384 writeln ("Learned " ^ string_of_int num_proofs ^ " " ^
1385 (if run_prover then "automatic" else "Isar") ^ " proof" ^
1386 plural_s num_proofs ^ " in the last " ^ string_of_time commit_timeout)
1391 fun learn_new_fact _ (accum as (_, (_, _, true))) = accum
1392 | learn_new_fact (parents, ((_, stature as (_, status)), th))
1393 (learns, (num_nontrivial, next_commit, _)) =
1395 val name = nickname_of_thm th
1396 val feats = features_of ctxt (Thm.theory_name th) stature [Thm.prop_of th]
1397 val deps = these (deps_of status th)
1398 val num_nontrivial = num_nontrivial |> not (null deps) ? Integer.add 1
1399 val learns = (name, parents, feats, deps) :: learns
1400 val (learns, next_commit) =
1401 if Timer.checkRealTimer timer > next_commit then
1402 (commit false learns [] []; ([], next_commit_time ()))
1404 (learns, next_commit)
1405 val timed_out = Timer.checkRealTimer timer > learn_timeout
1407 (learns, (num_nontrivial, next_commit, timed_out))
1410 val (num_new_facts, num_nontrivial) =
1411 if no_new_facts then
1415 val new_facts = facts
1416 |> sort (crude_thm_ord ctxt o apply2 snd)
1417 |> map (pair []) (* ignore parents *)
1418 |> filter_out (is_in_access_G o snd)
1419 val (learns, (num_nontrivial, _, _)) =
1420 ([], (0, next_commit_time (), false))
1421 |> fold learn_new_fact new_facts
1423 commit true learns [] []; (length new_facts, num_nontrivial)
1426 fun relearn_old_fact _ (accum as (_, (_, _, true))) = accum
1427 | relearn_old_fact ((_, (_, status)), th)
1428 ((relearns, flops), (num_nontrivial, next_commit, _)) =
1430 val name = nickname_of_thm th
1431 val (num_nontrivial, relearns, flops) =
1432 (case deps_of status th of
1433 SOME deps => (num_nontrivial + 1, (name, deps) :: relearns, flops)
1434 | NONE => (num_nontrivial, relearns, name :: flops))
1435 val (relearns, flops, next_commit) =
1436 if Timer.checkRealTimer timer > next_commit then
1437 (commit false [] relearns flops; ([], [], next_commit_time ()))
1439 (relearns, flops, next_commit)
1440 val timed_out = Timer.checkRealTimer timer > learn_timeout
1442 ((relearns, flops), (num_nontrivial, next_commit, timed_out))
1445 val num_nontrivial =
1446 if not run_prover then
1450 val max_isar = 1000 * max_dependencies
1452 fun priority_of th =
1453 Random.random_range 0 max_isar +
1454 (case try (Graph.get_node access_G) (nickname_of_thm th) of
1455 SOME (Isar_Proof, _, deps) => ~100 * length deps
1456 | SOME (Automatic_Proof, _, _) => 2 * max_isar
1457 | SOME (Isar_Proof_wegen_Prover_Flop, _, _) => max_isar
1460 val old_facts = facts
1461 |> filter is_in_access_G
1462 |> map (`(priority_of o snd))
1463 |> sort (int_ord o apply2 fst)
1465 val ((relearns, flops), (num_nontrivial, _, _)) =
1466 (([], []), (num_nontrivial, next_commit_time (), false))
1467 |> fold relearn_old_fact old_facts
1469 commit true [] relearns flops; num_nontrivial
1472 if verbose orelse auto_level < 2 then
1473 "Learned " ^ string_of_int num_new_facts ^ " fact" ^ plural_s num_new_facts ^
1474 " and " ^ string_of_int num_nontrivial ^ " nontrivial " ^
1475 (if run_prover then "automatic and " else "") ^ "Isar proof" ^
1476 plural_s num_nontrivial ^
1477 (if verbose then " in " ^ string_of_time (Timer.checkRealTimer timer) else "")
1484 fun mash_learn ctxt (params as {provers, timeout, ...}) fact_override chained run_prover =
1486 val css = Sledgehammer_Fact.clasimpset_rule_table_of ctxt
1487 val ctxt = ctxt |> Config.put instantiate_inducts false
1489 nearly_all_facts ctxt false fact_override Keyword.empty_keywords css chained [] \<^prop>\<open>True\<close>
1490 |> sort (crude_thm_ord ctxt o apply2 snd o swap)
1491 val num_facts = length facts
1492 val prover = hd provers
1494 fun learn auto_level run_prover =
1495 mash_learn_facts ctxt params prover auto_level run_prover one_year facts
1499 (writeln ("MaShing through " ^ string_of_int num_facts ^ " fact" ^
1500 plural_s num_facts ^ " for automatic proofs (" ^ quote prover ^ " timeout: " ^
1501 string_of_time timeout ^ ").\n\nCollecting Isar proofs first...");
1503 writeln "Now collecting automatic proofs\n\
1504 \This may take several hours; you can safely stop the learning process at any point";
1507 (writeln ("MaShing through " ^ string_of_int num_facts ^ " fact" ^
1508 plural_s num_facts ^ " for Isar proofs...");
1512 fun mash_can_suggest_facts ctxt =
1513 (case get_state ctxt of
1515 | SOME {access_G, ...} => not (Graph.is_empty access_G))
1517 fun mash_can_suggest_facts_fast ctxt =
1518 (case peek_state ctxt of
1520 | SOME (_, {access_G, ...}) => not (Graph.is_empty access_G))
1522 (* Generate more suggestions than requested, because some might be thrown out later for various
1523 reasons (e.g., duplicates). *)
1524 fun generous_max_suggestions max_facts = 2 * max_facts + 25 (* FUDGE *)
1526 val mepo_weight = 0.5 (* FUDGE *)
1527 val mash_weight = 0.5 (* FUDGE *)
1529 val max_facts_to_learn_before_query = 100 (* FUDGE *)
1531 (* The threshold should be large enough so that MaSh does not get activated for Auto Sledgehammer. *)
1532 val min_secs_for_learning = 10
1534 fun relevant_facts ctxt (params as {verbose, learn, fact_filter, timeout, ...}) prover
1535 max_facts ({add, only, ...} : fact_override) hyp_ts concl_t facts =
1536 if not (subset (op =) (the_list fact_filter, fact_filters)) then
1537 error ("Unknown fact filter: " ^ quote (the fact_filter))
1539 [("", map fact_of_raw_fact facts)]
1540 else if max_facts <= 0 orelse null facts then
1544 val thy_name = Context.theory_name (Proof_Context.theory_of ctxt)
1546 fun maybe_launch_thread exact min_num_facts_to_learn =
1547 if not (Async_Manager_Legacy.has_running_threads MaShN) andalso
1548 Time.toSeconds timeout >= min_secs_for_learning then
1549 let val timeout = time_mult learn_timeout_slack timeout in
1551 writeln ("Started MaShing through " ^
1552 (if exact then "" else "up to ") ^ string_of_int min_num_facts_to_learn ^
1553 " fact" ^ plural_s min_num_facts_to_learn ^ " in the background")
1556 launch_thread timeout
1557 (fn () => (true, mash_learn_facts ctxt params prover 2 false timeout facts))
1562 val mash_enabled = is_mash_enabled ()
1563 val mash_fast = mash_can_suggest_facts_fast ctxt
1565 fun please_learn () =
1567 (case get_state ctxt of
1568 NONE => maybe_launch_thread false (length facts)
1569 | SOME {access_G, xtabs = ((num_facts0, _), _), ...} =>
1571 val is_in_access_G = is_fact_in_graph access_G o snd
1572 val min_num_facts_to_learn = length facts - num_facts0
1574 if min_num_facts_to_learn <= max_facts_to_learn_before_query then
1575 (case length (filter_out is_in_access_G facts) of
1577 | num_facts_to_learn =>
1578 if num_facts_to_learn <= max_facts_to_learn_before_query then
1579 mash_learn_facts ctxt params prover 2 false timeout facts
1580 |> (fn "" => () | s => writeln (MaShN ^ ": " ^ s))
1582 maybe_launch_thread true num_facts_to_learn)
1584 maybe_launch_thread false min_num_facts_to_learn
1587 maybe_launch_thread false (length facts)
1590 if learn andalso mash_enabled andalso fact_filter <> SOME mepoN then please_learn () else ()
1592 val effective_fact_filter =
1593 (case fact_filter of
1595 | NONE => if mash_enabled andalso mash_fast then meshN else mepoN)
1597 val unique_facts = drop_duplicate_facts facts
1598 val add_ths = Attrib.eval_thms ctxt add
1600 fun in_add (_, th) = member Thm.eq_thm_prop add_ths th
1602 fun add_and_take accepts =
1606 (unique_facts |> filter in_add |> map fact_of_raw_fact) @ (accepts |> filter_out in_add))
1610 (mepo_suggested_facts ctxt params max_facts NONE hyp_ts concl_t unique_facts
1611 |> weight_facts_steeply, [])
1614 mash_suggested_facts ctxt thy_name params (generous_max_suggestions max_facts) hyp_ts
1616 |>> weight_facts_steeply
1619 (* the order is important for the "case" expression below *)
1620 [] |> effective_fact_filter <> mepoN ? cons (mash_weight, mash)
1621 |> effective_fact_filter <> mashN ? cons (mepo_weight, mepo)
1622 |> Par_List.map (apsnd (fn f => f ()))
1624 mesh_facts (fact_distinct (op aconv)) (eq_snd (gen_eq_thm ctxt)) max_facts mess
1627 (case (fact_filter, mess) of
1628 (NONE, [(_, (mepo, _)), (_, (mash, _))]) =>
1630 (mepoN, mepo |> map fst |> add_and_take),
1631 (mashN, mash |> map fst |> add_and_take)]
1632 | _ => [(effective_fact_filter, mesh)])