src/HOL/Tools/Sledgehammer/sledgehammer_isar_minimize.ML
author blanchet
Mon Feb 03 16:53:58 2014 +0100 (2014-02-03 ago)
changeset 55287 ffa306239316
parent 55280 f0187a12b8f2
child 55295 b18f65f77fcd
permissions -rw-r--r--
renamed ML file
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@55287
    24
open Sledgehammer_Proof_Methods
blanchet@55202
    25
open Sledgehammer_Isar_Proof
blanchet@55202
    26
open Sledgehammer_Isar_Preplay
smolkas@52611
    27
blanchet@55280
    28
fun keep_fastest_method_of_isar_step preplay_data (Prove (qs, xs, l, t, subproofs, facts, _)) =
blanchet@55280
    29
    Prove (qs, xs, l, t, subproofs, facts, [fastest_method_of_isar_step preplay_data l])
blanchet@55266
    30
  | keep_fastest_method_of_isar_step _ step = step
blanchet@55266
    31
blanchet@54712
    32
val slack = seconds 0.1
smolkas@52611
    33
blanchet@55266
    34
fun minimize_isar_step_dependencies ctxt preplay_data
blanchet@55280
    35
      (step as Prove (qs, xs, l, t, subproofs, (lfs0, gfs0), meths as meth :: _)) =
blanchet@55266
    36
    (case Lazy.force (preplay_outcome_of_isar_step_for_method (!preplay_data) l meth) of
blanchet@54828
    37
      Played time =>
blanchet@54712
    38
      let
blanchet@55280
    39
        fun mk_step_lfs_gfs lfs gfs = Prove (qs, xs, l, t, subproofs, (lfs, gfs), meths)
smolkas@52611
    40
blanchet@54712
    41
        fun minimize_facts _ time min_facts [] = (time, min_facts)
blanchet@54712
    42
          | minimize_facts mk_step time min_facts (f :: facts) =
blanchet@55278
    43
            (case preplay_isar_step_for_method ctxt (Time.+ (time, slack)) meth
blanchet@55258
    44
                (mk_step (min_facts @ facts)) of
blanchet@54828
    45
              Played time => minimize_facts mk_step time min_facts facts
blanchet@54827
    46
            | _ => minimize_facts mk_step time (f :: min_facts) facts)
smolkas@52611
    47
blanchet@55268
    48
        val (time, min_lfs) = minimize_facts (fn lfs => mk_step_lfs_gfs lfs gfs0) time [] lfs0
blanchet@55268
    49
        val (time, min_gfs) = minimize_facts (mk_step_lfs_gfs min_lfs) time [] gfs0
blanchet@55264
    50
blanchet@55264
    51
        val step' = mk_step_lfs_gfs min_lfs min_gfs
blanchet@54712
    52
      in
blanchet@55278
    53
        set_preplay_outcomes_of_isar_step ctxt time preplay_data step' [(meth, Played time)];
blanchet@55264
    54
        step'
blanchet@54826
    55
      end
blanchet@54826
    56
    | _ => step (* don't touch steps that time out or fail *))
blanchet@55266
    57
  | minimize_isar_step_dependencies _ _ step = step
smolkas@52611
    58
blanchet@55213
    59
fun postprocess_isar_proof_remove_unreferenced_steps postproc_step =
smolkas@52611
    60
  let
blanchet@55259
    61
    fun process_proof (Proof (fix, assms, steps)) =
blanchet@55265
    62
      process_steps steps ||> (fn steps => Proof (fix, assms, steps))
blanchet@55259
    63
    and process_steps [] = ([], [])
blanchet@55259
    64
      | process_steps steps =
blanchet@55212
    65
        (* the last step is always implicitly referenced *)
blanchet@55265
    66
        let val (steps, (used, concl)) = split_last steps ||> process_used_step in
blanchet@55265
    67
          fold_rev process_step steps (used, [concl])
blanchet@54754
    68
        end
blanchet@55265
    69
    and process_step step (used, accu) =
blanchet@55223
    70
      (case label_of_isar_step step of
blanchet@55265
    71
        NONE => (used, step :: accu)
smolkas@52611
    72
      | SOME l =>
blanchet@55265
    73
        if Ord_List.member label_ord used l then
blanchet@55265
    74
          process_used_step step |>> Ord_List.union label_ord used ||> (fn step => step :: accu)
blanchet@54813
    75
        else
blanchet@55265
    76
          (used, accu))
blanchet@55265
    77
    and process_used_step step = step |> postproc_step |> process_used_step_subproofs
blanchet@55280
    78
    and process_used_step_subproofs (Prove (qs, xs, l, t, subproofs, (lfs, gfs), meths)) =
blanchet@55259
    79
      let
blanchet@55265
    80
        val (used, subproofs) =
blanchet@55259
    81
          map process_proof subproofs
blanchet@55259
    82
          |> split_list
blanchet@55259
    83
          |>> Ord_List.unions label_ord
blanchet@55259
    84
          |>> fold (Ord_List.insert label_ord) lfs
blanchet@55259
    85
      in
blanchet@55280
    86
        (used, Prove (qs, xs, l, t, subproofs, (lfs, gfs), meths))
blanchet@55259
    87
      end
smolkas@52611
    88
  in
blanchet@55259
    89
    snd o process_proof
smolkas@52611
    90
  end
smolkas@52611
    91
blanchet@54504
    92
end;