src/HOL/Tools/Sledgehammer/sledgehammer_isar_compress.ML
author blanchet
Mon Jun 02 17:34:26 2014 +0200 (2014-06-02 ago)
changeset 57158 f028d93798e6
parent 57054 fed0329ea8e2
child 57162 5ed907407041
permissions -rw-r--r--
simplified counterexample handling
blanchet@55202
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar_compress.ML
blanchet@54813
     2
    Author:     Steffen Juilf Smolka, TU Muenchen
smolkas@50263
     3
    Author:     Jasmin Blanchette, TU Muenchen
smolkas@50263
     4
blanchet@55202
     5
Compression of Isar proofs by merging steps.
blanchet@54712
     6
Only proof steps using the same proof method are merged.
smolkas@50263
     7
*)
smolkas@50263
     8
blanchet@55202
     9
signature SLEDGEHAMMER_ISAR_COMPRESS =
smolkas@50259
    10
sig
blanchet@55202
    11
  type isar_proof = Sledgehammer_Isar_Proof.isar_proof
blanchet@55213
    12
  type isar_preplay_data = Sledgehammer_Isar_Preplay.isar_preplay_data
smolkas@52556
    13
blanchet@57054
    14
  val compress_isar_proof : Proof.context -> real -> Time.time ->
blanchet@55329
    15
    isar_preplay_data Unsynchronized.ref -> isar_proof -> isar_proof
blanchet@54504
    16
end;
smolkas@52556
    17
blanchet@55202
    18
structure Sledgehammer_Isar_Compress : SLEDGEHAMMER_ISAR_COMPRESS =
smolkas@50259
    19
struct
smolkas@50259
    20
smolkas@50265
    21
open Sledgehammer_Util
blanchet@55287
    22
open Sledgehammer_Proof_Methods
blanchet@55202
    23
open Sledgehammer_Isar_Proof
blanchet@55202
    24
open Sledgehammer_Isar_Preplay
smolkas@50259
    25
smolkas@52556
    26
fun collect_successors steps lbls =
smolkas@52556
    27
  let
blanchet@55329
    28
    fun collect_steps _ (accum as ([], _)) = accum
blanchet@55263
    29
      | collect_steps [] accum = accum
blanchet@55263
    30
      | collect_steps (step :: steps) accum = collect_steps steps (collect_step step accum)
blanchet@55329
    31
    and collect_step (step as Prove (_, _, l, _, subproofs, _, _, _)) x =
blanchet@55263
    32
        (case collect_subproofs subproofs x of
blanchet@55329
    33
          (accum as ([], _)) => accum
blanchet@54712
    34
        | accum as (l' :: lbls', accu) => if l = l' then (lbls', step :: accu) else accum)
blanchet@55329
    35
        | collect_step _ accum = accum
blanchet@55329
    36
    and collect_subproofs [] accum = accum
blanchet@55329
    37
      | collect_subproofs (proof :: subproofs) accum =
blanchet@55329
    38
        (case collect_steps (steps_of_isar_proof proof) accum of
blanchet@54712
    39
          accum as ([], _) => accum
blanchet@55263
    40
        | accum => collect_subproofs subproofs accum)
smolkas@52556
    41
  in
blanchet@55313
    42
    rev (snd (collect_steps steps (lbls, [])))
smolkas@52556
    43
  end
smolkas@50259
    44
blanchet@55332
    45
fun update_steps updates steps =
smolkas@52556
    46
  let
blanchet@55263
    47
    fun update_steps [] updates = ([], updates)
blanchet@55263
    48
      | update_steps steps [] = (steps, [])
blanchet@55263
    49
      | update_steps (step :: steps) updates = update_step step (update_steps steps updates)
blanchet@55263
    50
    and update_step step (steps, []) = (step :: steps, [])
blanchet@55299
    51
      | update_step (Prove (qs, xs, l, t, subproofs, facts, meths, comment))
