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