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