author | wenzelm |
Sat, 03 Feb 2001 15:22:57 +0100 | |
changeset 11045 | 971a50fda146 |
parent 10892 | 405b077433a3 |
child 11707 | 6c45813c2db1 |
permissions | -rw-r--r-- |
7145 | 1 |
(* Title: HOL/Tools/svc_funcs.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson |
|
4 |
Copyright 1999 University of Cambridge |
|
5 |
||
7285 | 6 |
Translation functions for the interface to SVC |
7145 | 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 = |
|
7285 | 25 |
Buildin of string * expr list |
26 |
| Interp of string * expr list |
|
27 |
| UnInterp of string * expr list |
|
28 |
| FalseExpr |
|
29 |
| TrueExpr |
|
30 |
| Int of int |
|
31 |
| Rat of int * int; |
|
7145 | 32 |
|
33 |
open BasisLibrary |
|
34 |
||
7285 | 35 |
fun signedInt i = |
36 |
if i < 0 then "-" ^ Int.toString (~i) |
|
37 |
else Int.toString i; |
|
38 |
||
39 |
fun is_intnat T = T = HOLogic.intT orelse T = HOLogic.natT; |
|
40 |
||
41 |
fun is_numeric T = is_intnat T orelse T = HOLogic.realT; |
|
42 |
||
43 |
fun is_numeric_op T = is_numeric (domain_type T); |
|
44 |
||
7145 | 45 |
fun toString t = |
7285 | 46 |
let fun ue (Buildin(s, l)) = |
7145 | 47 |
"(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") " |
7285 | 48 |
| ue (Interp(s, l)) = |
7145 | 49 |
"{" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ "} " |
7285 | 50 |
| ue (UnInterp(s, l)) = |
7145 | 51 |
"(" ^ s ^ (foldl (fn (a, b) => a ^ " " ^ (ue b)) ("", l)) ^ ") " |
7285 | 52 |
| ue (FalseExpr) = "FALSE " |
53 |
| ue (TrueExpr) = "TRUE " |
|
54 |
| ue (Int i) = (signedInt i) ^ " " |
|
55 |
| ue (Rat(i, j)) = (signedInt i) ^ "|" ^ (signedInt j) ^ " " |
|
7145 | 56 |
in |
57 |
ue t |
|
58 |
end; |
|
59 |
||
60 |
fun valid e = |
|
61 |
let val svc_home = getenv "SVC_HOME" |
|
62 |
val svc_machine = getenv "SVC_MACHINE" |
|
63 |
val check_valid = if svc_home = "" |
|
64 |
then error "Environment variable SVC_HOME not set" |
|
65 |
else if svc_machine = "" |
|
66 |
then error "Environment variable SVC_MACHINE not set" |
|
67 |
else svc_home ^ "/" ^ svc_machine ^ "/bin/check_valid" |
|
68 |
val svc_input = toString e |
|
69 |
val _ = if !trace then writeln ("Calling SVC:\n" ^ svc_input) else () |
|
70 |
val svc_input_file = File.tmp_path (Path.basic "SVM_in"); |
|
71 |
val svc_output_file = File.tmp_path (Path.basic "SVM_out"); |
|
72 |
val _ = (File.write svc_input_file svc_input; |
|
73 |
execute (check_valid ^ " -dump-result " ^ |
|
7164 | 74 |
File.sysify_path svc_output_file ^ |
75 |
" " ^ File.sysify_path svc_input_file ^ |
|
7145 | 76 |
"> /dev/null 2>&1")) |
10892 | 77 |
val svc_output = |
78 |
(case Library.try File.read svc_output_file of |
|
79 |
Some out => out |
|
80 |
| None => error "SVC returned no output"); |
|
7145 | 81 |
in |
7285 | 82 |
if ! trace then writeln ("SVC Returns:\n" ^ svc_output) |
83 |
else (File.rm svc_input_file; File.rm svc_output_file); |
|
7145 | 84 |
String.isPrefix "VALID" svc_output |
85 |
end |
|
86 |
||
87 |
(*New exception constructor for passing arguments to the oracle*) |
|
88 |
exception OracleExn of term; |
|
89 |
||
7164 | 90 |
fun apply c args = |
91 |
let val (ts, bs) = ListPair.unzip args |
|
92 |
in (list_comb(c,ts), exists I bs) end; |
|
93 |
||
7285 | 94 |
(*Determining whether the biconditionals must be unfolded: if there are |
7164 | 95 |
int or nat comparisons below*) |
96 |
val iff_tag = |
|
97 |
let fun tag t = |
|
98 |
let val (c,ts) = strip_comb t |
|
99 |
in case c of |
|
100 |
Const("op &", _) => apply c (map tag ts) |
|
101 |
| Const("op |", _) => apply c (map tag ts) |
|
102 |
| Const("op -->", _) => apply c (map tag ts) |
|
103 |
| Const("Not", _) => apply c (map tag ts) |
|
104 |
| Const("True", _) => (c, false) |
|
105 |
| Const("False", _) => (c, false) |
|
106 |
| Const("op =", Type ("fun", [T,_])) => |
|
107 |
if T = HOLogic.boolT then |
|
108 |
(*biconditional: with int/nat comparisons below?*) |
|
109 |
let val [t1,t2] = ts |
|
110 |
val (u1,b1) = tag t1 |
|
111 |
and (u2,b2) = tag t2 |
|
112 |
val cname = if b1 orelse b2 then "unfold" else "keep" |
|
113 |
in |
|
114 |
(Const ("SVC_Oracle.iff_" ^ cname, dummyT) $ u1 $ u2, |
|
115 |
b1 orelse b2) |
|
116 |
end |
|
7285 | 117 |
else (*might be numeric equality*) (t, is_intnat T) |
7164 | 118 |
| Const("op <", Type ("fun", [T,_])) => (t, is_intnat T) |
119 |
| Const("op <=", Type ("fun", [T,_])) => (t, is_intnat T) |
|
120 |
| _ => (t, false) |
|
121 |
end |
|
122 |
in #1 o tag end; |
|
123 |
||
124 |
(*Map expression e to 0<=a --> e, where "a" is the name of a nat variable*) |
|
125 |
fun add_nat_var (a, e) = |
|
7285 | 126 |
Buildin("=>", [Buildin("<=", [Int 0, UnInterp (a, [])]), |
127 |
e]); |
|
7164 | 128 |
|
7545
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
129 |
fun param_string [] = "" |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
130 |
| param_string is = "_" ^ space_implode "_" (map string_of_int is) |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
131 |
|
7145 | 132 |
(*Translate an Isabelle formula into an SVC expression |
133 |
pos ["positive"]: true if an assumption, false if a goal*) |
|
134 |
fun expr_of pos t = |
|
135 |
let |
|
7164 | 136 |
val params = rev (rename_wrt_term t (Term.strip_all_vars t)) |
137 |
and body = Term.strip_all_body t |
|
138 |
val nat_vars = ref ([] : string list) |
|
139 |
(*translation of a variable: record all natural numbers*) |
|
7545
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
140 |
fun trans_var (a,T,is) = |
7164 | 141 |
(if T = HOLogic.natT then nat_vars := (a ins_string (!nat_vars)) |
142 |
else (); |
|
7545
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
143 |
UnInterp (a ^ param_string is, [])) |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
144 |
(*A variable, perhaps applied to a series of parameters*) |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
145 |
fun var (Free(a,T), is) = trans_var ("F_" ^ a, T, is) |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
146 |
| var (Var((a, 0), T), is) = trans_var (a, T, is) |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
147 |
| var (Bound i, is) = |
7164 | 148 |
let val (a,T) = List.nth (params, i) |
7545
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
149 |
in trans_var ("B_" ^ a, T, is) end |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
150 |
| var (t $ Bound i, is) = var(t,i::is) |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
151 |
(*removing a parameter from a Var: the bound var index will |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
152 |
become part of the Var's name*) |
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
153 |
| var (t,_) = raise OracleExn t; |
7164 | 154 |
(*translation of a literal*) |
10892 | 155 |
fun lit (Const("Numeral.number_of", _) $ w) = |
156 |
(HOLogic.dest_binum w handle TERM _ => raise Match) |
|
7164 | 157 |
| lit (Const("0", _)) = 0 |
158 |
| lit (Const("RealDef.0r", _)) = 0 |
|
159 |
| lit (Const("RealDef.1r", _)) = 1 |
|
160 |
(*translation of a literal expression [no variables]*) |
|
7285 | 161 |
fun litExp (Const("op +", T) $ x $ y) = |
162 |
if is_numeric_op T then (litExp x) + (litExp y) |
|
163 |
else raise OracleExn t |
|
164 |
| litExp (Const("op -", T) $ x $ y) = |
|
165 |
if is_numeric_op T then (litExp x) - (litExp y) |
|
166 |
else raise OracleExn t |
|
167 |
| litExp (Const("op *", T) $ x $ y) = |
|
168 |
if is_numeric_op T then (litExp x) * (litExp y) |
|
169 |
else raise OracleExn t |
|
170 |
| litExp (Const("uminus", T) $ x) = |
|
171 |
if is_numeric_op T then ~(litExp x) |
|
172 |
else raise OracleExn t |
|
7164 | 173 |
| litExp t = lit t |
7285 | 174 |
handle Match => raise OracleExn t |
7164 | 175 |
(*translation of a real/rational expression*) |
7285 | 176 |
fun suc t = Interp("+", [Int 1, t]) |
7164 | 177 |
fun tm (Const("Suc", T) $ x) = suc (tm x) |
7285 | 178 |
| tm (Const("op +", T) $ x $ y) = |
179 |
if is_numeric_op T then Interp("+", [tm x, tm y]) |
|
180 |
else raise OracleExn t |
|
181 |
| tm (Const("op -", T) $ x $ y) = |
|
182 |
if is_numeric_op T then |
|
183 |
Interp("+", [tm x, Interp("*", [Int ~1, tm y])]) |
|
184 |
else raise OracleExn t |
|
185 |
| tm (Const("op *", T) $ x $ y) = |
|
186 |
if is_numeric_op T then Interp("*", [tm x, tm y]) |
|
187 |
else raise OracleExn t |
|
188 |
| tm (Const("RealDef.rinv", T) $ x) = |
|
189 |
if domain_type T = HOLogic.realT then |
|
190 |
Rat(1, litExp x) |
|
191 |
else raise OracleExn t |
|
192 |
| tm (Const("uminus", T) $ x) = |
|
193 |
if is_numeric_op T then Interp("*", [Int ~1, tm x]) |
|
194 |
else raise OracleExn t |
|
195 |
| tm t = Int (lit t) |
|
7545
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
196 |
handle Match => var (t,[]) |
7164 | 197 |
(*translation of a formula*) |
198 |
and fm pos (Const("op &", _) $ p $ q) = |
|
7285 | 199 |
Buildin("AND", [fm pos p, fm pos q]) |
7164 | 200 |
| fm pos (Const("op |", _) $ p $ q) = |
7285 | 201 |
Buildin("OR", [fm pos p, fm pos q]) |
7164 | 202 |
| fm pos (Const("op -->", _) $ p $ q) = |
7285 | 203 |
Buildin("=>", [fm (not pos) p, fm pos q]) |
7164 | 204 |
| fm pos (Const("Not", _) $ p) = |
7285 | 205 |
Buildin("NOT", [fm (not pos) p]) |
206 |
| fm pos (Const("True", _)) = TrueExpr |
|
207 |
| fm pos (Const("False", _)) = FalseExpr |
|
7164 | 208 |
| fm pos (Const("SVC_Oracle.iff_keep", _) $ p $ q) = |
209 |
(*polarity doesn't matter*) |
|
7285 | 210 |
Buildin("=", [fm pos p, fm pos q]) |
7164 | 211 |
| fm pos (Const("SVC_Oracle.iff_unfold", _) $ p $ q) = |
7285 | 212 |
Buildin("AND", (*unfolding uses both polarities*) |
213 |
[Buildin("=>", [fm (not pos) p, fm pos q]), |
|
214 |
Buildin("=>", [fm (not pos) q, fm pos p])]) |
|
7164 | 215 |
| fm pos (t as Const("op =", Type ("fun", [T,_])) $ x $ y) = |
216 |
let val tx = tm x and ty = tm y |
|
217 |
in if pos orelse T = HOLogic.realT then |
|
7285 | 218 |
Buildin("=", [tx, ty]) |
7164 | 219 |
else if is_intnat T then |
7285 | 220 |
Buildin("AND", |
221 |
[Buildin("<", [tx, suc ty]), |
|
222 |
Buildin("<", [ty, suc tx])]) |
|
7164 | 223 |
else raise OracleExn t |
224 |
end |
|
225 |
(*inequalities: possible types are nat, int, real*) |
|
226 |
| fm pos (t as Const("op <", Type ("fun", [T,_])) $ x $ y) = |
|
227 |
if not pos orelse T = HOLogic.realT then |
|
7285 | 228 |
Buildin("<", [tm x, tm y]) |
7164 | 229 |
else if is_intnat T then |
7285 | 230 |
Buildin("<=", [suc (tm x), tm y]) |
7164 | 231 |
else raise OracleExn t |
232 |
| fm pos (t as Const("op <=", Type ("fun", [T,_])) $ x $ y) = |
|
233 |
if pos orelse T = HOLogic.realT then |
|
7285 | 234 |
Buildin("<=", [tm x, tm y]) |
7164 | 235 |
else if is_intnat T then |
7285 | 236 |
Buildin("<", [tm x, suc (tm y)]) |
7164 | 237 |
else raise OracleExn t |
7545
1578f1fd62cf
fixed SOUNDNESS BUG concerning the map from terms like ?f x y to SVC variables
paulson
parents:
7285
diff
changeset
|
238 |
| fm pos t = var(t,[]); |
7164 | 239 |
(*entry point, and translation of a meta-formula*) |
240 |
fun mt pos ((c as Const("Trueprop", _)) $ p) = fm pos (iff_tag p) |
|
241 |
| mt pos ((c as Const("==>", _)) $ p $ q) = |
|
7285 | 242 |
Buildin("=>", [mt (not pos) p, mt pos q]) |
7164 | 243 |
| mt pos t = fm pos (iff_tag t) (*it might be a formula*) |
244 |
||
245 |
val body_e = mt pos body (*evaluate now to assign into !nat_vars*) |
|
7145 | 246 |
in |
7164 | 247 |
foldr add_nat_var (!nat_vars, body_e) |
7145 | 248 |
end; |
249 |
||
250 |
||
7285 | 251 |
(*The oracle proves the given formula t, if possible*) |
252 |
fun oracle (sign, OracleExn t) = |
|
253 |
let val dummy = if !trace then writeln ("Subgoal abstracted to\n" ^ |
|
254 |
Sign.string_of_term sign t) |
|
7164 | 255 |
else () |
256 |
in |
|
7285 | 257 |
if valid (expr_of false t) then t |
258 |
else raise OracleExn t |
|
7164 | 259 |
end; |
7145 | 260 |
|
261 |
end; |