src/HOL/Tools/Sledgehammer/sledgehammer_try0.ML
author smolkas
Thu, 11 Jul 2013 20:08:06 +0200
changeset 52592 8a25b17e3d79
child 52611 831f7479c74f
permissions -rw-r--r--
optimize isar-proofs by trying different proof methods
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_try0.ML
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     4
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     5
Try replacing calls to metis with calls to other proof methods in order to
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     6
speed up proofs, eliminate dependencies, and repair broken proof steps.
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     7
*)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     8
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
     9
signature SLEDGEHAMMER_TRY0 =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    10
sig
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    11
  type isar_proof = Sledgehammer_Proof.isar_proof
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    12
  type preplay_interface = Sledgehammer_Preplay.preplay_interface
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    13
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    14
  val try0 : Time.time -> preplay_interface -> isar_proof -> isar_proof
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    15
end
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    16
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    17
structure Sledgehammer_Try0 : SLEDGEHAMMER_TRY0 =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    18
struct
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    19
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    20
open Sledgehammer_Proof
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    21
open Sledgehammer_Preplay
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    22
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    23
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    24
fun reachable_labels proof =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    25
  let
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    26
    val refs_of_step =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    27
      byline_of_step #> (fn SOME (By ((lfs, _), _)) => lfs | NONE => [])
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    28
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    29
    val union = fold (Ord_List.insert label_ord)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    30
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    31
    fun do_proof proof reachable =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    32
      let
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    33
        val (steps, concl) = split_last (steps_of_proof proof)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    34
        val reachable =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    35
          union (refs_of_step concl) reachable
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    36
          |> union (the_list (label_of_step concl))
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    37
      in
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    38
        fold_rev do_step steps reachable
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    39
      end
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    40
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    41
    and do_step (Let _) reachable = reachable
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    42
      | do_step (Prove (_, _, l, _, subproofs, By ((lfs, _), _))) reachable =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    43
        if Ord_List.member label_ord reachable l
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    44
          then fold do_proof subproofs (union lfs reachable)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    45
          else reachable
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    46
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    47
  in
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    48
    do_proof proof []
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    49
  end
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    50
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    51
(** removes steps not referenced by the final proof step or any of its
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    52
    predecessors **)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    53
fun remove_unreachable_steps ({set_time, ...} : preplay_interface) proof =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    54
  let
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    55
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    56
    val reachable = reachable_labels proof
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    57
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    58
    fun do_proof (Proof (fix, assms, steps)) =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    59
      Proof (fix, assms, do_steps steps)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    60
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    61
    and do_steps steps =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    62
      uncurry (fold_rev do_step) (split_last steps ||> single)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    63
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    64
    and do_step (step as Let _) accu = step :: accu
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    65
      | do_step (Prove (qs, xs, l, t, subproofs, by)) accu =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    66
          if Ord_List.member label_ord reachable l
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    67
            then Prove (qs, xs, l, t, map do_proof subproofs, by) :: accu
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    68
            else (set_time l zero_preplay_time; accu)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    69
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    70
  in
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    71
    do_proof proof
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    72
  end
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    73
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    74
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    75
fun variants (step as Let _) = raise Fail "Sledgehammer_Try0: variants"
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    76
  | variants (Prove (qs, xs, l, t, subproofs, By (facts, method))) =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    77
      let
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    78
        val methods = [SimpM, AutoM, FastforceM, ArithM]
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    79
        fun step method = Prove (qs, xs, l, t, subproofs, By (facts, method))
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    80
        fun step_without_facts method =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    81
          Prove (qs, xs, l, t, subproofs, By (no_facts, method))
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    82
      in
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    83
        (* There seems to be a bias for methods earlier in the list, so we put
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    84
           the variants using no facts first. *)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    85
        map step_without_facts methods @ map step methods
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    86
      end
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    87
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    88
fun try0_step preplay_timeout preplay_interface (step as Let _) = step
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    89
  | try0_step preplay_timeout ({preplay_quietly, get_time, set_time,
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    90
    set_preplay_fail, ...} : preplay_interface)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    91
    (step as Prove (_, _, l, _, _, _)) =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    92
      let
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    93
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    94
        val (preplay_fail, timeout) =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    95
          case get_time l of
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    96
            (true, _) => (true, preplay_timeout)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    97
          | (false, t) => (false, t)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    98
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
    99
        fun try_variant variant =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   100
           case preplay_quietly timeout variant of
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   101
             (true, _) => NONE
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   102
           | time as (false, _) => SOME (variant, time)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   103
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   104
      in
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   105
        case Par_List.get_some try_variant (variants step) of
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   106
          SOME (step, time) => (set_time l time; step)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   107
        | NONE => (if preplay_fail then set_preplay_fail true else (); step)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   108
      end
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   109
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   110
fun try0 preplay_timeout
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   111
  (preplay_interface as {set_preplay_fail, ...} : preplay_interface) proof =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   112
    (set_preplay_fail false; (* reset preplay fail *)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   113
     map_isar_steps (try0_step preplay_timeout preplay_interface) proof
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   114
     |> remove_unreachable_steps preplay_interface)
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   115
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents:
diff changeset
   116
end