|
7145
|
1 |
(* Title: HOL/Tools/svc_funcs.ML
|
|
|
2 |
ID: $Id$
|
|
|
3 |
Author: Lawrence C Paulson
|
|
|
4 |
Copyright 1999 University of Cambridge
|
|
|
5 |
|
|
|
6 |
Translation and abstraction functions for the interface to SVC
|
|
|
7 |
|
|
|
8 |
Based upon the work of Søren T. Heilmann
|
|
|
9 |
|
|
7164
|
10 |
Integers and naturals are translated as follows:
|
|
|
11 |
In a positive context, replace x<y by x+1<=y
|
|
|
12 |
In a negative context, replace x<=y by x<y+1
|
|
|
13 |
In a negative context, replace x=y by x<y+1 & y<x+1
|
|
|
14 |
Biconditionals (if-and-only-iff) are expanded if they require such translations
|
|
|
15 |
in either operand.
|
|
7145
|
16 |
|
|
7164
|
17 |
For each variable of type nat, an assumption is added that it is non-negative.
|
|
|
18 |
*)
|
|
7145
|
19 |
|
|
|
20 |
structure Svc =
|
|
|
21 |
struct
|
|
|
22 |
val trace = ref false;
|
|
|
23 |
|
|
|
24 |
datatype expr =
|
|
|
25 |
bracketed_expr of expr
|
|
|
26 |
| ref_def_expr of string * expr
|
|
|
27 |
| ref_expr of string
|
|
|
28 |
| typed_expr of Type * expr
|
|
|
29 |
| buildin_expr of string * expr list
|
|
|
30 |
| interp_expr of string * expr list
|
|
|
31 |
| uninterp_expr of string * expr list
|
|
|
32 |
| false_expr
|
|
|
33 |
| true_expr
|
|
|
34 |
| distinct_expr of string
|
|
|
35 |
| int_expr of int
|
|
|
36 |
| rat_expr of int * int
|
|
|
37 |
and Type =
|
|
|
38 |
simple_type of string
|
|
|
39 |
| array_type of Type * Type
|
|
|
40 |
| record_type of (expr * Type) list
|
|
|
41 |
| bitvec_type of int;
|
|
|
42 |
|
|
|
43 |
open BasisLibrary
|
|
|
44 |
|
|
|
45 |
fun toString t =
|
|
|
46 |
let fun signedInt i =
|
|
|
47 |
if i < 0 then "-" ^ Int.toString (~i)
|
|
|
48 |
else Int.toString i
|
|
|
49 |
fun ut(simple_type s) = s ^ " "
|
|
|
50 |
| ut(array_type(t1, t2)) = "ARRAY " ^ (ut t1) ^ (ut t2)
|
|
|
51 |
| ut(record_type fl) =
|
|
|
52 |
"RECORD" ^
|
|
|
53 |
(foldl (fn (a, (d, t)) => a ^ (ue d) ^ (ut t)) (" ", fl))
|
|
|
54 |
| ut(bitvec_type n) = "BITVEC " ^ (Int.toString n) ^ " "
|
|
|
55 |
and ue(bracketed_expr e) = "(" ^ (ue e) ^ ") "
|
|
|
56 |
| ue(ref_def_expr(r, e)) = "$" ^ r ^ ":" ^ (ue e)
|
|
|
57 |
| ue(ref_expr r) = "$" ^ r ^ " "
|
|
|
58 |
| ue(typed_expr(t, e)) = (ut t) ^ (ue e)
|
|
|
59 |
| ue(buildin_expr(s, l)) =
|
|
|
60 |
"(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
|
|
|
61 |
| ue(interp_expr(s, l)) =
|
|
|
62 |
"{" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ "} "
|
|
|
63 |
| ue(uninterp_expr(s, l)) =
|
|
|
64 |
"(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") "
|
|
|
65 |
| ue(false_expr) = "FALSE "
|
|
|
66 |
| ue(true_expr) = "TRUE "
|
|
|
67 |
| ue(distinct_expr s) = "@" ^ s ^ " "
|
|
|
68 |
| ue(int_expr i) = (signedInt i) ^ " "
|
|
|
69 |
| ue(rat_expr(i, j)) = (signedInt i) ^ "|" ^ (signedInt j) ^ " "
|
|
|
70 |
in
|
|
|
71 |
ue t
|
|
|
72 |
end;
|
|
|
73 |
|
|
|
74 |
fun valid e =
|
|
|
75 |
let val svc_home = getenv "SVC_HOME"
|
|
|
76 |
val svc_machine = getenv "SVC_MACHINE"
|
|
|
77 |
val check_valid = if svc_home = ""
|
|
|
78 |
then error "Environment variable SVC_HOME not set"
|
|
|
79 |
else if svc_machine = ""
|
|
|
80 |
then error "Environment variable SVC_MACHINE not set"
|
|
|
81 |
else svc_home ^ "/" ^ svc_machine ^ "/bin/check_valid"
|
|
|
82 |
val svc_input = toString e
|
|
|
83 |
val _ = if !trace then writeln ("Calling SVC:\n" ^ svc_input) else ()
|
|
|
84 |
val svc_input_file = File.tmp_path (Path.basic "SVM_in");
|
|
|
85 |
val svc_output_file = File.tmp_path (Path.basic "SVM_out");
|
|
|
86 |
val _ = (File.write svc_input_file svc_input;
|
|
|
87 |
execute (check_valid ^ " -dump-result " ^
|
|
7164
|
88 |
File.sysify_path svc_output_file ^
|
|
|
89 |
" " ^ File.sysify_path svc_input_file ^
|
|
7145
|
90 |
"> /dev/null 2>&1"))
|
|
|
91 |
val svc_output = File.read svc_output_file
|
|
|
92 |
handle _ => error "SVC returned no output"
|
|
|
93 |
in
|
|
7232
|
94 |
if ! trace then writeln ("SVC Returns:\n" ^ svc_output) else ();
|
|
|
95 |
if not (! trace) then (File.rm svc_input_file; File.rm svc_output_file) else ();
|
|
7145
|
96 |
String.isPrefix "VALID" svc_output
|
|
|
97 |
end
|
|
|
98 |
|
|
|
99 |
(*New exception constructor for passing arguments to the oracle*)
|
|
|
100 |
exception OracleExn of term;
|
|
|
101 |
|
|
7164
|
102 |
fun apply c args =
|
|
|
103 |
let val (ts, bs) = ListPair.unzip args
|
|
|
104 |
in (list_comb(c,ts), exists I bs) end;
|
|
|
105 |
|
|
|
106 |
fun is_intnat T = T = HOLogic.intT orelse T = HOLogic.natT;
|
|
|
107 |
|
|
|
108 |
(*Determining whether the biconditionals must be unfoled: if there are
|
|
|
109 |
int or nat comparisons below*)
|
|
|
110 |
val iff_tag =
|
|
|
111 |
let fun tag t =
|
|
|
112 |
let val (c,ts) = strip_comb t
|
|
|
113 |
in case c of
|
|
|
114 |
Const("op &", _) => apply c (map tag ts)
|
|
|
115 |
| Const("op |", _) => apply c (map tag ts)
|
|
|
116 |
| Const("op -->", _) => apply c (map tag ts)
|
|
|
117 |
| Const("Not", _) => apply c (map tag ts)
|
|
|
118 |
| Const("True", _) => (c, false)
|
|
|
119 |
| Const("False", _) => (c, false)
|
|
|
120 |
| Const("op =", Type ("fun", [T,_])) =>
|
|
|
121 |
if T = HOLogic.boolT then
|
|
|
122 |
(*biconditional: with int/nat comparisons below?*)
|
|
|
123 |
let val [t1,t2] = ts
|
|
|
124 |
val (u1,b1) = tag t1
|
|
|
125 |
and (u2,b2) = tag t2
|
|
|
126 |
val cname = if b1 orelse b2 then "unfold" else "keep"
|
|
|
127 |
in
|
|
|
128 |
(Const ("SVC_Oracle.iff_" ^ cname, dummyT) $ u1 $ u2,
|
|
|
129 |
b1 orelse b2)
|
|
|
130 |
end
|
|
|
131 |
else (*numeric equality*) (t, is_intnat T)
|
|
|
132 |
| Const("op <", Type ("fun", [T,_])) => (t, is_intnat T)
|
|
|
133 |
| Const("op <=", Type ("fun", [T,_])) => (t, is_intnat T)
|
|
|
134 |
| _ => (t, false)
|
|
|
135 |
end
|
|
|
136 |
in #1 o tag end;
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
(*Map expression e to 0<=a --> e, where "a" is the name of a nat variable*)
|
|
|
140 |
fun add_nat_var (a, e) =
|
|
|
141 |
buildin_expr("=>", [buildin_expr("<=", [int_expr 0,
|
|
|
142 |
uninterp_expr (a, [])]),
|
|
|
143 |
e]);
|
|
|
144 |
|
|
7145
|
145 |
(*Translate an Isabelle formula into an SVC expression
|
|
|
146 |
pos ["positive"]: true if an assumption, false if a goal*)
|
|
|
147 |
fun expr_of pos t =
|
|
|
148 |
let
|
|
7164
|
149 |
val params = rev (rename_wrt_term t (Term.strip_all_vars t))
|
|
|
150 |
and body = Term.strip_all_body t
|
|
|
151 |
val nat_vars = ref ([] : string list)
|
|
|
152 |
(*translation of a variable: record all natural numbers*)
|
|
|
153 |
fun trans_var (a,T) =
|
|
|
154 |
(if T = HOLogic.natT then nat_vars := (a ins_string (!nat_vars))
|
|
|
155 |
else ();
|
|
|
156 |
uninterp_expr (a, []))
|
|
|
157 |
fun var (Free(a,T)) = trans_var ("F_" ^ a, T)
|
|
|
158 |
| var (Var((a, 0), T)) = trans_var (a, T)
|
|
|
159 |
| var (Bound i) =
|
|
|
160 |
let val (a,T) = List.nth (params, i)
|
|
|
161 |
in trans_var ("B_" ^ a, T) end
|
|
|
162 |
| var (t $ Bound _) = var t (*removing a parameter from a Var*)
|
|
|
163 |
| var t = raise OracleExn t;
|
|
|
164 |
(*translation of a literal*)
|
|
|
165 |
fun lit (Const("Numeral.number_of", _) $ w) = NumeralSyntax.dest_bin w
|
|
|
166 |
| lit (Const("0", _)) = 0
|
|
|
167 |
| lit (Const("RealDef.0r", _)) = 0
|
|
|
168 |
| lit (Const("RealDef.1r", _)) = 1
|
|
|
169 |
(*translation of a literal expression [no variables]*)
|
|
|
170 |
fun litExp (Const("op +", T) $ x $ y) = (litExp x) + (litExp y)
|
|
|
171 |
| litExp (Const("op -", T) $ x $ y) = (litExp x) - (litExp y)
|
|
|
172 |
| litExp (Const("op *", T) $ x $ y) = (litExp x) * (litExp y)
|
|
|
173 |
| litExp (Const("uminus", _) $ x) = ~(litExp x)
|
|
|
174 |
| litExp t = lit t
|
|
|
175 |
handle Match => raise OracleExn t
|
|
|
176 |
(*translation of a real/rational expression*)
|
|
|
177 |
fun suc t = interp_expr("+", [int_expr 1, t])
|
|
|
178 |
fun tm (Const("Suc", T) $ x) = suc (tm x)
|
|
|
179 |
| tm (Const("op +", T) $ x $ y) = interp_expr("+", [tm x, tm y])
|
|
|
180 |
| tm (Const("op -", _) $ x $ y) =
|
|
|
181 |
interp_expr("+", [tm x, interp_expr("*", [int_expr ~1, tm y])])
|
|
|
182 |
| tm (Const("op *", _) $ x $ y) = interp_expr("*", [tm x, tm y])
|
|
|
183 |
| tm (Const("op /", _) $ x $ y) =
|
|
|
184 |
interp_expr("*", [tm x, rat_expr(1, litExp y)])
|
|
|
185 |
| tm (Const("uminus", _) $ x) = interp_expr("*", [int_expr ~1, tm x])
|
|
|
186 |
| tm t = int_expr (lit t)
|
|
|
187 |
handle Match => var t
|
|
|
188 |
(*translation of a formula*)
|
|
|
189 |
and fm pos (Const("op &", _) $ p $ q) =
|
|
|
190 |
buildin_expr("AND", [fm pos p, fm pos q])
|
|
|
191 |
| fm pos (Const("op |", _) $ p $ q) =
|
|
|
192 |
buildin_expr("OR", [fm pos p, fm pos q])
|
|
|
193 |
| fm pos (Const("op -->", _) $ p $ q) =
|
|
|
194 |
buildin_expr("=>", [fm (not pos) p, fm pos q])
|
|
|
195 |
| fm pos (Const("Not", _) $ p) =
|
|
|
196 |
buildin_expr("NOT", [fm (not pos) p])
|
|
|
197 |
| fm pos (Const("True", _)) = true_expr
|
|
|
198 |
| fm pos (Const("False", _)) = false_expr
|
|
|
199 |
| fm pos (Const("SVC_Oracle.iff_keep", _) $ p $ q) =
|
|
|
200 |
(*polarity doesn't matter*)
|
|
|
201 |
buildin_expr("=", [fm pos p, fm pos q])
|
|
|
202 |
| fm pos (Const("SVC_Oracle.iff_unfold", _) $ p $ q) =
|
|
|
203 |
buildin_expr("AND", (*unfolding uses both polarities*)
|
|
|
204 |
[buildin_expr("=>", [fm (not pos) p, fm pos q]),
|
|
|
205 |
buildin_expr("=>", [fm (not pos) q, fm pos p])])
|
|
|
206 |
| fm pos (t as Const("op =", Type ("fun", [T,_])) $ x $ y) =
|
|
|
207 |
let val tx = tm x and ty = tm y
|
|
|
208 |
in if pos orelse T = HOLogic.realT then
|
|
|
209 |
buildin_expr("=", [tx, ty])
|
|
|
210 |
else if is_intnat T then
|
|
|
211 |
buildin_expr("AND",
|
|
|
212 |
[buildin_expr("<", [tx, suc ty]),
|
|
|
213 |
buildin_expr("<", [ty, suc tx])])
|
|
|
214 |
else raise OracleExn t
|
|
|
215 |
end
|
|
|
216 |
(*inequalities: possible types are nat, int, real*)
|
|
|
217 |
| fm pos (t as Const("op <", Type ("fun", [T,_])) $ x $ y) =
|
|
|
218 |
if not pos orelse T = HOLogic.realT then
|
|
|
219 |
buildin_expr("<", [tm x, tm y])
|
|
|
220 |
else if is_intnat T then
|
|
|
221 |
buildin_expr("<=", [suc (tm x), tm y])
|
|
|
222 |
else raise OracleExn t
|
|
|
223 |
| fm pos (t as Const("op <=", Type ("fun", [T,_])) $ x $ y) =
|
|
|
224 |
if pos orelse T = HOLogic.realT then
|
|
|
225 |
buildin_expr("<=", [tm x, tm y])
|
|
|
226 |
else if is_intnat T then
|
|
|
227 |
buildin_expr("<", [tm x, suc (tm y)])
|
|
|
228 |
else raise OracleExn t
|
|
|
229 |
| fm pos t = var t;
|
|
|
230 |
(*entry point, and translation of a meta-formula*)
|
|
|
231 |
fun mt pos ((c as Const("Trueprop", _)) $ p) = fm pos (iff_tag p)
|
|
|
232 |
| mt pos ((c as Const("==>", _)) $ p $ q) =
|
|
|
233 |
buildin_expr("=>", [mt (not pos) p, mt pos q])
|
|
|
234 |
| mt pos t = fm pos (iff_tag t) (*it might be a formula*)
|
|
|
235 |
|
|
|
236 |
val body_e = mt pos body (*evaluate now to assign into !nat_vars*)
|
|
7145
|
237 |
in
|
|
7164
|
238 |
foldr add_nat_var (!nat_vars, body_e)
|
|
7145
|
239 |
end;
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
(*Generalize an Isabelle formula, replacing by Vars
|
|
7164
|
243 |
all subterms not intelligible to SVC.
|
|
|
244 |
Do not present "raw" terms to expr_of; the translation could be unsound!*)
|
|
7145
|
245 |
fun abstract t =
|
|
|
246 |
let
|
|
7164
|
247 |
val params = Term.strip_all_vars t
|
|
|
248 |
and body = Term.strip_all_body t
|
|
|
249 |
val Us = map #2 params
|
|
|
250 |
val nPar = length params
|
|
|
251 |
val vname = ref "V_a"
|
|
|
252 |
val pairs = ref ([] : (term*term) list)
|
|
|
253 |
fun insert t =
|
|
|
254 |
let val T = fastype_of t
|
|
|
255 |
val v = Unify.combound (Var ((!vname,0), Us--->T),
|
|
|
256 |
0, nPar)
|
|
|
257 |
in vname := bump_string (!vname);
|
|
|
258 |
pairs := (t, v) :: !pairs;
|
|
|
259 |
v
|
|
|
260 |
end;
|
|
|
261 |
fun replace t =
|
|
|
262 |
case t of
|
|
|
263 |
Free _ => t (*but not existing Vars, lest the names clash*)
|
|
|
264 |
| Bound _ => t
|
|
|
265 |
| _ => (case gen_assoc (op aconv) (!pairs, t) of
|
|
|
266 |
Some v => v
|
|
|
267 |
| None => insert t)
|
|
|
268 |
(*abstraction of a real/rational expression*)
|
|
|
269 |
fun rat ((c as Const("op +", _)) $ x $ y) = c $ (rat x) $ (rat y)
|
|
|
270 |
| rat ((c as Const("op -", _)) $ x $ y) = c $ (rat x) $ (rat y)
|
|
|
271 |
| rat ((c as Const("op /", _)) $ x $ y) = c $ (rat x) $ (rat y)
|
|
|
272 |
| rat ((c as Const("op *", _)) $ x $ y) = c $ (rat x) $ (rat y)
|
|
|
273 |
| rat ((c as Const("uminus", _)) $ x) = c $ (rat x)
|
|
|
274 |
| rat ((c as Const("RealDef.0r", _))) = c
|
|
|
275 |
| rat ((c as Const("RealDef.1r", _))) = c
|
|
|
276 |
| rat (t as Const("Numeral.number_of", _) $ w) = t
|
|
|
277 |
| rat t = replace t
|
|
|
278 |
(*abstraction of an integer expression: no div, mod*)
|
|
|
279 |
fun int ((c as Const("op +", _)) $ x $ y) = c $ (int x) $ (int y)
|
|
|
280 |
| int ((c as Const("op -", _)) $ x $ y) = c $ (int x) $ (int y)
|
|
|
281 |
| int ((c as Const("op *", _)) $ x $ y) = c $ (int x) $ (int y)
|
|
|
282 |
| int ((c as Const("uminus", _)) $ x) = c $ (int x)
|
|
|
283 |
| int (t as Const("Numeral.number_of", _) $ w) = t
|
|
|
284 |
| int t = replace t
|
|
|
285 |
(*abstraction of a natural number expression: no minus*)
|
|
|
286 |
fun nat ((c as Const("op +", _)) $ x $ y) = c $ (nat x) $ (nat y)
|
|
|
287 |
| nat ((c as Const("op *", _)) $ x $ y) = c $ (nat x) $ (nat y)
|
|
|
288 |
| nat ((c as Const("Suc", _)) $ x) = c $ (nat x)
|
|
|
289 |
| nat (t as Const("0", _)) = t
|
|
|
290 |
| nat (t as Const("Numeral.number_of", _) $ w) = t
|
|
|
291 |
| nat t = replace t
|
|
|
292 |
(*abstraction of a relation: =, <, <=*)
|
|
|
293 |
fun rel (T, c $ x $ y) =
|
|
|
294 |
if T = HOLogic.realT then c $ (rat x) $ (rat y)
|
|
|
295 |
else if T = HOLogic.intT then c $ (int x) $ (int y)
|
|
|
296 |
else if T = HOLogic.natT then c $ (nat x) $ (nat y)
|
|
|
297 |
else if T = HOLogic.boolT then c $ (fm x) $ (fm y)
|
|
|
298 |
else replace (c $ x $ y) (*non-numeric comparison*)
|
|
|
299 |
(*abstraction of a formula*)
|
|
|
300 |
and fm ((c as Const("op &", _)) $ p $ q) = c $ (fm p) $ (fm q)
|
|
|
301 |
| fm ((c as Const("op |", _)) $ p $ q) = c $ (fm p) $ (fm q)
|
|
|
302 |
| fm ((c as Const("op -->", _)) $ p $ q) = c $ (fm p) $ (fm q)
|
|
|
303 |
| fm ((c as Const("Not", _)) $ p) = c $ (fm p)
|
|
|
304 |
| fm ((c as Const("True", _))) = c
|
|
|
305 |
| fm ((c as Const("False", _))) = c
|
|
|
306 |
| fm (t as Const("op =", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
|
|
|
307 |
| fm (t as Const("op <", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
|
|
|
308 |
| fm (t as Const("op <=", Type ("fun", [T,_])) $ x $ y) = rel (T, t)
|
|
|
309 |
| fm t = replace t
|
|
|
310 |
(*entry point, and abstraction of a meta-formula*)
|
|
|
311 |
fun mt ((c as Const("Trueprop", _)) $ p) = c $ (fm p)
|
|
|
312 |
| mt ((c as Const("==>", _)) $ p $ q) = c $ (mt p) $ (mt q)
|
|
|
313 |
| mt t = fm t (*it might be a formula*)
|
|
7145
|
314 |
in (list_all (params, mt body), !pairs) end;
|
|
|
315 |
|
|
7164
|
316 |
(*The oracle proves not the original formula but the abstracted version*)
|
|
|
317 |
fun oracle (sign, OracleExn P) =
|
|
|
318 |
let val (absP, _) = abstract P
|
|
|
319 |
val dummy = if !trace then writeln ("Subgoal abstracted to\n" ^
|
|
|
320 |
Sign.string_of_term sign absP)
|
|
|
321 |
else ()
|
|
|
322 |
in
|
|
|
323 |
if valid (expr_of false absP) then absP
|
|
|
324 |
else raise OracleExn P
|
|
|
325 |
end;
|
|
7145
|
326 |
|
|
|
327 |
end;
|