src/HOL/Tools/Sledgehammer/sledgehammer_isar_preplay.ML
author blanchet
Sun, 02 Feb 2014 20:53:51 +0100
changeset 55243 66709d41601e
parent 55223 3c593bad6b31
child 55244 12e1a5d8ee48
permissions -rw-r--r--
reset timing information after changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar_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
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
     8
signature SLEDGEHAMMER_ISAR_PREPLAY =
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
     9
sig
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
    10
  type play_outcome = Sledgehammer_Reconstructor.play_outcome
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
    11
  type proof_method = Sledgehammer_Isar_Proof.proof_method
55212
blanchet
parents: 55202
diff changeset
    12
  type isar_step = Sledgehammer_Isar_Proof.isar_step
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
    13
  type isar_proof = Sledgehammer_Isar_Proof.isar_proof
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
    14
  type label = Sledgehammer_Isar_Proof.label
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    15
55212
blanchet
parents: 55202
diff changeset
    16
  val trace : bool Config.T
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    17
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
    18
  type isar_preplay_data =
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
    19
    {reset_preplay_outcomes: isar_step -> unit,
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
    20
     set_preplay_outcome: label -> proof_method -> play_outcome -> unit,
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
    21
     preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
    22
     preplay_quietly: Time.time -> isar_step -> play_outcome,
54831
blanchet
parents: 54828
diff changeset
    23
     overall_preplay_outcome: isar_proof -> play_outcome}
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    24
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
    25
  val preplay_data_of_isar_proof : bool -> Proof.context -> string -> string -> bool -> Time.time ->
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
    26
    isar_proof -> isar_preplay_data
54504
blanchet
parents: 53761
diff changeset
    27
end;
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    28
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
    29
structure Sledgehammer_Isar_Preplay : SLEDGEHAMMER_ISAR_PREPLAY =
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    30
struct
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    31
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    32
open Sledgehammer_Util
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
    33
open Sledgehammer_Reconstructor
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
    34
open Sledgehammer_Isar_Proof
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    35
54763
blanchet
parents: 54761
diff changeset
    36
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
    37
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    38
fun preplay_trace ctxt assmsp concl result =
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    39
  let
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    40
    val ctxt = ctxt |> Config.put show_markup true
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    41
    val assms = op @ assmsp
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
    42
    val time = "[" ^ string_of_play_outcome result ^ "]" |> Pretty.str
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    43
    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
    44
    val assms = assms
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    45
      |> 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
    46
      |> (fn [] => Pretty.str ""
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    47
           | [a] => a
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    48
           | assms => Pretty.enum ";" "\<lbrakk>" "\<rbrakk>" assms)  (* Syntax.pretty_term!? *)
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    49
    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
    50
    val trace_list = []
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    51
      |> cons concl
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    52
      |> 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
    53
      |> cons assms
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    54
      |> cons time
