src/HOL/Tools/Sledgehammer/sledgehammer_shrink.ML
author smolkas
Wed, 28 Nov 2012 12:25:43 +0100
changeset 50278 05f8ec128e83
parent 50275 66cdf5c9b6c7
child 50557 31313171deb5
permissions -rw-r--r--
improved readability
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50263
0b430064296a added comments to new source files
smolkas
parents: 50261
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_shrink.ML
0b430064296a added comments to new source files
smolkas
parents: 50261
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
0b430064296a added comments to new source files
smolkas
parents: 50261
diff changeset
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
0b430064296a added comments to new source files
smolkas
parents: 50261
diff changeset
     4
50265
9eafa567e061 made use of sledgehammer_util
smolkas
parents: 50264
diff changeset
     5
Shrinking and preplaying of reconstructed isar proofs.
50263
0b430064296a added comments to new source files
smolkas
parents: 50261
diff changeset
     6
*)
0b430064296a added comments to new source files
smolkas
parents: 50261
diff changeset
     7
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     8
signature SLEDGEHAMMER_SHRINK =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     9
sig
50264
a9ec48b98734 renamed sledgehammer_isar_reconstruct to sledgehammer_proof
smolkas
parents: 50263
diff changeset
    10
  type isar_step = Sledgehammer_Proof.isar_step
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    11
	val shrink_proof : 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    12
    bool -> Proof.context -> string -> string -> bool -> Time.time -> real
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    13
    -> isar_step list -> isar_step list * (bool * (bool * Time.time))
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    14
end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    15
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
    16
structure Sledgehammer_Shrink : SLEDGEHAMMER_SHRINK =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    17
struct
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    18
50265
9eafa567e061 made use of sledgehammer_util
smolkas
parents: 50264
diff changeset
    19
open Sledgehammer_Util
50264
a9ec48b98734 renamed sledgehammer_isar_reconstruct to sledgehammer_proof
smolkas
parents: 50263
diff changeset
    20
