src/HOL/Tools/Sledgehammer/sledgehammer_reconstructor.ML
author blanchet
Mon, 03 Feb 2014 15:19:07 +0100
changeset 56627 e88ad20035f4
parent 56611 aae87746f412
child 56628 7bbbd9393ce0
permissions -rw-r--r--
merged 'reconstructors' and 'proof methods'
smolkas@53727
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_reconstructor.ML
smolkas@53692
     2
    Author:     Jasmin Blanchette, TU Muenchen
smolkas@53692
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
smolkas@53692
     4
smolkas@53692
     5
Reconstructors.
smolkas@53692
     6
*)
smolkas@53692
     7
smolkas@53692
     8
signature SLEDGEHAMMER_RECONSTRUCTOR =
smolkas@53692
     9
sig
smolkas@53692
    10
  type stature = ATP_Problem_Generate.stature
smolkas@53692
    11
blanchet@56627
    12
  datatype proof_method =
blanchet@56627
    13
    Metis_Method of string option * string option |
blanchet@56627
    14
    Meson_Method |
blanchet@56627
    15
    SMT_Method |
blanchet@56627
    16
    Simp_Method |
blanchet@56627
    17
    Simp_Size_Method |
blanchet@56627
    18
    Auto_Method |
blanchet@56627
    19
    Fastforce_Method |
blanchet@56627
    20
    Force_Method |
blanchet@56627
    21
    Arith_Method |
blanchet@56627
    22
    Blast_Method |
blanchet@56627
    23
    Algebra_Method
smolkas@53692
    24
blanchet@56166
    25
  datatype play_outcome =
blanchet@56166
    26
    Played of Time.time |
blanchet@56166
    27
    Play_Timed_Out of Time.time |
blanchet@56166
    28
    Play_Failed |
blanchet@56166
    29
    Not_Played
smolkas@53692
    30
smolkas@53692
    31
  type minimize_command = string list -> string
blanchet@56166
    32
  type one_line_params =
blanchet@56627
    33
    (proof_method * play_outcome) * string * (string * stature) list * minimize_command * int * int
smolkas@53692
    34
smolkas@53692
    35
  val smtN : string
blanchet@56554
    36
blanchet@56627
    37
  val string_of_proof_method : proof_method -> string
blanchet@56627
    38
  val tac_of_proof_method : Proof.context -> thm list * thm list -> proof_method -> int -> tactic
blanchet@56553
    39
  val string_of_play_outcome : play_outcome -> string
blanchet@56611
    40
  val play_outcome_ord : play_outcome * play_outcome -> order
blanchet@56611
    41
blanchet@56553
    42
  val one_line_proof_text : int -> one_line_params -> string
blanchet@56627
    43
  val silence_proof_methods : bool -> Proof.context -> Proof.context
blanchet@55868
    44
end;
smolkas@53692
    45
smolkas@53692
    46
structure Sledgehammer_Reconstructor : SLEDGEHAMMER_RECONSTRUCTOR =
smolkas@53692
    47
struct
smolkas@53692
    48
blanchet@56170
    49
open ATP_Util
smolkas@53692
    50
open ATP_Problem_Generate
blanchet@56553
    51
open ATP_Proof_Reconstruct
smolkas@53692
    52
blanchet@56627
    53
datatype proof_method =
blanchet@56627
    54
  Metis_Method of string option * string option |
blanchet@56627
    55
  Meson_Method |
blanchet@56627
    56
  SMT_Method |
blanchet@56627
    57
  Simp_Method |
blanchet@56627
    58
  Simp_Size_Method |
blanchet@56627
    59
  Auto_Method |
blanchet@56627
    60
  Fastforce_Method |
blanchet@56627
    61
  Force_Method |
blanchet@56627
    62
  Arith_Method |
blanchet@56627
    63
  Blast_Method |
blanchet@56627
    64
  Algebra_Method
smolkas@53692
    65
blanchet@56166
    66
datatype play_outcome =
blanchet@56166
    67
  Played of Time.time |
blanchet@56166
    68
  Play_Timed_Out of Time.time |
blanchet@56166
    69
  Play_Failed |
blanchet@56166
    70
  Not_Played
smolkas@53692
    71
blanchet@56553
    72
type minimize_command = string list -> string
blanchet@56553
    73
type one_line_params =
blanchet@56627
    74
  (proof_method * play_outcome) * string * (string * stature) list * minimize_command * int * int
blanchet@56553
    75
blanchet@56553
    76
val smtN = "smt"
blanchet@56553
    77
blanchet@56627
    78
