src/HOL/Tools/Sledgehammer/sledgehammer_isar.ML
author wenzelm
Sat Apr 02 23:29:05 2016 +0200 (2016-04-02 ago)
changeset 62826 eb94e570c1a4
parent 62220 0e17a97234bd
child 63518 ae8fd6fe63a1
permissions -rw-r--r--
prefer infix operations;
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     3     Author:     Steffen Juilf Smolka, TU Muenchen
     4 
     5 Isar proof reconstruction from ATP proofs.
     6 *)
     7 
     8 signature SLEDGEHAMMER_ISAR =
     9 sig
    10   type atp_step_name = ATP_Proof.atp_step_name
    11   type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
    12   type 'a atp_proof = 'a ATP_Proof.atp_proof
    13   type stature = ATP_Problem_Generate.stature
    14   type one_line_params = Sledgehammer_Proof_Methods.one_line_params
    15 
    16   val trace : bool Config.T
    17 
    18   type isar_params =
    19     bool * (string option * string option) * Time.time * real option * bool * bool
    20     * (term, string) atp_step list * thm
    21 
    22   val proof_text : Proof.context -> bool -> bool option -> bool option -> (unit -> isar_params) ->
    23     int -> one_line_params -> string
    24 end;
    25 
    26 structure Sledgehammer_Isar : SLEDGEHAMMER_ISAR =
    27 struct
    28 
    29 open ATP_Util
    30 open ATP_Problem
    31 open ATP_Proof
    32 open ATP_Proof_Reconstruct
    33 open ATP_Waldmeister
    34 open Sledgehammer_Util
    35 open Sledgehammer_Proof_Methods
    36 open Sledgehammer_Isar_Proof
    37 open Sledgehammer_Isar_Preplay
    38 open Sledgehammer_Isar_Compress
    39 open Sledgehammer_Isar_Minimize
    40 
    41 structure String_Redirect = ATP_Proof_Redirect(
    42   type key = atp_step_name
    43   val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
    44   val string_of = fst)
    45 
    46 open String_Redirect
    47 
    48 val trace = Attrib.setup_config_bool @{binding sledgehammer_isar_trace} (K false)
    49 
    50 val e_definition_rule = "definition"
    51 val e_skolemize_rule = "skolemize"
    52 val leo2_extcnf_forall_neg_rule = "extcnf_forall_neg"
    53 val pirate_datatype_rule = "DT"
    54 val satallax_skolemize_rule = "tab_ex"
    55 val vampire_skolemisation_rule = "skolemisation"
    56 val veriT_la_generic_rule = "la_generic"
    57 val veriT_simp_arith_rule = "simp_arith"
    58 val veriT_tmp_ite_elim_rule = "tmp_ite_elim"
    59 val veriT_tmp_skolemize_rule = "tmp_skolemize"
    60 val z3_skolemize_rule = Z3_Proof.string_of_rule Z3_Proof.Skolemize
    61 val z3_th_lemma_rule_prefix = Z3_Proof.string_of_rule (Z3_Proof.Th_Lemma "")
    62 val zipperposition_cnf_rule = "cnf"
    63 
    64 val skolemize_rules =
    65   [e_definition_rule, e_skolemize_rule, leo2_extcnf_forall_neg_rule, satallax_skolemize_rule,
    66    spass_skolemize_rule, vampire_skolemisation_rule, veriT_tmp_ite_elim_rule,
    67    veriT_tmp_skolemize_rule, waldmeister_skolemize_rule, z3_skolemize_rule, zipperposition_cnf_rule]
    68 
    69 fun is_ext_rule rule = (rule = leo2_extcnf_equal_neg_rule)
    70 val is_maybe_ext_rule = is_ext_rule orf String.isPrefix satallax_tab_rule_prefix
    71 
    72 val is_skolemize_rule = member (op =) skolemize_rules
    73 fun is_arith_rule rule =
    74   String.isPrefix z3_th_lemma_rule_prefix rule orelse rule = veriT_simp_arith_rule orelse
    75   rule = veriT_la_generic_rule
    76 val is_datatype_rule = String.isPrefix pirate_datatype_rule
    77 
    78 fun raw_label_of_num num = (num, 0)
    79 
    80 fun label_of_clause [(num, _)] = raw_label_of_num num
    81   | label_of_clause c = (space_implode "___" (map (fst o raw_label_of_num o fst) c), 0)
    82 
    83 fun add_global_fact ss = apsnd (union (op =) ss)
    84 
    85 fun add_fact_of_dependency [(_, ss as _ :: _)] = add_global_fact ss
    86   | add_fact_of_dependency names = apfst (insert (op =) (label_of_clause names))
    87 
    88 fun add_line_pass1 (line as (name, role, t, rule, [])) lines =
    89     (* No dependencies: lemma (for Z3), fact, conjecture, or (for Vampire) internal facts or
    90        definitions. *)
    91     if role = Conjecture orelse role = Negated_Conjecture then
    92       line :: lines
    93     else if t aconv @{prop True} then
    94       map (replace_dependencies_in_line (name, [])) lines
    95     else if role = Lemma orelse role = Hypothesis orelse is_arith_rule rule then
    96       line :: lines
    97     else if role = Axiom then
    98       lines (* axioms (facts) need no proof lines *)
    99     else
   100       map (replace_dependencies_in_line (name, [])) lines
   101   | add_line_pass1 line lines = line :: lines
   102 
   103 fun add_lines_pass2 res [] = rev res
   104   | add_lines_pass2 res ((line as (name, role, t, rule, deps)) :: lines) =
   105     let
   106       fun normalize role =
   107         role = Conjecture ? (HOLogic.dest_Trueprop #> s_not #> HOLogic.mk_Trueprop)
   108 
   109       val norm_t = normalize role t
   110       val is_duplicate =
   111         exists (fn (prev_name, prev_role, prev_t, _, _) =>
   112             (prev_role = Hypothesis andalso prev_t aconv t) orelse
   113             (member (op =) deps prev_name andalso
   114              Term.aconv_untyped (normalize prev_role prev_t, norm_t)))
   115           res
   116 
   117       fun looks_boring () = t aconv @{prop False} orelse length deps < 2
   118 
   119       fun is_skolemizing_line (_, _, _, rule', deps') =
   120         is_skolemize_rule rule' andalso member (op =) deps' name
   121 
   122       fun is_before_skolemize_rule () = exists is_skolemizing_line lines
   123     in
   124       if is_duplicate orelse
   125           (role = Plain andalso not (is_skolemize_rule rule) andalso
   126            not (is_ext_rule rule) andalso not (is_arith_rule rule) andalso
   127            not (is_datatype_rule rule) andalso not (null lines) andalso looks_boring () andalso
   128            not (is_before_skolemize_rule ())) then
   129         add_lines_pass2 res (map (replace_dependencies_in_line (name, deps)) lines)
   130       else
   131         add_lines_pass2 (line :: res) lines
   132     end
   133 
   134 type isar_params =
   135   bool * (string option * string option) * Time.time * real option * bool * bool
   136   * (term, string) atp_step list * thm
   137 
   138 val basic_systematic_methods = [Metis_Method (NONE, NONE), Meson_Method, Blast_Method, SATx_Method]
   139 val basic_simp_based_methods = [Auto_Method, Simp_Method, Fastforce_Method, Force_Method]
   140 val basic_arith_methods = [Linarith_Method, Presburger_Method, Algebra_Method]
   141 
   142 val arith_methods = basic_arith_methods @ basic_simp_based_methods @ basic_systematic_methods
   143 val datatype_methods = [Simp_Method, Simp_Size_Method]
   144 val systematic_methods =
   145   basic_systematic_methods @ basic_arith_methods @ basic_simp_based_methods @
   146   [Metis_Method (SOME full_typesN, NONE), Metis_Method (SOME no_typesN, NONE)]
   147 val rewrite_methods = basic_simp_based_methods @ basic_systematic_methods @ basic_arith_methods
   148 val skolem_methods = Moura_Method :: systematic_methods
   149 
   150 fun isar_proof_text ctxt debug num_chained isar_proofs smt_proofs isar_params
   151     (one_line_params as ((used_facts, (_, one_line_play)), banner, subgoal, subgoal_count)) =
   152   let
   153     val _ = if debug then writeln "Constructing Isar proof..." else ()
   154 
   155     fun generate_proof_text () =
   156       let
   157         val (verbose, alt_metis_args, preplay_timeout, compress, try0, minimize, atp_proof0, goal) =
   158           isar_params ()
   159       in
   160         if null atp_proof0 then
   161           one_line_proof_text ctxt 0 one_line_params
   162         else
   163           let
   164             val systematic_methods' = insert (op =) (Metis_Method alt_metis_args) systematic_methods
   165 
   166             fun massage_methods (meths as meth :: _) =
   167               if not try0 then [meth]
   168               else if smt_proofs = SOME true then SMT_Method :: meths
   169               else meths
   170 
   171             val (params, _, concl_t) = strip_subgoal goal subgoal ctxt
   172             val fixes = map (fn (s, T) => (Binding.name s, SOME T, NoSyn)) params
   173             val ctxt = ctxt |> Variable.set_body false |> Proof_Context.add_fixes fixes |> snd
   174 
   175             val do_preplay = preplay_timeout <> Time.zeroTime
   176             val compress =
   177               (case compress of
   178                 NONE => if isar_proofs = NONE andalso do_preplay then 1000.0 else 10.0
   179               | SOME n => n)
   180 
   181             fun is_fixed ctxt = Variable.is_declared ctxt orf Name.is_skolem
   182             fun skolems_of ctxt t = Term.add_frees t [] |> filter_out (is_fixed ctxt o fst) |> rev
   183 
   184             fun get_role keep_role ((num, _), role, t, rule, _) =
   185               if keep_role role then SOME ((raw_label_of_num num, t), rule) else NONE
   186 
   187             val atp_proof =
   188               fold_rev add_line_pass1 atp_proof0 []
   189               |> add_lines_pass2 []
   190 
   191             val conjs =
   192               map_filter (fn (name, role, _, _, _) =>
   193                   if member (op =) [Conjecture, Negated_Conjecture] role then SOME name else NONE)
   194                 atp_proof
   195             val assms = map_filter (Option.map fst o get_role (curry (op =) Hypothesis)) atp_proof
   196 
   197             fun add_lemma ((l, t), rule) ctxt =
   198               let
   199                 val (skos, meths) =
   200                   (if is_skolemize_rule rule then (skolems_of ctxt t, skolem_methods)
   201                    else if is_arith_rule rule then ([], arith_methods)
   202                    else ([], rewrite_methods))
   203                   ||> massage_methods
   204               in
   205                 (Prove ([], skos, l, t, [], ([], []), meths, ""),
   206                  ctxt |> not (null skos) ? (Variable.add_fixes (map fst skos) #> snd))
   207               end
   208 
   209             val (lems, _) =
   210               fold_map add_lemma (map_filter (get_role (curry (op =) Lemma)) atp_proof) ctxt
   211 
   212             val bot = #1 (List.last atp_proof)
   213 
   214             val refute_graph =
   215               atp_proof
   216               |> map (fn (name, _, _, _, from) => (from, name))
   217               |> make_refute_graph bot
   218               |> fold (Atom_Graph.default_node o rpair ()) conjs
   219 
   220             val axioms = axioms_of_refute_graph refute_graph conjs
   221 
   222             val tainted = tainted_atoms_of_refute_graph refute_graph conjs
   223             val is_clause_tainted = exists (member (op =) tainted)
   224             val steps =
   225               Symtab.empty
   226               |> fold (fn (name as (s, _), role, t, rule, _) =>
   227                   Symtab.update_new (s, (rule, t
   228                     |> (if is_clause_tainted [name] then
   229                           HOLogic.dest_Trueprop
   230                           #> role <> Conjecture ? s_not
   231                           #> fold exists_of (map Var (Term.add_vars t []))
   232                           #> HOLogic.mk_Trueprop
   233                         else
   234                           I))))
   235                 atp_proof
   236 
   237             fun is_referenced_in_step _ (Let _) = false
   238               | is_referenced_in_step l (Prove (_, _, _, _, subs, (ls, _), _, _)) =
   239                 member (op =) ls l orelse exists (is_referenced_in_proof l) subs
   240             and is_referenced_in_proof l (Proof (_, _, steps)) =
   241               exists (is_referenced_in_step l) steps
   242 
   243             fun insert_lemma_in_step lem
   244                 (step as Prove (qs, fix, l, t, subs, (ls, gs), meths, comment)) =
   245               let val l' = the (label_of_isar_step lem) in
   246                 if member (op =) ls l' then
   247                   [lem, step]
   248                 else
   249                   let val refs = map (is_referenced_in_proof l') subs in
   250                     if length (filter I refs) = 1 then
   251                       let
   252                         val subs' = map2 (fn false => I | true => insert_lemma_in_proof lem) refs
   253                           subs
   254                       in
   255                         [Prove (qs, fix, l, t, subs', (ls, gs), meths, comment)]
   256                       end
   257                     else
   258                       [lem, step]
   259                   end
   260               end
   261             and insert_lemma_in_steps lem [] = [lem]
   262               | insert_lemma_in_steps lem (step :: steps) =
   263                 if is_referenced_in_step (the (label_of_isar_step lem)) step then
   264                   insert_lemma_in_step lem step @ steps
   265                 else
   266                   step :: insert_lemma_in_steps lem steps
   267             and insert_lemma_in_proof lem (Proof (fix, assms, steps)) =
   268               Proof (fix, assms, insert_lemma_in_steps lem steps)
   269 
   270             val rule_of_clause_id = fst o the o Symtab.lookup steps o fst
   271 
   272             val finish_off = close_form #> rename_bound_vars
   273 
   274             fun prop_of_clause [(num, _)] = Symtab.lookup steps num |> the |> snd |> finish_off
   275               | prop_of_clause names =
   276                 let
   277                   val lits =
   278                     map (HOLogic.dest_Trueprop o snd) (map_filter (Symtab.lookup steps o fst) names)
   279                 in
   280                   (case List.partition (can HOLogic.dest_not) lits of
   281                     (negs as _ :: _, pos as _ :: _) =>
   282                     s_imp (Library.foldr1 s_conj (map HOLogic.dest_not negs),
   283                       Library.foldr1 s_disj pos)
   284                   | _ => fold (curry s_disj) lits @{term False})
   285                 end
   286                 |> HOLogic.mk_Trueprop |> finish_off
   287 
   288             fun maybe_show outer c = if outer andalso eq_set (op =) (c, conjs) then [Show] else []
   289 
   290             fun isar_steps outer predecessor accum [] =
   291                 accum
   292                 |> (if tainted = [] then
   293                       (* e.g., trivial, empty proof by Z3 *)
   294                       cons (Prove (if outer then [Show] else [], [], no_label, concl_t, [],
   295                         sort_facts (the_list predecessor, []), massage_methods systematic_methods',
   296                         ""))
   297                     else
   298                       I)
   299                 |> rev
   300               | isar_steps outer _ accum (Have (id, (gamma, c)) :: infs) =
   301                 let
   302                   val l = label_of_clause c
   303                   val t = prop_of_clause c
   304                   val rule = rule_of_clause_id id
   305                   val skolem = is_skolemize_rule rule
   306 
   307                   val deps = ([], [])
   308                     |> fold add_fact_of_dependency gamma
   309                     |> is_maybe_ext_rule rule ? add_global_fact [short_thm_name ctxt ext]
   310                     |> sort_facts
   311                   val meths =
   312                     (if skolem then skolem_methods
   313                      else if is_arith_rule rule then arith_methods
   314                      else if is_datatype_rule rule then datatype_methods
   315                      else systematic_methods')
   316                     |> massage_methods
   317 
   318                   fun prove sub facts = Prove (maybe_show outer c, [], l, t, sub, facts, meths, "")
   319                   fun steps_of_rest step = isar_steps outer (SOME l) (step :: accum) infs
   320                 in
   321                   if is_clause_tainted c then
   322                     (case gamma of
   323                       [g] =>
   324                       if skolem andalso is_clause_tainted g then
   325                         let
   326                           val skos = skolems_of ctxt (prop_of_clause g)
   327                           val subproof = Proof (skos, [], rev accum)
   328                         in
   329                           isar_steps outer (SOME l) [prove [subproof] ([], [])] infs
   330                         end
   331                       else
   332                         steps_of_rest (prove [] deps)
   333                     | _ => steps_of_rest (prove [] deps))
   334                   else
   335                     steps_of_rest
   336                       (if skolem then
   337                          (case skolems_of ctxt t of
   338                            [] => prove [] deps
   339                          | skos => Prove ([], skos, l, t, [], deps, meths, ""))
   340                        else
   341                          prove [] deps)
   342                 end
   343               | isar_steps outer predecessor accum (Cases cases :: infs) =
   344                 let
   345                   fun isar_case (c, subinfs) =
   346                     isar_proof false [] [(label_of_clause c, prop_of_clause c)] [] subinfs
   347                   val c = succedent_of_cases cases
   348                   val l = label_of_clause c
   349                   val t = prop_of_clause c
   350                   val step =
   351                     Prove (maybe_show outer c, [], l, t,
   352                       map isar_case (filter_out (null o snd) cases),
   353                       sort_facts (the_list predecessor, []), massage_methods systematic_methods',
   354                       "")
   355                 in
   356                   isar_steps outer (SOME l) (step :: accum) infs
   357                 end
   358             and isar_proof outer fix assms lems infs =
   359               Proof (fix, assms,
   360                 fold_rev insert_lemma_in_steps lems (isar_steps outer NONE [] infs))
   361 
   362             val trace = Config.get ctxt trace
   363 
   364             val canonical_isar_proof =
   365               refute_graph
   366               |> trace ? tap (tracing o prefix "Refute graph:\n" o string_of_refute_graph)
   367               |> redirect_graph axioms tainted bot
   368               |> trace ? tap (tracing o prefix "Direct proof:\n" o string_of_direct_proof)
   369               |> isar_proof true params assms lems
   370               |> postprocess_isar_proof_remove_show_stuttering
   371               |> postprocess_isar_proof_remove_unreferenced_steps I
   372               |> relabel_isar_proof_canonically
   373 
   374             val ctxt = ctxt |> enrich_context_with_local_facts canonical_isar_proof
   375 
   376             val preplay_data = Unsynchronized.ref Canonical_Label_Tab.empty
   377 
   378             val _ = fold_isar_steps (fn meth =>
   379                 K (set_preplay_outcomes_of_isar_step ctxt preplay_timeout preplay_data meth []))
   380               (steps_of_isar_proof canonical_isar_proof) ()
   381 
   382             fun str_of_preplay_outcome outcome =
   383               if Lazy.is_finished outcome then string_of_play_outcome (Lazy.force outcome) else "?"
   384             fun str_of_meth l meth =
   385               string_of_proof_method ctxt [] meth ^ " " ^
   386               str_of_preplay_outcome
   387                 (preplay_outcome_of_isar_step_for_method (!preplay_data) l meth)
   388             fun comment_of l = map (str_of_meth l) #> commas
   389 
   390             fun trace_isar_proof label proof =
   391               if trace then
   392                 tracing (timestamp () ^ "\n" ^ label ^ ":\n\n" ^
   393                   string_of_isar_proof ctxt subgoal subgoal_count
   394                     (comment_isar_proof comment_of proof) ^ "\n")
   395               else
   396                 ()
   397 
   398             fun comment_of l (meth :: _) =
   399               (case (verbose,
   400                   Lazy.force (preplay_outcome_of_isar_step_for_method (!preplay_data) l meth)) of
   401                 (false, Played _) => ""
   402               | (_, outcome) => string_of_play_outcome outcome)
   403 
   404             val (play_outcome, isar_proof) =
   405               canonical_isar_proof
   406               |> tap (trace_isar_proof "Original")
   407               |> compress_isar_proof ctxt compress preplay_timeout preplay_data
   408               |> tap (trace_isar_proof "Compressed")
   409               |> postprocess_isar_proof_remove_unreferenced_steps
   410                    (keep_fastest_method_of_isar_step (!preplay_data)
   411                     #> minimize ? minimize_isar_step_dependencies ctxt preplay_data)
   412               |> tap (trace_isar_proof "Minimized")
   413               |> `(preplay_outcome_of_isar_proof (!preplay_data))
   414               ||> (comment_isar_proof comment_of
   415                    #> chain_isar_proof
   416                    #> kill_useless_labels_in_isar_proof
   417                    #> relabel_isar_proof_nicely
   418                    #> rationalize_obtains_in_isar_proofs ctxt)
   419           in
   420             (case (num_chained, add_isar_steps (steps_of_isar_proof isar_proof) 0) of
   421               (0, 1) =>
   422               one_line_proof_text ctxt 0
   423                 (if play_outcome_ord (play_outcome, one_line_play) = LESS then
   424                    (case isar_proof of
   425                      Proof (_, _, [Prove (_, _, _, _, _, (_, gfs), meth :: _, _)]) =>
   426                      let
   427                        val used_facts' = filter (fn (s, (sc, _)) =>
   428                          member (op =) gfs s andalso sc <> Chained) used_facts
   429                      in
   430                        ((used_facts', (meth, play_outcome)), banner, subgoal, subgoal_count)
   431                      end)
   432                  else
   433                    one_line_params) ^
   434               (if isar_proofs = SOME true then "\n(No Isar proof available.)" else "")
   435             | (_, num_steps) =>
   436               let
   437                 val msg =
   438                   (if verbose then [string_of_int num_steps ^ " step" ^ plural_s num_steps]
   439                    else []) @
   440                   (if do_preplay then [string_of_play_outcome play_outcome] else [])
   441               in
   442                 one_line_proof_text ctxt 0 one_line_params ^
   443                 "\n\nIsar proof" ^ (commas msg |> not (null msg) ? enclose " (" ")") ^ ":\n" ^
   444                 Active.sendback_markup [Markup.padding_command]
   445                   (string_of_isar_proof ctxt subgoal subgoal_count isar_proof)
   446               end)
   447           end
   448       end
   449   in
   450     if debug then
   451       generate_proof_text ()
   452     else
   453       (case try generate_proof_text () of
   454         SOME s => s
   455       | NONE =>
   456         one_line_proof_text ctxt 0 one_line_params ^
   457         (if isar_proofs = SOME true then "\nWarning: Isar proof construction failed." else ""))
   458   end
   459 
   460 fun isar_proof_would_be_a_good_idea smt_proofs (meth, play) =
   461   (case play of
   462     Played _ => meth = SMT_Method andalso smt_proofs <> SOME true
   463   | Play_Timed_Out time => time > Time.zeroTime
   464   | Play_Failed => true)
   465 
   466 fun proof_text ctxt debug isar_proofs smt_proofs isar_params num_chained
   467     (one_line_params as ((_, preplay), _, _, _)) =
   468   (if isar_proofs = SOME true orelse
   469       (isar_proofs = NONE andalso isar_proof_would_be_a_good_idea smt_proofs preplay) then
   470      isar_proof_text ctxt debug num_chained isar_proofs smt_proofs isar_params
   471    else
   472      one_line_proof_text ctxt num_chained) one_line_params
   473 
   474 end;