src/HOL/Tools/SMT/smtlib_interface.ML
author blanchet
Mon Sep 29 18:37:33 2014 +0200 (2014-09-29)
changeset 58482 7836013951e6
parent 58429 0b94858325a5
child 59015 627a93f67182
permissions -rw-r--r--
simplified and repaired veriT index handling code
blanchet@58061
     1
(*  Title:      HOL/Tools/SMT/smtlib_interface.ML
blanchet@56078
     2
    Author:     Sascha Boehme, TU Muenchen
blanchet@56078
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@56078
     4
blanchet@56078
     5
Interface to SMT solvers based on the SMT-LIB 2 format.
blanchet@56078
     6
*)
blanchet@56078
     7
blanchet@58061
     8
signature SMTLIB_INTERFACE =
blanchet@56078
     9
sig
blanchet@58061
    10
  val smtlibC: SMT_Util.class
blanchet@56090
    11
  val add_logic: int * (term list -> string option) -> Context.generic -> Context.generic
blanchet@58061
    12
  val translate_config: Proof.context -> SMT_Translate.config
blanchet@58482
    13
  val assert_name_of_index: int -> string
blanchet@57203
    14
  val assert_index_of_name: string -> int
fleury@57704
    15
  val assert_prefix : string
blanchet@57229
    16
end;
blanchet@56078
    17
blanchet@58061
    18
structure SMTLIB_Interface: SMTLIB_INTERFACE =
blanchet@56078
    19
struct
blanchet@56078
    20
blanchet@58061
    21
val smtlibC = ["smtlib"]
blanchet@56078
    22
blanchet@56078
    23
blanchet@56078
    24
(* builtins *)
blanchet@56078
    25
blanchet@56078
    26
local
blanchet@56078
    27
  fun int_num _ i = SOME (string_of_int i)
blanchet@56078
    28
blanchet@58061
    29
  fun is_linear [t] = SMT_Util.is_number t
blanchet@58061
    30
    | is_linear [t, u] = SMT_Util.is_number t orelse SMT_Util.is_number u
blanchet@56078
    31
    | is_linear _ = false
blanchet@56078
    32
blanchet@56078
    33
  fun times _ _ ts =
blanchet@56078
    34
    let val mk = Term.list_comb o pair @{const times (int)}
blanchet@56078
    35
    in if is_linear ts then SOME ("*", 2, ts, mk) else NONE end
blanchet@56078
    36
in
blanchet@56078
    37
blanchet@56078
    38
val setup_builtins =
blanchet@58061
    39
  fold (SMT_Builtin.add_builtin_typ smtlibC) [
blanchet@56078
    40
    (@{typ bool}, K (SOME "Bool"), K (K NONE)),
blanchet@56078
    41
    (@{typ int}, K (SOME "Int"), int_num)] #>
