src/HOL/Tools/Sledgehammer/sledgehammer_preplay.ML
author blanchet
Thu, 12 Sep 2013 22:10:57 +0200
changeset 53586 bd5fa6425993
parent 52692 9306c309b892
child 53664 51595a047730
permissions -rw-r--r--
prefixed types and some functions with "atp_" for disambiguation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_preplay.ML
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     4
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
     5
Preplaying of isar proofs.
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     6
*)
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     7
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     8
signature SLEDGEHAMMER_PREPLAY =
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     9
sig
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    10
  type isar_proof = Sledgehammer_Proof.isar_proof
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    11
  type isar_step = Sledgehammer_Proof.isar_step
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    12
  type label = Sledgehammer_Proof.label
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    13
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    14
  eqtype preplay_time
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    15
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    16
  datatype preplay_result =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    17
    PplFail of exn |
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    18
    PplSucc of preplay_time
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    19
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    20
  val zero_preplay_time : preplay_time
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    21
  val some_preplay_time : preplay_time
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    22
  val add_preplay_time : preplay_time -> preplay_time -> preplay_time
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    23
  val string_of_preplay_time : preplay_time -> string
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    24
  (*val preplay_raw : bool -> bool -> string -> string -> Proof.context ->
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    25
    Time.time -> isar_step -> preplay_time*)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    26
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    27
  type preplay_interface =
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    28
  { get_preplay_result : label -> preplay_result,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    29
    set_preplay_result : label -> preplay_result -> unit,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    30
    get_preplay_time : label -> preplay_time,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    31
    set_preplay_time : label -> preplay_time -> unit,
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    32
    preplay_quietly : Time.time -> isar_step -> preplay_time,
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    33
    (* the returned flag signals a preplay failure *)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    34
    overall_preplay_stats : isar_proof -> preplay_time * bool }
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    35
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    36
  val proof_preplay_interface :
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
    37
    bool -> Proof.context -> string -> string -> bool -> Time.time -> bool
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
    38
    -> isar_proof -> preplay_interface
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    39
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    40
end
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    41
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    42
structure Sledgehammer_Preplay : SLEDGEHAMMER_PREPLAY =
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    43
struct
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    44
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    45
open Sledgehammer_Util
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    46
open Sledgehammer_Proof
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    47
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    48
(* The boolean flag encodes whether the time is exact (false) or an lower bound
51131
7de262be1e95 preplay subblocks
smolkas
parents: 51130
diff changeset
    49
   (true):
7de262be1e95 preplay subblocks
smolkas
parents: 51130
diff changeset
    50
      (t, false) = "t ms"
7de262be1e95 preplay subblocks
smolkas
parents: 51130
diff changeset
    51
      (t, true)  = "> t ms" *)
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    52
type preplay_time = bool * Time.time
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    53
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    54
datatype preplay_result =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    55
  PplFail of exn |
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    56
  PplSucc of preplay_time
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    57
51131
7de262be1e95 preplay subblocks
smolkas
parents: 51130
diff changeset
    58
val zero_preplay_time = (false, Time.zeroTime) (* 0 ms *)
7de262be1e95 preplay subblocks
smolkas
parents: 51130
diff changeset
    59
val some_preplay_time = (true, Time.zeroTime)  (* > 0 ms *)
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    60
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    61
fun add_preplay_time (b1, t1) (b2, t2) = (b1 orelse b2, Time.+(t1,t2))
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    62
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51998
diff changeset
    63
