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