src/HOL/Tools/Sledgehammer/sledgehammer_isar_minimize.ML
author blanchet
Mon Feb 03 10:14:18 2014 +0100 (2014-02-03 ago)
changeset 55266 d9d31354834e
parent 55265 bd9f033b9896
child 55268 a46458d368d5
permissions -rw-r--r--
centralize more preplaying
blanchet@55202
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar_minimize.ML
blanchet@54712
     2
    Author:     Steffen Juilf Smolka, TU Muenchen
smolkas@52611
     3
    Author:     Jasmin Blanchette, TU Muenchen
smolkas@52611
     4
smolkas@52611
     5
Minimize dependencies (used facts) of Isar proof steps.
smolkas@52611
     6
*)
smolkas@52611
     7
blanchet@55202
     8
signature SLEDGEHAMMER_ISAR_MINIMIZE =
smolkas@52611
     9
sig
blanchet@55202
    10
  type isar_step = Sledgehammer_Isar_Proof.isar_step
blanchet@55202
    11
  type isar_proof = Sledgehammer_Isar_Proof.isar_proof
blanchet@55213
    12
  type isar_preplay_data = Sledgehammer_Isar_Preplay.isar_preplay_data
blanchet@54712
    13
blanchet@55266
    14
  val keep_fastest_method_of_isar_step : isar_preplay_data -> isar_step -> isar_step
blanchet@55260
    15
  val minimize_isar_step_dependencies : Proof.context -> isar_preplay_data Unsynchronized.ref ->
blanchet@55260
    16
    isar_step -> isar_step
blanchet@55213
    17
  val postprocess_isar_proof_remove_unreferenced_steps : (isar_step -> isar_step) -> isar_proof ->
blanchet@55213
    18
    isar_proof
blanchet@54504
    19
end;
smolkas@52611
    20
blanchet@55202
    21
structure Sledgehammer_Isar_Minimize : SLEDGEHAMMER_ISAR_MINIMIZE =
smolkas@52611
    22
struct
smolkas@52611
    23
blanchet@54828
    24
open Sledgehammer_Reconstructor
blanchet@55202
    25
open Sledgehammer_Isar_Proof
blanchet@55202
    26
open Sledgehammer_Isar_Preplay
smolkas@52611
    27
blanchet@55266
    28
fun keep_fastest_method_of_isar_step preplay_data
blanchet@55266
    29
      (Prove (qs, xs, l, t, subproofs, (facts, _))) =
blanchet@55266
    30
    Prove (qs, xs, l, t, subproofs, (facts, [fastest_method_of_isar_step preplay_data l]))
blanchet@55266
    31
  | keep_fastest_method_of_isar_step _ step = step
blanchet@55266
    32
blanchet@54712
    33
val slack = seconds 0.1
smolkas@52611
    34
blanchet@55266
    35
fun minimize_isar_step_dependencies ctxt preplay_data
blanchet@55244
    36
      (step as Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meths as meth :: _))) =
blanchet@55266
    37
    (case Lazy.force (preplay_outcome_of_isar_step_for_method (!preplay_data) l meth) of
blanchet@54828
    38
      Played time =>
blanchet@54712
    39
      let
blanchet@55244
    40
        fun mk_step_lfs_gfs lfs gfs = Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meths))
smolkas@52611
    41
blanchet@54712
    42
        fun minimize_facts _ time min_facts [] = (time, min_facts)
blanchet@54712
    43
          | minimize_facts mk_step time min_facts (f :: facts) =
blanchet@55258
    44
            (case preplay_isar_step ctxt (Time.+ (time, slack)) meth
blanchet@55258
    45
                (mk_step (min_facts @ facts)) of
blanchet@54828
    46
              Played time => minimize_facts mk_step time min_facts facts
blanchet@54827
    47
            | _ => minimize_facts mk_step time (f :: min_facts) facts)
smolkas@52611
    48
blanchet@55263
    49
        val (time, min_lfs) = minimize_facts (fn lfs => mk_step_lfs_gfs lfs gfs) time [] lfs
blanchet@54712
    50
        val (time, min_gfs) = minimize_facts (mk_step_lfs_gfs min_lfs) time [] gfs
blanchet@55264
    51
blanchet@55264
    52
        val step' = mk_step_lfs_gfs min_lfs min_gfs
blanchet@54712
    53
      in
blanchet@55264
    54
        set_preplay_outcomes_of_isar_step ctxt time preplay_data step'
blanchet@55264
    55
          [(meth, Lazy.value (Played time))];
blanchet@55264
    56
        step'
blanchet@54826
    57
      end
blanchet@54826
    58
    | _ => step (* don't touch steps that time out or fail *))
blanchet@55266
    59
  | minimize_isar_step_dependencies _ _ step = step
smolkas@52611
    60
blanchet@55213
    61
fun postprocess_isar_proof_remove_unreferenced_steps postproc_step =
smolkas@52611
    62
  let
blanchet@55259
    63
    fun process_proof (Proof (fix, assms, steps)) =
blanchet@55265
    64
      process_steps steps ||> (fn steps => Proof (fix, assms, steps))
blanchet@55259
    65
    and process_steps [] = ([], [])
blanchet@55259
    66
      | process_steps steps =
blanchet@55212
    67
        (* the last step is always implicitly referenced *)
blanchet@55265
    68
        let val (steps, (used, concl)) = split_last steps ||> process_used_step in
blanchet@55265
    69
          fold_rev process_step steps (used, [concl])
blanchet@54754
    70
        end
blanchet@55265
    71
    and process_step step (used, accu) =
blanchet@55223
    72
      (case label_of_isar_step step of
blanchet@55265
    73
        NONE => (used, step :: accu)
smolkas@52611
    74
      | SOME l =>
blanchet@55265
    75
        if Ord_List.member label_ord used l then
blanchet@55265
    76
          process_used_step step |>> Ord_List.union label_ord used ||> (fn step => step :: accu)
blanchet@54813
    77
        else
blanchet@55265
    78
          (used, accu))
blanchet@55265
    79
    and process_used_step step = step |> postproc_step |> process_used_step_subproofs
blanchet@55265
    80
    and process_used_step_subproofs (Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meths))) =
blanchet@55259
    81
      let
blanchet@55265
    82
        val (used, subproofs) =
blanchet@55259
    83
          map process_proof subproofs
blanchet@55259
    84
          |> split_list
blanchet@55259
    85
          |>> Ord_List.unions label_ord
blanchet@55259
    86
          |>> fold (Ord_List.insert label_ord) lfs
blanchet@55265
    87
        val step = Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meths))
blanchet@55259
    88
      in
blanchet@55265
    89
        (used, step)
blanchet@55259
    90
      end
smolkas@52611
    91
  in
blanchet@55259
    92
    snd o process_proof
smolkas@52611
    93
  end
smolkas@52611
    94
blanchet@54504
    95
end;