src/HOL/Tools/Sledgehammer/sledgehammer_isar_preplay.ML
changeset 55256 6c317e374614
parent 55255 eceebcea3e00
child 55257 abfd7b90bba2
equal deleted inserted replaced
55255:eceebcea3e00 55256:6c317e374614
    19     {preplay_step: Time.time -> proof_method -> isar_step -> play_outcome,
    19     {preplay_step: Time.time -> proof_method -> isar_step -> play_outcome,
    20      set_preplay_outcomes: label -> (proof_method * play_outcome Lazy.lazy) list -> unit,
    20      set_preplay_outcomes: label -> (proof_method * play_outcome Lazy.lazy) list -> unit,
    21      preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
    21      preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
    22      overall_preplay_outcome: isar_proof -> play_outcome}
    22      overall_preplay_outcome: isar_proof -> play_outcome}
    23 
    23 
    24   val preplay_data_of_isar_proof : bool -> Proof.context -> string -> string -> Time.time ->
    24   val enrich_context_with_local_facts : isar_proof -> Proof.context -> Proof.context
    25     isar_proof -> isar_preplay_data
    25   val preplay_data_of_isar_proof : Proof.context -> string -> string -> Time.time -> isar_proof ->
       
    26     isar_preplay_data
    26 end;
    27 end;
    27 
    28 
    28 structure Sledgehammer_Isar_Preplay : SLEDGEHAMMER_ISAR_PREPLAY =
    29 structure Sledgehammer_Isar_Preplay : SLEDGEHAMMER_ISAR_PREPLAY =
    29 struct
    30 struct
    30 
    31 
    31 open Sledgehammer_Util
    32 open Sledgehammer_Util
    32 open Sledgehammer_Reconstructor
    33 open Sledgehammer_Reconstructor
    33 open Sledgehammer_Isar_Proof
    34 open Sledgehammer_Isar_Proof
    34 
    35 
    35 val trace = Attrib.setup_config_bool @{binding sledgehammer_preplay_trace} (K false)
    36 val trace = Attrib.setup_config_bool @{binding sledgehammer_preplay_trace} (K false)
       
    37 
       
    38 fun enrich_context_with_local_facts proof ctxt =
       
    39   let
       
    40     val thy = Proof_Context.theory_of ctxt
       
    41 
       
    42     fun enrich_with_fact l t =
       
    43       Proof_Context.put_thms false (string_of_label l, SOME [Skip_Proof.make_thm thy t])
       
    44 
       
    45     val enrich_with_assms = fold (uncurry enrich_with_fact)
       
    46 
       
    47     fun enrich_with_proof (Proof (_, assms, isar_steps)) =
       
    48       enrich_with_assms assms #> fold enrich_with_step isar_steps
       
    49     and enrich_with_step (Let _) = I
       
    50       | enrich_with_step (Prove (_, _, l, t, subproofs, _)) =
       
    51         enrich_with_fact l t #> fold enrich_with_proof subproofs
       
    52   in
       
    53     enrich_with_proof proof ctxt
       
    54   end
    36 
    55 
    37 fun preplay_trace ctxt assmsp concl result =
    56 fun preplay_trace ctxt assmsp concl result =
    38   let
    57   let
    39     val ctxt = ctxt |> Config.put show_markup true
    58     val ctxt = ctxt |> Config.put show_markup true
    40     val assms = op @ assmsp
    59     val assms = op @ assmsp
    93     | Blast_Method => blast_tac ctxt
   112     | Blast_Method => blast_tac ctxt
    94     | Algebra_Method => Groebner.algebra_tac [] [] ctxt
   113     | Algebra_Method => Groebner.algebra_tac [] [] ctxt
    95     | _ => raise Fail "Sledgehammer_Isar_Preplay: tac_of_method"))
   114     | _ => raise Fail "Sledgehammer_Isar_Preplay: tac_of_method"))
    96 
   115 
    97 (* main function for preplaying Isar steps; may throw exceptions *)
   116 (* main function for preplaying Isar steps; may throw exceptions *)
    98 fun raw_preplay_step debug type_enc lam_trans ctxt timeout meth
   117 fun raw_preplay_step type_enc lam_trans ctxt timeout meth
    99       (Prove (_, xs, _, t, subproofs, (fact_names, _))) =
   118       (Prove (_, xs, _, t, subproofs, (fact_names, _))) =
   100     let
   119     let
   101       val goal =
   120       val goal =
   102         (case xs of
   121         (case xs of
   103           [] => t
   122           [] => t
   119 
   138 
   120       val facts =
   139       val facts =
   121         resolve_fact_names ctxt fact_names
   140         resolve_fact_names ctxt fact_names
   122         |>> append (map (thm_of_proof ctxt) subproofs)
   141         |>> append (map (thm_of_proof ctxt) subproofs)
   123 
   142 
   124       val ctxt' = ctxt |> silence_reconstructors debug
       
   125 
       
   126       fun prove () =
   143       fun prove () =
   127         Goal.prove ctxt' [] [] goal (fn {context = ctxt, ...} =>
   144         Goal.prove ctxt [] [] goal (fn {context = ctxt, ...} =>
   128           HEADGOAL (tac_of_method meth type_enc lam_trans ctxt facts))
   145           HEADGOAL (tac_of_method meth type_enc lam_trans ctxt facts))
   129         handle ERROR msg => error ("Preplay error: " ^ msg)
   146         handle ERROR msg => error ("Preplay error: " ^ msg)
   130 
   147 
   131       val play_outcome = take_time timeout prove ()
   148       val play_outcome = take_time timeout prove ()
   132     in
   149     in
   138   {set_preplay_outcomes: label -> (proof_method * play_outcome Lazy.lazy) list -> unit,
   155   {set_preplay_outcomes: label -> (proof_method * play_outcome Lazy.lazy) list -> unit,
   139    preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
   156    preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
   140    preplay_step: Time.time -> proof_method -> isar_step -> play_outcome,
   157    preplay_step: Time.time -> proof_method -> isar_step -> play_outcome,
   141    overall_preplay_outcome: isar_proof -> play_outcome}
   158    overall_preplay_outcome: isar_proof -> play_outcome}
   142 
   159 
   143 fun enrich_context_with_local_facts proof ctxt =
   160 fun time_of_play (Played time) = time
   144   let
   161   | time_of_play (Play_Timed_Out time) = time
   145     val thy = Proof_Context.theory_of ctxt
   162 
   146 
   163 fun merge_preplay_outcomes Play_Failed _ = Play_Failed
   147     fun enrich_with_fact l t =
   164   | merge_preplay_outcomes _ Play_Failed = Play_Failed
   148       Proof_Context.put_thms false (string_of_label l, SOME [Skip_Proof.make_thm thy t])
   165   | merge_preplay_outcomes (Played time1) (Played time2) = Played (Time.+ (time1, time2))
   149 
   166   | merge_preplay_outcomes play1 play2 =
   150     val enrich_with_assms = fold (uncurry enrich_with_fact)
   167     Play_Timed_Out (Time.+ (pairself time_of_play (play1, play2)))
   151 
   168 
   152     fun enrich_with_proof (Proof (_, assms, isar_steps)) =
   169 (* Given a (canonically labeled) proof, produces an imperative preplay interface with a shared table
   153       enrich_with_assms assms #> fold enrich_with_step isar_steps
   170    mapping from labels to preplay results. The preplay results are caluclated lazily and cached to
   154     and enrich_with_step (Let _) = I
   171    avoid repeated calculation. *)
   155       | enrich_with_step (Prove (_, _, l, t, subproofs, _)) =
   172 fun preplay_data_of_isar_proof ctxt type_enc lam_trans preplay_timeout proof =
   156         enrich_with_fact l t #> fold enrich_with_proof subproofs
   173   let
   157   in
       
   158     enrich_with_proof proof ctxt
       
   159   end
       
   160 
       
   161 fun merge_preplay_outcomes _ Play_Failed = Play_Failed
       
   162   | merge_preplay_outcomes Play_Failed _ = Play_Failed
       
   163   | merge_preplay_outcomes (Play_Timed_Out t1) (Play_Timed_Out t2) =
       
   164     Play_Timed_Out (Time.+ (t1, t2))
       
   165   | merge_preplay_outcomes (Played t1) (Play_Timed_Out t2) = Play_Timed_Out (Time.+ (t1, t2))
       
   166   | merge_preplay_outcomes (Play_Timed_Out t1) (Played t2) = Play_Timed_Out (Time.+ (t1, t2))
       
   167   | merge_preplay_outcomes (Played t1) (Played t2) = Played (Time.+ (t1, t2))
       
   168 
       
   169 (* Given a proof, produces an imperative preplay interface with a shared table mapping from labels
       
   170    to preplay results. The preplay results are caluclated lazyly and cached to avoid repeated
       
   171    calculation.
       
   172 
       
   173    Precondition: The proof must be labeled canonically; cf.
       
   174    "Slegehammer_Isar_Proof.relabel_isar_proof_canonically". *)
       
   175 fun preplay_data_of_isar_proof debug ctxt type_enc lam_trans preplay_timeout proof =
       
   176   let
       
   177     val ctxt = enrich_context_with_local_facts proof ctxt
       
   178 
       
   179     fun preplay_step timeout meth =
   174     fun preplay_step timeout meth =
   180       try (raw_preplay_step debug type_enc lam_trans ctxt timeout meth)
   175       try (raw_preplay_step type_enc lam_trans ctxt timeout meth)
   181       #> the_default Play_Failed
   176       #> the_default Play_Failed
   182 
   177 
   183     val preplay_tab = Unsynchronized.ref Canonical_Label_Tab.empty
   178     val preplay_tab = Unsynchronized.ref Canonical_Label_Tab.empty
   184 
   179 
   185     fun set_preplay_outcomes l meths_outcomes =
   180     fun set_preplay_outcomes l meths_outcomes =