src/HOL/Tools/Sledgehammer/sledgehammer_compress.ML
author wenzelm
Sun, 07 Jul 2013 18:43:14 +0200
changeset 52550 09e52d4a850a
parent 52454 b528a975b256
child 52556 c8357085217c
permissions -rw-r--r--
discontinued obsolete "isabelle print";
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_compress.ML
50263
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
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
     5
Compression 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
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
     8
signature SLEDGEHAMMER_COMPRESS =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
     9
sig
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    10
  type isar_proof = Sledgehammer_Proof.isar_proof
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    11
  type preplay_time = Sledgehammer_Preplay.preplay_time
51741
blanchet
parents: 51260
diff changeset
    12
  val compress_and_preplay_proof :
50557
31313171deb5 thread no timeout properly
blanchet
parents: 50278
diff changeset
    13
    bool -> Proof.context -> string -> string -> bool -> Time.time option
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51877
diff changeset
    14
    -> bool -> real -> isar_proof -> isar_proof * (bool * preplay_time)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    15
end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    16
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
    17
structure Sledgehammer_Compress : SLEDGEHAMMER_COMPRESS =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    18
struct
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    19
50265
9eafa567e061 made use of sledgehammer_util
smolkas
parents: 50264
diff changeset
    20
open Sledgehammer_Util
50264
a9ec48b98734 renamed sledgehammer_isar_reconstruct to sledgehammer_proof
smolkas
parents: 50263
diff changeset
    21
open Sledgehammer_Proof
50923
141d8f575f6f move preplaying to own structure
smolkas
parents: 50908
diff changeset
    22
