src/HOL/Tools/Sledgehammer/sledgehammer_tptp_format.ML
author blanchet
Mon, 26 Jul 2010 23:54:40 +0200
changeset 38005 b6555e9c5de4
parent 38004 43fdc7c259ea
child 38006 31001bc88c1a
permissions -rw-r--r--
prevent schematic variable clash in combinator-introduction code, when invoked from Sledgehammer (another consequence of the CNF -> FOF transition)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_tptp_format.ML
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     2
    Author:     Jia Meng, NICTA
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
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     5
TPTP syntax.
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
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     8
signature SLEDGEHAMMER_TPTP_FORMAT =
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
     9
sig
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    10
  type name = Metis_Clauses.name
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    11
  type kind = Metis_Clauses.kind
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    12
  type combterm = Metis_Clauses.combterm
37925
1188e6bff48d rename "classrel" to "class_rel"
blanchet
parents: 37924
diff changeset
    13
  type class_rel_clause = Metis_Clauses.class_rel_clause
37578
9367cb36b1c4 renamed "Sledgehammer_FOL_Clauses" to "Metis_Clauses", so that Metis doesn't depend on Sledgehammer
blanchet
parents: 37577
diff changeset
    14
  type arity_clause = Metis_Clauses.arity_clause
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    15
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    16
  datatype 'a fo_term = ATerm of 'a * 'a fo_term list
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    17
  datatype quantifier = AForall | AExists
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    18
  datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    19
  datatype ('a, 'b) formula =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    20
    AQuant of quantifier * 'a list * ('a, 'b) formula |
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    21
    AConn of connective * ('a, 'b) formula list |
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    22
    APred of 'b
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    23
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    24
  datatype fol_formula =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    25
    FOLFormula of {formula_name: string,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    26
                   kind: kind,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    27
                   combformula: (name, combterm) formula,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    28
                   ctypes_sorts: typ list}
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    29
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
    30
  val axiom_prefix : string
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    31
  val conjecture_prefix : string
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    32
  val write_tptp_file :
37993
bb39190370fe generate close formulas for SPASS, but not for the others (to avoid clutter)
blanchet
parents: 37992
diff changeset
    33
    theory -> bool -> bool -> bool -> bool -> Path.T
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    34
    -> fol_formula list * fol_formula list * fol_formula list * fol_formula list
37925
1188e6bff48d rename "classrel" to "class_rel"
blanchet
parents: 37924
diff changeset
    35
       * class_rel_clause list * arity_clause list
37926
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
    36
    -> string Symtab.table * int
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
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    39
structure Sledgehammer_TPTP_Format : SLEDGEHAMMER_TPTP_FORMAT =
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
37578
9367cb36b1c4 renamed "Sledgehammer_FOL_Clauses" to "Metis_Clauses", so that Metis doesn't depend on Sledgehammer
blanchet
parents: 37577
diff changeset
    42
open Metis_Clauses
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    43
open Sledgehammer_Util
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
    44
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    45
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    46
(** ATP problem **)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    47
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    48
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
    49
datatype quantifier = AForall | AExists
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    50
datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    51
datatype ('a, 'b) formula =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    52
  AQuant of quantifier * 'a list * ('a, 'b) formula |
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    53
  AConn of connective * ('a, 'b) formula list |
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    54
  APred of 'b
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    55
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    56
fun mk_anot phi = AConn (ANot, [phi])
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    57
fun mk_aconn c phi1 phi2 = AConn (c, [phi1, phi2])
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    58
fun mk_adisjunction [] = APred (ATerm (("$false", "$false"), []))
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    59
  | mk_adisjunction (phi :: phis) = fold (mk_aconn AOr) phis phi
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    60
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
    61
datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    62
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
    63
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    64
fun string_for_term (ATerm (s, [])) = s
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    65
  | string_for_term (ATerm (s, ts)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    66
    if s = "equal" then space_implode " = " (map string_for_term ts)
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    67
    else s ^ "(" ^ commas (map string_for_term ts) ^ ")"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    68
fun string_for_quantifier AForall = "!"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    69
  | string_for_quantifier AExists = "?"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    70
fun string_for_connective ANot = "~"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    71
  | string_for_connective AAnd = "&"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    72
  | string_for_connective AOr = "|"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    73
  | string_for_connective AImplies = "=>"
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    74
  | string_for_connective AIf = "<="
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    75
  | string_for_connective AIff = "<=>"
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
    76
  | string_for_connective ANotIff = "<~>"
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    77
fun string_for_formula (AQuant (q, xs, phi)) =
37995
06f02b15ef8a generate full first-order formulas (FOF) in Sledgehammer
blanchet
parents: 37994
diff changeset
    78
    string_for_quantifier q ^ "[" ^ commas xs ^ "]: " ^
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    79
    string_for_formula phi
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
    80
  | string_for_formula (AConn (ANot, [APred (ATerm ("equal", ts))])) =
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
    81
    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
    82
  | string_for_formula (AConn (c, [phi])) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    83
    string_for_connective c ^ " " ^ string_for_formula phi
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    84
  | string_for_formula (AConn (c, phis)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    85
    "(" ^ space_implode (" " ^ string_for_connective c ^ " ")
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    86
                        (map string_for_formula phis) ^ ")"
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    87
  | string_for_formula (APred tm) = string_for_term tm
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    88
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    89
fun string_for_problem_line (Fof (ident, kind, phi)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    90
  "fof(" ^ ident ^ ", " ^
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    91
  (case kind of Axiom => "axiom" | Conjecture => "conjecture") ^ ",\n" ^
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
    92
  "    (" ^ string_for_formula phi ^ ")).\n"
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    93
fun strings_for_problem problem =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    94
  "% 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
    95
  \% " ^ timestamp () ^ "\n" ::
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    96
  maps (fn (_, []) => []
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    97
         | (heading, lines) =>
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    98
           "\n% " ^ heading ^ " (" ^ Int.toString (length lines) ^ ")\n" ::
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
    99
           map string_for_problem_line lines) problem
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   100
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   101
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   102
(** Nice names **)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   103
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   104
fun empty_name_pool readable_names =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   105
  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
   106
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   107
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
   108
fun pool_map f xs =
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   109
  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
   110
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   111
(* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   112
   unreadable "op_1", "op_2", etc., in the problem files. *)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   113
val reserved_nice_names = ["equal", "op"]
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   114
fun readable_name full_name s =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   115
  if s = full_name then
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   116
    s
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   117
  else
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   118
    let
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   119
      val s = s |> Long_Name.base_name
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   120
                |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   121
    in if member (op =) reserved_nice_names s then full_name else s end
37624
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   122
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   123
fun nice_name (full_name, _) NONE = (full_name, NONE)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   124
  | nice_name (full_name, desired_name) (SOME the_pool) =
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   125
    case Symtab.lookup (fst the_pool) full_name of
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   126
      SOME nice_name => (nice_name, SOME the_pool)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   127
    | NONE =>
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   128
      let
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   129
        val nice_prefix = readable_name full_name desired_name
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   130
        fun add j =
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   131
          let
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   132
            val nice_name = nice_prefix ^
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   133
                            (if j = 0 then "" else "_" ^ Int.toString j)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   134
          in
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   135
            case Symtab.lookup (snd the_pool) nice_name of
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   136
              SOME full_name' =>
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   137
              if full_name = full_name' then (nice_name, the_pool)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   138
              else add (j + 1)
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   139
            | NONE =>
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   140
              (nice_name,
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   141
               (Symtab.update_new (full_name, nice_name) (fst the_pool),
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   142
                Symtab.update_new (nice_name, full_name) (snd the_pool)))
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   143
          end
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   144
      in add 0 |> apsnd SOME end
3ee568334813 move "nice names" from Metis to TPTP format
blanchet
parents: 37617
diff changeset
   145
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   146
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   147
fun nice_term (ATerm (name, ts)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   148
  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
   149
fun nice_formula (AQuant (q, xs, phi)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   150
    pool_map nice_name xs ##>> nice_formula phi
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   151
    #>> (fn (xs, phi) => AQuant (q, xs, phi))
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   152
  | nice_formula (AConn (c, phis)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   153
    pool_map nice_formula phis #>> curry AConn c
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   154
  | nice_formula (APred tm) = nice_term tm #>> APred
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   155
fun nice_problem_line (Fof (ident, kind, phi)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   156
  nice_formula phi #>> (fn phi => Fof (ident, kind, phi))
37931
7b452ff6bff0 no polymorphic "var"s
blanchet
parents: 37926
diff changeset
   157
fun nice_problem problem =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   158
  pool_map (fn (heading, lines) =>
37931
7b452ff6bff0 no polymorphic "var"s
blanchet
parents: 37926
diff changeset
   159
               pool_map nice_problem_line lines #>> pair heading) problem
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   160
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   161
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   162
(** Sledgehammer stuff **)
37520
9fc2ae73c5ca fix syntax bug in the TPTP output, by ensuring that "hBOOL" is correctly used for n-ary predicates even if (n + k)-ary occurrences of the same predicate, but with a different type, occur in the same problem
blanchet
parents: 37519
diff changeset
   163
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   164
datatype fol_formula =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   165
  FOLFormula of {formula_name: string,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   166
                 kind: kind,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   167
                 combformula: (name, combterm) formula,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   168
                 ctypes_sorts: typ list}
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   169
37962
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
   170
val axiom_prefix = "ax_"
d7dbe01f48d7 keep track of clause numbers for SPASS now that we generate FOF rather than CNF problems;
blanchet
parents: 37961
diff changeset
   171
val conjecture_prefix = "conj_"
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   172
val arity_clause_prefix = "clsarity_"
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   173
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   174
fun wrap_type ty t = ATerm ((type_wrapper_name, type_wrapper_name), [ty, t])
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   175
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   176
fun fo_term_for_combtyp (CombTVar name) = ATerm (name, [])
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   177
  | fo_term_for_combtyp (CombTFree name) = ATerm (name, [])
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   178
  | fo_term_for_combtyp (CombType (name, tys)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   179
    ATerm (name, map fo_term_for_combtyp tys)
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   180
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   181
fun fo_literal_for_type_literal pos (TyLitVar (class, name)) =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   182
    (pos, ATerm (class, [ATerm (name, [])]))
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   183
  | fo_literal_for_type_literal pos (TyLitFree (class, name)) =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   184
    (pos, ATerm (class, [ATerm (name, [])]))
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   185
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   186
fun formula_for_fo_literal (pos, t) = APred t |> not pos ? mk_anot
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   187
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   188
fun fo_term_for_combterm full_types =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   189
  let
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   190
    fun aux top_level u =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   191
      let
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   192
        val (head, args) = strip_combterm_comb u
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   193
        val (x, ty_args) =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   194
          case head of
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   195
            CombConst (name, _, ty_args) =>
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   196
            if fst name = "equal" then
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   197
              (if top_level andalso length args = 2 then name
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   198
               else ("c_fequal", @{const_name fequal}), [])
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   199
            else
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   200
              (name, if full_types then [] else ty_args)
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   201
          | CombVar (name, _) => (name, [])
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   202
          | CombApp _ => raise Fail "impossible \"CombApp\""
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   203
        val t = ATerm (x, map fo_term_for_combtyp ty_args @
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   204
                          map (aux false) args)
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   205
    in
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   206
      if full_types then wrap_type (fo_term_for_combtyp (combtyp_of u)) t else t
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   207
    end
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   208
  in aux true end
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   209
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   210
fun formula_for_combformula full_types =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   211
  let
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   212
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   213
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   214
      | aux (APred tm) = APred (fo_term_for_combterm full_types tm)
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   215
  in aux end
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   216
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   217
fun formula_for_axiom full_types (FOLFormula {combformula, ctypes_sorts, ...}) =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   218
  formula_for_combformula full_types combformula ::
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   219
  map (formula_for_fo_literal o fo_literal_for_type_literal false)
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   220
      (type_literals_for_types ctypes_sorts)
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   221
  |> mk_adisjunction
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   222
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   223
fun problem_line_for_axiom full_types
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   224
        (formula as FOLFormula {formula_name, kind, ...}) =
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   225
  Fof (axiom_prefix ^ ascii_of formula_name, kind,
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   226
       formula_for_axiom full_types formula)
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   227
37926
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   228
fun problem_line_for_class_rel_clause
37925
1188e6bff48d rename "classrel" to "class_rel"
blanchet
parents: 37924
diff changeset
   229
        (ClassRelClause {axiom_name, subclass, superclass, ...}) =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   230
  let val ty_arg = ATerm (("T", "T"), []) in
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   231
    Fof (ascii_of axiom_name, Axiom,
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   232
         AConn (AImplies, [APred (ATerm (subclass, [ty_arg])),
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   233
                           APred (ATerm (superclass, [ty_arg]))]))
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   234
  end
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   235
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   236
fun fo_literal_for_arity_literal (TConsLit (c, t, args)) =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   237
    (true, ATerm (c, [ATerm (t, map (fn arg => ATerm (arg, [])) args)]))
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   238
  | fo_literal_for_arity_literal (TVarLit (c, sort)) =
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   239
    (false, ATerm (c, [ATerm (sort, [])]))
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   240
37926
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   241
fun problem_line_for_arity_clause
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   242
        (ArityClause {axiom_name, conclLit, premLits, ...}) =
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   243
  Fof (arity_clause_prefix ^ ascii_of axiom_name, Axiom,
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   244
       conclLit :: premLits
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   245
       |> map (formula_for_fo_literal o fo_literal_for_arity_literal)
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   246
       |> mk_adisjunction)
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   247
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   248
fun problem_line_for_conjecture full_types
38004
43fdc7c259ea remove obsolete field in record
blanchet
parents: 37995
diff changeset
   249
        (FOLFormula {formula_name, kind, combformula, ...}) =
43fdc7c259ea remove obsolete field in record
blanchet
parents: 37995
diff changeset
   250
  Fof (conjecture_prefix ^ formula_name, kind,
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   251
       formula_for_combformula full_types combformula)
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   252
37994
b04307085a09 make TPTP generator accept full first-order formulas
blanchet
parents: 37993
diff changeset
   253
fun atp_free_type_literals_for_conjecture (FOLFormula {ctypes_sorts, ...}) =
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   254
  map (fo_literal_for_type_literal true) (type_literals_for_types ctypes_sorts)
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   255
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   256
fun problem_line_for_free_type lit =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   257
  Fof ("tfree_tcs", Conjecture, formula_for_fo_literal lit)
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   258
fun problem_lines_for_free_types conjectures =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   259
  let
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   260
    val litss = map atp_free_type_literals_for_conjecture conjectures
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   261
    val lits = fold (union (op =)) litss []
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   262
  in map problem_line_for_free_type lits end
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   263
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   264
(** "hBOOL" and "hAPP" **)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   265
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   266
type const_info = {min_arity: int, max_arity: int, sub_level: bool}
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   267
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   268
fun is_variable s = Char.isUpper (String.sub (s, 0))
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   269
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   270
fun consider_term top_level (ATerm ((s, _), ts)) =
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   271
  (if is_variable s then
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   272
     I
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   273
   else
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   274
     let val n = length ts in
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   275
       Symtab.map_default
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   276
           (s, {min_arity = n, max_arity = 0, sub_level = false})
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   277
           (fn {min_arity, max_arity, sub_level} =>
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   278
               {min_arity = Int.min (n, min_arity),
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   279
                max_arity = Int.max (n, max_arity),
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   280
                sub_level = sub_level orelse not top_level})
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   281
     end)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   282
  #> fold (consider_term (top_level andalso s = type_wrapper_name)) ts
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   283
fun consider_formula (AQuant (_, _, phi)) = consider_formula phi
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   284
  | consider_formula (AConn (_, phis)) = fold consider_formula phis
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   285
  | consider_formula (APred tm) = consider_term true tm
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   286
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   287
fun consider_problem_line (Fof (_, _, phi)) = consider_formula phi
37931
7b452ff6bff0 no polymorphic "var"s
blanchet
parents: 37926
diff changeset
   288
fun consider_problem problem = fold (fold consider_problem_line o snd) problem
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   289
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   290
fun const_table_for_problem explicit_apply problem =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   291
  if explicit_apply then NONE
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   292
  else SOME (Symtab.empty |> consider_problem problem)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   293
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   294
val tc_fun = make_fixed_type_const @{type_name fun}
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   295
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   296
fun min_arity_of thy full_types NONE s =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   297
    (if s = "equal" orelse s = type_wrapper_name orelse
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   298
        String.isPrefix type_const_prefix s orelse
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   299
        String.isPrefix class_prefix s then
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   300
       16383 (* large number *)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   301
     else if full_types then
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   302
       0
37992
7911e78a7122 renamed internal function
blanchet
parents: 37962
diff changeset
   303
     else case strip_prefix_and_undo_ascii const_prefix s of
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   304
       SOME s' => num_type_args thy (invert_const s')
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   305
     | NONE => 0)
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   306
  | min_arity_of _ _ (SOME the_const_tab) s =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   307
    case Symtab.lookup the_const_tab s of
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   308
      SOME ({min_arity, ...} : const_info) => min_arity
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   309
    | NONE => 0
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   310
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   311
fun full_type_of (ATerm ((s, _), [ty, _])) =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   312
    if s = type_wrapper_name then ty else raise Fail "expected type wrapper"
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   313
  | full_type_of _ = raise Fail "expected type wrapper"
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   314
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   315
fun list_hAPP_rev _ t1 [] = t1
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   316
  | list_hAPP_rev NONE t1 (t2 :: ts2) =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   317
    ATerm (`I "hAPP", [list_hAPP_rev NONE t1 ts2, t2])
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   318
  | list_hAPP_rev (SOME ty) t1 (t2 :: ts2) =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   319
    let val ty' = ATerm (`make_fixed_type_const @{type_name fun},
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   320
                         [full_type_of t2, ty]) in
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   321
      ATerm (`I "hAPP", [wrap_type ty' (list_hAPP_rev (SOME ty') t1 ts2), t2])
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   322
    end
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   323
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   324
fun repair_applications_in_term thy full_types const_tab =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   325
  let
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   326
    fun aux opt_ty (ATerm (name as (s, _), ts)) =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   327
      if s = type_wrapper_name then
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   328
        case ts of
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   329
          [t1, t2] => ATerm (name, [aux NONE t1, aux (SOME t1) t2])
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   330
        | _ => raise Fail "malformed type wrapper"
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   331
      else
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   332
        let
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   333
          val ts = map (aux NONE) ts
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   334
          val (ts1, ts2) = chop (min_arity_of thy full_types const_tab s) ts
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   335
        in list_hAPP_rev opt_ty (ATerm (name, ts1)) (rev ts2) end
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   336
  in aux NONE end
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   337
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   338
fun boolify t = ATerm (`I "hBOOL", [t])
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   339
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   340
(* True if the constant ever appears outside of the top-level position in
37520
9fc2ae73c5ca fix syntax bug in the TPTP output, by ensuring that "hBOOL" is correctly used for n-ary predicates even if (n + k)-ary occurrences of the same predicate, but with a different type, occur in the same problem
blanchet
parents: 37519
diff changeset
   341
   literals, or if it appears with different arities (e.g., because of different
9fc2ae73c5ca fix syntax bug in the TPTP output, by ensuring that "hBOOL" is correctly used for n-ary predicates even if (n + k)-ary occurrences of the same predicate, but with a different type, occur in the same problem
blanchet
parents: 37519
diff changeset
   342
   type instantiations). If false, the constant always receives all of its
9fc2ae73c5ca fix syntax bug in the TPTP output, by ensuring that "hBOOL" is correctly used for n-ary predicates even if (n + k)-ary occurrences of the same predicate, but with a different type, occur in the same problem
blanchet
parents: 37519
diff changeset
   343
   arguments and is used as a predicate. *)
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   344
fun is_predicate NONE s =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   345
    s = "equal" orelse String.isPrefix type_const_prefix s orelse
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   346
    String.isPrefix class_prefix s
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   347
  | is_predicate (SOME the_const_tab) s =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   348
    case Symtab.lookup the_const_tab s of
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   349
      SOME {min_arity, max_arity, sub_level} =>
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   350
      not sub_level andalso min_arity = max_arity
37520
9fc2ae73c5ca fix syntax bug in the TPTP output, by ensuring that "hBOOL" is correctly used for n-ary predicates even if (n + k)-ary occurrences of the same predicate, but with a different type, occur in the same problem
blanchet
parents: 37519
diff changeset
   351
    | NONE => false
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   352
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   353
fun repair_predicates_in_term const_tab (t as ATerm ((s, _), ts)) =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   354
  if s = type_wrapper_name then
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   355
    case ts of
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   356
      [_, t' as ATerm ((s', _), _)] =>
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   357
      if is_predicate const_tab s' then t' else boolify t
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   358
    | _ => raise Fail "malformed type wrapper"
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   359
  else
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   360
    t |> not (is_predicate const_tab s) ? boolify
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   361
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   362
fun close_universally phi =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   363
  let
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   364
    fun term_vars bounds (ATerm (name as (s, _), tms)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   365
        (is_variable s andalso not (member (op =) bounds name))
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   366
          ? insert (op =) name
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   367
        #> fold (term_vars bounds) tms
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   368
    fun formula_vars bounds (AQuant (q, xs, phi)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   369
        formula_vars (xs @ bounds) phi
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   370
      | formula_vars bounds (AConn (_, phis)) = fold (formula_vars bounds) phis
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   371
      | formula_vars bounds (APred tm) = term_vars bounds tm
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   372
  in
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   373
    case formula_vars [] phi [] of [] => phi | xs => AQuant (AForall, xs, phi)
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   374
  end
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   375
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   376
fun repair_formula thy explicit_forall full_types const_tab =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   377
  let
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   378
    fun aux (AQuant (q, xs, phi)) = AQuant (q, xs, aux phi)
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   379
      | aux (AConn (c, phis)) = AConn (c, map aux phis)
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   380
      | aux (APred tm) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   381
        APred (tm |> repair_applications_in_term thy full_types const_tab
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   382
                  |> repair_predicates_in_term const_tab)
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   383
  in aux #> explicit_forall ? close_universally end
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   384
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   385
fun repair_problem_line thy explicit_forall full_types const_tab
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   386
                        (Fof (ident, kind, phi)) =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   387
  Fof (ident, kind, repair_formula thy explicit_forall full_types const_tab phi)
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   388
val repair_problem_with_const_table = map o apsnd o map oooo repair_problem_line
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   389
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   390
fun repair_problem thy explicit_forall full_types explicit_apply problem =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   391
  repair_problem_with_const_table thy explicit_forall full_types
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   392
      (const_table_for_problem explicit_apply problem) problem
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   393
37993
bb39190370fe generate close formulas for SPASS, but not for the others (to avoid clutter)
blanchet
parents: 37992
diff changeset
   394
fun write_tptp_file thy readable_names explicit_forall full_types explicit_apply
bb39190370fe generate close formulas for SPASS, but not for the others (to avoid clutter)
blanchet
parents: 37992
diff changeset
   395
                    file (conjectures, axiom_clauses, extra_clauses,
bb39190370fe generate close formulas for SPASS, but not for the others (to avoid clutter)
blanchet
parents: 37992
diff changeset
   396
                          helper_clauses, class_rel_clauses, arity_clauses) =
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   397
  let
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   398
    val axiom_lines = map (problem_line_for_axiom full_types) axiom_clauses
37926
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   399
    val class_rel_lines =
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   400
      map problem_line_for_class_rel_clause class_rel_clauses
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   401
    val arity_lines = map problem_line_for_arity_clause arity_clauses
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   402
    val helper_lines = map (problem_line_for_axiom full_types) helper_clauses
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   403
    val conjecture_lines =
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   404
      map (problem_line_for_conjecture full_types) conjectures
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   405
    val tfree_lines = problem_lines_for_free_types conjectures
37961
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   406
    val problem =
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   407
      [("Relevant facts", axiom_lines),
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   408
       ("Class relationships", class_rel_lines),
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   409
       ("Arity declarations", arity_lines),
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   410
       ("Helper facts", helper_lines),
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   411
       ("Conjectures", conjecture_lines),
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   412
       ("Type variables", tfree_lines)]
6a48c85a211a first step in using "fof" rather than "cnf" in TPTP problems
blanchet
parents: 37931
diff changeset
   413
      |> repair_problem thy explicit_forall full_types explicit_apply
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   414
    val (problem, pool) = 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
   415
    val conjecture_offset =
37925
1188e6bff48d rename "classrel" to "class_rel"
blanchet
parents: 37924
diff changeset
   416
      length axiom_lines + length class_rel_lines + length arity_lines
37643
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   417
      + length helper_lines
f576af716aa6 rewrote the TPTP problem generation code more or less from scratch;
blanchet
parents: 37642
diff changeset
   418
    val _ = File.write_list file (strings_for_problem problem)
37926
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   419
  in
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   420
    (case pool of SOME the_pool => snd the_pool | NONE => Symtab.empty,
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   421
     conjecture_offset)
e6ff246c0cdb renamings + only need second component of name pool to reconstruct proofs
blanchet
parents: 37925
diff changeset
   422
  end
37509
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   423
f39464d971c4 factor out TPTP format output into file of its own, to facilitate further changes
blanchet
parents:
diff changeset
   424
end;