author | boehmes |
Wed, 15 Dec 2010 08:39:24 +0100 | |
changeset 41126 | e0bd443c0fdd |
parent 41124 | 1de17a2de5ad |
child 41127 | 2ea84c8535c6 |
permissions | -rw-r--r-- |
36898 | 1 |
(* Title: HOL/Tools/SMT/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 |
|
41124
1de17a2de5ad
moved SMT classes and dictionary functions to SMT_Utils
boehmes
parents:
41072
diff
changeset
|
9 |
val smtlibC: SMT_Utils.class |
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
10 |
val add_logic: int * (term list -> string option) -> Context.generic -> |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
11 |
Context.generic |
41072
9f9bc1bdacef
be more flexible: store SMT built-in symbols in generic contexts (not in theory contexts)
boehmes
parents:
41059
diff
changeset
|
12 |
val interface: SMT_Solver.interface |
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
13 |
val setup: theory -> theory |
36898 | 14 |
end |
15 |
||
16 |
structure SMTLIB_Interface: SMTLIB_INTERFACE = |
|
17 |
struct |
|
18 |
||
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
19 |
structure B = SMT_Builtin |
36898 | 20 |
structure N = SMT_Normalize |
21 |
structure T = SMT_Translate |
|
22 |
||
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
23 |
val smtlibC = ["smtlib"] |
36898 | 24 |
|
25 |
||
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
26 |
(* builtins *) |
36898 | 27 |
|
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
28 |
local |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
29 |
fun int_num _ i = SOME (string_of_int i) |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
30 |
|
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
31 |
fun distinct _ _ [t] = |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
32 |
(case try HOLogic.dest_list t of |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
33 |
SOME (ts as _ :: _) => SOME ("distinct", ts) |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
34 |
| _ => NONE) |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
35 |
| distinct _ _ _ = NONE |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
36 |
in |
36898 | 37 |
|
41072
9f9bc1bdacef
be more flexible: store SMT built-in symbols in generic contexts (not in theory contexts)
boehmes
parents:
41059
diff
changeset
|
38 |
val setup_builtins = |
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
39 |
B.add_builtin_typ smtlibC (@{typ int}, K (SOME "Int"), int_num) #> |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
40 |
fold (B.add_builtin_fun' smtlibC) [ |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
41 |
(@{const True}, "true"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
42 |
(@{const False}, "false"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
43 |
(* FIXME: we do not test if these constants are fully applied! *) |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
44 |
(@{const Not}, "not"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
45 |
(@{const HOL.conj}, "and"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
46 |
(@{const HOL.disj}, "or"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
47 |
(@{const HOL.implies}, "implies"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
48 |
(@{const HOL.eq (bool)}, "iff"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
49 |
(@{const HOL.eq ('a)}, "="), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
50 |
(@{const If (bool)}, "if_then_else"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
51 |
(@{const If ('a)}, "ite"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
52 |
(@{const less (int)}, "<"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
53 |
(@{const less_eq (int)}, "<="), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
54 |
(@{const uminus (int)}, "~"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
55 |
(@{const plus (int)}, "+"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
56 |
(@{const minus (int)}, "-"), |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
57 |
(@{const times (int)}, "*") ] #> |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
58 |
B.add_builtin_fun smtlibC (Term.dest_Const @{const distinct ('a)}, distinct) |
36898 | 59 |
|
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
60 |
end |
36898 | 61 |
|
62 |
||
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
63 |
(* serialization *) |
36898 | 64 |
|
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
65 |
(** header **) |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
66 |
|
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
67 |
fun fst_int_ord ((i1, _), (i2, _)) = int_ord (i1, i2) |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
68 |
|
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
69 |
structure Logics = Generic_Data |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
70 |
( |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
71 |
type T = (int * (term list -> string option)) list |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
72 |
val empty = [] |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
73 |
val extend = I |
39687 | 74 |
fun merge (bs1, bs2) = Ord_List.union fst_int_ord bs2 bs1 |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
75 |
) |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
76 |
|
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
77 |
fun add_logic pf = Logics.map (Ord_List.insert fst_int_ord pf) |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
78 |
|
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
79 |
fun choose_logic ctxt ts = |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
80 |
let |
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
81 |
fun choose [] = "AUFLIA" |
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
82 |
| choose ((_, f) :: fs) = (case f ts of SOME s => s | NONE => choose fs) |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
83 |
in [":logic " ^ choose (Logics.get (Context.Proof ctxt))] end |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
84 |
|
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
85 |
|
41072
9f9bc1bdacef
be more flexible: store SMT built-in symbols in generic contexts (not in theory contexts)
boehmes
parents:
41059
diff
changeset
|
86 |
(** serialization **) |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
87 |
|
36898 | 88 |
val add = Buffer.add |
89 |
fun sep f = add " " #> f |
|
90 |
fun enclose l r f = sep (add l #> f #> add r) |
|
91 |
val par = enclose "(" ")" |
|
92 |
fun app n f = (fn [] => sep (add n) | xs => par (add n #> fold f xs)) |
|
93 |
fun line f = f #> add "\n" |
|
94 |
||
95 |
fun var i = add "?v" #> add (string_of_int i) |
|
96 |
||
97 |
fun sterm l (T.SVar i) = sep (var (l - i - 1)) |
|
98 |
| sterm l (T.SApp (n, ts)) = app n (sterm l) ts |
|
99 |
| sterm _ (T.SLet _) = raise Fail "SMT-LIB: unsupported let expression" |
|
40664 | 100 |
| sterm l (T.SQua (q, ss, ps, w, t)) = |
36898 | 101 |
let |
102 |
val quant = add o (fn T.SForall => "forall" | T.SExists => "exists") |
|
103 |
val vs = map_index (apfst (Integer.add l)) ss |
|
104 |
fun var_decl (i, s) = par (var i #> sep (add s)) |
|
105 |
val sub = sterm (l + length ss) |
|
106 |
fun pat kind ts = sep (add kind #> enclose "{" " }" (fold sub ts)) |
|
107 |
fun pats (T.SPat ts) = pat ":pat" ts |
|
108 |
| pats (T.SNoPat ts) = pat ":nopat" ts |
|
40664 | 109 |
fun weight NONE = I |
110 |
| weight (SOME i) = |
|
111 |
sep (add ":weight { " #> add (string_of_int i) #> add " }") |
|
112 |
in |
|
113 |
par (quant q #> fold var_decl vs #> sub t #> fold pats ps #> weight w) |
|
114 |
end |
|
36898 | 115 |
|
37155
e3f18cfc9829
sort signature in SMT-LIB output (improves sharing of SMT certificates: goals of the same logical structure are translated into equal SMT-LIB benchmarks)
boehmes
parents:
36936
diff
changeset
|
116 |
fun ssort sorts = sort fast_string_ord sorts |
e3f18cfc9829
sort signature in SMT-LIB output (improves sharing of SMT certificates: goals of the same logical structure are translated into equal SMT-LIB benchmarks)
boehmes
parents:
36936
diff
changeset
|
117 |
fun fsort funcs = sort (prod_ord fast_string_ord (K EQUAL)) funcs |
e3f18cfc9829
sort signature in SMT-LIB output (improves sharing of SMT certificates: goals of the same logical structure are translated into equal SMT-LIB benchmarks)
boehmes
parents:
36936
diff
changeset
|
118 |
|
39298
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
119 |
fun sdatatypes decls = |
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
120 |
let |
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
121 |
fun con (n, []) = add n |
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
122 |
| con (n, sels) = par (add n #> |
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
123 |
fold (fn (n, s) => sep (par (add n #> sep (add s)))) sels) |
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
124 |
fun dtyp (n, decl) = add n #> fold (sep o con) decl |
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
125 |
in line (add ":datatypes " #> par (fold (par o dtyp) decls)) end |
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
126 |
|
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
127 |
fun serialize comments {header, sorts, dtyps, funcs} ts = |
36898 | 128 |
Buffer.empty |
129 |
|> line (add "(benchmark Isabelle") |
|
130 |
|> line (add ":status unknown") |
|
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
131 |
|> fold (line o add) header |
36898 | 132 |
|> length sorts > 0 ? |
37155
e3f18cfc9829
sort signature in SMT-LIB output (improves sharing of SMT certificates: goals of the same logical structure are translated into equal SMT-LIB benchmarks)
boehmes
parents:
36936
diff
changeset
|
133 |
line (add ":extrasorts" #> par (fold (sep o add) (ssort sorts))) |
39298
5aefb5bc8a93
added preliminary support for SMT datatypes (for now restricted to tuples and lists); only the Z3 interface (in oracle mode) makes use of it, there is especially no Z3 proof reconstruction support for datatypes yet
boehmes
parents:
38864
diff
changeset
|
134 |
|> fold sdatatypes dtyps |
36898 | 135 |
|> length funcs > 0 ? ( |
136 |
line (add ":extrafuns" #> add " (") #> |
|
137 |
fold (fn (f, (ss, s)) => |
|
37155
e3f18cfc9829
sort signature in SMT-LIB output (improves sharing of SMT certificates: goals of the same logical structure are translated into equal SMT-LIB benchmarks)
boehmes
parents:
36936
diff
changeset
|
138 |
line (sep (app f (sep o add) (ss @ [s])))) (fsort funcs) #> |
36898 | 139 |
line (add ")")) |
140 |
|> fold (fn t => line (add ":assumption" #> sterm 0 t)) ts |
|
141 |
|> line (add ":formula true)") |
|
142 |
|> fold (fn str => line (add "; " #> add str)) comments |
|
143 |
|> Buffer.content |
|
144 |
||
145 |
||
41072
9f9bc1bdacef
be more flexible: store SMT built-in symbols in generic contexts (not in theory contexts)
boehmes
parents:
41059
diff
changeset
|
146 |
(* interface *) |
36898 | 147 |
|
148 |
val interface = { |
|
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
149 |
class = smtlibC, |
36898 | 150 |
translate = { |
151 |
prefixes = { |
|
152 |
sort_prefix = "S", |
|
153 |
func_prefix = "f"}, |
|
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
154 |
header = choose_logic, |
41059
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
155 |
is_fol = true, |
d2b1fc1b8e19
centralized handling of built-in types and constants;
boehmes
parents:
41057
diff
changeset
|
156 |
has_datatypes = false, |
36898 | 157 |
serialize = serialize}} |
158 |
||
41072
9f9bc1bdacef
be more flexible: store SMT built-in symbols in generic contexts (not in theory contexts)
boehmes
parents:
41059
diff
changeset
|
159 |
val setup = Context.theory_map setup_builtins |
9f9bc1bdacef
be more flexible: store SMT built-in symbols in generic contexts (not in theory contexts)
boehmes
parents:
41059
diff
changeset
|
160 |
|
36898 | 161 |
end |