src/HOL/Tools/Sledgehammer/sledgehammer_preplay.ML
author blanchet
Mon, 16 Dec 2013 12:26:18 +0100
changeset 54766 6ac273f176cd
parent 54765 b05b0ea06306
child 54767 81290fe85890
permissions -rw-r--r--
store alternative proof methods in Isar data structure
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
54712
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
     2
    Author:     Steffen Juilf Smolka, TU Muenchen
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, TU Muenchen
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     4
54763
blanchet
parents: 54761
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
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    14
  type preplay_result
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    15
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    16
  val trace: bool Config.T
54763
blanchet
parents: 54761
diff changeset
    17
  val zero_preplay_time: bool * Time.time
blanchet
parents: 54761
diff changeset
    18
  val some_preplay_time: bool * Time.time
blanchet
parents: 54761
diff changeset
    19
  val add_preplay_time: bool * Time.time -> bool * Time.time -> bool * Time.time
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    20
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    21
  type preplay_interface =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    22
    {get_preplay_result: label -> preplay_result,
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    23
     set_preplay_result: label -> preplay_result -> unit,
54763
blanchet
parents: 54761
diff changeset
    24
     get_preplay_time: label -> bool * Time.time,
blanchet
parents: 54761
diff changeset
    25
     set_preplay_time: label -> bool * Time.time -> unit,
blanchet
parents: 54761
diff changeset
    26
     preplay_quietly: Time.time -> isar_step -> bool * Time.time,
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    27
     (* the returned flag signals a preplay failure *)
54763
blanchet
parents: 54761
diff changeset
    28
     overall_preplay_stats: isar_proof -> (bool * Time.time) * bool}
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    29
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    30
  val proof_preplay_interface: bool -> Proof.context -> string -> string -> bool -> Time.time ->
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    31
    isar_proof -> preplay_interface
54504
blanchet
parents: 53761
diff changeset
    32
end;
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    33
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    34
structure Sledgehammer_Preplay : SLEDGEHAMMER_PREPLAY =
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    35
struct
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    36
54763
blanchet
parents: 54761
diff changeset
    37
open ATP_Util
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    38
open Sledgehammer_Util
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    39
open Sledgehammer_Proof
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    40
54763
blanchet
parents: 54761
diff changeset
    41
val trace = Attrib.setup_config_bool @{binding sledgehammer_preplay_trace} (K false)
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    42
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    43
datatype preplay_result =
54763
blanchet
parents: 54761
diff changeset
    44
  Preplay_Success of bool * Time.time |
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    45
  Preplay_Failure of exn
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
    46
51131
7de262be1e95 preplay subblocks
smolkas
parents: 51130
diff changeset
    47
val zero_preplay_time = (false, Time.zeroTime) (* 0 ms *)
7de262be1e95 preplay subblocks
smolkas
parents: 51130
diff changeset
    48
val some_preplay_time = (true, Time.zeroTime)  (* > 0 ms *)
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    49
54763
blanchet
parents: 54761
diff changeset
    50
fun add_preplay_time (b1, t1) (b2, t2) = (b1 orelse b2, Time.+ (t1, t2))
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    51
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    52
fun preplay_trace ctxt assms concl time =
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    53
  let
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    54
    val ctxt = ctxt |> Config.put show_markup true
54763
blanchet
parents: 54761
diff changeset
    55
    val time = "[" ^ string_of_ext_time time ^ "]" |> Pretty.str
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    56
    val nr_of_assms = length assms
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    57
    val assms = assms
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    58
      |> map (Display.pretty_thm ctxt)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    59
      |> (fn [] => Pretty.str ""
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    60
           | [a] => a
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    61
           | assms => Pretty.enum ";" "\<lbrakk>" "\<rbrakk>" assms)  (* Syntax.pretty_term!? *)
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    62
    val concl = concl |> Syntax.pretty_term ctxt
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    63
    val trace_list = []
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    64
      |> cons concl
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    65
      |> nr_of_assms > 0 ? cons (Pretty.str "\<Longrightarrow>")
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    66
      |> cons assms
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    67
      |> cons time
