| author | wenzelm | 
| Fri, 03 Feb 2023 22:39:59 +0100 | |
| changeset 77189 | 461c078e545f | 
| parent 75123 | 66eb6fdfc244 | 
| child 77272 | 0506c3273814 | 
| permissions | -rw-r--r-- | 
| 46320 | 1 | (* Title: HOL/Tools/ATP/atp_proof_reconstruct.ML | 
| 38027 | 2 | Author: Lawrence C. Paulson, Cambridge University Computer Laboratory | 
| 3 | Author: Claire Quigley, Cambridge University Computer Laboratory | |
| 36392 | 4 | Author: Jasmin Blanchette, TU Muenchen | 
| 21978 
72c21698a055
Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
 paulson parents: diff
changeset | 5 | |
| 49914 | 6 | Basic proof reconstruction from ATP proofs. | 
| 33310 | 7 | *) | 
| 8 | ||
| 46320 | 9 | signature ATP_PROOF_RECONSTRUCT = | 
| 24425 
ca97c6f3d9cd
Returning both a "one-line" proof and a structured proof
 paulson parents: 
24387diff
changeset | 10 | sig | 
| 54811 | 11 | type 'a atp_type = 'a ATP_Problem.atp_type | 
| 53586 
bd5fa6425993
prefixed types and some functions with "atp_" for disambiguation
 blanchet parents: 
52758diff
changeset | 12 |   type ('a, 'b) atp_term = ('a, 'b) ATP_Problem.atp_term
 | 
| 
bd5fa6425993
prefixed types and some functions with "atp_" for disambiguation
 blanchet parents: 
52758diff
changeset | 13 |   type ('a, 'b, 'c, 'd) atp_formula = ('a, 'b, 'c, 'd) ATP_Problem.atp_formula
 | 
| 54500 | 14 | type stature = ATP_Problem_Generate.stature | 
| 54772 | 15 | type atp_step_name = ATP_Proof.atp_step_name | 
| 54499 | 16 |   type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
 | 
| 17 | type 'a atp_proof = 'a ATP_Proof.atp_proof | |
| 45519 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 18 | |
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 19 | val metisN : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 20 | val full_typesN : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 21 | val partial_typesN : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 22 | val no_typesN : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 23 | val really_full_type_enc : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 24 | val full_type_enc : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 25 | val partial_type_enc : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 26 | val no_type_enc : string | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 27 | val full_type_encs : string list | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 28 | val partial_type_encs : string list | 
| 54500 | 29 | val default_metis_lam_trans : string | 
| 54772 | 30 | |
| 58653 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 31 | val leo2_extcnf_equal_neg_rule : string | 
| 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 32 | val satallax_tab_rule_prefix : string | 
| 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 33 | |
| 49983 
33e18e9916a8
use metaquantification when possible in Isar proofs
 blanchet parents: 
49914diff
changeset | 34 | val forall_of : term -> term -> term | 
| 
33e18e9916a8
use metaquantification when possible in Isar proofs
 blanchet parents: 
49914diff
changeset | 35 | val exists_of : term -> term -> term | 
| 57767 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 36 | val simplify_bool : term -> term | 
| 57792 | 37 | val var_name_of_typ : typ -> string | 
| 57765 | 38 | val rename_bound_vars : term -> term | 
| 55285 | 39 | val type_enc_aliases : (string * string list) list | 
| 45519 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 40 | val unalias_type_enc : string -> string list | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 41 | val term_of_atp : Proof.context -> ATP_Problem.atp_format -> ATP_Problem_Generate.type_enc -> | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 42 | bool -> int Symtab.table -> typ option -> (string, string atp_type) atp_term -> term | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 43 | val prop_of_atp : Proof.context -> ATP_Problem.atp_format -> ATP_Problem_Generate.type_enc -> | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 44 | bool -> int Symtab.table -> | 
| 54811 | 45 | (string, string, (string, string atp_type) atp_term, string) atp_formula -> term | 
| 54499 | 46 | |
| 57263 | 47 | val used_facts_in_atp_proof : Proof.context -> (string * stature) list -> string atp_proof -> | 
| 48 | (string * stature) list | |
| 49 | val used_facts_in_unsound_atp_proof : Proof.context -> (string * stature) list -> 'a atp_proof -> | |
| 50 | string list option | |
| 55257 | 51 | val atp_proof_prefers_lifting : string atp_proof -> bool | 
| 54500 | 52 | val is_typed_helper_used_in_atp_proof : string atp_proof -> bool | 
| 54772 | 53 |   val replace_dependencies_in_line : atp_step_name * atp_step_name list -> ('a, 'b) atp_step ->
 | 
| 54 |     ('a, 'b) atp_step
 | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 55 | val termify_atp_proof : Proof.context -> string -> ATP_Problem.atp_format -> | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 56 | ATP_Problem_Generate.type_enc -> string Symtab.table -> (string * term) list -> | 
| 54499 | 57 | int Symtab.table -> string atp_proof -> (term, string) atp_step list | 
| 57267 
8b87114357bd
integrated new Waldmeister code with 'sledgehammer' command
 blanchet parents: 
57263diff
changeset | 58 | val repair_waldmeister_endgame : (term, 'a) atp_step list -> (term, 'a) atp_step list | 
| 57699 | 59 | val infer_formulas_types : Proof.context -> term list -> term list | 
| 72401 
2783779b7dd3
removed obsolete unmaintained experimental prover Pirate
 blanchet parents: 
70940diff
changeset | 60 | val introduce_spass_skolems : (term, string) atp_step list -> (term, string) atp_step list | 
| 57263 | 61 | val factify_atp_proof : (string * 'a) list -> term list -> term -> (term, string) atp_step list -> | 
| 62 | (term, string) atp_step list | |
| 24425 
ca97c6f3d9cd
Returning both a "one-line" proof and a structured proof
 paulson parents: 
24387diff
changeset | 63 | end; | 
| 21978 
72c21698a055
Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
 paulson parents: diff
changeset | 64 | |
| 46320 | 65 | structure ATP_Proof_Reconstruct : ATP_PROOF_RECONSTRUCT = | 
| 21978 
72c21698a055
Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
 paulson parents: diff
changeset | 66 | struct | 
| 
72c21698a055
Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
 paulson parents: diff
changeset | 67 | |
| 43085 
0a2f5b86bdd7
first step in sharing more code between ATP and Metis translation
 blanchet parents: 
43062diff
changeset | 68 | open ATP_Util | 
| 38028 | 69 | open ATP_Problem | 
| 39452 | 70 | open ATP_Proof | 
| 46320 | 71 | open ATP_Problem_Generate | 
| 21978 
72c21698a055
Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
 paulson parents: diff
changeset | 72 | |
| 45519 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 73 | val metisN = "metis" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 74 | |
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 75 | val full_typesN = "full_types" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 76 | val partial_typesN = "partial_types" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 77 | val no_typesN = "no_types" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 78 | |
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 79 | val really_full_type_enc = "mono_tags" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 80 | val full_type_enc = "poly_guards_query" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 81 | val partial_type_enc = "poly_args" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 82 | val no_type_enc = "erased" | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 83 | |
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 84 | val full_type_encs = [full_type_enc, really_full_type_enc] | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 85 | val partial_type_encs = partial_type_enc :: full_type_encs | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 86 | |
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 87 | val type_enc_aliases = | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 88 | [(full_typesN, full_type_encs), | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 89 | (partial_typesN, partial_type_encs), | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 90 | (no_typesN, [no_type_enc])] | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 91 | |
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 92 | fun unalias_type_enc s = | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 93 | AList.lookup (op =) type_enc_aliases s |> the_default [s] | 
| 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 94 | |
| 54500 | 95 | val default_metis_lam_trans = combsN | 
| 45519 
cd6e78cb6ee8
make metis reconstruction handling more flexible
 blanchet parents: 
45511diff
changeset | 96 | |
| 58653 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 97 | val leo2_extcnf_equal_neg_rule = "extcnf_equal_neg" | 
| 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 98 | val satallax_tab_rule_prefix = "tab_" | 
| 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 99 | |
| 50670 
eaa540986291
properly take the existential closure of skolems
 blanchet parents: 
49983diff
changeset | 100 | fun term_name' (Var ((s, _), _)) = perhaps (try Name.dest_skolem) s | 
| 52034 
11b48e7a4e7e
correctly 'repair' the monomorphization context for SMT solvers from Sledgehammer
 blanchet parents: 
52031diff
changeset | 101 | | term_name' _ = "" | 
| 50670 
eaa540986291
properly take the existential closure of skolems
 blanchet parents: 
49983diff
changeset | 102 | |
| 
eaa540986291
properly take the existential closure of skolems
 blanchet parents: 
49983diff
changeset | 103 | fun lambda' v = Term.lambda_name (term_name' v, v) | 
| 
eaa540986291
properly take the existential closure of skolems
 blanchet parents: 
