author | boehmes |
Tue, 26 Oct 2010 11:39:26 +0200 | |
changeset 40161 | 539d07b00e5f |
parent 39483 | 9f0e5684f04b |
child 40162 | 7f58a9a843c2 |
permissions | -rw-r--r-- |
36898 | 1 |
(* Title: HOL/Tools/SMT/smt_normalize.ML |
2 |
Author: Sascha Boehme, TU Muenchen |
|
3 |
||
4 |
Normalization steps on theorems required by SMT solvers: |
|
5 |
* simplify trivial distincts (those with less than three elements), |
|
6 |
* rewrite bool case expressions as if expressions, |
|
7 |
* normalize numerals (e.g. replace negative numerals by negated positive |
|
8 |
numerals), |
|
9 |
* embed natural numbers into integers, |
|
10 |
* add extra rules specifying types and constants which occur frequently, |
|
11 |
* fully translate into object logic, add universal closure, |
|
39483
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
12 |
* monomorphize (create instances of schematic rules), |
36898 | 13 |
* lift lambda terms, |
14 |
* make applications explicit for functions with varying number of arguments. |
|
39483
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
15 |
* add (hypothetical definitions for) missing datatype selectors, |
36898 | 16 |
*) |
17 |
||
18 |
signature SMT_NORMALIZE = |
|
19 |
sig |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
20 |
type extra_norm = (int * thm) list -> Proof.context -> |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
21 |
(int * thm) list * Proof.context |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
22 |
val normalize: extra_norm -> bool -> (int * thm) list -> Proof.context -> |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
23 |
(int * thm) list * Proof.context |
36899
bcd6fce5bf06
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents:
36898
diff
changeset
|
24 |
val atomize_conv: Proof.context -> conv |
36898 | 25 |
val eta_expand_conv: (Proof.context -> conv) -> Proof.context -> conv |
26 |
end |
|
27 |
||
28 |
structure SMT_Normalize: SMT_NORMALIZE = |
|
29 |
struct |
|
30 |
||
31 |
infix 2 ?? |
|
32 |
fun (test ?? f) x = if test x then f x else x |
|
33 |
||
34 |
fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct |
|
35 |
fun if_true_conv c cv = if_conv c cv Conv.all_conv |
|
36 |
||
37 |
||
38 |
||
39 |
(* simplification of trivial distincts (distinct should have at least |
|
40 |
three elements in the argument list) *) |
|
41 |
||
42 |
local |
|
43 |
fun is_trivial_distinct (Const (@{const_name distinct}, _) $ t) = |
|
44 |
length (HOLogic.dest_list t) <= 2 |
|
45 |
| is_trivial_distinct _ = false |
|
46 |
||
37786
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
47 |
val thms = map mk_meta_eq @{lemma |
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
48 |
"distinct [] = True" |
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
49 |
"distinct [x] = True" |
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
50 |
"distinct [x, y] = (x ~= y)" |
36898 | 51 |
by simp_all} |
52 |
fun distinct_conv _ = |
|
36936
c52d1c130898
incorporated further conversions and conversionals, after some minor tuning;
wenzelm
parents:
36899
diff
changeset
|
53 |
if_true_conv is_trivial_distinct (Conv.rewrs_conv thms) |
36898 | 54 |
in |
55 |
fun trivial_distinct ctxt = |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
56 |
map (apsnd ((Term.exists_subterm is_trivial_distinct o Thm.prop_of) ?? |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
57 |
Conv.fconv_rule (Conv.top_conv distinct_conv ctxt))) |
36898 | 58 |
end |
59 |
||
60 |
||
61 |
||
62 |
(* rewrite bool case expressions as if expressions *) |
|
63 |
||
64 |
local |
|
65 |
val is_bool_case = (fn |
|
66 |
Const (@{const_name "bool.bool_case"}, _) $ _ $ _ $ _ => true |
|
67 |
| _ => false) |
|
68 |
||
37786
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
69 |
val thms = map mk_meta_eq @{lemma |
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
70 |
"(case P of True => x | False => y) = (if P then x else y)" |
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
71 |
"(case P of False => y | True => x) = (if P then x else y)" |
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
72 |
by simp_all} |
36936
c52d1c130898
incorporated further conversions and conversionals, after some minor tuning;
wenzelm
parents:
36899
diff
changeset
|
73 |
val unfold_conv = if_true_conv is_bool_case (Conv.rewrs_conv thms) |
36898 | 74 |
in |
75 |
fun rewrite_bool_cases ctxt = |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
76 |
map (apsnd ((Term.exists_subterm is_bool_case o Thm.prop_of) ?? |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
77 |
Conv.fconv_rule (Conv.top_conv (K unfold_conv) ctxt))) |
36898 | 78 |
end |
79 |
||
80 |
||
81 |
||
82 |
(* normalization of numerals: rewriting of negative integer numerals into |
|
83 |
positive numerals, Numeral0 into 0, Numeral1 into 1 *) |
|
84 |
||
85 |
local |
|
86 |
fun is_number_sort ctxt T = |
|
87 |
Sign.of_sort (ProofContext.theory_of ctxt) (T, @{sort number_ring}) |
|
88 |
||
89 |
fun is_strange_number ctxt (t as Const (@{const_name number_of}, _) $ _) = |
|
90 |
(case try HOLogic.dest_number t of |
|
91 |
SOME (T, i) => is_number_sort ctxt T andalso i < 2 |
|
92 |
| NONE => false) |
|
93 |
| is_strange_number _ _ = false |
|
94 |
||
95 |
val pos_numeral_ss = HOL_ss |
|
96 |
addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}] |
|
97 |
addsimps [@{thm Int.number_of_Pls}, @{thm Int.numeral_1_eq_1}] |
|
98 |
addsimps @{thms Int.pred_bin_simps} |
|
99 |
addsimps @{thms Int.normalize_bin_simps} |
|
100 |
addsimps @{lemma |
|
101 |
"Int.Min = - Int.Bit1 Int.Pls" |
|
102 |
"Int.Bit0 (- Int.Pls) = - Int.Pls" |
|
103 |
"Int.Bit0 (- k) = - Int.Bit0 k" |
|
104 |
"Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)" |
|
105 |
by simp_all (simp add: pred_def)} |
|
106 |
||
107 |
fun pos_conv ctxt = if_conv (is_strange_number ctxt) |
|
108 |
(Simplifier.rewrite (Simplifier.context ctxt pos_numeral_ss)) |
|
109 |
Conv.no_conv |
|
110 |
in |
|
111 |
fun normalize_numerals ctxt = |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
112 |
map (apsnd ((Term.exists_subterm (is_strange_number ctxt) o Thm.prop_of) ?? |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
113 |
Conv.fconv_rule (Conv.top_sweep_conv pos_conv ctxt))) |
36898 | 114 |
end |
115 |
||
116 |
||
117 |
||
118 |
(* embedding of standard natural number operations into integer operations *) |
|
119 |
||
120 |
local |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
121 |
val nat_embedding = map (pair ~1) @{lemma |
36898 | 122 |
"nat (int n) = n" |
123 |
"i >= 0 --> int (nat i) = i" |
|
124 |
"i < 0 --> int (nat i) = 0" |
|
125 |
by simp_all} |
|
126 |
||
127 |
val nat_rewriting = @{lemma |
|
128 |
"0 = nat 0" |
|
129 |
"1 = nat 1" |
|
130 |
"number_of i = nat (number_of i)" |
|
131 |
"int (nat 0) = 0" |
|
132 |
"int (nat 1) = 1" |
|
133 |
"a < b = (int a < int b)" |
|
134 |
"a <= b = (int a <= int b)" |
|
135 |
"Suc a = nat (int a + 1)" |
|
136 |
"a + b = nat (int a + int b)" |
|
137 |
"a - b = nat (int a - int b)" |
|
138 |
"a * b = nat (int a * int b)" |
|
139 |
"a div b = nat (int a div int b)" |
|
140 |
"a mod b = nat (int a mod int b)" |
|
141 |
"min a b = nat (min (int a) (int b))" |
|
142 |
"max a b = nat (max (int a) (int b))" |
|
143 |
"int (nat (int a + int b)) = int a + int b" |
|
144 |
"int (nat (int a * int b)) = int a * int b" |
|
145 |
"int (nat (int a div int b)) = int a div int b" |
|
146 |
"int (nat (int a mod int b)) = int a mod int b" |
|
147 |
"int (nat (min (int a) (int b))) = min (int a) (int b)" |
|
148 |
"int (nat (max (int a) (int b))) = max (int a) (int b)" |
|
149 |
by (simp_all add: nat_mult_distrib nat_div_distrib nat_mod_distrib |
|
150 |
int_mult[symmetric] zdiv_int[symmetric] zmod_int[symmetric])} |
|
151 |
||
152 |
fun on_positive num f x = |
|
153 |
(case try HOLogic.dest_number (Thm.term_of num) of |
|
154 |
SOME (_, i) => if i >= 0 then SOME (f x) else NONE |
|
155 |
| NONE => NONE) |
|
156 |
||
157 |
val cancel_int_nat_ss = HOL_ss |
|
158 |
addsimps [@{thm Nat_Numeral.nat_number_of}] |
|
159 |
addsimps [@{thm Nat_Numeral.int_nat_number_of}] |
|
160 |
addsimps @{thms neg_simps} |
|
161 |
||
162 |
fun cancel_int_nat_simproc _ ss ct = |
|
163 |
let |
|
164 |
val num = Thm.dest_arg (Thm.dest_arg ct) |
|
165 |
val goal = Thm.mk_binop @{cterm "op == :: int => _"} ct num |
|
166 |
val simpset = Simplifier.inherit_context ss cancel_int_nat_ss |
|
167 |
fun tac _ = Simplifier.simp_tac simpset 1 |
|
168 |
in on_positive num (Goal.prove_internal [] goal) tac end |
|
169 |
||
170 |
val nat_ss = HOL_ss |
|
171 |
addsimps nat_rewriting |
|
172 |
addsimprocs [Simplifier.make_simproc { |
|
173 |
name = "cancel_int_nat_num", lhss = [@{cpat "int (nat _)"}], |
|
174 |
proc = cancel_int_nat_simproc, identifier = [] }] |
|
175 |
||
176 |
fun conv ctxt = Simplifier.rewrite (Simplifier.context ctxt nat_ss) |
|
177 |
||
178 |
val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat})) |
|
179 |
val uses_nat_int = |
|
180 |
Term.exists_subterm (member (op aconv) [@{term int}, @{term nat}]) |
|
181 |
in |
|
182 |
fun nat_as_int ctxt = |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
183 |
map (apsnd ((uses_nat_type o Thm.prop_of) ?? Conv.fconv_rule (conv ctxt))) #> |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
184 |
exists (uses_nat_int o Thm.prop_of o snd) ?? append nat_embedding |
36898 | 185 |
end |
186 |
||
187 |
||
188 |
||
189 |
(* further normalizations: beta/eta, universal closure, atomize *) |
|
190 |
||
191 |
val eta_expand_eq = @{lemma "f == (%x. f x)" by (rule reflexive)} |
|
192 |
||
193 |
fun eta_expand_conv cv ctxt = |
|
194 |
Conv.rewr_conv eta_expand_eq then_conv Conv.abs_conv (cv o snd) ctxt |
|
195 |
||
196 |
local |
|
197 |
val eta_conv = eta_expand_conv |
|
198 |
||
36936
c52d1c130898
incorporated further conversions and conversionals, after some minor tuning;
wenzelm
parents:
36899
diff
changeset
|
199 |
fun keep_conv ctxt = Conv.binder_conv (norm_conv o snd) ctxt |
36898 | 200 |
and eta_binder_conv ctxt = Conv.arg_conv (eta_conv norm_conv ctxt) |
201 |
and keep_let_conv ctxt = Conv.combination_conv |
|
202 |
(Conv.arg_conv (norm_conv ctxt)) (Conv.abs_conv (norm_conv o snd) ctxt) |
|
203 |
and unfold_let_conv ctxt = Conv.combination_conv |
|
204 |
(Conv.arg_conv (norm_conv ctxt)) (eta_conv norm_conv ctxt) |
|
205 |
and unfold_conv thm ctxt = Conv.rewr_conv thm then_conv keep_conv ctxt |
|
206 |
and unfold_ex1_conv ctxt = unfold_conv @{thm Ex1_def} ctxt |
|
37786
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
207 |
and unfold_ball_conv ctxt = unfold_conv (mk_meta_eq @{thm Ball_def}) ctxt |
4eb98849c5c0
fixed handling of Ball/Bex: turn equalities into meta-equalities for the rewriting conversions;
boehmes
parents:
37153
diff
changeset
|
208 |
and unfold_bex_conv ctxt = unfold_conv (mk_meta_eq @{thm Bex_def}) ctxt |
36898 | 209 |
and norm_conv ctxt ct = |
210 |
(case Thm.term_of ct of |
|
211 |
Const (@{const_name All}, _) $ Abs _ => keep_conv |
|
212 |
| Const (@{const_name All}, _) $ _ => eta_binder_conv |
|
213 |
| Const (@{const_name All}, _) => eta_conv eta_binder_conv |
|
214 |
| Const (@{const_name Ex}, _) $ Abs _ => keep_conv |
|
215 |
| Const (@{const_name Ex}, _) $ _ => eta_binder_conv |
|
216 |
| Const (@{const_name Ex}, _) => eta_conv eta_binder_conv |
|
217 |
| Const (@{const_name Let}, _) $ _ $ Abs _ => keep_let_conv |
|
218 |
| Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv |
|
219 |
| Const (@{const_name Let}, _) $ _ => eta_conv unfold_let_conv |
|
220 |
| Const (@{const_name Let}, _) => eta_conv (eta_conv unfold_let_conv) |
|
221 |
| Const (@{const_name Ex1}, _) $ _ => unfold_ex1_conv |
|
222 |
| Const (@{const_name Ex1}, _) => eta_conv unfold_ex1_conv |
|
223 |
| Const (@{const_name Ball}, _) $ _ $ _ => unfold_ball_conv |
|
224 |
| Const (@{const_name Ball}, _) $ _ => eta_conv unfold_ball_conv |
|
225 |
| Const (@{const_name Ball}, _) => eta_conv (eta_conv unfold_ball_conv) |
|
226 |
| Const (@{const_name Bex}, _) $ _ $ _ => unfold_bex_conv |
|
227 |
| Const (@{const_name Bex}, _) $ _ => eta_conv unfold_bex_conv |
|
228 |
| Const (@{const_name Bex}, _) => eta_conv (eta_conv unfold_bex_conv) |
|
229 |
| Abs _ => Conv.abs_conv (norm_conv o snd) |
|
230 |
| _ $ _ => Conv.comb_conv o norm_conv |
|
231 |
| _ => K Conv.all_conv) ctxt ct |
|
232 |
||
233 |
fun is_normed t = |
|
234 |
(case t of |
|
235 |
Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed u |
|
236 |
| Const (@{const_name All}, _) $ _ => false |
|
237 |
| Const (@{const_name All}, _) => false |
|
238 |
| Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed u |
|
239 |
| Const (@{const_name Ex}, _) $ _ => false |
|
240 |
| Const (@{const_name Ex}, _) => false |
|
241 |
| Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) => |
|
242 |
is_normed u1 andalso is_normed u2 |
|
243 |
| Const (@{const_name Let}, _) $ _ $ _ => false |
|
244 |
| Const (@{const_name Let}, _) $ _ => false |
|
245 |
| Const (@{const_name Let}, _) => false |
|
246 |
| Const (@{const_name Ex1}, _) => false |
|
247 |
| Const (@{const_name Ball}, _) => false |
|
248 |
| Const (@{const_name Bex}, _) => false |
|
249 |
| Abs (_, _, u) => is_normed u |
|
250 |
| u1 $ u2 => is_normed u1 andalso is_normed u2 |
|
251 |
| _ => true) |
|
252 |
in |
|
253 |
fun norm_binder_conv ctxt = if_conv is_normed Conv.all_conv (norm_conv ctxt) |
|
254 |
end |
|
255 |
||
256 |
fun norm_def ctxt thm = |
|
257 |
(case Thm.prop_of thm of |
|
38864
4abe644fcea5
formerly unnamed infix equality now named HOL.eq
haftmann
parents:
37786
diff
changeset
|
258 |
@{term Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) => |
36898 | 259 |
norm_def ctxt (thm RS @{thm fun_cong}) |
260 |
| Const (@{const_name "=="}, _) $ _ $ Abs _ => |
|
261 |
norm_def ctxt (thm RS @{thm meta_eq_to_obj_eq}) |
|
262 |
| _ => thm) |
|
263 |
||
264 |
fun atomize_conv ctxt ct = |
|
265 |
(case Thm.term_of ct of |
|
266 |
@{term "op ==>"} $ _ $ _ => |
|
267 |
Conv.binop_conv (atomize_conv ctxt) then_conv |
|
268 |
Conv.rewr_conv @{thm atomize_imp} |
|
269 |
| Const (@{const_name "=="}, _) $ _ $ _ => |
|
270 |
Conv.binop_conv (atomize_conv ctxt) then_conv |
|
271 |
Conv.rewr_conv @{thm atomize_eq} |
|
272 |
| Const (@{const_name all}, _) $ Abs _ => |
|
36936
c52d1c130898
incorporated further conversions and conversionals, after some minor tuning;
wenzelm
parents:
36899
diff
changeset
|
273 |
Conv.binder_conv (atomize_conv o snd) ctxt then_conv |
36898 | 274 |
Conv.rewr_conv @{thm atomize_all} |
275 |
| _ => Conv.all_conv) ct |
|
276 |
||
277 |
fun normalize_rule ctxt = |
|
278 |
Conv.fconv_rule ( |
|
279 |
(* reduce lambda abstractions, except at known binders: *) |
|
280 |
Thm.beta_conversion true then_conv |
|
281 |
Thm.eta_conversion then_conv |
|
282 |
norm_binder_conv ctxt) #> |
|
283 |
norm_def ctxt #> |
|
284 |
Drule.forall_intr_vars #> |
|
285 |
Conv.fconv_rule (atomize_conv ctxt) |
|
286 |
||
287 |
||
288 |
||
289 |
(* lift lambda terms into additional rules *) |
|
290 |
||
291 |
local |
|
292 |
val meta_eq = @{cpat "op =="} |
|
293 |
val meta_eqT = hd (Thm.dest_ctyp (Thm.ctyp_of_term meta_eq)) |
|
294 |
fun inst_meta cT = Thm.instantiate_cterm ([(meta_eqT, cT)], []) meta_eq |
|
295 |
fun mk_meta_eq ct cu = Thm.mk_binop (inst_meta (Thm.ctyp_of_term ct)) ct cu |
|
296 |
||
297 |
fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt) |
|
298 |
||
299 |
fun used_vars cvs ct = |
|
300 |
let |
|
301 |
val lookup = AList.lookup (op aconv) (map (` Thm.term_of) cvs) |
|
302 |
val add = (fn SOME ct => insert (op aconvc) ct | _ => I) |
|
303 |
in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end |
|
304 |
||
305 |
fun apply cv thm = |
|
306 |
let val thm' = Thm.combination thm (Thm.reflexive cv) |
|
307 |
in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end |
|
308 |
fun apply_def cvs eq = Thm.symmetric (fold apply cvs eq) |
|
309 |
||
310 |
fun replace_lambda cvs ct (cx as (ctxt, defs)) = |
|
311 |
let |
|
312 |
val cvs' = used_vars cvs ct |
|
313 |
val ct' = fold_rev Thm.cabs cvs' ct |
|
314 |
in |
|
315 |
(case Termtab.lookup defs (Thm.term_of ct') of |
|
316 |
SOME eq => (apply_def cvs' eq, cx) |
|
317 |
| NONE => |
|
318 |
let |
|
319 |
val {T, ...} = Thm.rep_cterm ct' and n = Name.uu |
|
320 |
val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt |
|
321 |
val cu = mk_meta_eq (cert ctxt (Free (n', T))) ct' |
|
322 |
val (eq, ctxt'') = yield_singleton Assumption.add_assumes cu ctxt' |
|
323 |
val defs' = Termtab.update (Thm.term_of ct', eq) defs |
|
324 |
in (apply_def cvs' eq, (ctxt'', defs')) end) |
|
325 |
end |
|
326 |
||
327 |
fun none ct cx = (Thm.reflexive ct, cx) |
|
328 |
fun in_comb f g ct cx = |
|
329 |
let val (cu1, cu2) = Thm.dest_comb ct |
|
330 |
in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end |
|
331 |
fun in_arg f = in_comb none f |
|
332 |
fun in_abs f cvs ct (ctxt, defs) = |
|
333 |
let |
|
334 |
val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt |
|
335 |
val (cv, cu) = Thm.dest_abs (SOME n) ct |
|
336 |
in (ctxt', defs) |> f (cv :: cvs) cu |>> Thm.abstract_rule n cv end |
|
337 |
||
338 |
fun traverse cvs ct = |
|
339 |
(case Thm.term_of ct of |
|
340 |
Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs traverse cvs) |
|
341 |
| Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs traverse cvs) |
|
342 |
| Const (@{const_name Let}, _) $ _ $ Abs _ => |
|
343 |
in_comb (in_arg (traverse cvs)) (in_abs traverse cvs) |
|
344 |
| Abs _ => at_lambda cvs |
|
345 |
| _ $ _ => in_comb (traverse cvs) (traverse cvs) |
|
346 |
| _ => none) ct |
|
347 |
||
348 |
and at_lambda cvs ct = |
|
349 |
in_abs traverse cvs ct #-> (fn thm => |
|
350 |
replace_lambda cvs (Thm.rhs_of thm) #>> Thm.transitive thm) |
|
351 |
||
352 |
fun has_free_lambdas t = |
|
353 |
(case t of |
|
354 |
Const (@{const_name All}, _) $ Abs (_, _, u) => has_free_lambdas u |
|
355 |
| Const (@{const_name Ex}, _) $ Abs (_, _, u) => has_free_lambdas u |
|
356 |
| Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) => |
|
357 |
has_free_lambdas u1 orelse has_free_lambdas u2 |
|
358 |
| Abs _ => true |
|
359 |
| u1 $ u2 => has_free_lambdas u1 orelse has_free_lambdas u2 |
|
360 |
| _ => false) |
|
361 |
||
362 |
fun lift_lm f thm cx = |
|
363 |
if not (has_free_lambdas (Thm.prop_of thm)) then (thm, cx) |
|
364 |
else cx |> f (Thm.cprop_of thm) |>> (fn thm' => Thm.equal_elim thm' thm) |
|
365 |
in |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
366 |
fun lift_lambdas irules ctxt = |
36898 | 367 |
let |
368 |
val cx = (ctxt, Termtab.empty) |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
369 |
val (idxs, thms) = split_list irules |
36898 | 370 |
val (thms', (ctxt', defs)) = fold_map (lift_lm (traverse [])) thms cx |
371 |
val eqs = Termtab.fold (cons o normalize_rule ctxt' o snd) defs [] |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
372 |
in (map (pair ~1) eqs @ (idxs ~~ thms'), ctxt') end |
36898 | 373 |
end |
374 |
||
375 |
||
376 |
||
377 |
(* make application explicit for functions with varying number of arguments *) |
|
378 |
||
379 |
local |
|
380 |
val const = prefix "c" and free = prefix "f" |
|
381 |
fun min i (e as (_, j)) = if i <> j then (true, Int.min (i, j)) else e |
|
382 |
fun add t i = Symtab.map_default (t, (false, i)) (min i) |
|
383 |
fun traverse t = |
|
384 |
(case Term.strip_comb t of |
|
385 |
(Const (n, _), ts) => add (const n) (length ts) #> fold traverse ts |
|
386 |
| (Free (n, _), ts) => add (free n) (length ts) #> fold traverse ts |
|
387 |
| (Abs (_, _, u), ts) => fold traverse (u :: ts) |
|
388 |
| (_, ts) => fold traverse ts) |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
389 |
fun prune tab = Symtab.fold (fn (n, (true, i)) => |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
390 |
Symtab.update (n, i) | _ => I) tab Symtab.empty |
36898 | 391 |
|
392 |
fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2 |
|
393 |
fun nary_conv conv1 conv2 ct = |
|
394 |
(Conv.combination_conv (nary_conv conv1 conv2) conv2 else_conv conv1) ct |
|
395 |
fun abs_conv conv tb = Conv.abs_conv (fn (cv, cx) => |
|
396 |
let val n = fst (Term.dest_Free (Thm.term_of cv)) |
|
397 |
in conv (Symtab.update (free n, 0) tb) cx end) |
|
37153
8feed34275ce
renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents:
36936
diff
changeset
|
398 |
val fun_app_rule = @{lemma "f x == fun_app f x" by (simp add: fun_app_def)} |
36898 | 399 |
in |
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
400 |
fun explicit_application ctxt irules = |
36898 | 401 |
let |
402 |
fun sub_conv tb ctxt ct = |
|
403 |
(case Term.strip_comb (Thm.term_of ct) of |
|
404 |
(Const (n, _), ts) => app_conv tb (const n) (length ts) ctxt |
|
405 |
| (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt |
|
406 |
| (Abs _, _) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt) |
|
407 |
| (_, _) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct |
|
408 |
and app_conv tb n i ctxt = |
|
409 |
(case Symtab.lookup tb n of |
|
410 |
NONE => nary_conv Conv.all_conv (sub_conv tb ctxt) |
|
37153
8feed34275ce
renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents:
36936
diff
changeset
|
411 |
| SOME j => fun_app_conv tb ctxt (i - j)) |
8feed34275ce
renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents:
36936
diff
changeset
|
412 |
and fun_app_conv tb ctxt i ct = ( |
36898 | 413 |
if i = 0 then nary_conv Conv.all_conv (sub_conv tb ctxt) |
414 |
else |
|
37153
8feed34275ce
renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents:
36936
diff
changeset
|
415 |
Conv.rewr_conv fun_app_rule then_conv |
8feed34275ce
renamed constant "apply" to "fun_app" (which is closer to the related "fun_upd")
boehmes
parents:
36936
diff
changeset
|
416 |
binop_conv (fun_app_conv tb ctxt (i-1)) (sub_conv tb ctxt)) ct |
36898 | 417 |
|
418 |
fun needs_exp_app tab = Term.exists_subterm (fn |
|
419 |
Bound _ $ _ => true |
|
420 |
| Const (n, _) => Symtab.defined tab (const n) |
|
421 |
| Free (n, _) => Symtab.defined tab (free n) |
|
422 |
| _ => false) |
|
423 |
||
424 |
fun rewrite tab ctxt thm = |
|
425 |
if not (needs_exp_app tab (Thm.prop_of thm)) then thm |
|
426 |
else Conv.fconv_rule (sub_conv tab ctxt) thm |
|
427 |
||
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
428 |
val tab = prune (fold (traverse o Thm.prop_of o snd) irules Symtab.empty) |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
429 |
in map (apsnd (rewrite tab ctxt)) irules end |
36898 | 430 |
end |
431 |
||
432 |
||
433 |
||
39483
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
434 |
(* add missing datatype selectors via hypothetical definitions *) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
435 |
|
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
436 |
local |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
437 |
val add = (fn Type (n, _) => Symtab.update (n, ()) | _ => I) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
438 |
|
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
439 |
fun collect t = |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
440 |
(case Term.strip_comb t of |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
441 |
(Abs (_, T, t), _) => add T #> collect t |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
442 |
| (Const (_, T), ts) => collects T ts |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
443 |
| (Free (_, T), ts) => collects T ts |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
444 |
| _ => I) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
445 |
and collects T ts = |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
446 |
let val ((Ts, Us), U) = Term.strip_type T |> apfst (chop (length ts)) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
447 |
in fold add Ts #> add (Us ---> U) #> fold collect ts end |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
448 |
|
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
449 |
fun add_constructors thy n = |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
450 |
(case Datatype.get_info thy n of |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
451 |
NONE => I |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
452 |
| SOME {descr, ...} => fold (fn (_, (_, _, cs)) => fold (fn (n, ds) => |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
453 |
fold (insert (op =) o pair n) (1 upto length ds)) cs) descr) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
454 |
|
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
455 |
fun add_selector (c as (n, i)) ctxt = |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
456 |
(case Datatype_Selectors.lookup_selector ctxt c of |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
457 |
SOME _ => ctxt |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
458 |
| NONE => |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
459 |
let |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
460 |
val T = Sign.the_const_type (ProofContext.theory_of ctxt) n |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
461 |
val U = Term.body_type T --> nth (Term.binder_types T) (i-1) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
462 |
in |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
463 |
ctxt |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
464 |
|> yield_singleton Variable.variant_fixes Name.uu |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
465 |
|>> pair ((n, T), i) o rpair U |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
466 |
|-> Context.proof_map o Datatype_Selectors.add_selector |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
467 |
end) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
468 |
in |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
469 |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
470 |
fun datatype_selectors irules ctxt = |
39483
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
471 |
let |
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
472 |
val ns = Symtab.keys (fold (collect o Thm.prop_of o snd) irules Symtab.empty) |
39483
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
473 |
val cs = fold (add_constructors (ProofContext.theory_of ctxt)) ns [] |
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
474 |
in (irules, fold add_selector cs ctxt) end |
39483
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
475 |
(* FIXME: also generate hypothetical definitions for the selectors *) |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
476 |
|
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
477 |
end |
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
478 |
|
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
479 |
|
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
480 |
|
36898 | 481 |
(* combined normalization *) |
482 |
||
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
483 |
type extra_norm = (int * thm) list -> Proof.context -> |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
484 |
(int * thm) list * Proof.context |
36898 | 485 |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
486 |
fun with_context f irules ctxt = (f ctxt irules, ctxt) |
36898 | 487 |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
488 |
fun normalize extra_norm with_datatypes irules ctxt = |
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
489 |
irules |
36898 | 490 |
|> trivial_distinct ctxt |
491 |
|> rewrite_bool_cases ctxt |
|
492 |
|> normalize_numerals ctxt |
|
493 |
|> nat_as_int ctxt |
|
494 |
|> rpair ctxt |
|
495 |
|-> extra_norm |
|
40161
539d07b00e5f
keep track of theorems initially given to SMT (even if they are rewritten); provide interface to filter theorems necessary for SMT proofs
boehmes
parents:
39483
diff
changeset
|
496 |
|-> with_context (fn cx => map (apsnd (normalize_rule cx))) |
36898 | 497 |
|-> SMT_Monomorph.monomorph |
498 |
|-> lift_lambdas |
|
499 |
|-> with_context explicit_application |
|
39483
9f0e5684f04b
add full support for datatypes to the SMT interface (only used by Z3 in oracle mode so far); added store to keep track of datatype selector functions
boehmes
parents:
38864
diff
changeset
|
500 |
|-> (if with_datatypes then datatype_selectors else pair) |
36898 | 501 |
|
502 |
end |