blanchet@55299
    52
          (steps,
blanchet@55299
    53
           updates as Prove (qs', xs', l', t', subproofs', facts', meths', comment') :: updates') =
blanchet@55299
    54
        (if l = l' then
blanchet@55299
    55
           update_subproofs subproofs' updates'
blanchet@55299
    56
           |>> (fn subproofs' => Prove (qs', xs', l', t', subproofs', facts', meths', comment'))
blanchet@55299
    57
         else
blanchet@55299
    58
           update_subproofs subproofs updates
blanchet@55299
    59
           |>> (fn subproofs => Prove (qs, xs, l, t, subproofs, facts, meths, comment)))
blanchet@55299
    60
        |>> (fn step => step :: steps)
blanchet@55329
    61
      | update_step step (steps, updates) = (step :: steps, updates)
blanchet@55263
    62
    and update_subproofs [] updates = ([], updates)
blanchet@55263
    63
      | update_subproofs steps [] = (steps, [])
blanchet@55263
    64
      | update_subproofs (proof :: subproofs) updates =
blanchet@55263
    65
        update_proof proof (update_subproofs subproofs updates)
blanchet@55263
    66
    and update_proof proof (proofs, []) = (proof :: proofs, [])
blanchet@55263
    67
      | update_proof (Proof (fix, assms, steps)) (proofs, updates) =
blanchet@55263
    68
        let val (steps, updates) = update_steps steps updates in
blanchet@54712
    69
          (Proof (fix, assms, steps) :: proofs, updates)
blanchet@54712
    70
        end
smolkas@52556
    71
  in
blanchet@55313
    72
    fst (update_steps steps (rev updates))
smolkas@52556
    73
  end
blanchet@50672
    74
blanchet@55283
    75
fun merge_methods preplay_data (l1, meths1) (l2, meths2) =
blanchet@55283
    76
  let
blanchet@55328
    77
    fun is_hopeful l meth =
blanchet@55283
    78
      let val outcome = preplay_outcome_of_isar_step_for_method preplay_data l meth in
blanchet@55283
    79
        not (Lazy.is_finished outcome) orelse
blanchet@55328
    80
        (case Lazy.force outcome of Played _ => true | Play_Timed_Out _ => true | _ => false)
blanchet@55283
    81
      end
blanchet@55328
    82
blanchet@55328
    83
    val (hopeful, hopeless) =
blanchet@55328
    84
      meths2 @ subtract (op =) meths2 meths1
blanchet@55328
    85
      |> List.partition (is_hopeful l1 andf is_hopeful l2)
blanchet@55283
    86
  in
blanchet@55328
    87
    (hopeful @ hopeless, hopeless)
blanchet@55283
    88
  end
blanchet@55283
    89
blanchet@55332
    90
fun merge_steps preplay_data (Prove ([], fix1, l1, _, subproofs1, (lfs1, gfs1), meths1, comment1))
blanchet@55329
    91
      (Prove (qs2, fix2, l2, t, subproofs2, (lfs2, gfs2), meths2, comment2)) =
blanchet@55332
    92
  let
blanchet@55332
    93
    val (meths, hopeless) = merge_methods preplay_data (l1, meths1) (l2, meths2)
blanchet@55332
    94
    val lfs = union (op =) lfs1 (remove (op =) l1 lfs2)
blanchet@55332
    95
    val gfs = union (op =) gfs1 gfs2
blanchet@55332
    96
  in
blanchet@55332
    97
    (Prove (qs2, union (op =) fix1 fix2, l2, t, subproofs1 @ subproofs2, (lfs, gfs), meths,
blanchet@55332
    98
       comment1 ^ comment2), hopeless)
blanchet@55332
    99
  end
blanchet@50672
   100
blanchet@55329
   101
val merge_slack_time = seconds 0.005
blanchet@55329
   102
val merge_slack_factor = 1.5
blanchet@55329
   103
blanchet@55329
   104
fun adjust_merge_timeout max time =
blanchet@55329
   105
  let val timeout = time_mult merge_slack_factor (Time.+ (merge_slack_time, time)) in
blanchet@55329
   106
    if Time.< (max, timeout) then max else timeout
blanchet@55329
   107
  end
blanchet@55329
   108
blanchet@53763
   109
val compress_degree = 2
blanchet@55271
   110
blanchet@55271
   111
(* Precondition: The proof must be labeled canonically. *)
blanchet@57054
   112
fun compress_isar_proof ctxt compress_isar preplay_timeout preplay_data proof =
blanchet@55183
   113
  if compress_isar <= 1.0 then
smolkas@52556
   114
    proof
smolkas@52556
   115
  else
blanchet@54712
   116
    let
blanchet@54712
   117
      val (compress_further, decrement_step_count) =
blanchet@54712
   118
        let
blanchet@55260
   119
          val number_of_steps = add_isar_steps (steps_of_isar_proof proof) 0
blanchet@55183
   120
          val target_number_of_steps = Real.round (Real.fromInt number_of_steps / compress_isar)
blanchet@54712
   121
          val delta = Unsynchronized.ref (number_of_steps - target_number_of_steps)
blanchet@54712
   122
        in
blanchet@54712
   123
          (fn () => !delta > 0, fn () => delta := !delta - 1)
blanchet@54712
   124
        end
smolkas@52556
   125
blanchet@54712
   126
      val (get_successors, replace_successor) =
blanchet@54712
   127
        let
blanchet@55299
   128
          fun add_refs (Prove (_, _, l, _, _, (lfs, _), _, _)) =
blanchet@55299
   129
              fold (fn key => Canonical_Label_Tab.cons_list (key, l)) lfs
blanchet@55299
   130
            | add_refs _ = I
smolkas@52556
   131
blanchet@54712
   132
          val tab =
blanchet@55212
   133
            Canonical_Label_Tab.empty
blanchet@55260
   134
            |> fold_isar_steps add_refs (steps_of_isar_proof proof)
blanchet@54712
   135
            (* "rev" should have the same effect as "sort canonical_label_ord" *)
blanchet@55212
   136
            |> Canonical_Label_Tab.map (K rev)
blanchet@54712
   137
            |> Unsynchronized.ref
smolkas@51260
   138
blanchet@55212
   139
          fun get_successors l = Canonical_Label_Tab.lookup_list (!tab) l
blanchet@55212
   140
          fun set_successors l refs = tab := Canonical_Label_Tab.update (l, refs) (!tab)
blanchet@54712
   141
          fun replace_successor old new dest =
blanchet@54712
   142
            get_successors dest
blanchet@54712
   143
            |> Ord_List.remove canonical_label_ord old
blanchet@54712
   144
            |> Ord_List.union canonical_label_ord new
blanchet@54712
   145
            |> set_successors dest
blanchet@54712
   146
        in
blanchet@54712
   147
          (get_successors, replace_successor)
blanchet@54712
   148
        end
smolkas@52556
   149
blanchet@55333
   150
      fun reference_time l =
blanchet@55333
   151
        (case forced_intermediate_preplay_outcome_of_isar_step (!preplay_data) l of
blanchet@55333
   152
          Played time => time
blanchet@55333
   153
        | _ => preplay_timeout)
blanchet@55333
   154
blanchet@55243
   155
      (* elimination of trivial, one-step subproofs *)
blanchet@55330
   156
      fun elim_one_subproof time (step as Prove (qs, fix, l, t, _, (lfs, gfs), meths, comment)) subs
blanchet@55309
   157
          nontriv_subs =
blanchet@54712
   158
        if null subs orelse not (compress_further ()) then
blanchet@55330
   159
          Prove (qs, fix, l, t, List.revAppend (nontriv_subs, subs), (lfs, gfs), meths, comment)
blanchet@54712
   160
        else
blanchet@54712
   161
          (case subs of
blanchet@55333
   162
            (sub as Proof (_, assms, [Prove (_, _, l', _, [], (lfs', gfs'), meths', _)])) :: subs =>
blanchet@55333
   163
            let
smolkas@52556
   164
              (* merge steps *)
smolkas@52556
   165
              val subs'' = subs @ nontriv_subs
blanchet@55223
   166
              val lfs'' = union (op =) lfs (subtract (op =) (map fst assms) lfs')
smolkas@52556
   167
              val gfs'' = union (op =) gfs' gfs
blanchet@55328
   168
              val (meths'' as _ :: _, hopeless) =
blanchet@55328
   169
                merge_methods (!preplay_data) (l', meths') (l, meths)
blanchet@55299
   170
              val step'' = Prove (qs, fix, l, t, subs'', (lfs'', gfs''), meths'', comment)
smolkas@52556
   171
smolkas@52556
   172
              (* check if the modified step can be preplayed fast enough *)
blanchet@55333
   173
              val timeout = adjust_merge_timeout preplay_timeout (Time.+ (time, reference_time l'))
smolkas@52556
   174
            in
blanchet@57054
   175
              (case preplay_isar_step ctxt timeout hopeless step'' of
blanchet@55333
   176
                meths_outcomes as (_, Played time'') :: _ =>
blanchet@55333
   177
                (* l' successfully eliminated *)
blanchet@55333
   178
                (decrement_step_count ();
blanchet@57054
   179
                 set_preplay_outcomes_of_isar_step ctxt time'' preplay_data step'' meths_outcomes;
blanchet@55333
   180
                 map (replace_successor l' [l]) lfs';
blanchet@55333
   181
                 elim_one_subproof time'' step'' subs nontriv_subs)
blanchet@55333
   182
              | _ => elim_one_subproof time step subs (sub :: nontriv_subs))
smolkas@52556
   183
            end
blanchet@55333
   184
          | sub :: subs => elim_one_subproof time step subs (sub :: nontriv_subs))
smolkas@52556
   185
blanchet@55333
   186
      fun elim_subproofs (step as Prove (_, _, l, _, subproofs, _, _, _)) =
blanchet@55333
   187
          if exists (null o tl o steps_of_isar_proof) subproofs then
blanchet@55333
   188
            elim_one_subproof (reference_time l) step subproofs []
blanchet@55333
   189
          else
blanchet@55333
   190
            step
blanchet@55299
   191
        | elim_subproofs step = step
smolkas@52556
   192
blanchet@54712
   193
      fun compress_top_level steps =
blanchet@54712
   194
        let
blanchet@55331
   195
          fun cand_key (l, t_size) = (length (get_successors l), t_size)
blanchet@55331
   196
          val cand_ord = prod_ord int_ord (int_ord o swap) o pairself cand_key
smolkas@52556
   197
blanchet@55298
   198
          fun pop_next_candidate [] = (NONE, [])
blanchet@55298
   199
            | pop_next_candidate (cands as (cand :: cands')) =
blanchet@55331
   200
              fold (fn x => fn y => if cand_ord (x, y) = LESS then x else y) cands' cand
blanchet@55325
   201
              |> (fn best => (SOME best, remove (op =) best cands))
smolkas@52556
   202
blanchet@55332
   203
          fun try_eliminate i l labels steps =
blanchet@54712
   204
            let
blanchet@55330
   205
              val (steps_before, (cand as Prove (_, _, _, _, _, (lfs, _), _, _)) :: steps_after) =
blanchet@55332
   206
                chop i steps
blanchet@55330
   207
              val succs = collect_successors steps_after labels
blanchet@55332
   208
              val (succs', hopelesses) = split_list (map (merge_steps (!preplay_data) cand) succs)
blanchet@54712
   209
            in
blanchet@55332
   210
              (case try (map ((fn Played time => time) o
blanchet@55332
   211
                  forced_intermediate_preplay_outcome_of_isar_step (!preplay_data))) labels of
blanchet@55332
   212
                NONE => steps
blanchet@55332
   213
              | SOME times0 =>
blanchet@55332
   214
                let
blanchet@55333
   215
                  val time_slice = time_mult (1.0 / Real.fromInt (length labels)) (reference_time l)
blanchet@55332
   216
                  val timeouts =
blanchet@55332
   217
                    map (adjust_merge_timeout preplay_timeout o curry Time.+ time_slice) times0
blanchet@55452
   218
                  val meths_outcomess =
blanchet@57054
   219
                    map3 (preplay_isar_step ctxt) timeouts hopelesses succs'
blanchet@55332
   220
                in
blanchet@55332
   221
                  (case try (map (fn (_, Played time) :: _ => time)) meths_outcomess of
blanchet@55332
   222
                    NONE => steps
blanchet@55332
   223
                  | SOME times =>
blanchet@55332
   224
                    (* candidate successfully eliminated *)
blanchet@55332
   225
                    (decrement_step_count ();
blanchet@57054
   226
                     map3 (fn time => set_preplay_outcomes_of_isar_step ctxt time preplay_data)
blanchet@55332
   227
                       times succs' meths_outcomess;
blanchet@55332
   228
                     map (replace_successor l labels) lfs;
blanchet@55332
   229
                     steps_before @ update_steps succs' steps_after))
blanchet@55332
   230
                end)
blanchet@54712
   231
            end
smolkas@52556
   232
blanchet@54712
   233
          fun compression_loop candidates steps =
blanchet@54712
   234
            if not (compress_further ()) then
blanchet@54712
   235
              steps
blanchet@54712
   236
            else
blanchet@55298
   237
              (case pop_next_candidate candidates of
blanchet@54712
   238
                (NONE, _) => steps (* no more candidates for elimination *)
blanchet@55332
   239
              | (SOME (l, _), candidates') =>
blanchet@55332
   240
                (case find_index (curry (op =) (SOME l) o label_of_isar_step) steps of
blanchet@55332
   241
                  ~1 => steps
blanchet@55332
   242
                | i =>
blanchet@55332
   243
                  let val successors = get_successors l in
blanchet@55332
   244
                    if length successors > compress_degree then steps
blanchet@55332
   245
                    else compression_loop candidates' (try_eliminate i l successors steps)
blanchet@55332
   246
                  end))
blanchet@55331
   247
blanchet@55331
   248
          fun add_cand (Prove (_, _, l, t, _, _, _, _)) = cons (l, size_of_term t)
blanchet@55331
   249
            | add_cand _ = I
blanchet@55331
   250
blanchet@55331
   251
          (* the very last step is not a candidate *)
blanchet@55331
   252
          val candidates = fold add_cand (fst (split_last steps)) []
blanchet@54712
   253
        in
blanchet@54712
   254
          compression_loop candidates steps
blanchet@54712
   255
        end
smolkas@52556
   256
blanchet@55268
   257
      (* Proofs are compressed bottom-up, beginning with the innermost subproofs. On the innermost
blanchet@55268
   258
         proof level, the proof steps have no subproofs. In the best case, these steps can be merged
blanchet@55268
   259
         into just one step, resulting in a trivial subproof. Going one level up, trivial subproofs
blanchet@55268
   260
         can be eliminated. In the best case, this once again leads to a proof whose proof steps do
blanchet@55268
   261
         not have subproofs. Applying this approach recursively will result in a flat proof in the
blanchet@55268
   262
         best cast. *)
blanchet@55263
   263
      fun compress_proof (proof as (Proof (fix, assms, steps))) =
blanchet@55263
   264
        if compress_further () then Proof (fix, assms, compress_steps steps) else proof
blanchet@55263
   265
      and compress_steps steps =
blanchet@54712
   266
        (* bottom-up: compress innermost proofs first *)
blanchet@55263
   267
        steps |> map (fn step => step |> compress_further () ? compress_sub_levels)
blanchet@54712
   268
              |> compress_further () ? compress_top_level
blanchet@55329
   269
      and compress_sub_levels (Prove (qs, xs, l, t, subproofs, facts, meths, comment)) =
smolkas@52556
   270
          (* compress subproofs *)
blanchet@55299
   271
          Prove (qs, xs, l, t, map compress_proof subproofs, facts, meths, comment)
blanchet@54712
   272
          (* eliminate trivial subproofs *)
blanchet@54712
   273
          |> compress_further () ? elim_subproofs
blanchet@55329
   274
        | compress_sub_levels step = step
blanchet@54712
   275
    in
blanchet@55263
   276
      compress_proof proof
blanchet@54712
   277
    end
smolkas@50259
   278
blanchet@54504
   279
end;