1 (* Title: HOL/SMT/Tools/smt_normalize.ML |
|
2 Author: Sascha Boehme, TU Muenchen |
|
3 |
|
4 Normalization steps on theorems required by SMT solvers: |
|
5 * unfold trivial let expressions, |
|
6 * simplify trivial distincts (those with less than three elements), |
|
7 * rewrite bool case expressions as if expressions, |
|
8 * normalize numerals (e.g. replace negative numerals by negated positive |
|
9 numerals), |
|
10 * embed natural numbers into integers, |
|
11 * add extra rules specifying types and constants which occur frequently, |
|
12 * fully translate into object logic, add universal closure, |
|
13 * lift lambda terms, |
|
14 * make applications explicit for functions with varying number of arguments. |
|
15 *) |
|
16 |
|
17 signature SMT_NORMALIZE = |
|
18 sig |
|
19 val normalize: thm list -> Proof.context -> thm list * Proof.context |
|
20 end |
|
21 |
|
22 structure SMT_Normalize: SMT_NORMALIZE = |
|
23 struct |
|
24 |
|
25 infix 2 ?? |
|
26 fun (test ?? f) x = if test x then f x else x |
|
27 |
|
28 fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct |
|
29 fun if_true_conv c cv = if_conv c cv Conv.all_conv |
|
30 |
|
31 |
|
32 |
|
33 (* simplification of trivial distincts (distinct should have at least |
|
34 three elements in the argument list) *) |
|
35 |
|
36 local |
|
37 fun is_trivial_distinct (Const (@{const_name distinct}, _) $ t) = |
|
38 length (HOLogic.dest_list t) <= 2 |
|
39 | is_trivial_distinct _ = false |
|
40 |
|
41 val thms = @{lemma |
|
42 "distinct [] == True" |
|
43 "distinct [x] == True" |
|
44 "distinct [x, y] == (x ~= y)" |
|
45 by simp_all} |
|
46 fun distinct_conv _ = |
|
47 if_true_conv is_trivial_distinct (More_Conv.rewrs_conv thms) |
|
48 in |
|
49 fun trivial_distinct ctxt = |
|
50 map ((Term.exists_subterm is_trivial_distinct o Thm.prop_of) ?? |
|
51 Conv.fconv_rule (More_Conv.top_conv distinct_conv ctxt)) |
|
52 end |
|
53 |
|
54 |
|
55 |
|
56 (* rewrite bool case expressions as if expressions *) |
|
57 |
|
58 local |
|
59 val is_bool_case = (fn |
|
60 Const (@{const_name "bool.bool_case"}, _) $ _ $ _ $ _ => true |
|
61 | _ => false) |
|
62 |
|
63 val thms = @{lemma |
|
64 "(case P of True => x | False => y) == (if P then x else y)" |
|
65 "(case P of False => y | True => x) == (if P then x else y)" |
|
66 by (rule eq_reflection, simp)+} |
|
67 val unfold_conv = if_true_conv is_bool_case (More_Conv.rewrs_conv thms) |
|
68 in |
|
69 fun rewrite_bool_cases ctxt = |
|
70 map ((Term.exists_subterm is_bool_case o Thm.prop_of) ?? |
|
71 Conv.fconv_rule (More_Conv.top_conv (K unfold_conv) ctxt)) |
|
72 end |
|
73 |
|
74 |
|
75 |
|
76 (* normalization of numerals: rewriting of negative integer numerals into |
|
77 positive numerals, Numeral0 into 0, Numeral1 into 1 *) |
|
78 |
|
79 local |
|
80 fun is_number_sort ctxt T = |
|
81 Sign.of_sort (ProofContext.theory_of ctxt) (T, @{sort number_ring}) |
|
82 |
|
83 fun is_strange_number ctxt (t as Const (@{const_name number_of}, _) $ _) = |
|
84 (case try HOLogic.dest_number t of |
|
85 SOME (T, i) => is_number_sort ctxt T andalso i < 2 |
|
86 | NONE => false) |
|
87 | is_strange_number _ _ = false |
|
88 |
|
89 val pos_numeral_ss = HOL_ss |
|
90 addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}] |
|
91 addsimps [@{thm Int.number_of_Pls}, @{thm Int.numeral_1_eq_1}] |
|
92 addsimps @{thms Int.pred_bin_simps} |
|
93 addsimps @{thms Int.normalize_bin_simps} |
|
94 addsimps @{lemma |
|
95 "Int.Min = - Int.Bit1 Int.Pls" |
|
96 "Int.Bit0 (- Int.Pls) = - Int.Pls" |
|
97 "Int.Bit0 (- k) = - Int.Bit0 k" |
|
98 "Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)" |
|
99 by simp_all (simp add: pred_def)} |
|
100 |
|
101 fun pos_conv ctxt = if_conv (is_strange_number ctxt) |
|
102 (Simplifier.rewrite (Simplifier.context ctxt pos_numeral_ss)) |
|
103 Conv.no_conv |
|
104 in |
|
105 fun normalize_numerals ctxt = |
|
106 map ((Term.exists_subterm (is_strange_number ctxt) o Thm.prop_of) ?? |
|
107 Conv.fconv_rule (More_Conv.top_sweep_conv pos_conv ctxt)) |
|
108 end |
|
109 |
|
110 |
|
111 |
|
112 (* embedding of standard natural number operations into integer operations *) |
|
113 |
|
114 local |
|
115 val nat_embedding = @{lemma |
|
116 "nat (int n) = n" |
|
117 "i >= 0 --> int (nat i) = i" |
|
118 "i < 0 --> int (nat i) = 0" |
|
119 by simp_all} |
|
120 |
|
121 val nat_rewriting = @{lemma |
|
122 "0 = nat 0" |
|
123 "1 = nat 1" |
|
124 "number_of i = nat (number_of i)" |
|
125 "int (nat 0) = 0" |
|
126 "int (nat 1) = 1" |
|
127 "a < b = (int a < int b)" |
|
128 "a <= b = (int a <= int b)" |
|
129 "Suc a = nat (int a + 1)" |
|
130 "a + b = nat (int a + int b)" |
|
131 "a - b = nat (int a - int b)" |
|
132 "a * b = nat (int a * int b)" |
|
133 "a div b = nat (int a div int b)" |
|
134 "a mod b = nat (int a mod int b)" |
|
135 "min a b = nat (min (int a) (int b))" |
|
136 "max a b = nat (max (int a) (int b))" |
|
137 "int (nat (int a + int b)) = int a + int b" |
|
138 "int (nat (int a * int b)) = int a * int b" |
|
139 "int (nat (int a div int b)) = int a div int b" |
|
140 "int (nat (int a mod int b)) = int a mod int b" |
|
141 "int (nat (min (int a) (int b))) = min (int a) (int b)" |
|
142 "int (nat (max (int a) (int b))) = max (int a) (int b)" |
|
143 by (simp add: nat_mult_distrib nat_div_distrib nat_mod_distrib |
|
144 int_mult[symmetric] zdiv_int[symmetric] zmod_int[symmetric])+} |
|
145 |
|
146 fun on_positive num f x = |
|
147 (case try HOLogic.dest_number (Thm.term_of num) of |
|
148 SOME (_, i) => if i >= 0 then SOME (f x) else NONE |
|
149 | NONE => NONE) |
|
150 |
|
151 val cancel_int_nat_ss = HOL_ss |
|
152 addsimps [@{thm Nat_Numeral.nat_number_of}] |
|
153 addsimps [@{thm Nat_Numeral.int_nat_number_of}] |
|
154 addsimps @{thms neg_simps} |
|
155 |
|
156 fun cancel_int_nat_simproc _ ss ct = |
|
157 let |
|
158 val num = Thm.dest_arg (Thm.dest_arg ct) |
|
159 val goal = Thm.mk_binop @{cterm "op == :: int => _"} ct num |
|
160 val simpset = Simplifier.inherit_context ss cancel_int_nat_ss |
|
161 fun tac _ = Simplifier.simp_tac simpset 1 |
|
162 in on_positive num (Goal.prove_internal [] goal) tac end |
|
163 |
|
164 val nat_ss = HOL_ss |
|
165 addsimps nat_rewriting |
|
166 addsimprocs [Simplifier.make_simproc { |
|
167 name = "cancel_int_nat_num", lhss = [@{cpat "int (nat _)"}], |
|
168 proc = cancel_int_nat_simproc, identifier = [] }] |
|
169 |
|
170 fun conv ctxt = Simplifier.rewrite (Simplifier.context ctxt nat_ss) |
|
171 |
|
172 val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat})) |
|
173 val uses_nat_int = |
|
174 Term.exists_subterm (member (op aconv) [@{term int}, @{term nat}]) |
|
175 in |
|
176 fun nat_as_int ctxt = |
|
177 map ((uses_nat_type o Thm.prop_of) ?? Conv.fconv_rule (conv ctxt)) #> |
|
178 exists (uses_nat_int o Thm.prop_of) ?? append nat_embedding |
|
179 end |
|
180 |
|
181 |
|
182 |
|
183 (* unfold definitions of specific constants *) |
|
184 |
|
185 local |
|
186 fun mk_entry (t as Const (n, _)) thm = ((n, t), thm) |
|
187 | mk_entry t _ = raise TERM ("mk_entry", [t]) |
|
188 fun prepare_def thm = |
|
189 (case Thm.prop_of thm of |
|
190 Const (@{const_name "=="}, _) $ t $ _ => mk_entry (Term.head_of t) thm |
|
191 | t => raise TERM ("prepare_def", [t])) |
|
192 |
|
193 val defs = map prepare_def [ |
|
194 @{thm abs_if[where 'a = int, THEN eq_reflection]}, |
|
195 @{thm abs_if[where 'a = real, THEN eq_reflection]}, |
|
196 @{thm min_def[where 'a = int, THEN eq_reflection]}, |
|
197 @{thm min_def[where 'a = real, THEN eq_reflection]}, |
|
198 @{thm max_def[where 'a = int, THEN eq_reflection]}, |
|
199 @{thm max_def[where 'a = real, THEN eq_reflection]}, |
|
200 @{thm Ex1_def}, @{thm Ball_def}, @{thm Bex_def}] |
|
201 |
|
202 fun matches thy ((t as Const (n, _)), (m, p)) = |
|
203 n = m andalso Pattern.matches thy (p, t) |
|
204 | matches _ _ = false |
|
205 |
|
206 fun lookup_def thy = AList.lookup (matches thy) defs |
|
207 fun lookup_def_head thy = lookup_def thy o Term.head_of |
|
208 |
|
209 fun occurs_def thy = Term.exists_subterm (is_some o lookup_def thy) |
|
210 |
|
211 fun unfold_def_conv ctxt ct = |
|
212 (case lookup_def_head (ProofContext.theory_of ctxt) (Thm.term_of ct) of |
|
213 SOME thm => Conv.rewr_conv thm |
|
214 | NONE => Conv.all_conv) ct |
|
215 in |
|
216 fun unfold_defs ctxt = |
|
217 (occurs_def (ProofContext.theory_of ctxt) o Thm.prop_of) ?? |
|
218 Conv.fconv_rule (More_Conv.top_conv unfold_def_conv ctxt) |
|
219 end |
|
220 |
|
221 |
|
222 |
|
223 (* further normalizations: beta/eta, universal closure, atomize *) |
|
224 |
|
225 local |
|
226 val all1 = @{lemma "All P == ALL x. P x" by (rule reflexive)} |
|
227 val all2 = @{lemma "All == (%P. ALL x. P x)" by (rule reflexive)} |
|
228 val ex1 = @{lemma "Ex P == EX x. P x" by (rule reflexive)} |
|
229 val ex2 = @{lemma "Ex == (%P. EX x. P x)" by (rule reflexive)} |
|
230 val let1 = @{lemma "Let c P == let x = c in P x" by (rule reflexive)} |
|
231 val let2 = @{lemma "Let c == (%P. let x = c in P x)" by (rule reflexive)} |
|
232 val let3 = @{lemma "Let == (%c P. let x = c in P x)" by (rule reflexive)} |
|
233 |
|
234 fun all_abs_conv cv ctxt = |
|
235 Conv.abs_conv (all_abs_conv cv o snd) ctxt else_conv cv ctxt |
|
236 fun keep_conv ctxt = More_Conv.binder_conv norm_conv ctxt |
|
237 and unfold_conv rule ctxt = |
|
238 Conv.rewr_conv rule then_conv all_abs_conv keep_conv ctxt |
|
239 and unfold_let_conv rule ctxt = |
|
240 Conv.rewr_conv rule then_conv |
|
241 all_abs_conv (fn cx => Conv.combination_conv |
|
242 (Conv.arg_conv (norm_conv cx)) (Conv.abs_conv (norm_conv o snd) cx)) ctxt |
|
243 and norm_conv ctxt ct = |
|
244 (case Thm.term_of ct of |
|
245 Const (@{const_name All}, _) $ Abs _ => keep_conv |
|
246 | Const (@{const_name All}, _) $ _ => unfold_conv all1 |
|
247 | Const (@{const_name All}, _) => unfold_conv all2 |
|
248 | Const (@{const_name Ex}, _) $ Abs _ => keep_conv |
|
249 | Const (@{const_name Ex}, _) $ _ => unfold_conv ex1 |
|
250 | Const (@{const_name Ex}, _) => unfold_conv ex2 |
|
251 | Const (@{const_name Let}, _) $ _ $ Abs _ => keep_conv |
|
252 | Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv let1 |
|
253 | Const (@{const_name Let}, _) $ _ => unfold_let_conv let2 |
|
254 | Const (@{const_name Let}, _) => unfold_let_conv let3 |
|
255 | Abs _ => Conv.abs_conv (norm_conv o snd) |
|
256 | _ $ _ => Conv.comb_conv o norm_conv |
|
257 | _ => K Conv.all_conv) ctxt ct |
|
258 |
|
259 fun is_normed t = |
|
260 (case t of |
|
261 Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed u |
|
262 | Const (@{const_name All}, _) $ _ => false |
|
263 | Const (@{const_name All}, _) => false |
|
264 | Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed u |
|
265 | Const (@{const_name Ex}, _) $ _ => false |
|
266 | Const (@{const_name Ex}, _) => false |
|
267 | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) => |
|
268 is_normed u1 andalso is_normed u2 |
|
269 | Const (@{const_name Let}, _) $ _ $ _ => false |
|
270 | Const (@{const_name Let}, _) $ _ => false |
|
271 | Const (@{const_name Let}, _) => false |
|
272 | Abs (_, _, u) => is_normed u |
|
273 | u1 $ u2 => is_normed u1 andalso is_normed u2 |
|
274 | _ => true) |
|
275 in |
|
276 fun norm_binder_conv ctxt = if_conv is_normed Conv.all_conv (norm_conv ctxt) |
|
277 end |
|
278 |
|
279 fun norm_def ctxt thm = |
|
280 (case Thm.prop_of thm of |
|
281 @{term Trueprop} $ (Const (@{const_name "op ="}, _) $ _ $ Abs _) => |
|
282 norm_def ctxt (thm RS @{thm fun_cong}) |
|
283 | Const (@{const_name "=="}, _) $ _ $ Abs _ => |
|
284 norm_def ctxt (thm RS @{thm meta_eq_to_obj_eq}) |
|
285 | _ => thm) |
|
286 |
|
287 fun atomize_conv ctxt ct = |
|
288 (case Thm.term_of ct of |
|
289 @{term "op ==>"} $ _ $ _ => |
|
290 Conv.binop_conv (atomize_conv ctxt) then_conv |
|
291 Conv.rewr_conv @{thm atomize_imp} |
|
292 | Const (@{const_name "=="}, _) $ _ $ _ => |
|
293 Conv.binop_conv (atomize_conv ctxt) then_conv |
|
294 Conv.rewr_conv @{thm atomize_eq} |
|
295 | Const (@{const_name all}, _) $ Abs _ => |
|
296 More_Conv.binder_conv atomize_conv ctxt then_conv |
|
297 Conv.rewr_conv @{thm atomize_all} |
|
298 | _ => Conv.all_conv) ct |
|
299 |
|
300 fun normalize_rule ctxt = |
|
301 Conv.fconv_rule ( |
|
302 (* reduce lambda abstractions, except at known binders: *) |
|
303 Thm.beta_conversion true then_conv |
|
304 Thm.eta_conversion then_conv |
|
305 norm_binder_conv ctxt) #> |
|
306 norm_def ctxt #> |
|
307 Drule.forall_intr_vars #> |
|
308 Conv.fconv_rule (atomize_conv ctxt) |
|
309 |
|
310 |
|
311 |
|
312 (* lift lambda terms into additional rules *) |
|
313 |
|
314 local |
|
315 val meta_eq = @{cpat "op =="} |
|
316 val meta_eqT = hd (Thm.dest_ctyp (Thm.ctyp_of_term meta_eq)) |
|
317 fun inst_meta cT = Thm.instantiate_cterm ([(meta_eqT, cT)], []) meta_eq |
|
318 fun mk_meta_eq ct cu = Thm.mk_binop (inst_meta (Thm.ctyp_of_term ct)) ct cu |
|
319 |
|
320 fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt) |
|
321 |
|
322 fun used_vars cvs ct = |
|
323 let |
|
324 val lookup = AList.lookup (op aconv) (map (` Thm.term_of) cvs) |
|
325 val add = (fn SOME ct => insert (op aconvc) ct | _ => I) |
|
326 in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end |
|
327 |
|
328 fun apply cv thm = |
|
329 let val thm' = Thm.combination thm (Thm.reflexive cv) |
|
330 in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end |
|
331 fun apply_def cvs eq = Thm.symmetric (fold apply cvs eq) |
|
332 |
|
333 fun replace_lambda cvs ct (cx as (ctxt, defs)) = |
|
334 let |
|
335 val cvs' = used_vars cvs ct |
|
336 val ct' = fold_rev Thm.cabs cvs' ct |
|
337 in |
|
338 (case Termtab.lookup defs (Thm.term_of ct') of |
|
339 SOME eq => (apply_def cvs' eq, cx) |
|
340 | NONE => |
|
341 let |
|
342 val {T, ...} = Thm.rep_cterm ct' and n = Name.uu |
|
343 val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt |
|
344 val cu = mk_meta_eq (cert ctxt (Free (n', T))) ct' |
|
345 val (eq, ctxt'') = yield_singleton Assumption.add_assumes cu ctxt' |
|
346 val defs' = Termtab.update (Thm.term_of ct', eq) defs |
|
347 in (apply_def cvs' eq, (ctxt'', defs')) end) |
|
348 end |
|
349 |
|
350 fun none ct cx = (Thm.reflexive ct, cx) |
|
351 fun in_comb f g ct cx = |
|
352 let val (cu1, cu2) = Thm.dest_comb ct |
|
353 in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end |
|
354 fun in_arg f = in_comb none f |
|
355 fun in_abs f cvs ct (ctxt, defs) = |
|
356 let |
|
357 val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt |
|
358 val (cv, cu) = Thm.dest_abs (SOME n) ct |
|
359 in (ctxt', defs) |> f (cv :: cvs) cu |>> Thm.abstract_rule n cv end |
|
360 |
|
361 fun traverse cvs ct = |
|
362 (case Thm.term_of ct of |
|
363 Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs traverse cvs) |
|
364 | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs traverse cvs) |
|
365 | Const (@{const_name Let}, _) $ _ $ Abs _ => |
|
366 in_comb (in_arg (traverse cvs)) (in_abs traverse cvs) |
|
367 | Abs _ => at_lambda cvs |
|
368 | _ $ _ => in_comb (traverse cvs) (traverse cvs) |
|
369 | _ => none) ct |
|
370 |
|
371 and at_lambda cvs ct = |
|
372 in_abs traverse cvs ct #-> (fn thm => |
|
373 replace_lambda cvs (Thm.rhs_of thm) #>> Thm.transitive thm) |
|
374 |
|
375 fun has_free_lambdas t = |
|
376 (case t of |
|
377 Const (@{const_name All}, _) $ Abs (_, _, u) => has_free_lambdas u |
|
378 | Const (@{const_name Ex}, _) $ Abs (_, _, u) => has_free_lambdas u |
|
379 | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) => |
|
380 has_free_lambdas u1 orelse has_free_lambdas u2 |
|
381 | Abs _ => true |
|
382 | u1 $ u2 => has_free_lambdas u1 orelse has_free_lambdas u2 |
|
383 | _ => false) |
|
384 |
|
385 fun lift_lm f thm cx = |
|
386 if not (has_free_lambdas (Thm.prop_of thm)) then (thm, cx) |
|
387 else cx |> f (Thm.cprop_of thm) |>> (fn thm' => Thm.equal_elim thm' thm) |
|
388 in |
|
389 fun lift_lambdas thms ctxt = |
|
390 let |
|
391 val cx = (ctxt, Termtab.empty) |
|
392 val (thms', (ctxt', defs)) = fold_map (lift_lm (traverse [])) thms cx |
|
393 val eqs = Termtab.fold (cons o normalize_rule ctxt' o snd) defs [] |
|
394 in (eqs @ thms', ctxt') end |
|
395 end |
|
396 |
|
397 |
|
398 |
|
399 (* make application explicit for functions with varying number of arguments *) |
|
400 |
|
401 local |
|
402 val const = prefix "c" and free = prefix "f" |
|
403 fun min i (e as (_, j)) = if i <> j then (true, Int.min (i, j)) else e |
|
404 fun add t i = Symtab.map_default (t, (false, i)) (min i) |
|
405 fun traverse t = |
|
406 (case Term.strip_comb t of |
|
407 (Const (n, _), ts) => add (const n) (length ts) #> fold traverse ts |
|
408 | (Free (n, _), ts) => add (free n) (length ts) #> fold traverse ts |
|
409 | (Abs (_, _, u), ts) => fold traverse (u :: ts) |
|
410 | (_, ts) => fold traverse ts) |
|
411 val prune = (fn (n, (true, i)) => Symtab.update (n, i) | _ => I) |
|
412 fun prune_tab tab = Symtab.fold prune tab Symtab.empty |
|
413 |
|
414 fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2 |
|
415 fun nary_conv conv1 conv2 ct = |
|
416 (Conv.combination_conv (nary_conv conv1 conv2) conv2 else_conv conv1) ct |
|
417 fun abs_conv conv tb = Conv.abs_conv (fn (cv, cx) => |
|
418 let val n = fst (Term.dest_Free (Thm.term_of cv)) |
|
419 in conv (Symtab.update (free n, 0) tb) cx end) |
|
420 val apply_rule = @{lemma "f x == apply f x" by (simp add: apply_def)} |
|
421 in |
|
422 fun explicit_application ctxt thms = |
|
423 let |
|
424 fun sub_conv tb ctxt ct = |
|
425 (case Term.strip_comb (Thm.term_of ct) of |
|
426 (Const (n, _), ts) => app_conv tb (const n) (length ts) ctxt |
|
427 | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt |
|
428 | (Abs _, _) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt) |
|
429 | (_, _) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct |
|
430 and app_conv tb n i ctxt = |
|
431 (case Symtab.lookup tb n of |
|
432 NONE => nary_conv Conv.all_conv (sub_conv tb ctxt) |
|
433 | SOME j => apply_conv tb ctxt (i - j)) |
|
434 and apply_conv tb ctxt i ct = ( |
|
435 if i = 0 then nary_conv Conv.all_conv (sub_conv tb ctxt) |
|
436 else |
|
437 Conv.rewr_conv apply_rule then_conv |
|
438 binop_conv (apply_conv tb ctxt (i-1)) (sub_conv tb ctxt)) ct |
|
439 |
|
440 fun needs_exp_app tab = Term.exists_subterm (fn |
|
441 Bound _ $ _ => true |
|
442 | Const (n, _) => Symtab.defined tab (const n) |
|
443 | Free (n, _) => Symtab.defined tab (free n) |
|
444 | _ => false) |
|
445 |
|
446 fun rewrite tab ctxt thm = |
|
447 if not (needs_exp_app tab (Thm.prop_of thm)) then thm |
|
448 else Conv.fconv_rule (sub_conv tab ctxt) thm |
|
449 |
|
450 val tab = prune_tab (fold (traverse o Thm.prop_of) thms Symtab.empty) |
|
451 in map (rewrite tab ctxt) thms end |
|
452 end |
|
453 |
|
454 |
|
455 |
|
456 (* combined normalization *) |
|
457 |
|
458 fun normalize thms ctxt = |
|
459 thms |
|
460 |> trivial_distinct ctxt |
|
461 |> rewrite_bool_cases ctxt |
|
462 |> normalize_numerals ctxt |
|
463 |> nat_as_int ctxt |
|
464 |> map (unfold_defs ctxt #> normalize_rule ctxt) |
|
465 |> rpair ctxt |
|
466 |-> SMT_Monomorph.monomorph |
|
467 |-> lift_lambdas |
|
468 |-> (fn thms' => `(fn ctxt' => explicit_application ctxt' thms')) |
|
469 |
|
470 end |
|