src/HOL/Tools/SMT/smtlib_interface.ML
author wenzelm
Sat May 15 17:59:06 2010 +0200 (2010-05-15)
changeset 36936 c52d1c130898
parent 36899 bcd6fce5bf06
child 37155 e3f18cfc9829
permissions -rw-r--r--
incorporated further conversions and conversionals, after some minor tuning;
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@36899
     9
  type builtins = {
boehmes@36899
    10
    builtin_typ: typ -> string option,
boehmes@36899
    11
    builtin_num: typ -> int -> string option,
boehmes@36899
    12
    builtin_func: string * typ -> term list -> (string * term list) option,
boehmes@36899
    13
    builtin_pred: string * typ -> term list -> (string * term list) option,
boehmes@36899
    14
    is_builtin_pred: string -> typ -> bool }
boehmes@36899
    15
  val add_builtins: builtins -> Context.generic -> Context.generic
boehmes@36899
    16
  val add_logic: (term list -> string option) -> Context.generic ->
boehmes@36899
    17
    Context.generic
boehmes@36898
    18
  val interface: SMT_Solver.interface
boehmes@36898
    19
end
boehmes@36898
    20
boehmes@36898
    21
structure SMTLIB_Interface: SMTLIB_INTERFACE =
boehmes@36898
    22
struct
boehmes@36898
    23
boehmes@36898
    24
structure N = SMT_Normalize
boehmes@36898
    25
structure T = SMT_Translate
boehmes@36898
    26
boehmes@36898
    27
boehmes@36898
    28
boehmes@36898
    29
(** facts about uninterpreted constants **)
boehmes@36898
    30
boehmes@36898
    31
infix 2 ??
boehmes@36898
    32
fun (ex ?? f) thms = if exists (ex o Thm.prop_of) thms then f thms else thms
boehmes@36898
    33
boehmes@36898
    34
boehmes@36898
    35
(* pairs *)
boehmes@36898
    36
boehmes@36898
    37
val pair_rules = [@{thm fst_conv}, @{thm snd_conv}, @{thm pair_collapse}]
boehmes@36898
    38
boehmes@36898
    39
val pair_type = (fn Type (@{type_name "*"}, _) => true | _ => false)
boehmes@36898
    40
val exists_pair_type = Term.exists_type (Term.exists_subtype pair_type)
boehmes@36898
    41
boehmes@36898
    42
val add_pair_rules = exists_pair_type ?? append pair_rules
boehmes@36898
    43
boehmes@36898
    44
boehmes@36898
    45
(* function update *)
boehmes@36898
    46
boehmes@36898
    47
val fun_upd_rules = [@{thm fun_upd_same}, @{thm fun_upd_apply}]
boehmes@36898
    48
boehmes@36898
    49
val is_fun_upd = (fn Const (@{const_name fun_upd}, _) => true | _ => false)
boehmes@36898
    50
val exists_fun_upd = Term.exists_subterm is_fun_upd
boehmes@36898
    51
boehmes@36898
    52
val add_fun_upd_rules = exists_fun_upd ?? append fun_upd_rules
boehmes@36898
    53
boehmes@36898
    54
boehmes@36898
    55
(* abs/min/max *)
boehmes@36898
    56
boehmes@36898
    57
val exists_abs_min_max = Term.exists_subterm (fn
boehmes@36898
    58
    Const (@{const_name abs}, _) => true
boehmes@36898
    59
  | Const (@{const_name min}, _) => true
boehmes@36898
    60
  | Const (@{const_name max}, _) => true
boehmes@36898
    61
  | _ => false)
boehmes@36898
    62
boehmes@36898
    63
val unfold_abs_conv = Conv.rewr_conv @{thm abs_if[THEN eq_reflection]}
boehmes@36898
    64
val unfold_min_conv = Conv.rewr_conv @{thm min_def[THEN eq_reflection]}
boehmes@36898
    65
val unfold_max_conv = Conv.rewr_conv @{thm max_def[THEN eq_reflection]}
boehmes@36898
    66
boehmes@36898
    67
fun expand_conv cv = N.eta_expand_conv (K cv)
boehmes@36898
    68
fun expand2_conv cv = N.eta_expand_conv (N.eta_expand_conv (K cv))
boehmes@36898
    69
boehmes@36898
    70
