src/HOL/Tools/ATP/atp_problem.ML
author blanchet
Sun, 01 May 2011 18:37:24 +0200
changeset 43396 7a506b0b644f
parent 43320 494e4ac5b0f8
child 43397 46d485f8d144
permissions -rw-r--r--
distinguish FOF and TFF (typed first-order) in ATP abstract syntax tree
blanchet@38293
     1
(*  Title:      HOL/Tools/ATP/atp_problem.ML
blanchet@38261
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory and NICTA
blanchet@37509
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@37509
     4
blanchet@39692
     5
Abstract representation of ATP problems and TPTP syntax.
blanchet@37509
     6
*)
blanchet@37509
     7
blanchet@38253
     8
signature ATP_PROBLEM =
blanchet@37509
     9
sig
blanchet@38226
    10
  datatype 'a fo_term = ATerm of 'a * 'a fo_term list
blanchet@38226
    11
  datatype quantifier = AForall | AExists
blanchet@38226
    12
  datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
blanchet@38228
    13
  datatype ('a, 'b) formula =
blanchet@38228
    14
    AQuant of quantifier * 'a list * ('a, 'b) formula |
blanchet@38228
    15
    AConn of connective * ('a, 'b) formula list |
blanchet@38280
    16
    AAtom of 'b
blanchet@39693
    17
  type 'a uniform_formula = ('a, 'a fo_term) formula
blanchet@38228
    18
blanchet@43396
    19
  datatype logic = Fof | Tff
blanchet@43396
    20
  datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
blanchet@43396
    21
  type 'a problem_line =
blanchet@43396
    22
    logic * string * formula_kind * ('a, 'a fo_term) formula
blanchet@43396
    23
    * string fo_term option
blanchet@38251
    24
  type 'a problem = (string * 'a problem_line list) list
blanchet@38226
    25
blanchet@38253
    26
  val timestamp : unit -> string
blanchet@39692
    27
  val is_atp_variable : string -> bool
blanchet@39692
    28
  val tptp_strings_for_atp_problem :
blanchet@38854
    29
    bool -> (string * string problem_line list) list -> string list
blanchet@39692
    30
  val nice_atp_problem :
