src/HOL/Tools/ATP/atp_problem.ML
author wenzelm
Mon Jan 10 15:45:46 2011 +0100 (2011-01-10)
changeset 41491 a2ad5b824051
parent 39453 1740a2d6bef9
child 41769 eb2e39555f98
permissions -rw-r--r--
eliminated Int.toString;
     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 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 kind = Axiom | Hypothesis | Conjecture
    20   datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
    21   type 'a problem = (string * 'a problem_line list) list
    22 
    23   val timestamp : unit -> string
    24   val is_atp_variable : string -> bool
    25   val tptp_strings_for_atp_problem :
    26     bool -> (string * string problem_line list) list -> string list
    27   val nice_atp_problem :
    28     bool -> ('a * (string * string) problem_line list) list
    29     -> ('a * string problem_line list) list
    30        * (string Symtab.table * string Symtab.table) option
    31 end;
    32 
    33 structure ATP_Problem : ATP_PROBLEM =
    34 struct
    35 
    36 (** ATP problem **)
    37 
    38 datatype 'a fo_term = ATerm of 'a * 'a fo_term list
    39 datatype quantifier = AForall | AExists
    40 datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
    41 datatype ('a, 'b) formula =
    42   AQuant of quantifier * 'a list * ('a, 'b) formula |
    43   AConn of connective * ('a, 'b) formula list |
    44   AAtom of 'b
    45 type 'a uniform_formula = ('a, 'a fo_term) formula
    46 
    47 datatype kind = Axiom | Hypothesis | Conjecture
    48 datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
    49 type 'a problem = (string * 'a problem_line list) list
    50 
    51 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
    52 
    53 fun string_for_kind Axiom = "axiom"
    54   | string_for_kind Hypothesis = "hypothesis"
    55   | string_for_kind Conjecture = "conjecture"
    56 
    57 fun string_for_term (ATerm (s, [])) = s
    58   | string_for_term (ATerm ("equal", ts)) =
    59     space_implode " = " (map string_for_term ts)
    60   | string_for_term (ATerm (s, ts)) =
    61     s ^ "(" ^ commas (map string_for_term ts) ^ ")"
    62 fun string_for_quantifier AForall = "!"
    63   | string_for_quantifier AExists = "?"
    64 fun string_for_connective ANot = "~"
    65   | string_for_connective AAnd = "&"
    66   | string_for_connective AOr = "|"
    67   | string_for_connective AImplies = "=>"
    68   | string_for_connective AIf = "<="
    69   | string_for_connective AIff = "<=>"
    70   | string_for_connective ANotIff = "<~>"
    71 fun string_for_formula (AQuant (q, xs, phi)) =
    72     "(" ^ string_for_quantifier q ^ "[" ^ commas xs ^ "] : " ^
    73     string_for_formula phi ^ ")"
    74   | string_for_formula (AConn (ANot, [AAtom (ATerm ("equal", ts))])) =
    75     space_implode " != " (map string_for_term ts)
    76   | string_for_formula (AConn (c, [phi])) =
    77     "(" ^ string_for_connective c ^ " " ^ string_for_formula phi ^ ")"
    78   | string_for_formula (AConn (c, phis)) =
    79     "(" ^ space_implode (" " ^ string_for_connective c ^ " ")
    80                         (map string_for_formula phis) ^ ")"
    81   | string_for_formula (AAtom tm) = string_for_term tm
    82 
    83 fun string_for_problem_line use_conjecture_for_hypotheses
    84                             (Fof (ident, kind, phi)) =
    85   let
    86     val (kind, phi) =
    87       if kind = Hypothesis andalso use_conjecture_for_hypotheses then
    88         (Conjecture, AConn (ANot, [phi]))
    89       else
    90         (kind, phi)
    91   in
    92     "fof(" ^ ident ^ ", " ^ string_for_kind kind ^ ",\n    (" ^
    93     string_for_formula phi ^ ")).\n"
    94   end
    95 fun tptp_strings_for_atp_problem use_conjecture_for_hypotheses problem =
    96   "% This file was generated by Isabelle (most likely Sledgehammer)\n\
    97   \% " ^ timestamp () ^ "\n" ::
    98   maps (fn (_, []) => []
    99          | (heading, lines) =>
   100            "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
   101            map (string_for_problem_line use_conjecture_for_hypotheses) lines)
   102        problem
   103 
   104 fun is_atp_variable s = Char.isUpper (String.sub (s, 0))
   105 
   106 
   107 (** Nice names **)
   108 
   109 fun empty_name_pool readable_names =
   110   if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
   111 
   112 fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
   113 fun pool_map f xs =
   114   pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
   115 
   116 (* "op" is also reserved, to avoid the unreadable "op_1", "op_2", etc., in the
   117    problem files. "equal" is reserved by some ATPs. "eq" is reserved to ensure
   118    that "HOL.eq" is correctly mapped to equality. *)
   119 val reserved_nice_names = ["op", "equal", "eq"]
   120 fun readable_name full_name s =
   121   if s = full_name then
   122     s
   123   else
   124     let
   125       val s = s |> Long_Name.base_name
   126                 |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
   127     in if member (op =) reserved_nice_names s then full_name else s end
   128 
   129 fun nice_name (full_name, _) NONE = (full_name, NONE)
   130   | nice_name (full_name, desired_name) (SOME the_pool) =
   131     if String.isPrefix "$" full_name then
   132       (full_name, SOME the_pool)
   133     else case Symtab.lookup (fst the_pool) full_name of
   134       SOME nice_name => (nice_name, SOME the_pool)
   135     | NONE =>
   136       let
   137         val nice_prefix = readable_name full_name desired_name
   138         fun add j =
   139           let
   140             val nice_name = nice_prefix ^
   141                             (if j = 0 then "" else "_" ^ string_of_int j)
   142           in
   143             case Symtab.lookup (snd the_pool) nice_name of
   144               SOME full_name' =>
   145               if full_name = full_name' then (nice_name, the_pool)
   146               else add (j + 1)
   147             | NONE =>
   148               (nice_name,
   149                (Symtab.update_new (full_name, nice_name) (fst the_pool),
   150                 Symtab.update_new (nice_name, full_name) (snd the_pool)))
   151           end
   152       in add 0 |> apsnd SOME end
   153 
   154 
   155 fun nice_term (ATerm (name, ts)) =
   156   nice_name name ##>> pool_map nice_term ts #>> ATerm
   157 fun nice_formula (AQuant (q, xs, phi)) =
   158     pool_map nice_name xs ##>> nice_formula phi
   159     #>> (fn (xs, phi) => AQuant (q, xs, phi))
   160   | nice_formula (AConn (c, phis)) =
   161     pool_map nice_formula phis #>> curry AConn c
   162   | nice_formula (AAtom tm) = nice_term tm #>> AAtom
   163 fun nice_problem_line (Fof (ident, kind, phi)) =
   164   nice_formula phi #>> (fn phi => Fof (ident, kind, phi))
   165 fun nice_problem problem =
   166   pool_map (fn (heading, lines) =>
   167                pool_map nice_problem_line lines #>> pair heading) problem
   168 fun nice_atp_problem readable_names problem =
   169   nice_problem problem (empty_name_pool readable_names)
   170 
   171 end;