src/HOL/Tools/ATP/atp_proof_reconstruct.ML
author fleury
Mon, 16 Jun 2014 16:21:52 +0200
changeset 57258 67d85a8aa6cc
parent 57257 0d5e34ba4d28
child 57263 2b6a96cc64c9
permissions -rw-r--r--
Moving the remote prefix deleting on Sledgehammer's side
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
     1
(*  Title:      HOL/Tools/ATP/atp_proof_reconstruct.ML
38027
505657ddb047 standardize "Author" tags
blanchet
parents: 38019
diff changeset
     2
    Author:     Lawrence C. Paulson, Cambridge University Computer Laboratory
505657ddb047 standardize "Author" tags
blanchet
parents: 38019
diff changeset
     3
    Author:     Claire Quigley, Cambridge University Computer Laboratory
36392
c00c57850eb7 cosmetics: rename internal functions
blanchet
parents: 36369
diff changeset
     4
    Author:     Jasmin Blanchette, TU Muenchen
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
     5
49914
23e36a4d28f1 refactor code
blanchet
parents: 49881
diff changeset
     6
Basic proof reconstruction from ATP proofs.
33310
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     7
*)
44f9665c2091 proper header;
wenzelm
parents: 33243
diff changeset
     8
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
     9
signature ATP_PROOF_RECONSTRUCT =
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    10
sig
54811
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
    11
  type 'a atp_type = 'a ATP_Problem.atp_type
53586
bd5fa6425993 prefixed types and some functions with "atp_" for disambiguation
blanchet
parents: 52758
diff 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: 52758
diff changeset
    13
  type ('a, 'b, 'c, 'd) atp_formula = ('a, 'b, 'c, 'd) ATP_Problem.atp_formula
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
    14
  type stature = ATP_Problem_Generate.stature
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
    15
  type atp_step_name = ATP_Proof.atp_step_name
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
    16
  type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
    17
  type 'a atp_proof = 'a ATP_Proof.atp_proof
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    18
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    19
  val metisN : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    20
  val full_typesN : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    21
  val partial_typesN : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    22
  val no_typesN : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    23
  val really_full_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    24
  val full_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    25
  val partial_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    26
  val no_type_enc : string
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    27
  val full_type_encs : string list
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    28
  val partial_type_encs : string list
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
    29
  val default_metis_lam_trans : string
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
    30
49983
33e18e9916a8 use metaquantification when possible in Isar proofs
blanchet
parents: 49914
diff changeset
    31
  val forall_of : term -> term -> term
33e18e9916a8 use metaquantification when possible in Isar proofs
blanchet
parents: 49914
diff changeset
    32
  val exists_of : term -> term -> term
55285
e88ad20035f4 merged 'reconstructors' and 'proof methods'
blanchet
parents: 55257
diff changeset
    33
  val type_enc_aliases : (string * string list) list
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    34
  val unalias_type_enc : string -> string list
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
    35
  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: 57199
diff changeset
    36
    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: 57199
diff changeset
    37
  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: 57199
diff changeset
    38
    bool -> int Symtab.table ->
54811
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
    39
    (string, string, (string, string atp_type) atp_term, string) atp_formula -> term
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
    40
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
    41
  val used_facts_in_atp_proof :
54507
d983a020489d whitespace tuning
blanchet
parents: 54506
diff changeset
    42
    Proof.context -> (string * stature) list vector -> string atp_proof -> (string * stature) list
d983a020489d whitespace tuning
blanchet
parents: 54506
diff changeset
    43
  val used_facts_in_unsound_atp_proof : Proof.context -> (string * stature) list vector ->
d983a020489d whitespace tuning
blanchet
parents: 54506
diff changeset
    44
    'a atp_proof -> string list option
55257
abfd7b90bba2 rationalized threading of 'metis' arguments
blanchet
parents: 55195
diff changeset
    45
  val atp_proof_prefers_lifting : string atp_proof -> bool
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
    46
  val is_typed_helper_used_in_atp_proof : string atp_proof -> bool
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
    47
  val replace_dependencies_in_line : atp_step_name * atp_step_name list -> ('a, 'b) atp_step ->
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
    48
    ('a, 'b) atp_step
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
    49
  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: 57199
diff changeset
    50
    ATP_Problem_Generate.type_enc -> string Symtab.table -> (string * term) list ->
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
    51
    int Symtab.table -> string atp_proof -> (term, string) atp_step list
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
    52
  val introduce_spass_skolem : (term, string) atp_step list -> (term, string) atp_step list
54505
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
    53
  val factify_atp_proof : (string * 'a) list vector -> term list -> term ->
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
    54
    (term, string) atp_step list -> (term, string) atp_step list
24425
ca97c6f3d9cd Returning both a "one-line" proof and a structured proof
paulson
parents: 24387
diff changeset
    55
end;
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    56
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
    57
structure ATP_Proof_Reconstruct : ATP_PROOF_RECONSTRUCT =
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    58
struct
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    59
43085
0a2f5b86bdd7 first step in sharing more code between ATP and Metis translation
blanchet
parents: 43062
diff changeset
    60
open ATP_Util
38028
22dcaec5fa77 minor refactoring
blanchet
parents: 38027
diff changeset
    61
open ATP_Problem
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39425
diff changeset
    62
open ATP_Proof
46320
0b8b73b49848 renamed two files to make room for a new file
blanchet
parents: 45887
diff changeset
    63
open ATP_Problem_Generate
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
    64
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    65
val metisN = "metis"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    66
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    67
val full_typesN = "full_types"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    68
val partial_typesN = "partial_types"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    69
val no_typesN = "no_types"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    70
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    71
val really_full_type_enc = "mono_tags"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    72
val full_type_enc = "poly_guards_query"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    73
val partial_type_enc = "poly_args"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    74
val no_type_enc = "erased"
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    75
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    76
val full_type_encs = [full_type_enc, really_full_type_enc]
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    77
val partial_type_encs = partial_type_enc :: full_type_encs
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    78
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    79
val type_enc_aliases =
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    80
  [(full_typesN, full_type_encs),
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    81
   (partial_typesN, partial_type_encs),
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    82
   (no_typesN, [no_type_enc])]
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    83
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    84
fun unalias_type_enc s =
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    85
  AList.lookup (op =) type_enc_aliases s |> the_default [s]
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    86
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
    87
val default_metis_lam_trans = combsN
45519
cd6e78cb6ee8 make metis reconstruction handling more flexible
blanchet
parents: 45511
diff changeset
    88
50670
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 49983
diff changeset
    89
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: 52031
diff changeset
    90
  | term_name' _ = ""
50670
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 49983
diff changeset
    91
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 49983
diff changeset
    92
fun lambda' v = Term.lambda_name (term_name' v, v)
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 49983
diff changeset
    93
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 49983
diff changeset
    94