blanchet@38251
    31
    bool -> ('a * (string * string) problem_line list) list
blanchet@38251
    32
    -> ('a * string problem_line list) list
blanchet@38251
    33
       * (string Symtab.table * string Symtab.table) option
blanchet@37509
    34
end;
blanchet@37509
    35
blanchet@38253
    36
structure ATP_Problem : ATP_PROBLEM =
blanchet@37509
    37
struct
blanchet@37509
    38
blanchet@37643
    39
(** ATP problem **)
blanchet@37643
    40
blanchet@38198
    41
datatype 'a fo_term = ATerm of 'a * 'a fo_term list
blanchet@38198
    42
datatype quantifier = AForall | AExists
blanchet@38226
    43
datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
blanchet@38228
    44
datatype ('a, 'b) formula =
blanchet@38228
    45
  AQuant of quantifier * 'a list * ('a, 'b) formula |
blanchet@38228
    46
  AConn of connective * ('a, 'b) formula list |
blanchet@38280
    47
  AAtom of 'b
blanchet@39693
    48
type 'a uniform_formula = ('a, 'a fo_term) formula
blanchet@38198
    49
blanchet@43396
    50
datatype logic = Fof | Tff
blanchet@43396
    51
datatype formula_kind = Axiom | Definition | Lemma | Hypothesis | Conjecture
blanchet@43396
    52
type 'a problem_line =
blanchet@43396
    53
  logic * string * formula_kind * ('a, 'a fo_term) formula
blanchet@43396
    54
  * string fo_term option
blanchet@37643
    55
type 'a problem = (string * 'a problem_line list) list
blanchet@37643
    56
blanchet@38253
    57
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@38253
    58
blanchet@38854
    59
fun string_for_kind Axiom = "axiom"
blanchet@42640
    60
  | string_for_kind Definition = "definition"
blanchet@42640
    61
  | string_for_kind Lemma = "lemma"
blanchet@38854
    62
  | string_for_kind Hypothesis = "hypothesis"
blanchet@38854
    63
  | string_for_kind Conjecture = "conjecture"
blanchet@38854
    64
blanchet@38198
    65
fun string_for_term (ATerm (s, [])) = s
blanchet@38334
    66
  | string_for_term (ATerm ("equal", ts)) =
blanchet@38334
    67
    space_implode " = " (map string_for_term ts)
blanchet@42640
    68
  | string_for_term (ATerm ("[]", ts)) =
blanchet@42640
    69
    (* used for lists in the optional "source" field of a derivation *)
blanchet@42640
    70
    "[" ^ commas (map string_for_term ts) ^ "]"
blanchet@38198
    71
  | string_for_term (ATerm (s, ts)) =
blanchet@38334
    72
    s ^ "(" ^ commas (map string_for_term ts) ^ ")"
blanchet@38198
    73
fun string_for_quantifier AForall = "!"
blanchet@38198
    74
  | string_for_quantifier AExists = "?"
blanchet@38198
    75
fun string_for_connective ANot = "~"
blanchet@38198
    76
  | string_for_connective AAnd = "&"
blanchet@38198
    77
  | string_for_connective AOr = "|"
blanchet@38198
    78
  | string_for_connective AImplies = "=>"
blanchet@38226
    79
  | string_for_connective AIf = "<="
blanchet@38198
    80
  | string_for_connective AIff = "<=>"
blanchet@38226
    81
  | string_for_connective ANotIff = "<~>"
blanchet@38198
    82
fun string_for_formula (AQuant (q, xs, phi)) =
blanchet@38714
    83
    "(" ^ string_for_quantifier q ^ "[" ^ commas xs ^ "] : " ^
blanchet@38714
    84
    string_for_formula phi ^ ")"
blanchet@38280
    85
  | string_for_formula (AConn (ANot, [AAtom (ATerm ("equal", ts))])) =
blanchet@38239
    86
    space_implode " != " (map string_for_term ts)
blanchet@38198
    87
  | string_for_formula (AConn (c, [phi])) =
blanchet@38714
    88
    "(" ^ string_for_connective c ^ " " ^ string_for_formula phi ^ ")"
blanchet@38198
    89
  | string_for_formula (AConn (c, phis)) =
blanchet@38198
    90
    "(" ^ space_implode (" " ^ string_for_connective c ^ " ")
blanchet@38198
    91
                        (map string_for_formula phis) ^ ")"
blanchet@38280
    92
  | string_for_formula (AAtom tm) = string_for_term tm
blanchet@38198
    93
blanchet@38854
    94
fun string_for_problem_line use_conjecture_for_hypotheses
blanchet@43396
    95
                            (logic, ident, kind, phi, source) =
blanchet@38854
    96
  let
blanchet@38854
    97
    val (kind, phi) =
blanchet@38854
    98
      if kind = Hypothesis andalso use_conjecture_for_hypotheses then
blanchet@38854
    99
        (Conjecture, AConn (ANot, [phi]))
blanchet@38854
   100
      else
blanchet@38854
   101
        (kind, phi)
blanchet@38854
   102
  in
blanchet@43396
   103
    (case logic of Fof => "fof" | Tff => "tff") ^
blanchet@43396
   104
    "(" ^ ident ^ ", " ^ string_for_kind kind ^ ",\n    (" ^
blanchet@42640
   105
    string_for_formula phi ^ ")" ^
blanchet@42640
   106
    (case source of
blanchet@42640
   107
       SOME tm => ", " ^ string_for_term tm
blanchet@42640
   108
     | NONE => "") ^ ").\n"
blanchet@38854
   109
  end
blanchet@39692
   110
fun tptp_strings_for_atp_problem use_conjecture_for_hypotheses problem =
blanchet@37643
   111
  "% This file was generated by Isabelle (most likely Sledgehammer)\n\
blanchet@37643
   112
  \% " ^ timestamp () ^ "\n" ::
blanchet@37643
   113
  maps (fn (_, []) => []
blanchet@37643
   114
         | (heading, lines) =>
wenzelm@41739
   115
           "\n% " ^ heading ^ " (" ^ string_of_int (length lines) ^ ")\n" ::
blanchet@38854
   116
           map (string_for_problem_line use_conjecture_for_hypotheses) lines)
blanchet@38854
   117
       problem
blanchet@37643
   118
blanchet@39692
   119
fun is_atp_variable s = Char.isUpper (String.sub (s, 0))
blanchet@38251
   120
blanchet@37643
   121
blanchet@37643
   122
(** Nice names **)
blanchet@37643
   123
blanchet@37624
   124
fun empty_name_pool readable_names =
blanchet@37643
   125
  if readable_names then SOME (Symtab.empty, Symtab.empty) else NONE
blanchet@37624
   126
blanchet@37624
   127
fun pool_fold f xs z = pair z #> fold_rev (fn x => uncurry (f x)) xs
blanchet@37624
   128
fun pool_map f xs =
blanchet@37624
   129
  pool_fold (fn x => fn ys => fn pool => f x pool |>> (fn y => y :: ys)) xs []
blanchet@37624
   130
blanchet@43088
   131
val no_qualifiers =
blanchet@43088
   132
  let
blanchet@43088
   133
    fun skip [] = []
blanchet@43088
   134
      | skip (#"." :: cs) = skip cs
blanchet@43088
   135
      | skip (c :: cs) = if Char.isAlphaNum c then skip cs else c :: keep cs
blanchet@43088
   136
    and keep [] = []
blanchet@43088
   137
      | keep (#"." :: cs) = skip cs
blanchet@43088
   138
      | keep (c :: cs) = c :: keep cs
blanchet@43088
   139
  in String.explode #> rev #> keep #> rev #> String.implode end
blanchet@43088
   140
blanchet@39355
   141
(* "op" is also reserved, to avoid the unreadable "op_1", "op_2", etc., in the
blanchet@39355
   142
   problem files. "equal" is reserved by some ATPs. "eq" is reserved to ensure
blanchet@39355
   143
   that "HOL.eq" is correctly mapped to equality. *)
blanchet@39355
   144
val reserved_nice_names = ["op", "equal", "eq"]
blanchet@37624
   145
fun readable_name full_name s =
blanchet@37643
   146
  if s = full_name then
blanchet@37643
   147
    s
blanchet@37643
   148
  else
blanchet@37643
   149
    let
blanchet@43088
   150
      val s = s |> no_qualifiers
blanchet@37643
   151
                |> Name.desymbolize (Char.isUpper (String.sub (full_name, 0)))
blanchet@37643
   152
    in if member (op =) reserved_nice_names s then full_name else s end
blanchet@37624
   153
blanchet@37624
   154
fun nice_name (full_name, _) NONE = (full_name, NONE)
blanchet@37624
   155
  | nice_name (full_name, desired_name) (SOME the_pool) =
blanchet@39630
   156
    if String.isPrefix "$" full_name then
blanchet@39630
   157
      (full_name, SOME the_pool)
blanchet@39630
   158
    else case Symtab.lookup (fst the_pool) full_name of
blanchet@37624
   159
      SOME nice_name => (nice_name, SOME the_pool)
blanchet@37624
   160
    | NONE =>
blanchet@37624
   161
      let
blanchet@37624
   162
        val nice_prefix = readable_name full_name desired_name
blanchet@37624
   163
        fun add j =
blanchet@37624
   164
          let
blanchet@37624
   165
            val nice_name = nice_prefix ^
wenzelm@41739
   166
                            (if j = 0 then "" else "_" ^ string_of_int j)
blanchet@37624
   167
          in
blanchet@37624
   168
            case Symtab.lookup (snd the_pool) nice_name of
blanchet@37624
   169
              SOME full_name' =>
blanchet@37624
   170
              if full_name = full_name' then (nice_name, the_pool)
blanchet@37624
   171
              else add (j + 1)
blanchet@37624
   172
            | NONE =>
blanchet@37624
   173
              (nice_name,
blanchet@37624
   174
               (Symtab.update_new (full_name, nice_name) (fst the_pool),
blanchet@37624
   175
                Symtab.update_new (nice_name, full_name) (snd the_pool)))
blanchet@37624
   176
          end
blanchet@37624
   177
      in add 0 |> apsnd SOME end
blanchet@37624
   178
blanchet@38198
   179
fun nice_term (ATerm (name, ts)) =
blanchet@38198
   180
  nice_name name ##>> pool_map nice_term ts #>> ATerm
blanchet@38198
   181
fun nice_formula (AQuant (q, xs, phi)) =
blanchet@38198
   182
    pool_map nice_name xs ##>> nice_formula phi
blanchet@38198
   183
    #>> (fn (xs, phi) => AQuant (q, xs, phi))
blanchet@38198
   184
  | nice_formula (AConn (c, phis)) =
blanchet@38198
   185
    pool_map nice_formula phis #>> curry AConn c
blanchet@38280
   186
  | nice_formula (AAtom tm) = nice_term tm #>> AAtom
blanchet@43396
   187
fun nice_problem_line (logic, ident, kind, phi, source) =
blanchet@43396
   188
  nice_formula phi #>> (fn phi => (logic, ident, kind, phi, source))
blanchet@38171
   189
fun nice_problem problem =
blanchet@37643
   190
  pool_map (fn (heading, lines) =>
blanchet@38171
   191
               pool_map nice_problem_line lines #>> pair heading) problem
blanchet@39692
   192
fun nice_atp_problem readable_names problem =
blanchet@38251
   193
  nice_problem problem (empty_name_pool readable_names)
blanchet@37509
   194
blanchet@37509
   195
end;