val string_of_preplay_time = ATP_Util.string_of_ext_time
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    64
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    65
(* preplay tracing *)
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    66
fun preplay_trace ctxt assms concl time =
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    67
  let
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    68
    val ctxt = ctxt |> Config.put show_markup true
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    69
    val time = "[" ^ (string_of_preplay_time time) ^ "]" |> Pretty.str
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    70
    val nr_of_assms = length assms
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    71
    val assms = assms |> map (Display.pretty_thm ctxt)
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    72
                      |> (fn [] => Pretty.str ""
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    73
                           | [a] => a
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    74
                           | assms => Pretty.enum ";" "⟦" "⟧" assms)
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    75
    val concl = concl |> Syntax.pretty_term ctxt
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    76
    val trace_list = [] |> cons concl
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    77
                        |> nr_of_assms>0 ? cons (Pretty.str "⟹")
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    78
                        |> cons assms
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    79
                        |> cons time
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    80
    val pretty_trace = Pretty.blk(2, Pretty.breaks trace_list)
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    81
  in tracing (Pretty.string_of pretty_trace) end
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    82
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    83
(* timing *)
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    84
fun take_time timeout tac arg =
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    85
  let
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    86
    val timing = Timing.start ()
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    87
  in
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    88
    (TimeLimit.timeLimit timeout tac arg;
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    89
      Timing.result timing |> #cpu |> pair false)
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    90
    handle TimeLimit.TimeOut => (true, timeout)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    91
  end
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    92
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    93
(* lookup facts in context *)
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    94
fun resolve_fact_names ctxt names =
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    95
  (names
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51879
diff changeset
    96
    |>> map string_of_label
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    97
    |> op @
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    98
    |> maps (thms_of_name ctxt))
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    99
  handle ERROR msg => error ("preplay error: " ^ msg)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   100
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
   101
(* turn terms/proofs into theorems *)
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   102
fun thm_of_term ctxt = Skip_Proof.make_thm (Proof_Context.theory_of ctxt)
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
   103
fun thm_of_proof ctxt (Proof (Fix fixed_frees, Assume assms, steps)) =
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   104
  let
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   105
    val concl = (case try List.last steps of
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   106
                  SOME (Prove (_, Fix [], _, t, _, _)) => t
51876
724c67f59929 added informative error messages
smolkas
parents: 51179
diff changeset
   107
                | _ => raise Fail "preplay error: malformed subproof")
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   108
    val var_idx = maxidx_of_term concl + 1
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   109
    fun var_of_free (x, T) = Var((x, var_idx), T)
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   110
    val substitutions =
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   111
      map (`var_of_free #> swap #> apfst Free) fixed_frees
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   112
  in
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   113
    Logic.list_implies (assms |> map snd, concl)
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   114
      |> subst_free substitutions
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   115
      |> thm_of_term ctxt
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   116
  end
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   117
52633
smolkas
parents: 52629
diff changeset
   118
(* mapping from proof methods to tactics *)
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   119
fun tac_of_method method type_enc lam_trans ctxt facts =
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   120
  case method of
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   121
    MetisM => Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   122
  | _ =>
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   123
      Method.insert_tac facts
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   124
      THEN' (case method of
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   125
              SimpM => Simplifier.asm_full_simp_tac
52633
smolkas
parents: 52629
diff changeset
   126
            | AutoM => Clasimp.auto_tac #> K
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   127
            | FastforceM => Clasimp.fast_force_tac
52629
d6f2a7c196f7 added blast, force
smolkas
parents: 52627
diff changeset
   128
            | ForceM => Clasimp.force_tac
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   129
            | ArithM => Arith_Data.arith_tac
52629
d6f2a7c196f7 added blast, force
smolkas
parents: 52627
diff changeset
   130
            | BlastM => blast_tac
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   131
            | _ => raise Fail "Sledgehammer_Preplay: tac_of_method") ctxt
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   132
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   133
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   134
(* main function for preplaying isar_steps; may throw exceptions *)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   135
fun preplay_raw _ _ _ _ _ _ (Let _) = zero_preplay_time
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   136
  | preplay_raw debug trace type_enc lam_trans ctxt timeout
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   137
      (Prove (_, Fix xs, _, t, subproofs, By (fact_names, proof_method))) =
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   138
  let
52453
2cba5906d836 simplified data structure
smolkas
parents: 52125
diff changeset
   139
    val (prop, obtain) =
2cba5906d836 simplified data structure
smolkas
parents: 52125
diff changeset
   140
      (case xs of
2cba5906d836 simplified data structure
smolkas
parents: 52125
diff changeset
   141
        [] => (t, false)
2cba5906d836 simplified data structure
smolkas
parents: 52125
diff changeset
   142
      | _ =>
2cba5906d836 simplified data structure
smolkas
parents: 52125
diff changeset
   143
      (* proof obligation: !!thesis. (!!x. A x ==> thesis) ==> thesis
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   144
           (see ~~/src/Pure/Isar/obtain.ML) *)
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   145
        let
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   146
          val thesis = Term.Free ("thesis", HOLogic.boolT)
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   147
          val thesis_prop = thesis |> HOLogic.mk_Trueprop
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   148
          val frees = map Term.Free xs
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   149
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   150
          (* !!x1..xn. t ==> thesis (xs = [x1, .., xn]) *)
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   151
          val inner_prop =
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   152
            fold_rev Logic.all frees (Logic.mk_implies (t, thesis_prop))
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   153
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   154
          (* !!thesis. (!!x1..xn. t ==> thesis) ==> thesis *)
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   155
          val prop =
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   156
            Logic.all thesis (Logic.mk_implies (inner_prop, thesis_prop))
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   157
        in
52453
2cba5906d836 simplified data structure
smolkas
parents: 52125
diff changeset
   158
          (prop, true)
2cba5906d836 simplified data structure
smolkas
parents: 52125
diff changeset
   159
        end)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   160
    val facts =
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
   161
      map (thm_of_proof ctxt) subproofs @ resolve_fact_names ctxt fact_names
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   162
    val ctxt = ctxt |> Config.put Metis_Tactic.verbose debug
52692
smolkas
parents: 52633
diff changeset
   163
                    |> Config.put Lin_Arith.verbose debug
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   164
                    |> obtain ? Config.put Metis_Tactic.new_skolem true
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   165
    val goal =
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
   166
      Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] prop
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   167
    fun tac {context = ctxt, prems = _} =
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   168
      HEADGOAL (tac_of_method proof_method type_enc lam_trans ctxt facts)
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
   169
    fun run_tac () = goal tac
52627
smolkas
parents: 52626
diff changeset
   170
      handle ERROR msg => error ("Preplay error: " ^ msg)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   171
    val preplay_time = take_time timeout run_tac ()
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   172
  in
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   173
    (* tracing *)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   174
    (if trace then preplay_trace ctxt facts prop preplay_time else () ;
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   175
     preplay_time)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   176
  end
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   177
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   178
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   179
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   180
(*** proof preplay interface ***)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   181
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   182
type preplay_interface =
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   183
{ get_preplay_result : label -> preplay_result,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   184
  set_preplay_result : label -> preplay_result -> unit,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   185
  get_preplay_time : label -> preplay_time,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   186
  set_preplay_time : label -> preplay_time -> unit,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   187
  preplay_quietly : Time.time -> isar_step -> preplay_time,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   188
  (* the returned flag signals a preplay failure *)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   189
  overall_preplay_stats : isar_proof -> preplay_time * bool }
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   190
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   191
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   192
(* enriches context with local proof facts *)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   193
fun enrich_context proof ctxt =
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   194
  let
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   195
    val thy = Proof_Context.theory_of ctxt
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   196
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   197
    fun enrich_with_fact l t =
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   198
      Proof_Context.put_thms false
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   199
        (string_of_label l, SOME [Skip_Proof.make_thm thy t])
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   200
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   201
    val enrich_with_assms = fold (uncurry enrich_with_fact)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   202
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   203
    fun enrich_with_proof (Proof (_, Assume assms, isar_steps)) =
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   204
      enrich_with_assms assms #> fold enrich_with_step isar_steps
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   205
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   206
    and enrich_with_step (Let _) = I
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   207
      | enrich_with_step (Prove (_, _, l, t, subproofs, _)) =
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   208
          enrich_with_fact l t #> fold enrich_with_proof subproofs
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   209
  in
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   210
    enrich_with_proof proof ctxt
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   211
  end
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   212
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   213
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   214
(* Given a proof, produces an imperative preplay interface with a shared table
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   215
   mapping from labels to preplay results.
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   216
   The preplay results are caluclated lazyly and cached to avoid repeated
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   217
   calculation.
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   218
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   219
   PRE CONDITION: the proof must be labeled canocially, see
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   220
   Slegehammer_Proof.relabel_proof_canonically
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   221
*)
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   222
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   223
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   224
fun proof_preplay_interface debug ctxt type_enc lam_trans do_preplay
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   225
      preplay_timeout preplay_trace proof : preplay_interface =
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   226
  if not do_preplay then
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   227
    (* the dont_preplay option pretends that everything works just fine *)
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   228
    { get_preplay_result = K (PplSucc zero_preplay_time),
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   229
      set_preplay_result = K (K ()),
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   230
      get_preplay_time = K zero_preplay_time,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   231
      set_preplay_time = K (K ()),
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   232
      preplay_quietly = K (K zero_preplay_time),
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   233
      overall_preplay_stats = K (zero_preplay_time, false)}
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   234
  else
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   235
    let
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   236
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   237
      (* add local proof facts to context *)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   238
      val ctxt = enrich_context proof ctxt
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   239
52627
smolkas
parents: 52626
diff changeset
   240
      fun preplay quietly timeout step =
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   241
        preplay_raw debug preplay_trace type_enc lam_trans ctxt timeout step
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   242
        |> PplSucc
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   243
        handle exn =>
52627
smolkas
parents: 52626
diff changeset
   244
          if Exn.is_interrupt exn then
smolkas
parents: 52626
diff changeset
   245
            reraise exn
smolkas
parents: 52626
diff changeset
   246
          else if not quietly andalso debug then
smolkas
parents: 52626
diff changeset
   247
            (warning ("Preplay failure:\n  " ^ @{make_string} step ^ "\n  "
smolkas
parents: 52626
diff changeset
   248
                      ^ @{make_string} exn);
smolkas
parents: 52626
diff changeset
   249
             PplFail exn)
smolkas
parents: 52626
diff changeset
   250
          else
smolkas
parents: 52626
diff changeset
   251
            PplFail exn
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   252
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   253
      (* preplay steps treating exceptions like timeouts *)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   254
      fun preplay_quietly timeout step =
52627
smolkas
parents: 52626
diff changeset
   255
        case preplay true timeout step of
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   256
          PplSucc preplay_time => preplay_time
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   257
        | PplFail _ => (true, timeout)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   258
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   259
      val preplay_tab =
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   260
        let
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   261
          fun add_step_to_tab step tab =
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   262
            case label_of_step step of
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   263
              NONE => tab
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   264
            | SOME l =>
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   265
                Canonical_Lbl_Tab.update_new
52627
smolkas
parents: 52626
diff changeset
   266
                  (l, (fn () => preplay false preplay_timeout step) |> Lazy.lazy)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   267
                  tab
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   268
            handle Canonical_Lbl_Tab.DUP _ =>
52575
e78ea835b5f8 made SML/NJ happy
smolkas
parents: 52556
diff changeset
   269
              raise Fail "Sledgehammer_Preplay: preplay time table"
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   270
        in
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   271
          Canonical_Lbl_Tab.empty
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   272
          |> fold_isar_steps add_step_to_tab (steps_of_proof proof)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   273
          |> Unsynchronized.ref
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   274
        end
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   275
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   276
      fun get_preplay_result lbl =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   277
        Canonical_Lbl_Tab.lookup (!preplay_tab) lbl |> the |> Lazy.force
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   278
        handle
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   279
          Option.Option =>
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   280
            raise Fail "Sledgehammer_Preplay: preplay time table"
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   281
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   282
      fun set_preplay_result lbl result =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   283
        preplay_tab :=
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   284
          Canonical_Lbl_Tab.update (lbl, Lazy.value result) (!preplay_tab)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   285
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   286
      fun get_preplay_time lbl =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   287
        case get_preplay_result lbl of
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   288
          PplSucc preplay_time => preplay_time
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   289
        | PplFail _ => some_preplay_time (* best approximation possible *)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   290
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   291
      fun set_preplay_time lbl time = set_preplay_result lbl (PplSucc time)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   292
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   293
      fun overall_preplay_stats (Proof(_, _, steps)) =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   294
        let
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   295
          fun result_of_step step =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   296
            try (label_of_step #> the #> get_preplay_result) step
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   297
            |> the_default (PplSucc zero_preplay_time)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   298
          fun do_step step =
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   299
            case result_of_step step of
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   300
              PplSucc preplay_time => apfst (add_preplay_time preplay_time)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   301
            | PplFail _ => apsnd (K true)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   302
        in
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   303
          fold_isar_steps do_step steps (zero_preplay_time, false)
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   304
        end
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   305
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   306
    in
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   307
      { get_preplay_result = get_preplay_result,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   308
        set_preplay_result = set_preplay_result,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   309
        get_preplay_time = get_preplay_time,
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   310
        set_preplay_time = set_preplay_time,
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   311
        preplay_quietly = preplay_quietly,
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   312
        overall_preplay_stats = overall_preplay_stats}
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   313
    end
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   314
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   315
end