src/HOL/Tools/Sledgehammer/sledgehammer_shrink.ML
author smolkas
Wed, 28 Nov 2012 12:22:17 +0100
changeset 50259 9c64a52ae499
child 50260 87ddf7eddfc9
permissions -rw-r--r--
put shrink in own structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     1
signature SLEDGEHAMMER_SHRINK =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     2
sig
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     3
  type isar_step = Sledgehammer_Isar_Reconstruct.isar_step
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     4
	val shrink_proof : 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     5
    bool -> Proof.context -> string -> string -> bool -> Time.time -> real
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     6
    -> isar_step list -> isar_step list * (bool * Time.time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     7
end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     8
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     9
structure Sledgehammer_Shrink (* : SLEDGEHAMMER_SHRINK *) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    10
struct
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    11
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    12
open Sledgehammer_Isar_Reconstruct
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    13
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    14
(* Parameters *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    15
val merge_timeout_slack = 1.2
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    16
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    17
(* Data structures, orders *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    18
val label_ord = prod_ord int_ord fast_string_ord o pairself swap
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    19
structure Label_Table = Table(
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    20
  type key = label
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    21
  val ord = label_ord)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    22
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    23
(* Timing *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    24
type ext_time = bool * Time.time
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    25
fun ext_time_add (b1, t1) (b2, t2) : ext_time = (b1 orelse b2, t1+t2)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    26
val no_time = (false, seconds 0.0)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    27
fun take_time timeout tac arg =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    28
  let val timing = Timing.start () in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    29
    (TimeLimit.timeLimit timeout tac arg;
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    30
     Timing.result timing |> #cpu |> SOME)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    31
    handle _ => NONE
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    32
  end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    33
fun sum_up_time timeout =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    34
  Vector.foldl
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    35
    ((fn (SOME t, (b, ts)) => (b, t+ts)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    36
       | (NONE, (_, ts)) => (true, ts+timeout)) o apfst Lazy.force)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    37
    no_time
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    38
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    39
(* clean vector interface *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    40
fun get i v = Vector.sub (v, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    41
fun replace x i v = Vector.update (v, i, x)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    42
fun update f i v = replace (get i v |> f) i v
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    43
fun v_fold_index f v s =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    44
  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
    45
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    46
(* Queue interface to table *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    47
fun pop tab key =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    48
  let val v = hd (Inttab.lookup_list tab key) in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    49
    (v, Inttab.remove_list (op =) (key, v) tab)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    50
  end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    51
fun pop_max tab = pop tab (the (Inttab.max_key tab))
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    52
fun add_list tab xs = fold (Inttab.insert_list (op =)) xs tab
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    53
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    54
(* Main function for shrinking proofs *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    55
fun shrink_proof debug ctxt type_enc lam_trans preplay preplay_timeout
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    56
                 isar_shrink proof =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    57
let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    58
  fun shrink_top_level top_level ctxt proof =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    59
  let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    60
    (* proof vector *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    61
    val proof_vect = proof |> map SOME |> Vector.fromList
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    62
    val n = metis_steps_top_level proof
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    63
    val n_target = Real.fromInt n / isar_shrink |> Real.round
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    64
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    65
    (* table for mapping from (top-level-)label to proof position *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    66
    fun update_table (i, Assume (label, _)) = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    67
        Label_Table.update_new (label, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    68
      | update_table (i, Prove (_, label, _, _)) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    69
        Label_Table.update_new (label, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    70
      | update_table _ = I
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    71
    val label_index_table = fold_index update_table proof Label_Table.empty
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    72
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    73
    (* proof references *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    74
    fun refs (Prove (_, _, _, By_Metis (lfs, _))) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    75
        maps (the_list o Label_Table.lookup label_index_table) lfs
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    76
      | refs (Prove (_, _, _, Case_Split (cases, (lfs, _)))) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    77
        maps (the_list o Label_Table.lookup label_index_table) lfs 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    78
          @ maps (maps refs) cases
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    79
      | refs _ = []
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    80
    val refed_by_vect =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    81
      Vector.tabulate (Vector.length proof_vect, (fn _ => []))
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    82
      |> fold_index (fn (i, step) => fold (update (cons i)) (refs step)) proof
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    83
      |> Vector.map rev (* after rev, indices are sorted in ascending order *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    84
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    85
    (* candidates for elimination, use table as priority queue (greedy
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    86
       algorithm) *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    87
    fun add_if_cand proof_vect (i, [j]) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    88
        (case (the (get i proof_vect), the (get j proof_vect)) of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    89
          (Prove (_, _, t, By_Metis _), Prove (_, _, _, By_Metis _)) =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    90
            cons (Term.size_of_term t, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    91
        | _ => I)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    92
      | add_if_cand _ _ = I
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    93
    val cand_tab = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    94
      v_fold_index (add_if_cand proof_vect) refed_by_vect []
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    95
      |> Inttab.make_list
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    96
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    97
    (* Metis Preplaying *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    98
    fun try_metis timeout (Prove (_, _, t, By_Metis fact_names)) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    99
      if not preplay then (fn () => SOME (seconds 0.0)) else
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   100
        let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   101
          val facts =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   102
            fact_names
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   103
            |>> map string_for_label 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   104
            |> op @
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   105
            |> map (the_single o thms_of_name ctxt) (* FIXME: maps (the o thms_of_name ctxt) *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   106
          val goal =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   107
            Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   108
          fun tac {context = ctxt, prems = _} =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   109
            Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   110
        in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   111
          take_time timeout (fn () => goal tac)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   112
        end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   113
      | try_metis _ _ = (fn () => SOME (seconds 0.0) )
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   114
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   115
    (* Lazy metis time vector, cache *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   116
    val metis_time =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   117
      Vector.map (Lazy.lazy o try_metis preplay_timeout o the) proof_vect
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   118
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   119
    (* Merging *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   120
    fun merge (Prove (qs1, label1, _, By_Metis (lfs1, gfs1)))
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   121
              (Prove (qs2, label2 , t, By_Metis (lfs2, gfs2))) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   122
      let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   123
        val qs = inter (op =) qs1 qs2 (* FIXME: Is this correct? *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   124
          |> member (op =) (union (op =) qs1 qs2) Ultimately ? cons Ultimately
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   125
          |> member (op =) qs2 Show ? cons Show
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   126
        val ls = remove (op =) label1 lfs2 |> union (op =) lfs1
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   127
        val ss = union (op =) gfs1 gfs2
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   128
      in Prove (qs, label2, t, By_Metis (ls, ss)) end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   129
    fun try_merge metis_time (s1, i) (s2, j) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   130
      (case get i metis_time |> Lazy.force of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   131
        NONE => (NONE, metis_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   132
      | SOME t1 =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   133
        (case get j metis_time |> Lazy.force of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   134
          NONE => (NONE, metis_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   135
        | SOME t2 =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   136
          let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   137
            val s12 = merge s1 s2
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   138
            val timeout =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   139
              Time.+ (t1, t2) |> Time.toReal |> curry Real.* merge_timeout_slack
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   140
                      |> Time.fromReal
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   141
          in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   142
            case try_metis timeout s12 () of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   143
              NONE => (NONE, metis_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   144
            | some_t12 =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   145
              (SOME s12, metis_time
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   146
                         |> replace (seconds 0.0 |> SOME |> Lazy.value) i
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   147
                         |> replace (Lazy.value some_t12) j)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   148
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   149
          end))
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   150
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   151
    fun merge_steps metis_time proof_vect refed_by cand_tab n' =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   152
      if Inttab.is_empty cand_tab 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   153
        orelse n' <= n_target 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   154
        orelse (top_level andalso Vector.length proof_vect<3)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   155
      then
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   156
        (Vector.foldr
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   157
           (fn (NONE, proof) => proof | (SOME s, proof) => s :: proof)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   158
           [] proof_vect,
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   159
         sum_up_time preplay_timeout metis_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   160
      else
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   161
        let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   162
          val (i, cand_tab) = pop_max cand_tab
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   163
          val j = get i refed_by |> the_single
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   164
          val s1 = get i proof_vect |> the
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   165
          val s2 = get j proof_vect |> the
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   166
        in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   167
          case try_merge metis_time (s1, i) (s2, j) of
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   168
            (NONE, metis_time) =>
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   169
            merge_steps metis_time proof_vect refed_by cand_tab n'
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   170
          | (s, metis_time) => 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   171
          let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   172
            val refs = refs s1
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   173
            val refed_by = refed_by |> fold
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   174
              (update (Ord_List.remove int_ord i #> Ord_List.insert int_ord j)) refs
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   175
            val new_candidates =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   176
              fold (add_if_cand proof_vect)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   177
                (map (fn i => (i, get i refed_by)) refs) []
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   178
            val cand_tab = add_list cand_tab new_candidates
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   179
            val proof_vect = proof_vect |> replace NONE i |> replace s j
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   180
          in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   181
            merge_steps metis_time proof_vect refed_by cand_tab (n' - 1)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   182
          end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   183
        end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   184
  in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   185
    merge_steps metis_time proof_vect refed_by_vect cand_tab n
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   186
  end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   187
  
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   188
  fun shrink_proof' top_level ctxt proof = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   189
    let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   190
      (* Enrich context with top-level facts *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   191
      val thy = Proof_Context.theory_of ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   192
      fun enrich_ctxt (Assume (label, t)) ctxt = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   193
          Proof_Context.put_thms false
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   194
            (string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   195
        | enrich_ctxt (Prove (_, label, t, _)) ctxt =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   196
          Proof_Context.put_thms false
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   197
            (string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   198
        | enrich_ctxt _ ctxt = ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   199
      val rich_ctxt = fold enrich_ctxt proof ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   200
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   201
      (* Shrink case_splits and top-levl *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   202
      val ((proof, top_level_time), lower_level_time) = 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   203
        proof |> shrink_case_splits rich_ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   204
              |>> shrink_top_level top_level rich_ctxt
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   205
    in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   206
      (proof, ext_time_add lower_level_time top_level_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   207
    end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   208
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   209
  and shrink_case_splits ctxt proof =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   210
    let
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   211
      fun shrink_and_collect_time shrink candidates =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   212
            let fun f_m cand time = shrink cand ||> ext_time_add time
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   213
            in fold_map f_m candidates no_time end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   214
      val shrink_case_split = shrink_and_collect_time (shrink_proof' false ctxt)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   215
      fun shrink (Prove (qs, lbl, t, Case_Split (cases, facts))) =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   216
          let val (cases, time) = shrink_case_split cases
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   217
          in (Prove (qs, lbl, t, Case_Split (cases, facts)), time) end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   218
        | shrink step = (step, no_time)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   219
    in 
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   220
      shrink_and_collect_time shrink proof
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   221
    end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   222
in
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   223
  shrink_proof' true ctxt proof
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   224
end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   225
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   226
end