src/HOL/Tools/ATP/atp_proof_reconstruct.ML
author blanchet
Mon, 23 Jul 2012 15:32:30 +0200
changeset 49453 3e45c98fe127
parent 49150 a44f34694406
child 49554 0debf65972c7
permissions -rw-r--r--
distinguish between recursive and nonrecursive definitions + clean up typedef dependencies in MaSh
     1 (*  Title:      HOL/Tools/ATP/atp_proof_reconstruct.ML
     2     Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
     3     Author:     Claire Quigley, Cambridge University Computer Laboratory
     4     Author:     Jasmin Blanchette, TU Muenchen
     5 
     6 Proof reconstruction from ATP proofs.
     7 *)
     8 
     9 signature ATP_PROOF_RECONSTRUCT =
    10 sig
    11   type ('a, 'b) ho_term = ('a, 'b) ATP_Problem.ho_term
    12   type ('a, 'b, 'c, 'd) formula = ('a, 'b, 'c, 'd) ATP_Problem.formula
    13   type 'a proof = 'a ATP_Proof.proof
    14   type stature = ATP_Problem_Generate.stature
    15 
    16   datatype reconstructor =
    17     Metis of string * string |
    18     SMT
    19 
    20   datatype play =
    21     Played of reconstructor * Time.time |
    22     Trust_Playable of reconstructor * Time.time option |
    23     Failed_to_Play of reconstructor
    24 
    25   type minimize_command = string list -> string
    26   type one_line_params =
    27     play * string * (string * stature) list * minimize_command * int * int
    28   type isar_params =
    29     bool * int * string Symtab.table * (string * stature) list vector
    30     * int Symtab.table * string proof * thm
    31 
    32   val metisN : string
    33   val smtN : string
    34   val full_typesN : string
    35   val partial_typesN : string
    36   val no_typesN : string
    37   val really_full_type_enc : string
    38   val full_type_enc : string
    39   val partial_type_enc : string
    40   val no_type_enc : string
    41   val full_type_encs : string list
    42   val partial_type_encs : string list
    43   val metis_default_lam_trans : string
    44   val metis_call : string -> string -> string
    45   val string_for_reconstructor : reconstructor -> string
    46   val used_facts_in_atp_proof :
    47     Proof.context -> (string * stature) list vector -> string proof
    48     -> (string * stature) list
    49   val lam_trans_from_atp_proof : string proof -> string -> string
    50   val is_typed_helper_used_in_atp_proof : string proof -> bool
    51   val used_facts_in_unsound_atp_proof :
    52     Proof.context -> (string * stature) list vector -> 'a proof
    53     -> string list option
    54   val unalias_type_enc : string -> string list
    55   val one_line_proof_text : one_line_params -> string
    56   val make_tvar : string -> typ
    57   val make_tfree : Proof.context -> string -> typ
    58   val term_from_atp :
    59     Proof.context -> bool -> int Symtab.table -> typ option
    60     -> (string, string) ho_term -> term
    61   val prop_from_atp :
    62     Proof.context -> bool -> int Symtab.table
    63     -> (string, string, (string, string) ho_term, string) formula -> term
    64   val isar_proof_text :
    65     Proof.context -> bool -> isar_params -> one_line_params -> string
    66   val proof_text :
    67     Proof.context -> bool -> isar_params -> one_line_params -> string
    68 end;
    69 
    70 structure ATP_Proof_Reconstruct : ATP_PROOF_RECONSTRUCT =
    71 struct
    72 
    73 open ATP_Util
    74 open ATP_Problem
    75 open ATP_Proof
    76 open ATP_Problem_Generate
    77 
    78 structure String_Redirect = ATP_Proof_Redirect(
    79     type key = step_name
    80     val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s')
    81     val string_of = fst)
    82 
    83 open String_Redirect
    84 
    85 datatype reconstructor =
    86   Metis of string * string |
    87   SMT
    88 
    89 datatype play =
    90   Played of reconstructor * Time.time |
    91   Trust_Playable of reconstructor * Time.time option |
    92   Failed_to_Play of reconstructor
    93 
    94 type minimize_command = string list -> string
    95 type one_line_params =
    96   play * string * (string * stature) list * minimize_command * int * int
    97 type isar_params =
    98   bool * int * string Symtab.table * (string * stature) list vector
    99   * int Symtab.table * string proof * thm
   100 
   101 val metisN = "metis"
   102 val smtN = "smt"
   103 
   104 val full_typesN = "full_types"
   105 val partial_typesN = "partial_types"
   106 val no_typesN = "no_types"
   107 
   108 val really_full_type_enc = "mono_tags"
   109 val full_type_enc = "poly_guards_query"
   110 val partial_type_enc = "poly_args"
   111 val no_type_enc = "erased"
   112 
   113 val full_type_encs = [full_type_enc, really_full_type_enc]
   114 val partial_type_encs = partial_type_enc :: full_type_encs
   115 
   116 val type_enc_aliases =
   117   [(full_typesN, full_type_encs),
   118    (partial_typesN, partial_type_encs),
   119    (no_typesN, [no_type_enc])]
   120 
   121 fun unalias_type_enc s =
   122   AList.lookup (op =) type_enc_aliases s |> the_default [s]
   123 
   124 val metis_default_lam_trans = combsN
   125 
   126 fun metis_call type_enc lam_trans =
   127   let
   128     val type_enc =
   129       case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases
   130                       type_enc of
   131         [alias] => alias
   132       | _ => type_enc
   133     val opts = [] |> type_enc <> partial_typesN ? cons type_enc
   134                   |> lam_trans <> metis_default_lam_trans ? cons lam_trans
   135   in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end
   136 
   137 fun string_for_reconstructor (Metis (type_enc, lam_trans)) =
   138     metis_call type_enc lam_trans
   139   | string_for_reconstructor SMT = smtN
   140 
   141 fun find_first_in_list_vector vec key =
   142   Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
   143                  | (_, value) => value) NONE vec
   144 
   145 val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
   146 
   147 fun resolve_one_named_fact fact_names s =
   148   case try (unprefix fact_prefix) s of
   149     SOME s' =>
   150     let val s' = s' |> unprefix_fact_number |> unascii_of in
   151       s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
   152     end
   153   | NONE => NONE
   154 fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
   155 fun is_fact fact_names = not o null o resolve_fact fact_names
   156 
   157 fun resolve_one_named_conjecture s =
   158   case try (unprefix conjecture_prefix) s of
   159     SOME s' => Int.fromString s'
   160   | NONE => NONE
   161 
   162 val resolve_conjecture = map_filter resolve_one_named_conjecture
   163 val is_conjecture = not o null o resolve_conjecture
   164 
   165 fun is_axiom_used_in_proof pred =
   166   exists (fn Inference_Step ((_, ss), _, _, []) => exists pred ss | _ => false)
   167 
   168 val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
   169 
   170 val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
   171 
   172 (* overapproximation (good enough) *)
   173 fun is_lam_lifted s =
   174   String.isPrefix fact_prefix s andalso
   175   String.isSubstring ascii_of_lam_fact_prefix s
   176 
   177 fun lam_trans_from_atp_proof atp_proof default =
   178   case (is_axiom_used_in_proof is_combinator_def atp_proof,
   179         is_axiom_used_in_proof is_lam_lifted atp_proof) of
   180     (false, false) => default
   181   | (false, true) => liftingN
   182 (*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
   183   | (true, _) => combsN
   184 
   185 val is_typed_helper_name =
   186   String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
   187 fun is_typed_helper_used_in_atp_proof atp_proof =
   188   is_axiom_used_in_proof is_typed_helper_name atp_proof
   189 
   190 val leo2_ext = "extcnf_equal_neg"
   191 val leo2_unfold_def = "unfold_def"
   192 
   193 val isa_ext = Thm.get_name_hint @{thm ext}
   194 val isa_short_ext = Long_Name.base_name isa_ext
   195 
   196 fun ext_name ctxt =
   197   if Thm.eq_thm_prop (@{thm ext},
   198          singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
   199     isa_short_ext
   200   else
   201     isa_ext
   202 
   203 fun add_non_rec_defs fact_names accum =
   204   Vector.foldl
   205       (fn (facts, facts') =>
   206           union (op =)
   207                 (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
   208                 facts')
   209       accum fact_names
   210 
   211 fun add_fact ctxt fact_names (Inference_Step ((_, ss), _, rule, deps)) =
   212     (if rule = leo2_ext then
   213        insert (op =) (ext_name ctxt, (Global, General))
   214      else if rule = leo2_unfold_def then
   215        (* LEO 1.3.3 does not record definitions properly, leading to missing
   216          dependencies in the TSTP proof. Remove the next line once this is
   217          fixed. *)
   218        add_non_rec_defs fact_names
   219      else if rule = satallax_unsat_coreN then
   220        (fn [] =>
   221            (* Satallax doesn't include definitions in its unsatisfiable cores,
   222               so we assume the worst and include them all here. *)
   223            [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
   224          | facts => facts)
   225      else
   226        I)
   227     #> (if null deps then union (op =) (resolve_fact fact_names ss)
   228         else I)
   229   | add_fact _ _ _ = I
   230 
   231 fun used_facts_in_atp_proof ctxt fact_names atp_proof =
   232   if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
   233   else fold (add_fact ctxt fact_names) atp_proof []
   234 
   235 fun used_facts_in_unsound_atp_proof _ _ [] = NONE
   236   | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
   237     let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
   238       if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
   239          not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
   240         SOME (map fst used_facts)
   241       else
   242         NONE
   243     end
   244 
   245 
   246 (** Soft-core proof reconstruction: one-liners **)
   247 
   248 fun string_for_label (s, num) = s ^ string_of_int num
   249 
   250 fun show_time NONE = ""
   251   | show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")"
   252 
   253 fun apply_on_subgoal _ 1 = "by "
   254   | apply_on_subgoal 1 _ = "apply "
   255   | apply_on_subgoal i n =
   256     "prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n
   257 fun command_call name [] =
   258     name |> not (Lexicon.is_identifier name) ? enclose "(" ")"
   259   | command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")"
   260 fun try_command_line banner time command =
   261   banner ^ ": " ^ Markup.markup Isabelle_Markup.sendback command ^ show_time time ^ "."
   262 fun using_labels [] = ""
   263   | using_labels ls =
   264     "using " ^ space_implode " " (map string_for_label ls) ^ " "
   265 fun reconstructor_command reconstr i n (ls, ss) =
   266   using_labels ls ^ apply_on_subgoal i n ^
   267   command_call (string_for_reconstructor reconstr) ss
   268 fun minimize_line _ [] = ""
   269   | minimize_line minimize_command ss =
   270     case minimize_command ss of
   271       "" => ""
   272     | command =>
   273       "\nTo minimize: " ^ Markup.markup Isabelle_Markup.sendback command ^ "."
   274 
   275 fun split_used_facts facts =
   276   facts |> List.partition (fn (_, (sc, _)) => sc = Chained)
   277         |> pairself (sort_distinct (string_ord o pairself fst))
   278 
   279 fun one_line_proof_text (preplay, banner, used_facts, minimize_command,
   280                          subgoal, subgoal_count) =
   281   let
   282     val (chained, extra) = split_used_facts used_facts
   283     val (failed, reconstr, ext_time) =
   284       case preplay of
   285         Played (reconstr, time) => (false, reconstr, (SOME (false, time)))
   286       | Trust_Playable (reconstr, time) =>
   287         (false, reconstr,
   288          case time of
   289            NONE => NONE
   290          | SOME time =>
   291            if time = Time.zeroTime then NONE else SOME (true, time))
   292       | Failed_to_Play reconstr => (true, reconstr, NONE)
   293     val try_line =
   294       ([], map fst extra)
   295       |> reconstructor_command reconstr subgoal subgoal_count
   296       |> (if failed then
   297             enclose "One-line proof reconstruction failed: "
   298                      ".\n(Invoking \"sledgehammer\" with \"[strict]\" might \
   299                      \solve this.)"
   300           else
   301             try_command_line banner ext_time)
   302   in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end
   303 
   304 (** Hard-core proof reconstruction: structured Isar proofs **)
   305 
   306 fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda v t
   307 fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda v t
   308 
   309 fun make_tvar s = TVar (("'" ^ s, 0), HOLogic.typeS)
   310 fun make_tfree ctxt w =
   311   let val ww = "'" ^ w in
   312     TFree (ww, the_default HOLogic.typeS (Variable.def_sort ctxt (ww, ~1)))
   313   end
   314 
   315 val indent_size = 2
   316 val no_label = ("", ~1)
   317 
   318 val raw_prefix = "x"
   319 val assum_prefix = "a"
   320 val have_prefix = "f"
   321 
   322 fun raw_label_for_name (num, ss) =
   323   case resolve_conjecture ss of
   324     [j] => (conjecture_prefix, j)
   325   | _ => (raw_prefix ^ ascii_of num, 0)
   326 
   327 (**** INTERPRETATION OF TSTP SYNTAX TREES ****)
   328 
   329 exception HO_TERM of (string, string) ho_term list
   330 exception FORMULA of
   331     (string, string, (string, string) ho_term, string) formula list
   332 exception SAME of unit
   333 
   334 (* Type variables are given the basic sort "HOL.type". Some will later be
   335    constrained by information from type literals, or by type inference. *)
   336 fun typ_from_atp ctxt (u as ATerm ((a, _), us)) =
   337   let val Ts = map (typ_from_atp ctxt) us in
   338     case unprefix_and_unascii type_const_prefix a of
   339       SOME b => Type (invert_const b, Ts)
   340     | NONE =>
   341       if not (null us) then
   342         raise HO_TERM [u]  (* only "tconst"s have type arguments *)
   343       else case unprefix_and_unascii tfree_prefix a of
   344         SOME b => make_tfree ctxt b
   345       | NONE =>
   346         (* Could be an Isabelle variable or a variable from the ATP, say "X1"
   347            or "_5018". Sometimes variables from the ATP are indistinguishable
   348            from Isabelle variables, which forces us to use a type parameter in
   349            all cases. *)
   350         (a |> perhaps (unprefix_and_unascii tvar_prefix), HOLogic.typeS)
   351         |> Type_Infer.param 0
   352   end
   353 
   354 (* Type class literal applied to a type. Returns triple of polarity, class,
   355    type. *)
   356 fun type_constraint_from_term ctxt (u as ATerm ((a, _), us)) =
   357   case (unprefix_and_unascii class_prefix a, map (typ_from_atp ctxt) us) of
   358     (SOME b, [T]) => (b, T)
   359   | _ => raise HO_TERM [u]
   360 
   361 (* Accumulate type constraints in a formula: negative type literals. *)
   362 fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
   363 fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
   364   | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
   365   | add_type_constraint _ _ = I
   366 
   367 fun repair_variable_name f s =
   368   let
   369     fun subscript_name s n = s ^ nat_subscript n
   370     val s = String.map f s
   371   in
   372     case space_explode "_" s of
   373       [_] => (case take_suffix Char.isDigit (String.explode s) of
   374                 (cs1 as _ :: _, cs2 as _ :: _) =>
   375                 subscript_name (String.implode cs1)
   376                                (the (Int.fromString (String.implode cs2)))
   377               | (_, _) => s)
   378     | [s1, s2] => (case Int.fromString s2 of
   379                      SOME n => subscript_name s1 n
   380                    | NONE => s)
   381     | _ => s
   382   end
   383 
   384 (* The number of type arguments of a constant, zero if it's monomorphic. For
   385    (instances of) Skolem pseudoconstants, this information is encoded in the
   386    constant name. *)
   387 fun num_type_args thy s =
   388   if String.isPrefix skolem_const_prefix s then
   389     s |> Long_Name.explode |> List.last |> Int.fromString |> the
   390   else if String.isPrefix lam_lifted_prefix s then
   391     if String.isPrefix lam_lifted_poly_prefix s then 2 else 0
   392   else
   393     (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
   394 
   395 fun slack_fastype_of t = fastype_of t handle TERM _ => HOLogic.typeT
   396 
   397 (* First-order translation. No types are known for variables. "HOLogic.typeT"
   398    should allow them to be inferred. *)
   399 fun term_from_atp ctxt textual sym_tab =
   400   let
   401     val thy = Proof_Context.theory_of ctxt
   402     (* For Metis, we use 1 rather than 0 because variable references in clauses
   403        may otherwise conflict with variable constraints in the goal. At least,
   404        type inference often fails otherwise. See also "axiom_inference" in
   405        "Metis_Reconstruct". *)
   406     val var_index = if textual then 0 else 1
   407     fun do_term extra_ts opt_T u =
   408       case u of
   409         ATerm ((s, _), us) =>
   410         if String.isPrefix native_type_prefix s then
   411           @{const True} (* ignore TPTP type information *)
   412         else if s = tptp_equal then
   413           let val ts = map (do_term [] NONE) us in
   414             if textual andalso length ts = 2 andalso
   415               hd ts aconv List.last ts then
   416               (* Vampire is keen on producing these. *)
   417               @{const True}
   418             else
   419               list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
   420           end
   421         else case unprefix_and_unascii const_prefix s of
   422           SOME s' =>
   423           let
   424             val ((s', s''), mangled_us) =
   425               s' |> unmangled_const |>> `invert_const
   426           in
   427             if s' = type_tag_name then
   428               case mangled_us @ us of
   429                 [typ_u, term_u] =>
   430                 do_term extra_ts (SOME (typ_from_atp ctxt typ_u)) term_u
   431               | _ => raise HO_TERM us
   432             else if s' = predicator_name then
   433               do_term [] (SOME @{typ bool}) (hd us)
   434             else if s' = app_op_name then
   435               let val extra_t = do_term [] NONE (List.last us) in
   436                 do_term (extra_t :: extra_ts)
   437                         (case opt_T of
   438                            SOME T => SOME (slack_fastype_of extra_t --> T)
   439                          | NONE => NONE)
   440                         (nth us (length us - 2))
   441               end
   442             else if s' = type_guard_name then
   443               @{const True} (* ignore type predicates *)
   444             else
   445               let
   446                 val new_skolem = String.isPrefix new_skolem_const_prefix s''
   447                 val num_ty_args =
   448                   length us - the_default 0 (Symtab.lookup sym_tab s)
   449                 val (type_us, term_us) =
   450                   chop num_ty_args us |>> append mangled_us
   451                 val term_ts = map (do_term [] NONE) term_us
   452                 val T =
   453                   (if not (null type_us) andalso
   454                       num_type_args thy s' = length type_us then
   455                      let val Ts = type_us |> map (typ_from_atp ctxt) in
   456                        if new_skolem then
   457                          SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts))
   458                        else if textual then
   459                          try (Sign.const_instance thy) (s', Ts)
   460                        else
   461                          NONE
   462                      end
   463                    else
   464                      NONE)
   465                   |> (fn SOME T => T
   466                        | NONE => map slack_fastype_of term_ts --->
   467                                  (case opt_T of
   468                                     SOME T => T
   469                                   | NONE => HOLogic.typeT))
   470                 val t =
   471                   if new_skolem then
   472                     Var ((new_skolem_var_name_from_const s'', var_index), T)
   473                   else
   474                     Const (unproxify_const s', T)
   475               in list_comb (t, term_ts @ extra_ts) end
   476           end
   477         | NONE => (* a free or schematic variable *)
   478           let
   479             val term_ts = map (do_term [] NONE) us
   480             val ts = term_ts @ extra_ts
   481             val T =
   482               case opt_T of
   483                 SOME T => map slack_fastype_of term_ts ---> T
   484               | NONE => map slack_fastype_of ts ---> HOLogic.typeT
   485             val t =
   486               case unprefix_and_unascii fixed_var_prefix s of
   487                 SOME s => Free (s, T)
   488               | NONE =>
   489                 case unprefix_and_unascii schematic_var_prefix s of
   490                   SOME s => Var ((s, var_index), T)
   491                 | NONE =>
   492                   Var ((s |> textual ? repair_variable_name Char.toLower,
   493                         var_index), T)
   494           in list_comb (t, ts) end
   495   in do_term [] end
   496 
   497 fun term_from_atom ctxt textual sym_tab pos (u as ATerm ((s, _), _)) =
   498   if String.isPrefix class_prefix s then
   499     add_type_constraint pos (type_constraint_from_term ctxt u)
   500     #> pair @{const True}
   501   else
   502     pair (term_from_atp ctxt textual sym_tab (SOME @{typ bool}) u)
   503 
   504 val combinator_table =
   505   [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
   506    (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
   507    (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
   508    (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
   509    (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
   510 
   511 fun uncombine_term thy =
   512   let
   513     fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
   514       | aux (Abs (s, T, t')) = Abs (s, T, aux t')
   515       | aux (t as Const (x as (s, _))) =
   516         (case AList.lookup (op =) combinator_table s of
   517            SOME thm => thm |> prop_of |> specialize_type thy x
   518                            |> Logic.dest_equals |> snd
   519          | NONE => t)
   520       | aux t = t
   521   in aux end
   522 
   523 (* Update schematic type variables with detected sort constraints. It's not
   524    totally clear whether this code is necessary. *)
   525 fun repair_tvar_sorts (t, tvar_tab) =
   526   let
   527     fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
   528       | do_type (TVar (xi, s)) =
   529         TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
   530       | do_type (TFree z) = TFree z
   531     fun do_term (Const (a, T)) = Const (a, do_type T)
   532       | do_term (Free (a, T)) = Free (a, do_type T)
   533       | do_term (Var (xi, T)) = Var (xi, do_type T)
   534       | do_term (t as Bound _) = t
   535       | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
   536       | do_term (t1 $ t2) = do_term t1 $ do_term t2
   537   in t |> not (Vartab.is_empty tvar_tab) ? do_term end
   538 
   539 fun quantify_over_var quant_of var_s t =
   540   let
   541     val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s)
   542                   |> map Var
   543   in fold_rev quant_of vars t end
   544 
   545 (* Interpret an ATP formula as a HOL term, extracting sort constraints as they
   546    appear in the formula. *)
   547 fun prop_from_atp ctxt textual sym_tab phi =
   548   let
   549     fun do_formula pos phi =
   550       case phi of
   551         AQuant (_, [], phi) => do_formula pos phi
   552       | AQuant (q, (s, _) :: xs, phi') =>
   553         do_formula pos (AQuant (q, xs, phi'))
   554         (* FIXME: TFF *)
   555         #>> quantify_over_var (case q of
   556                                  AForall => forall_of
   557                                | AExists => exists_of)
   558                               (s |> textual ? repair_variable_name Char.toLower)
   559       | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
   560       | AConn (c, [phi1, phi2]) =>
   561         do_formula (pos |> c = AImplies ? not) phi1
   562         ##>> do_formula pos phi2
   563         #>> (case c of
   564                AAnd => s_conj
   565              | AOr => s_disj
   566              | AImplies => s_imp
   567              | AIff => s_iff
   568              | ANot => raise Fail "impossible connective")
   569       | AAtom tm => term_from_atom ctxt textual sym_tab pos tm
   570       | _ => raise FORMULA [phi]
   571   in repair_tvar_sorts (do_formula true phi Vartab.empty) end
   572 
   573 fun infer_formula_types ctxt =
   574   Type.constraint HOLogic.boolT
   575   #> Syntax.check_term
   576          (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
   577 
   578 fun uncombined_etc_prop_from_atp ctxt textual sym_tab =
   579   let val thy = Proof_Context.theory_of ctxt in
   580     prop_from_atp ctxt textual sym_tab
   581     #> textual ? uncombine_term thy #> infer_formula_types ctxt
   582   end
   583 
   584 (**** Translation of TSTP files to Isar proofs ****)
   585 
   586 fun unvarify_term (Var ((s, 0), T)) = Free (s, T)
   587   | unvarify_term t = raise TERM ("unvarify_term: non-Var", [t])
   588 
   589 fun decode_line sym_tab (Definition_Step (name, phi1, phi2)) ctxt =
   590     let
   591       val thy = Proof_Context.theory_of ctxt
   592       val t1 = prop_from_atp ctxt true sym_tab phi1
   593       val vars = snd (strip_comb t1)
   594       val frees = map unvarify_term vars
   595       val unvarify_args = subst_atomic (vars ~~ frees)
   596       val t2 = prop_from_atp ctxt true sym_tab phi2
   597       val (t1, t2) =
   598         HOLogic.eq_const HOLogic.typeT $ t1 $ t2
   599         |> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt
   600         |> HOLogic.dest_eq
   601     in
   602       (Definition_Step (name, t1, t2),
   603        fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt)
   604     end
   605   | decode_line sym_tab (Inference_Step (name, u, rule, deps)) ctxt =
   606     let val t = u |> uncombined_etc_prop_from_atp ctxt true sym_tab in
   607       (Inference_Step (name, t, rule, deps),
   608        fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt)
   609     end
   610 fun decode_lines ctxt sym_tab lines =
   611   fst (fold_map (decode_line sym_tab) lines ctxt)
   612 
   613 fun is_same_inference _ (Definition_Step _) = false
   614   | is_same_inference t (Inference_Step (_, t', _, _)) = t aconv t'
   615 
   616 (* No "real" literals means only type information (tfree_tcs, clsrel, or
   617    clsarity). *)
   618 fun is_only_type_information t = t aconv @{term True}
   619 
   620 fun replace_one_dependency (old, new) dep =
   621   if is_same_atp_step dep old then new else [dep]
   622 fun replace_dependencies_in_line _ (line as Definition_Step _) = line
   623   | replace_dependencies_in_line p (Inference_Step (name, t, rule, deps)) =
   624     Inference_Step (name, t, rule,
   625                     fold (union (op =) o replace_one_dependency p) deps [])
   626 
   627 (* Discard facts; consolidate adjacent lines that prove the same formula, since
   628    they differ only in type information.*)
   629 fun add_line _ (line as Definition_Step _) lines = line :: lines
   630   | add_line fact_names (Inference_Step (name as (_, ss), t, rule, [])) lines =
   631     (* No dependencies: fact, conjecture, or (for Vampire) internal facts or
   632        definitions. *)
   633     if is_fact fact_names ss then
   634       (* Facts are not proof lines. *)
   635       if is_only_type_information t then
   636         map (replace_dependencies_in_line (name, [])) lines
   637       (* Is there a repetition? If so, replace later line by earlier one. *)
   638       else case take_prefix (not o is_same_inference t) lines of
   639         (_, []) => lines (* no repetition of proof line *)
   640       | (pre, Inference_Step (name', _, _, _) :: post) =>
   641         pre @ map (replace_dependencies_in_line (name', [name])) post
   642       | _ => raise Fail "unexpected inference"
   643     else if is_conjecture ss then
   644       Inference_Step (name, t, rule, []) :: lines
   645     else
   646       map (replace_dependencies_in_line (name, [])) lines
   647   | add_line _ (Inference_Step (name, t, rule, deps)) lines =
   648     (* Type information will be deleted later; skip repetition test. *)
   649     if is_only_type_information t then
   650       Inference_Step (name, t, rule, deps) :: lines
   651     (* Is there a repetition? If so, replace later line by earlier one. *)
   652     else case take_prefix (not o is_same_inference t) lines of
   653       (* FIXME: Doesn't this code risk conflating proofs involving different
   654          types? *)
   655        (_, []) => Inference_Step (name, t, rule, deps) :: lines
   656      | (pre, Inference_Step (name', t', rule, _) :: post) =>
   657        Inference_Step (name, t', rule, deps) ::
   658        pre @ map (replace_dependencies_in_line (name', [name])) post
   659      | _ => raise Fail "unexpected inference"
   660 
   661 val waldmeister_conjecture_num = "1.0.0.0"
   662 
   663 val repair_waldmeister_endgame =
   664   let
   665     fun do_tail (Inference_Step (name, t, rule, deps)) =
   666         Inference_Step (name, s_not t, rule, deps)
   667       | do_tail line = line
   668     fun do_body [] = []
   669       | do_body ((line as Inference_Step ((num, _), _, _, _)) :: lines) =
   670         if num = waldmeister_conjecture_num then map do_tail (line :: lines)
   671         else line :: do_body lines
   672       | do_body (line :: lines) = line :: do_body lines
   673   in do_body end
   674 
   675 (* Recursively delete empty lines (type information) from the proof. *)
   676 fun add_nontrivial_line (line as Inference_Step (name, t, _, [])) lines =
   677     if is_only_type_information t then delete_dependency name lines
   678     else line :: lines
   679   | add_nontrivial_line line lines = line :: lines
   680 and delete_dependency name lines =
   681   fold_rev add_nontrivial_line
   682            (map (replace_dependencies_in_line (name, [])) lines) []
   683 
   684 (* ATPs sometimes reuse free variable names in the strangest ways. Removing
   685    offending lines often does the trick. *)
   686 fun is_bad_free frees (Free x) = not (member (op =) frees x)
   687   | is_bad_free _ _ = false
   688 
   689 fun add_desired_line _ _ _ (line as Definition_Step (name, _, _)) (j, lines) =
   690     (j, line :: map (replace_dependencies_in_line (name, [])) lines)
   691   | add_desired_line isar_shrink_factor fact_names frees
   692         (Inference_Step (name as (_, ss), t, rule, deps)) (j, lines) =
   693     (j + 1,
   694      if is_fact fact_names ss orelse
   695         is_conjecture ss orelse
   696         (* the last line must be kept *)
   697         j = 0 orelse
   698         (not (is_only_type_information t) andalso
   699          null (Term.add_tvars t []) andalso
   700          not (exists_subterm (is_bad_free frees) t) andalso
   701          length deps >= 2 andalso j mod isar_shrink_factor = 0 andalso
   702          (* kill next to last line, which usually results in a trivial step *)
   703          j <> 1) then
   704        Inference_Step (name, t, rule, deps) :: lines  (* keep line *)
   705      else
   706        map (replace_dependencies_in_line (name, deps)) lines)  (* drop line *)
   707 
   708 (** Isar proof construction and manipulation **)
   709 
   710 type label = string * int
   711 type facts = label list * string list
   712 
   713 datatype isar_qualifier = Show | Then | Moreover | Ultimately
   714 
   715 datatype isar_step =
   716   Fix of (string * typ) list |
   717   Let of term * term |
   718   Assume of label * term |
   719   Prove of isar_qualifier list * label * term * byline
   720 and byline =
   721   By_Metis of facts |
   722   Case_Split of isar_step list list * facts
   723 
   724 fun add_fact_from_dependency fact_names (name as (_, ss)) =
   725   if is_fact fact_names ss then
   726     apsnd (union (op =) (map fst (resolve_fact fact_names ss)))
   727   else
   728     apfst (insert (op =) (raw_label_for_name name))
   729 
   730 fun repair_name "$true" = "c_True"
   731   | repair_name "$false" = "c_False"
   732   | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
   733   | repair_name s =
   734     if is_tptp_equal s orelse
   735        (* seen in Vampire proofs *)
   736        (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
   737       tptp_equal
   738     else
   739       s
   740 
   741 (* FIXME: Still needed? Try with SPASS proofs perhaps. *)
   742 val kill_duplicate_assumptions_in_proof =
   743   let
   744     fun relabel_facts subst =
   745       apfst (map (fn l => AList.lookup (op =) subst l |> the_default l))
   746     fun do_step (step as Assume (l, t)) (proof, subst, assums) =
   747         (case AList.lookup (op aconv) assums t of
   748            SOME l' => (proof, (l, l') :: subst, assums)
   749          | NONE => (step :: proof, subst, (t, l) :: assums))
   750       | do_step (Prove (qs, l, t, by)) (proof, subst, assums) =
   751         (Prove (qs, l, t,
   752                 case by of
   753                   By_Metis facts => By_Metis (relabel_facts subst facts)
   754                 | Case_Split (proofs, facts) =>
   755                   Case_Split (map do_proof proofs,
   756                               relabel_facts subst facts)) ::
   757          proof, subst, assums)
   758       | do_step step (proof, subst, assums) = (step :: proof, subst, assums)
   759     and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev
   760   in do_proof end
   761 
   762 fun used_labels_of_step (Prove (_, _, _, by)) =
   763     (case by of
   764        By_Metis (ls, _) => ls
   765      | Case_Split (proofs, (ls, _)) =>
   766        fold (union (op =) o used_labels_of) proofs ls)
   767   | used_labels_of_step _ = []
   768 and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof []
   769 
   770 fun kill_useless_labels_in_proof proof =
   771   let
   772     val used_ls = used_labels_of proof
   773     fun do_label l = if member (op =) used_ls l then l else no_label
   774     fun do_step (Assume (l, t)) = Assume (do_label l, t)
   775       | do_step (Prove (qs, l, t, by)) =
   776         Prove (qs, do_label l, t,
   777                case by of
   778                  Case_Split (proofs, facts) =>
   779                  Case_Split (map (map do_step) proofs, facts)
   780                | _ => by)
   781       | do_step step = step
   782   in map do_step proof end
   783 
   784 fun prefix_for_depth n = replicate_string (n + 1)
   785 
   786 val relabel_proof =
   787   let
   788     fun aux _ _ _ [] = []
   789       | aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) =
   790         if l = no_label then
   791           Assume (l, t) :: aux subst depth (next_assum, next_fact) proof
   792         else
   793           let val l' = (prefix_for_depth depth assum_prefix, next_assum) in
   794             Assume (l', t) ::
   795             aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof
   796           end
   797       | aux subst depth (next_assum, next_fact)
   798             (Prove (qs, l, t, by) :: proof) =
   799         let
   800           val (l', subst, next_fact) =
   801             if l = no_label then
   802               (l, subst, next_fact)
   803             else
   804               let
   805                 val l' = (prefix_for_depth depth have_prefix, next_fact)
   806               in (l', (l, l') :: subst, next_fact + 1) end
   807           val relabel_facts =
   808             apfst (maps (the_list o AList.lookup (op =) subst))
   809           val by =
   810             case by of
   811               By_Metis facts => By_Metis (relabel_facts facts)
   812             | Case_Split (proofs, facts) =>
   813               Case_Split (map (aux subst (depth + 1) (1, 1)) proofs,
   814                           relabel_facts facts)
   815         in
   816           Prove (qs, l', t, by) :: aux subst depth (next_assum, next_fact) proof
   817         end
   818       | aux subst depth nextp (step :: proof) =
   819         step :: aux subst depth nextp proof
   820   in aux [] 0 (1, 1) end
   821 
   822 fun string_for_proof ctxt0 type_enc lam_trans i n =
   823   let
   824     val ctxt = ctxt0
   825 (* FIXME: Implement proper handling of type constraints:
   826       |> Config.put show_free_types false
   827       |> Config.put show_types false
   828       |> Config.put show_sorts false
   829 *)
   830     fun fix_print_mode f x =
   831       Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN)
   832                                (print_mode_value ())) f x
   833     fun do_indent ind = replicate_string (ind * indent_size) " "
   834     fun do_free (s, T) =
   835       maybe_quote s ^ " :: " ^
   836       maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T)
   837     fun do_label l = if l = no_label then "" else string_for_label l ^ ": "
   838     fun do_have qs =
   839       (if member (op =) qs Moreover then "moreover " else "") ^
   840       (if member (op =) qs Ultimately then "ultimately " else "") ^
   841       (if member (op =) qs Then then
   842          if member (op =) qs Show then "thus" else "hence"
   843        else
   844          if member (op =) qs Show then "show" else "have")
   845     val do_term = maybe_quote o fix_print_mode (Syntax.string_of_term ctxt)
   846     val reconstr = Metis (type_enc, lam_trans)
   847     fun do_facts (ls, ss) =
   848       reconstructor_command reconstr 1 1
   849           (ls |> sort_distinct (prod_ord string_ord int_ord),
   850            ss |> sort_distinct string_ord)
   851     and do_step ind (Fix xs) =
   852         do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n"
   853       | do_step ind (Let (t1, t2)) =
   854         do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n"
   855       | do_step ind (Assume (l, t)) =
   856         do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n"
   857       | do_step ind (Prove (qs, l, t, By_Metis facts)) =
   858         do_indent ind ^ do_have qs ^ " " ^
   859         do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n"
   860       | do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) =
   861         implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind)
   862                      proofs) ^
   863         do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^
   864         do_facts facts ^ "\n"
   865     and do_steps prefix suffix ind steps =
   866       let val s = implode (map (do_step ind) steps) in
   867         replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
   868         String.extract (s, ind * indent_size,
   869                         SOME (size s - ind * indent_size - 1)) ^
   870         suffix ^ "\n"
   871       end
   872     and do_block ind proof = do_steps "{ " " }" (ind + 1) proof
   873     (* One-step proofs are pointless; better use the Metis one-liner
   874        directly. *)
   875     and do_proof [Prove (_, _, _, By_Metis _)] = ""
   876       | do_proof proof =
   877         (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
   878         do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^
   879         (if n <> 1 then "next" else "qed")
   880   in do_proof end
   881 
   882 fun isar_proof_text ctxt isar_proof_requested
   883         (debug, isar_shrink_factor, pool, fact_names, sym_tab, atp_proof, goal)
   884         (one_line_params as (_, _, _, _, subgoal, subgoal_count)) =
   885   let
   886     val isar_shrink_factor =
   887       (if isar_proof_requested then 1 else 2) * isar_shrink_factor
   888     val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
   889     val frees = fold Term.add_frees (concl_t :: hyp_ts) []
   890     val one_line_proof = one_line_proof_text one_line_params
   891     val type_enc =
   892       if is_typed_helper_used_in_atp_proof atp_proof then full_typesN
   893       else partial_typesN
   894     val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans
   895 
   896     fun isar_proof_of () =
   897       let
   898         val atp_proof =
   899           atp_proof
   900           |> clean_up_atp_proof_dependencies
   901           |> nasty_atp_proof pool
   902           |> map_term_names_in_atp_proof repair_name
   903           |> decode_lines ctxt sym_tab
   904           |> rpair [] |-> fold_rev (add_line fact_names)
   905           |> repair_waldmeister_endgame
   906           |> rpair [] |-> fold_rev add_nontrivial_line
   907           |> rpair (0, [])
   908           |-> fold_rev (add_desired_line isar_shrink_factor fact_names frees)
   909           |> snd
   910         val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts)
   911         val conjs =
   912           atp_proof
   913           |> map_filter (fn Inference_Step (name as (_, ss), _, _, []) =>
   914                             if member (op =) ss conj_name then SOME name else NONE
   915                           | _ => NONE)
   916         fun dep_of_step (Definition_Step _) = NONE
   917           | dep_of_step (Inference_Step (name, _, _, from)) = SOME (from, name)
   918         val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph
   919         val axioms = axioms_of_ref_graph ref_graph conjs
   920         val tainted = tainted_atoms_of_ref_graph ref_graph conjs
   921         val props =
   922           Symtab.empty
   923           |> fold (fn Definition_Step _ => I (* FIXME *)
   924                     | Inference_Step ((s, _), t, _, _) =>
   925                       Symtab.update_new (s,
   926                           t |> fold forall_of (map Var (Term.add_vars t []))
   927                             |> member (op = o apsnd fst) tainted s ? s_not))
   928                   atp_proof
   929         fun prop_of_clause c =
   930           fold (curry s_disj) (map_filter (Symtab.lookup props o fst) c)
   931                @{term False}
   932         fun label_of_clause [name] = raw_label_for_name name
   933           | label_of_clause c = (space_implode "___" (map fst c), 0)
   934         fun maybe_show outer c =
   935           (outer andalso length c = 1 andalso subset (op =) (c, conjs))
   936           ? cons Show
   937         fun do_have outer qs (gamma, c) =
   938           Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c,
   939                  By_Metis (fold (add_fact_from_dependency fact_names
   940                                  o the_single) gamma ([], [])))
   941         fun do_inf outer (Have z) = do_have outer [] z
   942           | do_inf outer (Hence z) = do_have outer [Then] z
   943           | do_inf outer (Cases cases) =
   944             let val c = succedent_of_cases cases in
   945               Prove (maybe_show outer c [Ultimately], label_of_clause c,
   946                      prop_of_clause c,
   947                      Case_Split (map (do_case false) cases, ([], [])))
   948             end
   949         and do_case outer (c, infs) =
   950           Assume (label_of_clause c, prop_of_clause c) ::
   951           map (do_inf outer) infs
   952         val isar_proof =
   953           (if null params then [] else [Fix params]) @
   954           (ref_graph
   955            |> redirect_graph axioms tainted
   956            |> chain_direct_proof
   957            |> map (do_inf true)
   958            |> kill_duplicate_assumptions_in_proof
   959            |> kill_useless_labels_in_proof
   960            |> relabel_proof)
   961           |> string_for_proof ctxt type_enc lam_trans subgoal subgoal_count
   962       in
   963         case isar_proof of
   964           "" =>
   965           if isar_proof_requested then
   966             "\nNo structured proof available (proof too short)."
   967           else
   968             ""
   969         | _ =>
   970           "\n\n" ^ (if isar_proof_requested then "Structured proof"
   971                     else "Perhaps this will work") ^
   972           ":\n" ^ Markup.markup Isabelle_Markup.sendback isar_proof
   973       end
   974     val isar_proof =
   975       if debug then
   976         isar_proof_of ()
   977       else case try isar_proof_of () of
   978         SOME s => s
   979       | NONE => if isar_proof_requested then
   980                   "\nWarning: The Isar proof construction failed."
   981                 else
   982                   ""
   983   in one_line_proof ^ isar_proof end
   984 
   985 fun proof_text ctxt isar_proof isar_params
   986                (one_line_params as (preplay, _, _, _, _, _)) =
   987   (if case preplay of Failed_to_Play _ => true | _ => isar_proof then
   988      isar_proof_text ctxt isar_proof isar_params
   989    else
   990      one_line_proof_text) one_line_params
   991 
   992 end;