src/HOL/Tools/Sledgehammer/sledgehammer_tptp_format.ML
author blanchet
Tue Jul 27 17:32:10 2010 +0200 (2010-07-27)
changeset 38017 3ad3e3ca2451
parent 38014 81c23d286f0c
permissions -rw-r--r--
move Sledgehammer-specific code out of "Sledgehammer_TPTP_Format"
blanchet@37509
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_tptp_format.ML
blanchet@37509
     2
    Author:     Jia Meng, NICTA
blanchet@37509
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@37509
     4
blanchet@37509
     5
TPTP syntax.
blanchet@37509
     6
*)
blanchet@37509
     7
blanchet@37509
     8
signature SLEDGEHAMMER_TPTP_FORMAT =
blanchet@37509
     9
sig
blanchet@37994
    10
  type kind = Metis_Clauses.kind
blanchet@37509
    11
blanchet@37992
    12
  datatype 'a fo_term = ATerm of 'a * 'a fo_term list
blanchet@37992
    13
  datatype quantifier = AForall | AExists
blanchet@37992
    14
  datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
blanchet@37994
    15
  datatype ('a, 'b) formula =
blanchet@37994
    16
    AQuant of quantifier * 'a list * ('a, 'b) formula |
blanchet@37994
    17
    AConn of connective * ('a, 'b) formula list |
blanchet@37994
    18
    APred of 'b
blanchet@37994
    19
blanchet@38017
    20
  datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
blanchet@38017
    21
  type 'a problem = (string * 'a problem_line list) list
blanchet@37992
    22
blanchet@38017
    23
  val is_tptp_variable : string -> bool
blanchet@38017
    24
  val strings_for_tptp_problem :
blanchet@38017
    25
    (string * string problem_line list) list -> string list
blanchet@38017
    26
  val nice_tptp_problem :
