src/HOL/Tools/ATP/atp_problem.ML
author blanchet
Fri Aug 20 15:16:27 2010 +0200 (2010-08-20)
changeset 38613 4ca2cae2653f
parent 38489 124193c26751
child 38631 979a0b37f981
permissions -rw-r--r--
use "hypothesis" rather than "conjecture" for hypotheses in TPTP format;
avoids relying on inconsistently implemented feature of TPTP format
blanchet@38047
     1
(*  Title:      HOL/Tools/ATP/atp_problem.ML
blanchet@38027
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@37509
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@37509
     4
blanchet@37509
     5
TPTP syntax.
blanchet@37509
     6
*)
blanchet@37509
     7
blanchet@38019
     8
signature ATP_PROBLEM =
blanchet@37509
     9
sig
blanchet@37992
    10
  datatype 'a fo_term = ATerm of 'a * 'a fo_term list
blanchet@37992
    11
  datatype quantifier = AForall | AExists
blanchet@37992
    12
  datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
blanchet@37994
    13
  datatype ('a, 'b) formula =
blanchet@37994
    14
    AQuant of quantifier * 'a list * ('a, 'b) formula |
blanchet@37994
    15
    AConn of connective * ('a, 'b) formula list |
blanchet@38034
    16
    AAtom of 'b
blanchet@37994
    17
blanchet@38613
    18
  datatype kind = Axiom | Hypothesis | Conjecture
blanchet@38017
    19
  datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
blanchet@38017
    20
  type 'a problem = (string * 'a problem_line list) list
blanchet@37992
    21
blanchet@38019
    22
  val timestamp : unit -> string
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@38019
    32
structure ATP_Problem : ATP_PROBLEM =
blanchet@37509
    33
struct
blanchet@37509
    34
blanchet@37643
    35
(** ATP problem **)
blanchet@37643
    36
blanchet@37961
    37
datatype 'a fo_term = ATerm of 'a * 'a fo_term list
blanchet@37961
    38
datatype quantifier = AForall | AExists
blanchet@37992
    39
datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
blanchet@37994
    40
datatype ('a, 'b) formula =
blanchet@37994
    41
  AQuant of quantifier * 'a list * ('a, 'b) formula |
blanchet@37994
    42
  AConn of connective * ('a, 'b) formula list |
blanchet@38034
    43
  AAtom of 'b
blanchet@37961
    44
blanchet@38613
    45
datatype kind = Axiom | Hypothesis | Conjecture
blanchet@37994
    46
datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
blanchet@37643
    47
type 'a problem = (string * 'a problem_line list) list
blanchet@37643
    48
blanchet@38019
    49
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@38019
    50
blanchet@37961
    51
fun string_for_term (ATerm (s, [])) = s
blanchet@38088
    52
  | string_for_term (ATerm ("equal", ts)) =
blanchet@38088
    53
    space_implode " = " (map string_for_term ts)
blanchet@37961
    54
  | string_for_term (ATerm (s, ts)) =
blanchet@38088
    55
    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@38489
    66
    "(" ^ string_for_quantifier q ^ "[" ^ commas xs ^ "] : " ^
blanchet@38489
    67
    string_for_formula phi ^ ")"
blanchet@38034
    68
  | string_for_formula (AConn (ANot, [AAtom (ATerm ("equal", ts))])) =
blanchet@38005
    69
    space_implode " != " (map string_for_term ts)
blanchet@37961
    70
  | string_for_formula (AConn (c, [phi])) =