fun forall_of v t = HOLogic.all_const (fastype_of v) $ lambda' v t
eaa540986291 properly take the existential closure of skolems
blanchet
parents: 49983
diff changeset
    95
fun exists_of v t = HOLogic.exists_const (fastype_of v) $ lambda' v t
39425
5d97fd83ab37 fix parsing of higher-order formulas;
blanchet
parents: 39374
diff changeset
    96
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
    97
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: 43131
diff changeset
    98
  let val ww = "'" ^ w in
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
    99
    TFree (ww, the_default @{sort type} (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: 43131
diff changeset
   100
  end
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   101
54820
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   102
exception ATP_CLASS of string list
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   103
exception ATP_TYPE of string atp_type list
54811
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   104
exception ATP_TERM of (string, string atp_type) atp_term list
53586
bd5fa6425993 prefixed types and some functions with "atp_" for disambiguation
blanchet
parents: 52758
diff changeset
   105
exception ATP_FORMULA of
54811
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   106
  (string, string, (string, string atp_type) atp_term, string) atp_formula list
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   107
exception SAME of unit
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   108
54820
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   109
fun class_of_atp_class cls =
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   110
  (case unprefix_and_unascii class_prefix cls of
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   111
    SOME s => s
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   112
  | NONE => raise ATP_CLASS [cls])
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   113
54811
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   114
(* Type variables are given the basic sort "HOL.type". Some will later be constrained by information
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   115
   from type literals, or by type inference. *)
54820
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   116
fun typ_of_atp_type ctxt (ty as AType ((a, clss), tys)) =
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   117
  let val Ts = map (typ_of_atp_type ctxt) tys in
54789
blanchet
parents: 54788
diff changeset
   118
    (case unprefix_and_unascii type_const_prefix a of
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   119
      SOME b => Type (invert_const b, Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   120
    | NONE =>
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   121
      if not (null tys) then
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   122
        raise ATP_TYPE [ty] (* only "tconst"s have type arguments *)
54789
blanchet
parents: 54788
diff changeset
   123
      else
blanchet
parents: 54788
diff changeset
   124
        (case unprefix_and_unascii tfree_prefix a of
blanchet
parents: 54788
diff changeset
   125
          SOME b => make_tfree ctxt b
blanchet
parents: 54788
diff changeset
   126
        | NONE =>
blanchet
parents: 54788
diff changeset
   127
          (* The term could be an Isabelle variable or a variable from the ATP, say "X1" or "_5018".
blanchet
parents: 54788
diff changeset
   128
             Sometimes variables from the ATP are indistinguishable from Isabelle variables, which
blanchet
parents: 54788
diff changeset
   129
             forces us to use a type parameter in all cases. *)
54821
blanchet
parents: 54820
diff changeset
   130
          Type_Infer.param 0 ("'" ^ perhaps (unprefix_and_unascii tvar_prefix) a,
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
   131
            (if null clss then @{sort type} else map class_of_atp_class clss))))
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   132
  end
57256
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   133
  | typ_of_atp_type ctxt (ty as AFun (a, tys)) =
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   134
    (typ_of_atp_type ctxt a) --> (typ_of_atp_type ctxt tys)
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   135
54820
d9ab86c044fd extended ATP types with sorts
blanchet
parents: 54819
diff changeset
   136
fun atp_type_of_atp_term (ATerm ((s, _), us)) = AType ((s, []), map atp_type_of_atp_term us)
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   137
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   138
fun typ_of_atp_term ctxt = typ_of_atp_type ctxt o atp_type_of_atp_term
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   139
54789
blanchet
parents: 54788
diff changeset
   140
(* Type class literal applied to a type. Returns triple of polarity, class, type. *)
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51878
diff changeset
   141
fun type_constraint_of_term ctxt (u as ATerm ((a, _), us)) =
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   142
  (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: 42595
diff changeset
   143
    (SOME b, [T]) => (b, T)
54789
blanchet
parents: 54788
diff changeset
   144
  | _ => raise ATP_TERM [u])
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   145
43907
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   146
(* 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: 57199
diff changeset
   147
fun add_var (key, z) = Vartab.map_default (key, []) (cons z)
42606
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   148
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: 42595
diff changeset
   149
  | add_type_constraint false (cl, TVar (ix, _)) = add_var (ix, cl)
0c76cf483899 show sorts not just types in Isar proofs + tuning
blanchet
parents: 42595
diff changeset
   150
  | add_type_constraint _ _ = I
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   151
55185
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   152
fun repair_var_name_raw s =
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   153
  let
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   154
    fun subscript_name s n = s ^ nat_subscript n
50704
cd1fcda1ea88 rename variable in binder, not just in body
blanchet
parents: 50703
diff changeset
   155
    val s = s |> String.map Char.toLower
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   156
  in
54789
blanchet
parents: 54788
diff changeset
   157
    (case space_explode "_" s of
blanchet
parents: 54788
diff changeset
   158
      [_] =>
blanchet
parents: 54788
diff changeset
   159
      (case take_suffix Char.isDigit (String.explode s) of
blanchet
parents: 54788
diff changeset
   160
        (cs1 as _ :: _, cs2 as _ :: _) =>
blanchet
parents: 54788
diff changeset
   161
        subscript_name (String.implode cs1) (the (Int.fromString (String.implode cs2)))
blanchet
parents: 54788
diff changeset
   162
      | (_, _) => s)
blanchet
parents: 54788
diff changeset
   163
    | [s1, s2] => (case Int.fromString s2 of SOME n => subscript_name s1 n | NONE => s)
blanchet
parents: 54788
diff changeset
   164
    | _ => s)
36486
c2d7e2dff59e support Vampire definitions of constants as "let" constructs in Isar proofs
blanchet
parents: 36485
diff changeset
   165
  end
43182
649bada59658 slacker version of "fastype_of", in case a function has dummy type
blanchet
parents: 43176
diff changeset
   166
55185
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   167
fun repair_var_name textual s =
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   168
  (case unprefix_and_unascii schematic_var_prefix s of
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   169
    SOME s => s
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   170
  | NONE => s |> textual ? repair_var_name_raw)
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   171
54789
blanchet
parents: 54788
diff changeset
   172
(* The number of type arguments of a constant, zero if it's monomorphic. For (instances of) Skolem
blanchet
parents: 54788
diff changeset
   173
   pseudoconstants, this information is encoded in the constant name. *)
52125
ac7830871177 improved handling of free variables' types in Isar proofs
blanchet
parents: 52034
diff changeset
   174
fun robust_const_num_type_args thy s =
43907
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   175
  if String.isPrefix skolem_const_prefix s then
46711
f745bcc4a1e5 more explicit Long_Name operations (NB: analyzing qualifiers is inherently fragile);
wenzelm
parents: 46435
diff changeset
   176
    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: 45553
diff changeset
   177
  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: 45553
diff changeset
   178
    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: 43863
diff changeset
   179
  else
073ab5379842 pass type arguments to lambda-lifted Frees, to account for polymorphism
blanchet
parents: 43863
diff changeset
   180
    (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: 43863
diff changeset
   181
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
   182
fun slack_fastype_of t = fastype_of t handle TERM _ => Type_Infer.anyT @{sort type}
43182
649bada59658 slacker version of "fastype_of", in case a function has dummy type
blanchet
parents: 43176
diff changeset
   183
51192
4dc6bb65c3c3 slacker comparison for Skolems, to avoid trivial equations
blanchet
parents: 50704
diff changeset
   184
(* Cope with "tt(X) = X" atoms, where "X" is existentially quantified. *)
4dc6bb65c3c3 slacker comparison for Skolems, to avoid trivial equations
blanchet
parents: 50704
diff changeset
   185
fun loose_aconv (Free (s, _), Free (s', _)) = s = s'
4dc6bb65c3c3 slacker comparison for Skolems, to avoid trivial equations
blanchet
parents: 50704
diff changeset
   186
  | loose_aconv (t, t') = t aconv t'
4dc6bb65c3c3 slacker comparison for Skolems, to avoid trivial equations
blanchet
parents: 50704
diff changeset
   187
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   188
val spass_skolem_prefix = "sk" (* "skc" or "skf" *)
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   189
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: 50693
diff changeset
   190
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   191
57257
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   192
fun var_index_of_textual textual = if textual then 0 else 1
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   193
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   194
fun quantify_over_var textual quant_of var_s var_T t =
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   195
  let
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   196
    val vars = ((var_s, var_index_of_textual textual), var_T) ::
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   197
                 filter (fn ((s, _), _) => s = var_s) (Term.add_vars t [])
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   198
    val normTs = vars |> AList.group (op =) |> map (apsnd hd)
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   199
    fun norm_var_types (Var (x, T)) =
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   200
        Var (x, the_default T (AList.lookup (op =) normTs x))
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   201
      | norm_var_types t = t
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   202
  in t |> map_aterms norm_var_types |> fold_rev quant_of (map Var normTs) end
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   203
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   204
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   205
(* Higher-order translation. Variables are typed (although we don't use that information). Lambdas
57256
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   206
   are typed.
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   207
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   208
  The code is similar to term_of_atp_fo. *)
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   209
fun term_of_atp_ho ctxt textual sym_tab =
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   210
  let
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   211
    val thy = Proof_Context.theory_of ctxt
57257
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   212
    val var_index = var_index_of_textual textual
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   213
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   214
    fun do_term opt_T u =
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   215
      (case u of
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   216
        AAbs(((var, ty), term), []) =>
57257
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   217
        let
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   218
          val typ = typ_of_atp_type ctxt ty
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   219
          val var_name = repair_var_name textual var
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   220
          val tm = do_term NONE term
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   221
        in quantify_over_var textual lambda' var_name typ tm end
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   222
      | ATerm ((s, tys), us) =>
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   223
        if s = ""
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   224
        then error "Isar proof reconstruction failed because the ATP proof \
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   225
                     \contains unparsable material."
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   226
        else if s = tptp_equal then
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   227
          let val ts = map (do_term NONE) us in
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   228
            if textual andalso length ts = 2 andalso loose_aconv (hd ts, List.last ts) then
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   229
              @{const True}
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   230
            else
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   231
              list_comb (Const (@{const_name HOL.eq}, Type_Infer.anyT @{sort type}), ts)
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   232
          end
57256
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   233
        else if not (null us) then
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   234
          let
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   235
            val args = List.map (do_term NONE) us
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   236
            val opt_T' = SOME (map slack_fastype_of args ---> the_default dummyT opt_T)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   237
            val func = do_term opt_T' (ATerm ((s, tys), []))
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   238
          in foldl1 (op $) (func :: args) end
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   239
        else if s = tptp_or then HOLogic.disj
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   240
        else if s = tptp_and then HOLogic.conj
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   241
        else if s = tptp_implies then HOLogic.imp
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   242
        else if s = tptp_iff orelse s = tptp_equal then HOLogic.eq_const dummyT
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   243
        else if s = tptp_not_iff orelse s = tptp_not_equal then @{term "% P Q. Q ~= P"}
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   244
        else if s = tptp_if then @{term "% P Q. Q --> P"}
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   245
        else if s = tptp_not_and then @{term "% P Q. ~ (P & Q)"}
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   246
        else if s = tptp_not_or then @{term "% P Q. ~ (P | Q)"}
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   247
        else if s = tptp_not then HOLogic.Not
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   248
        else if s = tptp_ho_forall then HOLogic.all_const dummyT
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   249
        else if s = tptp_ho_exists then HOLogic.exists_const dummyT
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   250
        else
57256
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   251
          (case unprefix_and_unascii const_prefix s of
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   252
            SOME s' =>
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   253
            let
57256
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   254
              val ((s', s''), mangled_us) = s' |> unmangled_const |>> `invert_const
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   255
              val num_ty_args = length us - the_default 0 (Symtab.lookup sym_tab s)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   256
              val (type_us, term_us) = chop num_ty_args us |>> append mangled_us
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   257
              val term_ts = map (do_term NONE) term_us
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   258
              val Ts = map (typ_of_atp_type ctxt) tys @ map (typ_of_atp_term ctxt) type_us
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   259
              val T =
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   260
                (if not (null Ts) andalso robust_const_num_type_args thy s' = length Ts then
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   261
                   if textual then try (Sign.const_instance thy) (s', Ts) else NONE
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   262
                 else
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   263
                   NONE)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   264
                |> (fn SOME T => T
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   265
                     | NONE =>
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   266
                       map slack_fastype_of term_ts --->
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   267
                       the_default (Type_Infer.anyT @{sort type}) opt_T)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   268
              val t = Const (unproxify_const s', T)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   269
            in list_comb (t, term_ts) end
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   270
          | NONE => (* a free or schematic variable *)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   271
            let
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   272
              fun fresh_up s =
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   273
                [(s, ())] |> Variable.variant_frees ctxt [] |> hd |> fst
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   274
              val ts = map (do_term NONE) us
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   275
              val T =
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   276
                (case opt_T of
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   277
                  SOME T => map slack_fastype_of ts ---> T
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   278
                | NONE =>
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   279
                  map slack_fastype_of ts --->
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   280
                    (case tys of
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   281
                      [ty] => typ_of_atp_type ctxt ty
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   282
                    | _ => Type_Infer.anyT @{sort type}))
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   283
              val t =
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   284
                (case unprefix_and_unascii fixed_var_prefix s of
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   285
                  SOME s => Free (s, T)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   286
                | NONE =>
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   287
                  if textual andalso not (is_tptp_variable s) then
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   288
                    Free (s |> textual ? (repair_var_name_raw #> fresh_up), T)
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   289
                  else
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   290
                    Var ((repair_var_name textual s, var_index), T))
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   291
            in list_comb (t, ts) end))
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   292
  in do_term end
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   293
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
   294
(* First-order translation. No types are known for variables. "Type_Infer.anyT @{sort type}"
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
   295
   should allow them to be inferred. *)
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   296
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: 36607
diff changeset
   297
  let
43135
8c32a0160b0d more uniform handling of tfree sort inference in ATP reconstruction code, based on what Metis always has done
blanchet
parents: 43131
diff changeset
   298
    val thy = Proof_Context.theory_of ctxt
54789
blanchet
parents: 54788
diff changeset
   299
    (* For Metis, we use 1 rather than 0 because variable references in clauses may otherwise
blanchet
parents: 54788
diff changeset
   300
       conflict with variable constraints in the goal. At least, type inference often fails
blanchet
parents: 54788
diff changeset
   301
       otherwise. See also "axiom_inference" in "Metis_Reconstruct". *)
57257
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   302
    val var_index = var_index_of_textual textual
57199
blanchet
parents: 56854
diff changeset
   303
43131
9e9420122f91 fixed interaction between type tags and hAPP in reconstruction code
blanchet
parents: 43130
diff changeset
   304
    fun do_term extra_ts opt_T u =
54789
blanchet
parents: 54788
diff changeset
   305
      (case u of
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   306
        ATerm ((s, tys), us) =>
57199
blanchet
parents: 56854
diff changeset
   307
        if s = "" then
blanchet
parents: 56854
diff changeset
   308
          error "Isar proof reconstruction failed because the ATP proof contains unparsable \
blanchet
parents: 56854
diff changeset
   309
            \material."
51878
f11039b31bae handle dummy atp terms
smolkas
parents: 51192
diff changeset
   310
        else if String.isPrefix native_type_prefix s then
42962
3b50fdeb6cfc started adding support for THF output (but no lambdas)
blanchet
parents: 42943
diff changeset
   311
          @{const True} (* ignore TPTP type information *)
44773
e701dabbfe37 perform mangling before computing symbol arity, to avoid needless "hAPP"s and "hBOOL"s
blanchet
parents: 44399
diff changeset
   312
        else if s = tptp_equal then
43093
blanchet
parents: 43085
diff changeset
   313
          let val ts = map (do_term [] NONE) us in
54822
d4a56c826769 pick up tfree/tvar type from SPASS-Pirate proof
blanchet
parents: 54821
diff changeset
   314
            if textual andalso length ts = 2 andalso loose_aconv (hd ts, List.last ts) then
39106
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   315
              @{const True}
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   316
            else
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
   317
              list_comb (Const (@{const_name HOL.eq}, Type_Infer.anyT @{sort type}), ts)
39106
5ab6a3707499 fix trivial "x = x" fact detection
blanchet
parents: 38988
diff changeset
   318
          end
54789
blanchet
parents: 54788
diff changeset
   319
        else
blanchet
parents: 54788
diff changeset
   320
          (case unprefix_and_unascii const_prefix s of
blanchet
parents: 54788
diff changeset
   321
            SOME s' =>
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   322
            let val ((s', s''), mangled_us) = s' |> unmangled_const |>> `invert_const in
54789
blanchet
parents: 54788
diff changeset
   323
              if s' = type_tag_name then
blanchet
parents: 54788
diff changeset
   324
                (case mangled_us @ us of
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   325
                  [typ_u, term_u] => do_term extra_ts (SOME (typ_of_atp_term ctxt typ_u)) term_u
54789
blanchet
parents: 54788
diff changeset
   326
                | _ => raise ATP_TERM us)
blanchet
parents: 54788
diff changeset
   327
              else if s' = predicator_name then
blanchet
parents: 54788
diff changeset
   328
                do_term [] (SOME @{typ bool}) (hd us)
blanchet
parents: 54788
diff changeset
   329
              else if s' = app_op_name then
blanchet
parents: 54788
diff changeset
   330
                let val extra_t = do_term [] NONE (List.last us) in
blanchet
parents: 54788
diff changeset
   331
                  do_term (extra_t :: extra_ts)
57199
blanchet
parents: 56854
diff changeset
   332
                    (case opt_T of SOME T => SOME (slack_fastype_of extra_t --> T) | NONE => NONE)
blanchet
parents: 56854
diff changeset
   333
                    (nth us (length us - 2))
54789
blanchet
parents: 54788
diff changeset
   334
                end
blanchet
parents: 54788
diff changeset
   335
              else if s' = type_guard_name then
blanchet
parents: 54788
diff changeset
   336
                @{const True} (* ignore type predicates *)
blanchet
parents: 54788
diff changeset
   337
              else
blanchet
parents: 54788
diff changeset
   338
                let
blanchet
parents: 54788
diff changeset
   339
                  val new_skolem = String.isPrefix new_skolem_const_prefix s''
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   340
                  val num_ty_args = length us - the_default 0 (Symtab.lookup sym_tab s)
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   341
                  val (type_us, term_us) = chop num_ty_args us |>> append mangled_us
54789
blanchet
parents: 54788
diff changeset
   342
                  val term_ts = map (do_term [] NONE) term_us
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   343
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   344
                  val Ts = map (typ_of_atp_type ctxt) tys @ map (typ_of_atp_term ctxt) type_us
54789
blanchet
parents: 54788
diff changeset
   345
                  val T =
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   346
                    (if not (null Ts) andalso robust_const_num_type_args thy s' = length Ts then
57199
blanchet
parents: 56854
diff changeset
   347
                       if new_skolem then SOME (Type_Infer.paramify_vars (tl Ts ---> hd Ts))
blanchet
parents: 56854
diff changeset
   348
                       else if textual then try (Sign.const_instance thy) (s', Ts)
blanchet
parents: 56854
diff changeset
   349
                       else NONE
54789
blanchet
parents: 54788
diff changeset
   350
                     else
blanchet
parents: 54788
diff changeset
   351
                       NONE)
blanchet
parents: 54788
diff changeset
   352
                    |> (fn SOME T => T
blanchet
parents: 54788
diff changeset
   353
                         | NONE =>
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
   354
                           map slack_fastype_of term_ts --->
57199
blanchet
parents: 56854
diff changeset
   355
                           the_default (Type_Infer.anyT @{sort type}) opt_T)
54789
blanchet
parents: 54788
diff changeset
   356
                  val t =
blanchet
parents: 54788
diff changeset
   357
                    if new_skolem then Var ((new_skolem_var_name_of_const s'', var_index), T)
blanchet
parents: 54788
diff changeset
   358
                    else Const (unproxify_const s', T)
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   359
                in
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   360
                  list_comb (t, term_ts @ extra_ts)
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   361
                end
54789
blanchet
parents: 54788
diff changeset
   362
            end
blanchet
parents: 54788
diff changeset
   363
          | NONE => (* a free or schematic variable *)
blanchet
parents: 54788
diff changeset
   364
            let
blanchet
parents: 54788
diff changeset
   365
              (* This assumes that distinct names are mapped to distinct names by
54822
d4a56c826769 pick up tfree/tvar type from SPASS-Pirate proof
blanchet
parents: 54821
diff changeset
   366
                 "Variable.variant_frees". This does not hold in general but should hold for
d4a56c826769 pick up tfree/tvar type from SPASS-Pirate proof
blanchet
parents: 54821
diff changeset
   367
                 ATP-generated Skolem function names, since these end with a digit and
d4a56c826769 pick up tfree/tvar type from SPASS-Pirate proof
blanchet
parents: 54821
diff changeset
   368
                 "variant_frees" appends letters. *)
57199
blanchet
parents: 56854
diff changeset
   369
              fun fresh_up s = [(s, ())] |> Variable.variant_frees ctxt [] |> hd |> fst
blanchet
parents: 56854
diff changeset
   370
54789
blanchet
parents: 54788
diff changeset
   371
              val term_ts =
blanchet
parents: 54788
diff changeset
   372
                map (do_term [] NONE) us
blanchet
parents: 54788
diff changeset
   373
                (* SPASS (3.8ds) and Vampire (2.6) pass arguments to Skolem functions in reverse
blanchet
parents: 54788
diff changeset
   374
                   order, which is incompatible with the new Metis skolemizer. *)
blanchet
parents: 54788
diff changeset
   375
                |> exists (fn pre => String.isPrefix pre s)
blanchet
parents: 54788
diff changeset
   376
                  [spass_skolem_prefix, vampire_skolem_prefix] ? rev
blanchet
parents: 54788
diff changeset
   377
              val ts = term_ts @ extra_ts
blanchet
parents: 54788
diff changeset
   378
              val T =
blanchet
parents: 54788
diff changeset
   379
                (case opt_T of
blanchet
parents: 54788
diff changeset
   380
                  SOME T => map slack_fastype_of term_ts ---> T
54822
d4a56c826769 pick up tfree/tvar type from SPASS-Pirate proof
blanchet
parents: 54821
diff changeset
   381
                | NONE =>
d4a56c826769 pick up tfree/tvar type from SPASS-Pirate proof
blanchet
parents: 54821
diff changeset
   382
                  map slack_fastype_of ts --->
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56104
diff changeset
   383
                  (case tys of [ty] => typ_of_atp_type ctxt ty | _ => Type_Infer.anyT @{sort type}))
54789
blanchet
parents: 54788
diff changeset
   384
              val t =
blanchet
parents: 54788
diff changeset
   385
                (case unprefix_and_unascii fixed_var_prefix s of
blanchet
parents: 54788
diff changeset
   386
                  SOME s => Free (s, T)
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   387
                | NONE =>
55185
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   388
                  if textual andalso not (is_tptp_variable s) then
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   389
                    Free (s |> textual ? (repair_var_name_raw #> fresh_up), T)
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   390
                  else
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   391
                    Var ((repair_var_name textual s, var_index), T))
54789
blanchet
parents: 54788
diff changeset
   392
            in list_comb (t, ts) end))
43093
blanchet
parents: 43085
diff changeset
   393
  in do_term [] end
21978
72c21698a055 Contains old Tools/ATP/AtpCommunication.ML, plus proof reconstruction
paulson
parents:
diff changeset
   394
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   395
fun term_of_atp ctxt (ATP_Problem.THF _) type_enc =
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   396
    if ATP_Problem_Generate.is_type_enc_higher_order type_enc
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   397
    then term_of_atp_ho ctxt
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   398
    else error "Unsupported Isar reconstruction."
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   399
  | term_of_atp ctxt _ type_enc =
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   400
    if not (ATP_Problem_Generate.is_type_enc_higher_order type_enc)
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   401
    then term_of_atp_fo ctxt
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   402
    else error "Unsupported Isar reconstruction."
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   403
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   404
fun term_of_atom ctxt format type_enc textual sym_tab pos (u as ATerm ((s, _), _)) =
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   405
  if String.isPrefix class_prefix s then
52031
9a9238342963 tuning -- renamed '_from_' to '_of_' in Sledgehammer
blanchet
parents: 51878
diff changeset
   406
    add_type_constraint pos (type_constraint_of_term ctxt u)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   407
    #> pair @{const True}
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   408
  else
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   409
    pair (term_of_atp ctxt format type_enc textual sym_tab (SOME @{typ bool}) u)
36402
1b20805974c7 introduced direct proof reconstruction code, eliminating the need for the "neg_clausify" method;
blanchet
parents: 36396
diff changeset
   410
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   411
(* Update schematic type variables with detected sort constraints. It's not
42563
e70ffe3846d0 improve helper type instantiation code
blanchet
parents: 42562
diff changeset
   412
   totally clear whether this code is necessary. *)
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   413
fun repair_tvar_sorts (t, tvar_tab) =
36909
7d5587f6d5f7 made Sledgehammer's full-typed proof reconstruction work for the first time;
blanchet
parents: 36607
diff changeset
   414
  let
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   415
    fun do_type (Type (a, Ts)) = Type (a, map do_type Ts)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   416
      | do_type (TVar (xi, s)) =
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   417
        TVar (xi, the_default s (Vartab.lookup tvar_tab xi))
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   418
      | do_type (TFree z) = TFree z
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   419
    fun do_term (Const (a, T)) = Const (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   420
      | do_term (Free (a, T)) = Free (a, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   421
      | do_term (Var (xi, T)) = Var (xi, do_type T)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   422
      | do_term (t as Bound _) = t
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   423
      | do_term (Abs (a, T, t)) = Abs (a, do_type T, do_term t)
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   424
      | do_term (t1 $ t2) = do_term t1 $ do_term t2
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   425
  in t |> not (Vartab.is_empty tvar_tab) ? do_term end
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   426
54811
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   427
(* Interpret an ATP formula as a HOL term, extracting sort constraints as they appear in the
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   428
   formula. *)
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   429
fun prop_of_atp ctxt format type_enc textual sym_tab phi =
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   430
  let
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   431
    fun do_formula pos phi =
54789
blanchet
parents: 54788
diff changeset
   432
      (case phi of
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   433
        AQuant (_, [], phi) => do_formula pos phi
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42451
diff changeset
   434
      | AQuant (q, (s, _) :: xs, phi') =>
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   435
        do_formula pos (AQuant (q, xs, phi'))
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42451
diff changeset
   436
        (* FIXME: TFF *)
57257
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   437
        #>> quantify_over_var textual (case q of AForall => forall_of | AExists => exists_of)
0d5e34ba4d28 Correcting the type parser
fleury
parents: 57256
diff changeset
   438
          (repair_var_name textual s) dummyT
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   439
      | AConn (ANot, [phi']) => do_formula (not pos) phi' #>> s_not
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   440
      | AConn (c, [phi1, phi2]) =>
38014
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   441
        do_formula (pos |> c = AImplies ? not) phi1
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   442
        ##>> do_formula pos phi2
81c23d286f0c extract sort constraints from FOFs properly;
blanchet
parents: 38007
diff changeset
   443
        #>> (case c of
54811
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   444
              AAnd => s_conj
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   445
            | AOr => s_disj
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   446
            | AImplies => s_imp
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   447
            | AIff => s_iff
df56a01f5684 parse SPASS-Pirate types
blanchet
parents: 54799
diff changeset
   448
            | ANot => raise Fail "impossible connective")
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   449
      | AAtom tm => term_of_atom ctxt format type_enc textual sym_tab pos tm
54789
blanchet
parents: 54788
diff changeset
   450
      | _ => raise ATP_FORMULA [phi])
blanchet
parents: 54788
diff changeset
   451
  in
blanchet
parents: 54788
diff changeset
   452
    repair_tvar_sorts (do_formula true phi Vartab.empty)
blanchet
parents: 54788
diff changeset
   453
  end
37991
3093ab32f1e7 proof reconstruction for full FOF terms
blanchet
parents: 37962
diff changeset
   454
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   455
fun find_first_in_list_vector vec key =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   456
  Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   457
                 | (_, value) => value) NONE vec
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   458
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   459
val unprefix_fact_number = space_implode "_" o tl o space_explode "_"
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   460
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   461
fun resolve_one_named_fact fact_names s =
54789
blanchet
parents: 54788
diff changeset
   462
  (case try (unprefix fact_prefix) s of
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   463
    SOME s' =>
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   464
    let val s' = s' |> unprefix_fact_number |> unascii_of in
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   465
      s' |> find_first_in_list_vector fact_names |> Option.map (pair s')
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   466
    end
54789
blanchet
parents: 54788
diff changeset
   467
  | NONE => NONE)
54506
blanchet
parents: 54505
diff changeset
   468
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   469
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   470
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   471
fun resolve_one_named_conjecture s =
54789
blanchet
parents: 54788
diff changeset
   472
  (case try (unprefix conjecture_prefix) s of
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   473
    SOME s' => Int.fromString s'
54789
blanchet
parents: 54788
diff changeset
   474
  | NONE => NONE)
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   475
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   476
val resolve_conjecture = map_filter resolve_one_named_conjecture
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   477
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   478
fun is_axiom_used_in_proof pred =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   479
  exists (fn ((_, ss), _, _, _, []) => exists pred ss | _ => false)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   480
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   481
fun add_non_rec_defs fact_names accum =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   482
  Vector.foldl (fn (facts, facts') =>
54818
a80bd631e573 honor SPASS-Pirate type arguments
blanchet
parents: 54811
diff changeset
   483
      union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts) facts')
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   484
    accum fact_names
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   485
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   486
val leo2_extcnf_equal_neg_rule = "extcnf_equal_neg"
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   487
val leo2_unfold_def_rule = "unfold_def"
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   488
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   489
fun add_fact ctxt fact_names ((_, ss), _, _, rule, deps) =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   490
  (if rule = leo2_extcnf_equal_neg_rule then
56104
fd6e132ee4fb correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents: 55285
diff changeset
   491
     insert (op =) (short_thm_name ctxt ext, (Global, General))
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   492
   else if rule = leo2_unfold_def_rule then
54789
blanchet
parents: 54788
diff changeset
   493
     (* LEO 1.3.3 does not record definitions properly, leading to missing dependencies in the TSTP
blanchet
parents: 54788
diff changeset
   494
        proof. Remove the next line once this is fixed. *)
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   495
     add_non_rec_defs fact_names
54788
a898e15b522a primitive support for SPASS-Pirate (Daniel Wand's polymorphic SPASS prototype)
blanchet
parents: 54774
diff changeset
   496
   else if rule = agsyhol_core_rule orelse rule = satallax_core_rule then
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   497
     (fn [] =>
54789
blanchet
parents: 54788
diff changeset
   498
         (* agsyHOL and Satallax don't include definitions in their unsatisfiable cores, so we
blanchet
parents: 54788
diff changeset
   499
            assume the worst and include them all here. *)
56104
fd6e132ee4fb correctly reconstruct helper facts (e.g. 'nat_int') in Isar proofs
blanchet
parents: 55285
diff changeset
   500
         [(short_thm_name ctxt ext, (Global, General))] |> add_non_rec_defs fact_names
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   501
       | facts => facts)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   502
   else
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   503
     I)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   504
  #> (if null deps then union (op =) (resolve_fact fact_names ss) else I)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   505
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   506
fun used_facts_in_atp_proof ctxt fact_names atp_proof =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   507
  if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   508
  else fold (add_fact ctxt fact_names) atp_proof []
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   509
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   510
fun used_facts_in_unsound_atp_proof _ _ [] = NONE
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   511
  | used_facts_in_unsound_atp_proof ctxt fact_names atp_proof =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   512
    let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   513
      if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso
54506
blanchet
parents: 54505
diff changeset
   514
         not (is_axiom_used_in_proof (not o null o resolve_conjecture o single) atp_proof) then
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   515
        SOME (map fst used_facts)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   516
      else
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   517
        NONE
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   518
    end
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   519
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   520
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   521
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   522
(* overapproximation (good enough) *)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   523
fun is_lam_lifted s =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   524
  String.isPrefix fact_prefix s andalso
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   525
  String.isSubstring ascii_of_lam_fact_prefix s
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   526
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   527
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix)
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   528
55257
abfd7b90bba2 rationalized threading of 'metis' arguments
blanchet
parents: 55195
diff changeset
   529
fun atp_proof_prefers_lifting atp_proof =
abfd7b90bba2 rationalized threading of 'metis' arguments
blanchet
parents: 55195
diff changeset
   530
  (is_axiom_used_in_proof is_combinator_def atp_proof,
abfd7b90bba2 rationalized threading of 'metis' arguments
blanchet
parents: 55195
diff changeset
   531
   is_axiom_used_in_proof is_lam_lifted atp_proof) = (false, true)
54500
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   532
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   533
val is_typed_helper_name =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   534
  String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   535
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   536
fun is_typed_helper_used_in_atp_proof atp_proof =
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   537
  is_axiom_used_in_proof is_typed_helper_name atp_proof
f625e0e79dd1 refactoring
blanchet
parents: 54499
diff changeset
   538
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   539
fun replace_one_dependency (old, new) dep = if is_same_atp_step dep old then new else [dep]
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   540
fun replace_dependencies_in_line old_new (name, role, t, rule, deps) =
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   541
  (name, role, t, rule, fold (union (op =) o replace_one_dependency old_new) deps [])
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   542
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   543
fun repair_name "$true" = "c_True"
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   544
  | repair_name "$false" = "c_False"
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   545
  | repair_name "$$e" = tptp_equal (* seen in Vampire proofs *)
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   546
  | repair_name s =
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   547
    if is_tptp_equal s orelse
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   548
       (* seen in Vampire proofs *)
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   549
       (String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   550
      tptp_equal
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   551
    else
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   552
      s
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   553
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   554
fun infer_formula_types ctxt =
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   555
  Type.constraint HOLogic.boolT
54756
blanchet
parents: 54507
diff changeset
   556
  #> Syntax.check_term (Proof_Context.set_mode Proof_Context.mode_schematic ctxt)
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   557
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   558
val combinator_table =
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   559
  [(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}),
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   560
   (@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}),
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   561
   (@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}),
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   562
   (@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}),
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   563
   (@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})]
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   564
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   565
fun uncombine_term thy =
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   566
  let
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   567
    fun aux (t1 $ t2) = betapply (pairself aux (t1, t2))
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   568
      | aux (Abs (s, T, t')) = Abs (s, T, aux t')
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   569
      | aux (t as Const (x as (s, _))) =
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   570
        (case AList.lookup (op =) combinator_table s of
54756
blanchet
parents: 54507
diff changeset
   571
          SOME thm => thm |> prop_of |> specialize_type thy x |> Logic.dest_equals |> snd
blanchet
parents: 54507
diff changeset
   572
        | NONE => t)
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   573
      | aux t = t
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   574
  in aux end
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   575
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   576
fun unlift_term lifted =
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   577
  map_aterms (fn t as Const (s, _) =>
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   578
                 if String.isPrefix lam_lifted_prefix s then
54756
blanchet
parents: 54507
diff changeset
   579
                   (* FIXME: do something about the types *)
blanchet
parents: 54507
diff changeset
   580
                   (case AList.lookup (op =) lifted s of
blanchet
parents: 54507
diff changeset
   581
                     SOME t => unlift_term lifted t
blanchet
parents: 54507
diff changeset
   582
                   | NONE => t)
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   583
                 else
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   584
                   t
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   585
               | t => t)
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   586
57256
cf43583f9bb9 imported patch leo2_skolem_simplication
fleury
parents: 57255
diff changeset
   587
fun termify_line _ _ _ _ _ (_, Type_Role, _, _, _) = NONE
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   588
  | 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: 57199
diff changeset
   589
    let
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   590
      val thy = Proof_Context.theory_of ctxt
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   591
      val t = u
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   592
        |> prop_of_atp ctxt format type_enc true sym_tab
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   593
        |> uncombine_term thy
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   594
        |> unlift_term lifted
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   595
        |> infer_formula_types ctxt
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   596
        |> HOLogic.mk_Trueprop
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   597
    in
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   598
      SOME (name, role, t, rule, deps)
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   599
    end
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   600
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   601
val waldmeister_conjecture_num = "1.0.0.0"
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   602
54756
blanchet
parents: 54507
diff changeset
   603
fun repair_waldmeister_endgame proof =
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   604
  let
56854
ddd3af5a683d fixed Waldmeister endgame w.r.t. "Trueprop"
blanchet
parents: 56254
diff changeset
   605
    fun repair_tail (name, _, @{const Trueprop} $ t, rule, deps) =
ddd3af5a683d fixed Waldmeister endgame w.r.t. "Trueprop"
blanchet
parents: 56254
diff changeset
   606
      (name, Negated_Conjecture, @{const Trueprop} $ s_not t, rule, deps)
54756
blanchet
parents: 54507
diff changeset
   607
    fun repair_body [] = []
blanchet
parents: 54507
diff changeset
   608
      | repair_body ((line as ((num, _), _, _, _, _)) :: lines) =
blanchet
parents: 54507
diff changeset
   609
        if num = waldmeister_conjecture_num then map repair_tail (line :: lines)
blanchet
parents: 54507
diff changeset
   610
        else line :: repair_body lines
blanchet
parents: 54507
diff changeset
   611
  in
blanchet
parents: 54507
diff changeset
   612
    repair_body proof
blanchet
parents: 54507
diff changeset
   613
  end
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   614
57258
67d85a8aa6cc Moving the remote prefix deleting on Sledgehammer's side
fleury
parents: 57257
diff changeset
   615
fun termify_atp_proof ctxt local_prover format type_enc pool lifted sym_tab =
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   616
  clean_up_atp_proof_dependencies
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   617
  #> nasty_atp_proof pool
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   618
  #> map_term_names_in_atp_proof repair_name
57255
488046fdda59 add support for Isar reconstruction for thf1 ATP provers like Leo-II.
fleury
parents: 57199
diff changeset
   619
  #> map_filter (termify_line ctxt format type_enc lifted sym_tab)
57258
67d85a8aa6cc Moving the remote prefix deleting on Sledgehammer's side
fleury
parents: 57257
diff changeset
   620
  #> local_prover = waldmeisterN ? repair_waldmeister_endgame
54499
319f8659267d refactoring
blanchet
parents: 53586
diff changeset
   621
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   622
fun take_distinct_vars seen ((t as Var _) :: ts) =
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   623
    if member (op aconv) seen t then rev seen else take_distinct_vars (t :: seen) ts
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   624
  | take_distinct_vars seen _ = rev seen
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   625
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   626
fun unskolemize_term skos t =
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   627
  let
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   628
    val is_sko = member (op =) skos
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   629
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   630
    fun find_args args (t $ u) = find_args (u :: args) t #> find_args [] u
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   631
      | find_args _ (Abs (_, _, t)) = find_args [] t
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   632
      | find_args args (Free (s, _)) =
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   633
        if is_sko s then
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   634
          let val new = take_distinct_vars [] args in
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   635
            (fn [] => new | old => if length new < length old then new else old)
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   636
          end
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   637
        else
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   638
          I
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   639
      | find_args _ _ = I
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   640
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   641
    val alls = find_args [] t []
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   642
    val num_alls = length alls
55195
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   643
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   644
    fun fix_skos args (t $ u) = fix_skos (fix_skos [] u :: args) t
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   645
      | fix_skos args (t as Free (s, T)) =
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   646
        if is_sko s then list_comb (Free (s, funpow num_alls body_type T), drop num_alls args)
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   647
        else list_comb (t, args)
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   648
      | fix_skos [] (Abs (s, T, t)) = Abs (s, T, fix_skos [] t)
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   649
      | fix_skos [] t = t
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   650
      | fix_skos args t = list_comb (fix_skos [] t, args)
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   651
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   652
    val t' = fix_skos [] t
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   653
55195
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   654
    fun add_sko (t as Free (s, _)) = is_sko s ? insert (op aconv) t
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   655
      | add_sko _ = I
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   656
55195
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   657
    val exs = Term.fold_aterms add_sko t' []
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   658
  in
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   659
    t'
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   660
    |> HOLogic.dest_Trueprop
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   661
    |> fold exists_of exs |> Term.map_abs_vars (K Name.uu)
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   662
    |> fold_rev forall_of alls
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   663
    |> HOLogic.mk_Trueprop
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   664
  end
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   665
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   666
fun introduce_spass_skolem [] = []
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   667
  | introduce_spass_skolem (proof as (_, _, _, rule1, _) :: _) =
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   668
    if rule1 = spass_input_rule then
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   669
      let
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   670
        fun add_sko (Free (s, _)) = String.isPrefix spass_skolem_prefix s ? insert (op =) s
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   671
          | add_sko _ = I
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   672
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   673
        (* union-find would be faster *)
54799
blanchet
parents: 54789
diff changeset
   674
        fun add_cycle [] = I
blanchet
parents: 54789
diff changeset
   675
          | add_cycle ss =
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   676
            fold (fn s => Graph.default_node (s, ())) ss
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   677
            #> fold Graph.add_edge (ss ~~ tl ss @ [hd ss])
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   678
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   679
        val (input_steps, other_steps) = List.partition (null o #5) proof
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   680
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   681
        val skoss = map (fn (_, _, t, _, _) => Term.fold_aterms add_sko t []) input_steps
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   682
        val skoss_input_steps = filter_out (null o fst) (skoss ~~ input_steps)
54799
blanchet
parents: 54789
diff changeset
   683
        val groups = Graph.strong_conn (fold add_cycle skoss Graph.empty)
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   684
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   685
        fun step_name_of_group skos = (implode skos, [])
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   686
        fun in_group group = member (op =) group o hd
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   687
        fun group_of sko = the (find_first (fn group => in_group group sko) groups)
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   688
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   689
        fun new_steps (skoss_steps : (string list * (term, 'a) atp_step) list) group =
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   690
          let
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   691
            val name = step_name_of_group group
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   692
            val name0 = name |>> prefix "0"
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   693
            val t =
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   694
              skoss_steps
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   695
              |> map (snd #> #3 #> HOLogic.dest_Trueprop)
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   696
              |> Library.foldr1 s_conj
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   697
              |> HOLogic.mk_Trueprop
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   698
            val skos = fold (union (op =) o fst) skoss_steps []
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   699
            val deps = map (snd #> #1) skoss_steps
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   700
          in
55195
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   701
            [(name0, Unknown, unskolemize_term skos t, spass_pre_skolemize_rule, deps),
067142c53c3b reverted unsound optimization
blanchet
parents: 55192
diff changeset
   702
             (name, Unknown, t, spass_skolemize_rule, [name0])]
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   703
          end
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   704
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   705
        val sko_steps =
55192
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   706
          maps (fn group => new_steps (filter (in_group group o fst) skoss_input_steps) group)
b75b52c7cf94 unskolemize SPASS formula to ensure that the variables are in the right order for 'metis's skolemizer
blanchet
parents: 55185
diff changeset
   707
            groups
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   708
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   709
        val old_news =
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   710
          map (fn (skos, (name, _, _, _, _)) => (name, [step_name_of_group (group_of skos)]))
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   711
            skoss_input_steps
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   712
        val repair_deps = fold replace_dependencies_in_line old_news
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   713
      in
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   714
        input_steps @ sko_steps @ map repair_deps other_steps
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   715
      end
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   716
  else
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   717
    proof
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   718
54505
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   719
fun factify_atp_proof fact_names hyp_ts concl_t atp_proof =
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   720
  let
54799
blanchet
parents: 54789
diff changeset
   721
    fun factify_step ((num, ss), _, t, rule, deps) =
54505
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   722
      let
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   723
        val (ss', role', t') =
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   724
          (case resolve_conjecture ss of
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   725
            [j] =>
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   726
            if j = length hyp_ts then ([], Conjecture, concl_t) else ([], Hypothesis, nth hyp_ts j)
55185
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   727
          | _ =>
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   728
            (case resolve_fact fact_names ss of
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   729
              [] => (ss, Plain, t)
a0bd8d6414e6 centralized & repaired handling of bound variables in intermediate data structure (viva de Bruijn)
blanchet
parents: 54822
diff changeset
   730
            | facts => (map fst facts, Axiom, t)))
54505
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   731
      in
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   732
        ((num, ss'), role', t', rule, deps)
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   733
      end
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   734
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   735
    val atp_proof = map factify_step atp_proof
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   736
    val names = map #1 atp_proof
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   737
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   738
    fun repair_dep (num, ss) = (num, the_default ss (AList.lookup (op =) names num))
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   739
    fun repair_deps (name, role, t, rule, deps) = (name, role, t, rule, map repair_dep deps)
54772
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   740
  in
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   741
    map repair_deps atp_proof
f5fd4a34b0e8 handle Skolems gracefully for SPASS as well
blanchet
parents: 54770
diff changeset
   742
  end
54505
d023838eb252 more refactoring to accommodate SMT proofs
blanchet
parents: 54501
diff changeset
   743
31038
immler@in.tum.de
parents: 31037
diff changeset
   744
end;