src/Tools/isac/Knowledge/Integrate.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Sun, 23 Jun 2019 14:23:09 +0200
changeset 59552 ab7955d2ead3
parent 59551 6ea6d9c377a0
child 59603 30cd47104ad7
permissions -rw-r--r--
funpack: remove remainings of Script, finished.
     1 (* integration over the reals
     2    author: Walther Neuper
     3    050814, 08:51
     4    (c) due to copyright terms
     5 *)
     6 
     7 theory Integrate imports Diff begin
     8 
     9 consts
    10 
    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)
    14 
    15   (*descriptions in the related problems*)
    16   integrateBy         :: "real => una"
    17   antiDerivative      :: "real => una"
    18   antiDerivativeName  :: "(real => real) => una"
    19 
    20   (*the CAS-command, eg. "Integrate (2*x^^^3, x)"*)
    21   Integrate           :: "[real * real] => real"
    22 
    23 axiomatization where
    24 (*stated as axioms, todo: prove as theorems
    25   'bdv' is a constant handled on the meta-level 
    26    specifically as a 'bound variable'            *)
    27 
    28   integral_const:    "Not (bdv occurs_in u) ==> Integral u D bdv = u * bdv" and
    29   integral_var:      "Integral bdv D bdv = bdv ^^^ 2 / 2" and
    30 
    31   integral_add:      "Integral (u + v) D bdv =  
    32 		     (Integral u D bdv) + (Integral v D bdv)" and
    33   integral_mult:     "[| Not (bdv occurs_in u); bdv occurs_in v |] ==>  
    34 		     Integral (u * v) D bdv = u * (Integral v D bdv)" and
    35 (*WN080222: this goes into sub-terms, too ...
    36   call_for_new_c:    "[| Not (matches (u + new_c v) a); Not (a is_f_x) |] ==>  
    37 		     a = a + new_c a"
    38 *)
    39   integral_pow:      "Integral bdv ^^^ n D bdv = bdv ^^^ (n+1) / (n + 1)"
    40 
    41 ML \<open>
    42 val thy = @{theory};
    43 
    44 (** eval functions **)
    45 
    46 val c = Free ("c", HOLogic.realT);
    47 (*.create a new unique variable 'c..' in a term; for use by Rule.Calc 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.*)
    51 fun new_c term = 
    52     let fun selc var = 
    53 	    case (Symbol.explode o id_of) var of
    54 		"c"::[] => true
    55 	      |	"c"::"_"::is => (case (TermC.int_of_str_opt o implode) is of
    56 				     SOME _ => true
    57 				   | NONE => false)
    58               | _ => false;
    59 	fun get_coeff c = case (Symbol.explode o id_of) c of
    60 	      		      "c"::"_"::is => (the o TermC.int_of_str_opt o implode) is
    61 			    | _ => 0;
    62         val cs = filter selc (TermC.vars term);
    63     in 
    64 	case cs of
    65 	    [] => c
    66 	  | [c] => Free ("c_2", HOLogic.realT)
    67 	  | cs => 
    68 	    let val max_coeff = maxl (map get_coeff cs)
    69 	    in Free ("c_"^string_of_int (max_coeff + 1), HOLogic.realT) end
    70     end;
    71 
    72 (*WN080222
    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 ((Rule.term2str p) ^ " = " ^ Rule.term2str (new_c p),
    76 	  Trueprop $ (mk_equality (p, new_c p)))
    77   | eval_new_c _ _ _ _ = NONE;
    78 *)
    79 
    80 (*WN080222:*)
    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 ("HOL.eq", T) $ lh $ rh => 
    87 		     Const ("HOL.eq", T) $ lh $ TermC.mk_add rh (new_c rh)
    88 		   | p => TermC.mk_add p (new_c p)
    89     in SOME ((Rule.term2str p) ^ " = " ^ Rule.term2str p',
    90 	  HOLogic.Trueprop $ (TermC.mk_equality (p, p')))
    91     end
    92   | eval_add_new_c _ _ _ _ = NONE;
    93 
    94 
    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", _)
    97 					   $ arg)) _ =
    98     if TermC.is_f_x arg
    99     then SOME ((Rule.term2str p) ^ " = True",
   100 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term True})))
   101     else SOME ((Rule.term2str p) ^ " = False",
   102 	       HOLogic.Trueprop $ (TermC.mk_equality (p, @{term False})))
   103   | eval_is_f_x _ _ _ _ = NONE;
   104 \<close>
   105 setup \<open>KEStore_Elems.add_calcs
   106   [("add_new_c", ("Integrate.add'_new'_c", eval_add_new_c "add_new_c_")),
   107     ("is_f_x", ("Integrate.is'_f'_x", eval_is_f_x "is_f_idextifier_"))]\<close>
   108 ML \<open>
   109 (** rulesets **)
   110 
   111 (*.rulesets for integration.*)
   112 val integration_rules = 
   113     Rule.Rls {id="integration_rules", preconds = [], 
   114 	 rew_ord = ("termlessI",termlessI), 
   115 	 erls = Rule.Rls {id="conditions_in_integration_rules", 
   116 		     preconds = [], 
   117 		     rew_ord = ("termlessI",termlessI), 
   118 		     erls = Rule.Erls, 
   119 		     srls = Rule.Erls, calc = [], errpatts = [],
   120 		     rules = [(*for rewriting conditions in Thm's*)
   121 			      Rule.Calc ("Atools.occurs'_in", 
   122 				    eval_occurs_in "#occurs_in_"),
   123 			      Rule.Thm ("not_true", TermC.num_str @{thm not_true}),
   124 			      Rule.Thm ("not_false",@{thm not_false})
   125 			      ],
   126 		     scr = Rule.EmptyScr}, 
   127 	 srls = Rule.Erls, calc = [], errpatts = [],
   128 	 rules = [
   129 		  Rule.Thm ("integral_const", TermC.num_str @{thm integral_const}),
   130 		  Rule.Thm ("integral_var", TermC.num_str @{thm integral_var}),
   131 		  Rule.Thm ("integral_add", TermC.num_str @{thm integral_add}),
   132 		  Rule.Thm ("integral_mult", TermC.num_str @{thm integral_mult}),
   133 		  Rule.Thm ("integral_pow", TermC.num_str @{thm integral_pow}),
   134 		  Rule.Calc ("Groups.plus_class.plus", eval_binop "#add_")(*for n+1*)
   135 		  ],
   136 	 scr = Rule.EmptyScr};
   137 \<close>
   138 ML \<open>
   139 val add_new_c = 
   140     Rule.Seq {id="add_new_c", preconds = [], 
   141 	 rew_ord = ("termlessI",termlessI), 
   142 	 erls = Rule.Rls {id="conditions_in_add_new_c", 
   143 		     preconds = [], 
   144 		     rew_ord = ("termlessI",termlessI), 
   145 		     erls = Rule.Erls, 
   146 		     srls = Rule.Erls, calc = [], errpatts = [],
   147 		     rules = [Rule.Calc ("Tools.matches", Tools.eval_matches""),
   148 			      Rule.Calc ("Integrate.is'_f'_x", 
   149 				    eval_is_f_x "is_f_x_"),
   150 			      Rule.Thm ("not_true", TermC.num_str @{thm not_true}),
   151 			      Rule.Thm ("not_false", TermC.num_str @{thm not_false})
   152 			      ],
   153 		     scr = Rule.EmptyScr}, 
   154 	 srls = Rule.Erls, calc = [], errpatts = [],
   155 	 rules = [ (*Rule.Thm ("call_for_new_c", TermC.num_str @{thm call_for_new_c}),*)
   156 		   Rule.Cal1 ("Integrate.add'_new'_c", eval_add_new_c "new_c_")
   157 		   ],
   158 	 scr = Rule.EmptyScr};
   159 \<close>
   160 ML \<open>
   161 
   162 (*.rulesets for simplifying Integrals.*)
   163 
   164 (*.for simplify_Integral adapted from 'norm_Rational_rls'.*)
   165 val norm_Rational_rls_noadd_fractions = 
   166 Rule.Rls {id = "norm_Rational_rls_noadd_fractions", preconds = [], 
   167      rew_ord = ("dummy_ord",Rule.dummy_ord), 
   168      erls = norm_rat_erls, srls = Rule.Erls, calc = [], errpatts = [],
   169      rules = [(*Rule.Rls_ add_fractions_p_rls,!!!*)
   170 	      Rule.Rls_ (*rat_mult_div_pow original corrected WN051028*)
   171 		  (Rule.Rls {id = "rat_mult_div_pow", preconds = [], 
   172 		       rew_ord = ("dummy_ord",Rule.dummy_ord), 
   173 		       erls = (*FIXME.WN051028 Rule.e_rls,*)
   174 		       Rule.append_rls "Rule.e_rls-is_polyexp" Rule.e_rls
   175 				  [Rule.Calc ("Poly.is'_polyexp", 
   176 					 eval_is_polyexp "")],
   177 				  srls = Rule.Erls, calc = [], errpatts = [],
   178 				  rules = [Rule.Thm ("rat_mult", TermC.num_str @{thm rat_mult}),
   179 	       (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
   180 	       Rule.Thm ("rat_mult_poly_l", TermC.num_str @{thm rat_mult_poly_l}),
   181 	       (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
   182 	       Rule.Thm ("rat_mult_poly_r", TermC.num_str @{thm rat_mult_poly_r}),
   183 	       (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
   184 
   185 	       Rule.Thm ("real_divide_divide1_mg",
   186                      TermC.num_str @{thm real_divide_divide1_mg}),
   187 	       (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
   188 	       Rule.Thm ("divide_divide_eq_right", 
   189                      TermC.num_str @{thm divide_divide_eq_right}),
   190 	       (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
   191 	       Rule.Thm ("divide_divide_eq_left",
   192                      TermC.num_str @{thm divide_divide_eq_left}),
   193 	       (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
   194 	       Rule.Calc ("Rings.divide_class.divide"  ,eval_cancel "#divide_e"),
   195 	      
   196 	       Rule.Thm ("rat_power", TermC.num_str @{thm rat_power})
   197 		(*"(?a / ?b) ^^^ ?n = ?a ^^^ ?n / ?b ^^^ ?n"*)
   198 	       ],
   199       scr = Rule.EmptyScr
   200       }),
   201 		Rule.Rls_ make_rat_poly_with_parentheses,
   202 		Rule.Rls_ cancel_p_rls,(*FIXME:cancel_p does NOT order sometimes*)
   203 		Rule.Rls_ rat_reduce_1
   204 		],
   205        scr = Rule.EmptyScr
   206        };
   207 
   208 (*.for simplify_Integral adapted from 'norm_Rational'.*)
   209 val norm_Rational_noadd_fractions = 
   210    Rule.Seq {id = "norm_Rational_noadd_fractions", preconds = [], 
   211        rew_ord = ("dummy_ord",Rule.dummy_ord), 
   212        erls = norm_rat_erls, srls = Rule.Erls, calc = [], errpatts = [],
   213        rules = [Rule.Rls_ discard_minus,
   214 		Rule.Rls_ rat_mult_poly,(* removes double fractions like a/b/c    *)
   215 		Rule.Rls_ make_rat_poly_with_parentheses, (*WN0510 also in(#)below*)
   216 		Rule.Rls_ cancel_p_rls, (*FIXME.MG:cancel_p does NOT order sometim*)
   217 		Rule.Rls_ norm_Rational_rls_noadd_fractions,(* the main rls (#)   *)
   218 		Rule.Rls_ discard_parentheses1 (* mult only                       *)
   219 		],
   220        scr = Rule.EmptyScr
   221        };
   222 
   223 (*.simplify terms before and after Integration such that  
   224    ..a.x^2/2 + b.x^3/3.. is made to ..a/2.x^2 + b/3.x^3.. (and NO
   225    common denominator as done by norm_Rational or make_ratpoly_in.
   226    This is a copy from 'make_ratpoly_in' with respective reduction of rules and
   227    *1* expand the term, ie. distribute * and / over +
   228 .*)
   229 val separate_bdv2 =
   230     Rule.append_rls "separate_bdv2"
   231 	       collect_bdv
   232 	       [Rule.Thm ("separate_bdv", TermC.num_str @{thm separate_bdv}),
   233 		(*"?a * ?bdv / ?b = ?a / ?b * ?bdv"*)
   234 		Rule.Thm ("separate_bdv_n", TermC.num_str @{thm separate_bdv_n}),
   235 		Rule.Thm ("separate_1_bdv",  TermC.num_str @{thm separate_1_bdv}),
   236 		(*"?bdv / ?b = (1 / ?b) * ?bdv"*)
   237 		Rule.Thm ("separate_1_bdv_n",  TermC.num_str @{thm separate_1_bdv_n})(*,
   238 			  (*"?bdv ^^^ ?n / ?b = 1 / ?b * ?bdv ^^^ ?n"*)
   239 			  *****Rule.Thm ("add_divide_distrib", 
   240 			  ***** TermC.num_str @{thm add_divide_distrib})
   241 			  (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)----------*)
   242 		];
   243 val simplify_Integral = 
   244   Rule.Seq {id = "simplify_Integral", preconds = []:term list, 
   245        rew_ord = ("dummy_ord", Rule.dummy_ord),
   246       erls = Atools_erls, srls = Rule.Erls,
   247       calc = [],  errpatts = [],
   248       rules = [Rule.Thm ("distrib_right", TermC.num_str @{thm distrib_right}),
   249  	       (*"(?z1.0 + ?z2.0) * ?w = ?z1.0 * ?w + ?z2.0 * ?w"*)
   250 	       Rule.Thm ("add_divide_distrib", TermC.num_str @{thm add_divide_distrib}),
   251  	       (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)
   252 	       (*^^^^^ *1* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*)
   253 	       Rule.Rls_ norm_Rational_noadd_fractions,
   254 	       Rule.Rls_ order_add_mult_in,
   255 	       Rule.Rls_ discard_parentheses,
   256 	       (*Rule.Rls_ collect_bdv, from make_polynomial_in*)
   257 	       Rule.Rls_ separate_bdv2,
   258 	       Rule.Calc ("Rings.divide_class.divide"  ,eval_cancel "#divide_e")
   259 	       ],
   260       scr = Rule.EmptyScr};      
   261 
   262 
   263 (*simplify terms before and after Integration such that  
   264    ..a.x^2/2 + b.x^3/3.. is made to ..a/2.x^2 + b/3.x^3.. (and NO
   265    common denominator as done by norm_Rational or make_ratpoly_in.
   266    This is a copy from 'make_polynomial_in' with insertions from 
   267    'make_ratpoly_in' 
   268 THIS IS KEPT FOR COMPARISON ............................................   
   269 * val simplify_Integral = prep_rls'(
   270 *   Rule.Seq {id = "", preconds = []:term list, 
   271 *        rew_ord = ("dummy_ord", Rule.dummy_ord),
   272 *       erls = Atools_erls, srls = Rule.Erls,
   273 *       calc = [], (*asm_thm = [],*)
   274 *       rules = [Rule.Rls_ expand_poly,
   275 * 	       Rule.Rls_ order_add_mult_in,
   276 * 	       Rule.Rls_ simplify_power,
   277 * 	       Rule.Rls_ collect_numerals,
   278 * 	       Rule.Rls_ reduce_012,
   279 * 	       Rule.Thm ("realpow_oneI", TermC.num_str @{thm realpow_oneI}),
   280 * 	       Rule.Rls_ discard_parentheses,
   281 * 	       Rule.Rls_ collect_bdv,
   282 * 	       (*below inserted from 'make_ratpoly_in'*)
   283 * 	       Rule.Rls_ (Rule.append_rls "separate_bdv"
   284 * 			 collect_bdv
   285 * 			 [Rule.Thm ("separate_bdv", TermC.num_str @{thm separate_bdv}),
   286 * 			  (*"?a * ?bdv / ?b = ?a / ?b * ?bdv"*)
   287 * 			  Rule.Thm ("separate_bdv_n", TermC.num_str @{thm separate_bdv_n}),
   288 * 			  Rule.Thm ("separate_1_bdv", TermC.num_str @{thm separate_1_bdv}),
   289 * 			  (*"?bdv / ?b = (1 / ?b) * ?bdv"*)
   290 * 			  Rule.Thm ("separate_1_bdv_n", TermC.num_str @{thm separate_1_bdv_n})(*,
   291 * 			  (*"?bdv ^^^ ?n / ?b = 1 / ?b * ?bdv ^^^ ?n"*)
   292 * 			  Rule.Thm ("add_divide_distrib", 
   293 * 				  TermC.num_str @{thm add_divide_distrib})
   294 * 			   (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)*)
   295 * 			  ]),
   296 * 	       Rule.Calc ("Rings.divide_class.divide"  ,eval_cancel "#divide_e")
   297 * 	       ],
   298 *       scr = Rule.EmptyScr
   299 *       }); 
   300 .......................................................................*)
   301 
   302 val integration = 
   303     Rule.Seq {id="integration", preconds = [], 
   304 	 rew_ord = ("termlessI",termlessI), 
   305 	 erls = Rule.Rls {id="conditions_in_integration", 
   306 		     preconds = [], 
   307 		     rew_ord = ("termlessI",termlessI), 
   308 		     erls = Rule.Erls, 
   309 		     srls = Rule.Erls, calc = [], errpatts = [],
   310 		     rules = [],
   311 		     scr = Rule.EmptyScr}, 
   312 	 srls = Rule.Erls, calc = [], errpatts = [],
   313 	 rules = [ Rule.Rls_ integration_rules,
   314 		   Rule.Rls_ add_new_c,
   315 		   Rule.Rls_ simplify_Integral
   316 		   ],
   317 	 scr = Rule.EmptyScr};
   318 
   319 val prep_rls' = LTool.prep_rls @{theory};
   320 \<close>
   321 setup \<open>KEStore_Elems.add_rlss 
   322   [("integration_rules", (Context.theory_name @{theory}, prep_rls' integration_rules)), 
   323   ("add_new_c", (Context.theory_name @{theory}, prep_rls' add_new_c)), 
   324   ("simplify_Integral", (Context.theory_name @{theory}, prep_rls' simplify_Integral)), 
   325   ("integration", (Context.theory_name @{theory}, prep_rls' integration)), 
   326   ("separate_bdv2", (Context.theory_name @{theory}, prep_rls' separate_bdv2)),
   327 
   328   ("norm_Rational_noadd_fractions", (Context.theory_name @{theory},
   329     prep_rls' norm_Rational_noadd_fractions)), 
   330   ("norm_Rational_rls_noadd_fractions", (Context.theory_name @{theory},
   331     prep_rls' norm_Rational_rls_noadd_fractions))]\<close>
   332 
   333 (** problems **)
   334 setup \<open>KEStore_Elems.add_pbts
   335   [(Specify.prep_pbt thy "pbl_fun_integ" [] Celem.e_pblID
   336       (["integrate","function"],
   337         [("#Given" ,["functionTerm f_f", "integrateBy v_v"]),
   338           ("#Find"  ,["antiDerivative F_F"])],
   339         Rule.append_rls "e_rls" Rule.e_rls [(*for preds in where_*)], 
   340         SOME "Integrate (f_f, v_v)", 
   341         [["diff","integration"]])),
   342     (*here "named" is used differently from Differentiation"*)
   343     (Specify.prep_pbt thy "pbl_fun_integ_nam" [] Celem.e_pblID
   344       (["named","integrate","function"],
   345         [("#Given" ,["functionTerm f_f", "integrateBy v_v"]),
   346           ("#Find"  ,["antiDerivativeName F_F"])],
   347         Rule.append_rls "e_rls" Rule.e_rls [(*for preds in where_*)], 
   348         SOME "Integrate (f_f, v_v)", 
   349         [["diff","integration","named"]]))]\<close>
   350 
   351 (** methods **)
   352 
   353 partial_function (tailrec) integrate :: "real \<Rightarrow> real \<Rightarrow> real"
   354   where
   355 "integrate f_f v_v =
   356   (let t_t = Take (Integral f_f D v_v)
   357    in (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'' False) t_t)"
   358 setup \<open>KEStore_Elems.add_mets
   359     [Specify.prep_met thy "met_diffint" [] Celem.e_metID
   360 	    (["diff","integration"],
   361 	      [("#Given" ,["functionTerm f_f", "integrateBy v_v"]), ("#Find"  ,["antiDerivative F_F"])],
   362 	      {rew_ord'="tless_true", rls'=Atools_erls, calc = [], srls = Rule.e_rls, prls=Rule.e_rls,
   363 	        crls = Atools_erls, errpats = [], nrls = Rule.e_rls},
   364 	      @{thm integrate.simps})]
   365 \<close>
   366 
   367 partial_function (tailrec) intergrate_named :: "real \<Rightarrow> real \<Rightarrow> (real \<Rightarrow> real) \<Rightarrow> bool"
   368   where "intergrate_named f_f v_v F_F =
   369   (let t_t = Take (F_F v_v = Integral f_f D v_v)
   370    in ((Try (Rewrite_Set_Inst [(''bdv'', v_v)] ''simplify_Integral'' False)) @@
   371        (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'' False)) t_t)"
   372 setup \<open>KEStore_Elems.add_mets
   373     [Specify.prep_met thy "met_diffint_named" [] Celem.e_metID
   374 	    (["diff","integration","named"],
   375 	      [("#Given" ,["functionTerm f_f", "integrateBy v_v"]),
   376 	        ("#Find"  ,["antiDerivativeName F_F"])],
   377 	      {rew_ord'="tless_true", rls'=Atools_erls, calc = [], srls = Rule.e_rls, prls=Rule.e_rls,
   378           crls = Atools_erls, errpats = [], nrls = Rule.e_rls},
   379         @{thm intergrate_named.simps})]
   380 \<close>
   381 
   382 end