src/HOL/Tools/SMT/smtlib_interface.ML
author boehmes
Tue Mar 27 17:11:02 2012 +0200 (2012-03-27)
changeset 47155 ade3fc826af3
parent 41473 3717fc42ebe9
permissions -rw-r--r--
dropped support for List.distinct in binding to SMT solvers: only few applications benefited from this support, and in some cases the smt method fails due to its support for List.distinct
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@36898
    19
boehmes@41059
    20
val smtlibC = ["smtlib"]
boehmes@36898
    21
boehmes@36898
    22
boehmes@41059
    23
(* builtins *)
boehmes@36898
    24
boehmes@41059
    25
local
boehmes@41059
    26
  fun int_num _ i = SOME (string_of_int i)
boehmes@36899
    27
boehmes@41328
    28
  fun is_linear [t] = SMT_Utils.is_number t
boehmes@41328
    29
    | is_linear [t, u] = SMT_Utils.is_number t orelse SMT_Utils.is_number u
boehmes@41280
    30
    | is_linear _ = false
boehmes@41280
    31
boehmes@41281
    32
  fun times _ _ ts =
boehmes@41281
    33
    let val mk = Term.list_comb o pair @{const times (int)}
boehmes@41281
    34
    in if is_linear ts then SOME ("*", 2, ts, mk) else NONE end
boehmes@41059
    35
in
boehmes@36898
    36
boehmes@41072
    37
val setup_builtins =
boehmes@41328
    38
  SMT_Builtin.add_builtin_typ smtlibC (@{typ int}, K (SOME "Int"), int_num) #>
