src/HOL/Tools/SMT/smtlib_interface.ML
author wenzelm
Sat Jan 08 17:30:05 2011 +0100 (2011-01-08)
changeset 41473 3717fc42ebe9
parent 41426 09615ed31f04
child 47155 ade3fc826af3
permissions -rw-r--r--
Ord_List.merge convenience;
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@41280
    35
boehmes@41281
    36
  fun distinct _ T [t] =
boehmes@41059
    37
        (case try HOLogic.dest_list t of
boehmes@41127
    38
          SOME (ts as _ :: _) =>
boehmes@41127
    39
            let
boehmes@41281
    40
              val c = Const (@{const_name distinct}, T)
boehmes@41281
    41
              fun mk us = c $ HOLogic.mk_list T us
boehmes@41281
    42
            in SOME ("distinct", length ts, ts, mk) end
boehmes@41059
    43
        | _ => NONE)
boehmes@41059
    44
    | distinct _ _ _ = NONE
boehmes@41059
    45
in
boehmes@36898
    46
boehmes@41072
    47
val setup_builtins =
boehmes@41328
    48
  SMT_Builtin.add_builtin_typ smtlibC (@{typ int}, K (SOME "Int"), int_num) #>
boehmes@41328
    49
  fold (SMT_Builtin.add_builtin_fun' smtlibC) [
boehmes@41059
    50
    (@{const True}, "true"),
boehmes@41059
    51
    (@{const False}, "false"),
boehmes@41059
    52
    (@{const Not}, "not"),
boehmes@41059
    53
    (@{const HOL.conj}, "and"),
boehmes@41059
    54
    (@{const HOL.disj}, "or"),
boehmes@41059
    55
    (@{const HOL.implies}, "implies"),
boehmes@41059
    56
    (@{const HOL.eq (bool)}, "iff"),
boehmes@41059
    57
    (@{const HOL.eq ('a)}, "="),
boehmes@41059
    58
    (@{const If (bool)}, "if_then_else"),
boehmes@41059
    59
    (@{const If ('a)}, "ite"),
boehmes@41059
    60
    (@{const less (int)}, "<"),
boehmes@41059
    61
    (@{const less_eq (int)}, "<="),
boehmes@41059
    62
    (@{const uminus (int)}, "~"),
boehmes@41059
    63
    (@{const plus (int)}, "+"),
boehmes@41280
    64
    (@{const minus (int)}, "-") ] #>
boehmes@41328
    65
  SMT_Builtin.add_builtin_fun smtlibC
boehmes@41328
    66
    (Term.dest_Const @{const times (int)}, times) #>
boehmes@41328
    67
  SMT_Builtin.add_builtin_fun smtlibC
boehmes@41328
    68
    (Term.dest_Const @{const distinct ('a)}, distinct)
boehmes@36898
    69
boehmes@41059
    70
end
boehmes@36898
    71
boehmes@36898
    72
boehmes@41059
    73
(* serialization *)
boehmes@36898
    74
boehmes@41059
    75
(** header **)
boehmes@41059
    76
boehmes@41059
    77
fun fst_int_ord ((i1, _), (i2, _)) = int_ord (i1, i2)
boehmes@36899
    78
boehmes@36899
    79
structure Logics = Generic_Data
boehmes@36899
    80
(
boehmes@36899
    81
  type T = (int * (term list -> string option)) list
boehmes@36899
    82
  val empty = []
boehmes@36899
    83
  val extend = I
wenzelm@41473
    84
  fun merge data = Ord_List.merge fst_int_ord data
boehmes@36899
    85
)
boehmes@36899
    86
boehmes@41059
    87
fun add_logic pf = Logics.map (Ord_List.insert fst_int_ord pf)
boehmes@36899
    88
boehmes@36899
    89
fun choose_logic ctxt ts =
boehmes@36899
    90
  let
boehmes@36899
    91
    fun choose [] = "AUFLIA"
boehmes@41059
    92
      | choose ((_, f) :: fs) = (case f ts of SOME s => s | NONE => choose fs)
boehmes@41059
    93
  in [":logic " ^ choose (Logics.get (Context.Proof ctxt))] end
boehmes@36899
    94
boehmes@36899
    95
boehmes@41072
    96
(** serialization **)
boehmes@36899
    97
boehmes@36898
    98
val add = Buffer.add
boehmes@36898
    99
fun sep f = add " " #> f
boehmes@36898
   100
fun enclose l r f = sep (add l #> f #> add r)
boehmes@36898
   101
val par = enclose "(" ")"
boehmes@36898
   102
fun app n f = (fn [] => sep (add n) | xs => par (add n #> fold f xs))
boehmes@36898
   103
fun line f = f #> add "\n"
boehmes@36898
   104
boehmes@36898
   105
fun var i = add "?v" #> add (string_of_int i)
boehmes@36898
   106
boehmes@41328
   107
fun sterm l (SMT_Translate.SVar i) = sep (var (l - i - 1))
boehmes@41328
   108
  | sterm l (SMT_Translate.SApp (n, ts)) = app n (sterm l) ts
boehmes@41328
   109
  | sterm _ (SMT_Translate.SLet _) =
boehmes@41328
   110
      raise Fail "SMT-LIB: unsupported let expression"
boehmes@41328
   111
  | sterm l (SMT_Translate.SQua (q, ss, ps, w, t)) =
boehmes@36898
   112
      let
boehmes@41328
   113
        fun quant SMT_Translate.SForall = add "forall"
boehmes@41328
   114
          | quant SMT_Translate.SExists = add "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@41328
   119
        fun pats (SMT_Translate.SPat ts) = pat ":pat" ts
boehmes@41328
   120
          | pats (SMT_Translate.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@41426
   133
    fun con (n, []) = sep (add n)
boehmes@39298
   134
      | con (n, sels) = par (add n #>
boehmes@41426
   135
          fold (fn (n, s) => par (add n #> sep (add s))) sels)
boehmes@41426
   136
    fun dtyp (n, decl) = add n #> fold 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@41328
   171
  SMT_Translate.add_config (smtlibC, translate_config))
boehmes@41072
   172
boehmes@36898
   173
end