src/HOL/Tools/ATP/atp_proof_reconstruct.ML
changeset 54500 f625e0e79dd1
parent 54499 319f8659267d
child 54501 77c9460e01b0
equal deleted inserted replaced
54499:319f8659267d 54500:f625e0e79dd1
     8 
     8 
     9 signature ATP_PROOF_RECONSTRUCT =
     9 signature ATP_PROOF_RECONSTRUCT =
    10 sig
    10 sig
    11   type ('a, 'b) atp_term = ('a, 'b) ATP_Problem.atp_term
    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
    12   type ('a, 'b, 'c, 'd) atp_formula = ('a, 'b, 'c, 'd) ATP_Problem.atp_formula
       
    13   type stature = ATP_Problem_Generate.stature
    13   type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
    14   type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
    14   type 'a atp_proof = 'a ATP_Proof.atp_proof
    15   type 'a atp_proof = 'a ATP_Proof.atp_proof
    15 
    16 
    16   val metisN : string
    17   val metisN : string
    17   val full_typesN : string
    18   val full_typesN : string
    21   val full_type_enc : string
    22   val full_type_enc : string
    22   val partial_type_enc : string
    23   val partial_type_enc : string
    23   val no_type_enc : string
    24   val no_type_enc : string
    24   val full_type_encs : string list
    25   val full_type_encs : string list
    25   val partial_type_encs : string list
    26   val partial_type_encs : string list
    26   val metis_default_lam_trans : string
    27   val default_metis_lam_trans : string
    27   val metis_call : string -> string -> string
    28   val metis_call : string -> string -> string
    28   val forall_of : term -> term -> term
    29   val forall_of : term -> term -> term
    29   val exists_of : term -> term -> term
    30   val exists_of : term -> term -> term
    30   val unalias_type_enc : string -> string list
    31   val unalias_type_enc : string -> string list
    31   val term_of_atp :
    32   val term_of_atp :
    33     (string, string) atp_term -> term
    34     (string, string) atp_term -> term
    34   val prop_of_atp :
    35   val prop_of_atp :
    35     Proof.context -> bool -> int Symtab.table ->
    36     Proof.context -> bool -> int Symtab.table ->
    36     (string, string, (string, string) atp_term, string) atp_formula -> term
    37     (string, string, (string, string) atp_term, string) atp_formula -> term
    37 
    38 
       
    39   val resolve_fact : (string * 'a) list vector -> string list -> (string * 'a) list
       
    40   val resolve_conjecture : string list -> int list
       
    41   val is_fact : (string * 'a) list vector -> string list -> bool
       
    42   val is_conjecture : string list -> bool
       
    43   val used_facts_in_atp_proof :
       
    44     Proof.context -> (string * stature) list vector -> string atp_proof ->
       
    45     (string * stature) list
       
    46   val used_facts_in_unsound_atp_proof :
       
    47     Proof.context -> (string * stature) list vector -> 'a atp_proof ->
       
    48     string list option
       
    49   val lam_trans_of_atp_proof : string atp_proof -> string -> string
       
    50   val is_typed_helper_used_in_atp_proof : string atp_proof -> bool
    38   val termify_atp_proof :
    51   val termify_atp_proof :
    39     Proof.context -> string Symtab.table -> (string * term) list ->
    52     Proof.context -> string Symtab.table -> (string * term) list ->
    40     int Symtab.table -> string atp_proof -> (term, string) atp_step list
    53     int Symtab.table -> string atp_proof -> (term, string) atp_step list
    41 end;
    54 end;
    42 
    55 
    68    (no_typesN, [no_type_enc])]
    81    (no_typesN, [no_type_enc])]
    69 
    82 
    70 fun unalias_type_enc s =
    83 fun unalias_type_enc s =
    71   AList.lookup (op =) type_enc_aliases s |> the_default [s]
    84   AList.lookup (op =) type_enc_aliases s |> the_default [s]
    72 
    85 
    73 val metis_default_lam_trans = combsN
    86 val default_metis_lam_trans = combsN
    74 
    87 
    75 fun metis_call type_enc lam_trans =
    88 fun metis_call type_enc lam_trans =
    76   let
    89   let
    77     val type_enc =
    90     val type_enc =
    78       case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases
    91       case AList.find (fn (enc, encs) => enc = hd encs) type_enc_aliases
    79                       type_enc of
    92                       type_enc of
    80         [alias] => alias
    93         [alias] => alias
    81       | _ => type_enc
    94       | _ => type_enc
    82     val opts = [] |> type_enc <> partial_typesN ? cons type_enc
    95     val opts = [] |> type_enc <> partial_typesN ? cons type_enc
    83                   |> lam_trans <> metis_default_lam_trans ? cons lam_trans
    96                   |> lam_trans <> default_metis_lam_trans ? cons lam_trans
    84   in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end
    97   in metisN ^ (if null opts then "" else " (" ^ commas opts ^ ")") end
    85 
    98 
    86 fun term_name' (Var ((s, _), _)) = perhaps (try Name.dest_skolem) s
    99 fun term_name' (Var ((s, _), _)) = perhaps (try Name.dest_skolem) s
    87   | term_name' _ = ""
   100   | term_name' _ = ""
    88 
   101 
   343              | ANot => raise Fail "impossible connective")
   356              | ANot => raise Fail "impossible connective")
   344       | AAtom tm => term_of_atom ctxt textual sym_tab pos tm
   357       | AAtom tm => term_of_atom ctxt textual sym_tab pos tm
   345       | _ => raise ATP_FORMULA [phi]
   358       | _ => raise ATP_FORMULA [phi]
   346   in repair_tvar_sorts (do_formula true phi Vartab.empty) end
   359   in repair_tvar_sorts (do_formula true phi Vartab.empty) end
   347 
   360 
       
   361 fun find_first_in_list_vector vec key =
       
   362   Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
       
   363                  | (_, value) => value) NONE vec
       
   364 
       
   365 val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
       
   366 
       
   367 fun resolve_one_named_fact fact_names s =
       
   368   case try (unprefix fact_prefix) s of
       
   369     SOME s' =>
       
   370     let val s' = s' |> unprefix_fact_number |> unascii_of in
       
   371       s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
       
   372     end
       
   373   | NONE => NONE
       
   374 fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
       
   375 fun is_fact fact_names = not o null o resolve_fact fact_names
       
   376 
       
   377 fun resolve_one_named_conjecture s =
       
   378   case try (unprefix conjecture_prefix) s of
       
   379     SOME s' => Int.fromString s'
       
   380   | NONE => NONE
       
   381 
       
   382 val resolve_conjecture = map_filter resolve_one_named_conjecture
       
   383 val is_conjecture = not o null o resolve_conjecture
       
   384 
       
   385 fun is_axiom_used_in_proof pred =
       
   386   exists (fn ((_, ss), _, _, _, []) => exists pred ss | _ => false)
       
   387 
       
   388 fun add_non_rec_defs fact_names accum =
       
   389   Vector.foldl (fn (facts, facts') =>
       
   390       union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts)
       
   391             facts')
       
   392     accum fact_names
       
   393 
       
   394 val isa_ext = Thm.get_name_hint @{thm ext}
       
   395 val isa_short_ext = Long_Name.base_name isa_ext
       
   396 
       
   397 fun ext_name ctxt =
       
   398   if Thm.eq_thm_prop (@{thm ext},
       
   399        singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then
       
   400     isa_short_ext
       
   401   else
       
   402     isa_ext
       
   403 
       
   404 val leo2_extcnf_equal_neg_rule = "extcnf_equal_neg"
       
   405 val leo2_unfold_def_rule = "unfold_def"
       
   406 
       
   407 fun add_fact ctxt fact_names ((_, ss), _, _, rule, deps) =
       
   408   (if rule = leo2_extcnf_equal_neg_rule then
       
   409      insert (op =) (ext_name ctxt, (Global, General))
       
   410    else if rule = leo2_unfold_def_rule then
       
   411      (* LEO 1.3.3 does not record definitions properly, leading to missing
       
   412         dependencies in the TSTP proof. Remove the next line once this is
       
   413         fixed. *)
       
   414      add_non_rec_defs fact_names
       
   415    else if rule = agsyhol_coreN orelse rule = satallax_coreN then
       
   416      (fn [] =>
       
   417          (* agsyHOL and Satallax don't include definitions in their
       
   418             unsatisfiable cores, so we assume the worst and include them all
       
   419             here. *)
       
   420          [(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names
       
   421        | facts => facts)
       
   422    else
       
   423      I)
       
   424   #> (if null deps then union (op =) (resolve_fact fact_names ss) else I)
       
   425 
       
   426 fun used_facts_in_atp_proof ctxt fact_names atp_proof =
       
   427   if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
       
   428   else fold (add_fact ctxt fact_names) atp_proof []
       
   429 
       
   430 fun used_facts_in_unsound_atp_proof _ _ [] = NONE
       
   431   | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
       
   432     let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
       
   433       if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
       
   434          not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then
       
   435         SOME (map fst used_facts)
       
   436       else
       
   437         NONE
       
   438     end
       
   439 
       
   440 val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
       
   441 
       
   442 (* overapproximation (good enough) *)
       
   443 fun is_lam_lifted s =
       
   444   String.isPrefix fact_prefix s andalso
       
   445   String.isSubstring ascii_of_lam_fact_prefix s
       
   446 
       
   447 val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
       
   448 
       
   449 fun lam_trans_of_atp_proof atp_proof default =
       
   450   case (is_axiom_used_in_proof is_combinator_def atp_proof,
       
   451         is_axiom_used_in_proof is_lam_lifted atp_proof) of
       
   452     (false, false) => default
       
   453   | (false, true) => liftingN
       
   454 (*  | (true, true) => combs_and_liftingN -- not supported by "metis" *)
       
   455   | (true, _) => combsN
       
   456 
       
   457 val is_typed_helper_name =
       
   458   String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
       
   459 
       
   460 fun is_typed_helper_used_in_atp_proof atp_proof =
       
   461   is_axiom_used_in_proof is_typed_helper_name atp_proof
       
   462 
   348 fun repair_name "$true" = "c_True"
   463 fun repair_name "$true" = "c_True"
   349   | repair_name "$false" = "c_False"
   464   | repair_name "$false" = "c_False"
   350   | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
   465   | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
   351   | repair_name s =
   466   | repair_name s =
   352     if is_tptp_equal s orelse
   467     if is_tptp_equal s orelse