53761
4d34e267fba9 use configuration mechanism for low-level tracing
blanchet
parents: 53664
diff changeset
    55
    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
    56
  in
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    57
    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
    58
  end
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51876
diff changeset
    59
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    60
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
    61
  let val timing = Timing.start () in
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
    62
    (TimeLimit.timeLimit timeout tac arg; Played (#cpu (Timing.result timing)))
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
    63
    handle TimeLimit.TimeOut => Play_Timed_Out timeout
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    64
  end
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    65
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    66
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
    67
  (names
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    68
   |>> map string_of_label
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    69
   |> pairself (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
    70
  handle ERROR msg => error ("preplay error: " ^ msg)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    71
54700
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    72
fun thm_of_proof ctxt (Proof (fixed_frees, assms, steps)) =
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
    73
  let
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    74
    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
    75
54700
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    76
    val concl = 
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    77
      (case try List.last steps of
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    78
        SOME (Prove (_, [], _, t, _, _)) => t
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
    79
      | _ => 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
    80
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
    81
    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
    82
    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
    83
    val subst = map (`var_of_free #> swap #> apfst Free) fixed_frees
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
    84
  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
    85
    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
    86
    |> subst_free subst
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
    87
    |> Skip_Proof.make_thm thy
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
    88
  end
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51155
diff changeset
    89
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    90
fun tac_of_method meth type_enc lam_trans ctxt (local_facts, global_facts) =
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    91
  Method.insert_tac local_facts THEN'
54766
6ac273f176cd store alternative proof methods in Isar data structure
blanchet
parents: 54765
diff changeset
    92
  (case meth of
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    93
    Meson_Method => Meson.meson_tac ctxt global_facts
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    94
  | Metis_Method => Metis_Tactic.metis_tac [type_enc] lam_trans ctxt global_facts
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
    95
  | _ =>
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    96
    Method.insert_tac global_facts THEN'
54766
6ac273f176cd store alternative proof methods in Isar data structure
blanchet
parents: 54765
diff changeset
    97
    (case meth of
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
    98
      Simp_Method => Simplifier.asm_full_simp_tac ctxt
54838
16511f84913c reconstruct SPASS-Pirate steps of the form 'x ~= C x' (or more complicated)
blanchet
parents: 54831
diff changeset
    99
    | Simp_Size_Method =>
16511f84913c reconstruct SPASS-Pirate steps of the form 'x ~= C x' (or more complicated)
blanchet
parents: 54831
diff changeset
   100
      Simplifier.asm_full_simp_tac (Simplifier.add_simp @{thm size_ne_size_imp_ne} ctxt)
54765
blanchet
parents: 54764
diff changeset
   101
    | Auto_Method => K (Clasimp.auto_tac ctxt)
blanchet
parents: 54764
diff changeset
   102
    | Fastforce_Method => Clasimp.fast_force_tac ctxt
blanchet
parents: 54764
diff changeset
   103
    | Force_Method => Clasimp.force_tac ctxt
blanchet
parents: 54764
diff changeset
   104
    | Arith_Method => Arith_Data.arith_tac ctxt
blanchet
parents: 54764
diff changeset
   105
    | Blast_Method => blast_tac ctxt
55219
6fe9fd75f9d7 added 'algebra' to the mix
blanchet
parents: 55213
diff changeset
   106
    | Algebra_Method => Groebner.algebra_tac [] [] ctxt
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
   107
    | _ => raise Fail "Sledgehammer_Isar_Preplay: tac_of_method"))
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52575
diff changeset
   108
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   109
(* main function for preplaying Isar steps; may throw exceptions *)
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   110
fun preplay_raw debug type_enc lam_trans ctxt timeout meth
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   111
      (Prove (_, xs, _, t, subproofs, (fact_names, _))) =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   112
    let
54763
blanchet
parents: 54761
diff changeset
   113
      val goal =
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   114
        (case xs of
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   115
          [] => t
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   116
        | _ =>
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   117
          (* 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
   118
             (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
   119
          let
54813
blanchet
parents: 54799
diff changeset
   120
            (* FIXME: generate fresh name *)
blanchet
parents: 54799
diff changeset
   121
            val thesis = Free ("thesis", HOLogic.boolT)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   122
            val thesis_prop = thesis |> HOLogic.mk_Trueprop
54813
blanchet
parents: 54799
diff changeset
   123
            val frees = map Free xs
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   124
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   125
            (* !!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
   126
            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
   127
          in
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   128
            (* !!thesis. (!!x1..xn. t ==> thesis) ==> thesis *)
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   129
            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
   130
          end)
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   131
55194
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
   132
      val facts =
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
   133
        resolve_fact_names ctxt fact_names
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
   134
        |>> append (map (thm_of_proof ctxt) subproofs)
daa64e603e70 got rid of one of two Metis variants
blanchet
parents: 55193
diff changeset
   135
55170
cdb9435e3cae silenced reconstructors in Sledgehammer
blanchet
parents: 54838
diff changeset
   136
      val ctxt' = ctxt |> silence_reconstructors debug
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   137
54817
e1da23db35a9 made SML/NJ-friendlier (hopefully)
blanchet
parents: 54813
diff changeset
   138
      fun prove () =
e1da23db35a9 made SML/NJ-friendlier (hopefully)
blanchet
parents: 54813
diff changeset
   139
        Goal.prove ctxt' [] [] goal (fn {context = ctxt, ...} =>
e1da23db35a9 made SML/NJ-friendlier (hopefully)
blanchet
parents: 54813
diff changeset
   140
          HEADGOAL (tac_of_method meth type_enc lam_trans ctxt facts))
e1da23db35a9 made SML/NJ-friendlier (hopefully)
blanchet
parents: 54813
diff changeset
   141
        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
   142
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   143
      val play_outcome = take_time timeout prove ()
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   144
    in
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   145
      (if Config.get ctxt trace then preplay_trace ctxt facts goal play_outcome else ();
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   146
       play_outcome)
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   147
    end
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   148
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
   149
type isar_preplay_data =
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   150
  {reset_preplay_outcomes: isar_step -> unit,
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   151
   set_preplay_outcome: label -> proof_method -> play_outcome -> unit,
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   152
   preplay_outcome: label -> proof_method -> play_outcome Lazy.lazy,
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   153
   preplay_quietly: Time.time -> isar_step -> play_outcome,
54831
blanchet
parents: 54828
diff changeset
   154
   overall_preplay_outcome: isar_proof -> play_outcome}
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   155
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   156
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
   157
  let
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   158
    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
   159
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   160
    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
   161
      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
   162
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   163
    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
   164
54700
64177ce0a7bd adapted code for Z3 proof reconstruction
blanchet
parents: 54504
diff changeset
   165
    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
   166
      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
   167
    and enrich_with_step (Let _) = I
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   168
      | 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
   169
        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
   170
  in
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   171
    enrich_with_proof proof ctxt
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   172
  end
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   173
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   174
fun merge_preplay_outcomes _ Play_Failed = Play_Failed
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   175
  | merge_preplay_outcomes Play_Failed _ = Play_Failed
54831
blanchet
parents: 54828
diff changeset
   176
  | merge_preplay_outcomes (Play_Timed_Out t1) (Play_Timed_Out t2) =
blanchet
parents: 54828
diff changeset
   177
    Play_Timed_Out (Time.+ (t1, t2))
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   178
  | merge_preplay_outcomes (Played t1) (Play_Timed_Out t2) = Play_Timed_Out (Time.+ (t1, t2))
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   179
  | merge_preplay_outcomes (Play_Timed_Out t1) (Played t2) = Play_Timed_Out (Time.+ (t1, t2))
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   180
  | merge_preplay_outcomes (Played t1) (Played t2) = Played (Time.+ (t1, t2))
54827
14e2c7616209 more data structure refactoring
blanchet
parents: 54826
diff changeset
   181
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   182
(* 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
   183
   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
   184
   calculation.
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   185
54763
blanchet
parents: 54761
diff changeset
   186
   Precondition: The proof must be labeled canonically; cf.
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
   187
   "Slegehammer_Isar_Proof.relabel_isar_proof_canonically". *)
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
   188
fun preplay_data_of_isar_proof 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
   189
  if not do_preplay then
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   190
    (* the "dont_preplay" option pretends that everything works just fine *)
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   191
    {reset_preplay_outcomes = K (),
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   192
     set_preplay_outcome = K (K (K ())),
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   193
     preplay_outcome = K (K (Lazy.value (Played Time.zeroTime))),
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   194
     preplay_quietly = K (K (Played Time.zeroTime)),
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
   195
     overall_preplay_outcome = K (Played Time.zeroTime)} : isar_preplay_data
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   196
  else
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   197
    let
54761
0ef52f40d419 use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents: 54712
diff changeset
   198
      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
   199
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   200
      fun preplay quietly timeout meth step =
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   201
        preplay_raw debug type_enc lam_trans ctxt timeout meth step
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   202
        handle exn =>
55212
blanchet
parents: 55202
diff changeset
   203
          if Exn.is_interrupt exn then
blanchet
parents: 55202
diff changeset
   204
            reraise exn
blanchet
parents: 55202
diff changeset
   205
          else
blanchet
parents: 55202
diff changeset
   206
            (if not quietly andalso debug then
blanchet
parents: 55202
diff changeset
   207
               warning ("Preplay failure:\n " ^ @{make_string} step ^ "\n " ^ @{make_string} exn)
blanchet
parents: 55202
diff changeset
   208
             else
blanchet
parents: 55202
diff changeset
   209
               ();
blanchet
parents: 55202
diff changeset
   210
             Play_Failed)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   211
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   212
      (* preplay steps treating exceptions like timeouts *)
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   213
      fun preplay_quietly timeout (step as Prove (_, _, _, _, _, (_, (meth :: _) :: _))) =
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   214
          preplay true timeout meth step
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   215
        | preplay_quietly _ _ = Played Time.zeroTime
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   216
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   217
      val preplay_tab = Unsynchronized.ref Canonical_Label_Tab.empty
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   218
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   219
      fun reset_preplay_outcomes (step as Prove (_, _, l, _, _, (_, methss))) =
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   220
          preplay_tab := Canonical_Label_Tab.update (l, maps (map (fn meth =>
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   221
              (meth, Lazy.lazy (fn () => preplay false preplay_timeout meth step)))) methss)
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   222
            (!preplay_tab)
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   223
        | reset_preplay_outcomes _ = ()
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   224
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   225
      fun set_preplay_outcome l meth result =
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   226
        preplay_tab := Canonical_Label_Tab.map_entry l
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   227
          (AList.update (op =) (meth, Lazy.value result)) (!preplay_tab)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   228
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   229
      fun preplay_outcome l meth =
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   230
        (case Canonical_Label_Tab.lookup (!preplay_tab) l of
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   231
          SOME meths_outcomes =>
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   232
          (case AList.lookup (op =) meths_outcomes meth of
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   233
            SOME outcome => outcome
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   234
          | NONE => raise Fail "Sledgehammer_Isar_Preplay: missing method")
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   235
        | NONE => raise Fail "Sledgehammer_Isar_Preplay: missing label")
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   236
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   237
      val _ = fold_isar_steps (K o reset_preplay_outcomes) (steps_of_proof proof) ()
52626
79a4e7f8d758 cleaner preplay interface
smolkas
parents: 52613
diff changeset
   238
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   239
      fun result_of_step (Prove (_, _, l, _, _, (_, (meth :: _) :: _))) =
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   240
          Lazy.force (preplay_outcome l meth)
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55221
diff changeset
   241
        | result_of_step _ = Played Time.zeroTime
54763
blanchet
parents: 54761
diff changeset
   242
54831
blanchet
parents: 54828
diff changeset
   243
      fun overall_preplay_outcome (Proof (_, _, steps)) =
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   244
        fold_isar_steps (merge_preplay_outcomes o result_of_step) steps (Played Time.zeroTime)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   245
    in
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   246
      {reset_preplay_outcomes = reset_preplay_outcomes,
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54827
diff changeset
   247
       set_preplay_outcome = set_preplay_outcome,
55243
66709d41601e reset timing information after changes
blanchet
parents: 55223
diff changeset
   248
       preplay_outcome = preplay_outcome,
54712
cbebe2cf77f1 more work on Z3 Isar proofs
blanchet
parents: 54700
diff changeset
   249
       preplay_quietly = preplay_quietly,
54831
blanchet
parents: 54828
diff changeset
   250
       overall_preplay_outcome = overall_preplay_outcome}
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   251
    end
50923
141d8f575f6f move preplaying to own structure
smolkas
parents:
diff changeset
   252
54504
blanchet
parents: 53761
diff changeset
   253
end;