fun unfold_def_conv ctxt ct =
boehmes@36898
    71
  (case Thm.term_of ct of
boehmes@36898
    72
    Const (@{const_name abs}, _) $ _ => unfold_abs_conv
boehmes@36898
    73
  | Const (@{const_name abs}, _) => expand_conv unfold_abs_conv ctxt
boehmes@36898
    74
  | Const (@{const_name min}, _) $ _ $ _ => unfold_min_conv
boehmes@36898
    75
  | Const (@{const_name min}, _) $ _ => expand_conv unfold_min_conv ctxt
boehmes@36898
    76
  | Const (@{const_name min}, _) => expand2_conv unfold_min_conv ctxt
boehmes@36898
    77
  | Const (@{const_name max}, _) $ _ $ _ => unfold_max_conv
boehmes@36898
    78
  | Const (@{const_name max}, _) $ _ => expand_conv unfold_max_conv ctxt
boehmes@36898
    79
  | Const (@{const_name max}, _) => expand2_conv unfold_max_conv ctxt
boehmes@36898
    80
  | _ => Conv.all_conv) ct
boehmes@36898
    81
boehmes@36898
    82
fun unfold_abs_min_max_defs ctxt thm =
boehmes@36898
    83
  if exists_abs_min_max (Thm.prop_of thm)
wenzelm@36936
    84
  then Conv.fconv_rule (Conv.top_conv unfold_def_conv ctxt) thm
boehmes@36898
    85
  else thm
boehmes@36898
    86
boehmes@36898
    87
boehmes@36898
    88
(* include additional facts *)
boehmes@36898
    89
boehmes@36898
    90
fun extra_norm thms ctxt =
boehmes@36898
    91
  thms
boehmes@36898
    92
  |> add_pair_rules
boehmes@36898
    93
  |> add_fun_upd_rules
boehmes@36898
    94
  |> map (unfold_abs_min_max_defs ctxt)
boehmes@36898
    95
  |> rpair ctxt
boehmes@36898
    96
boehmes@36898
    97
boehmes@36898
    98
boehmes@36898
    99
(** builtins **)
boehmes@36898
   100
boehmes@36899
   101
(* additional builtins *)
boehmes@36898
   102
boehmes@36899
   103
type builtins = {
boehmes@36899
   104
  builtin_typ: typ -> string option,
boehmes@36899
   105
  builtin_num: typ -> int -> string option,
boehmes@36899
   106
  builtin_func: string * typ -> term list -> (string * term list) option,
boehmes@36899
   107
  builtin_pred: string * typ -> term list -> (string * term list) option,
boehmes@36899
   108
  is_builtin_pred: string -> typ -> bool }
boehmes@36898
   109
boehmes@36899
   110
fun chained _ [] = NONE
boehmes@36899
   111
  | chained f (b :: bs) = (case f b of SOME y => SOME y | NONE => chained f bs)
boehmes@36898
   112
boehmes@36899
   113
fun chained' _ [] = false
boehmes@36899
   114
  | chained' f (b :: bs) = f b orelse chained' f bs
boehmes@36899
   115
boehmes@36899
   116
fun chained_builtin_typ bs T =
boehmes@36899
   117
  chained (fn {builtin_typ, ...} : builtins => builtin_typ T) bs
boehmes@36898
   118
boehmes@36899
   119
fun chained_builtin_num bs T i =
boehmes@36899
   120
  chained (fn {builtin_num, ...} : builtins => builtin_num T i) bs
boehmes@36898
   121
boehmes@36899
   122
fun chained_builtin_func bs c ts =
boehmes@36899
   123
  chained (fn {builtin_func, ...} : builtins => builtin_func c ts) bs
boehmes@36899
   124
boehmes@36899
   125
fun chained_builtin_pred bs c ts =
boehmes@36899
   126
  chained (fn {builtin_pred, ...} : builtins => builtin_pred c ts) bs
boehmes@36898
   127
boehmes@36899
   128
fun chained_is_builtin_pred bs n T =
boehmes@36899
   129
  chained' (fn {is_builtin_pred, ...} : builtins => is_builtin_pred n T) bs
boehmes@36898
   130
boehmes@36899
   131
fun fst_int_ord ((s1, _), (s2, _)) = int_ord (s1, s2)
boehmes@36898
   132
boehmes@36899
   133
structure Builtins = Generic_Data
boehmes@36899
   134
(
boehmes@36899
   135
  type T = (int * builtins) list
boehmes@36899
   136
  val empty = []
boehmes@36899
   137
  val extend = I
boehmes@36899
   138
  fun merge (bs1, bs2) = OrdList.union fst_int_ord bs2 bs1
boehmes@36899
   139
)
boehmes@36898
   140
boehmes@36899
   141
fun add_builtins bs = Builtins.map (OrdList.insert fst_int_ord (serial (), bs))
boehmes@36898
   142
boehmes@36899
   143
fun get_builtins ctxt = map snd (Builtins.get (Context.Proof ctxt))
boehmes@36899
   144
boehmes@36899
   145
boehmes@36899
   146
(* basic builtins combined with additional builtins *)
boehmes@36899
   147
boehmes@36899
   148
