src/HOL/Tools/SMT/smtlib_interface.ML
author boehmes
Sun Dec 19 18:54:29 2010 +0100 (2010-12-19)
changeset 41281 679118e35378
parent 41280 a7de9d36f4f2
child 41328 6792a5c92a58
permissions -rw-r--r--
removed odd decoration of built-in symbols as Vars (instead provide built-in desctructor functions along with their inverse functions);
removed odd retyping during folify (instead, keep all terms well-typed)
boehmes@36898
     1
(*  Title:      HOL/Tools/SMT/smtlib_interface.ML
boehmes@36898
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36898
     3
boehmes@36898
     4
Interface to SMT solvers based on the SMT-LIB format.
boehmes@36898
     5
*)
boehmes@36898
     6
boehmes@36898
     7
signature SMTLIB_INTERFACE =
boehmes@36898
     8
sig
boehmes@41124
     9
  val smtlibC: SMT_Utils.class
boehmes@41059
    10
  val add_logic: int * (term list -> string option) -> Context.generic ->
boehmes@36899
    11
    Context.generic
boehmes@41127
    12
  val translate_config: Proof.context -> SMT_Translate.config
boehmes@41059
    13
  val setup: theory -> theory
boehmes@36898
    14
end
boehmes@36898
    15
boehmes@36898
    16
structure SMTLIB_Interface: SMTLIB_INTERFACE =
boehmes@36898
    17
struct
boehmes@36898
    18
boehmes@41280
    19
structure U = SMT_Utils
boehmes@41059
    20
structure B = SMT_Builtin
boehmes@36898
    21
structure N = SMT_Normalize
boehmes@36898
    22
structure T = SMT_Translate
boehmes@36898
    23
boehmes@41059
    24
val smtlibC = ["smtlib"]
boehmes@36898
    25
boehmes@36898
    26
boehmes@41059
    27
(* builtins *)
boehmes@36898
    28
boehmes@41059
    29
local
boehmes@41059
    30
  fun int_num _ i = SOME (string_of_int i)
boehmes@36899
    31
boehmes@41280
    32
  fun is_linear [t] = U.is_number t
boehmes@41280
    33
    | is_linear [t, u] = U.is_number t orelse U.is_number u
boehmes@41280
    34
    | is_linear _ = false
boehmes@41280
    35
boehmes@41281
    36
  fun times _ _ ts =
boehmes@41281
    37
    let val mk = Term.list_comb o pair @{const times (int)}
boehmes@41281
    38
    in if is_linear ts then SOME ("*", 2, ts, mk) else NONE end
boehmes@41280
    39
boehmes@41281
    40
  fun distinct _ T [t] =
boehmes@41059
    41
        (case try HOLogic.dest_list t of
boehmes@41127
    42
          SOME (ts as _ :: _) =>
boehmes@41127
    43
            let
boehmes@41281
    44
              val c = Const (@{const_name distinct}, T)
boehmes@41281
    45
              fun mk us = c $ HOLogic.mk_list T us
boehmes@41281
    46
            in SOME ("distinct", length ts, ts, mk) end
boehmes@41059
    47
        | _ => NONE)
boehmes@41059
    48
    | distinct _ _ _ = NONE
boehmes@41059
    49
in
boehmes@36898
    50
boehmes@41072
    51
val setup_builtins =
boehmes@41059
    52
  B.add_builtin_typ smtlibC (@{typ int}, K (SOME "Int"), int_num) #>
