src/HOL/Tools/ATP/atp_problem.ML
author blanchet
Sun, 01 May 2011 18:37:24 +0200
changeset 43399 a15f0db2bcaf
parent 43398 6a9458524f01
child 43400 747736d8b47e
permissions -rw-r--r--
added support for TFF type declarations
     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 Abstract representation of ATP problems and 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 * 'a option) list * ('a, 'b) formula |
    15     AConn of connective * ('a, 'b) formula list |
    16     AAtom of 'b
    17   type 'a uniform_formula = ('a, 'a fo_term) formula
    18 
    19   datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
    20   datatype 'a problem_line =
    21     Type_Decl of string * 'a * 'a list * 'a |
    22     Formula of string * formula_kind * ('a, 'a fo_term) formula
    23                * string fo_term option
    24   type 'a problem = (string * 'a problem_line list) list
    25 
    26   val timestamp : unit -> string
    27   val is_atp_variable : string -> bool
    28   val tptp_strings_for_atp_problem :
    29     bool -> (string * string problem_line list) list -> string list
    30   val nice_atp_problem :
    31     bool -> ('a * (string * string) problem_line list) list
    32     -> ('a * string problem_line list) list
    33        * (string Symtab.table * string Symtab.table) option
    34 end;
    35 
    36 structure ATP_Problem : ATP_PROBLEM =
    37 struct
    38 
    39 (** ATP problem **)
    40 
    41 datatype 'a fo_term = ATerm of 'a * 'a fo_term list
    42 datatype quantifier = AForall | AExists
    43 datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
    44 datatype ('a, 'b) formula =
    45   AQuant of quantifier * ('a * 'a option) list * ('a, 'b) formula |
    46   AConn of connective * ('a, 'b) formula list |
    47   AAtom of 'b
    48 type 'a uniform_formula = ('a, 'a fo_term) formula
    49 
    50 datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
    51 datatype 'a problem_line =
    52   Type_Decl of string * 'a * 'a list * 'a |
    53   Formula of string * formula_kind * ('a, 'a fo_term) formula
    54              * string fo_term option
    55 type 'a problem = (string * 'a problem_line list) list
    56 
    57 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
    58 
    59 fun string_for_kind Axiom = "axiom"
    60   | string_for_kind Definition = "definition"
    61   | string_for_kind Lemma = "lemma"
    62   | string_for_kind Hypothesis = "hypothesis"
    63   | string_for_kind Conjecture = "conjecture"
    64 
    65 fun string_for_term (ATerm (s, [])) = s
    66   | string_for_term (ATerm ("equal", ts)) =
    67     space_implode " = " (map string_for_term ts)
    68   | string_for_term (ATerm ("[]", ts)) =
    69     (* used for lists in the optional "source" field of a derivation *)
    70     "[" ^ commas (map string_for_term ts) ^ "]"
    71   | string_for_term (ATerm (s, ts)) =
    72     s ^ "(" ^ commas (map string_for_term ts) ^ ")"
    73 fun string_for_quantifier AForall = "!"
    74   | string_for_quantifier AExists = "?"
    75 fun string_for_connective ANot = "~"
    76   | string_for_connective AAnd = "&"
    77   | string_for_connective AOr = "|"
    78   | string_for_connective AImplies = "=>"
    79   | string_for_connective AIf = "<="
    80   | string_for_connective AIff = "<=>"
    81   | string_for_connective ANotIff = "<~>"
    82 fun string_for_bound_var (s, NONE) = s
    83   | string_for_bound_var (s, SOME t) = s ^ " : " ^ t
    84 fun string_for_formula (AQuant (q, xs, phi)) =
    85     "(" ^ string_for_quantifier q ^
    86     "[" ^ commas (map string_for_bound_var xs) ^ "] : " ^
    87     string_for_formula phi ^ ")"
    88   | string_for_formula (AConn (ANot, [AAtom (ATerm ("equal", ts))])) =
    89     space_implode " != " (map string_for_term ts)
    90   | string_for_formula (AConn (c, [phi])) =
    91     "(" ^ string_for_connective c ^ " " ^ string_for_formula phi ^ ")"
    92   | string_for_formula (AConn (c, phis)) =
    93     "(" ^ space_implode (" " ^ string_for_connective c ^ " ")
    94                         (map string_for_formula phis) ^ ")"
    95   | string_for_formula (AAtom tm) = string_for_term tm
    96 
    97 fun formula_needs_typed_logic (AQuant (_, xs, phi)) =
    98     exists (is_some o snd) xs orelse formula_needs_typed_logic phi
    99   | formula_needs_typed_logic (AConn (_, phis)) =
   100     exists formula_needs_typed_logic phis
   101   | formula_needs_typed_logic (AAtom _) = false
   102 
   103 fun string_for_symbol_type [] res_ty = res_ty
   104   | string_for_symbol_type [arg_ty] res_ty = arg_ty ^ " > " ^ res_ty
   105   | string_for_symbol_type arg_tys res_ty =
   106     string_for_symbol_type ["(" ^ space_implode " * " arg_tys ^ ")"] res_ty
   107 
   108 fun string_for_problem_line _ (Type_Decl (ident, sym, arg_tys, res_ty)) =
   109     "tff(" ^ ident ^ ", type, " ^ sym ^ " : " ^
   110     string_for_symbol_type arg_tys res_ty ^ ").\n"
   111   | string_for_problem_line use_conjecture_for_hypotheses
   112                             (Formula (ident, kind, phi, source)) =
   113     let
   114       val (kind, phi) =
   115         if kind = Hypothesis andalso use_conjecture_for_hypotheses then
   116           (Conjecture, AConn (ANot, [phi]))
   117         else
   118           (kind, phi)
   119     in
   120       (if formula_needs_typed_logic phi then "tff" else "fof") ^
   121       "(" ^ ident ^ ", " ^ string_for_kind kind ^ ",\n    (" ^
   122       string_for_formula phi ^ ")" ^
   123       (case source of
   124          SOME tm => ", " ^ string_for_term tm
   125        | NONE => "") ^ ").\n"
   126     end
   127 fun tptp_strings_for_atp_problem use_conjecture_for_hypotheses problem =
   128   "% This file was generated by Isabelle (most likely Sledgehammer)\n\
   129   \% " ^ timestamp () ^ "\n" ::
   130   maps (fn (_, []) => []
   131          | (heading, lines) =>
   132            "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
   133            map (string_for_problem_line use_conjecture_for_hypotheses) lines)
   134        problem
   135 
   136 fun is_atp_variable s = Char.isUpper (String.sub (s, 0))
   137 
   138 
   139 (** Nice names **)
   140 
   141 fun empty_name_pool readable_names =
   142   if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
   143 
   144 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
   145 fun pool_map f xs =
   146   pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
   147 
   148 val no_qualifiers =
   149   let
   150     fun skip [] = []
   151       | skip (#"." :: cs) = skip cs
   152       | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
   153     and keep [] = []
   154       | keep (#"." :: cs) = skip cs
   155       | keep (c :: cs) = c :: keep cs
   156   in String.explode #> rev #> keep #> rev #> String.implode end
   157 
   158 (* "op" is also reserved, to avoid the unreadable "op_1", "op_2", etc., in the
   159    problem files. "equal" is reserved by some ATPs. "eq" is reserved to ensure
   160    that "HOL.eq" is correctly mapped to equality. *)
   161 val reserved_nice_names = ["op", "equal", "eq"]
   162 fun readable_name full_name s =
   163   if s = full_name then
   164     s
   165   else
   166     let
   167       val s = s |> no_qualifiers
   168                 |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
   169     in if member (op =) reserved_nice_names s then full_name else s end
   170 
   171 fun nice_name (full_name, _) NONE = (full_name, NONE)
   172   | nice_name (full_name, desired_name) (SOME the_pool) =
   173     if String.isPrefix "$" full_name then
   174       (full_name, SOME the_pool)
   175     else case Symtab.lookup (fst the_pool) full_name of
   176       SOME nice_name => (nice_name, SOME the_pool)
   177     | NONE =>
   178       let
   179         val nice_prefix = readable_name full_name desired_name
   180         fun add j =
   181           let
   182             val nice_name = nice_prefix ^
   183                             (if j = 0 then "" else "_" ^ string_of_int j)
   184           in
   185             case Symtab.lookup (snd the_pool) nice_name of
   186               SOME full_name' =>
   187               if full_name = full_name' then (nice_name, the_pool)
   188               else add (j + 1)
   189             | NONE =>
   190               (nice_name,
   191                (Symtab.update_new (full_name, nice_name) (fst the_pool),
   192                 Symtab.update_new (nice_name, full_name) (snd the_pool)))
   193           end
   194       in add 0 |> apsnd SOME end
   195 
   196 fun nice_term (ATerm (name, ts)) =
   197   nice_name name ##>> pool_map nice_term ts #>> ATerm
   198 fun nice_formula (AQuant (q, xs, phi)) =
   199     pool_map nice_name (map fst xs)
   200     ##>> pool_map (fn NONE => pair NONE
   201                     | SOME s => nice_name s #>> SOME) (map snd xs)
   202     ##>> nice_formula phi
   203     #>> (fn ((ss, ts), phi) => AQuant (q, ss ~~ ts, phi))
   204   | nice_formula (AConn (c, phis)) =
   205     pool_map nice_formula phis #>> curry AConn c
   206   | nice_formula (AAtom tm) = nice_term tm #>> AAtom
   207 fun nice_problem_line (Type_Decl (ident, sym, arg_tys, res_ty)) =
   208     nice_name sym
   209     ##>> pool_map nice_name arg_tys
   210     ##>> nice_name res_ty
   211     #>> (fn ((sym, arg_tys), res_ty) => Type_Decl (ident, sym, arg_tys, res_ty))
   212   | nice_problem_line (Formula (ident, kind, phi, source)) =
   213     nice_formula phi #>> (fn phi => Formula (ident, kind, phi, source))
   214 fun nice_problem problem =
   215   pool_map (fn (heading, lines) =>
   216                pool_map nice_problem_line lines #>> pair heading) problem
   217 fun nice_atp_problem readable_names problem =
   218   nice_problem problem (empty_name_pool readable_names)
   219 
   220 end;