fun builtin_typ _ @{typ int} = SOME "Int"
boehmes@36899
   149
  | builtin_typ ctxt T = chained_builtin_typ (get_builtins ctxt) T
boehmes@36898
   150
boehmes@36899
   151
fun builtin_num _ @{typ int} i = SOME (string_of_int i)
boehmes@36899
   152
  | builtin_num ctxt T i = chained_builtin_num (get_builtins ctxt) T i
boehmes@36898
   153
boehmes@36899
   154
fun if_int_type T n =
boehmes@36899
   155
  (case try Term.domain_type T of
boehmes@36899
   156
    SOME @{typ int} => SOME n
boehmes@36899
   157
  | _ => NONE)
boehmes@36898
   158
boehmes@36898
   159
fun conn @{const_name True} = SOME "true"
boehmes@36898
   160
  | conn @{const_name False} = SOME "false"
boehmes@36898
   161
  | conn @{const_name Not} = SOME "not"
boehmes@36898
   162
  | conn @{const_name "op &"} = SOME "and"
boehmes@36898
   163
  | conn @{const_name "op |"} = SOME "or"
boehmes@36898
   164
  | conn @{const_name "op -->"} = SOME "implies"
boehmes@36898
   165
  | conn @{const_name "op ="} = SOME "iff"
boehmes@36898
   166
  | conn @{const_name If} = SOME "if_then_else"
boehmes@36898
   167
  | conn _ = NONE
boehmes@36898
   168
boehmes@36898
   169
fun pred @{const_name distinct} _ = SOME "distinct"
boehmes@36898
   170
  | pred @{const_name "op ="} _ = SOME "="
boehmes@36898
   171
  | pred @{const_name term_eq} _ = SOME "="
boehmes@36899
   172
  | pred @{const_name less} T = if_int_type T "<"
boehmes@36899
   173
  | pred @{const_name less_eq} T = if_int_type T "<="
boehmes@36898
   174
  | pred _ _ = NONE
boehmes@36898
   175
boehmes@36899
   176
fun func @{const_name If} _ = SOME "ite"
boehmes@36899
   177
  | func @{const_name uminus} T = if_int_type T "~"
boehmes@36899
   178
  | func @{const_name plus} T = if_int_type T "+"
boehmes@36899
   179
  | func @{const_name minus} T = if_int_type T "-"
boehmes@36899
   180
  | func @{const_name times} T = if_int_type T "*"
boehmes@36899
   181
  | func _ _ = NONE
boehmes@36899
   182
boehmes@36899
   183
val is_propT = (fn @{typ prop} => true | _ => false)
boehmes@36899
   184
fun is_connT T = Term.strip_type T |> (fn (Us, U) => forall is_propT (U :: Us))
boehmes@36899
   185
fun is_predT T = is_propT (Term.body_type T)
boehmes@36898
   186
boehmes@36898
   187
fun is_builtin_conn (n, T) = is_connT T andalso is_some (conn n)
boehmes@36899
   188
fun is_builtin_pred ctxt (n, T) = is_predT T andalso
boehmes@36899
   189
  (is_some (pred n T) orelse chained_is_builtin_pred (get_builtins ctxt) n T)
boehmes@36898
   190
boehmes@36899
   191
fun builtin_fun ctxt (c as (n, T)) ts =
boehmes@36899
   192
  let
boehmes@36899
   193
    val builtin_func' = chained_builtin_func (get_builtins ctxt)
boehmes@36899
   194
    val builtin_pred' = chained_builtin_pred (get_builtins ctxt)
boehmes@36899
   195
  in
boehmes@36899
   196
    if is_connT T then conn n |> Option.map (rpair ts)
boehmes@36899
   197
    else if is_predT T then
boehmes@36899
   198
      (case pred n T of SOME c' => SOME (c', ts) | NONE => builtin_pred' c ts)
boehmes@36899
   199
    else 
boehmes@36899
   200
      (case func n T of SOME c' => SOME (c', ts) | NONE => builtin_func' c ts)
boehmes@36899
   201
  end
boehmes@36898
   202
boehmes@36898
   203
boehmes@36898
   204
boehmes@36898
   205
(** serialization **)
boehmes@36898
   206
boehmes@36899
   207
(* header *)
boehmes@36899
   208
boehmes@36899
   209
structure Logics = Generic_Data
boehmes@36899
   210
(
boehmes@36899
   211
  type T = (int * (term list -> string option)) list
boehmes@36899
   212
  val empty = []
boehmes@36899
   213
  val extend = I
boehmes@36899
   214
  fun merge (bs1, bs2) = OrdList.union fst_int_ord bs2 bs1
boehmes@36899
   215
)
boehmes@36899
   216
boehmes@36899
   217
fun add_logic l = Logics.map (OrdList.insert fst_int_ord (serial (), l))
boehmes@36899
   218