fun string_of_proof_method meth =
blanchet@56627
    79
  (case meth of
blanchet@56627
    80
    Metis_Method (NONE, NONE) => "metis"
blanchet@56627
    81
  | Metis_Method (type_enc_opt, lam_trans_opt) =>
blanchet@56627
    82
    "metis (" ^ commas (map_filter I [type_enc_opt, lam_trans_opt]) ^ ")"
blanchet@56627
    83
  | Meson_Method => "meson"
blanchet@56627
    84
  | SMT_Method => "smt"
blanchet@56627
    85
  | Simp_Method => "simp"
blanchet@56627
    86
  | Simp_Size_Method => "simp add: size_ne_size_imp_ne"
blanchet@56627
    87
  | Auto_Method => "auto"
blanchet@56627
    88
  | Fastforce_Method => "fastforce"
blanchet@56627
    89
  | Force_Method => "force"
blanchet@56627
    90
  | Arith_Method => "arith"
blanchet@56627
    91
  | Blast_Method => "blast"
blanchet@56627
    92
  | Algebra_Method => "algebra")
blanchet@56627
    93
blanchet@56627
    94
fun tac_of_proof_method ctxt (local_facts, global_facts) meth =
blanchet@56627
    95
  Method.insert_tac local_facts THEN'
blanchet@56627
    96
  (case meth of
blanchet@56627
    97
    Metis_Method (type_enc_opt, lam_trans_opt) =>
blanchet@56627
    98
    Metis_Tactic.metis_tac [type_enc_opt |> the_default partial_type_enc]
blanchet@56627
    99
      (the_default default_metis_lam_trans lam_trans_opt) ctxt global_facts
blanchet@56627
   100
  | SMT_Method => SMT_Solver.smt_tac ctxt global_facts
blanchet@56627
   101
  | Meson_Method => Meson.meson_tac ctxt global_facts
blanchet@56627
   102
  | _ =>
blanchet@56627
   103
    Method.insert_tac global_facts THEN'
blanchet@56627
   104
    (case meth of
blanchet@56627
   105
      Simp_Method => Simplifier.asm_full_simp_tac ctxt
blanchet@56627
   106
    | Simp_Size_Method =>
blanchet@56627
   107
      Simplifier.asm_full_simp_tac (Simplifier.add_simp @{thm size_ne_size_imp_ne} ctxt)
blanchet@56627
   108
    | Auto_Method => K (Clasimp.auto_tac ctxt)
blanchet@56627
   109
    | Fastforce_Method => Clasimp.fast_force_tac ctxt
blanchet@56627
   110
    | Force_Method => Clasimp.force_tac ctxt
blanchet@56627
   111
    | Arith_Method => Arith_Data.arith_tac ctxt
blanchet@56627
   112
    | Blast_Method => blast_tac ctxt
blanchet@56627
   113
    | Algebra_Method => Groebner.algebra_tac [] [] ctxt))
blanchet@56553
   114
blanchet@56170
   115
fun string_of_play_outcome (Played time) = string_of_ext_time (false, time)
blanchet@56170
   116
  | string_of_play_outcome (Play_Timed_Out time) = string_of_ext_time (true, time) ^ ", timed out"
blanchet@56170
   117
  | string_of_play_outcome Play_Failed = "failed"
blanchet@56170
   118
  | string_of_play_outcome _ = "unknown"
blanchet@56170
   119
blanchet@56611
   120
fun play_outcome_ord (Played time1, Played time2) =
blanchet@56611
   121
    int_ord (pairself Time.toMilliseconds (time1, time2))
blanchet@56611
   122
  | play_outcome_ord (Played _, _) = LESS
blanchet@56611
   123
  | play_outcome_ord (_, Played _) = GREATER
blanchet@56611
   124
  | play_outcome_ord (Not_Played, Not_Played) = EQUAL
blanchet@56611
   125
  | play_outcome_ord (Not_Played, _) = LESS
blanchet@56611
   126
  | play_outcome_ord (_, Not_Played) = GREATER
blanchet@56611
   127
  | play_outcome_ord (Play_Timed_Out time1, Play_Timed_Out time2) =
blanchet@56611
   128
    int_ord (pairself Time.toMilliseconds (time1, time2))
blanchet@56611
   129
  | play_outcome_ord (Play_Timed_Out _, _) = LESS
blanchet@56611
   130
  | play_outcome_ord (_, Play_Timed_Out _) = GREATER
blanchet@56611
   131
  | play_outcome_ord (Play_Failed, Play_Failed) = EQUAL
blanchet@56611
   132
blanchet@56553
   133