49983diff
changeset | 104 | |
| 
eaa540986291
properly take the existential closure of skolems
 blanchet parents: 
49983diff
changeset | 105 | fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda' v t | 
| 
eaa540986291
properly take the existential closure of skolems
 blanchet parents: 
49983diff
changeset | 106 | fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda' v t | 
| 39425 | 107 | |
| 43135 
8c32a0160b0d
more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
 blanchet parents: 
43131diff
changeset | 108 | fun make_tfree ctxt w = | 
| 
8c32a0160b0d
more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
 blanchet parents: 
43131diff
changeset | 109 | let val ww = "'" ^ w in | 
| 69593 | 110 | TFree (ww, the_default \<^sort>\<open>type\<close> (Variable.def_sort ctxt (ww, ~1))) | 
| 43135 
8c32a0160b0d
more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
 blanchet parents: 
43131diff
changeset | 111 | end | 
| 
8c32a0160b0d
more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
 blanchet parents: 
43131diff
changeset | 112 | |
| 69593 | 113 | fun simplify_bool ((all as Const (\<^const_name>\<open>All\<close>, _)) $ Abs (s, T, t)) = | 
| 57767 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 114 | let val t' = simplify_bool t in | 
| 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 115 | if loose_bvar1 (t', 0) then all $ Abs (s, T, t') else t' | 
| 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 116 | end | 
| 69593 | 117 | | simplify_bool (Const (\<^const_name>\<open>Not\<close>, _) $ t) = s_not (simplify_bool t) | 
| 118 | | simplify_bool (Const (\<^const_name>\<open>conj\<close>, _) $ t $ u) = | |
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 119 | s_conj (simplify_bool t, simplify_bool u) | 
| 69593 | 120 | | simplify_bool (Const (\<^const_name>\<open>disj\<close>, _) $ t $ u) = | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 121 | s_disj (simplify_bool t, simplify_bool u) | 
| 69593 | 122 | | simplify_bool (Const (\<^const_name>\<open>implies\<close>, _) $ t $ u) = | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 123 | s_imp (simplify_bool t, simplify_bool u) | 
| 69593 | 124 | | simplify_bool ((t as Const (\<^const_name>\<open>HOL.eq\<close>, _)) $ u $ v) = | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 125 | (case (u, v) of | 
| 69593 | 126 | (Const (\<^const_name>\<open>True\<close>, _), _) => v | 
| 127 | | (u, Const (\<^const_name>\<open>True\<close>, _)) => u | |
| 128 | | (Const (\<^const_name>\<open>False\<close>, _), v) => s_not v | |
| 129 | | (u, Const (\<^const_name>\<open>False\<close>, _)) => s_not u | |
| 74379 | 130 | | _ => if u aconv v then \<^Const>\<open>True\<close> else t $ simplify_bool u $ simplify_bool v) | 
| 57767 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 131 | | simplify_bool (t $ u) = simplify_bool t $ simplify_bool u | 
| 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 132 | | simplify_bool (Abs (s, T, t)) = Abs (s, T, simplify_bool t) | 
| 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 133 | | simplify_bool t = t | 
| 
5bc204ca27ce
centralized boolean simplification so that e.g. LEO-II benefits from it
 blanchet parents: 
