author | wenzelm |
Sat, 23 May 2015 17:19:37 +0200 | |
changeset 60299 | 5ae2a2e74c93 |
parent 59970 | e9f73d87d904 |
child 60642 | 48dd1cefb4ae |
permissions | -rw-r--r-- |
56813 | 1 |
(* Title: HOL/Decision_Procs/approximation.ML |
2 |
Author: Johannes Hoelzl, TU Muenchen |
|
3 |
*) |
|
4 |
||
5 |
signature APPROXIMATION = |
|
6 |
sig |
|
7 |
val approx: int -> Proof.context -> term -> term |
|
59850 | 8 |
val approximate: Proof.context -> term -> term |
9 |
val approximation_tac : int -> (string * int) list -> int option -> Proof.context -> int -> tactic |
|
56813 | 10 |
end |
11 |
||
12 |
structure Approximation: APPROXIMATION = |
|
13 |
struct |
|
14 |
||
59850 | 15 |
fun reorder_bounds_tac prems i = |
16 |
let |
|
17 |
fun variable_of_bound (Const (@{const_name Trueprop}, _) $ |
|
18 |
(Const (@{const_name Set.member}, _) $ |
|
19 |
Free (name, _) $ _)) = name |
|
20 |
| variable_of_bound (Const (@{const_name Trueprop}, _) $ |
|
21 |
(Const (@{const_name HOL.eq}, _) $ |
|
22 |
Free (name, _) $ _)) = name |
|
23 |
| variable_of_bound t = raise TERM ("variable_of_bound", [t]) |
|
24 |
||
25 |
val variable_bounds |
|
26 |
= map (`(variable_of_bound o Thm.prop_of)) prems |
|
27 |
||
28 |
fun add_deps (name, bnds) |
|
29 |
= Graph.add_deps_acyclic (name, |
|
30 |
remove (op =) name (Term.add_free_names (Thm.prop_of bnds) [])) |
|
31 |
||
32 |
val order = Graph.empty |
|
33 |
|> fold Graph.new_node variable_bounds |
|
34 |
|> fold add_deps variable_bounds |
|
35 |
|> Graph.strong_conn |> map the_single |> rev |
|
36 |
|> map_filter (AList.lookup (op =) variable_bounds) |
|
37 |
||
38 |
fun prepend_prem th tac |
|
39 |
= tac THEN rtac (th RSN (2, @{thm mp})) i |
|
40 |
in |
|
41 |
fold prepend_prem order all_tac |
|
42 |
end |
|
43 |
||
44 |
fun approximation_conv ctxt ct = |
|
45 |
approximation_oracle (Proof_Context.theory_of ctxt, Thm.term_of ct |> tap (tracing o Syntax.string_of_term ctxt)); |
|
46 |
||
47 |
fun approximate ctxt t = |
|
48 |
approximation_oracle (Proof_Context.theory_of ctxt, t) |
|
49 |
|> Thm.prop_of |> Logic.dest_equals |> snd; |
|
50 |
||
51 |
(* Should be in HOL.thy ? *) |
|
52 |
fun gen_eval_tac conv ctxt = CONVERSION |
|
59970 | 53 |
(Object_Logic.judgment_conv ctxt (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)) |
59850 | 54 |
THEN' rtac TrueI |
55 |
||
56 |
val form_equations = @{thms interpret_form_equations}; |
|
57 |
||
58 |
fun rewrite_interpret_form_tac ctxt prec splitting taylor i st = let |
|
59 |
fun lookup_splitting (Free (name, _)) |
|
60 |
= case AList.lookup (op =) splitting name |
|
61 |
of SOME s => HOLogic.mk_number @{typ nat} s |
|
62 |
| NONE => @{term "0 :: nat"} |
|
63 |
val vs = nth (Thm.prems_of st) (i - 1) |
|
64 |
|> Logic.strip_imp_concl |
|
65 |
|> HOLogic.dest_Trueprop |
|
66 |
|> Term.strip_comb |> snd |> List.last |
|
67 |
|> HOLogic.dest_list |
|
68 |
val p = prec |
|
69 |
|> HOLogic.mk_number @{typ nat} |
|
70 |
|> Thm.cterm_of ctxt |
|
71 |
in case taylor |
|
72 |
of NONE => let |
|
73 |
val n = vs |> length |
|
74 |
|> HOLogic.mk_number @{typ nat} |
|
75 |
|> Thm.cterm_of ctxt |
|
76 |
val s = vs |
|
77 |
|> map lookup_splitting |
|
78 |
|> HOLogic.mk_list @{typ nat} |
|
79 |
|> Thm.cterm_of ctxt |
|
80 |
in |
|
81 |
(rtac (Thm.instantiate ([], [(@{cpat "?n::nat"}, n), |
|
82 |
(@{cpat "?prec::nat"}, p), |
|
83 |
(@{cpat "?ss::nat list"}, s)]) |
|
84 |
@{thm "approx_form"}) i |
|
85 |
THEN simp_tac (put_simpset (simpset_of @{context}) ctxt) i) st |
|
86 |
end |
|
87 |
||
88 |
| SOME t => |
|
89 |
if length vs <> 1 |
|
90 |
then raise (TERM ("More than one variable used for taylor series expansion", [Thm.prop_of st])) |
|
91 |
else let |
|
92 |
val t = t |
|
93 |
|> HOLogic.mk_number @{typ nat} |
|
94 |
|> Thm.cterm_of ctxt |
|
95 |
val s = vs |> map lookup_splitting |> hd |
|
96 |
|> Thm.cterm_of ctxt |
|
97 |
in |
|
98 |
rtac (Thm.instantiate ([], [(@{cpat "?s::nat"}, s), |
|
99 |
(@{cpat "?t::nat"}, t), |
|
100 |
(@{cpat "?prec::nat"}, p)]) |
|
101 |
@{thm "approx_tse_form"}) i st |
|
102 |
end |
|
103 |
end |
|
104 |
||
56813 | 105 |
fun calculated_subterms (@{const Trueprop} $ t) = calculated_subterms t |
106 |
| calculated_subterms (@{const HOL.implies} $ _ $ t) = calculated_subterms t |
|
107 |
| calculated_subterms (@{term "op <= :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = [t1, t2] |
|
108 |
| calculated_subterms (@{term "op < :: real \<Rightarrow> real \<Rightarrow> bool"} $ t1 $ t2) = [t1, t2] |
|
109 |
| calculated_subterms (@{term "op : :: real \<Rightarrow> real set \<Rightarrow> bool"} $ t1 $ |
|
110 |
(@{term "atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set"} $ t2 $ t3)) = [t1, t2, t3] |
|
111 |
| calculated_subterms t = raise TERM ("calculated_subterms", [t]) |
|
112 |
||
113 |
fun dest_interpret_form (@{const "interpret_form"} $ b $ xs) = (b, xs) |
|
114 |
| dest_interpret_form t = raise TERM ("dest_interpret_form", [t]) |
|
115 |
||
116 |
fun dest_interpret (@{const "interpret_floatarith"} $ b $ xs) = (b, xs) |
|
117 |
| dest_interpret t = raise TERM ("dest_interpret", [t]) |
|
118 |
||
119 |
||
120 |
fun dest_float (@{const "Float"} $ m $ e) = |
|
121 |
(snd (HOLogic.dest_number m), snd (HOLogic.dest_number e)) |
|
122 |
||
123 |
fun dest_ivl (Const (@{const_name "Some"}, _) $ |
|
124 |
(Const (@{const_name Pair}, _) $ u $ l)) = SOME (dest_float u, dest_float l) |
|
125 |
| dest_ivl (Const (@{const_name "None"}, _)) = NONE |
|
126 |
| dest_ivl t = raise TERM ("dest_result", [t]) |
|
127 |
||
128 |
fun mk_approx' prec t = (@{const "approx'"} |
|
129 |
$ HOLogic.mk_number @{typ nat} prec |
|
130 |
$ t $ @{term "[] :: (float * float) option list"}) |
|
131 |
||
132 |
fun mk_approx_form_eval prec t xs = (@{const "approx_form_eval"} |
|
133 |
$ HOLogic.mk_number @{typ nat} prec |
|
134 |
$ t $ xs) |
|
135 |
||
136 |
fun float2_float10 prec round_down (m, e) = ( |
|
137 |
let |
|
138 |
val (m, e) = (if e < 0 then (m,e) else (m * Integer.pow e 2, 0)) |
|
139 |
||
140 |
fun frac c p 0 digits cnt = (digits, cnt, 0) |
|
141 |
| frac c 0 r digits cnt = (digits, cnt, r) |
|
142 |
| frac c p r digits cnt = (let |
|
143 |
val (d, r) = Integer.div_mod (r * 10) (Integer.pow (~e) 2) |
|
144 |
in frac (c orelse d <> 0) (if d <> 0 orelse c then p - 1 else p) r |
|
145 |
(digits * 10 + d) (cnt + 1) |
|
146 |
end) |
|
147 |
||
148 |
val sgn = Int.sign m |
|
149 |
val m = abs m |
|
150 |
||
151 |
val round_down = (sgn = 1 andalso round_down) orelse |
|
152 |
(sgn = ~1 andalso not round_down) |
|
153 |
||
154 |
val (x, r) = Integer.div_mod m (Integer.pow (~e) 2) |
|
155 |
||
156 |
val p = ((if x = 0 then prec else prec - (IntInf.log2 x + 1)) * 3) div 10 + 1 |
|
157 |
||
158 |
val (digits, e10, r) = if p > 0 then frac (x <> 0) p r 0 0 else (0,0,0) |
|
159 |
||
160 |
val digits = if round_down orelse r = 0 then digits else digits + 1 |
|
161 |
||
162 |
in (sgn * (digits + x * (Integer.pow e10 10)), ~e10) |
|
163 |
end) |
|
164 |
||
165 |
fun mk_result prec (SOME (l, u)) = |
|
166 |
(let |
|
167 |
fun mk_float10 rnd x = (let val (m, e) = float2_float10 prec rnd x |
|
168 |
in if e = 0 then HOLogic.mk_number @{typ real} m |
|
169 |
else if e = 1 then @{term "divide :: real \<Rightarrow> real \<Rightarrow> real"} $ |
|
170 |
HOLogic.mk_number @{typ real} m $ |
|
171 |
@{term "10"} |
|
172 |
else @{term "divide :: real \<Rightarrow> real \<Rightarrow> real"} $ |
|
173 |
HOLogic.mk_number @{typ real} m $ |
|
174 |
(@{term "power 10 :: nat \<Rightarrow> real"} $ |
|
175 |
HOLogic.mk_number @{typ nat} (~e)) end) |
|
176 |
in @{term "atLeastAtMost :: real \<Rightarrow> real \<Rightarrow> real set"} $ mk_float10 true l $ mk_float10 false u end) |
|
177 |
| mk_result _ NONE = @{term "UNIV :: real set"} |
|
178 |
||
179 |
fun realify t = |
|
180 |
let |
|
181 |
val t = Logic.varify_global t |
|
182 |
val m = map (fn (name, _) => (name, @{typ real})) (Term.add_tvars t []) |
|
183 |
val t = Term.subst_TVars m t |
|
184 |
in t end |
|
185 |
||
186 |
fun apply_tactic ctxt term tactic = |
|
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59582
diff
changeset
|
187 |
Thm.cterm_of ctxt term |
56813 | 188 |
|> Goal.init |
189 |
|> SINGLE tactic |
|
59582 | 190 |
|> the |> Thm.prems_of |> hd |
56813 | 191 |
|
192 |
fun prepare_form ctxt term = apply_tactic ctxt term ( |
|
193 |
REPEAT (FIRST' [etac @{thm intervalE}, etac @{thm meta_eqE}, rtac @{thm impI}] 1) |
|
194 |
THEN Subgoal.FOCUS (fn {prems, ...} => reorder_bounds_tac prems 1) ctxt 1 |
|
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59174
diff
changeset
|
195 |
THEN DETERM (TRY (filter_prems_tac ctxt (K false) 1))) |
56813 | 196 |
|
197 |
fun reify_form ctxt term = apply_tactic ctxt term |
|
198 |
(Reification.tac ctxt form_equations NONE 1) |
|
199 |
||
200 |
fun approx_form prec ctxt t = |
|
201 |
realify t |
|
202 |
|> prepare_form ctxt |
|
203 |
|> (fn arith_term => reify_form ctxt arith_term |
|
204 |
|> HOLogic.dest_Trueprop |> dest_interpret_form |
|
205 |
|> (fn (data, xs) => |
|
206 |
mk_approx_form_eval prec data (HOLogic.mk_list @{typ "(float * float) option"} |
|
207 |
(map (fn _ => @{term "None :: (float * float) option"}) (HOLogic.dest_list xs))) |
|
208 |
|> approximate ctxt |
|
209 |
|> HOLogic.dest_list |
|
210 |
|> curry ListPair.zip (HOLogic.dest_list xs @ calculated_subterms arith_term) |
|
211 |
|> map (fn (elem, s) => @{term "op : :: real \<Rightarrow> real set \<Rightarrow> bool"} $ elem $ mk_result prec (dest_ivl s)) |
|
212 |
|> foldr1 HOLogic.mk_conj)) |
|
213 |
||
214 |
fun approx_arith prec ctxt t = realify t |
|
59621
291934bac95e
Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents:
59582
diff
changeset
|
215 |
|> Thm.cterm_of ctxt |
56813 | 216 |
|> Reification.conv ctxt form_equations |
59582 | 217 |
|> Thm.prop_of |
56813 | 218 |
|> Logic.dest_equals |> snd |
219 |
|> dest_interpret |> fst |
|
220 |
|> mk_approx' prec |
|
221 |
|> approximate ctxt |
|
222 |
|> dest_ivl |
|
223 |
|> mk_result prec |
|
224 |
||
225 |
fun approx prec ctxt t = |
|
226 |
if type_of t = @{typ prop} then approx_form prec ctxt t |
|
227 |
else if type_of t = @{typ bool} then approx_form prec ctxt (@{const Trueprop} $ t) |
|
228 |
else approx_arith prec ctxt t |
|
229 |
||
56923 | 230 |
fun approximate_cmd modes raw_t state = |
231 |
let |
|
232 |
val ctxt = Toplevel.context_of state; |
|
233 |
val t = Syntax.read_term ctxt raw_t; |
|
234 |
val t' = approx 30 ctxt t; |
|
235 |
val ty' = Term.type_of t'; |
|
236 |
val ctxt' = Variable.auto_fixes t' ctxt; |
|
59174 | 237 |
in |
238 |
Print_Mode.with_modes modes (fn () => |
|
56923 | 239 |
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk, |
59174 | 240 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) () |
241 |
end |> Pretty.writeln; |
|
56923 | 242 |
|
243 |
val opt_modes = |
|
244 |
Scan.optional (@{keyword "("} |-- Parse.!!! (Scan.repeat1 Parse.xname --| @{keyword ")"})) []; |
|
245 |
||
246 |
val _ = |
|
59936
b8ffc3dc9e24
@{command_spec} is superseded by @{command_keyword};
wenzelm
parents:
59850
diff
changeset
|
247 |
Outer_Syntax.command @{command_keyword approximate} "print approximation of term" |
56923 | 248 |
(opt_modes -- Parse.term |
249 |
>> (fn (modes, t) => Toplevel.keep (approximate_cmd modes t))); |
|
250 |
||
59850 | 251 |
fun approximation_tac prec splitting taylor ctxt i = |
252 |
REPEAT (FIRST' [etac @{thm intervalE}, |
|
253 |
etac @{thm meta_eqE}, |
|
254 |
rtac @{thm impI}] i) |
|
255 |
THEN Subgoal.FOCUS (fn {prems, ...} => reorder_bounds_tac prems i) ctxt i |
|
256 |
THEN DETERM (TRY (filter_prems_tac ctxt (K false) i)) |
|
257 |
THEN DETERM (Reification.tac ctxt form_equations NONE i) |
|
258 |
THEN rewrite_interpret_form_tac ctxt prec splitting taylor i |
|
259 |
THEN gen_eval_tac (approximation_conv ctxt) ctxt i |
|
260 |
||
261 |
end; |