53761
4d34e267fba9 use configuration mechanism for low-level tracing
blanchet
parents: 53664
diff changeset
    68
    val pretty_trace = Pretty.blk (2, Pretty.breaks trace_list)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    69
  in
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    70
    tracing (Pretty.string_of pretty_trace)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    71
  end
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    72
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    73
fun take_time timeout tac arg =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    74
  let val timing = Timing.start () in
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    75
    (TimeLimit.timeLimit timeout tac arg;
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    76
     Timing.result timing |> #cpu |> pair false)
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    77
    handle TimeLimit.TimeOut => (true, timeout)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    78
  end
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    79
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    80
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
    81
  (names
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    82
   |>> map string_of_label
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    83
   |> op @
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    84
   |> maps (thms_of_name ctxt))
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
    85
  handle ERROR msg => error ("preplay error: " ^ msg)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    86
54700
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    87
fun thm_of_proof ctxt (Proof (fixed_frees, assms, steps)) =
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    88
  let
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    89
    val thy = Proof_Context.theory_of ctxt
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    90
54700
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    91
    val concl = 
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    92
      (case try List.last steps of
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    93
        SOME (Prove (_, [], _, t, _, _)) => t
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    94
      | _ => raise Fail "preplay error: malformed subproof")
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    95
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
    96
    val var_idx = maxidx_of_term concl + 1
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    97
    fun var_of_free (x, T) = Var ((x, var_idx), T)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    98
    val subst = map (`var_of_free #> swap #> apfst Free) fixed_frees
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
    99
  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
   100
    Logic.list_implies (assms |> map snd, concl)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   101
    |> subst_free subst
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   102
    |> Skip_Proof.make_thm thy
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   103
  end
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
   104
54766
6ac273f176cd store alternative proof methods in Isar data structure
blanchet
parents: 54765
diff changeset
   105
fun tac_of_method meth type_enc lam_trans ctxt facts =
6ac273f176cd store alternative proof methods in Isar data structure
blanchet
parents: 54765
diff changeset
   106
  (case meth of
54765
blanchet
parents: 54764
diff changeset
   107
    Metis_Method => Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts
blanchet
parents: 54764
diff changeset
   108
  | Meson_Method => Meson.meson_tac ctxt facts
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   109
  | _ =>
54763
blanchet
parents: 54761
diff changeset
   110
    Method.insert_tac facts THEN'
54766
6ac273f176cd store alternative proof methods in Isar data structure
blanchet
parents: 54765
diff changeset
   111
    (case meth of
54765
blanchet
parents: 54764
diff changeset
   112
      Simp_Method => Simplifier.asm_full_simp_tac ctxt
blanchet
parents: 54764
diff changeset
   113
    | Auto_Method => K (Clasimp.auto_tac ctxt)
blanchet
parents: 54764
diff changeset
   114
    | Fastforce_Method => Clasimp.fast_force_tac ctxt
blanchet
parents: 54764
diff changeset
   115
    | Force_Method => Clasimp.force_tac ctxt
blanchet
parents: 54764
diff changeset
   116
    | Arith_Method => Arith_Data.arith_tac ctxt
blanchet
parents: 54764
diff changeset
   117
    | Blast_Method => blast_tac ctxt
54764
1c9ef5c834e8 added 'meson' to the mix
blanchet
parents: 54763
diff changeset
   118
    | _ => raise Fail "Sledgehammer_Preplay: tac_of_method"))
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   119
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   120
(* main function for preplaying Isar steps; may throw exceptions *)
53761
4d34e267fba9 use configuration mechanism for low-level tracing
blanchet
parents: 53664
diff changeset
   121
fun preplay_raw _ _ _ _ _ (Let _) = zero_preplay_time
4d34e267fba9 use configuration mechanism for low-level tracing
blanchet
parents: 53664
diff changeset
   122
  | preplay_raw debug type_enc lam_trans ctxt timeout
54766
6ac273f176cd store alternative proof methods in Isar data structure
blanchet
parents: 54765
diff changeset
   123
      (step as Prove (_, xs, _, t, subproofs, (fact_names, meth :: _))) =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   124
    let
54763
blanchet
parents: 54761
diff changeset
   125
      val goal =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   126
        (case xs of
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   127
          [] => t
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   128
        | _ =>
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   129
          (* proof obligation: !!thesis. (!!x. A x ==> thesis) ==> thesis
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   130
             (cf. "~~/src/Pure/Isar/obtain.ML") *)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   131
          let
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   132
            val thesis = Term.Free ("thesis", HOLogic.boolT)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   133
            val thesis_prop = thesis |> HOLogic.mk_Trueprop
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   134
            val frees = map Term.Free xs
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   135
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   136
            (* !!x1..xn. t ==> thesis (xs = [x1, .., xn]) *)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   137
            val inner_prop = fold_rev Logic.all frees (Logic.mk_implies (t, thesis_prop))
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   138
          in
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   139
            (* !!thesis. (!!x1..xn. t ==> thesis) ==> thesis *)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   140
            Logic.all thesis (Logic.mk_implies (inner_prop, thesis_prop))
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   141
          end)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   142
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   143
      val facts = map (thm_of_proof ctxt) subproofs @ resolve_fact_names ctxt fact_names
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   144
54763
blanchet
parents: 54761
diff changeset
   145
      val ctxt' = ctxt
blanchet
parents: 54761
diff changeset
   146
        |> debug ? (Config.put Metis_Tactic.verbose true #> Config.put Lin_Arith.verbose true)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   147
        |> use_metis_new_skolem step ? Config.put Metis_Tactic.new_skolem true
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   148
54763
blanchet
parents: 54761
diff changeset
   149
      val prove = Goal.prove ctxt' [] [] goal
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   150
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   151
      fun tac {context = ctxt, ...} = HEADGOAL (tac_of_method meth type_enc lam_trans ctxt facts)
54763
blanchet
parents: 54761
diff changeset
   152
      fun run_tac () = prove tac handle ERROR msg => error ("Preplay error: " ^ msg)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   153
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   154
      val preplay_time = take_time timeout run_tac ()
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   155
    in
54763
blanchet
parents: 54761
diff changeset
   156
      (if Config.get ctxt trace then preplay_trace ctxt facts goal preplay_time else ();
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   157
       preplay_time)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   158
    end
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   159
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   160
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   161
(*** proof preplay interface ***)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   162
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   163
type preplay_interface =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   164
  {get_preplay_result: label -> preplay_result,
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   165
   set_preplay_result: label -> preplay_result -> unit,
54763
blanchet
parents: 54761
diff changeset
   166
   get_preplay_time: label -> bool * Time.time,
blanchet
parents: 54761
diff changeset
   167
   set_preplay_time: label -> bool * Time.time -> unit,
blanchet
parents: 54761
diff changeset
   168
   preplay_quietly: Time.time -> isar_step -> bool * Time.time,
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   169
   (* the returned flag signals a preplay failure *)
54763
blanchet
parents: 54761
diff changeset
   170
   overall_preplay_stats: isar_proof -> (bool * Time.time) * bool}
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   171
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   172
fun enrich_context_with_local_facts proof ctxt =
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   173
  let
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   174
    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
   175
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   176
    fun enrich_with_fact l t =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   177
      Proof_Context.put_thms false (string_of_label l, SOME [Skip_Proof.make_thm thy t])
52556
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
    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
   180
54700
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
   181
    fun enrich_with_proof (Proof (_, assms, isar_steps)) =
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   182
      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
   183
    and enrich_with_step (Let _) = I
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   184
      | enrich_with_step (Prove (_, _, l, t, subproofs, _)) =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   185
        enrich_with_fact l t #> fold enrich_with_proof subproofs
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   186
  in
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   187
    enrich_with_proof proof ctxt
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   188
  end
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   189
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   190
(* Given a proof, produces an imperative preplay interface with a shared table mapping from labels
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   191
   to preplay results. 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
   192
   calculation.
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   193
54763
blanchet
parents: 54761
diff changeset
   194
   Precondition: The proof must be labeled canonically; cf.
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   195
   "Slegehammer_Proof.relabel_proof_canonically". *)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   196
fun proof_preplay_interface debug ctxt type_enc lam_trans do_preplay preplay_timeout proof =
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   197
  if not do_preplay then
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   198
    (* the dont_preplay option pretends that everything works just fine *)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   199
    {get_preplay_result = K (Preplay_Success zero_preplay_time),
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   200
     set_preplay_result = K (K ()),
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   201
     get_preplay_time = K zero_preplay_time,
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   202
     set_preplay_time = K (K ()),
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   203
     preplay_quietly = K (K zero_preplay_time),
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   204
     overall_preplay_stats = K (zero_preplay_time, false)} : preplay_interface
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   205
  else
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   206
    let
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   207
      val ctxt = enrich_context_with_local_facts proof ctxt
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   208
52627
smolkas
parents: 52626
diff changeset
   209
      fun preplay quietly timeout step =
54763
blanchet
parents: 54761
diff changeset
   210
        Preplay_Success (preplay_raw debug type_enc lam_trans ctxt timeout step)
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   211
        handle exn =>
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   212
               if Exn.is_interrupt exn then
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   213
                 reraise exn
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   214
               else if not quietly andalso debug then
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   215
                 (warning ("Preplay failure:\n  " ^ @{make_string} step ^ "\n  " ^
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   216
                    @{make_string} exn);
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   217
                  Preplay_Failure exn)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   218
               else
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   219
                 Preplay_Failure exn
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   220
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   221
      (* 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
   222
      fun preplay_quietly timeout step =
54712
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   223
        (case preplay true timeout step of
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   224
          Preplay_Success preplay_time => preplay_time
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   225
        | Preplay_Failure _ => (true, timeout))
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   226
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   227
      val preplay_tab =
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   228
        let
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   229
          fun add_step_to_tab step tab =
54763
blanchet
parents: 54761
diff changeset
   230
            (case label_of_step step of
blanchet
parents: 54761
diff changeset
   231
               NONE => tab
blanchet
parents: 54761
diff changeset
   232
             | SOME l =>
blanchet
parents: 54761
diff changeset
   233
               Canonical_Lbl_Tab.update_new
blanchet
parents: 54761
diff changeset
   234
                 (l, (fn () => preplay false preplay_timeout step) |> Lazy.lazy) tab)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   235
            handle Canonical_Lbl_Tab.DUP _ => 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
   236
        in
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   237
          Canonical_Lbl_Tab.empty
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   238
          |> 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
   239
          |> Unsynchronized.ref
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   240
        end
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   241
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   242
      fun get_preplay_result l =
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   243
        Canonical_Lbl_Tab.lookup (!preplay_tab) l |> the |> Lazy.force
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   244
        handle Option.Option => 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
   245
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   246
      fun set_preplay_result l result =
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   247
        preplay_tab := Canonical_Lbl_Tab.update (l, Lazy.value result) (!preplay_tab)
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   248
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   249
      fun get_preplay_time l =
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   250
        (case get_preplay_result l of
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   251
          Preplay_Success preplay_time => preplay_time
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   252
        | Preplay_Failure _ => some_preplay_time)
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   253
54763
blanchet
parents: 54761
diff changeset
   254
      fun set_preplay_time l = set_preplay_result l o Preplay_Success
blanchet
parents: 54761
diff changeset
   255
blanchet
parents: 54761
diff changeset
   256
      val result_of_step =
blanchet
parents: 54761
diff changeset
   257
        try (label_of_step #> the #> get_preplay_result)
blanchet
parents: 54761
diff changeset
   258
        #> the_default (Preplay_Success zero_preplay_time)
blanchet
parents: 54761
diff changeset
   259
blanchet
parents: 54761
diff changeset
   260
      fun do_step step =
blanchet
parents: 54761
diff changeset
   261
        (case result_of_step step of
blanchet
parents: 54761
diff changeset
   262
          Preplay_Success preplay_time => apfst (add_preplay_time preplay_time)
blanchet
parents: 54761
diff changeset
   263
        | Preplay_Failure _ => apsnd (K true))
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   264
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   265
      fun overall_preplay_stats (Proof (_, _, steps)) =
54763
blanchet
parents: 54761
diff changeset
   266
        fold_isar_steps do_step steps (zero_preplay_time, false)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   267
    in
54712
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   268
      {get_preplay_result = get_preplay_result,
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   269
       set_preplay_result = set_preplay_result,
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   270
       get_preplay_time = get_preplay_time,
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   271
       set_preplay_time = set_preplay_time,
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   272
       preplay_quietly = preplay_quietly,
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   273
       overall_preplay_stats = overall_preplay_stats}
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   274
    end
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   275
54504
blanchet
parents: 53761
diff changeset
   276
end;