src/HOL/Tools/Sledgehammer/sledgehammer_reconstructor.ML
author blanchet
Mon, 03 Feb 2014 10:14:18 +0100
changeset 55269 aae87746f412
parent 55212 5832470d956e
child 55285 e88ad20035f4
permissions -rw-r--r--
more thorough, hybrid compression
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52590
smolkas
parents: 52555
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_reconstructor.ML
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     4
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     5
Reconstructors.
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     6
*)
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     7
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     8
signature SLEDGEHAMMER_RECONSTRUCTOR =
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
     9
sig
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    10
  type stature = ATP_Problem_Generate.stature
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    11
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    12
  datatype reconstructor =
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    13
    Metis of string * string |
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    14
    SMT
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    15
54824
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    16
  datatype play_outcome =
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    17
    Played of Time.time |
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    18
    Play_Timed_Out of Time.time |
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    19
    Play_Failed |
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    20
    Not_Played
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    21
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    22
  type minimize_command = string list -> string
54824
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    23
  type one_line_params =
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    24
    (reconstructor * play_outcome) * string * (string * stature) list * minimize_command * int * int
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    25
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    26
  val smtN : string
55212
blanchet
parents: 55211
diff changeset
    27
55211
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    28
  val string_of_reconstructor : reconstructor -> string
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    29
  val string_of_play_outcome : play_outcome -> string
55269
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    30
  val play_outcome_ord : play_outcome * play_outcome -> order
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    31
55211
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    32
  val one_line_proof_text : int -> one_line_params -> string
55170
cdb9435e3cae silenced reconstructors in Sledgehammer
blanchet
parents: 54828
diff changeset
    33
  val silence_reconstructors : bool -> Proof.context -> Proof.context
54495
237d5be57277 refactored
blanchet
parents: 52590
diff changeset
    34
end;
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    35
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    36
structure Sledgehammer_Reconstructor : SLEDGEHAMMER_RECONSTRUCTOR =
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    37
struct
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    38
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54824
diff changeset
    39
open ATP_Util
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    40
open ATP_Problem_Generate
55211
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    41
open ATP_Proof_Reconstruct
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    42
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    43
datatype reconstructor =
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    44
  Metis of string * string |
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    45
  SMT
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    46
54824
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    47
datatype play_outcome =
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    48
  Played of Time.time |
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    49
  Play_Timed_Out of Time.time |
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    50
  Play_Failed |
4e58a38b330b refactored preplaying outcome data structure
blanchet
parents: 54823
diff changeset
    51
  Not_Played
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
    52
55211
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    53
type minimize_command = string list -> string
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    54
type one_line_params =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    55
  (reconstructor * play_outcome) * string * (string * stature) list * minimize_command * int * int
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    56
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    57
val smtN = "smt"
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    58
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    59
fun string_of_reconstructor (Metis (type_enc, lam_trans)) = metis_call type_enc lam_trans
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    60
  | string_of_reconstructor SMT = smtN
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    61
54828
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54824
diff changeset
    62
fun string_of_play_outcome (Played time) = string_of_ext_time (false, time)
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54824
diff changeset
    63
  | string_of_play_outcome (Play_Timed_Out time) = string_of_ext_time (true, time) ^ ", timed out"
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54824
diff changeset
    64
  | string_of_play_outcome Play_Failed = "failed"
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54824
diff changeset
    65
  | string_of_play_outcome _ = "unknown"
b2271ad695db don't do 'isar_try0' if preplaying is off
blanchet
parents: 54824
diff changeset
    66
55269
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    67
fun play_outcome_ord (Played time1, Played time2) =
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    68
    int_ord (pairself Time.toMilliseconds (time1, time2))
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    69
  | play_outcome_ord (Played _, _) = LESS
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    70
  | play_outcome_ord (_, Played _) = GREATER
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    71
  | play_outcome_ord (Not_Played, Not_Played) = EQUAL
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    72
  | play_outcome_ord (Not_Played, _) = LESS
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    73
  | play_outcome_ord (_, Not_Played) = GREATER
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    74
  | play_outcome_ord (Play_Timed_Out time1, Play_Timed_Out time2) =
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    75
    int_ord (pairself Time.toMilliseconds (time1, time2))
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    76
  | play_outcome_ord (Play_Timed_Out _, _) = LESS
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    77
  | play_outcome_ord (_, Play_Timed_Out _) = GREATER
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    78
  | play_outcome_ord (Play_Failed, Play_Failed) = EQUAL
