src/HOL/Tools/ATP/atp_problem.ML
author blanchet
Thu, 16 Sep 2010 11:59:45 +0200
changeset 39693 1740a2d6bef9
parent 39692 70a57e40f795
child 41739 a2ad5b824051
permissions -rw-r--r--
use the same TSTP/Vampire/SPASS parser for one-liners as for Isar proofs
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@38836
    19
  datatype kind = Axiom | Hypothesis | Conjecture
blanchet@38251
    20
  datatype 'a problem_line = Fof of string * kind * ('a, 'a fo_term) formula
blanchet@38251
    21
  type 'a problem = (string * 'a problem_line list) list
blanchet@38226
    22
blanchet@38253
    23
  val timestamp : unit -> string
blanchet@39692
    24
  val is_atp_variable : string -> bool
blanchet@39692
    25
  val tptp_strings_for_atp_problem :
blanchet@38854
    26
    bool -> (string * string problem_line list) list -> string list
blanchet@39692
    27
  val nice_atp_problem :
blanchet@38251
    28
    bool -> ('a * (string * string) problem_line list) list
blanchet@38251
    29
    -> ('a * string problem_line list) list
blanchet@38251
    30
       * (string Symtab.table * string Symtab.table) option
blanchet@37509
    31
end;
blanchet@37509
    32
blanchet@38253
    33
structure ATP_Problem : ATP_PROBLEM =
blanchet@37509
    34
struct
blanchet@37509
    35
blanchet@37643
    36
(** ATP problem **)
blanchet@37643
    37
blanchet@38198
    38
datatype 'a fo_term = ATerm of 'a * 'a fo_term list
blanchet@38198
    39
datatype quantifier = AForall | AExists
blanchet@38226
    40
datatype connective = ANot | AAnd | AOr | AImplies | AIf | AIff | ANotIff
blanchet@38228
    41
datatype ('a, 'b) formula =
blanchet@38228
    42
  AQuant of quantifier * 'a list * ('a, 'b) formula |
blanchet@38228
    43
  AConn of connective * ('a, 'b) formula list |
blanchet@38280
    44
  AAtom of 'b
blanchet@39693
    45
type 'a uniform_formula = ('a, 'a fo_term) formula
blanchet@38198
    46
blanchet@38836
    47
datatype kind = Axiom | Hypothesis | Conjecture
blanchet@38228
    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@38253
    51
val timestamp = Date.fmt "%Y-%m-%d %H:%M:%S" o Date.fromTimeLocal o Time.now
blanchet@38253
    52
blanchet@38854
    53
fun string_for_kind Axiom = "axiom"
blanchet@38854
    54
  | string_for_kind Hypothesis = "hypothesis"
blanchet@38854
    55
  | string_for_kind Conjecture = "conjecture"
blanchet@38854
    56
blanchet@38198
    57
fun string_for_term (ATerm (s, [])) = s
blanchet@38334
    58
  | string_for_term (ATerm ("equal", ts)) =
blanchet@38334
    59
    space_implode " = " (map string_for_term ts)
blanchet@38198
    60
  | string_for_term (ATerm (s, ts)) =
blanchet@38334
    61
    s ^ "(" ^ commas (map string_for_term ts) ^ ")"
blanchet@38198
    62
fun string_for_quantifier AForall = "!"
blanchet@38198
    63
  | string_for_quantifier AExists = "?"
blanchet@38198
    64
fun string_for_connective ANot = "~"
blanchet@38198
    65
  | string_for_connective AAnd = "&"
blanchet@38198
    66
  | string_for_connective AOr = "|"
blanchet@38198
    67
  | string_for_connective AImplies = "=>"
blanchet@38226
    68
  | string_for_connective AIf = "<="
blanchet@38198
    69
  | string_for_connective AIff = "<=>"
blanchet@38226
    70
  | string_for_connective ANotIff = "<~>"
blanchet@38198
    71
fun string_for_formula (AQuant (q, xs, phi)) =
blanchet@38714
    72
    "(" ^ string_for_quantifier q ^ "[" ^ commas xs ^ "] : " ^
blanchet@38714
    73
    string_for_formula phi ^ ")"
blanchet@38280
    74
  | string_for_formula (AConn (ANot, [AAtom (ATerm ("equal", ts))])) =
blanchet@38239
    75
    space_implode " != " (map string_for_term ts)
blanchet@38198
    76
  | string_for_formula (AConn (c, [phi])) =
blanchet@38714
    77
    "(" ^ string_for_connective c ^ " " ^ string_for_formula phi ^ ")"
blanchet@38198
    78
  | string_for_formula (AConn (c, phis)) =
blanchet@38198
    79
    "(" ^ space_implode (" " ^ string_for_connective c ^ " ")
blanchet@38198
    80
                        (map string_for_formula phis) ^ ")"
blanchet@38280
    81
  | string_for_formula (AAtom tm) = string_for_term tm
blanchet@38198
    82
blanchet@38854
    83
fun string_for_problem_line use_conjecture_for_hypotheses
blanchet@38854
    84
                            (Fof (ident, kind, phi)) =
blanchet@38854
    85
  let
blanchet@38854
    86
    val (kind, phi) =
blanchet@38854
    87
      if kind = Hypothesis andalso use_conjecture_for_hypotheses then
blanchet@38854
    88
        (Conjecture, AConn (ANot, [phi]))
blanchet@38854
    89
      else
blanchet@38854
    90
        (kind, phi)
blanchet@38854
    91
  in
blanchet@38854
    92
    "fof(" ^ ident ^ ", " ^ string_for_kind kind ^ ",\n    (" ^
blanchet@38854
    93
    string_for_formula phi ^ ")).\n"
blanchet@38854
    94
  end
blanchet@39692
    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@38854
   101
           map (string_for_problem_line use_conjecture_for_hypotheses) lines)
blanchet@38854
   102
       problem
blanchet@37643
   103
blanchet@39692
   104
fun is_atp_variable s = Char.isUpper (String.sub (s, 0))
blanchet@38251
   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@39355
   116
(* "op" is also reserved, to avoid the unreadable "op_1", "op_2", etc., in the
blanchet@39355
   117
   problem files. "equal" is reserved by some ATPs. "eq" is reserved to ensure
blanchet@39355
   118
   that "HOL.eq" is correctly mapped to equality. *)
blanchet@39355
   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@39630
   131
    if String.isPrefix "$" full_name then
blanchet@39630
   132
      (full_name, SOME the_pool)
blanchet@39630
   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@38198
   154
blanchet@38198
   155
fun nice_term (ATerm (name, ts)) =
blanchet@38198
   156
  nice_name name ##>> pool_map nice_term ts #>> ATerm
blanchet@38198
   157
fun nice_formula (AQuant (q, xs, phi)) =
blanchet@38198
   158
    pool_map nice_name xs ##>> nice_formula phi
blanchet@38198
   159
    #>> (fn (xs, phi) => AQuant (q, xs, phi))
blanchet@38198
   160
  | nice_formula (AConn (c, phis)) =
blanchet@38198
   161
    pool_map nice_formula phis #>> curry AConn c
blanchet@38280
   162
  | nice_formula (AAtom tm) = nice_term tm #>> AAtom
blanchet@38198
   163
fun nice_problem_line (Fof (ident, kind, phi)) =
blanchet@38198
   164
  nice_formula phi #>> (fn phi => Fof (ident, kind, phi))
blanchet@38171
   165
fun nice_problem problem =
blanchet@37643
   166
  pool_map (fn (heading, lines) =>
blanchet@38171
   167
               pool_map nice_problem_line lines #>> pair heading) problem
blanchet@39692
   168
fun nice_atp_problem readable_names problem =
blanchet@38251
   169
  nice_problem problem (empty_name_pool readable_names)
blanchet@37509
   170
blanchet@37509
   171
end;