|
1 (* Title: HOL/SMT/Tools/smtlib_interface.ML |
|
2 Author: Sascha Boehme, TU Muenchen |
|
3 |
|
4 Interface to SMT solvers based on the SMT-LIB format. |
|
5 *) |
|
6 |
|
7 signature SMTLIB_INTERFACE = |
|
8 sig |
|
9 val interface: string list -> SMT_Translate.config |
|
10 end |
|
11 |
|
12 structure SMTLIB_Interface: SMTLIB_INTERFACE = |
|
13 struct |
|
14 |
|
15 structure T = SMT_Translate |
|
16 |
|
17 fun dest_binT T = |
|
18 (case T of |
|
19 Type (@{type_name "Numeral_Type.num0"}, _) => 0 |
|
20 | Type (@{type_name "Numeral_Type.num1"}, _) => 1 |
|
21 | Type (@{type_name "Numeral_Type.bit0"}, [T]) => 2 * dest_binT T |
|
22 | Type (@{type_name "Numeral_Type.bit1"}, [T]) => 1 + 2 * dest_binT T |
|
23 | _ => raise TYPE ("dest_binT", [T], [])) |
|
24 |
|
25 fun dest_wordT (Type (@{type_name word}, [T])) = dest_binT T |
|
26 | dest_wordT T = raise TYPE ("dest_wordT", [T], []) |
|
27 |
|
28 |
|
29 |
|
30 (* builtins *) |
|
31 |
|
32 fun index1 n i = n ^ "[" ^ string_of_int i ^ "]" |
|
33 fun index2 n i j = n ^ "[" ^ string_of_int i ^ ":" ^ string_of_int j ^ "]" |
|
34 |
|
35 fun builtin_typ @{typ int} = SOME "Int" |
|
36 | builtin_typ @{typ real} = SOME "Real" |
|
37 | builtin_typ (Type (@{type_name word}, [T])) = |
|
38 Option.map (index1 "BitVec") (try dest_binT T) |
|
39 | builtin_typ _ = NONE |
|
40 |
|
41 fun builtin_num @{typ int} i = SOME (string_of_int i) |
|
42 | builtin_num @{typ real} i = SOME (string_of_int i ^ ".0") |
|
43 | builtin_num (Type (@{type_name word}, [T])) i = |
|
44 Option.map (index1 ("bv" ^ string_of_int i)) (try dest_binT T) |
|
45 | builtin_num _ _ = NONE |
|
46 |
|
47 val is_propT = (fn @{typ prop} => true | _ => false) |
|
48 fun is_connT T = Term.strip_type T |> (fn (Us, U) => forall is_propT (U :: Us)) |
|
49 fun is_predT T = is_propT (Term.body_type T) |
|
50 |
|
51 fun just c ts = SOME (c, ts) |
|
52 |
|
53 val is_arith_type = member (op =) [@{typ int}, @{typ real}] o Term.domain_type |
|
54 |
|
55 fun fixed_bvT (Ts, T) x = |
|
56 if forall (can dest_wordT) (T :: Ts) then SOME x else NONE |
|
57 |
|
58 fun if_fixed_bvT' T = fixed_bvT ([], Term.domain_type T) |
|
59 fun if_fixed_bvT T = curry (fixed_bvT ([], Term.domain_type T)) |
|
60 fun if_full_fixed_bvT T = curry (fixed_bvT (Term.strip_type T)) |
|
61 |
|
62 fun dest_word_funT (Type ("fun", [T, U])) = (dest_wordT T, dest_wordT U) |
|
63 | dest_word_funT T = raise TYPE ("dest_word_funT", [T], []) |
|
64 fun dest_nat (@{term nat} $ n :: ts) = (snd (HOLogic.dest_number n), ts) |
|
65 | dest_nat ts = raise TERM ("dest_nat", ts) |
|
66 fun dest_nat_word_funT (T, ts) = |
|
67 (dest_word_funT (Term.range_type T), dest_nat ts) |
|
68 |
|
69 fun bv_extend n T ts = |
|
70 (case try dest_word_funT T of |
|
71 SOME (i, j) => if j-i >= 0 then SOME (index1 n (j-i), ts) else NONE |
|
72 | _ => NONE) |
|
73 |
|
74 fun bv_rotate n T ts = |
|
75 try dest_nat ts |
|
76 |> Option.map (fn (i, ts') => (index1 n i, ts')) |
|
77 |
|
78 fun bv_extract n T ts = |
|
79 try dest_nat_word_funT (T, ts) |
|
80 |> Option.map (fn ((_, i), (lb, ts')) => (index2 n (i + lb - 1) lb, ts')) |
|
81 |
|
82 |
|
83 fun conn @{const_name True} = SOME "true" |
|
84 | conn @{const_name False} = SOME "false" |
|
85 | conn @{const_name Not} = SOME "not" |
|
86 | conn @{const_name "op &"} = SOME "and" |
|
87 | conn @{const_name "op |"} = SOME "or" |
|
88 | conn @{const_name "op -->"} = SOME "implies" |
|
89 | conn @{const_name "op ="} = SOME "iff" |
|
90 | conn @{const_name If} = SOME "if_then_else" |
|
91 | conn _ = NONE |
|
92 |
|
93 fun pred @{const_name distinct} _ = SOME "distinct" |
|
94 | pred @{const_name "op ="} _ = SOME "=" |
|
95 | pred @{const_name term_eq} _ = SOME "=" |
|
96 | pred @{const_name less} T = |
|
97 if is_arith_type T then SOME "<" |
|
98 else if_fixed_bvT' T "bvult" |
|
99 | pred @{const_name less_eq} T = |
|
100 if is_arith_type T then SOME "<=" |
|
101 else if_fixed_bvT' T "bvule" |
|
102 | pred @{const_name word_sless} T = if_fixed_bvT' T "bvslt" |
|
103 | pred @{const_name word_sle} T = if_fixed_bvT' T "bvsle" |
|
104 | pred _ _ = NONE |
|
105 |
|
106 fun func @{const_name If} _ = just "ite" |
|
107 | func @{const_name uminus} T = |
|
108 if is_arith_type T then just "~" |
|
109 else if_fixed_bvT T "bvneg" |
|
110 | func @{const_name plus} T = |
|
111 if is_arith_type T then just "+" |
|
112 else if_fixed_bvT T "bvadd" |
|
113 | func @{const_name minus} T = |
|
114 if is_arith_type T then just "-" |
|
115 else if_fixed_bvT T "bvsub" |
|
116 | func @{const_name times} T = |
|
117 if is_arith_type T then just "*" |
|
118 else if_fixed_bvT T "bvmul" |
|
119 | func @{const_name bitNOT} T = if_fixed_bvT T "bvnot" |
|
120 | func @{const_name bitAND} T = if_fixed_bvT T "bvand" |
|
121 | func @{const_name bitOR} T = if_fixed_bvT T "bvor" |
|
122 | func @{const_name bitXOR} T = if_fixed_bvT T "bvxor" |
|
123 | func @{const_name div} T = if_fixed_bvT T "bvudiv" |
|
124 | func @{const_name mod} T = if_fixed_bvT T "bvurem" |
|
125 | func @{const_name sdiv} T = if_fixed_bvT T "bvsdiv" |
|
126 | func @{const_name smod} T = if_fixed_bvT T "bvsmod" |
|
127 | func @{const_name srem} T = if_fixed_bvT T "bvsrem" |
|
128 | func @{const_name word_cat} T = if_full_fixed_bvT T "concat" |
|
129 | func @{const_name bv_shl} T = if_full_fixed_bvT T "bvshl" |
|
130 | func @{const_name bv_lshr} T = if_full_fixed_bvT T "bvlshr" |
|
131 | func @{const_name bv_ashr} T = if_full_fixed_bvT T "bvashr" |
|
132 | func @{const_name slice} T = bv_extract "extract" T |
|
133 | func @{const_name ucast} T = bv_extend "zero_extend" T |
|
134 | func @{const_name scast} T = bv_extend "sign_extend" T |
|
135 | func @{const_name word_rotl} T = bv_rotate "rotate_left" T |
|
136 | func @{const_name word_rotr} T = bv_rotate "rotate_right" T |
|
137 | func _ _ = K NONE |
|
138 |
|
139 fun is_builtin_conn (n, T) = is_connT T andalso is_some (conn n) |
|
140 fun is_builtin_pred (n, T) = is_predT T andalso is_some (pred n T) |
|
141 |
|
142 fun builtin_fun (n, T) ts = |
|
143 if is_connT T then conn n |> Option.map (rpair ts) |
|
144 else if is_predT T then pred n T |> Option.map (rpair ts) |
|
145 else func n T ts |
|
146 |
|
147 |
|
148 |
|
149 (* serialization *) |
|
150 |
|
151 val add = Buffer.add |
|
152 fun sep f = add " " #> f |
|
153 fun enclose l r f = sep (add l #> f #> add r) |
|
154 val par = enclose "(" ")" |
|
155 fun app n f = (fn [] => sep (add n) | xs => par (add n #> fold f xs)) |
|
156 fun line f = f #> add "\n" |
|
157 |
|
158 fun var i = add "?v" #> add (string_of_int i) |
|
159 |
|
160 fun sterm l (T.SVar i) = sep (var (l - i - 1)) |
|
161 | sterm l (T.SApp (n, ts)) = app n (sterm l) ts |
|
162 | sterm _ (T.SLet _) = raise Fail "SMT-LIB: unsupported let expression" |
|
163 | sterm l (T.SQua (q, ss, ps, t)) = |
|
164 let |
|
165 val quant = add o (fn T.SForall => "forall" | T.SExists => "exists") |
|
166 val vs = map_index (apfst (Integer.add l)) ss |
|
167 fun var_decl (i, s) = par (var i #> sep (add s)) |
|
168 val sub = sterm (l + length ss) |
|
169 fun pat kind ts = sep (add kind #> enclose "{" " }" (fold sub ts)) |
|
170 fun pats (T.SPat ts) = pat ":pat" ts |
|
171 | pats (T.SNoPat ts) = pat ":nopat" ts |
|
172 in par (quant q #> fold var_decl vs #> sub t #> fold pats ps) end |
|
173 |
|
174 fun choose_logic theories = |
|
175 if member (op =) theories T.Bitvector then "QF_AUFBV" |
|
176 else if member (op =) theories T.Real then "AUFLIRA" |
|
177 else "AUFLIA" |
|
178 |
|
179 fun serialize comments {theories, sorts, funcs} ts = |
|
180 Buffer.empty |
|
181 |> line (add "(benchmark Isabelle") |
|
182 |> line (add ":status unknown") |
|
183 |> line (add ":logic " #> add (choose_logic theories)) |
|
184 |> length sorts > 0 ? |
|
185 line (add ":extrasorts" #> par (fold (sep o add) sorts)) |
|
186 |> length funcs > 0 ? ( |
|
187 line (add ":extrafuns" #> add " (") #> |
|
188 fold (fn (f, (ss, s)) => |
|
189 line (sep (app f (sep o add) (ss @ [s])))) funcs #> |
|
190 line (add ")")) |
|
191 |> fold (fn t => line (add ":assumption" #> sterm 0 t)) ts |
|
192 |> line (add ":formula true)") |
|
193 |> fold (fn str => line (add "; " #> add str)) comments |
|
194 |> Buffer.content |
|
195 |
|
196 |
|
197 |
|
198 (* interface *) |
|
199 |
|
200 fun interface comments = { |
|
201 prefixes = { |
|
202 sort_prefix = "S", |
|
203 func_prefix = "f"}, |
|
204 strict = SOME { |
|
205 is_builtin_conn = is_builtin_conn, |
|
206 is_builtin_pred = is_builtin_pred, |
|
207 is_builtin_distinct = true}, |
|
208 builtins = { |
|
209 builtin_typ = builtin_typ, |
|
210 builtin_num = builtin_num, |
|
211 builtin_fun = builtin_fun}, |
|
212 serialize = serialize comments} |
|
213 |
|
214 end |