src/HOL/Tools/ATP/atp_problem.ML
author blanchet
Sun, 01 May 2011 18:37:25 +0200
changeset 42583 84b134118616
parent 42577 78414ec6fa4e
child 42589 9f7c48463645
permissions -rw-r--r--
avoid trailing digits for SNARK (type) names -- grr...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38047
9033c03cc214 consequence of directory renaming
blanchet
parents: 38046
diff changeset
     1
(*  Title:      HOL/Tools/ATP/atp_problem.ML
38027
505657ddb047 standardize "Author" tags
blanchet
parents: 38025
diff changeset
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, TU Muenchen
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     4
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39384
diff changeset
     5
Abstract representation of ATP problems and TPTP syntax.
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     6
*)
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     7
38019
e207a64e1e0b complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
blanchet
parents: 38018
diff changeset
     8
signature ATP_PROBLEM =
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     9
sig
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    10
  datatype 'a fo_term = ATerm of 'a * 'a fo_term list
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    11
  datatype quantifier = AForall | AExists
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    12
  datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
42531
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    13
  datatype ('a, 'b, 'c) formula =
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    14
    AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    15
    AConn of connective * ('a, 'b, 'c) formula list |
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    16
    AAtom of 'c
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    17
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    18
  datatype format = Fof | Tff
42525
7a506b0b644f distinguish FOF and TFF (typed first-order) in ATP abstract syntax tree
blanchet
parents: 42449
diff changeset
    19
  datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
42527
6a9458524f01 reintroduced constructor for formulas, and automatically detect which logic to use (TFF or FOF) to avoid clutter
blanchet
parents: 42526
diff changeset
    20
  datatype 'a problem_line =
42543
f9d402d144d4 declare TFF types so that SNARK can be used with types
blanchet
parents: 42538
diff changeset
    21
    Decl of string * 'a * 'a list * 'a |
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    22
    Formula of string * formula_kind * ('a, 'a, 'a fo_term) formula
42529
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
    23
               * string fo_term option * string fo_term option
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38014
diff changeset
    24
  type 'a problem = (string * 'a problem_line list) list
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    25
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    26
  val mk_anot : ('a, 'b, 'c) formula -> ('a, 'b, 'c) formula
38019
e207a64e1e0b complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
blanchet
parents: 38018
diff changeset
    27
  val timestamp : unit -> string
42567
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    28
  val hashw : word * word -> word
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    29
  val hashw_string : string * word -> word
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39384
diff changeset
    30
  val is_atp_variable : string -> bool
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39384
diff changeset
    31
  val tptp_strings_for_atp_problem :
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    32
    formula_kind -> format -> string problem -> string list
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39384
diff changeset
    33
  val nice_atp_problem :
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38014
diff changeset
    34
    bool -> ('a * (string * string) problem_line list) list
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38014
diff changeset
    35
    -> ('a * string problem_line list) list
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38014
diff changeset
    36
       * (string Symtab.table * string Symtab.table) option
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    37
end;
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    38
38019
e207a64e1e0b complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
blanchet
parents: 38018
diff changeset
    39
structure ATP_Problem : ATP_PROBLEM =
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    40
struct
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    41
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    42
(** ATP problem **)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    43
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    44
datatype 'a fo_term = ATerm of 'a * 'a fo_term list
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    45
datatype quantifier = AForall | AExists
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    46
datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
42531
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    47
datatype ('a, 'b, 'c) formula =
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    48
  AQuant of quantifier * ('a * 'b option) list * ('a, 'b, 'c) formula |
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    49
  AConn of connective * ('a, 'b, 'c) formula list |
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    50
  AAtom of 'c
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    51
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    52
fun mk_anot phi = AConn (ANot, [phi])
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    53
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    54
datatype format = Fof | Tff
42525
7a506b0b644f distinguish FOF and TFF (typed first-order) in ATP abstract syntax tree
blanchet
parents: 42449
diff changeset
    55
datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
42527
6a9458524f01 reintroduced constructor for formulas, and automatically detect which logic to use (TFF or FOF) to avoid clutter
blanchet
parents: 42526
diff changeset
    56
datatype 'a problem_line =
42543
f9d402d144d4 declare TFF types so that SNARK can be used with types
blanchet
parents: 42538
diff changeset
    57
  Decl of string * 'a * 'a list * 'a |
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
    58
  Formula of string * formula_kind * ('a, 'a, 'a fo_term) formula
42529
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
    59
             * string fo_term option * string fo_term option
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    60
type 'a problem = (string * 'a problem_line list) list
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    61
38019
e207a64e1e0b complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
blanchet
parents: 38018
diff changeset
    62
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
e207a64e1e0b complete renaming of "Sledgehammer_TPTP_Format" to "ATP_Problem"
blanchet
parents: 38018
diff changeset
    63
42567
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    64
(* This hash function is recommended in Compilers: Principles, Techniques, and
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    65
   Tools, by Aho, Sethi, and Ullman. The "hashpjw" function, which they
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    66
   particularly recommend, triggers a bug in versions of Poly/ML up to 4.2.0. *)
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    67
fun hashw (u, w) = Word.+ (u, Word.* (0w65599, w))
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    68
fun hashw_char (c, w) = hashw (Word.fromInt (Char.ord c), w)
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    69
fun hashw_string (s : string, w) = CharVector.foldl hashw_char w s
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
    70
38631
979a0b37f981 prefer TPTP "conjecture" tag to "hypothesis" on ATPs where this is possible;
blanchet
parents: 38613
diff changeset
    71
fun string_for_kind Axiom = "axiom"
41769
eb2e39555f98 extended ATP problem syntax to support other applications than Sledgehammer, e.g. experiments with ATPs
blanchet
parents: 41491
diff changeset
    72
  | string_for_kind Definition = "definition"
eb2e39555f98 extended ATP problem syntax to support other applications than Sledgehammer, e.g. experiments with ATPs
blanchet
parents: 41491
diff changeset
    73
  | string_for_kind Lemma = "lemma"
38631
979a0b37f981 prefer TPTP "conjecture" tag to "hypothesis" on ATPs where this is possible;
blanchet
parents: 38613
diff changeset
    74
  | string_for_kind Hypothesis = "hypothesis"
979a0b37f981 prefer TPTP "conjecture" tag to "hypothesis" on ATPs where this is possible;
blanchet
parents: 38613
diff changeset
    75
  | string_for_kind Conjecture = "conjecture"
979a0b37f981 prefer TPTP "conjecture" tag to "hypothesis" on ATPs where this is possible;
blanchet
parents: 38613
diff changeset
    76
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    77
fun string_for_term (ATerm (s, [])) = s
38088
a9847fb539dd fix bug with "=" vs. "fequal" introduced by last change (dddb8ba3a1ce)
blanchet
parents: 38047
diff changeset
    78
  | string_for_term (ATerm ("equal", ts)) =
a9847fb539dd fix bug with "=" vs. "fequal" introduced by last change (dddb8ba3a1ce)
blanchet
parents: 38047
diff changeset
    79
    space_implode " = " (map string_for_term ts)
41769
eb2e39555f98 extended ATP problem syntax to support other applications than Sledgehammer, e.g. experiments with ATPs
blanchet
parents: 41491
diff changeset
    80
  | string_for_term (ATerm ("[]", ts)) =
eb2e39555f98 extended ATP problem syntax to support other applications than Sledgehammer, e.g. experiments with ATPs
blanchet
parents: 41491
diff changeset
    81
    (* used for lists in the optional "source" field of a derivation *)
eb2e39555f98 extended ATP problem syntax to support other applications than Sledgehammer, e.g. experiments with ATPs
blanchet
parents: 41491
diff changeset
    82
    "[" ^ commas (map string_for_term ts) ^ "]"
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    83
  | string_for_term (ATerm (s, ts)) =
38088
a9847fb539dd fix bug with "=" vs. "fequal" introduced by last change (dddb8ba3a1ce)
blanchet
parents: 38047
diff changeset
    84
    s ^ "(" ^ commas (map string_for_term ts) ^ ")"
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    85
fun string_for_quantifier AForall = "!"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    86
  | string_for_quantifier AExists = "?"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    87
fun string_for_connective ANot = "~"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    88
  | string_for_connective AAnd = "&"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    89
  | string_for_connective AOr = "|"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    90
  | string_for_connective AImplies = "=>"
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    91
  | string_for_connective AIf = "<="
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    92
  | string_for_connective AIff = "<=>"
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    93
  | string_for_connective ANotIff = "<~>"
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42525
diff changeset
    94
fun string_for_bound_var (s, NONE) = s
42531
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
    95
  | string_for_bound_var (s, SOME ty) = s ^ " : " ^ ty
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    96
fun string_for_formula (AQuant (q, xs, phi)) =
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42525
diff changeset
    97
    "(" ^ string_for_quantifier q ^
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42525
diff changeset
    98
    "[" ^ commas (map string_for_bound_var xs) ^ "] : " ^
38489
124193c26751 more parentheses in TPTP formulas, just in case
blanchet
parents: 38088
diff changeset
    99
    string_for_formula phi ^ ")"
38034
ecae87b9b9c4 renaming
blanchet
parents: 38027
diff changeset
   100
  | string_for_formula (AConn (ANot, [AAtom (ATerm ("equal", ts))])) =
38005
b6555e9c5de4 prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
blanchet
parents: 38004
diff changeset
   101
    space_implode " != " (map string_for_term ts)
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   102
  | string_for_formula (AConn (c, [phi])) =
38489
124193c26751 more parentheses in TPTP formulas, just in case
blanchet
parents: 38088
diff changeset
   103
    "(" ^ string_for_connective c ^ " " ^ string_for_formula phi ^ ")"
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   104
  | string_for_formula (AConn (c, phis)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   105
    "(" ^ space_implode (" " ^ string_for_connective c ^ " ")
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   106
                        (map string_for_formula phis) ^ ")"
38034
ecae87b9b9c4 renaming
blanchet
parents: 38027
diff changeset
   107
  | string_for_formula (AAtom tm) = string_for_term tm
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   108
42528
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   109
fun string_for_symbol_type [] res_ty = res_ty
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   110
  | string_for_symbol_type [arg_ty] res_ty = arg_ty ^ " > " ^ res_ty
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   111
  | string_for_symbol_type arg_tys res_ty =
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   112
    string_for_symbol_type ["(" ^ space_implode " * " arg_tys ^ ")"] res_ty
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   113
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   114
fun string_for_problem_line _ _ (Decl (ident, sym, arg_tys, res_ty)) =
42533
dc81fe6b7a87 generate TFF type declarations in typed mode
blanchet
parents: 42531
diff changeset
   115
    "tff(" ^ ident ^ ", type,\n    " ^ sym ^ " : " ^
42528
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   116
    string_for_symbol_type arg_tys res_ty ^ ").\n"
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   117
  | string_for_problem_line hypothesis_kind format
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   118
                            (Formula (ident, kind, phi, source, useful_info)) =
42528
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   119
    let
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   120
      val (kind, phi) =
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   121
        if kind = Hypothesis then
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   122
          (hypothesis_kind, phi |> hypothesis_kind = Conjecture ? mk_anot)
42528
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   123
        else
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   124
          (kind, phi)
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   125
    in
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   126
      (case format of Fof => "fof" | Tff => "tff") ^
42528
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   127
      "(" ^ ident ^ ", " ^ string_for_kind kind ^ ",\n    (" ^
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   128
      string_for_formula phi ^ ")" ^
42529
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
   129
      (case (source, useful_info) of
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
   130
         (NONE, NONE) => ""
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
   131
       | (SOME tm, NONE) => ", " ^ string_for_term tm
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
   132
       | (_, SOME tm) =>
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
   133
         ", " ^ string_for_term (source |> the_default (ATerm ("[]", []))) ^
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
   134
         ", " ^ string_for_term tm) ^ ").\n"
42528
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   135
    end
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   136
fun tptp_strings_for_atp_problem hypothesis_kind format problem =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   137
  "% This file was generated by Isabelle (most likely Sledgehammer)\n\
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   138
  \% " ^ timestamp () ^ "\n" ::
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   139
  maps (fn (_, []) => []
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   140
         | (heading, lines) =>
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 39453
diff changeset
   141
           "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   142
           map (string_for_problem_line hypothesis_kind format) lines)
38631
979a0b37f981 prefer TPTP "conjecture" tag to "hypothesis" on ATPs where this is possible;
blanchet
parents: 38613
diff changeset
   143
       problem
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   144
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39384
diff changeset
   145
fun is_atp_variable s = Char.isUpper (String.sub (s, 0))
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38014
diff changeset
   146
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   147
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   148
(** Nice names **)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   149
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   150
fun empty_name_pool readable_names =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   151
  if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   152
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   153
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   154
fun pool_map f xs =
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   155
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   156
42227
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   157
val no_qualifiers =
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   158
  let
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   159
    fun skip [] = []
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   160
      | skip (#"." :: cs) = skip cs
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   161
      | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   162
    and keep [] = []
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   163
      | keep (#"." :: cs) = skip cs
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   164
      | keep (c :: cs) = c :: keep cs
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   165
  in String.explode #> rev #> keep #> rev #> String.implode end
662b50b7126f if "monomorphize" is enabled, mangle the type information in the names by default
blanchet
parents: 41769
diff changeset
   166
42567
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
   167
val max_readable_name_length = 32
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
   168
39109
ceee95f41823 fix bug in "debug" mode
blanchet
parents: 38631
diff changeset
   169
(* "op" is also reserved, to avoid the unreadable "op_1", "op_2", etc., in the
ceee95f41823 fix bug in "debug" mode
blanchet
parents: 38631
diff changeset
   170
   problem files. "equal" is reserved by some ATPs. "eq" is reserved to ensure
ceee95f41823 fix bug in "debug" mode
blanchet
parents: 38631
diff changeset
   171
   that "HOL.eq" is correctly mapped to equality. *)
ceee95f41823 fix bug in "debug" mode
blanchet
parents: 38631
diff changeset
   172
val reserved_nice_names = ["op", "equal", "eq"]
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   173
fun readable_name full_name s =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   174
  if s = full_name then
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   175
    s
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   176
  else
42567
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
   177
    s |> no_qualifiers
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
   178
      |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
42583
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   179
         (* SNARK doesn't like sort (type) names that end with digits. We make
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   180
            an effort to avoid this here. *)
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   181
      |> (fn s => if Char.isDigit (String.sub (s, size s - 1)) then s ^ "_"
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   182
                  else s)
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   183
      |> (fn s =>
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   184
             if size s > max_readable_name_length then
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   185
               String.substring (s, 0, max_readable_name_length div 2 - 4) ^
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   186
               Word.toString (hashw_string (full_name, 0w0)) ^
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   187
               String.extract (s, max_readable_name_length div 2 - 4, NONE)
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   188
             else
84b134118616 avoid trailing digits for SNARK (type) names -- grr...
blanchet
parents: 42577
diff changeset
   189
               s)
42567
d012947edd36 shorten readable names -- they can get really long with monomorphization, which actually slows down the ATPs
blanchet
parents: 42543
diff changeset
   190
      |> (fn s => if member (op =) reserved_nice_names s then full_name else s)
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   191
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   192
fun nice_name (full_name, _) NONE = (full_name, NONE)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   193
  | nice_name (full_name, desired_name) (SOME the_pool) =
39384
76603e40bd4c in debug mode, don't touch "$true" and "$false"
blanchet
parents: 39109
diff changeset
   194
    if String.isPrefix "$" full_name then
76603e40bd4c in debug mode, don't touch "$true" and "$false"
blanchet
parents: 39109
diff changeset
   195
      (full_name, SOME the_pool)
76603e40bd4c in debug mode, don't touch "$true" and "$false"
blanchet
parents: 39109
diff changeset
   196
    else case Symtab.lookup (fst the_pool) full_name of
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   197
      SOME nice_name => (nice_name, SOME the_pool)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   198
    | NONE =>
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   199
      let
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   200
        val nice_prefix = readable_name full_name desired_name
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   201
        fun add j =
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   202
          let
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   203
            val nice_name = nice_prefix ^
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 39453
diff changeset
   204
                            (if j = 0 then "" else "_" ^ string_of_int j)
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   205
          in
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   206
            case Symtab.lookup (snd the_pool) nice_name of
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   207
              SOME full_name' =>
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   208
              if full_name = full_name' then (nice_name, the_pool)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   209
              else add (j + 1)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   210
            | NONE =>
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   211
              (nice_name,
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   212
               (Symtab.update_new (full_name, nice_name) (fst the_pool),
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   213
                Symtab.update_new (nice_name, full_name) (snd the_pool)))
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   214
          end
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   215
      in add 0 |> apsnd SOME end
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   216
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   217
fun nice_term (ATerm (name, ts)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   218
  nice_name name ##>> pool_map nice_term ts #>> ATerm
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   219
fun nice_formula (AQuant (q, xs, phi)) =
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42525
diff changeset
   220
    pool_map nice_name (map fst xs)
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42525
diff changeset
   221
    ##>> pool_map (fn NONE => pair NONE
42531
a462dbaa584f added more rudimentary type support to Sledgehammer's ATP encoding
blanchet
parents: 42530
diff changeset
   222
                    | SOME ty => nice_name ty #>> SOME) (map snd xs)
42526
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42525
diff changeset
   223
    ##>> nice_formula phi
46d485f8d144 added room for types in ATP quantifiers
blanchet
parents: 42525
diff changeset
   224
    #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   225
  | nice_formula (AConn (c, phis)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   226
    pool_map nice_formula phis #>> curry AConn c
38034
ecae87b9b9c4 renaming
blanchet
parents: 38027
diff changeset
   227
  | nice_formula (AAtom tm) = nice_term tm #>> AAtom
42543
f9d402d144d4 declare TFF types so that SNARK can be used with types
blanchet
parents: 42538
diff changeset
   228
fun nice_problem_line (Decl (ident, sym, arg_tys, res_ty)) =
42528
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   229
    nice_name sym
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   230
    ##>> pool_map nice_name arg_tys
a15f0db2bcaf added support for TFF type declarations
blanchet
parents: 42527
diff changeset
   231
    ##>> nice_name res_ty
42543
f9d402d144d4 declare TFF types so that SNARK can be used with types
blanchet
parents: 42538
diff changeset
   232
    #>> (fn ((sym, arg_tys), res_ty) => Decl (ident, sym, arg_tys, res_ty))
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   233
  | nice_problem_line (Formula (ident, kind, phi, source, useful_info)) =
42529
747736d8b47e added "useful_info" argument to ATP formulas -- this will probably be useful later to specify intro, simp, elim to SPASS
blanchet
parents: 42528
diff changeset
   234
    nice_formula phi
42577
78414ec6fa4e made the format (TFF or FOF) of the TPTP problem a global argument of the problem again and have the ATPs report which formats they support
blanchet
parents: 42567
diff changeset
   235
    #>> (fn phi => Formula (ident, kind, phi, source, useful_info))
37931
7b452ff6bff0 no polymorphic "var"s
blanchet
parents: 37926
diff changeset
   236
fun nice_problem problem =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   237
  pool_map (fn (heading, lines) =>
37931
7b452ff6bff0 no polymorphic "var"s
blanchet
parents: 37926
diff changeset
   238
               pool_map nice_problem_line lines #>> pair heading) problem
39452
70a57e40f795 factored out TSTP/SPASS/Vampire proof parsing;
blanchet
parents: 39384
diff changeset
   239
fun nice_atp_problem readable_names problem =
38017
3ad3e3ca2451 move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet
parents: 38014
diff changeset
   240
  nice_problem problem (empty_name_pool readable_names)
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   241
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   242
end;