open Sledgehammer_Preplay
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    23
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    24
(* Parameters *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    25
val merge_timeout_slack = 1.2
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    26
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    27
(* Data structures, orders *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    28
val label_ord = prod_ord int_ord fast_string_ord o pairself swap
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    29
structure Label_Table = Table(
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    30
  type key = label
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    31
  val ord = label_ord)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    32
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    33
(* clean vector interface *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    34
fun get i v = Vector.sub (v, i)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    35
fun replace x i v = Vector.update (v, i, x)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    36
fun update f i v = replace (get i v |> f) i v
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    37
fun v_fold_index f v s =
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    38
  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
    39
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    40
(* Queue interface to table *)
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    41
fun pop tab key =
51876
724c67f59929 added informative error messages
smolkas
parents: 51741
diff changeset
    42
  (let val v = hd (Inttab.lookup_list tab key) in
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    43
    (v, Inttab.remove_list (op =) (key, v) tab)
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51879
diff changeset
    44
  end) handle List.Empty => raise Fail "sledgehammer_compress: pop"
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 51998
diff changeset
    45
fun pop_max tab = pop tab (fst (the (Inttab.max tab)))
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51879
diff changeset
    46
  handle Option.Option => raise Fail "sledgehammer_compress: pop_max"
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    47
fun add_list tab xs = fold (Inttab.insert_list (op =)) xs tab
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    48
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
    49
(* Main function for compresing proofs *)
51741
blanchet
parents: 51260
diff changeset
    50
fun compress_and_preplay_proof debug ctxt type_enc lam_trans preplay
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51877
diff changeset
    51
      preplay_timeout preplay_trace isar_compress proof =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    52
  let
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    53
    (* 60 seconds seems like a good interpreation of "no timeout" *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    54
    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
    55
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    56
    (* handle metis preplay fail *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    57
    local
51931
7c517c92d315 never open structure Unsynchronized (cf. "implementation" manual);
wenzelm
parents: 51930
diff changeset
    58
      val metis_fail = Unsynchronized.ref false
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    59
    in
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    60
      fun handle_metis_fail try_metis () =
50901
wenzelm
parents: 50786
diff changeset
    61
        try_metis () handle exn =>
wenzelm
parents: 50786
diff changeset
    62
          (if Exn.is_interrupt exn orelse debug then reraise exn
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    63
           else metis_fail := true; some_preplay_time)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    64
      fun get_time lazy_time =
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    65
        if !metis_fail andalso not (Lazy.is_finished lazy_time)
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    66
          then some_preplay_time
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
    67
          else Lazy.force lazy_time
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    68
      val metis_fail = fn () => !metis_fail
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    69
    end
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    70
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    71
    (* compress top level steps - do not compress subproofs *)
51260
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
    72
    fun compress_top_level on_top_level ctxt n steps =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    73
    let
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    74
      (* proof step vector *)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    75
      val step_vect = steps |> map SOME |> Vector.fromList
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    76
      val n_metis = add_metis_steps_top_level steps 0
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
    77
      val target_n_metis = Real.fromInt n_metis / isar_compress |> Real.round
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    78
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    79
      (* table for mapping from (top-level-)label to step_vect position *)
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    80
      fun update_table (i, Prove (_, _, l, _, _, _)) =
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    81
            Label_Table.update_new (l, i)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    82
        | update_table _ = I
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    83
      val label_index_table = fold_index update_table steps Label_Table.empty
50711
smolkas
parents: 50678
diff changeset
    84
      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
    85
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    86
      (* proof step references *)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    87
      fun refs step =
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    88
        fold_isar_step
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    89
          (byline_of_step
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    90
           #> (fn SOME (By_Metis (lfs, _)) => append (lookup_indices lfs)
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    91
                | _ => I))
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    92
          step []
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    93
      val refed_by_vect =
51260
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
    94
        Vector.tabulate (Vector.length step_vect, K [])
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    95
        |> fold_index (fn (i, step) => fold (update (cons i)) (refs step)) steps
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    96
        |> 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
    97
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    98
      (* candidates for elimination, use table as priority queue (greedy
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
    99
         algorithm) *)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   100
      fun add_if_cand step_vect (i, [j]) =
51876
724c67f59929 added informative error messages
smolkas
parents: 51741
diff changeset
   101
          ((case (the (get i step_vect), the (get j step_vect)) of
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   102
            (Prove (_, Fix [], _, t, _, By_Metis _),
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   103
             Prove (_, _, _, _, _, By_Metis _)) => cons (Term.size_of_term t, i)
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51877
diff changeset
   104
          | _ => I)
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51879
diff changeset
   105
            handle Option.Option => raise Fail "sledgehammer_compress: add_if_cand")
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   106
        | add_if_cand _ _ = I
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   107
      val cand_tab =
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   108
        v_fold_index (add_if_cand step_vect) refed_by_vect []
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   109
        |> Inttab.make_list
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   110
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   111
      (* cache metis preplay times in lazy time vector *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   112
      val metis_time =
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   113
        Vector.map
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   114
          (if not preplay then K (zero_preplay_time) #> Lazy.value
50923
141d8f575f6f move preplaying to own structure
smolkas
parents: 50908
diff changeset
   115
           else
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   116
             the
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51877
diff changeset
   117
             #> try_metis debug preplay_trace type_enc lam_trans ctxt
ee9562d31778 added preplay tracing
smolkas
parents: 51877
diff changeset
   118
              preplay_timeout
50923
141d8f575f6f move preplaying to own structure
smolkas
parents: 50908
diff changeset
   119
             #> handle_metis_fail
141d8f575f6f move preplaying to own structure
smolkas
parents: 50908
diff changeset
   120
             #> Lazy.lazy)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   121
          step_vect
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51879
diff changeset
   122
        handle Option.Option => raise Fail "sledgehammer_compress: metis_time"
50923
141d8f575f6f move preplaying to own structure
smolkas
parents: 50908
diff changeset
   123
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   124
      fun sum_up_time lazy_time_vector =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   125
        Vector.foldl
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   126
          (apfst get_time #> uncurry add_preplay_time)
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   127
          zero_preplay_time lazy_time_vector
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   128
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   129
      (* Merging *)
52453
2cba5906d836 simplified data structure
smolkas
parents: 52049
diff changeset
   130
      fun merge
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   131
          (Prove (_, Fix [], lbl1, _, subproofs1, By_Metis (lfs1, gfs1)))
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   132
          (Prove (qs2, fix, lbl2, t, subproofs2, By_Metis (lfs2, gfs2))) =
52453
2cba5906d836 simplified data structure
smolkas
parents: 52049
diff changeset
   133
            let
2cba5906d836 simplified data structure
smolkas
parents: 52049
diff changeset
   134
              val lfs = remove (op =) lbl1 lfs2 |> union (op =) lfs1
2cba5906d836 simplified data structure
smolkas
parents: 52049
diff changeset
   135
              val gfs = union (op =) gfs1 gfs2
2cba5906d836 simplified data structure
smolkas
parents: 52049
diff changeset
   136
              val subproofs = subproofs1 @ subproofs2
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   137
            in Prove (qs2, fix, lbl2, t, subproofs, By_Metis (lfs, gfs)) end
51876
724c67f59929 added informative error messages
smolkas
parents: 51741
diff changeset
   138
        | merge _ _ = raise Fail "sledgehammer_compress: unmergeable Isar steps"
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   139
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   140
      fun try_merge metis_time (s1, i) (s2, j) =
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   141
        if not preplay then (merge s1 s2 |> SOME, metis_time)
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   142
        else
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   143
          (case get i metis_time |> Lazy.force of
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   144
            (true, _) => (NONE, metis_time)
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   145
          | (_, t1) =>
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   146
            (case get j metis_time |> Lazy.force of
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   147
              (true, _) => (NONE, metis_time)
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   148
            | (_, t2) =>
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   149
              let
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   150
                val s12 = merge s1 s2
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   151
                val timeout = time_mult merge_timeout_slack (Time.+(t1, t2))
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   152
              in
51879
ee9562d31778 added preplay tracing
smolkas
parents: 51877
diff changeset
   153
                case try_metis_quietly debug preplay_trace type_enc
ee9562d31778 added preplay tracing
smolkas
parents: 51877
diff changeset
   154
                                                lam_trans ctxt timeout s12 () of
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   155
                  (true, _) => (NONE, metis_time)
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   156
                | exact_time =>
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   157
                  (SOME s12, metis_time
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   158
                             |> replace (zero_preplay_time |> Lazy.value) i
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   159
                             |> replace (Lazy.value exact_time) j)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   160
50924
beb95bf66b21 changed type of preplay time; tuned preplaying
smolkas
parents: 50923
diff changeset
   161
              end))
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   162
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   163
      fun merge_steps metis_time step_vect refed_by cand_tab n' n_metis' =
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   164
        if Inttab.is_empty cand_tab
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   165
          orelse n_metis' <= target_n_metis
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   166
          orelse (on_top_level andalso n'<3)
51877
71052c42edf2 avoid dummy annotations; terminate preplay/compression if metis fails; fixed bug
smolkas
parents: 51876
diff changeset
   167
          orelse metis_fail()
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   168
        then
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   169
          (Vector.foldr
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   170
             (fn (NONE, steps) => steps | (SOME s, steps) => s :: steps)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   171
             [] step_vect,
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   172
           sum_up_time metis_time)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   173
        else
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   174
          let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   175
            val (i, cand_tab) = pop_max cand_tab
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   176
            val j = get i refed_by |> the_single
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   177
            val s1 = get i step_vect |> the
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   178
            val s2 = get j step_vect |> the
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   179
          in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   180
            case try_merge metis_time (s1, i) (s2, j) of
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   181
              (NONE, metis_time) =>
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   182
              merge_steps metis_time step_vect refed_by cand_tab n' n_metis'
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   183
            | (s, metis_time) =>
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   184
            let
51877
71052c42edf2 avoid dummy annotations; terminate preplay/compression if metis fails; fixed bug
smolkas
parents: 51876
diff changeset
   185
              val refs_s1 = refs s1
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   186
              val refed_by = refed_by |> fold
51877
71052c42edf2 avoid dummy annotations; terminate preplay/compression if metis fails; fixed bug
smolkas
parents: 51876
diff changeset
   187
                (update (Ord_List.remove int_ord i #> Ord_List.insert int_ord j))
71052c42edf2 avoid dummy annotations; terminate preplay/compression if metis fails; fixed bug
smolkas
parents: 51876
diff changeset
   188
                refs_s1
71052c42edf2 avoid dummy annotations; terminate preplay/compression if metis fails; fixed bug
smolkas
parents: 51876
diff changeset
   189
              val shared_refs = Ord_List.inter int_ord refs_s1 (refs s2)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   190
              val new_candidates =
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   191
                fold (add_if_cand step_vect)
51877
71052c42edf2 avoid dummy annotations; terminate preplay/compression if metis fails; fixed bug
smolkas
parents: 51876
diff changeset
   192
                  (map (fn i => (i, get i refed_by)) shared_refs) []
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   193
              val cand_tab = add_list cand_tab new_candidates
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   194
              val step_vect = step_vect |> replace NONE i |> replace s j
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   195
            in
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   196
              merge_steps metis_time step_vect refed_by cand_tab (n' - 1)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   197
                          (n_metis' - 1)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   198
            end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   199
          end
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 51879
diff changeset
   200
        handle Option.Option => raise Fail "sledgehammer_compress: merge_steps"
51876
724c67f59929 added informative error messages
smolkas
parents: 51741
diff changeset
   201
             | List.Empty => raise Fail "sledgehammer_compress: merge_steps"
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   202
    in
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   203
      merge_steps metis_time step_vect refed_by_vect cand_tab n n_metis
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   204
    end
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   205
51260
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
   206
    fun do_proof on_top_level ctxt (Proof (Fix fix, Assume assms, steps)) =
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   207
      let
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   208
        (* Enrich context with top-level facts *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   209
        val thy = Proof_Context.theory_of ctxt
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   210
        (* TODO: add Skolem variables to context? *)
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   211
        fun enrich_with_fact l t =
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   212
          Proof_Context.put_thms false
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51931
diff changeset
   213
              (string_of_label l, SOME [Skip_Proof.make_thm thy t])
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   214
        fun enrich_with_step (Prove (_, _, l, t, _, _)) = enrich_with_fact l t
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   215
          | enrich_with_step _ = I
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   216
        val enrich_with_steps = fold enrich_with_step
51260
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
   217
        val enrich_with_assms = fold (uncurry enrich_with_fact)
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   218
        val rich_ctxt =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   219
          ctxt |> enrich_with_assms assms |> enrich_with_steps steps
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   220
51260
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
   221
        val n = List.length fix + List.length assms + List.length steps
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
   222
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   223
        (* compress subproofs and top-levl steps *)
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   224
        val ((steps, top_level_time), lower_level_time) =
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   225
          steps |> do_subproofs rich_ctxt
51260
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
   226
                |>> compress_top_level on_top_level rich_ctxt n
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   227
      in
51260
61bc5a3bef09 tuned agressiveness of isar compression
smolkas
parents: 51179
diff changeset
   228
        (Proof (Fix fix, Assume assms, steps),
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   229
          add_preplay_time lower_level_time top_level_time)
50672
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
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   232
    and do_subproofs ctxt subproofs =
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   233
      let
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   234
        fun compress_each_and_collect_time compress subproofs =
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   235
          let fun f_m proof time = compress proof ||> add_preplay_time time
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   236
          in fold_map f_m subproofs zero_preplay_time end
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   237
        val compress_subproofs =
51130
76d68444cd59 renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents: 51128
diff changeset
   238
          compress_each_and_collect_time (do_proof false ctxt)
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   239
        fun compress (Prove (qs, fix, l, t, subproofs, By_Metis facts)) =
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   240
              let val (subproofs, time) = compress_subproofs subproofs
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   241
              in (Prove (qs, fix, l, t, subproofs, By_Metis  facts), time) end
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   242
          | compress atomic_step = (atomic_step, zero_preplay_time)
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   243
      in
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   244
        compress_each_and_collect_time compress subproofs
50672
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   245
      end
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   246
  in
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   247
    do_proof true ctxt proof
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   248
    |> apsnd (pair (metis_fail ()))
ab5b8b5c9cbe added "obtain" to Isar proof construction data structure
blanchet
parents: 50557
diff changeset
   249
  end
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   250
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   251
end