src/HOL/Tools/ATP/atp_proof_reconstruct.ML
author blanchet
Tue, 19 Nov 2013 22:20:01 +0100
changeset 55880 d983a020489d
parent 55879 8b5caa190054
child 56098 dd0f4d265730
permissions -rw-r--r--
whitespace tuning
     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 Basic proof reconstruction from ATP proofs.
     7 *)
     8 
     9 signature ATP_PROOF_RECONSTRUCT =
    10 sig
    11   type ('a, 'b) atp_term = ('a, 'b) ATP_Problem.atp_term
    12   type ('a, 'b, 'c, 'd) atp_formula = ('a, 'b, 'c, 'd) ATP_Problem.atp_formula
    13   type stature = ATP_Problem_Generate.stature
    14   type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
    15   type 'a atp_proof = 'a ATP_Proof.atp_proof
    16 
    17   val metisN : string
    18   val full_typesN : string
    19   val partial_typesN : string
    20   val no_typesN : string
    21   val really_full_type_enc : string
    22   val full_type_enc : string
    23   val partial_type_enc : string
    24   val no_type_enc : string
    25   val full_type_encs : string list
    26   val partial_type_encs : string list
    27   val default_metis_lam_trans : string
    28   val metis_call : string -> string -> string
    29   val forall_of : term -> term -> term
    30   val exists_of : term -> term -> term
    31   val unalias_type_enc : string -> string list
    32   val term_of_atp : Proof.context -> bool -> int Symtab.table -> typ option ->
    33     (string, string) atp_term -> term
    34   val prop_of_atp : Proof.context -> bool -> int Symtab.table ->
    35     (string, string, (string, string) atp_term, string) atp_formula -> term
    36 
    37   val used_facts_in_atp_proof :
    38     Proof.context -> (string * stature) list vector -> string atp_proof -> (string * stature) list
    39   val used_facts_in_unsound_atp_proof : Proof.context -> (string * stature) list vector ->
    40     'a atp_proof -> string list option
    41   val lam_trans_of_atp_proof : string atp_proof -> string -> string
    42   val is_typed_helper_used_in_atp_proof : string atp_proof -> bool
    43   val termify_atp_proof : Proof.context -> string Symtab.table -> (string * term) list ->
    44     int Symtab.table -> string atp_proof -> (term, string) atp_step list
    45   val factify_atp_proof : (string * 'a) list vector -> term list -> term ->
    46     (term, string) atp_step list -> (term, string) atp_step list
    47 end;
    48 
    49 structure ATP_Proof_Reconstruct : ATP_PROOF_RECONSTRUCT =
    50 struct
    51 
    52 open ATP_Util
    53 open ATP_Problem
    54 open ATP_Proof
    55 open ATP_Problem_Generate
    56 
    57 val metisN = "metis"
    58 
    59 val full_typesN = "full_types"
    60 val partial_typesN = "partial_types"
    61 val no_typesN = "no_types"
    62 
    63 val really_full_type_enc = "mono_tags"
    64 val full_type_enc = "poly_guards_query"
    65 val partial_type_enc = "poly_args"
    66 val no_type_enc = "erased"
    67 
    68 val full_type_encs = [full_type_enc, really_full_type_enc]
    69 val partial_type_encs = partial_type_enc :: full_type_encs
    70 
    71 val type_enc_aliases =
    72   [(full_typesN, full_type_encs),
    73    (partial_typesN, partial_type_encs),
    74    (no_typesN, [no_type_enc])]
    75 
    76 fun unalias_type_enc s =
    77   AList.lookup (op =) type_enc_aliases s |> the_default [s]
    78 
    79 val default_metis_lam_trans = combsN
    80 
    81 fun metis_call type_enc lam_trans =
    82   let
    83     val type_enc =
    84       case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases
    85                       type_enc of
    86         [alias] => alias
    87       | _ => type_enc
    88     val opts = [] |> type_enc <> partial_typesN ? cons type_enc
    89                   |> lam_trans <> default_metis_lam_trans ? cons lam_trans
    90   in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end
    91 
    92 fun term_name' (Var ((s, _), _)) = perhaps (try Name.dest_skolem) s
    93   | term_name' _ = ""
    94 
    95 fun lambda' v = Term.lambda_name (term_name' v, v)
    96 
    97 fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda' v t
    98 fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda' v t
    99 
   100 fun make_tfree ctxt w =
   101   let val ww = "'" ^ w in
   102     TFree (ww, the_default HOLogic.typeS (Variable.def_sort ctxt (ww, ~1)))
   103   end
   104 
   105 exception ATP_TERM of (string, string) atp_term list
   106 exception ATP_FORMULA of
   107     (string, string, (string, string) atp_term, string) atp_formula list
   108 exception SAME of unit
   109 
   110 (* Type variables are given the basic sort "HOL.type". Some will later be
   111    constrained by information from type literals, or by type inference. *)
   112 fun typ_of_atp ctxt (u as ATerm ((a, _), us)) =
   113   let val Ts = map (typ_of_atp ctxt) us in
   114     case unprefix_and_unascii type_const_prefix a of
   115       SOME b => Type (invert_const b, Ts)
   116     | NONE =>
   117       if not (null us) then
   118         raise ATP_TERM [u]  (* only "tconst"s have type arguments *)
   119       else case unprefix_and_unascii tfree_prefix a of
   120         SOME b => make_tfree ctxt b
   121       | NONE =>
   122         (* Could be an Isabelle variable or a variable from the ATP, say "X1"
   123            or "_5018". Sometimes variables from the ATP are indistinguishable
   124            from Isabelle variables, which forces us to use a type parameter in
   125            all cases. *)
   126         (a |> perhaps (unprefix_and_unascii tvar_prefix), HOLogic.typeS)
   127         |> Type_Infer.param 0
   128   end
   129 
   130 (* Type class literal applied to a type. Returns triple of polarity, class,
   131    type. *)
   132 fun type_constraint_of_term ctxt (u as ATerm ((a, _), us)) =
   133   case (unprefix_and_unascii class_prefix a, map (typ_of_atp ctxt) us) of
   134     (SOME b, [T]) => (b, T)
   135   | _ => raise ATP_TERM [u]
   136 
   137 (* Accumulate type constraints in a formula: negative type literals. *)
   138 fun add_var (key, z)  = Vartab.map_default (key, []) (cons z)
   139 fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl)
   140   | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
   141   | add_type_constraint _ _ = I
   142 
   143 fun repair_var_name s =
   144   let
   145     fun subscript_name s n = s ^ nat_subscript n
   146     val s = s |> String.map Char.toLower
   147   in
   148     case space_explode "_" s of
   149       [_] => (case take_suffix Char.isDigit (String.explode s) of
   150                 (cs1 as _ :: _, cs2 as _ :: _) =>
   151                 subscript_name (String.implode cs1)
   152                                (the (Int.fromString (String.implode cs2)))
   153               | (_, _) => s)
   154     | [s1, s2] => (case Int.fromString s2 of
   155                      SOME n => subscript_name s1 n
   156                    | NONE => s)
   157     | _ => s
   158   end
   159 
   160 (* The number of type arguments of a constant, zero if it's monomorphic. For
   161    (instances of) Skolem pseudoconstants, this information is encoded in the
   162    constant name. *)
   163 fun robust_const_num_type_args thy s =
   164   if String.isPrefix skolem_const_prefix s then
   165     s |> Long_Name.explode |> List.last |> Int.fromString |> the
   166   else if String.isPrefix lam_lifted_prefix s then
   167     if String.isPrefix lam_lifted_poly_prefix s then 2 else 0
   168   else
   169     (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length
   170 
   171 fun slack_fastype_of t = fastype_of t handle TERM _ => HOLogic.typeT
   172 
   173 (* Cope with "tt(X) = X" atoms, where "X" is existentially quantified. *)
   174 fun loose_aconv (Free (s, _), Free (s', _)) = s = s'
   175   | loose_aconv (t, t') = t aconv t'
   176 
   177 val vampire_skolem_prefix = "sK"
   178 
   179 (* First-order translation. No types are known for variables. "HOLogic.typeT"
   180    should allow them to be inferred. *)
   181 fun term_of_atp ctxt textual sym_tab =
   182   let
   183     val thy = Proof_Context.theory_of ctxt
   184     (* For Metis, we use 1 rather than 0 because variable references in clauses
   185        may otherwise conflict with variable constraints in the goal. At least,
   186        type inference often fails otherwise. See also "axiom_inference" in
   187        "Metis_Reconstruct". *)
   188     val var_index = if textual then 0 else 1
   189     fun do_term extra_ts opt_T u =
   190       case u of
   191         ATerm ((s, _), us) =>
   192         if s = ""
   193           then error "Isar proof reconstruction failed because the ATP proof \
   194                      \contains unparsable material."
   195         else if String.isPrefix native_type_prefix s then
   196           @{const True} (* ignore TPTP type information *)
   197         else if s = tptp_equal then
   198           let val ts = map (do_term [] NONE) us in
   199             if textual andalso length ts = 2 andalso
   200                loose_aconv (hd ts, List.last ts) then
   201               @{const True}
   202             else
   203               list_comb (Const (@{const_name HOL.eq}, HOLogic.typeT), ts)
   204           end
   205         else case unprefix_and_unascii const_prefix s of
   206           SOME s' =>
   207           let
   208             val ((s', s''), mangled_us) =
   209               s' |> unmangled_const |>> `invert_const
   210           in
   211             if s' = type_tag_name then
   212               case mangled_us @ us of
   213                 [typ_u, term_u] =>
   214                 do_term extra_ts (SOME (typ_of_atp ctxt typ_u)) term_u
   215               | _ => raise ATP_TERM us
   216             else if s' = predicator_name then
   217               do_term [] (SOME @{typ bool}) (hd us)
   218             else if s' = app_op_name then
   219               let val extra_t = do_term [] NONE (List.last us) in
   220                 do_term (extra_t :: extra_ts)
   221                         (case opt_T of
   222                            SOME T => SOME (slack_fastype_of extra_t --> T)
   223                          | NONE => NONE)
   224                         (nth us (length us - 2))
   225               end
   226             else if s' = type_guard_name then
   227               @{const True} (* ignore type predicates *)
   228             else
   229               let
   230                 val new_skolem = String.isPrefix new_skolem_const_prefix s''
   231                 val num_ty_args =
   232                   length us - the_default 0 (Symtab.lookup sym_tab s)
   233                 val (type_us, term_us) =
   234                   chop num_ty_args us |>> append mangled_us
   235                 val term_ts = map (do_term [] NONE) term_us
   236                 val T =
   237                   (if not (null type_us) andalso
   238                       robust_const_num_type_args thy s' = length type_us then
   239                      let val Ts = type_us |> map (typ_of_atp ctxt) in
   240                        if new_skolem then
   241                          SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts))
   242                        else if textual then
   243                          try (Sign.const_instance thy) (s', Ts)
   244                        else
   245                          NONE
   246                      end
   247                    else
   248                      NONE)
   249                   |> (fn SOME T => T
   250                        | NONE => map slack_fastype_of term_ts --->
   251                                  (case opt_T of
   252                                     SOME T => T
   253                                   | NONE => HOLogic.typeT))
   254                 val t =
   255                   if new_skolem then
   256                     Var ((new_skolem_var_name_of_const s'', var_index), T)
   257                   else
   258                     Const (unproxify_const s', T)
   259               in list_comb (t, term_ts @ extra_ts) end
   260           end
   261         | NONE => (* a free or schematic variable *)
   262           let
   263             (* This assumes that distinct names are mapped to distinct names by
   264                "Variable.variant_frees". This does not hold in general but
   265                should hold for ATP-generated Skolem function names, since these
   266                end with a digit and "variant_frees" appends letters. *)
   267             fun fresh_up s =
   268               [(s, ())] |> Variable.variant_frees ctxt [] |> hd |> fst
   269             val term_ts =
   270               map (do_term [] NONE) us
   271               (* Vampire (2.6) passes arguments to Skolem functions in reverse
   272                  order *)
   273               |> String.isPrefix vampire_skolem_prefix s ? rev
   274             val ts = term_ts @ extra_ts
   275             val T =
   276               case opt_T of
   277                 SOME T => map slack_fastype_of term_ts ---> T
   278               | NONE => map slack_fastype_of ts ---> HOLogic.typeT
   279             val t =
   280               case unprefix_and_unascii fixed_var_prefix s of
   281                 SOME s => Free (s, T)
   282               | NONE =>
   283                 case unprefix_and_unascii schematic_var_prefix s of
   284                   SOME s => Var ((s, var_index), T)
   285                 | NONE =>
   286                   if textual andalso not (is_tptp_variable s) then
   287                     Free (s |> textual ? (repair_var_name #> fresh_up), T)
   288                   else
   289                     Var ((s |> textual ? repair_var_name, var_index), T)
   290           in list_comb (t, ts) end
   291   in do_term [] end
   292 
   293 fun term_of_atom ctxt textual sym_tab pos (u as ATerm ((s, _), _)) =
   294   if String.isPrefix class_prefix s then
   295     add_type_constraint pos (type_constraint_of_term ctxt u)
   296     #> pair @{const True}
   297   else
   298     pair (term_of_atp ctxt textual sym_tab (SOME @{typ bool}) u)
   299 
   300 (* Update schematic type variables with detected sort constraints. It's not
   301    totally clear whether this code is necessary. *)
   302 fun repair_tvar_sorts (t, tvar_tab) =
   303   let
   304     fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
   305       | do_type (TVar (xi, s)) =
   306         TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
   307       | do_type (TFree z) = TFree z
   308     fun do_term (Const (a, T)) = Const (a, do_type T)
   309       | do_term (Free (a, T)) = Free (a, do_type T)
   310       | do_term (Var (xi, T)) = Var (xi, do_type T)
   311       | do_term (t as Bound _) = t
   312       | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
   313       | do_term (t1 $ t2) = do_term t1 $ do_term t2
   314   in t |> not (Vartab.is_empty tvar_tab) ? do_term end
   315 
   316 fun quantify_over_var quant_of var_s t =
   317   let
   318     val vars = [] |> Term.add_vars t |> filter (fn ((s, _), _) => s = var_s)
   319     val normTs = vars |> AList.group (op =) |> map (apsnd hd)
   320     fun norm_var_types (Var (x, T)) =
   321         Var (x, case AList.lookup (op =) normTs x of
   322                   NONE => T
   323                 | SOME T' => T')
   324       | norm_var_types t = t
   325   in t |> map_aterms norm_var_types |> fold_rev quant_of (map Var normTs) end
   326 
   327 (* Interpret an ATP formula as a HOL term, extracting sort constraints as they
   328    appear in the formula. *)
   329 fun prop_of_atp ctxt textual sym_tab phi =
   330   let
   331     fun do_formula pos phi =
   332       case phi of
   333         AQuant (_, [], phi) => do_formula pos phi
   334       | AQuant (q, (s, _) :: xs, phi') =>
   335         do_formula pos (AQuant (q, xs, phi'))
   336         (* FIXME: TFF *)
   337         #>> quantify_over_var
   338               (case q of AForall => forall_of | AExists => exists_of)
   339               (s |> textual ? repair_var_name)
   340       | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
   341       | AConn (c, [phi1, phi2]) =>
   342         do_formula (pos |> c = AImplies ? not) phi1
   343         ##>> do_formula pos phi2
   344         #>> (case c of
   345                AAnd => s_conj
   346              | AOr => s_disj
   347              | AImplies => s_imp
   348              | AIff => s_iff
   349              | ANot => raise Fail "impossible connective")
   350       | AAtom tm => term_of_atom ctxt textual sym_tab pos tm
   351       | _ => raise ATP_FORMULA [phi]
   352   in repair_tvar_sorts (do_formula true phi Vartab.empty) end
   353 
   354 fun find_first_in_list_vector vec key =
   355   Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
   356                  | (_, value) => value) NONE vec
   357 
   358 val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
   359 
   360 fun resolve_one_named_fact fact_names s =
   361   case try (unprefix fact_prefix) s of
   362     SOME s' =>
   363     let val s' = s' |> unprefix_fact_number |> unascii_of in
   364       s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
   365     end
   366   | NONE => NONE
   367 
   368 fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
   369 
   370 fun resolve_one_named_conjecture s =
   371   case try (unprefix conjecture_prefix) s of
   372     SOME s' => Int.fromString s'
   373   | NONE => NONE
   374 
   375 val resolve_conjecture = map_filter resolve_one_named_conjecture
   376 
   377 fun is_axiom_used_in_proof pred =
   378   exists (fn ((_, ss), _, _, _, []) => exists pred ss | _ => false)
   379 
   380 fun add_non_rec_defs fact_names accum =
   381   Vector.foldl (fn (facts, facts') =>
   382       union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
   383             facts')
   384     accum fact_names
   385 
   386 val isa_ext = Thm.get_name_hint @{thm ext}
   387 val isa_short_ext = Long_Name.base_name isa_ext
   388 
   389 fun ext_name ctxt =
   390   if Thm.eq_thm_prop (@{thm ext},
   391        singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
   392     isa_short_ext
   393   else
   394     isa_ext
   395 
   396 val leo2_extcnf_equal_neg_rule = "extcnf_equal_neg"
   397 val leo2_unfold_def_rule = "unfold_def"
   398 
   399 fun add_fact ctxt fact_names ((_, ss), _, _, rule, deps) =
   400   (if rule = leo2_extcnf_equal_neg_rule then
   401      insert (op =) (ext_name ctxt, (Global, General))
   402    else if rule = leo2_unfold_def_rule then
   403      (* LEO 1.3.3 does not record definitions properly, leading to missing
   404         dependencies in the TSTP proof. Remove the next line once this is
   405         fixed. *)
   406      add_non_rec_defs fact_names
   407    else if rule = agsyhol_coreN orelse rule = satallax_coreN then
   408      (fn [] =>
   409          (* agsyHOL and Satallax don't include definitions in their
   410             unsatisfiable cores, so we assume the worst and include them all
   411             here. *)
   412          [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
   413        | facts => facts)
   414    else
   415      I)
   416   #> (if null deps then union (op =) (resolve_fact fact_names ss) else I)
   417 
   418 fun used_facts_in_atp_proof ctxt fact_names atp_proof =
   419   if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
   420   else fold (add_fact ctxt fact_names) atp_proof []
   421 
   422 fun used_facts_in_unsound_atp_proof _ _ [] = NONE
   423   | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
   424     let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
   425       if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
   426          not (is_axiom_used_in_proof (not o null o resolve_conjecture o single) atp_proof) then
   427         SOME (map fst used_facts)
   428       else
   429         NONE
   430     end
   431 
   432 val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
   433 
   434 (* overapproximation (good enough) *)
   435 fun is_lam_lifted s =
   436   String.isPrefix fact_prefix s andalso
   437   String.isSubstring ascii_of_lam_fact_prefix s
   438 
   439 val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
   440 
   441 fun lam_trans_of_atp_proof atp_proof default =
   442   case (is_axiom_used_in_proof is_combinator_def atp_proof,
   443         is_axiom_used_in_proof is_lam_lifted atp_proof) of
   444     (false, false) => default
   445   | (false, true) => liftingN
   446 (*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
   447   | (true, _) => combsN
   448 
   449 val is_typed_helper_name =
   450   String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
   451 
   452 fun is_typed_helper_used_in_atp_proof atp_proof =
   453   is_axiom_used_in_proof is_typed_helper_name atp_proof
   454 
   455 fun repair_name "$true" = "c_True"
   456   | repair_name "$false" = "c_False"
   457   | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
   458   | repair_name s =
   459     if is_tptp_equal s orelse
   460        (* seen in Vampire proofs *)
   461        (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
   462       tptp_equal
   463     else
   464       s
   465 
   466 fun infer_formula_types ctxt =
   467   Type.constraint HOLogic.boolT
   468   #> Syntax.check_term
   469          (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
   470 
   471 val combinator_table =
   472   [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
   473    (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
   474    (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
   475    (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
   476    (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
   477 
   478 fun uncombine_term thy =
   479   let
   480     fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
   481       | aux (Abs (s, T, t')) = Abs (s, T, aux t')
   482       | aux (t as Const (x as (s, _))) =
   483         (case AList.lookup (op =) combinator_table s of
   484            SOME thm => thm |> prop_of |> specialize_type thy x
   485                            |> Logic.dest_equals |> snd
   486          | NONE => t)
   487       | aux t = t
   488   in aux end
   489 
   490 fun unlift_term lifted =
   491   map_aterms (fn t as Const (s, _) =>
   492                  if String.isPrefix lam_lifted_prefix s then
   493                    case AList.lookup (op =) lifted s of
   494                      SOME t =>
   495                      (* FIXME: do something about the types *)
   496                      unlift_term lifted t
   497                    | NONE => t
   498                  else
   499                    t
   500                | t => t)
   501 
   502 fun decode_line ctxt lifted sym_tab (name, role, u, rule, deps) =
   503   let
   504     val thy = Proof_Context.theory_of ctxt
   505     val t =
   506       u |> prop_of_atp ctxt true sym_tab
   507         |> uncombine_term thy
   508         |> unlift_term lifted
   509         |> infer_formula_types ctxt
   510   in (name, role, t, rule, deps) end
   511 
   512 val waldmeister_conjecture_num = "1.0.0.0"
   513 
   514 fun repair_waldmeister_endgame arg =
   515   let
   516     fun do_tail (name, _, t, rule, deps) =
   517       (name, Negated_Conjecture, s_not t, rule, deps)
   518     fun do_body [] = []
   519       | do_body ((line as ((num, _), _, _, _, _)) :: lines) =
   520         if num = waldmeister_conjecture_num then map do_tail (line :: lines)
   521         else line :: do_body lines
   522   in do_body arg end
   523 
   524 fun termify_atp_proof ctxt pool lifted sym_tab =
   525   clean_up_atp_proof_dependencies
   526   #> nasty_atp_proof pool
   527   #> map_term_names_in_atp_proof repair_name
   528   #> map (decode_line ctxt lifted sym_tab)
   529   #> repair_waldmeister_endgame
   530 
   531 fun factify_atp_proof fact_names hyp_ts concl_t atp_proof =
   532   let
   533     fun factify_step ((num, ss), role, t, rule, deps) =
   534       let
   535         val (ss', role', t') =
   536           (case resolve_conjecture ss of
   537             [j] =>
   538             if j = length hyp_ts then ([], Conjecture, concl_t) else ([], Hypothesis, nth hyp_ts j)
   539            | _ =>
   540              (case resolve_fact fact_names ss of
   541                [] => (ss, Plain, t)
   542              | facts => (map fst facts, Axiom, t)))
   543       in
   544         ((num, ss'), role', t', rule, deps)
   545       end
   546 
   547     val atp_proof = map factify_step atp_proof
   548     val names = map #1 atp_proof
   549 
   550     fun repair_dep (num, ss) = (num, the_default ss (AList.lookup (op =) names num))
   551     fun repair_deps (name, role, t, rule, deps) = (name, role, t, rule, map repair_dep deps)
   552 
   553   in map repair_deps atp_proof end
   554 
   555 end;