aae87746f412 more thorough, hybrid compression
blanchet
parents: 55212
diff changeset
    79
55211
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    80
(* FIXME: Various bugs, esp. with "unfolding"
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    81
fun unusing_chained_facts _ 0 = ""
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    82
  | unusing_chained_facts used_chaineds num_chained =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    83
    if length used_chaineds = num_chained then ""
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    84
    else if null used_chaineds then "(* using no facts *) "
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    85
    else "(* using only " ^ space_implode " " used_chaineds ^ " *) "
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    86
*)
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    87
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    88
fun apply_on_subgoal _ 1 = "by "
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    89
  | apply_on_subgoal 1 _ = "apply "
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    90
  | apply_on_subgoal i n =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    91
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    92
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    93
fun command_call name [] =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    94
    name |> not (Symbol_Pos.is_identifier name) ? enclose "(" ")"
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    95
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    96
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    97
fun reconstructor_command reconstr i n used_chaineds num_chained ss =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    98
  (* unusing_chained_facts used_chaineds num_chained ^ *)
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
    99
  apply_on_subgoal i n ^ command_call (string_of_reconstructor reconstr) ss
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   100
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   101
fun show_time NONE = ""
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   102
  | show_time (SOME ext_time) = " (" ^ string_of_ext_time ext_time ^ ")"
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   103
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   104
fun try_command_line banner time command =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   105
  banner ^ ": " ^ Active.sendback_markup [Markup.padding_command] command ^ show_time time ^ "."
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
   106
55211
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   107
fun minimize_line _ [] = ""
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   108
  | minimize_line minimize_command ss =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   109
    (case minimize_command ss of
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   110
      "" => ""
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   111
    | command => "\nTo minimize: " ^ Active.sendback_markup [Markup.padding_command] command ^ ".")
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   112
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   113
fun split_used_facts facts =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   114
  facts
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   115
  |> List.partition (fn (_, (sc, _)) => sc = Chained)
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   116
  |> pairself (sort_distinct (string_ord o pairself fst))
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   117
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   118
fun one_line_proof_text num_chained
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   119
    ((reconstr, play), banner, used_facts, minimize_command, subgoal, subgoal_count) =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   120
  let
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   121
    val (chained, extra) = split_used_facts used_facts
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   122
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   123
    val (failed, ext_time) =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   124
      (case play of
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   125
        Played time => (false, (SOME (false, time)))
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   126
      | Play_Timed_Out time => (false, SOME (true, time))
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   127
      | Play_Failed => (true, NONE)
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   128
      | Not_Played => (false, NONE))
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   129
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   130
    val try_line =
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   131
      map fst extra
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   132
      |> reconstructor_command reconstr subgoal subgoal_count (map fst chained) num_chained
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   133
      |> (if failed then
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   134
            enclose "One-line proof reconstruction failed: "
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   135
              ".\n(Invoking \"sledgehammer\" with \"[strict]\" might solve this.)"
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   136
          else
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   137
            try_command_line banner ext_time)
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   138
  in
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   139
    try_line ^ minimize_line minimize_command (map fst (extra @ chained))
5d027af93a08 moved ML code around
blanchet
parents: 55180
diff changeset
   140
  end
52555
6811291d1869 moved code -> easier debugging
smolkas
parents:
diff changeset
   141
55170
cdb9435e3cae silenced reconstructors in Sledgehammer
blanchet
parents: 54828
diff changeset
   142
(* Makes reconstructor tools as silent as possible. The "set_visible" calls suppresses "Unification
cdb9435e3cae silenced reconstructors in Sledgehammer
blanchet
parents: 54828
diff changeset
   143
   bound exceeded" warnings and the like. *)
cdb9435e3cae silenced reconstructors in Sledgehammer
blanchet
parents: 54828
diff changeset
   144
fun silence_reconstructors debug =
55180
03ac74b01e49 compile
blanchet
parents: 55177
diff changeset
   145
  Try0.silence_methods debug
55170
cdb9435e3cae silenced reconstructors in Sledgehammer
blanchet
parents: 54828
diff changeset
   146
  #> Config.put SMT_Config.verbose debug
cdb9435e3cae silenced reconstructors in Sledgehammer
blanchet
parents: 54828
diff changeset
   147
54495
237d5be57277 refactored
blanchet
parents: 52590
diff changeset
   148
end;