src/HOL/Tools/Sledgehammer/sledgehammer_isar_minimize.ML
author blanchet
Fri Jan 31 16:10:39 2014 +0100 (2014-01-31 ago)
changeset 55212 5832470d956e
parent 55202 824c48a539c9
child 55213 dcb36a2540bc
permissions -rw-r--r--
tuning
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@55212
    10
  type preplay_data = Sledgehammer_Isar_Preplay.preplay_data
blanchet@55202
    11
  type isar_step = Sledgehammer_Isar_Proof.isar_step
blanchet@55202
    12
  type isar_proof = Sledgehammer_Isar_Proof.isar_proof
blanchet@54712
    13
blanchet@55212
    14
  val minimal_deps_of_step : preplay_data -> isar_step -> isar_step
blanchet@54712
    15
  val postprocess_remove_unreferenced_steps : (isar_step -> isar_step) -> isar_proof -> isar_proof
blanchet@54504
    16
end;
smolkas@52611
    17
blanchet@55202
    18
structure Sledgehammer_Isar_Minimize : SLEDGEHAMMER_ISAR_MINIMIZE =
smolkas@52611
    19
struct
smolkas@52611
    20
blanchet@54828
    21
open Sledgehammer_Reconstructor
blanchet@55202
    22
open Sledgehammer_Isar_Proof
blanchet@55202
    23
open Sledgehammer_Isar_Preplay
smolkas@52611
    24
blanchet@54712
    25
val slack = seconds 0.1
smolkas@52611
    26
blanchet@55212
    27
fun minimal_deps_of_step (_ : preplay_data) (step as Let _) = step
blanchet@55212
    28
  | minimal_deps_of_step {get_preplay_outcome, set_preplay_outcome, preplay_quietly, ...}
blanchet@54712
    29
      (step as Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meth))) =
blanchet@54828
    30
    (case get_preplay_outcome l of
blanchet@54828
    31
      Played time =>
blanchet@54712
    32
      let
blanchet@54712
    33
        fun mk_step_lfs_gfs lfs gfs = Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meth))
blanchet@54712
    34
        val mk_step_gfs_lfs = curry (swap #> uncurry mk_step_lfs_gfs)
smolkas@52611
    35
blanchet@54712
    36
        fun minimize_facts _ time min_facts [] = (time, min_facts)
blanchet@54712
    37
          | minimize_facts mk_step time min_facts (f :: facts) =
blanchet@54712
    38
            (case preplay_quietly (Time.+ (time, slack)) (mk_step (min_facts @ facts)) of
blanchet@54828
    39
              Played time => minimize_facts mk_step time min_facts facts
blanchet@54827
    40
            | _ => minimize_facts mk_step time (f :: min_facts) facts)
smolkas@52611
    41
blanchet@54712
    42
        val (time, min_lfs) = minimize_facts (mk_step_gfs_lfs gfs) time [] lfs
blanchet@54712
    43
        val (time, min_gfs) = minimize_facts (mk_step_lfs_gfs min_lfs) time [] gfs
blanchet@54712
    44
      in
blanchet@54828
    45
        set_preplay_outcome l (Played time); mk_step_lfs_gfs min_lfs min_gfs
blanchet@54826
    46
      end
blanchet@54826
    47
    | _ => step (* don't touch steps that time out or fail *))
smolkas@52611
    48
blanchet@54712
    49
fun postprocess_remove_unreferenced_steps postproc_step =
smolkas@52611
    50
  let
smolkas@52611
    51
    val add_lfs = fold (Ord_List.insert label_ord)
smolkas@52611
    52
smolkas@52611
    53
    fun do_proof (Proof (fix, assms, steps)) =
blanchet@54712
    54
      let val (refed, steps) = do_steps steps in
smolkas@52611
    55
        (refed, Proof (fix, assms, steps))
smolkas@52611
    56
      end
blanchet@54754
    57
    and do_steps [] = ([], [])
blanchet@54754
    58
      | do_steps steps =
blanchet@55212
    59
        (* the last step is always implicitly referenced *)
blanchet@55212
    60
        let val (steps, (refed, concl)) = split_last steps ||> do_refed_step in
blanchet@54754
    61
          fold_rev do_step steps (refed, [concl])
blanchet@54754
    62
        end
smolkas@52611
    63
    and do_step step (refed, accu) =
blanchet@54754
    64
      (case label_of_step step of
smolkas@52611
    65
        NONE => (refed, step :: accu)
smolkas@52611
    66
      | SOME l =>
blanchet@54813
    67
        if Ord_List.member label_ord refed l then
blanchet@54813
    68
          do_refed_step step
blanchet@54813
    69
          |>> Ord_List.union label_ord refed
blanchet@54813
    70
          ||> (fn x => x :: accu)
blanchet@54813
    71
        else
blanchet@54813
    72
          (refed, accu))
blanchet@54712
    73
    and do_refed_step step = step |> postproc_step |> do_refed_step'
blanchet@55202
    74
    and do_refed_step' (Let _) = raise Fail "Sledgehammer_Isar_Minimize"
blanchet@54700
    75
      | do_refed_step' (Prove (qs, xs, l, t, subproofs, ((lfs, gfs), m))) =
smolkas@52611
    76
        let
smolkas@52611
    77
          val (refed, subproofs) =
smolkas@52611
    78
            map do_proof subproofs
smolkas@52611
    79
            |> split_list
smolkas@52611
    80
            |>> Ord_List.unions label_ord
smolkas@52611
    81
            |>> add_lfs lfs
blanchet@54700
    82
          val step = Prove (qs, xs, l, t, subproofs, ((lfs, gfs), m))
smolkas@52611
    83
        in
smolkas@52611
    84
          (refed, step)
smolkas@52611
    85
        end
smolkas@52611
    86
  in
blanchet@54712
    87
    snd o do_proof
smolkas@52611
    88
  end
smolkas@52611
    89
blanchet@54504
    90
end;