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