src/HOL/Tools/ATP/atp_problem.ML
 author haftmann Wed Dec 08 13:34:50 2010 +0100 (2010-12-08) changeset 41075 4bed56dc95fb parent 39453 1740a2d6bef9 child 41491 a2ad5b824051 permissions -rw-r--r--
primitive definitions of bot/top/inf/sup for bool and fun are named with canonical suffix `_def` rather than `_eq`
1 (*  Title:      HOL/Tools/ATP/atp_problem.ML
2     Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
3     Author:     Jasmin Blanchette, TU Muenchen
5 Abstract representation of ATP problems and TPTP syntax.
6 *)
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
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
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;
33 structure ATP_Problem : ATP_PROBLEM =
34 struct
36 (** ATP problem **)
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
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
51 val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
53 fun string_for_kind Axiom = "axiom"
54   | string_for_kind Hypothesis = "hypothesis"
55   | string_for_kind Conjecture = "conjecture"
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
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 (_, []) => []
100            "\n% " ^ heading ^ " (" ^ Int.toString (length lines) ^ ")\n" ::
101            map (string_for_problem_line use_conjecture_for_hypotheses) lines)
102        problem
104 fun is_atp_variable s = Char.isUpper (String.sub (s, 0))
107 (** Nice names **)
110   if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
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 []
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
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
139           let
140             val nice_name = nice_prefix ^
141                             (if j = 0 then "" else "_" ^ Int.toString 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
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 =