blanchet@38017
    27
    bool -> ('a * (string * string) problem_line list) list
blanchet@38017
    28
    -> ('a * string problem_line list) list
blanchet@38017
    29
       * (string Symtab.table * string Symtab.table) option
blanchet@37509
    30
end;
blanchet@37509
    31
blanchet@37509
    32
structure Sledgehammer_TPTP_Format : SLEDGEHAMMER_TPTP_FORMAT =
blanchet@37509
    33
struct
blanchet@37509
    34
blanchet@37509
    35
open Sledgehammer_Util
blanchet@37509
    36
blanchet@38017
    37
type kind = Metis_Clauses.kind
blanchet@37643
    38
blanchet@37643
    39
(** ATP problem **)
blanchet@37643
    40
blanchet@37961
    41
datatype 'a fo_term = ATerm of 'a * 'a fo_term list
blanchet@37961
    42
datatype quantifier = AForall | AExists
blanchet@37992
    43
datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
blanchet@37994
    44
datatype ('a, 'b) formula =
blanchet@37994
    45
  AQuant of quantifier * 'a list * ('a, 'b) formula |
blanchet@37994
    46
  AConn of connective * ('a, 'b) formula list |
blanchet@37994
    47
  APred of 'b
blanchet@37961
    48
blanchet@37994
    49
datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
blanchet@37643
    50
type 'a problem = (string * 'a problem_line list) list
blanchet@37643
    51
blanchet@37961
    52
fun string_for_term (ATerm (s, [])) = s
blanchet@37961
    53
  | string_for_term (ATerm (s, ts)) =
blanchet@37961
    54
    if s = "equal" then space_implode " = " (map string_for_term ts)
blanchet@37961
    55
    else s ^ "(" ^ commas (map string_for_term ts) ^ ")"
blanchet@37961
    56
fun string_for_quantifier AForall = "!"
blanchet@37961
    57
  | string_for_quantifier AExists = "?"
blanchet@37961
    58
fun string_for_connective ANot = "~"
blanchet@37961
    59
  | string_for_connective AAnd = "&"
blanchet@37961
    60
  | string_for_connective AOr = "|"
blanchet@37961
    61
  | string_for_connective AImplies = "=>"
blanchet@37992
    62
  | string_for_connective AIf = "<="
blanchet@37961
    63
  | string_for_connective AIff = "<=>"
blanchet@37992
    64
  | string_for_connective ANotIff = "<~>"
blanchet@37961
    65
fun string_for_formula (AQuant (q, xs, phi)) =
blanchet@37995
    66
    string_for_quantifier q ^ "[" ^ commas xs ^ "]: " ^
blanchet@37961
    67
    string_for_formula phi
blanchet@38005
    68
  | string_for_formula (AConn (ANot, [APred (ATerm ("equal", ts))])) =
blanchet@38005
    69
    space_implode " != " (map string_for_term ts)
blanchet@37961
    70
  | string_for_formula (AConn (c, [phi])) =
blanchet@37961
    71
    string_for_connective c ^ " " ^ string_for_formula phi
blanchet@37961
    72
  | string_for_formula (AConn (c, phis)) =
blanchet@37961
    73
    "(" ^ space_implode (" " ^ string_for_connective c ^ " ")
blanchet@37961
    74
                        (map string_for_formula phis) ^ ")"
blanchet@37961
    75
  | string_for_formula (APred tm) = string_for_term tm
blanchet@37961
    76
blanchet@37961
    77
fun string_for_problem_line (Fof (ident, kind, phi)) =
blanchet@37961
    78
  "fof(" ^ ident ^ ", " ^
blanchet@37961
    79
  (case kind of Axiom => "axiom" | Conjecture => "conjecture") ^ ",\n" ^
blanchet@37961
    80
  "    (" ^ string_for_formula phi ^ ")).\n"
blanchet@38017
    81
fun strings_for_tptp_problem problem =
blanchet@37643
    82
  "% This file was generated by Isabelle (most likely Sledgehammer)\n\
blanchet@37643
    83
  \% " ^ timestamp () ^ "\n" ::
blanchet@37643
    84
  maps (fn (_, []) => []
blanchet@37643
    85
         | (heading, lines) =>
blanchet@37643
    86
           "\n% " ^ heading ^ " (" ^ Int.toString (length lines) ^ ")\n" ::
blanchet@37643
    87
           map string_for_problem_line lines) problem
blanchet@37643
    88
blanchet@38017
    89
fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
blanchet@38017
    90
blanchet@37643
    91
blanchet@37643
    92
(** Nice names **)
blanchet@37643
    93
blanchet@37624
    94
fun empty_name_pool readable_names =
blanchet@37643
    95
  if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
blanchet@37624
    96
blanchet@37624
    97
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
blanchet@37624
    98
fun pool_map f xs =
blanchet@37624
    99
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
blanchet@37624
   100
blanchet@37643
   101
(* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
blanchet@37643
   102
   unreadable "op_1", "op_2", etc., in the problem files. *)
blanchet@37643
   103
val reserved_nice_names = ["equal", "op"]
blanchet@37624
   104
fun readable_name full_name s =
blanchet@37643
   105
  if s = full_name then
blanchet@37643
   106
    s
blanchet@37643
   107
  else
blanchet@37643
   108
    let
blanchet@37643
   109
      val s = s |> Long_Name.base_name
blanchet@37643
   110
                |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
blanchet@37643
   111
    in if member (op =) reserved_nice_names s then full_name else s end
blanchet@37624
   112
blanchet@37624
   113
fun nice_name (full_name, _) NONE = (full_name, NONE)
blanchet@37624
   114
  | nice_name (full_name, desired_name) (SOME the_pool) =
blanchet@37624
   115
    case Symtab.lookup (fst the_pool) full_name of
blanchet@37624
   116
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@37624
   117
    | NONE =>
blanchet@37624
   118
      let
blanchet@37624
   119
        val nice_prefix = readable_name full_name desired_name
blanchet@37624
   120
        fun add j =
blanchet@37624
   121
          let
blanchet@37624
   122
            val nice_name = nice_prefix ^
blanchet@37624
   123
                            (if j = 0 then "" else "_" ^ Int.toString j)
blanchet@37624
   124
          in
blanchet@37624
   125
            case Symtab.lookup (snd the_pool) nice_name of
blanchet@37624
   126
              SOME full_name' =>
blanchet@37624
   127
              if full_name = full_name' then (nice_name, the_pool)
blanchet@37624
   128
              else add (j + 1)
blanchet@37624
   129
            | NONE =>
blanchet@37624
   130
              (nice_name,
blanchet@37624
   131
               (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@37624
   132
                Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@37624
   133
          end
blanchet@37624
   134
      in add 0 |> apsnd SOME end
blanchet@37624
   135
blanchet@37961
   136
blanchet@37961
   137
fun nice_term (ATerm (name, ts)) =
blanchet@37961
   138
  nice_name name ##>> pool_map nice_term ts #>> ATerm
blanchet@37961
   139
fun nice_formula (AQuant (q, xs, phi)) =
blanchet@37961
   140
    pool_map nice_name xs ##>> nice_formula phi
blanchet@37961
   141
    #>> (fn (xs, phi) => AQuant (q, xs, phi))
blanchet@37961
   142
  | nice_formula (AConn (c, phis)) =
blanchet@37961
   143
    pool_map nice_formula phis #>> curry AConn c
blanchet@37961
   144
  | nice_formula (APred tm) = nice_term tm #>> APred
blanchet@37961
   145
fun nice_problem_line (Fof (ident, kind, phi)) =
blanchet@37961
   146
  nice_formula phi #>> (fn phi => Fof (ident, kind, phi))
blanchet@37931
   147
fun nice_problem problem =
blanchet@37643
   148
  pool_map (fn (heading, lines) =>
blanchet@37931
   149
               pool_map nice_problem_line lines #>> pair heading) problem
blanchet@38017
   150
fun nice_tptp_problem readable_names problem =
blanchet@38017
   151
  nice_problem problem (empty_name_pool readable_names)
blanchet@37509
   152
blanchet@37509
   153
end;