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