boehmes@41059
    53
  fold (B.add_builtin_fun' smtlibC) [
boehmes@41059
    54
    (@{const True}, "true"),
boehmes@41059
    55
    (@{const False}, "false"),
boehmes@41059
    56
    (@{const Not}, "not"),
boehmes@41059
    57
    (@{const HOL.conj}, "and"),
boehmes@41059
    58
    (@{const HOL.disj}, "or"),
boehmes@41059
    59
    (@{const HOL.implies}, "implies"),
boehmes@41059
    60
    (@{const HOL.eq (bool)}, "iff"),
boehmes@41059
    61
    (@{const HOL.eq ('a)}, "="),
boehmes@41059
    62
    (@{const If (bool)}, "if_then_else"),
boehmes@41059
    63
    (@{const If ('a)}, "ite"),
boehmes@41059
    64
    (@{const less (int)}, "<"),
boehmes@41059
    65
    (@{const less_eq (int)}, "<="),
boehmes@41059
    66
    (@{const uminus (int)}, "~"),
boehmes@41059
    67
    (@{const plus (int)}, "+"),
boehmes@41280
    68
    (@{const minus (int)}, "-") ] #>
boehmes@41280
    69
  B.add_builtin_fun smtlibC (Term.dest_Const @{const times (int)}, times) #>
boehmes@41059
    70
  B.add_builtin_fun smtlibC (Term.dest_Const @{const distinct ('a)}, distinct)
boehmes@36898
    71
boehmes@41059
    72
end
boehmes@36898
    73
boehmes@36898
    74
boehmes@41059
    75
(* serialization *)
boehmes@36898
    76
boehmes@41059
    77
(** header **)
boehmes@41059
    78
boehmes@41059
    79
fun fst_int_ord ((i1, _), (i2, _)) = int_ord (i1, i2)
boehmes@36899
    80
boehmes@36899
    81
structure Logics = Generic_Data
boehmes@36899
    82
(
boehmes@36899
    83
  type T = (int * (term list -> string option)) list
boehmes@36899
    84
  val empty = []
boehmes@36899
    85
  val extend = I
wenzelm@39687
    86
  fun merge (bs1, bs2) = Ord_List.union fst_int_ord bs2 bs1
boehmes@36899
    87
)
boehmes@36899
    88
boehmes@41059
    89
fun add_logic pf = Logics.map (Ord_List.insert fst_int_ord pf)
boehmes@36899
    90
boehmes@36899
    91
fun choose_logic ctxt ts =
boehmes@36899
    92
  let
boehmes@36899
    93
    fun choose [] = "AUFLIA"
boehmes@41059
    94
      | choose ((_, f) :: fs) = (case f ts of SOME s => s | NONE => choose fs)
boehmes@41059
    95
  in [":logic " ^ choose (Logics.get (Context.Proof ctxt))] end
boehmes@36899
    96
boehmes@36899
    97
boehmes@41072
    98
(** serialization **)
boehmes@36899
    99
boehmes@36898
   100
val add = Buffer.add
boehmes@36898
   101
fun sep f = add " " #> f
boehmes@36898
   102
fun enclose l r f = sep (add l #> f #> add r)
boehmes@36898
   103
val par = enclose "(" ")"
boehmes@36898
   104
fun app n f = (fn [] => sep (add n) | xs => par (add n #> fold f xs))
boehmes@36898
   105
fun line f = f #> add "\n"
boehmes@36898
   106
boehmes@36898
   107
fun var i = add "?v" #> add (string_of_int i)
boehmes@36898
   108
boehmes@36898
   109
fun sterm l (T.SVar i) = sep (var (l - i - 1))
boehmes@36898
   110
  | sterm l (T.SApp (n, ts)) = app n (sterm l) ts
boehmes@36898
   111
  | sterm _ (T.SLet _) = raise Fail "SMT-LIB: unsupported let expression"
boehmes@40664
   112
  | sterm l (T.SQua (q, ss, ps, w, t)) =
boehmes@36898
   113
      let
boehmes@36898
   114
        val quant = add o (fn T.SForall => "forall" | T.SExists => "exists")
boehmes@36898
   115
        val vs = map_index (apfst (Integer.add l)) ss
boehmes@36898
   116
        fun var_decl (i, s) = par (var i #> sep (add s))
boehmes@36898
   117
        val sub = sterm (l + length ss)
boehmes@36898
   118
        fun pat kind ts = sep (add kind #> enclose "{" " }" (fold sub ts))
boehmes@36898
   119
        fun pats (T.SPat ts) = pat ":pat" ts
boehmes@36898
   120
          | pats (T.SNoPat ts) = pat ":nopat" ts
boehmes@40664
   121
        fun weight NONE = I
boehmes@40664
   122
          | weight (SOME i) =
boehmes@40664
   123
              sep (add ":weight { " #> add (string_of_int i) #> add " }")
boehmes@40664
   124
      in
boehmes@40664
   125
        par (quant q #> fold var_decl vs #> sub t #> fold pats ps #> weight w)
boehmes@40664
   126
      end
boehmes@36898
   127
boehmes@37155
   128
fun ssort sorts = sort fast_string_ord sorts
boehmes@37155
   129
fun fsort funcs = sort (prod_ord fast_string_ord (K EQUAL)) funcs
boehmes@37155
   130
boehmes@39298
   131
fun sdatatypes decls =
boehmes@39298
   132
  let
boehmes@39298
   133
    fun con (n, []) = add n
boehmes@39298
   134
      | con (n, sels) = par (add n #>
boehmes@39298
   135
          fold (fn (n, s) => sep (par (add n #> sep (add s)))) sels)
boehmes@39298
   136
    fun dtyp (n, decl) = add n #> fold (sep o con) decl
boehmes@39298
   137
  in line (add ":datatypes " #> par (fold (par o dtyp) decls)) end
boehmes@39298
   138
boehmes@39298
   139
fun serialize comments {header, sorts, dtyps, funcs} ts =
boehmes@36898
   140
  Buffer.empty
boehmes@36898
   141
  |> line (add "(benchmark Isabelle")
boehmes@36898
   142
  |> line (add ":status unknown")
boehmes@36899
   143
  |> fold (line o add) header
boehmes@36898
   144
  |> length sorts > 0 ?
boehmes@37155
   145
       line (add ":extrasorts" #> par (fold (sep o add) (ssort sorts)))
boehmes@39298
   146
  |> fold sdatatypes dtyps
boehmes@36898
   147
  |> length funcs > 0 ? (
boehmes@36898
   148
       line (add ":extrafuns" #> add " (") #>
boehmes@36898
   149
       fold (fn (f, (ss, s)) =>
boehmes@37155
   150
         line (sep (app f (sep o add) (ss @ [s])))) (fsort funcs) #>
boehmes@36898
   151
       line (add ")"))
boehmes@36898
   152
  |> fold (fn t => line (add ":assumption" #> sterm 0 t)) ts
boehmes@36898
   153
  |> line (add ":formula true)")
boehmes@36898
   154
  |> fold (fn str => line (add "; " #> add str)) comments
boehmes@36898
   155
  |> Buffer.content
boehmes@36898
   156
boehmes@36898
   157
boehmes@41072
   158
(* interface *)
boehmes@36898
   159
boehmes@41127
   160
fun translate_config ctxt = {
boehmes@41127
   161
  prefixes = {
boehmes@41127
   162
    sort_prefix = "S",
boehmes@41127
   163
    func_prefix = "f"},
boehmes@41127
   164
  header = choose_logic ctxt,
boehmes@41127
   165
  is_fol = true,
boehmes@41127
   166
  has_datatypes = false,
boehmes@41127
   167
  serialize = serialize}
boehmes@36898
   168
boehmes@41127
   169
val setup = Context.theory_map (
boehmes@41127
   170
  setup_builtins #>
boehmes@41127
   171
  T.add_config (smtlibC, translate_config))
boehmes@41072
   172
boehmes@36898
   173
end