src/HOL/Tools/Sledgehammer/sledgehammer_isar_preplay.ML
changeset 55243 66709d41601e
parent 55223 3c593bad6b31
child 55244 12e1a5d8ee48
equal deleted inserted replaced
55242:413ec965f95d 55243:66709d41601e
    14   type label = Sledgehammer_Isar_Proof.label
    14   type label = Sledgehammer_Isar_Proof.label
    15 
    15 
    16   val trace : bool Config.T
    16   val trace : bool Config.T
    17 
    17 
    18   type isar_preplay_data =
    18   type isar_preplay_data =
    19     {preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
    19     {reset_preplay_outcomes: isar_step -> unit,
    20      set_preplay_outcome: label -> proof_method -> play_outcome -> unit,
    20      set_preplay_outcome: label -> proof_method -> play_outcome -> unit,
       
    21      preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
    21      preplay_quietly: Time.time -> isar_step -> play_outcome,
    22      preplay_quietly: Time.time -> isar_step -> play_outcome,
    22      overall_preplay_outcome: isar_proof -> play_outcome}
    23      overall_preplay_outcome: isar_proof -> play_outcome}
    23 
    24 
    24   val preplay_data_of_isar_proof : bool -> Proof.context -> string -> string -> bool -> Time.time ->
    25   val preplay_data_of_isar_proof : bool -> Proof.context -> string -> string -> bool -> Time.time ->
    25     isar_proof -> isar_preplay_data
    26     isar_proof -> isar_preplay_data
   144       (if Config.get ctxt trace then preplay_trace ctxt facts goal play_outcome else ();
   145       (if Config.get ctxt trace then preplay_trace ctxt facts goal play_outcome else ();
   145        play_outcome)
   146        play_outcome)
   146     end
   147     end
   147 
   148 
   148 type isar_preplay_data =
   149 type isar_preplay_data =
   149   {preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
   150   {reset_preplay_outcomes: isar_step -> unit,
   150    set_preplay_outcome: label -> proof_method -> play_outcome -> unit,
   151    set_preplay_outcome: label -> proof_method -> play_outcome -> unit,
       
   152    preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
   151    preplay_quietly: Time.time -> isar_step -> play_outcome,
   153    preplay_quietly: Time.time -> isar_step -> play_outcome,
   152    overall_preplay_outcome: isar_proof -> play_outcome}
   154    overall_preplay_outcome: isar_proof -> play_outcome}
   153 
   155 
   154 fun enrich_context_with_local_facts proof ctxt =
   156 fun enrich_context_with_local_facts proof ctxt =
   155   let
   157   let
   184    Precondition: The proof must be labeled canonically; cf.
   186    Precondition: The proof must be labeled canonically; cf.
   185    "Slegehammer_Isar_Proof.relabel_isar_proof_canonically". *)
   187    "Slegehammer_Isar_Proof.relabel_isar_proof_canonically". *)
   186 fun preplay_data_of_isar_proof debug ctxt type_enc lam_trans do_preplay preplay_timeout proof =
   188 fun preplay_data_of_isar_proof debug ctxt type_enc lam_trans do_preplay preplay_timeout proof =
   187   if not do_preplay then
   189   if not do_preplay then
   188     (* the "dont_preplay" option pretends that everything works just fine *)
   190     (* the "dont_preplay" option pretends that everything works just fine *)
   189     {preplay_outcome = K (K (Lazy.value (Played Time.zeroTime))),
   191     {reset_preplay_outcomes = K (),
   190      set_preplay_outcome = K (K (K ())),
   192      set_preplay_outcome = K (K (K ())),
       
   193      preplay_outcome = K (K (Lazy.value (Played Time.zeroTime))),
   191      preplay_quietly = K (K (Played Time.zeroTime)),
   194      preplay_quietly = K (K (Played Time.zeroTime)),
   192      overall_preplay_outcome = K (Played Time.zeroTime)} : isar_preplay_data
   195      overall_preplay_outcome = K (Played Time.zeroTime)} : isar_preplay_data
   193   else
   196   else
   194     let
   197     let
   195       val ctxt = enrich_context_with_local_facts proof ctxt
   198       val ctxt = enrich_context_with_local_facts proof ctxt
   209       (* preplay steps treating exceptions like timeouts *)
   212       (* preplay steps treating exceptions like timeouts *)
   210       fun preplay_quietly timeout (step as Prove (_, _, _, _, _, (_, (meth :: _) :: _))) =
   213       fun preplay_quietly timeout (step as Prove (_, _, _, _, _, (_, (meth :: _) :: _))) =
   211           preplay true timeout meth step
   214           preplay true timeout meth step
   212         | preplay_quietly _ _ = Played Time.zeroTime
   215         | preplay_quietly _ _ = Played Time.zeroTime
   213 
   216 
   214       fun add_step_to_tab (step as Prove (_, _, l, _, _, (_, methss))) =
   217       val preplay_tab = Unsynchronized.ref Canonical_Label_Tab.empty
   215           Canonical_Label_Tab.update_new
   218 
   216             (l, maps (map (fn meth =>
   219       fun reset_preplay_outcomes (step as Prove (_, _, l, _, _, (_, methss))) =
   217                (meth, Lazy.lazy (fn () => preplay false preplay_timeout meth step)))) methss)
   220           preplay_tab := Canonical_Label_Tab.update (l, maps (map (fn meth =>
   218         | add_step_to_tab _ = I
   221               (meth, Lazy.lazy (fn () => preplay false preplay_timeout meth step)))) methss)
   219 
   222             (!preplay_tab)
   220       val preplay_tab =
   223         | reset_preplay_outcomes _ = ()
   221         Canonical_Label_Tab.empty
   224 
   222         |> fold_isar_steps add_step_to_tab (steps_of_proof proof)
   225       fun set_preplay_outcome l meth result =
   223         |> Unsynchronized.ref
   226         preplay_tab := Canonical_Label_Tab.map_entry l
       
   227           (AList.update (op =) (meth, Lazy.value result)) (!preplay_tab)
   224 
   228 
   225       fun preplay_outcome l meth =
   229       fun preplay_outcome l meth =
   226         (case Canonical_Label_Tab.lookup (!preplay_tab) l of
   230         (case Canonical_Label_Tab.lookup (!preplay_tab) l of
   227           SOME meths_outcomes =>
   231           SOME meths_outcomes =>
   228           (case AList.lookup (op =) meths_outcomes meth of
   232           (case AList.lookup (op =) meths_outcomes meth of
   229             SOME outcome => outcome
   233             SOME outcome => outcome
   230           | NONE => raise Fail "Sledgehammer_Isar_Preplay: missing method")
   234           | NONE => raise Fail "Sledgehammer_Isar_Preplay: missing method")
   231         | NONE => raise Fail "Sledgehammer_Isar_Preplay: missing label")
   235         | NONE => raise Fail "Sledgehammer_Isar_Preplay: missing label")
   232 
   236 
   233       fun set_preplay_outcome l meth result =
   237       val _ = fold_isar_steps (K o reset_preplay_outcomes) (steps_of_proof proof) ()
   234         preplay_tab := Canonical_Label_Tab.map_entry l
       
   235           (AList.update (op =) (meth, Lazy.value result)) (!preplay_tab)
       
   236 
   238 
   237       fun result_of_step (Prove (_, _, l, _, _, (_, (meth :: _) :: _))) =
   239       fun result_of_step (Prove (_, _, l, _, _, (_, (meth :: _) :: _))) =
   238           Lazy.force (preplay_outcome l meth)
   240           Lazy.force (preplay_outcome l meth)
   239         | result_of_step _ = Played Time.zeroTime
   241         | result_of_step _ = Played Time.zeroTime
   240 
   242 
   241       fun overall_preplay_outcome (Proof (_, _, steps)) =
   243       fun overall_preplay_outcome (Proof (_, _, steps)) =
   242         fold_isar_steps (merge_preplay_outcomes o result_of_step) steps (Played Time.zeroTime)
   244         fold_isar_steps (merge_preplay_outcomes o result_of_step) steps (Played Time.zeroTime)
   243     in
   245     in
   244       {preplay_outcome = preplay_outcome,
   246       {reset_preplay_outcomes = reset_preplay_outcomes,
   245        set_preplay_outcome = set_preplay_outcome,
   247        set_preplay_outcome = set_preplay_outcome,
       
   248        preplay_outcome = preplay_outcome,
   246        preplay_quietly = preplay_quietly,
   249        preplay_quietly = preplay_quietly,
   247        overall_preplay_outcome = overall_preplay_outcome}
   250        overall_preplay_outcome = overall_preplay_outcome}
   248     end
   251     end
   249 
   252 
   250 end;
   253 end;