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