blanchet@58061
    42
  fold (SMT_Builtin.add_builtin_fun' smtlibC) [
blanchet@56078
    43
    (@{const True}, "true"),
blanchet@56078
    44
    (@{const False}, "false"),
blanchet@56078
    45
    (@{const Not}, "not"),
blanchet@56078
    46
    (@{const HOL.conj}, "and"),
blanchet@56078
    47
    (@{const HOL.disj}, "or"),
blanchet@56078
    48
    (@{const HOL.implies}, "=>"),
blanchet@56078
    49
    (@{const HOL.eq ('a)}, "="),
blanchet@56078
    50
    (@{const If ('a)}, "ite"),
blanchet@56078
    51
    (@{const less (int)}, "<"),
blanchet@56078
    52
    (@{const less_eq (int)}, "<="),
fleury@57711
    53
    (@{const uminus (int)}, "-"),
blanchet@56078
    54
    (@{const plus (int)}, "+"),
blanchet@56078
    55
    (@{const minus (int)}, "-")] #>
blanchet@58061
    56
  SMT_Builtin.add_builtin_fun smtlibC
blanchet@56078
    57
    (Term.dest_Const @{const times (int)}, times)
blanchet@56078
    58
blanchet@56078
    59
end
blanchet@56078
    60
blanchet@56078
    61
blanchet@56078
    62
(* serialization *)
blanchet@56078
    63
blanchet@57238
    64
(** logic **)
blanchet@56078
    65
blanchet@56078
    66
fun fst_int_ord ((i1, _), (i2, _)) = int_ord (i1, i2)
blanchet@56078
    67
blanchet@56078
    68
structure Logics = Generic_Data
blanchet@56078
    69
(
blanchet@56078
    70
  type T = (int * (term list -> string option)) list
blanchet@56078
    71
  val empty = []
blanchet@56078
    72
  val extend = I
blanchet@56078
    73
  fun merge data = Ord_List.merge fst_int_ord data
blanchet@56078
    74
)
blanchet@56078
    75
blanchet@56078
    76
fun add_logic pf = Logics.map (Ord_List.insert fst_int_ord pf)
blanchet@56078
    77
blanchet@56078
    78
fun choose_logic ctxt ts =
blanchet@56078
    79
  let
blanchet@56078
    80
    fun choose [] = "AUFLIA"
blanchet@56078
    81
      | choose ((_, f) :: fs) = (case f ts of SOME s => s | NONE => choose fs)
blanchet@57553
    82
  in
blanchet@57553
    83
    (case choose (Logics.get (Context.Proof ctxt)) of
blanchet@57553
    84
      "" => "" (* for default Z3 logic, a subset of everything *)
blanchet@57553
    85
    | logic => "(set-logic " ^ logic ^ ")\n")
blanchet@57553
    86
  end
blanchet@56078
    87
blanchet@56078
    88
blanchet@56078
    89
(** serialization **)
blanchet@56078
    90
blanchet@56078
    91
fun var i = "?v" ^ string_of_int i
blanchet@56078
    92
blanchet@58061
    93
fun tree_of_sterm l (SMT_Translate.SVar i) = SMTLIB.Sym (var (l - i - 1))
blanchet@58061
    94
  | tree_of_sterm _ (SMT_Translate.SApp (n, [])) = SMTLIB.Sym n
blanchet@58061
    95
  | tree_of_sterm l (SMT_Translate.SApp (n, ts)) =
blanchet@58061
    96
      SMTLIB.S (SMTLIB.Sym n :: map (tree_of_sterm l) ts)
blanchet@58061
    97
  | tree_of_sterm _ (SMT_Translate.SLet _) =
blanchet@56078
    98
      raise Fail "SMT-LIB: unsupported let expression"
blanchet@58061
    99
  | tree_of_sterm l (SMT_Translate.SQua (q, ss, pats, t)) =
blanchet@56078
   100
      let
blanchet@56078
   101
        val l' = l + length ss
blanchet@56078
   102
blanchet@58061
   103
        fun quant_name SMT_Translate.SForall = "forall"
blanchet@58061
   104
          | quant_name SMT_Translate.SExists = "exists"
blanchet@56078
   105
blanchet@56078
   106
        fun gen_trees_of_pat keyword ps =
blanchet@58061
   107
          [SMTLIB.Key keyword, (case map (tree_of_sterm l') ps of [t] => t | ts => SMTLIB.S ts)]
blanchet@58061
   108
        fun trees_of_pat (SMT_Translate.SPat ps) = gen_trees_of_pat "pattern" ps
blanchet@58061
   109
          | trees_of_pat (SMT_Translate.SNoPat ps) = gen_trees_of_pat "no-pattern" ps
blanchet@57165
   110
        fun tree_of_pats [] t = t
blanchet@58061
   111
          | tree_of_pats pats t = SMTLIB.S (SMTLIB.Sym "!" :: t :: maps trees_of_pat pats)
blanchet@56078
   112
blanchet@56078
   113
        val vs = map_index (fn (i, ty) =>
blanchet@58061
   114
          SMTLIB.S [SMTLIB.Sym (var (l + i)), SMTLIB.Sym ty]) ss
blanchet@56078
   115
blanchet@56078
   116
        val body = t
blanchet@56078
   117
          |> tree_of_sterm l'
blanchet@57165
   118
          |> tree_of_pats pats
blanchet@56078
   119
      in
blanchet@58061
   120
        SMTLIB.S [SMTLIB.Sym (quant_name q), SMTLIB.S vs, body]
blanchet@56078
   121
      end
blanchet@56078
   122
blanchet@56078
   123
blanchet@56078
   124
fun sctrarg (sel, typ) = "(" ^ sel ^ " " ^ typ ^ ")"
blanchet@56078
   125
fun sctr (name, args) = enclose "(" ")" (space_implode " " (name :: map sctrarg args))
blanchet@56078
   126
fun sdatatype (name, ctrs) = enclose "(" ")" (space_implode " " (name :: map sctr ctrs))
blanchet@56078
   127
blanchet@56078
   128
fun string_of_fun (f, (ss, s)) = f ^ " (" ^ space_implode " " ss ^ ") " ^ s
blanchet@56078
   129
blanchet@58061
   130
fun named_sterm s t = SMTLIB.S [SMTLIB.Sym "!", t, SMTLIB.Key "named", SMTLIB.Sym s]
blanchet@57203
   131
blanchet@57203
   132
val assert_prefix = "a"
blanchet@57203
   133
blanchet@57203
   134
fun assert_name_of_index i = assert_prefix ^ string_of_int i
blanchet@57203
   135
fun assert_index_of_name s = the_default ~1 (Int.fromString (unprefix assert_prefix s))
blanchet@57203
   136
blanchet@58429
   137
fun sdtyp (fp, dtyps) =
blanchet@58429
   138
  Buffer.add (enclose ("(declare-" ^ BNF_FP_Util.co_prefix fp ^ "datatypes () (") "))\n"
blanchet@58429
   139
    (space_implode "\n  " (map sdatatype dtyps)))
blanchet@58429
   140
blanchet@58429
   141
fun serialize smt_options comments {logic, sorts, dtyps, funcs} ts =
blanchet@56078
   142
  Buffer.empty
blanchet@56078
   143
  |> fold (Buffer.add o enclose "; " "\n") comments
blanchet@57239
   144
  |> fold (fn (k, v) => Buffer.add ("(set-option " ^ k ^ " " ^ v ^ ")\n")) smt_options
blanchet@57238
   145
  |> Buffer.add logic
blanchet@57203
   146
  |> fold (Buffer.add o enclose "(declare-sort " " 0)\n") (sort fast_string_ord sorts)
blanchet@58429
   147
  |> fold sdtyp (AList.coalesce (op =) dtyps)
blanchet@56078
   148
  |> fold (Buffer.add o enclose "(declare-fun " ")\n" o string_of_fun)
blanchet@58061
   149
      (sort (fast_string_ord o pairself fst) funcs)
blanchet@57203
   150
  |> fold (fn (i, t) => Buffer.add (enclose "(assert " ")\n"
blanchet@58061
   151
      (SMTLIB.str_of (named_sterm (assert_name_of_index i) (tree_of_sterm 0 t))))) (map_index I ts)
blanchet@57203
   152
  |> Buffer.add "(check-sat)\n(get-proof)\n"
blanchet@56078
   153
  |> Buffer.content
blanchet@56078
   154
blanchet@56078
   155
(* interface *)
blanchet@56078
   156
blanchet@56078
   157
fun translate_config ctxt = {
blanchet@57238
   158
  logic = choose_logic ctxt,
blanchet@58360
   159
  fp_kinds = [],
blanchet@56078
   160
  serialize = serialize}
blanchet@56078
   161
blanchet@56078
   162
val _ = Theory.setup (Context.theory_map
blanchet@56078
   163
  (setup_builtins #>
blanchet@58061
   164
   SMT_Translate.add_config (smtlibC, translate_config)))
blanchet@56078
   165
blanchet@57229
   166
end;