open Sledgehammer_Proof
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    21
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    22
(* Parameters *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    23
val merge_timeout_slack = 1.2
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    24
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    25
(* Data structures, orders *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    26
val label_ord = prod_ord int_ord fast_string_ord o pairself swap
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    27
structure Label_Table = Table(
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    28
  type key = label
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    29
  val ord = label_ord)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    30
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    31
(* clean vector interface *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    32
fun get i v = Vector.sub (v, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    33
fun replace x i v = Vector.update (v, i, x)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    34
fun update f i v = replace (get i v |> f) i v
50273
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
    35
fun v_map_index f v = Vector.foldr (op::) nil v |> map_index f |> Vector.fromList
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    36
fun v_fold_index f v s =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    37
  Vector.foldl (fn (x, (i, s)) => (i+1, f (i, x) s)) (0, s) v |> snd
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    38
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    39
(* Queue interface to table *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    40
fun pop tab key =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    41
  let val v = hd (Inttab.lookup_list tab key) in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    42
    (v, Inttab.remove_list (op =) (key, v) tab)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    43
  end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    44
fun pop_max tab = pop tab (the (Inttab.max_key tab))
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    45
fun add_list tab xs = fold (Inttab.insert_list (op =)) xs tab
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    46
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    47
(* Timing *)
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    48
fun ext_time_add (b1, t1) (b2, t2) = (b1 orelse b2, Time.+(t1,t2))
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    49
val no_time = (false, seconds 0.0)
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    50
fun take_time timeout tac arg =
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    51
  let val timing = Timing.start () in
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    52
    (TimeLimit.timeLimit timeout tac arg;
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    53
     Timing.result timing |> #cpu |> SOME)
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    54
    handle TimeLimit.TimeOut => NONE
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    55
  end
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    56
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    57
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    58
(* Main function for shrinking proofs *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    59
fun shrink_proof debug ctxt type_enc lam_trans preplay preplay_timeout
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    60
                 isar_shrink proof =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    61
let
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    62
  (* handle metis preplay fail *)
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    63
  local
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    64
    open Unsynchronized
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    65
    val metis_fail = ref false
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    66
  in
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    67
    fun handle_metis_fail try_metis () =
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    68
      try_metis () handle _ => (metis_fail := true ; SOME (seconds 0.0))
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    69
    fun get_time lazy_time = 
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    70
      if !metis_fail then SOME (seconds 0.0) else Lazy.force lazy_time
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    71
    val metis_fail = fn () => !metis_fail
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    72
  end
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    73
  
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    74
  (* Shrink top level proof - do not shrink case splits *)
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
    75
  fun shrink_top_level on_top_level ctxt proof =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    76
  let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    77
    (* proof vector *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    78
    val proof_vect = proof |> map SOME |> Vector.fromList
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
    79
    val n = Vector.length proof_vect
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
    80
    val n_metis = metis_steps_top_level proof
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
    81
    val target_n_metis = Real.fromInt n_metis / isar_shrink |> Real.round
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    82
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    83
    (* table for mapping from (top-level-)label to proof position *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    84
    fun update_table (i, Assume (label, _)) = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    85
        Label_Table.update_new (label, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    86
      | update_table (i, Prove (_, label, _, _)) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    87
        Label_Table.update_new (label, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    88
      | update_table _ = I
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    89
    val label_index_table = fold_index update_table proof Label_Table.empty
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    90
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    91
    (* proof references *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    92
    fun refs (Prove (_, _, _, By_Metis (lfs, _))) =
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
    93
        map_filter (Label_Table.lookup label_index_table) lfs
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    94
      | refs (Prove (_, _, _, Case_Split (cases, (lfs, _)))) =
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
    95
        map_filter (Label_Table.lookup label_index_table) lfs
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    96
          @ maps (maps refs) cases
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    97
      | refs _ = []
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    98
    val refed_by_vect =
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
    99
      Vector.tabulate (n, (fn _ => []))
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   100
      |> fold_index (fn (i, step) => fold (update (cons i)) (refs step)) proof
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   101
      |> Vector.map rev (* after rev, indices are sorted in ascending order *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   102
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   103
    (* candidates for elimination, use table as priority queue (greedy
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   104
       algorithm) *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   105
    fun add_if_cand proof_vect (i, [j]) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   106
        (case (the (get i proof_vect), the (get j proof_vect)) of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   107
          (Prove (_, _, t, By_Metis _), Prove (_, _, _, By_Metis _)) =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   108
            cons (Term.size_of_term t, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   109
        | _ => I)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   110
      | add_if_cand _ _ = I
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   111
    val cand_tab = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   112
      v_fold_index (add_if_cand proof_vect) refed_by_vect []
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   113
      |> Inttab.make_list
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   114
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   115
    (* Metis Preplaying *)
50273
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   116
    fun resolve_fact_names names =
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   117
      names
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   118
        |>> map string_for_label 
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   119
        |> op @
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   120
        |> maps (thms_of_name ctxt)
50275
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   121
50273
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   122
    fun try_metis timeout (succedent, Prove (_, _, t, byline)) =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   123
      if not preplay then (fn () => SOME (seconds 0.0)) else
50273
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   124
      (case byline of
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   125
        By_Metis fact_names =>
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   126
          let
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   127
            val facts = resolve_fact_names fact_names
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   128
            val goal =
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   129
              Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   130
            fun tac {context = ctxt, prems = _} =
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   131
              Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   132
          in
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   133
            take_time timeout (fn () => goal tac)
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   134
          end
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   135
      | Case_Split (cases, fact_names) =>
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   136
          let
50275
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   137
            val facts = 
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   138
              resolve_fact_names fact_names
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   139
                @ (case the succedent of
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   140
                    Prove (_, _, t, _) => 
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   141
                      Skip_Proof.make_thm (Proof_Context.theory_of ctxt) t
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   142
                  | Assume (_, t) =>
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   143
                      Skip_Proof.make_thm (Proof_Context.theory_of ctxt) t
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   144
                  | _ => error "Internal error: unexpected succedent of case split")
50278
05f8ec128e83 improved readability
smolkas
parents: 50275
diff changeset
   145
                ::  map 
05f8ec128e83 improved readability
smolkas
parents: 50275
diff changeset
   146
                      (hd #> (fn Assume (_, a) => Logic.mk_implies(a, t)
05f8ec128e83 improved readability
smolkas
parents: 50275
diff changeset
   147
                               | _ => error "Internal error: malformed case split") 
05f8ec128e83 improved readability
smolkas
parents: 50275
diff changeset
   148
                          #> Skip_Proof.make_thm (Proof_Context.theory_of ctxt)) 
50273
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   149
                      cases
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   150
            val goal =
50275
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   151
              Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t
50273
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   152
            fun tac {context = ctxt, prems = _} =
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   153
              Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   154
          in
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   155
            take_time timeout (fn () => goal tac)
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   156
          end)
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   157
      | try_metis _ _  = (fn () => SOME (seconds 0.0) )
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   158
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   159
    val try_metis_quietly = the_default NONE oo try oo try_metis
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   160
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   161
    (* cache metis preplay times in lazy time vector *)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   162
    val metis_time =
50274
2f6035e858b6 fixed preplaying of case splits; incorperated new name of structure: Isabelle_Markup -> Markup
smolkas
parents: 50273
diff changeset
   163
      v_map_index
50273
f066768743c7 preplay case splits
smolkas
parents: 50271
diff changeset
   164
        (Lazy.lazy o handle_metis_fail o try_metis preplay_timeout 
50275
66cdf5c9b6c7 fixed case split preplaying
smolkas
parents: 50274
diff changeset
   165
          o apfst (fn i => try (the o get (i-1)) proof_vect) o apsnd the)
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   166
        proof_vect
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   167
    fun sum_up_time lazy_time_vector =
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   168
      Vector.foldl
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   169
        ((fn (SOME t, (b, ts)) => (b, Time.+(t, ts))
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   170
           | (NONE, (_, ts)) => (true, Time.+(ts, preplay_timeout))) 
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   171
          o apfst get_time)
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   172
        no_time lazy_time_vector
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   173
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   174
    (* Merging *)
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
   175
    fun merge (Prove (_, label1, _, By_Metis (lfs1, gfs1)))
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   176
              (Prove (qs2, label2 , t, By_Metis (lfs2, gfs2))) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   177
      let
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   178
        val lfs = remove (op =) label1 lfs2 |> union (op =) lfs1
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   179
        val gfs = union (op =) gfs1 gfs2
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   180
      in Prove (qs2, label2, t, By_Metis (lfs, gfs)) end
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   181
      | merge _ _ = error "Internal error: Tring to merge unmergable isar steps"
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   182
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   183
    fun try_merge metis_time (s1, i) (s2, j) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   184
      (case get i metis_time |> Lazy.force of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   185
        NONE => (NONE, metis_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   186
      | SOME t1 =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   187
        (case get j metis_time |> Lazy.force of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   188
          NONE => (NONE, metis_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   189
        | SOME t2 =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   190
          let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   191
            val s12 = merge s1 s2
50270
64d5767ea9b3 reapplied changes to make SML/NJ happy
smolkas
parents: 50269
diff changeset
   192
            val timeout = time_mult merge_timeout_slack (Time.+(t1, t2))
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   193
          in
50274
2f6035e858b6 fixed preplaying of case splits; incorperated new name of structure: Isabelle_Markup -> Markup
smolkas
parents: 50273
diff changeset
   194
            case try_metis_quietly timeout (NONE, s12) () of
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   195
              NONE => (NONE, metis_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   196
            | some_t12 =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   197
              (SOME s12, metis_time
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   198
                         |> replace (seconds 0.0 |> SOME |> Lazy.value) i
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   199
                         |> replace (Lazy.value some_t12) j)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   200
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   201
          end))
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   202
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
   203
    fun merge_steps metis_time proof_vect refed_by cand_tab n' n_metis' =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   204
      if Inttab.is_empty cand_tab 
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
   205
        orelse n_metis' <= target_n_metis 
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   206
        orelse (on_top_level andalso n'<3)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   207
      then
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   208
        (Vector.foldr
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   209
           (fn (NONE, proof) => proof | (SOME s, proof) => s :: proof)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   210
           [] proof_vect,
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   211
         sum_up_time metis_time)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   212
      else
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   213
        let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   214
          val (i, cand_tab) = pop_max cand_tab
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   215
          val j = get i refed_by |> the_single
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   216
          val s1 = get i proof_vect |> the
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   217
          val s2 = get j proof_vect |> the
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   218
        in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   219
          case try_merge metis_time (s1, i) (s2, j) of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   220
            (NONE, metis_time) =>
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
   221
            merge_steps metis_time proof_vect refed_by cand_tab n' n_metis'
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   222
          | (s, metis_time) => 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   223
          let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   224
            val refs = refs s1
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   225
            val refed_by = refed_by |> fold
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   226
              (update (Ord_List.remove int_ord i #> Ord_List.insert int_ord j)) refs
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   227
            val new_candidates =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   228
              fold (add_if_cand proof_vect)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   229
                (map (fn i => (i, get i refed_by)) refs) []
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   230
            val cand_tab = add_list cand_tab new_candidates
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   231
            val proof_vect = proof_vect |> replace NONE i |> replace s j
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   232
          in
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
   233
            merge_steps metis_time proof_vect refed_by cand_tab (n' - 1) (n_metis' - 1)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   234
          end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   235
        end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   236
  in
50260
87ddf7eddfc9 simplified isar_qualifiers and qs merging
smolkas
parents: 50259
diff changeset
   237
    merge_steps metis_time proof_vect refed_by_vect cand_tab n n_metis
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   238
  end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   239
  
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   240
  fun shrink_proof' on_top_level ctxt proof = 
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   241
    let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   242
      (* Enrich context with top-level facts *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   243
      val thy = Proof_Context.theory_of ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   244
      fun enrich_ctxt (Assume (label, t)) ctxt = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   245
          Proof_Context.put_thms false
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   246
            (string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   247
        | enrich_ctxt (Prove (_, label, t, _)) ctxt =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   248
          Proof_Context.put_thms false
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   249
            (string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   250
        | enrich_ctxt _ ctxt = ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   251
      val rich_ctxt = fold enrich_ctxt proof ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   252
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   253
      (* Shrink case_splits and top-levl *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   254
      val ((proof, top_level_time), lower_level_time) = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   255
        proof |> shrink_case_splits rich_ctxt
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   256
              |>> shrink_top_level on_top_level rich_ctxt
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   257
    in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   258
      (proof, ext_time_add lower_level_time top_level_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   259
    end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   260
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   261
  and shrink_case_splits ctxt proof =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   262
    let
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   263
      fun shrink_each_and_collect_time shrink candidates =
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   264
        let fun f_m cand time = shrink cand ||> ext_time_add time
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   265
        in fold_map f_m candidates no_time end
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   266
      val shrink_case_split = shrink_each_and_collect_time (shrink_proof' false ctxt)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   267
      fun shrink (Prove (qs, lbl, t, Case_Split (cases, facts))) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   268
          let val (cases, time) = shrink_case_split cases
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   269
          in (Prove (qs, lbl, t, Case_Split (cases, facts)), time) end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   270
        | shrink step = (step, no_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   271
    in 
50269
20a01c3e8072 renaming, minor tweaks, added signature
smolkas
parents: 50267
diff changeset
   272
      shrink_each_and_collect_time shrink proof
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   273
    end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   274
in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   275
  shrink_proof' true ctxt proof
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
   276
  |> apsnd (pair (metis_fail () ) )
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   277
end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   278
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   279
end