blanchet@38489
    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@38034
    75
  | string_for_formula (AAtom 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@38613
    79
  (case kind of
blanchet@38613
    80
     Axiom => "axiom"
blanchet@38613
    81
   | Hypothesis => "hypothesis"
blanchet@38613
    82
   | Conjecture => "conjecture") ^
blanchet@38613
    83
  ",\n    (" ^ string_for_formula phi ^ ")).\n"
blanchet@38017
    84
fun strings_for_tptp_problem problem =
blanchet@37643
    85
  "% This file was generated by Isabelle (most likely Sledgehammer)\n\
blanchet@37643
    86
  \% " ^ timestamp () ^ "\n" ::
blanchet@37643
    87
  maps (fn (_, []) => []
blanchet@37643
    88
         | (heading, lines) =>
blanchet@37643
    89
           "\n% " ^ heading ^ " (" ^ Int.toString (length lines) ^ ")\n" ::
blanchet@37643
    90
           map string_for_problem_line lines) problem
blanchet@37643
    91
blanchet@38017
    92
fun is_tptp_variable s = Char.isUpper (String.sub (s, 0))
blanchet@38017
    93
blanchet@37643
    94
blanchet@37643
    95
(** Nice names **)
blanchet@37643
    96
blanchet@37624
    97
fun empty_name_pool readable_names =
blanchet@37643
    98
  if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
blanchet@37624
    99
blanchet@37624
   100
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
blanchet@37624
   101
fun pool_map f xs =
blanchet@37624
   102
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
blanchet@37624
   103
blanchet@37643
   104
(* "equal" is reserved by some ATPs. "op" is also reserved, to avoid the
blanchet@37643
   105
   unreadable "op_1", "op_2", etc., in the problem files. *)
blanchet@37643
   106
val reserved_nice_names = ["equal", "op"]
blanchet@37624
   107
fun readable_name full_name s =
blanchet@37643
   108
  if s = full_name then
blanchet@37643
   109
    s
blanchet@37643
   110
  else
blanchet@37643
   111
    let
blanchet@37643
   112
      val s = s |> Long_Name.base_name
blanchet@37643
   113
                |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
blanchet@37643
   114
    in if member (op =) reserved_nice_names s then full_name else s end
blanchet@37624
   115
blanchet@37624
   116
fun nice_name (full_name, _) NONE = (full_name, NONE)
blanchet@37624
   117
  | nice_name (full_name, desired_name) (SOME the_pool) =
blanchet@37624
   118
    case Symtab.lookup (fst the_pool) full_name of
blanchet@37624
   119
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@37624
   120
    | NONE =>
blanchet@37624
   121
      let
blanchet@37624
   122
        val nice_prefix = readable_name full_name desired_name
blanchet@37624
   123
        fun add j =
blanchet@37624
   124
          let
blanchet@37624
   125
            val nice_name = nice_prefix ^
blanchet@37624
   126
                            (if j = 0 then "" else "_" ^ Int.toString j)
blanchet@37624
   127
          in
blanchet@37624
   128
            case Symtab.lookup (snd the_pool) nice_name of
blanchet@37624
   129
              SOME full_name' =>
blanchet@37624
   130
              if full_name = full_name' then (nice_name, the_pool)
blanchet@37624
   131
              else add (j + 1)
blanchet@37624
   132
            | NONE =>
blanchet@37624
   133
              (nice_name,
blanchet@37624
   134
               (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@37624
   135
                Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@37624
   136
          end
blanchet@37624
   137
      in add 0 |> apsnd SOME end
blanchet@37624
   138
blanchet@37961
   139
blanchet@37961
   140
fun nice_term (ATerm (name, ts)) =
blanchet@37961
   141
  nice_name name ##>> pool_map nice_term ts #>> ATerm
blanchet@37961
   142
fun nice_formula (AQuant (q, xs, phi)) =
blanchet@37961
   143
    pool_map nice_name xs ##>> nice_formula phi
blanchet@37961
   144
    #>> (fn (xs, phi) => AQuant (q, xs, phi))
blanchet@37961
   145
  | nice_formula (AConn (c, phis)) =
blanchet@37961
   146
    pool_map nice_formula phis #>> curry AConn c
blanchet@38034
   147
  | nice_formula (AAtom tm) = nice_term tm #>> AAtom
blanchet@37961
   148
fun nice_problem_line (Fof (ident, kind, phi)) =
blanchet@37961
   149
  nice_formula phi #>> (fn phi => Fof (ident, kind, phi))
blanchet@37931
   150
fun nice_problem problem =
blanchet@37643
   151
  pool_map (fn (heading, lines) =>
blanchet@37931
   152
               pool_map nice_problem_line lines #>> pair heading) problem
blanchet@38017
   153
fun nice_tptp_problem readable_names problem =
blanchet@38017
   154
  nice_problem problem (empty_name_pool readable_names)
blanchet@37509
   155
blanchet@37509
   156
end;