57765diff
changeset | 134 | |
| 57765 | 135 | fun single_letter upper s = | 
| 136 | let val s' = if String.isPrefix "o" s orelse String.isPrefix "O" s then "z" else s in | |
| 137 | String.extract (Name.desymbolize (SOME upper) (Long_Name.base_name s'), 0, SOME 1) | |
| 138 | end | |
| 139 | ||
| 69593 | 140 | fun var_name_of_typ (Type (\<^type_name>\<open>fun\<close>, [_, T])) = | 
| 57765 | 141 | if body_type T = HOLogic.boolT then "p" else "f" | 
| 69593 | 142 | | var_name_of_typ (Type (\<^type_name>\<open>set\<close>, [T])) = | 
| 58600 | 143 | let fun default () = single_letter true (var_name_of_typ T) in | 
| 58599 | 144 | (case T of | 
| 145 | Type (s, [T1, T2]) => if String.isSuffix "prod" s andalso T1 = T2 then "r" else default () | |
| 146 | | _ => default ()) | |
| 147 | end | |
| 57765 | 148 | | var_name_of_typ (Type (s, Ts)) = | 
| 149 | if String.isSuffix "list" s then var_name_of_typ (the_single Ts) ^ "s" | |
| 150 | else single_letter false (Long_Name.base_name s) | |
| 151 | | var_name_of_typ (TFree (s, _)) = single_letter false (perhaps (try (unprefix "'")) s) | |
| 152 | | var_name_of_typ (TVar ((s, _), T)) = var_name_of_typ (TFree (s, T)) | |
| 153 | ||
| 154 | fun rename_bound_vars (t $ u) = rename_bound_vars t $ rename_bound_vars u | |
| 155 | | rename_bound_vars (Abs (_, T, t)) = Abs (var_name_of_typ T, T, rename_bound_vars t) | |
| 156 | | rename_bound_vars t = t | |
| 157 | ||
| 54820 | 158 | exception ATP_CLASS of string list | 
| 54818 | 159 | exception ATP_TYPE of string atp_type list | 
| 54811 | 160 | exception ATP_TERM of (string, string atp_type) atp_term list | 
| 53586 
bd5fa6425993
prefixed types and some functions with "atp_" for disambiguation
 blanchet parents: 
52758diff
changeset | 161 | exception ATP_FORMULA of | 
| 54811 | 162 | (string, string, (string, string atp_type) atp_term, string) atp_formula list | 
| 37991 | 163 | exception SAME of unit | 
| 21978 
72c21698a055
Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
 paulson parents: diff
changeset | 164 | |
| 54820 | 165 | fun class_of_atp_class cls = | 
| 166 | (case unprefix_and_unascii class_prefix cls of | |
| 167 | SOME s => s | |
| 168 | | NONE => raise ATP_CLASS [cls]) | |
| 169 | ||
| 57697 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 170 | fun atp_type_of_atp_term (ATerm ((s, _), us)) = | 
| 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 171 | let val tys = map atp_type_of_atp_term us in | 
| 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 172 | if s = tptp_fun_type then | 
| 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 173 | (case tys of | 
| 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 174 | [ty1, ty2] => AFun (ty1, ty2) | 
| 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 175 | | _ => raise ATP_TYPE tys) | 
| 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 176 | else | 
| 
44341963ade3
correctly translate THF functions from terms to types
 blanchet parents: 
57635diff
changeset | 177 | AType ((s, []), tys) | 
| 37991 | 178 | end | 
| 54818 | 179 | |
| 58477 | 180 | (* Type variables are given the basic sort "HOL.type". Some will later be constrained by information | 
| 181 | from type literals, or by type inference. *) | |
| 182 | fun typ_of_atp_type ctxt (ty as AType ((a, clss), tys)) = | |
| 183 | let val Ts = map (typ_of_atp_type ctxt) tys in | |
| 184 | (case unprefix_and_unascii native_type_prefix a of | |
| 185 | SOME b => typ_of_atp_type ctxt (atp_type_of_atp_term (unmangled_type b)) | |
| 186 | | NONE => | |
| 187 | (case unprefix_and_unascii type_const_prefix a of | |
| 188 | SOME b => Type (invert_const b, Ts) | |
| 189 | | NONE => | |
| 190 | if not (null tys) then | |
| 191 | raise ATP_TYPE [ty] (* only "tconst"s have type arguments *) | |
| 192 | else | |
| 193 | (case unprefix_and_unascii tfree_prefix a of | |
| 194 | SOME b => make_tfree ctxt b | |
| 195 | | NONE => | |
| 196 | (* The term could be an Isabelle variable or a variable from the ATP, say "X1" or "_5018". | |
| 197 | Sometimes variables from the ATP are indistinguishable from Isabelle variables, which | |
| 198 | forces us to use a type parameter in all cases. *) | |
| 199 |               Type_Infer.param 0 ("'" ^ perhaps (unprefix_and_unascii tvar_prefix) a,
 | |
| 69593 | 200 | (if null clss then \<^sort>\<open>type\<close> else map class_of_atp_class clss))))) | 
| 58477 | 201 | end | 
| 202 | | typ_of_atp_type ctxt (AFun (ty1, ty2)) = typ_of_atp_type ctxt ty1 --> typ_of_atp_type ctxt ty2 | |
| 203 | ||
| 54818 | 204 | fun typ_of_atp_term ctxt = typ_of_atp_type ctxt o atp_type_of_atp_term | 
| 205 | ||
| 54789 | 206 | (* Type class literal applied to a type. Returns triple of polarity, class, type. *) | 
| 52031 
9a9238342963
tuning -- renamed '_from_' to '_of_' in Sledgehammer
 blanchet parents: 
51878diff
changeset | 207 | fun type_constraint_of_term ctxt (u as ATerm ((a, _), us)) = | 
| 54818 | 208 | (case (unprefix_and_unascii class_prefix a, map (typ_of_atp_term ctxt) us) of | 
| 42606 
0c76cf483899
show sorts not just types in Isar proofs + tuning
 blanchet parents: 
42595diff
changeset | 209 | (SOME b, [T]) => (b, T) | 
| 54789 | 210 | | _ => raise ATP_TERM [u]) | 
| 38014 | 211 | |
| 43907 
073ab5379842
pass type arguments to lambda-lifted Frees, to account for polymorphism
 blanchet parents: 
43863diff
changeset | 212 | (* Accumulate type constraints in a formula: negative type literals. *) | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 213 | fun add_var (key, z) = Vartab.map_default (key, []) (cons z) | 
| 42606 
0c76cf483899
show sorts not just types in Isar proofs + tuning
 blanchet parents: 
42595diff
changeset | 214 | fun add_type_constraint false (cl, TFree (a ,_)) = add_var ((a, ~1), cl) | 
| 
0c76cf483899
show sorts not just types in Isar proofs + tuning
 blanchet parents: 
42595diff
changeset | 215 | | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl) | 
| 
0c76cf483899
show sorts not just types in Isar proofs + tuning
 blanchet parents: 
42595diff
changeset | 216 | | add_type_constraint _ _ = I | 
| 38014 | 217 | |
| 57789 
a73255cffb5b
don't rename variables which will be renamed later anyway
 blanchet parents: 
57788diff
changeset | 218 | fun repair_var_name s = | 
| 55185 
a0bd8d6414e6
centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
 blanchet parents: 
54822diff
changeset | 219 | (case unprefix_and_unascii schematic_var_prefix s of | 
| 57789 
a73255cffb5b
don't rename variables which will be renamed later anyway
 blanchet parents: 
57788diff
changeset | 220 | SOME s' => s' | 
| 
a73255cffb5b
don't rename variables which will be renamed later anyway
 blanchet parents: 
57788diff
changeset | 221 | | NONE => s) | 
| 55185 
a0bd8d6414e6
centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
 blanchet parents: 
54822diff
changeset | 222 | |
| 54789 | 223 | (* The number of type arguments of a constant, zero if it's monomorphic. For (instances of) Skolem | 
| 224 | pseudoconstants, this information is encoded in the constant name. *) | |
| 52125 
ac7830871177
improved handling of free variables' types in Isar proofs
 blanchet parents: 
52034diff
changeset | 225 | fun robust_const_num_type_args thy s = | 
| 43907 
073ab5379842
pass type arguments to lambda-lifted Frees, to account for polymorphism
 blanchet parents: 
43863diff
changeset | 226 | if String.isPrefix skolem_const_prefix s then | 
| 46711 
f745bcc4a1e5
more explicit Long_Name operations (NB: analyzing qualifiers is inherently fragile);
 wenzelm parents: 
46435diff
changeset | 227 | s |> Long_Name.explode |> List.last |> Int.fromString |> the | 
| 45554 
09ad83de849c
don't pass "lam_lifted" option to "metis" unless there's a good reason
 blanchet parents: 
45553diff
changeset | 228 | else if String.isPrefix lam_lifted_prefix s then | 
| 
09ad83de849c
don't pass "lam_lifted" option to "metis" unless there's a good reason
 blanchet parents: 
45553diff
changeset | 229 | if String.isPrefix lam_lifted_poly_prefix s then 2 else 0 | 
| 43907 
073ab5379842
pass type arguments to lambda-lifted Frees, to account for polymorphism
 blanchet parents: 
43863diff
changeset | 230 | else | 
| 
073ab5379842
pass type arguments to lambda-lifted Frees, to account for polymorphism
 blanchet parents: 
43863diff
changeset | 231 | (s, Sign.the_const_type thy s) |> Sign.const_typargs thy |> length | 
| 
073ab5379842
pass type arguments to lambda-lifted Frees, to account for polymorphism
 blanchet parents: 
43863diff
changeset | 232 | |
| 69593 | 233 | fun slack_fastype_of t = fastype_of t handle TERM _ => Type_Infer.anyT \<^sort>\<open>type\<close> | 
| 43182 
649bada59658
slacker version of "fastype_of", in case a function has dummy type
 blanchet parents: 
43176diff
changeset | 234 | |
| 54772 | 235 | val spass_skolem_prefix = "sk" (* "skc" or "skf" *) | 
| 236 | val vampire_skolem_prefix = "sK" | |
| 50703 
76a2e506c125
swap Vampire's Skolem arguments to bring them in line with what E and metis's new skolemizer do (helps Isar proof reconstruction in some cases)
 blanchet parents: 
50693diff
changeset | 237 | |
| 57257 | 238 | fun var_index_of_textual textual = if textual then 0 else 1 | 
| 239 | ||
| 240 | fun quantify_over_var textual quant_of var_s var_T t = | |
| 241 | let | |
| 57790 | 242 | val vars = | 
| 243 | ((var_s, var_index_of_textual textual), var_T) :: | |
| 244 | filter (fn ((s, _), _) => s = var_s) (Term.add_vars t []) | |
| 57257 | 245 | val normTs = vars |> AList.group (op =) |> map (apsnd hd) | 
| 246 | fun norm_var_types (Var (x, T)) = | |
| 247 | Var (x, the_default T (AList.lookup (op =) normTs x)) | |
| 248 | | norm_var_types t = t | |
| 249 | in t |> map_aterms norm_var_types |> fold_rev quant_of (map Var normTs) end | |
| 250 | ||
| 57790 | 251 | (* This assumes that distinct names are mapped to distinct names by "Variable.variant_frees". This | 
| 252 | does not hold in general but should hold for ATP-generated Skolem function names, since these end | |
| 253 | with a digit and "variant_frees" appends letters. *) | |
| 254 | fun fresh_up ctxt s = fst (hd (Variable.variant_frees ctxt [] [(s, ())])) | |
| 255 | ||
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 256 | (* Higher-order translation. Variables are typed (although we don't use that information). Lambdas | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 257 | are typed. The code is similar to "term_of_atp_fo". *) | 
| 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 258 | fun term_of_atp_ho ctxt sym_tab = | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 259 | let | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 260 | val thy = Proof_Context.theory_of ctxt | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 261 | val var_index = var_index_of_textual true | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 262 | |
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 263 | fun do_term opt_T u = | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 264 | (case u of | 
| 57788 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 265 | AAbs (((var, ty), term), []) => | 
| 57257 | 266 | let | 
| 267 | val typ = typ_of_atp_type ctxt ty | |
| 57789 
a73255cffb5b
don't rename variables which will be renamed later anyway
 blanchet parents: 
57788diff
changeset | 268 | val var_name = repair_var_name var | 
| 57257 | 269 | val tm = do_term NONE term | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 270 | in quantify_over_var true lambda' var_name typ tm end | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 271 | | ATerm ((s, tys), us) => | 
| 74050 | 272 | if s = "" (* special marker generated on parse error *) then | 
| 273 | error "Isar proof reconstruction failed because the ATP proof contains unparsable \ | |
| 274 | \material" | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 275 | else if s = tptp_equal then | 
| 69593 | 276 | list_comb (Const (\<^const_name>\<open>HOL.eq\<close>, Type_Infer.anyT \<^sort>\<open>type\<close>), | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 277 | map (do_term NONE) us) | 
| 75052 | 278 | else if s = tptp_not_equal andalso length us = 2 then | 
| 75048 
e926618f9474
handle TPTP '!=' more gracefully in Isar proof reconstruction
 blanchet parents: 
74379diff
changeset | 279 | \<^const>\<open>HOL.Not\<close> $ list_comb (Const (\<^const_name>\<open>HOL.eq\<close>, Type_Infer.anyT \<^sort>\<open>type\<close>), | 
| 
e926618f9474
handle TPTP '!=' more gracefully in Isar proof reconstruction
 blanchet parents: 
74379diff
changeset | 280 | map (do_term NONE) us) | 
| 57256 | 281 | else if not (null us) then | 
| 282 | let | |
| 57820 | 283 | val args = map (do_term NONE) us | 
| 57256 | 284 | val opt_T' = SOME (map slack_fastype_of args ---> the_default dummyT opt_T) | 
| 285 | val func = do_term opt_T' (ATerm ((s, tys), [])) | |
| 286 | in foldl1 (op $) (func :: args) end | |
| 287 | else if s = tptp_or then HOLogic.disj | |
| 288 | else if s = tptp_and then HOLogic.conj | |
| 289 | else if s = tptp_implies then HOLogic.imp | |
| 290 | else if s = tptp_iff orelse s = tptp_equal then HOLogic.eq_const dummyT | |
| 75052 | 291 | else if s = tptp_not_iff orelse s = tptp_not_equal then \<^term>\<open>\<lambda>x y. x \<noteq> y\<close> | 
| 69593 | 292 | else if s = tptp_if then \<^term>\<open>\<lambda>P Q. Q \<longrightarrow> P\<close> | 
| 293 | else if s = tptp_not_and then \<^term>\<open>\<lambda>P Q. \<not> (P \<and> Q)\<close> | |
| 294 | else if s = tptp_not_or then \<^term>\<open>\<lambda>P Q. \<not> (P \<or> Q)\<close> | |
| 57256 | 295 | else if s = tptp_not then HOLogic.Not | 
| 296 | else if s = tptp_ho_forall then HOLogic.all_const dummyT | |
| 297 | else if s = tptp_ho_exists then HOLogic.exists_const dummyT | |
| 57709 | 298 | else if s = tptp_hilbert_choice then HOLogic.choice_const dummyT | 
| 69593 | 299 | else if s = tptp_hilbert_the then \<^term>\<open>The\<close> | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 300 | else | 
| 57256 | 301 | (case unprefix_and_unascii const_prefix s of | 
| 302 | SOME s' => | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 303 | let | 
| 57547 
677b07d777c3
don't generate TPTP THF 'Definition's, because they complicate reconstruction for AgsyHOL and Satallax
 blanchet parents: 
57267diff
changeset | 304 | val ((s', _), mangled_us) = s' |> unmangled_const |>> `invert_const | 
| 57256 | 305 | val num_ty_args = length us - the_default 0 (Symtab.lookup sym_tab s) | 
| 306 | val (type_us, term_us) = chop num_ty_args us |>> append mangled_us | |
| 307 | val term_ts = map (do_term NONE) term_us | |
| 308 | val Ts = map (typ_of_atp_type ctxt) tys @ map (typ_of_atp_term ctxt) type_us | |
| 309 | val T = | |
| 310 | (if not (null Ts) andalso robust_const_num_type_args thy s' = length Ts then | |
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 311 | try (Sign.const_instance thy) (s', Ts) | 
| 57256 | 312 | else | 
| 313 | NONE) | |
| 314 | |> (fn SOME T => T | |
| 315 | | NONE => | |
| 316 | map slack_fastype_of term_ts ---> | |
| 69593 | 317 | the_default (Type_Infer.anyT \<^sort>\<open>type\<close>) opt_T) | 
| 57256 | 318 | val t = Const (unproxify_const s', T) | 
| 319 | in list_comb (t, term_ts) end | |
| 320 | | NONE => (* a free or schematic variable *) | |
| 321 | let | |
| 322 | val ts = map (do_term NONE) us | |
| 323 | val T = | |
| 58500 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 324 | (case tys of | 
| 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 325 | [ty] => typ_of_atp_type ctxt ty | 
| 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 326 | | _ => | 
| 57256 | 327 | map slack_fastype_of ts ---> | 
| 58500 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 328 | (case opt_T of | 
| 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 329 | SOME T => T | 
| 69593 | 330 | | NONE => Type_Infer.anyT \<^sort>\<open>type\<close>)) | 
| 57256 | 331 | val t = | 
| 332 | (case unprefix_and_unascii fixed_var_prefix s of | |
| 333 | SOME s => Free (s, T) | |
| 334 | | NONE => | |
| 57790 | 335 | if not (is_tptp_variable s) then Free (fresh_up ctxt s, T) | 
| 57789 
a73255cffb5b
don't rename variables which will be renamed later anyway
 blanchet parents: 
57788diff
changeset | 336 | else Var ((repair_var_name s, var_index), T)) | 
| 57256 | 337 | in list_comb (t, ts) end)) | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 338 | in do_term end | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 339 | |
| 56254 | 340 | (* First-order translation. No types are known for variables. "Type_Infer.anyT @{sort type}"
 | 
| 341 | should allow them to be inferred. *) | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 342 | fun term_of_atp_fo ctxt textual sym_tab = | 
| 36909 
7d5587f6d5f7
made Sledgehammer's full-typed proof reconstruction work for the first time;
 blanchet parents: 
36607diff
changeset | 343 | let | 
| 43135 
8c32a0160b0d
more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
 blanchet parents: 
43131diff
changeset | 344 | val thy = Proof_Context.theory_of ctxt | 
| 54789 | 345 | (* For Metis, we use 1 rather than 0 because variable references in clauses may otherwise | 
| 346 | conflict with variable constraints in the goal. At least, type inference often fails | |
| 347 | otherwise. See also "axiom_inference" in "Metis_Reconstruct". *) | |
| 57257 | 348 | val var_index = var_index_of_textual textual | 
| 57199 | 349 | |
| 43131 
9e9420122f91
fixed interaction between type tags and hAPP in reconstruction code
 blanchet parents: 
43130diff
changeset | 350 | fun do_term extra_ts opt_T u = | 
| 54789 | 351 | (case u of | 
| 54818 | 352 | ATerm ((s, tys), us) => | 
| 74050 | 353 | if s = "" (* special marker generated on parse error *) then | 
| 57199 | 354 | error "Isar proof reconstruction failed because the ATP proof contains unparsable \ | 
| 63692 | 355 | \material" | 
| 51878 | 356 | else if String.isPrefix native_type_prefix s then | 
| 74379 | 357 | \<^Const>\<open>True\<close> (* ignore TPTP type information (needed?) *) | 
| 44773 
e701dabbfe37
perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
 blanchet parents: 
44399diff
changeset | 358 | else if s = tptp_equal then | 
| 69593 | 359 | list_comb (Const (\<^const_name>\<open>HOL.eq\<close>, Type_Infer.anyT \<^sort>\<open>type\<close>), | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 360 | map (do_term [] NONE) us) | 
| 54789 | 361 | else | 
| 362 | (case unprefix_and_unascii const_prefix s of | |
| 363 | SOME s' => | |
| 54818 | 364 | let val ((s', s''), mangled_us) = s' |> unmangled_const |>> `invert_const in | 
| 54789 | 365 | if s' = type_tag_name then | 
| 366 | (case mangled_us @ us of | |
| 54818 | 367 | [typ_u, term_u] => do_term extra_ts (SOME (typ_of_atp_term ctxt typ_u)) term_u | 
| 54789 | 368 | | _ => raise ATP_TERM us) | 
| 369 | else if s' = predicator_name then | |
| 69593 | 370 | do_term [] (SOME \<^typ>\<open>bool\<close>) (hd us) | 
| 54789 | 371 | else if s' = app_op_name then | 
| 372 | let val extra_t = do_term [] NONE (List.last us) in | |
| 373 | do_term (extra_t :: extra_ts) | |
| 57199 | 374 | (case opt_T of SOME T => SOME (slack_fastype_of extra_t --> T) | NONE => NONE) | 
| 375 | (nth us (length us - 2)) | |
| 54789 | 376 | end | 
| 377 | else if s' = type_guard_name then | |
| 74379 | 378 | \<^Const>\<open>True\<close> (* ignore type predicates *) | 
| 54789 | 379 | else | 
| 380 | let | |
| 381 | val new_skolem = String.isPrefix new_skolem_const_prefix s'' | |
| 54818 | 382 | val num_ty_args = length us - the_default 0 (Symtab.lookup sym_tab s) | 
| 383 | val (type_us, term_us) = chop num_ty_args us |>> append mangled_us | |
| 54789 | 384 | val term_ts = map (do_term [] NONE) term_us | 
| 54818 | 385 | |
| 386 | val Ts = map (typ_of_atp_type ctxt) tys @ map (typ_of_atp_term ctxt) type_us | |
| 54789 | 387 | val T = | 
| 54818 | 388 | (if not (null Ts) andalso robust_const_num_type_args thy s' = length Ts then | 
| 57199 | 389 | if new_skolem then SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts)) | 
| 390 | else if textual then try (Sign.const_instance thy) (s', Ts) | |
| 391 | else NONE | |
| 54789 | 392 | else | 
| 393 | NONE) | |
| 394 | |> (fn SOME T => T | |
| 395 | | NONE => | |
| 56254 | 396 | map slack_fastype_of term_ts ---> | 
| 69593 | 397 | the_default (Type_Infer.anyT \<^sort>\<open>type\<close>) opt_T) | 
| 54789 | 398 | val t = | 
| 399 | if new_skolem then Var ((new_skolem_var_name_of_const s'', var_index), T) | |
| 400 | else Const (unproxify_const s', T) | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 401 | in | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 402 | list_comb (t, term_ts @ extra_ts) | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 403 | end | 
| 54789 | 404 | end | 
| 405 | | NONE => (* a free or schematic variable *) | |
| 406 | let | |
| 407 | val term_ts = | |
| 408 | map (do_term [] NONE) us | |
| 409 | (* SPASS (3.8ds) and Vampire (2.6) pass arguments to Skolem functions in reverse | |
| 57699 | 410 | order, which is incompatible with "metis"'s new skolemizer. *) | 
| 54789 | 411 | |> exists (fn pre => String.isPrefix pre s) | 
| 412 | [spass_skolem_prefix, vampire_skolem_prefix] ? rev | |
| 413 | val ts = term_ts @ extra_ts | |
| 414 | val T = | |
| 58500 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 415 | (case tys of | 
| 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 416 | [ty] => typ_of_atp_type ctxt ty | 
| 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 417 | | _ => | 
| 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 418 | (case opt_T of | 
| 
430306aa03b1
proper types for applied variables, for typed formats (TFF0, DFG)
 blanchet parents: 
58484diff
changeset | 419 | SOME T => map slack_fastype_of term_ts ---> T | 
| 69593 | 420 | | NONE => map slack_fastype_of ts ---> Type_Infer.anyT \<^sort>\<open>type\<close>)) | 
| 54789 | 421 | val t = | 
| 422 | (case unprefix_and_unascii fixed_var_prefix s of | |
| 423 | SOME s => Free (s, T) | |
| 36909 
7d5587f6d5f7
made Sledgehammer's full-typed proof reconstruction work for the first time;
 blanchet parents: 
36607diff
changeset | 424 | | NONE => | 
| 55185 
a0bd8d6414e6
centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
 blanchet parents: 
54822diff
changeset | 425 | if textual andalso not (is_tptp_variable s) then | 
| 57790 | 426 | Free (s |> textual ? fresh_up ctxt, T) | 
| 55185 
a0bd8d6414e6
centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
 blanchet parents: 
54822diff
changeset | 427 | else | 
| 57789 
a73255cffb5b
don't rename variables which will be renamed later anyway
 blanchet parents: 
57788diff
changeset | 428 | Var ((repair_var_name s, var_index), T)) | 
| 54789 | 429 | in list_comb (t, ts) end)) | 
| 43093 | 430 | in do_term [] end | 
| 21978 
72c21698a055
Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
 paulson parents: diff
changeset | 431 | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 432 | fun term_of_atp ctxt (ATP_Problem.THF _) type_enc = | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 433 | if ATP_Problem_Generate.is_type_enc_higher_order type_enc then K (term_of_atp_ho ctxt) | 
| 63692 | 434 | else error "Unsupported Isar reconstruction" | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 435 | | term_of_atp ctxt _ type_enc = | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 436 | if not (ATP_Problem_Generate.is_type_enc_higher_order type_enc) then term_of_atp_fo ctxt | 
| 63692 | 437 | else error "Unsupported Isar reconstruction" | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 438 | |
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 439 | fun term_of_atom ctxt format type_enc textual sym_tab pos (u as ATerm ((s, _), _)) = | 
| 38014 | 440 | if String.isPrefix class_prefix s then | 
| 52031 
9a9238342963
tuning -- renamed '_from_' to '_of_' in Sledgehammer
 blanchet parents: 
51878diff
changeset | 441 | add_type_constraint pos (type_constraint_of_term ctxt u) | 
| 74379 | 442 | #> pair \<^Const>\<open>True\<close> | 
| 38014 | 443 | else | 
| 69593 | 444 | pair (term_of_atp ctxt format type_enc textual sym_tab (SOME \<^typ>\<open>bool\<close>) u) | 
| 36402 
1b20805974c7
introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
 blanchet parents: 
36396diff
changeset | 445 | |
| 37991 | 446 | (* Update schematic type variables with detected sort constraints. It's not | 
| 42563 | 447 | totally clear whether this code is necessary. *) | 
| 38014 | 448 | fun repair_tvar_sorts (t, tvar_tab) = | 
| 36909 
7d5587f6d5f7
made Sledgehammer's full-typed proof reconstruction work for the first time;
 blanchet parents: 
36607diff
changeset | 449 | let | 
| 37991 | 450 | fun do_type (Type (a, Ts)) = Type (a, map do_type Ts) | 
| 451 | | do_type (TVar (xi, s)) = | |
| 452 | TVar (xi, the_default s (Vartab.lookup tvar_tab xi)) | |
| 453 | | do_type (TFree z) = TFree z | |
| 454 | fun do_term (Const (a, T)) = Const (a, do_type T) | |
| 455 | | do_term (Free (a, T)) = Free (a, do_type T) | |
| 456 | | do_term (Var (xi, T)) = Var (xi, do_type T) | |
| 457 | | do_term (t as Bound _) = t | |
| 458 | | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t) | |
| 459 | | do_term (t1 $ t2) = do_term t1 $ do_term t2 | |
| 460 | in t |> not (Vartab.is_empty tvar_tab) ? do_term end | |
| 461 | ||
| 54811 | 462 | (* Interpret an ATP formula as a HOL term, extracting sort constraints as they appear in the | 
| 463 | formula. *) | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 464 | fun prop_of_atp ctxt format type_enc textual sym_tab phi = | 
| 38014 | 465 | let | 
| 466 | fun do_formula pos phi = | |
| 54789 | 467 | (case phi of | 
| 38014 | 468 | AQuant (_, [], phi) => do_formula pos phi | 
| 42526 | 469 | | AQuant (q, (s, _) :: xs, phi') => | 
| 38014 | 470 | do_formula pos (AQuant (q, xs, phi')) | 
| 42526 | 471 | (* FIXME: TFF *) | 
| 57257 | 472 | #>> quantify_over_var textual (case q of AForall => forall_of | AExists => exists_of) | 
| 57789 
a73255cffb5b
don't rename variables which will be renamed later anyway
 blanchet parents: 
57788diff
changeset | 473 | (repair_var_name s) dummyT | 
| 38014 | 474 | | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not | 
| 37991 | 475 | | AConn (c, [phi1, phi2]) => | 
| 38014 | 476 | do_formula (pos |> c = AImplies ? not) phi1 | 
| 477 | ##>> do_formula pos phi2 | |
| 478 | #>> (case c of | |
| 54811 | 479 | AAnd => s_conj | 
| 480 | | AOr => s_disj | |
| 481 | | AImplies => s_imp | |
| 482 | | AIff => s_iff | |
| 483 | | ANot => raise Fail "impossible connective") | |
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 484 | | AAtom tm => term_of_atom ctxt format type_enc textual sym_tab pos tm | 
| 54789 | 485 | | _ => raise ATP_FORMULA [phi]) | 
| 486 | in | |
| 487 | repair_tvar_sorts (do_formula true phi Vartab.empty) | |
| 488 | end | |
| 37991 | 489 | |
| 54500 | 490 | val unprefix_fact_number = space_implode "_" o tl o space_explode "_" | 
| 491 | ||
| 57263 | 492 | fun resolve_fact facts s = | 
| 54789 | 493 | (case try (unprefix fact_prefix) s of | 
| 54500 | 494 | SOME s' => | 
| 495 | let val s' = s' |> unprefix_fact_number |> unascii_of in | |
| 57263 | 496 | AList.lookup (op =) facts s' |> Option.map (pair s') | 
| 54500 | 497 | end | 
| 54789 | 498 | | NONE => NONE) | 
| 54506 | 499 | |
| 57263 | 500 | fun resolve_conjecture s = | 
| 54789 | 501 | (case try (unprefix conjecture_prefix) s of | 
| 54500 | 502 | SOME s' => Int.fromString s' | 
| 54789 | 503 | | NONE => NONE) | 
| 54500 | 504 | |
| 57263 | 505 | fun resolve_facts facts = map_filter (resolve_fact facts) | 
| 506 | val resolve_conjectures = map_filter resolve_conjecture | |
| 54500 | 507 | |
| 508 | fun is_axiom_used_in_proof pred = | |
| 509 | exists (fn ((_, ss), _, _, _, []) => exists pred ss | _ => false) | |
| 510 | ||
| 70940 
ce3a05ad07b7
added support for Zipperposition on SystemOnTPTP
 blanchet parents: 
69597diff
changeset | 511 | fun add_fact ctxt facts ((num, ss), _, _, rule, deps) = | 
| 58653 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 512 | (if member (op =) [agsyhol_core_rule, leo2_extcnf_equal_neg_rule] rule orelse | 
| 
4b44c227c0e0
improved handling of extensionality in Isar proofs generated from LEO-II and Satallax
 blanchet parents: 
58652diff
changeset | 513 | String.isPrefix satallax_tab_rule_prefix rule then | 
| 56104 
fd6e132ee4fb
correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
 blanchet parents: 
55285diff
changeset | 514 | insert (op =) (short_thm_name ctxt ext, (Global, General)) | 
| 54500 | 515 | else | 
| 516 | I) | |
| 70940 
ce3a05ad07b7
added support for Zipperposition on SystemOnTPTP
 blanchet parents: 
69597diff
changeset | 517 | #> (if null deps then union (op =) (resolve_facts facts (num :: ss)) else I) | 
| 54500 | 518 | |
| 57263 | 519 | fun used_facts_in_atp_proof ctxt facts atp_proof = | 
| 520 | if null atp_proof then facts else fold (add_fact ctxt facts) atp_proof [] | |
| 54500 | 521 | |
| 522 | fun used_facts_in_unsound_atp_proof _ _ [] = NONE | |
| 57263 | 523 | | used_facts_in_unsound_atp_proof ctxt facts atp_proof = | 
| 72967 
11de287ed481
tweaked tptp parsing when source info is missing
 desharna parents: 
72401diff
changeset | 524 | let | 
| 
11de287ed481
tweaked tptp parsing when source info is missing
 desharna parents: 
72401diff
changeset | 525 | val used_facts = used_facts_in_atp_proof ctxt facts atp_proof | 
| 
11de287ed481
tweaked tptp parsing when source info is missing
 desharna parents: 
72401diff
changeset | 526 | val all_global_facts = forall (fn (_, (sc, _)) => sc = Global) used_facts | 
| 
11de287ed481
tweaked tptp parsing when source info is missing
 desharna parents: 
72401diff
changeset | 527 | val axiom_used = is_axiom_used_in_proof (is_some o resolve_conjecture) atp_proof | 
| 
11de287ed481
tweaked tptp parsing when source info is missing
 desharna parents: 
72401diff
changeset | 528 | in | 
| 
11de287ed481
tweaked tptp parsing when source info is missing
 desharna parents: 
72401diff
changeset | 529 | if all_global_facts andalso not axiom_used then | 
| 54500 | 530 | SOME (map fst used_facts) | 
| 531 | else | |
| 532 | NONE | |
| 533 | end | |
| 534 | ||
| 535 | val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix | |
| 536 | ||
| 537 | (* overapproximation (good enough) *) | |
| 538 | fun is_lam_lifted s = | |
| 539 | String.isPrefix fact_prefix s andalso | |
| 540 | String.isSubstring ascii_of_lam_fact_prefix s | |
| 541 | ||
| 542 | val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix) | |
| 543 | ||
| 55257 | 544 | fun atp_proof_prefers_lifting atp_proof = | 
| 545 | (is_axiom_used_in_proof is_combinator_def atp_proof, | |
| 546 | is_axiom_used_in_proof is_lam_lifted atp_proof) = (false, true) | |
| 54500 | 547 | |
| 548 | val is_typed_helper_name = | |
| 549 | String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix | |
| 550 | ||
| 551 | fun is_typed_helper_used_in_atp_proof atp_proof = | |
| 552 | is_axiom_used_in_proof is_typed_helper_name atp_proof | |
| 553 | ||
| 54772 | 554 | fun replace_one_dependency (old, new) dep = if is_same_atp_step dep old then new else [dep] | 
| 555 | fun replace_dependencies_in_line old_new (name, role, t, rule, deps) = | |
| 556 | (name, role, t, rule, fold (union (op =) o replace_one_dependency old_new) deps []) | |
| 557 | ||
| 54499 | 558 | fun repair_name "$true" = "c_True" | 
| 559 | | repair_name "$false" = "c_False" | |
| 560 | | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *) | |
| 561 | | repair_name s = | |
| 562 | if is_tptp_equal s orelse | |
| 563 | (* seen in Vampire proofs *) | |
| 564 | (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then | |
| 565 | tptp_equal | |
| 566 | else | |
| 567 | s | |
| 568 | ||
| 57699 | 569 | fun set_var_index j = map_aterms (fn Var ((s, 0), T) => Var ((s, j), T) | t => t) | 
| 57635 
97adb86619a4
more robust handling of types for skolems (modeled as Frees)
 blanchet parents: 
57547diff
changeset | 570 | |
| 57699 | 571 | fun infer_formulas_types ctxt = | 
| 57635 
97adb86619a4
more robust handling of types for skolems (modeled as Frees)
 blanchet parents: 
57547diff
changeset | 572 | map_index (uncurry (fn j => set_var_index j #> Type.constraint HOLogic.boolT)) | 
| 
97adb86619a4
more robust handling of types for skolems (modeled as Frees)
 blanchet parents: 
57547diff
changeset | 573 | #> Syntax.check_terms (Proof_Context.set_mode Proof_Context.mode_schematic ctxt) | 
| 54499 | 574 | |
| 575 | val combinator_table = | |
| 69593 | 576 |   [(\<^const_name>\<open>Meson.COMBI\<close>, @{thm Meson.COMBI_def [abs_def]}),
 | 
| 577 |    (\<^const_name>\<open>Meson.COMBK\<close>, @{thm Meson.COMBK_def [abs_def]}),
 | |
| 578 |    (\<^const_name>\<open>Meson.COMBB\<close>, @{thm Meson.COMBB_def [abs_def]}),
 | |
| 579 |    (\<^const_name>\<open>Meson.COMBC\<close>, @{thm Meson.COMBC_def [abs_def]}),
 | |
| 580 |    (\<^const_name>\<open>Meson.COMBS\<close>, @{thm Meson.COMBS_def [abs_def]})]
 | |
| 54499 | 581 | |
| 582 | fun uncombine_term thy = | |
| 583 | let | |
| 57717 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 584 | fun uncomb (t1 $ t2) = betapply (uncomb t1, uncomb t2) | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 585 | | uncomb (Abs (s, T, t)) = Abs (s, T, uncomb t) | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 586 | | uncomb (t as Const (x as (s, _))) = | 
| 54499 | 587 | (case AList.lookup (op =) combinator_table s of | 
| 59582 | 588 | SOME thm => thm |> Thm.prop_of |> specialize_type thy x |> Logic.dest_equals |> snd | 
| 54756 | 589 | | NONE => t) | 
| 57717 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 590 | | uncomb t = t | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 591 | in uncomb end | 
| 54499 | 592 | |
| 57717 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 593 | fun unlift_aterm lifted (t as Const (s, _)) = | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 594 | if String.isPrefix lam_lifted_prefix s then | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 595 | (* FIXME: do something about the types *) | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 596 | (case AList.lookup (op =) lifted s of | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 597 | SOME t' => unlift_term lifted t' | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 598 | | NONE => t) | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 599 | else | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 600 | t | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 601 | | unlift_aterm _ t = t | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 602 | and unlift_term lifted = | 
| 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 603 | map_aterms (unlift_aterm lifted) | 
| 54499 | 604 | |
| 57256 | 605 | fun termify_line _ _ _ _ _ (_, Type_Role, _, _, _) = NONE | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 606 | | termify_line ctxt format type_enc lifted sym_tab (name, role, u, rule, deps) = | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 607 | let | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 608 | val thy = Proof_Context.theory_of ctxt | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 609 | val t = u | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 610 | |> prop_of_atp ctxt format type_enc true sym_tab | 
| 57717 
949838aba487
unlift before uncombine, because the definition of a lambda-lifted symbol might have an SK combinator in it (in hybrid encodings)
 blanchet parents: 
57713diff
changeset | 611 | |> unlift_term lifted | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 612 | |> uncombine_term thy | 
| 57768 
a63f14f1214c
fine-tuned Isar reconstruction, esp. boolean simplifications
 blanchet parents: 
57767diff
changeset | 613 | |> simplify_bool | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 614 | in | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 615 | SOME (name, role, t, rule, deps) | 
| 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 616 | end | 
| 54499 | 617 | |
| 618 | val waldmeister_conjecture_num = "1.0.0.0" | |
| 619 | ||
| 54756 | 620 | fun repair_waldmeister_endgame proof = | 
| 54499 | 621 | let | 
| 74379 | 622 | fun repair_tail (name, _, \<^Const>\<open>Trueprop for t\<close>, rule, deps) = | 
| 623 | (name, Negated_Conjecture, \<^Const>\<open>Trueprop for \<open>s_not t\<close>\<close>, rule, deps) | |
| 54756 | 624 | fun repair_body [] = [] | 
| 625 | | repair_body ((line as ((num, _), _, _, _, _)) :: lines) = | |
| 626 | if num = waldmeister_conjecture_num then map repair_tail (line :: lines) | |
| 627 | else line :: repair_body lines | |
| 628 | in | |
| 629 | repair_body proof | |
| 630 | end | |
| 54499 | 631 | |
| 57635 
97adb86619a4
more robust handling of types for skolems (modeled as Frees)
 blanchet parents: 
57547diff
changeset | 632 | fun map_proof_terms f (lines : ('a * 'b * 'c * 'd * 'e) list) =
 | 
| 
97adb86619a4
more robust handling of types for skolems (modeled as Frees)
 blanchet parents: 
57547diff
changeset | 633 | map2 (fn c => fn (a, b, _, d, e) => (a, b, c, d, e)) (f (map #3 lines)) lines | 
| 
97adb86619a4
more robust handling of types for skolems (modeled as Frees)
 blanchet parents: 
57547diff
changeset | 634 | |
| 57258 
67d85a8aa6cc
Moving the remote prefix deleting on Sledgehammer's side
 fleury parents: 
57257diff
changeset | 635 | fun termify_atp_proof ctxt local_prover format type_enc pool lifted sym_tab = | 
| 57267 
8b87114357bd
integrated new Waldmeister code with 'sledgehammer' command
 blanchet parents: 
57263diff
changeset | 636 | nasty_atp_proof pool | 
| 54499 | 637 | #> map_term_names_in_atp_proof repair_name | 
| 57255 
488046fdda59
add support for Isar reconstruction for thf1 ATP provers like Leo-II.
 fleury parents: 
57199diff
changeset | 638 | #> map_filter (termify_line ctxt format type_enc lifted sym_tab) | 
| 57699 | 639 | #> map_proof_terms (infer_formulas_types ctxt #> map HOLogic.mk_Trueprop) | 
| 57258 
67d85a8aa6cc
Moving the remote prefix deleting on Sledgehammer's side
 fleury parents: 
57257diff
changeset | 640 | #> local_prover = waldmeisterN ? repair_waldmeister_endgame | 
| 54499 | 641 | |
| 58091 | 642 | fun unskolemize_term skos = | 
| 55192 
b75b52c7cf94
unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
 blanchet parents: 
55185diff
changeset | 643 | let | 
| 58597 | 644 | val is_skolem_name = member (op =) skos | 
| 55192 
b75b52c7cf94
unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
 blanchet parents: 
55185diff
changeset | 645 | |
| 58091 | 646 | fun find_argless_skolem (Free _ $ Var _) = NONE | 
| 58597 | 647 | | find_argless_skolem (Free (x as (s, _))) = if is_skolem_name s then SOME x else NONE | 
| 58091 | 648 | | find_argless_skolem (t $ u) = | 
| 649 | (case find_argless_skolem t of NONE => find_argless_skolem u | sk => sk) | |
| 650 | | find_argless_skolem (Abs (_, _, t)) = find_argless_skolem t | |
| 651 | | find_argless_skolem _ = NONE | |
| 55192 
b75b52c7cf94
unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
 blanchet parents: 
55185diff
changeset | 652 | |
| 58597 | 653 | fun find_skolem_arg (Free (s, _) $ Var v) = if is_skolem_name s then SOME v else NONE | 
| 58091 | 654 | | find_skolem_arg (t $ u) = (case find_skolem_arg t of NONE => find_skolem_arg u | v => v) | 
| 655 | | find_skolem_arg (Abs (_, _, t)) = find_skolem_arg t | |
| 656 | | find_skolem_arg _ = NONE | |
| 657 | ||
| 58597 | 658 | fun kill_skolem_arg (t as Free (s, T) $ Var _) = | 
| 659 | if is_skolem_name s then Free (s, range_type T) else t | |
| 660 | | kill_skolem_arg (t $ u) = kill_skolem_arg t $ kill_skolem_arg u | |
| 661 | | kill_skolem_arg (Abs (s, T, t)) = Abs (s, T, kill_skolem_arg t) | |
| 662 | | kill_skolem_arg t = t | |
| 663 | ||
| 58091 | 664 | fun find_var (Var v) = SOME v | 
| 665 | | find_var (t $ u) = (case find_var t of NONE => find_var u | v => v) | |
| 666 | | find_var (Abs (_, _, t)) = find_var t | |
| 667 | | find_var _ = NONE | |
| 668 | ||
| 669 | val safe_abstract_over = abstract_over o apsnd (incr_boundvars 1) | |
| 55192 
b75b52c7cf94
unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
 blanchet parents: 
55185diff
changeset | 670 | |
| 58091 | 671 | fun unskolem t = | 
| 672 | (case find_argless_skolem t of | |
| 673 | SOME (x as (s, T)) => | |
| 674 | HOLogic.exists_const T $ Abs (s, T, unskolem (safe_abstract_over (Free x, t))) | |
| 675 | | NONE => | |
| 58597 | 676 | (case find_skolem_arg t of | 
| 58091 | 677 | SOME (v as ((s, _), T)) => | 
| 678 | let | |
| 679 | val (haves, have_nots) = | |
| 680 | HOLogic.disjuncts t | |
| 58597 | 681 | |> List.partition (exists_subterm (curry (op =) (Var v))) | 
| 69593 | 682 | |> apply2 (fn lits => fold (curry s_disj) lits \<^term>\<open>False\<close>) | 
| 58091 | 683 | in | 
| 684 | s_disj (HOLogic.all_const T | |
| 685 | $ Abs (s, T, unskolem (safe_abstract_over (Var v, kill_skolem_arg haves))), | |
| 686 | unskolem have_nots) | |
| 687 | end | |
| 58597 | 688 | | NONE => | 
| 689 | (case find_var t of | |
| 690 | SOME (v as ((s, _), T)) => | |
| 691 | HOLogic.all_const T $ Abs (s, T, unskolem (safe_abstract_over (Var v, t))) | |
| 692 | | NONE => t))) | |
| 55195 | 693 | in | 
| 58091 | 694 | HOLogic.mk_Trueprop o unskolem o HOLogic.dest_Trueprop | 
| 55192 
b75b52c7cf94
unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
 blanchet parents: 
55185diff
changeset | 695 | end | 
| 
b75b52c7cf94
unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
 blanchet parents: 
55185diff
changeset | 696 | |
| 57788 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 697 | fun rename_skolem_args t = | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 698 | let | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 699 | fun add_skolem_args (Abs (_, _, t)) = add_skolem_args t | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 700 | | add_skolem_args t = | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 701 | (case strip_comb t of | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 702 | (Free (s, _), args as _ :: _) => | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 703 | if String.isPrefix spass_skolem_prefix s then | 
| 67522 | 704 | insert (op =) (s, take_prefix is_Var args) | 
| 57788 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 705 | else | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 706 | fold add_skolem_args args | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 707 | | (u, args as _ :: _) => fold add_skolem_args (u :: args) | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 708 | | _ => I) | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 709 | |
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 710 | fun subst_of_skolem (sk, args) = | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 711 | map_index (fn (j, Var (z, T)) => (z, Var ((sk ^ "_" ^ string_of_int j, 0), T))) args | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 712 | |
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 713 | val subst = maps subst_of_skolem (add_skolem_args t []) | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 714 | in | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 715 | subst_vars ([], subst) t | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 716 | end | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 717 | |
| 72401 
2783779b7dd3
removed obsolete unmaintained experimental prover Pirate
 blanchet parents: 
70940diff
changeset | 718 | fun introduce_spass_skolems proof = | 
| 57787 | 719 | let | 
| 57788 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 720 | fun add_skolem (Free (s, _)) = String.isPrefix spass_skolem_prefix s ? insert (op =) s | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 721 | | add_skolem _ = I | 
| 54772 | 722 | |
| 57787 | 723 | (* union-find would be faster *) | 
| 724 | fun add_cycle [] = I | |
| 725 | | add_cycle ss = | |
| 726 | fold (fn s => Graph.default_node (s, ())) ss | |
| 727 | #> fold Graph.add_edge (ss ~~ tl ss @ [hd ss]) | |
| 54772 | 728 | |
| 57787 | 729 | val (input_steps, other_steps) = List.partition (null o #5) proof | 
| 54772 | 730 | |
| 58597 | 731 | (* The names of the formulas are added to the Skolem constants, to ensure that formulas giving | 
| 732 | rise to several clauses are skolemized together. *) | |
| 733 | val skoXss = map (fn ((_, ss), _, t, _, _) => Term.fold_aterms add_skolem t ss) input_steps | |
| 58601 
85fa90262807
avoid creating needless skolemization steps for SPASS
 blanchet parents: 
58600diff
changeset | 734 | val groups0 = Graph.strong_conn (fold add_cycle skoXss Graph.empty) | 
| 
85fa90262807
avoid creating needless skolemization steps for SPASS
 blanchet parents: 
58600diff
changeset | 735 | val groups = filter (exists (String.isPrefix spass_skolem_prefix)) groups0 | 
| 
85fa90262807
avoid creating needless skolemization steps for SPASS
 blanchet parents: 
58600diff
changeset | 736 | |
| 
85fa90262807
avoid creating needless skolemization steps for SPASS
 blanchet parents: 
58600diff
changeset | 737 | val skoXss_input_steps = skoXss ~~ input_steps | 
| 54772 | 738 | |
| 58597 | 739 | fun step_name_of_group skoXs = (implode skoXs, []) | 
| 57787 | 740 | fun in_group group = member (op =) group o hd | 
| 58601 
85fa90262807
avoid creating needless skolemization steps for SPASS
 blanchet parents: 
58600diff
changeset | 741 | fun group_of skoX = find_first (fn group => in_group group skoX) groups | 
| 54772 | 742 | |
| 58652 | 743 | fun new_steps (skoXss_steps : (string list * (term, 'a) atp_step) list) group = | 
| 57787 | 744 | let | 
| 745 | val name = step_name_of_group group | |
| 746 | val name0 = name |>> prefix "0" | |
| 57788 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 747 | val t = | 
| 58597 | 748 | (case map (snd #> #3) skoXss_steps of | 
| 57788 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 749 | [t] => t | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 750 | | ts => ts | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 751 | |> map (HOLogic.dest_Trueprop #> rename_skolem_args) | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 752 | |> Library.foldr1 s_conj | 
| 
0a38c47ebb69
normalize skolem argument variable names so that they coincide when taking the conjunction
 blanchet parents: 
57787diff
changeset | 753 | |> HOLogic.mk_Trueprop) | 
| 58597 | 754 | val skos = | 
| 755 | fold (union (op =) o filter (String.isPrefix spass_skolem_prefix) o fst) skoXss_steps [] | |
| 756 | val deps = map (snd #> #1) skoXss_steps | |
| 57787 | 757 | in | 
| 758 | [(name0, Unknown, unskolemize_term skos t, spass_pre_skolemize_rule, deps), | |
| 759 | (name, Unknown, t, spass_skolemize_rule, [name0])] | |
| 760 | end | |
| 54772 | 761 | |
| 57787 | 762 | val sko_steps = | 
| 58597 | 763 | maps (fn group => new_steps (filter (in_group group o fst) skoXss_input_steps) group) groups | 
| 54772 | 764 | |
| 57787 | 765 | val old_news = | 
| 58601 
85fa90262807
avoid creating needless skolemization steps for SPASS
 blanchet parents: 
58600diff
changeset | 766 | map_filter (fn (skoXs, (name, _, _, _, _)) => | 
| 
85fa90262807
avoid creating needless skolemization steps for SPASS
 blanchet parents: 
58600diff
changeset | 767 | Option.map (pair name o single o step_name_of_group) (group_of skoXs)) | 
| 58597 | 768 | skoXss_input_steps | 
| 57787 | 769 | val repair_deps = fold replace_dependencies_in_line old_news | 
| 770 | in | |
| 771 | input_steps @ sko_steps @ map repair_deps other_steps | |
| 772 | end | |
| 54772 | 773 | |
| 57263 | 774 | fun factify_atp_proof facts hyp_ts concl_t atp_proof = | 
| 54505 | 775 | let | 
| 57785 | 776 | fun factify_step ((num, ss), role, t, rule, deps) = | 
| 54505 | 777 | let | 
| 778 | val (ss', role', t') = | |
| 57263 | 779 | (case resolve_conjectures ss of | 
| 54505 | 780 | [j] => | 
| 58484 
b4c0e2b00036
support hypotheses with schematics in Isar proofs
 blanchet parents: 
58477diff
changeset | 781 | if j = length hyp_ts then ([], Conjecture, concl_t) | 
| 
b4c0e2b00036
support hypotheses with schematics in Isar proofs
 blanchet parents: 
58477diff
changeset | 782 | else ([], Hypothesis, close_form (nth hyp_ts j)) | 
| 55185 
a0bd8d6414e6
centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
 blanchet parents: 
54822diff
changeset | 783 | | _ => | 
| 70940 
ce3a05ad07b7
added support for Zipperposition on SystemOnTPTP
 blanchet parents: 
69597diff
changeset | 784 | (case resolve_facts facts (num :: ss) of | 
| 75123 
66eb6fdfc244
handle Zipperposition definitions in Isar proof construction
 blanchet parents: 
75052diff
changeset | 785 | [] => (ss, if member (op =) [Definition, Lemma] role then role else Plain, t) | 
| 55185 
a0bd8d6414e6
centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
 blanchet parents: 
54822diff
changeset | 786 | | facts => (map fst facts, Axiom, t))) | 
| 54505 | 787 | in | 
| 788 | ((num, ss'), role', t', rule, deps) | |
| 789 | end | |
| 790 | ||
| 791 | val atp_proof = map factify_step atp_proof | |
| 792 | val names = map #1 atp_proof | |
| 793 | ||
| 794 | fun repair_dep (num, ss) = (num, the_default ss (AList.lookup (op =) names num)) | |
| 795 | fun repair_deps (name, role, t, rule, deps) = (name, role, t, rule, map repair_dep deps) | |
| 54772 | 796 | in | 
| 797 | map repair_deps atp_proof | |
| 798 | end | |
| 54505 | 799 | |
| 31038 | 800 | end; |