src/HOL/Tools/Sledgehammer/sledgehammer_mash.ML
changeset 48384 83dc102041e6
parent 48383 df75b2d7e26a
child 48385 2779dea0b1e0
equal deleted inserted replaced
48383:df75b2d7e26a 48384:83dc102041e6
    52   val mash_can_suggest_facts : Proof.context -> bool
    52   val mash_can_suggest_facts : Proof.context -> bool
    53   val mash_suggest_facts :
    53   val mash_suggest_facts :
    54     Proof.context -> params -> string -> int -> term list -> term
    54     Proof.context -> params -> string -> int -> term list -> term
    55     -> ('a * thm) list -> ('a * thm) list * ('a * thm) list
    55     -> ('a * thm) list -> ('a * thm) list * ('a * thm) list
    56   val mash_learn_proof :
    56   val mash_learn_proof :
    57     Proof.context -> params -> term -> ('a * thm) list -> thm list -> unit
    57     Proof.context -> params -> string -> term -> ('a * thm) list -> thm list
       
    58     -> unit
    58   val mash_learn_thy :
    59   val mash_learn_thy :
    59     Proof.context -> params -> theory -> Time.time -> fact list -> string
    60     Proof.context -> params -> theory -> Time.time -> fact list -> string
    60   val mash_learn : Proof.context -> params -> unit
    61   val mash_learn : Proof.context -> params -> unit
    61   val relevant_facts :
    62   val relevant_facts :
    62     Proof.context -> params -> string -> int -> fact_override -> term list
    63     Proof.context -> params -> string -> int -> fact_override -> term list
   545     val graph = graph |> Graph.default_node (name, ())
   546     val graph = graph |> Graph.default_node (name, ())
   546     val (parents, graph) = ([], graph) |> fold maybe_add_from parents
   547     val (parents, graph) = ([], graph) |> fold maybe_add_from parents
   547     val (deps, graph) = ([], graph) |> fold maybe_add_from deps
   548     val (deps, graph) = ([], graph) |> fold maybe_add_from deps
   548   in ((name, parents, feats, deps) :: upds, graph) end
   549   in ((name, parents, feats, deps) :: upds, graph) end
   549 
   550 
   550 val pass1_learn_timeout_factor = 0.5
   551 val learn_timeout_slack = 2.0
   551 
   552 
   552 fun mash_learn_proof ctxt ({provers, overlord, ...} : params) t facts used_ths =
   553 fun launch_thread timeout task =
   553   let
   554   let
   554     val thy = Proof_Context.theory_of ctxt
   555     val hard_timeout = time_mult learn_timeout_slack timeout
   555     val prover = hd provers
   556     val birth_time = Time.now ()
   556     val name = ATP_Util.timestamp () ^ " " ^ serial_string () (* fresh enough *)
   557     val death_time = Time.+ (birth_time, hard_timeout)
   557     val feats = features_of ctxt prover thy General [t]
   558     val desc = ("machine learner for Sledgehammer", "")
   558     val deps = used_ths |> map nickname_of
   559   in Async_Manager.launch MaShN birth_time death_time desc task end
   559   in
   560 
   560     mash_map ctxt (fn {thys, fact_graph} =>
   561 fun mash_learn_proof ctxt ({overlord, timeout, ...} : params) prover t facts
   561         let
   562                      used_ths =
   562           val parents = parents_wrt_facts facts fact_graph
   563   if is_smt_prover ctxt prover then
   563           val upds = [(name, parents, feats, deps)]
   564     ()
   564           val (upds, fact_graph) =
   565   else
   565             ([], fact_graph) |> fold (update_fact_graph ctxt) upds
   566     launch_thread timeout
   566         in
   567         (fn () =>
   567           mash_ADD ctxt overlord upds;
   568             let
   568           {thys = thys, fact_graph = fact_graph}
   569               val thy = Proof_Context.theory_of ctxt
   569         end)
   570               val name = timestamp () ^ " " ^ serial_string () (* freshish *)
   570   end
   571               val feats = features_of ctxt prover thy General [t]
       
   572               val deps = used_ths |> map nickname_of
       
   573             in
       
   574               mash_map ctxt (fn {thys, fact_graph} =>
       
   575                   let
       
   576                     val parents = parents_wrt_facts facts fact_graph
       
   577                     val upds = [(name, parents, feats, deps)]
       
   578                     val (upds, fact_graph) =
       
   579                       ([], fact_graph) |> fold (update_fact_graph ctxt) upds
       
   580                   in
       
   581                     mash_ADD ctxt overlord upds;
       
   582                     {thys = thys, fact_graph = fact_graph}
       
   583                   end);
       
   584               (true, "")
       
   585             end)
   571 
   586 
   572 (* Too many dependencies is a sign that a decision procedure is at work. There
   587 (* Too many dependencies is a sign that a decision procedure is at work. There
   573    isn't much too learn from such proofs. *)
   588    isn't much too learn from such proofs. *)
   574 val max_dependencies = 10
   589 val max_dependencies = 10
       
   590 val pass1_learn_timeout_factor = 0.75
   575 
   591 
   576 (* The timeout is understood in a very slack fashion. *)
   592 (* The timeout is understood in a very slack fashion. *)
   577 fun mash_learn_thy ctxt ({provers, verbose, overlord, ...} : params) thy timeout
   593 fun mash_learn_thy ctxt ({provers, verbose, overlord, ...} : params) thy timeout
   578                    facts =
   594                    facts =
   579   let
   595   let
   645   end
   661   end
   646 
   662 
   647 (* The threshold should be large enough so that MaSh doesn't kick in for Auto
   663 (* The threshold should be large enough so that MaSh doesn't kick in for Auto
   648    Sledgehammer and Try. *)
   664    Sledgehammer and Try. *)
   649 val min_secs_for_learning = 15
   665 val min_secs_for_learning = 15
   650 val learn_timeout_factor = 2.0
       
   651 
   666 
   652 fun relevant_facts ctxt (params as {learn, fact_filter, timeout, ...}) prover
   667 fun relevant_facts ctxt (params as {learn, fact_filter, timeout, ...}) prover
   653         max_facts ({add, only, ...} : fact_override) hyp_ts concl_t facts =
   668         max_facts ({add, only, ...} : fact_override) hyp_ts concl_t facts =
   654   if not (subset (op =) (the_list fact_filter, fact_filters)) then
   669   if not (subset (op =) (the_list fact_filter, fact_filters)) then
   655     error ("Unknown fact filter: " ^ quote (the fact_filter) ^ ".")
   670     error ("Unknown fact filter: " ^ quote (the fact_filter) ^ ".")
   659     []
   674     []
   660   else
   675   else
   661     let
   676     let
   662       val thy = Proof_Context.theory_of ctxt
   677       val thy = Proof_Context.theory_of ctxt
   663       fun maybe_learn () =
   678       fun maybe_learn () =
   664         if not learn orelse Async_Manager.has_running_threads MaShN then
   679         if learn andalso not (Async_Manager.has_running_threads MaShN) andalso
   665           ()
   680            Time.toSeconds timeout >= min_secs_for_learning then
   666         else if Time.toSeconds timeout >= min_secs_for_learning then
   681           let val timeout = time_mult learn_timeout_slack timeout in
   667           let
   682             launch_thread timeout
   668             val soft_timeout = time_mult learn_timeout_factor timeout
   683                 (fn () => (true, mash_learn_thy ctxt params thy timeout facts))
   669             val hard_timeout = time_mult 4.0 soft_timeout
       
   670             val birth_time = Time.now ()
       
   671             val death_time = Time.+ (birth_time, hard_timeout)
       
   672             val desc = ("machine learner for Sledgehammer", "")
       
   673           in
       
   674             Async_Manager.launch MaShN birth_time death_time desc
       
   675                 (fn () =>
       
   676                     (true, mash_learn_thy ctxt params thy soft_timeout facts))
       
   677           end
   684           end
   678         else
   685         else
   679           ()
   686           ()
   680       val fact_filter =
   687       val fact_filter =
   681         case fact_filter of
   688         case fact_filter of