boehmes@41328
    39
  fold (SMT_Builtin.add_builtin_fun' smtlibC) [
boehmes@41059
    40
    (@{const True}, "true"),
boehmes@41059
    41
    (@{const False}, "false"),
boehmes@41059
    42
    (@{const Not}, "not"),
boehmes@41059
    43
    (@{const HOL.conj}, "and"),
boehmes@41059
    44
    (@{const HOL.disj}, "or"),
boehmes@41059
    45
    (@{const HOL.implies}, "implies"),
boehmes@41059
    46
    (@{const HOL.eq (bool)}, "iff"),
boehmes@41059
    47
    (@{const HOL.eq ('a)}, "="),
boehmes@41059
    48
    (@{const If (bool)}, "if_then_else"),
boehmes@41059
    49
    (@{const If ('a)}, "ite"),
boehmes@41059
    50
    (@{const less (int)}, "<"),
boehmes@41059
    51
    (@{const less_eq (int)}, "<="),
boehmes@41059
    52
    (@{const uminus (int)}, "~"),
boehmes@41059
    53
    (@{const plus (int)}, "+"),
boehmes@41280
    54
    (@{const minus (int)}, "-") ] #>
boehmes@41328
    55
  SMT_Builtin.add_builtin_fun smtlibC
boehmes@47155
    56
    (Term.dest_Const @{const times (int)}, times)
boehmes@36898
    57
boehmes@41059
    58
end
boehmes@36898
    59
boehmes@36898
    60
boehmes@41059
    61
(* serialization *)
boehmes@36898
    62
boehmes@41059
    63
(** header **)
boehmes@41059
    64
boehmes@41059
    65
fun fst_int_ord ((i1, _), (i2, _)) = int_ord (i1, i2)
boehmes@36899
    66
boehmes@36899
    67
structure Logics = Generic_Data
boehmes@36899
    68
(
boehmes@36899
    69
  type T = (int * (term list -> string option)) list
boehmes@36899
    70
  val empty = []
boehmes@36899
    71
  val extend = I
wenzelm@41473
    72
  fun merge data = Ord_List.merge fst_int_ord data
boehmes@36899
    73
)
boehmes@36899
    74
boehmes@41059
    75
fun add_logic pf = Logics.map (Ord_List.insert fst_int_ord pf)
boehmes@36899
    76
boehmes@36899
    77
fun choose_logic ctxt ts =
boehmes@36899
    78
  let
boehmes@36899
    79
    fun choose [] = "AUFLIA"
boehmes@41059
    80
      | choose ((_, f) :: fs) = (case f ts of SOME s => s | NONE => choose fs)
boehmes@41059
    81
  in [":logic " ^ choose (Logics.get (Context.Proof ctxt))] end
boehmes@36899
    82
boehmes@36899
    83
boehmes@41072
    84
(** serialization **)
boehmes@36899
    85
boehmes@36898
    86
val add = Buffer.add
boehmes@36898
    87
fun sep f = add " " #> f
boehmes@36898
    88
fun enclose l r f = sep (add l #> f #> add r)
boehmes@36898
    89
val par = enclose "(" ")"
boehmes@36898
    90
fun app n f = (fn [] => sep (add n) | xs => par (add n #> fold f xs))
boehmes@36898
    91
fun line f = f #> add "\n"
boehmes@36898
    92
boehmes@36898
    93
fun var i = add "?v" #> add (string_of_int i)
boehmes@36898
    94
boehmes@41328
    95
fun sterm l (SMT_Translate.SVar i) = sep (var (l - i - 1))
boehmes@41328
    96
  | sterm l (SMT_Translate.SApp (n, ts)) = app n (sterm l) ts
boehmes@41328
    97
  | sterm _ (SMT_Translate.SLet _) =
boehmes@41328
    98
      raise Fail "SMT-LIB: unsupported let expression"
boehmes@41328
    99
  | sterm l (SMT_Translate.SQua (q, ss, ps, w, t)) =
boehmes@36898
   100
      let
boehmes@41328
   101
        fun quant SMT_Translate.SForall = add "forall"
boehmes@41328
   102
          | quant SMT_Translate.SExists = add "exists"
boehmes@36898
   103
        val vs = map_index (apfst (Integer.add l)) ss
boehmes@36898
   104
        fun var_decl (i, s) = par (var i #> sep (add s))
boehmes@36898
   105
        val sub = sterm (l + length ss)
boehmes@36898
   106
        fun pat kind ts = sep (add kind #> enclose "{" " }" (fold sub ts))
boehmes@41328
   107
        fun pats (SMT_Translate.SPat ts) = pat ":pat" ts
boehmes@41328
   108
          | pats (SMT_Translate.SNoPat ts) = pat ":nopat" ts
boehmes@40664
   109
        fun weight NONE = I
boehmes@40664
   110
          | weight (SOME i) =
boehmes@40664
   111
              sep (add ":weight { " #> add (string_of_int i) #> add " }")
boehmes@40664
   112
      in
boehmes@40664
   113
        par (quant q #> fold var_decl vs #> sub t #> fold pats ps #> weight w)
boehmes@40664
   114
      end
boehmes@36898
   115
boehmes@37155
   116
fun ssort sorts = sort fast_string_ord sorts
boehmes@37155
   117
fun fsort funcs = sort (prod_ord fast_string_ord (K EQUAL)) funcs
boehmes@37155
   118
boehmes@39298
   119
fun sdatatypes decls =
boehmes@39298
   120
  let
boehmes@41426
   121
    fun con (n, []) = sep (add n)
boehmes@39298
   122
      | con (n, sels) = par (add n #>
boehmes@41426
   123
          fold (fn (n, s) => par (add n #> sep (add s))) sels)
boehmes@41426
   124
    fun dtyp (n, decl) = add n #> fold con decl
boehmes@39298
   125
  in line (add ":datatypes " #> par (fold (par o dtyp) decls)) end
boehmes@39298
   126
boehmes@39298
   127
fun serialize comments {header, sorts, dtyps, funcs} ts =
boehmes@36898
   128
  Buffer.empty
boehmes@36898
   129
  |> line (add "(benchmark Isabelle")
boehmes@36898
   130
  |> line (add ":status unknown")
boehmes@36899
   131
  |> fold (line o add) header
boehmes@36898
   132
  |> length sorts > 0 ?
boehmes@37155
   133
       line (add ":extrasorts" #> par (fold (sep o add) (ssort sorts)))
boehmes@39298
   134
  |> fold sdatatypes dtyps
boehmes@36898
   135
  |> length funcs > 0 ? (
boehmes@36898
   136
       line (add ":extrafuns" #> add " (") #>
boehmes@36898
   137
       fold (fn (f, (ss, s)) =>
boehmes@37155
   138
         line (sep (app f (sep o add) (ss @ [s])))) (fsort funcs) #>
boehmes@36898
   139
       line (add ")"))
boehmes@36898
   140
  |> fold (fn t => line (add ":assumption" #> sterm 0 t)) ts
boehmes@36898
   141
  |> line (add ":formula true)")
boehmes@36898
   142
  |> fold (fn str => line (add "; " #> add str)) comments
boehmes@36898
   143
  |> Buffer.content
boehmes@36898
   144
boehmes@36898
   145
boehmes@41072
   146
(* interface *)
boehmes@36898
   147
boehmes@41127
   148
fun translate_config ctxt = {
boehmes@41127
   149
  prefixes = {
boehmes@41127
   150
    sort_prefix = "S",
boehmes@41127
   151
    func_prefix = "f"},
boehmes@41127
   152
  header = choose_logic ctxt,
boehmes@41127
   153
  is_fol = true,
boehmes@41127
   154
  has_datatypes = false,
boehmes@41127
   155
  serialize = serialize}
boehmes@36898
   156
boehmes@41127
   157
val setup = Context.theory_map (
boehmes@41127
   158
  setup_builtins #>
boehmes@41328
   159
  SMT_Translate.add_config (smtlibC, translate_config))
boehmes@41072
   160
boehmes@36898
   161
end