(* FIXME: Various bugs, esp. with "unfolding"
blanchet@56553
   134
fun unusing_chained_facts _ 0 = ""
blanchet@56553
   135
  | unusing_chained_facts used_chaineds num_chained =
blanchet@56553
   136
    if length used_chaineds = num_chained then ""
blanchet@56553
   137
    else if null used_chaineds then "(* using no facts *) "
blanchet@56553
   138
    else "(* using only " ^ space_implode " " used_chaineds ^ " *) "
blanchet@56553
   139
*)
smolkas@53692
   140
blanchet@56553
   141
fun apply_on_subgoal _ 1 = "by "
blanchet@56553
   142
  | apply_on_subgoal 1 _ = "apply "
blanchet@56553
   143
  | apply_on_subgoal i n =
blanchet@56553
   144
    "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
blanchet@56553
   145
blanchet@56553
   146
fun command_call name [] =
blanchet@56553
   147
    name |> not (Symbol_Pos.is_identifier name) ? enclose "(" ")"
blanchet@56553
   148
  | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
blanchet@56553
   149
blanchet@56627
   150
(* FIXME *)
blanchet@56627
   151
fun proof_method_command meth i n used_chaineds num_chained ss =
blanchet@56553
   152
  (* unusing_chained_facts used_chaineds num_chained ^ *)
blanchet@56627
   153
  apply_on_subgoal i n ^ command_call (string_of_proof_method meth) ss
blanchet@56553
   154
blanchet@56553
   155
fun show_time NONE = ""
blanchet@56553
   156
  | show_time (SOME ext_time) = " (" ^ string_of_ext_time ext_time ^ ")"
blanchet@56553
   157
blanchet@56553
   158
fun try_command_line banner time command =
blanchet@56553
   159
  banner ^ ": " ^ Active.sendback_markup [Markup.padding_command] command ^ show_time time ^ "."
blanchet@56553
   160
blanchet@56553
   161
fun minimize_line _ [] = ""
blanchet@56553
   162
  | minimize_line minimize_command ss =
blanchet@56553
   163
    (case minimize_command ss of
blanchet@56553
   164
      "" => ""
blanchet@56553
   165
    | command => "\nTo minimize: " ^ Active.sendback_markup [Markup.padding_command] command ^ ".")
blanchet@56553
   166
blanchet@56553
   167
fun split_used_facts facts =
blanchet@56553
   168
  facts
blanchet@56553
   169
  |> List.partition (fn (_, (sc, _)) => sc = Chained)
blanchet@56553
   170
  |> pairself (sort_distinct (string_ord o pairself fst))
blanchet@56553
   171
blanchet@56553
   172
fun one_line_proof_text num_chained
blanchet@56627
   173
    ((meth, play), banner, used_facts, minimize_command, subgoal, subgoal_count) =
blanchet@56553
   174
  let
blanchet@56553
   175
    val (chained, extra) = split_used_facts used_facts
blanchet@56553
   176
blanchet@56553
   177
    val (failed, ext_time) =
blanchet@56553
   178
      (case play of
blanchet@56553
   179
        Played time => (false, (SOME (false, time)))
blanchet@56553
   180
      | Play_Timed_Out time => (false, SOME (true, time))
blanchet@56553
   181
      | Play_Failed => (true, NONE)
blanchet@56553
   182
      | Not_Played => (false, NONE))
blanchet@56553
   183
blanchet@56553
   184
    val try_line =
blanchet@56553
   185
      map fst extra
blanchet@56627
   186
      |> proof_method_command meth subgoal subgoal_count (map fst chained) num_chained
blanchet@56553
   187
      |> (if failed then
blanchet@56553
   188
            enclose "One-line proof reconstruction failed: "
blanchet@56553
   189
              ".\n(Invoking \"sledgehammer\" with \"[strict]\" might solve this.)"
blanchet@56553
   190
          else
blanchet@56553
   191
            try_command_line banner ext_time)
blanchet@56553
   192
  in
blanchet@56553
   193
    try_line ^ minimize_line minimize_command (map fst (extra @ chained))
blanchet@56553
   194
  end
smolkas@53692
   195
blanchet@56627
   196
(* Makes proof methods as silent as possible. The "set_visible" calls suppresses "Unification bound
blanchet@56627
   197
   exceeded" warnings and the like. *)
blanchet@56627
   198
fun silence_proof_methods debug =
blanchet@56522
   199
  Try0.silence_methods debug
blanchet@56512
   200
  #> Config.put SMT_Config.verbose debug
blanchet@56512
   201
blanchet@55868
   202
end;