boehmes@36899
   219
fun choose_logic ctxt ts =
boehmes@36899
   220
  let
boehmes@36899
   221
    fun choose [] = "AUFLIA"
boehmes@36899
   222
      | choose ((_, l) :: ls) = (case l ts of SOME s => s | NONE => choose ls)
boehmes@36899
   223
  in [":logic " ^ choose (rev (Logics.get (Context.Proof ctxt)))] end
boehmes@36899
   224
boehmes@36899
   225
boehmes@36899
   226
(* serialization *)
boehmes@36899
   227
boehmes@36898
   228
val add = Buffer.add
boehmes@36898
   229
fun sep f = add " " #> f
boehmes@36898
   230
fun enclose l r f = sep (add l #> f #> add r)
boehmes@36898
   231
val par = enclose "(" ")"
boehmes@36898
   232
fun app n f = (fn [] => sep (add n) | xs => par (add n #> fold f xs))
boehmes@36898
   233
fun line f = f #> add "\n"
boehmes@36898
   234
boehmes@36898
   235
fun var i = add "?v" #> add (string_of_int i)
boehmes@36898
   236
boehmes@36898
   237
fun sterm l (T.SVar i) = sep (var (l - i - 1))
boehmes@36898
   238
  | sterm l (T.SApp (n, ts)) = app n (sterm l) ts
boehmes@36898
   239
  | sterm _ (T.SLet _) = raise Fail "SMT-LIB: unsupported let expression"
boehmes@36898
   240
  | sterm l (T.SQua (q, ss, ps, t)) =
boehmes@36898
   241
      let
boehmes@36898
   242
        val quant = add o (fn T.SForall => "forall" | T.SExists => "exists")
boehmes@36898
   243
        val vs = map_index (apfst (Integer.add l)) ss
boehmes@36898
   244
        fun var_decl (i, s) = par (var i #> sep (add s))
boehmes@36898
   245
        val sub = sterm (l + length ss)
boehmes@36898
   246
        fun pat kind ts = sep (add kind #> enclose "{" " }" (fold sub ts))
boehmes@36898
   247
        fun pats (T.SPat ts) = pat ":pat" ts
boehmes@36898
   248
          | pats (T.SNoPat ts) = pat ":nopat" ts
boehmes@36898
   249
      in par (quant q #> fold var_decl vs #> sub t #> fold pats ps) end
boehmes@36898
   250
boehmes@36899
   251
fun serialize comments {header, sorts, funcs} ts =
boehmes@36898
   252
  Buffer.empty
boehmes@36898
   253
  |> line (add "(benchmark Isabelle")
boehmes@36898
   254
  |> line (add ":status unknown")
boehmes@36899
   255
  |> fold (line o add) header
boehmes@36898
   256
  |> length sorts > 0 ?
boehmes@36898
   257
       line (add ":extrasorts" #> par (fold (sep o add) sorts))
boehmes@36898
   258
  |> length funcs > 0 ? (
boehmes@36898
   259
       line (add ":extrafuns" #> add " (") #>
boehmes@36898
   260
       fold (fn (f, (ss, s)) =>
boehmes@36898
   261
         line (sep (app f (sep o add) (ss @ [s])))) funcs #>
boehmes@36898
   262
       line (add ")"))
boehmes@36898
   263
  |> fold (fn t => line (add ":assumption" #> sterm 0 t)) ts
boehmes@36898
   264
  |> line (add ":formula true)")
boehmes@36898
   265
  |> fold (fn str => line (add "; " #> add str)) comments
boehmes@36898
   266
  |> Buffer.content
boehmes@36898
   267
boehmes@36898
   268
boehmes@36898
   269
boehmes@36899
   270
(** interfaces **)
boehmes@36898
   271
boehmes@36898
   272
val interface = {
boehmes@36898
   273
  extra_norm = extra_norm,
boehmes@36898
   274
  translate = {
boehmes@36898
   275
    prefixes = {
boehmes@36898
   276
      sort_prefix = "S",
boehmes@36898
   277
      func_prefix = "f"},
boehmes@36899
   278
    header = choose_logic,
boehmes@36898
   279
    strict = SOME {
boehmes@36898
   280
      is_builtin_conn = is_builtin_conn,
boehmes@36898
   281
      is_builtin_pred = is_builtin_pred,
boehmes@36898
   282
      is_builtin_distinct = true},
boehmes@36898
   283
    builtins = {
boehmes@36898
   284
      builtin_typ = builtin_typ,
boehmes@36898
   285
      builtin_num = builtin_num,
boehmes@36898
   286
      builtin_fun = builtin_fun},
boehmes@36898
   287
    serialize = serialize}}
boehmes@36898
   288
boehmes@36898
   289
end