Isar command 'calculation' as front-end for KEStore_Elems.add_calcs, without change of semantics;
1 (* integration over the reals
4 (c) due to copyright terms
7 theory Integrate imports Diff begin
11 Integral :: "[real, real]=> real" ("Integral _ D _" 91)
12 (*new_c :: "real => real" ("new_c _" 66)*)
13 is_f_x :: "real => bool" ("_ is'_f'_x" 10)
15 (*descriptions in the related problems*)
16 integrateBy :: "real => una"
17 antiDerivative :: "real => una"
18 antiDerivativeName :: "(real => real) => una"
20 (*the CAS-command, eg. "Integrate (2*x \<up> 3, x)"*)
21 Integrate :: "[real * real] => real"
24 (*stated as axioms, todo: prove as theorems
25 'bdv' is a constant handled on the meta-level
26 specifically as a 'bound variable' *)
28 (*Ambiguous input\<^here> produces 3 parse trees -----------------------------\\*)
29 integral_const: "Not (bdv occurs_in u) ==> Integral u D bdv = u * bdv" and
30 integral_var: "Integral bdv D bdv = bdv \<up> 2 / 2" and
32 integral_add: "Integral (u + v) D bdv =
33 (Integral u D bdv) + (Integral v D bdv)" and
34 integral_mult: "[| Not (bdv occurs_in u); bdv occurs_in v |] ==>
35 Integral (u * v) D bdv = u * (Integral v D bdv)" and
36 (*WN080222: this goes into sub-terms, too ...
37 call_for_new_c: "[| Not (matches (u + new_c v) a); Not (a is_f_x) |] ==>
40 integral_pow: "Integral bdv \<up> n D bdv = bdv \<up> (n+1) / (n + 1)"
41 (*Ambiguous input\<^here> produces 3 parse trees -----------------------------//*)
44 (** eval functions **)
46 val c = Free ("c", HOLogic.realT);
47 (*.create a new unique variable 'c..' in a term; for use by Rule.Eval in a rls;
48 an alternative to do this would be '(Try (Calculate new_c_) (new_c es__))'
49 in the script; this will be possible if currying doesnt take the value
50 from a variable, but the value '(new_c es__)' itself.*)
53 case (Symbol.explode o id_of) var of
55 | "c"::"_"::is => (case (TermC.int_opt_of_string o implode) is of
59 fun get_coeff c = case (Symbol.explode o id_of) c of
60 "c"::"_"::is => (the o TermC.int_opt_of_string o implode) is
62 val cs = filter selc (TermC.vars term);
66 | [_] => Free ("c_2", HOLogic.realT)
68 let val max_coeff = maxl (map get_coeff cs)
69 in Free ("c_"^string_of_int (max_coeff + 1), HOLogic.realT) end
73 (*("new_c", ("Integrate.new_c", eval_new_c "#new_c_"))*)
74 fun eval_new_c _ _ (p as (Const ("Integrate.new_c",_) $ t)) _ =
75 SOME ((UnparseC.term p) ^ " = " ^ UnparseC.term (new_c p),
76 Trueprop $ (mk_equality (p, new_c p)))
77 | eval_new_c _ _ _ _ = NONE;
81 (*("add_new_c", ("Integrate.add_new_c", eval_add_new_c "#add_new_c_"))
82 add a new c to a term or a fun-equation;
83 this is _not in_ the term, because only applied to _whole_ term*)
84 fun eval_add_new_c (_:string) "Integrate.add_new_c" p (_:theory) =
85 let val p' = case p of
86 Const (\<^const_name>\<open>HOL.eq\<close>, T) $ lh $ rh =>
87 Const (\<^const_name>\<open>HOL.eq\<close>, T) $ lh $ TermC.mk_add rh (new_c rh)
88 | p => TermC.mk_add p (new_c p)
89 in SOME ((UnparseC.term p) ^ " = " ^ UnparseC.term p',
90 HOLogic.Trueprop $ (TermC.mk_equality (p, p')))
92 | eval_add_new_c _ _ _ _ = NONE;
95 (*("is_f_x", ("Integrate.is_f_x", eval_is_f_x "is_f_x_"))*)
96 fun eval_is_f_x _ _(p as (Const ("Integrate.is_f_x", _)
99 then SOME ((UnparseC.term p) ^ " = True",
100 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
101 else SOME ((UnparseC.term p) ^ " = False",
102 HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
103 | eval_is_f_x _ _ _ _ = NONE;
105 setup \<open>KEStore_Elems.add_calcs
106 [("add_new_c", ("Integrate.add_new_c" (* FIXME proper const!? *), eval_add_new_c "add_new_c_"))]\<close>
107 calculation is_f_x = \<open>eval_is_f_x "is_f_idextifier_"\<close>
112 (*.rulesets for integration.*)
113 val integration_rules =
114 Rule_Def.Repeat {id="integration_rules", preconds = [],
115 rew_ord = ("termlessI",termlessI),
116 erls = Rule_Def.Repeat {id="conditions_in_integration_rules",
118 rew_ord = ("termlessI",termlessI),
119 erls = Rule_Set.Empty,
120 srls = Rule_Set.Empty, calc = [], errpatts = [],
121 rules = [(*for rewriting conditions in Thm's*)
122 \<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "#occurs_in_"),
123 \<^rule_thm>\<open>not_true\<close>,
124 \<^rule_thm>\<open>not_false\<close>
126 scr = Rule.Empty_Prog},
127 srls = Rule_Set.Empty, calc = [], errpatts = [],
129 \<^rule_thm>\<open>integral_const\<close>,
130 \<^rule_thm>\<open>integral_var\<close>,
131 \<^rule_thm>\<open>integral_add\<close>,
132 \<^rule_thm>\<open>integral_mult\<close>,
133 \<^rule_thm>\<open>integral_pow\<close>,
134 \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")(*for n+1*)
136 scr = Rule.Empty_Prog};
140 Rule_Set.Sequence {id="add_new_c", preconds = [],
141 rew_ord = ("termlessI",termlessI),
142 erls = Rule_Def.Repeat {id="conditions_in_add_new_c",
144 rew_ord = ("termlessI",termlessI),
145 erls = Rule_Set.Empty,
146 srls = Rule_Set.Empty, calc = [], errpatts = [],
147 rules = [\<^rule_eval>\<open>Prog_Expr.matches\<close> (Prog_Expr.eval_matches""),
148 \<^rule_eval>\<open>Integrate.is_f_x\<close> (eval_is_f_x "is_f_x_"),
149 \<^rule_thm>\<open>not_true\<close>,
150 \<^rule_thm>\<open>not_false\<close>
152 scr = Rule.Empty_Prog},
153 srls = Rule_Set.Empty, calc = [], errpatts = [],
154 rules = [ (*\<^rule_thm>\<open>call_for_new_c\<close>,*)
155 Rule.Cal1 ("Integrate.add_new_c", eval_add_new_c "new_c_")
157 scr = Rule.Empty_Prog};
161 (*.rulesets for simplifying Integrals.*)
163 (*.for simplify_Integral adapted from 'norm_Rational_rls'.*)
164 val norm_Rational_rls_noadd_fractions =
165 Rule_Def.Repeat {id = "norm_Rational_rls_noadd_fractions", preconds = [],
166 rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
167 erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
168 rules = [(*Rule.Rls_ add_fractions_p_rls,!!!*)
169 Rule.Rls_ (*rat_mult_div_pow original corrected WN051028*)
170 (Rule_Def.Repeat {id = "rat_mult_div_pow", preconds = [],
171 rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
172 erls = (*FIXME.WN051028 Rule_Set.empty,*)
173 Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty
174 [\<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp "")],
175 srls = Rule_Set.Empty, calc = [], errpatts = [],
176 rules = [\<^rule_thm>\<open>rat_mult\<close>,
177 (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
178 \<^rule_thm>\<open>rat_mult_poly_l\<close>,
179 (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
180 \<^rule_thm>\<open>rat_mult_poly_r\<close>,
181 (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
183 \<^rule_thm>\<open>real_divide_divide1_mg\<close>,
184 (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
185 \<^rule_thm>\<open>divide_divide_eq_right\<close>,
186 (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
187 \<^rule_thm>\<open>divide_divide_eq_left\<close>,
188 (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
189 \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e"),
191 \<^rule_thm>\<open>rat_power\<close>
192 (*"(?a / ?b) \<up> ?n = ?a \<up> ?n / ?b \<up> ?n"*)
194 scr = Rule.Empty_Prog
196 Rule.Rls_ make_rat_poly_with_parentheses,
197 Rule.Rls_ cancel_p_rls,(*FIXME:cancel_p does NOT order sometimes*)
198 Rule.Rls_ rat_reduce_1
200 scr = Rule.Empty_Prog
203 (*.for simplify_Integral adapted from 'norm_Rational'.*)
204 val norm_Rational_noadd_fractions =
205 Rule_Set.Sequence {id = "norm_Rational_noadd_fractions", preconds = [],
206 rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord),
207 erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
208 rules = [Rule.Rls_ discard_minus,
209 Rule.Rls_ rat_mult_poly,(* removes double fractions like a/b/c *)
210 Rule.Rls_ make_rat_poly_with_parentheses, (*WN0510 also in(#)below*)
211 Rule.Rls_ cancel_p_rls, (*FIXME.MG:cancel_p does NOT order sometim*)
212 Rule.Rls_ norm_Rational_rls_noadd_fractions,(* the main rls (#) *)
213 Rule.Rls_ discard_parentheses1 (* mult only *)
215 scr = Rule.Empty_Prog
218 (*.simplify terms before and after Integration such that
219 ..a.x^2/2 + b.x^3/3.. is made to ..a/2.x^2 + b/3.x^3.. (and NO
220 common denominator as done by norm_Rational or make_ratpoly_in.
221 This is a copy from 'make_ratpoly_in' with respective reduction of rules and
222 *1* expand the term, ie. distribute * and / over +
225 Rule_Set.append_rules "separate_bdv2"
227 [\<^rule_thm>\<open>separate_bdv\<close>,
228 (*"?a * ?bdv / ?b = ?a / ?b * ?bdv"*)
229 \<^rule_thm>\<open>separate_bdv_n\<close>,
230 \<^rule_thm>\<open>separate_1_bdv\<close>,
231 (*"?bdv / ?b = (1 / ?b) * ?bdv"*)
232 \<^rule_thm>\<open>separate_1_bdv_n\<close>(*,
233 (*"?bdv \<up> ?n / ?b = 1 / ?b * ?bdv \<up> ?n"*)
234 *****\<^rule_thm>\<open>add_divide_distrib\<close>
235 (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)----------*)
237 val simplify_Integral =
238 Rule_Set.Sequence {id = "simplify_Integral", preconds = []:term list,
239 rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
240 erls = Atools_erls, srls = Rule_Set.Empty,
241 calc = [], errpatts = [],
242 rules = [\<^rule_thm>\<open>distrib_right\<close>,
243 (*"(?z1.0 + ?z2.0) * ?w = ?z1.0 * ?w + ?z2.0 * ?w"*)
244 \<^rule_thm>\<open>add_divide_distrib\<close>,
245 (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)
246 (*^^^^^ *1* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*)
247 Rule.Rls_ norm_Rational_noadd_fractions,
248 Rule.Rls_ order_add_mult_in,
249 Rule.Rls_ discard_parentheses,
250 (*Rule.Rls_ collect_bdv, from make_polynomial_in*)
251 Rule.Rls_ separate_bdv2,
252 \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e")
254 scr = Rule.Empty_Prog};
257 (*simplify terms before and after Integration such that
258 ..a.x^2/2 + b.x^3/3.. is made to ..a/2.x^2 + b/3.x^3.. (and NO
259 common denominator as done by norm_Rational or make_ratpoly_in.
260 This is a copy from 'make_polynomial_in' with insertions from
262 THIS IS KEPT FOR COMPARISON ............................................
263 * val simplify_Integral = prep_rls'(
264 * Rule_Set.Sequence {id = "", preconds = []:term list,
265 * rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
266 * erls = Atools_erls, srls = Rule_Set.Empty,
267 * calc = [], (*asm_thm = [],*)
268 * rules = [Rule.Rls_ expand_poly,
269 * Rule.Rls_ order_add_mult_in,
270 * Rule.Rls_ simplify_power,
271 * Rule.Rls_ collect_numerals,
272 * Rule.Rls_ reduce_012,
273 * \<^rule_thm>\<open>realpow_oneI\<close>,
274 * Rule.Rls_ discard_parentheses,
275 * Rule.Rls_ collect_bdv,
276 * (*below inserted from 'make_ratpoly_in'*)
277 * Rule.Rls_ (Rule_Set.append_rules "separate_bdv"
279 * [\<^rule_thm>\<open>separate_bdv\<close>,
280 * (*"?a * ?bdv / ?b = ?a / ?b * ?bdv"*)
281 * \<^rule_thm>\<open>separate_bdv_n\<close>,
282 * \<^rule_thm>\<open>separate_1_bdv\<close>,
283 * (*"?bdv / ?b = (1 / ?b) * ?bdv"*)
284 * \<^rule_thm>\<open>separate_1_bdv_n\<close>(*,
285 * (*"?bdv \<up> ?n / ?b = 1 / ?b * ?bdv \<up> ?n"*)
286 * \<^rule_thm>\<open>add_divide_distrib\<close>
287 * (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)*)
289 * \<^rule_eval>\<open>divide\<close> (eval_cancel "#divide_e")
291 * scr = Rule.Empty_Prog
293 .......................................................................*)
296 Rule_Set.Sequence {id="integration", preconds = [],
297 rew_ord = ("termlessI",termlessI),
298 erls = Rule_Def.Repeat {id="conditions_in_integration",
300 rew_ord = ("termlessI",termlessI),
301 erls = Rule_Set.Empty,
302 srls = Rule_Set.Empty, calc = [], errpatts = [],
304 scr = Rule.Empty_Prog},
305 srls = Rule_Set.Empty, calc = [], errpatts = [],
306 rules = [ Rule.Rls_ integration_rules,
308 Rule.Rls_ simplify_Integral
310 scr = Rule.Empty_Prog};
312 val prep_rls' = Auto_Prog.prep_rls @{theory};
315 integration_rules = \<open>prep_rls' integration_rules\<close> and
316 add_new_c = \<open>prep_rls' add_new_c\<close> and
317 simplify_Integral = \<open>prep_rls' simplify_Integral\<close> and
318 integration = \<open>prep_rls' integration\<close> and
319 separate_bdv2 = \<open>prep_rls' separate_bdv2\<close> and
320 norm_Rational_noadd_fractions = \<open>prep_rls' norm_Rational_noadd_fractions\<close> and
321 norm_Rational_rls_noadd_fractions = \<open>prep_rls' norm_Rational_rls_noadd_fractions\<close>
325 problem pbl_fun_integ : "integrate/function" =
326 \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
327 Method: "diff/integration"
328 CAS: "Integrate (f_f, v_v)"
329 Given: "functionTerm f_f" "integrateBy v_v"
330 Find: "antiDerivative F_F"
332 problem pbl_fun_integ_nam : "named/integrate/function" =
333 (*here "named" is used differently from Differentiation"*)
334 \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
335 Method: "diff/integration/named"
336 CAS: "Integrate (f_f, v_v)"
337 Given: "functionTerm f_f" "integrateBy v_v"
338 Find: "antiDerivativeName F_F"
342 partial_function (tailrec) integrate :: "real \<Rightarrow> real \<Rightarrow> real"
344 "integrate f_f v_v = (
346 t_t = Take (Integral f_f D v_v)
348 (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'') t_t)"
350 method met_diffint : "diff/integration" =
351 \<open>{rew_ord'="tless_true", rls'=Atools_erls, calc = [], srls = Rule_Set.empty, prls=Rule_Set.empty,
352 crls = Atools_erls, errpats = [], nrls = Rule_Set.empty}\<close>
353 Program: integrate.simps
354 Given: "functionTerm f_f" "integrateBy v_v"
355 Find: "antiDerivative F_F"
357 partial_function (tailrec) intergrate_named :: "real \<Rightarrow> real \<Rightarrow> (real \<Rightarrow> real) \<Rightarrow> bool"
359 "intergrate_named f_f v_v F_F =(
361 t_t = Take (F_F v_v = Integral f_f D v_v)
363 (Try (Rewrite_Set_Inst [(''bdv'', v_v)] ''simplify_Integral'')) #>
364 (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'')
367 method met_diffint_named : "diff/integration/named" =
368 \<open>{rew_ord'="tless_true", rls'=Atools_erls, calc = [], srls = Rule_Set.empty, prls=Rule_Set.empty,
369 crls = Atools_erls, errpats = [], nrls = Rule_Set.empty}\<close>
370 Program: intergrate_named.simps
371 Given: "functionTerm f_f" "integrateBy v_v"
372 Find: "antiDerivativeName F_F"