src/HOL/Tools/Sledgehammer/sledgehammer_shrink.ML
author smolkas
Thu, 03 Jan 2013 15:05:48 +0100
changeset 50711 eb67eec63a8b
parent 50678 027c09d7f6ec
child 50779 6f571f6797bd
permissions -rw-r--r--
tuned
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
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    11
  val shrink_proof :
50557
31313171deb5 thread no timeout properly
blanchet
parents: 50278
diff changeset
    12
    bool -> Proof.context -> string -> string -> bool -> Time.time option
31313171deb5 thread no timeout properly
blanchet
parents: 50278
diff changeset
    13
    -> real -> 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))
50557
31313171deb5 thread no timeout properly
blanchet
parents: 50278
diff changeset
    49
val no_time = (false, Time.zeroTime)
50271
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
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    62
    (* 60 seconds seems like a good interpreation of "no timeout" *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    63
    val preplay_timeout = preplay_timeout |> the_default (seconds 60.0)
50271
2be84eaf7ebb deal with the case that metis does not time out, but fails instead
smolkas
parents: 50270
diff changeset
    64
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    65
    (* handle metis preplay fail *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    66
    local
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    67
      open Unsynchronized
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    68
      val metis_fail = ref false
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    69
    in
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    70
      fun handle_metis_fail try_metis () =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    71
        try_metis () handle _ => (metis_fail := true; SOME Time.zeroTime)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    72
      fun get_time lazy_time =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    73
        if !metis_fail then SOME Time.zeroTime else Lazy.force lazy_time
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    74
      val metis_fail = fn () => !metis_fail
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    75
    end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    76
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    77
    (* Shrink top level proof - do not shrink case splits *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    78
    fun shrink_top_level on_top_level ctxt proof =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    79
    let
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    80
      (* proof vector *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    81
      val proof_vect = proof |> map SOME |> Vector.fromList
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    82
      val n = Vector.length proof_vect
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    83
      val n_metis = metis_steps_top_level proof
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    84
      val target_n_metis = Real.fromInt n_metis / isar_shrink |> Real.round
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    85
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    86
      (* table for mapping from (top-level-)label to proof position *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    87
      fun update_table (i, Assume (l, _)) = Label_Table.update_new (l, i)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    88
        | update_table (i, Obtain (_, _, l, _, _)) = Label_Table.update_new (l, i)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    89
        | update_table (i, Prove (_, l, _, _)) = Label_Table.update_new (l, i)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    90
        | update_table _ = I
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    91
      val label_index_table = fold_index update_table proof Label_Table.empty
50711
smolkas
parents: 50678
diff changeset
    92
      val lookup_indices = map_filter (Label_Table.lookup label_index_table)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    93
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    94
      (* proof references *)
50711
smolkas
parents: 50678
diff changeset
    95
      fun refs (Obtain (_, _, _, _, By_Metis (lfs, _))) = lookup_indices lfs
smolkas
parents: 50678
diff changeset
    96
        | refs (Prove (_, _, _, By_Metis (lfs, _))) = lookup_indices lfs
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    97
        | refs (Prove (_, _, _, Case_Split (cases, (lfs, _)))) =
50711
smolkas
parents: 50678
diff changeset
    98
          lookup_indices lfs @ maps (maps refs) cases
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    99
        | refs _ = []
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   100
      val refed_by_vect =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   101
        Vector.tabulate (n, (fn _ => []))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   102
        |> fold_index (fn (i, step) => fold (update (cons i)) (refs step)) proof
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   103
        |> Vector.map rev (* after rev, indices are sorted in ascending order *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   104
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   105
      (* candidates for elimination, use table as priority queue (greedy
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   106
         algorithm) *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   107
      (* TODO: consider adding "Obtain" cases *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   108
      fun add_if_cand proof_vect (i, [j]) =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   109
          (case (the (get i proof_vect), the (get j proof_vect)) of
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   110
            (Prove (_, _, t, By_Metis _), Prove (_, _, _, By_Metis _)) =>
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   111
            cons (Term.size_of_term t, i)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   112
          | _ => I)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   113
        | add_if_cand _ _ = I
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   114
      val cand_tab =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   115
        v_fold_index (add_if_cand proof_vect) refed_by_vect []
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   116
        |> Inttab.make_list
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   117
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   118
      (* Metis Preplaying *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   119
      fun resolve_fact_names names =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   120
        names
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   121
          |>> map string_for_label
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   122
          |> op @
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   123
          |> maps (thms_of_name ctxt)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   124
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   125
      (* TODO: add "Obtain" case *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   126
      fun try_metis timeout (succedent, Prove (_, _, t, byline)) =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   127
        if not preplay then K (SOME Time.zeroTime) else
50678
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   128
        let
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   129
          val make_thm = Skip_Proof.make_thm (Proof_Context.theory_of ctxt)
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   130
          val facts =
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   131
            (case byline of
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   132
              By_Metis fact_names => resolve_fact_names fact_names
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   133
            | Case_Split (cases, fact_names) =>
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   134
              resolve_fact_names fact_names
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   135
                @ (case the succedent of
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   136
                    Assume (_, t) => make_thm t
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   137
                  | Obtain (_, _, _, t, _) => make_thm t
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   138
                  | Prove (_, _, t, _) => make_thm t
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   139
                  | _ => error "Internal error: unexpected succedent of case split")
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   140
                :: map (hd #> (fn Assume (_, a) => Logic.mk_implies (a, t)
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   141
                                | _ => error "Internal error: malformed case split")
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   142
                           #> make_thm)
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   143
                     cases)
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   144
          val goal =
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   145
            Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   146
          fun tac {context = ctxt, prems = _} =
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   147
            Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   148
        in
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   149
          take_time timeout (fn () => goal tac)
027c09d7f6ec removed duplicate code
smolkas
parents: 50672
diff changeset
   150
        end
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   151
        | try_metis _ _  = K (SOME Time.zeroTime)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   152
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   153
      val try_metis_quietly = the_default NONE oo try oo try_metis
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   154
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   155
      (* cache metis preplay times in lazy time vector *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   156
      val metis_time =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   157
        v_map_index
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   158
          (Lazy.lazy o handle_metis_fail o try_metis preplay_timeout
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   159
            o apfst (fn i => try (the o get (i-1)) proof_vect) o apsnd the)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   160
          proof_vect
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   161
      fun sum_up_time lazy_time_vector =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   162
        Vector.foldl
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   163
          ((fn (SOME t, (b, ts)) => (b, Time.+(t, ts))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   164
             | (NONE, (_, ts)) => (true, Time.+(ts, preplay_timeout)))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   165
            o apfst get_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   166
          no_time lazy_time_vector
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   167
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   168
      (* Merging *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   169
      (* TODO: consider adding "Obtain" cases *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   170
      fun merge (Prove (_, label1, _, By_Metis (lfs1, gfs1)))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   171
                (Prove (qs2, label2, t, By_Metis (lfs2, gfs2))) =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   172
          let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   173
            val lfs = remove (op =) label1 lfs2 |> union (op =) lfs1
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   174
            val gfs = union (op =) gfs1 gfs2
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   175
          in Prove (qs2, label2, t, By_Metis (lfs, gfs)) end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   176
        | merge _ _ = error "Internal error: Unmergeable Isar steps"
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   177
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   178
      fun try_merge metis_time (s1, i) (s2, j) =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   179
        (case get i metis_time |> Lazy.force of
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   180
          NONE => (NONE, metis_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   181
        | SOME t1 =>
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   182
          (case get j metis_time |> Lazy.force of
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   183
            NONE => (NONE, metis_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   184
          | SOME t2 =>
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   185
            let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   186
              val s12 = merge s1 s2
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   187
              val timeout = time_mult merge_timeout_slack (Time.+(t1, t2))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   188
            in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   189
              case try_metis_quietly timeout (NONE, s12) () of
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   190
                NONE => (NONE, metis_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   191
              | some_t12 =>
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   192
                (SOME s12, metis_time
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   193
                           |> replace (Time.zeroTime |> SOME |> Lazy.value) i
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   194
                           |> replace (Lazy.value some_t12) j)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   195
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   196
            end))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   197
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   198
      fun merge_steps metis_time proof_vect refed_by cand_tab n' n_metis' =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   199
        if Inttab.is_empty cand_tab
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   200
          orelse n_metis' <= target_n_metis
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   201
          orelse (on_top_level andalso n'<3)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   202
        then
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   203
          (Vector.foldr
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   204
             (fn (NONE, proof) => proof | (SOME s, proof) => s :: proof)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   205
             [] proof_vect,
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   206
           sum_up_time metis_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   207
        else
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   208
          let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   209
            val (i, cand_tab) = pop_max cand_tab
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   210
            val j = get i refed_by |> the_single
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   211
            val s1 = get i proof_vect |> the
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   212
            val s2 = get j proof_vect |> the
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   213
          in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   214
            case try_merge metis_time (s1, i) (s2, j) of
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   215
              (NONE, metis_time) =>
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   216
              merge_steps metis_time proof_vect refed_by cand_tab n' n_metis'
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   217
            | (s, metis_time) =>
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   218
            let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   219
              val refs = refs s1
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   220
              val refed_by = refed_by |> fold
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   221
                (update (Ord_List.remove int_ord i #> Ord_List.insert int_ord j)) refs
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   222
              val new_candidates =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   223
                fold (add_if_cand proof_vect)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   224
                  (map (fn i => (i, get i refed_by)) refs) []
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   225
              val cand_tab = add_list cand_tab new_candidates
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   226
              val proof_vect = proof_vect |> replace NONE i |> replace s j
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   227
            in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   228
              merge_steps metis_time proof_vect refed_by cand_tab (n' - 1)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   229
                          (n_metis' - 1)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   230
            end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   231
          end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   232
    in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   233
      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
   234
    end
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   235
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   236
    fun do_proof on_top_level ctxt proof =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   237
      let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   238
        (* Enrich context with top-level facts *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   239
        val thy = Proof_Context.theory_of ctxt
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   240
        (* TODO: add Skolem variables to context? *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   241
        fun enrich_with_fact l t =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   242
          Proof_Context.put_thms false
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   243
            (string_for_label l, SOME [Skip_Proof.make_thm thy t])
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   244
        fun enrich_with_step (Assume (l, t)) = enrich_with_fact l t
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   245
          | enrich_with_step (Obtain (_, _, l, t, _)) = enrich_with_fact l t
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   246
          | enrich_with_step (Prove (_, l, t, _)) = enrich_with_fact l t
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   247
          | enrich_with_step _ = I
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   248
        val rich_ctxt = fold enrich_with_step proof ctxt
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   249
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   250
        (* Shrink case_splits and top-levl *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   251
        val ((proof, top_level_time), lower_level_time) =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   252
          proof |> do_case_splits rich_ctxt
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   253
                |>> shrink_top_level on_top_level rich_ctxt
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   254
      in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   255
        (proof, ext_time_add lower_level_time top_level_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   256
      end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   257
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   258
    and do_case_splits ctxt proof =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   259
      let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   260
        fun shrink_each_and_collect_time shrink candidates =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   261
          let fun f_m cand time = shrink cand ||> ext_time_add time
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   262
          in fold_map f_m candidates no_time end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   263
        val shrink_case_split =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   264
          shrink_each_and_collect_time (do_proof false ctxt)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   265
        fun shrink (Prove (qs, l, t, Case_Split (cases, facts))) =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   266
            let val (cases, time) = shrink_case_split cases
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   267
            in (Prove (qs, l, t, Case_Split (cases, facts)), time) end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   268
          | shrink step = (step, no_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   269
      in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   270
        shrink_each_and_collect_time shrink proof
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   271
      end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   272
  in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   273
    do_proof true ctxt proof
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   274
    |> apsnd (pair (metis_fail ()))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   275
  end
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   276
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   277
end