src/Tools/isac/Knowledge/Integrate.thy
author wneuper <Walther.Neuper@jku.at>
Thu, 04 Aug 2022 12:48:37 +0200
changeset 60509 2e0b7ca391dc
parent 60504 8cc1415b3530
child 60515 03e19793d81e
permissions -rw-r--r--
polish naming in Rewrite_Order
     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   add_new_c          :: "real => real"        ("add'_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 \<up> 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 (*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
    31 
    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) |] ==>  
    38 		     a = a + new_c a"
    39 *)
    40   integral_pow:      "Integral bdv \<up> n D bdv = bdv \<up> (n+1) / (n + 1)"
    41 (*Ambiguous input\<^here> produces 3 parse trees -----------------------------//*)
    42 
    43 ML \<open>
    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_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.*)
    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_opt_of_string 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_opt_of_string o implode) is
    61 			    | _ => 0;
    62         val cs = filter selc (TermC.vars term);
    63     in 
    64 	case cs of
    65 	    [] => c
    66 	  | [_] => 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 (\<^const_name>\<open>Integrate.new_c\<close>,_) $ t)) _ =
    75      SOME ((UnparseC.term p) ^ " = " ^ UnparseC.term (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 (_:Proof.context) =
    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')))
    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 (\<^const_name>\<open>Integrate.is_f_x\<close>, _)
    97 					   $ arg)) _ =
    98     if TermC.is_f_x arg
    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;
   104 \<close>
   105 
   106 calculation add_new_c = \<open>eval_add_new_c "add_new_c_"\<close>
   107 calculation is_f_x = \<open>eval_is_f_x "is_f_idextifier_"\<close>
   108 
   109 ML \<open>
   110 (** rulesets **)
   111 
   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", 
   117    	  preconds = [], 
   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>],
   125    	  scr = Rule.Empty_Prog}, 
   126     srls = Rule_Set.Empty, calc = [], errpatts = [],
   127     rules = [
   128    	  \<^rule_thm>\<open>integral_const\<close>,
   129    	  \<^rule_thm>\<open>integral_var\<close>,
   130    	  \<^rule_thm>\<open>integral_add\<close>,
   131    	  \<^rule_thm>\<open>integral_mult\<close>,
   132    	  \<^rule_thm>\<open>integral_pow\<close>,
   133    	  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")(*for n+1*)],
   134     scr = Rule.Empty_Prog};
   135 \<close>
   136 ML \<open>
   137 val add_new_c = 
   138   Rule_Set.Sequence {id="add_new_c", preconds = [], 
   139     rew_ord = ("termlessI",termlessI), 
   140     erls = Rule_Def.Repeat {id="conditions_in_add_new_c", 
   141       preconds = [], rew_ord = ("termlessI",termlessI), erls = Rule_Set.Empty, 
   142       srls = Rule_Set.Empty, calc = [], errpatts = [],
   143       rules = [
   144         \<^rule_eval>\<open>Prog_Expr.matches\<close> (Prog_Expr.eval_matches""),
   145    	    \<^rule_eval>\<open>Integrate.is_f_x\<close> (eval_is_f_x "is_f_x_"),
   146    	    \<^rule_thm>\<open>not_true\<close>,
   147    	    \<^rule_thm>\<open>not_false\<close>],
   148       scr = Rule.Empty_Prog}, 
   149     srls = Rule_Set.Empty, calc = [], errpatts = [],
   150     rules = [ (*\<^rule_thm>\<open>call_for_new_c\<close>,*)
   151       Rule.Cal1 ("Integrate.add_new_c", eval_add_new_c "new_c_")],
   152     scr = Rule.Empty_Prog};
   153 \<close>
   154 ML \<open>
   155 
   156 (*.rulesets for simplifying Integrals.*)
   157 
   158 (*.for simplify_Integral adapted from 'norm_Rational_rls'.*)
   159 val norm_Rational_rls_noadd_fractions = 
   160   Rule_Def.Repeat {id = "norm_Rational_rls_noadd_fractions", preconds = [], 
   161     rew_ord = ("dummy_ord",Rewrite_Ord.function_empty), 
   162     erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   163     rules = [(*Rule.Rls_ add_fractions_p_rls,!!!*)
   164   	  Rule.Rls_ (*rat_mult_div_pow original corrected WN051028*)
   165   		(Rule_Def.Repeat {id = "rat_mult_div_pow", preconds = [], 
   166   		   rew_ord = ("dummy_ord",Rewrite_Ord.function_empty), 
   167   		   erls = Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty
   168   				 [\<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp "")],
   169   			 srls = Rule_Set.Empty, calc = [], errpatts = [],
   170   		   rules = [
   171            \<^rule_thm>\<open>rat_mult\<close>, (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
   172   	       \<^rule_thm>\<open>rat_mult_poly_l\<close>, (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
   173   	       \<^rule_thm>\<open>rat_mult_poly_r\<close>, (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
   174   
   175   	       \<^rule_thm>\<open>real_divide_divide1_mg\<close>, (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
   176   	       \<^rule_thm>\<open>divide_divide_eq_right\<close>, (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
   177   	       \<^rule_thm>\<open>divide_divide_eq_left\<close>, (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
   178   	       \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e"),
   179   	      
   180   	       \<^rule_thm>\<open>rat_power\<close>], (*"(?a / ?b)  \<up>  ?n = ?a  \<up>  ?n / ?b  \<up>  ?n"*)
   181         scr = Rule.Empty_Prog}),
   182   		Rule.Rls_ make_rat_poly_with_parentheses,
   183   		Rule.Rls_ cancel_p_rls,(*FIXME:cancel_p does NOT order sometimes*)
   184   		Rule.Rls_ rat_reduce_1],
   185     scr = Rule.Empty_Prog};
   186 
   187 (*.for simplify_Integral adapted from 'norm_Rational'.*)
   188 val norm_Rational_noadd_fractions = 
   189   Rule_Set.Sequence {id = "norm_Rational_noadd_fractions", preconds = [], 
   190     rew_ord = ("dummy_ord",Rewrite_Ord.function_empty), 
   191     erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   192     rules = [Rule.Rls_ discard_minus,
   193   		Rule.Rls_ rat_mult_poly,(* removes double fractions like a/b/c    *)
   194   		Rule.Rls_ make_rat_poly_with_parentheses, (*WN0510 also in(#)below*)
   195   		Rule.Rls_ cancel_p_rls, (*FIXME.MG:cancel_p does NOT order sometim*)
   196   		Rule.Rls_ norm_Rational_rls_noadd_fractions,(* the main rls (#)   *)
   197   		Rule.Rls_ discard_parentheses1], (* mult only                       *)
   198     scr = Rule.Empty_Prog};
   199 
   200 (*.simplify terms before and after Integration such that  
   201    ..a.x^2/2 + b.x^3/3.. is made to ..a/2.x^2 + b/3.x^3.. (and NO
   202    common denominator as done by norm_Rational or make_ratpoly_in.
   203    This is a copy from 'make_ratpoly_in' with respective reduction of rules and
   204    *1* expand the term, ie. distribute * and / over +
   205 .*)
   206 val separate_bdv2 =
   207    Rule_Set.append_rules "separate_bdv2" collect_bdv [
   208     \<^rule_thm>\<open>separate_bdv\<close>, (*"?a * ?bdv / ?b = ?a / ?b * ?bdv"*)
   209 		\<^rule_thm>\<open>separate_bdv_n\<close>,
   210 		\<^rule_thm>\<open>separate_1_bdv\<close>, (*"?bdv / ?b = (1 / ?b) * ?bdv"*)
   211 		\<^rule_thm>\<open>separate_1_bdv_n\<close> (*"?bdv  \<up>  ?n / ?b = 1 / ?b * ?bdv  \<up>  ?n"*)
   212     (*
   213 		rule_thm>\<open>add_divide_distrib\<close> (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)*)
   214 		];
   215 val simplify_Integral = 
   216   Rule_Set.Sequence {id = "simplify_Integral", preconds = []:term list, 
   217     rew_ord = ("dummy_ord", Rewrite_Ord.function_empty),
   218     erls = Atools_erls, srls = Rule_Set.Empty,
   219     calc = [],  errpatts = [],
   220     rules = [
   221       \<^rule_thm>\<open>distrib_right\<close>, (*"(?z1.0 + ?z2.0) * ?w = ?z1.0 * ?w + ?z2.0 * ?w"*)
   222 	    \<^rule_thm>\<open>add_divide_distrib\<close>, (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)
   223 	     (*^^^^^ *1* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*)
   224 	    Rule.Rls_ norm_Rational_noadd_fractions,
   225 	    Rule.Rls_ order_add_mult_in,
   226 	    Rule.Rls_ discard_parentheses,
   227 	    (*Rule.Rls_ collect_bdv, from make_polynomial_in*)
   228 	    Rule.Rls_ separate_bdv2,
   229 	    \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e")],
   230     scr = Rule.Empty_Prog};      
   231 
   232 val integration =
   233   Rule_Set.Sequence {
   234      id="integration", preconds = [], rew_ord = ("termlessI",termlessI), 
   235   	 erls = Rule_Def.Repeat {
   236        id="conditions_in_integration",  preconds = [], rew_ord = ("termlessI",termlessI), 
   237   		 erls = Rule_Set.Empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
   238   		 rules = [], scr = Rule.Empty_Prog}, 
   239   	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   240   	 rules = [
   241       Rule.Rls_ integration_rules,
   242   		 Rule.Rls_ add_new_c,
   243   		 Rule.Rls_ simplify_Integral],
   244   	 scr = Rule.Empty_Prog};
   245 
   246 val prep_rls' = Auto_Prog.prep_rls @{theory};
   247 \<close>
   248 rule_set_knowledge
   249   integration_rules = \<open>prep_rls' integration_rules\<close> and
   250   add_new_c = \<open>prep_rls' add_new_c\<close> and
   251   simplify_Integral = \<open>prep_rls' simplify_Integral\<close> and
   252   integration = \<open>prep_rls' integration\<close> and
   253   separate_bdv2 = \<open>prep_rls' separate_bdv2\<close> and
   254   norm_Rational_noadd_fractions = \<open>prep_rls' norm_Rational_noadd_fractions\<close> and
   255   norm_Rational_rls_noadd_fractions = \<open>prep_rls' norm_Rational_rls_noadd_fractions\<close>
   256 
   257 (** problems **)
   258 
   259 problem pbl_fun_integ : "integrate/function" =
   260   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   261   Method_Ref: "diff/integration"
   262   CAS: "Integrate (f_f, v_v)"
   263   Given: "functionTerm f_f" "integrateBy v_v"
   264   Find: "antiDerivative F_F"
   265 
   266 problem pbl_fun_integ_nam : "named/integrate/function" =
   267   (*here "named" is used differently from Differentiation"*)
   268   \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
   269   Method_Ref: "diff/integration/named"
   270   CAS: "Integrate (f_f, v_v)"
   271   Given: "functionTerm f_f" "integrateBy v_v"
   272   Find: "antiDerivativeName F_F"
   273 
   274 (** methods **)
   275 
   276 partial_function (tailrec) integrate :: "real \<Rightarrow> real \<Rightarrow> real"
   277   where
   278 "integrate f_f v_v = (
   279   let
   280     t_t = Take (Integral f_f D v_v)
   281   in
   282     (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'') t_t)"
   283 
   284 method met_diffint : "diff/integration" =
   285   \<open>{rew_ord'="tless_true", rls'=Atools_erls, calc = [], srls = Rule_Set.empty, prls=Rule_Set.empty,
   286 	  crls = Atools_erls, errpats = [], nrls = Rule_Set.empty}\<close>
   287   Program: integrate.simps
   288   Given: "functionTerm f_f" "integrateBy v_v"
   289   Find: "antiDerivative F_F"
   290 
   291 partial_function (tailrec) intergrate_named :: "real \<Rightarrow> real \<Rightarrow> (real \<Rightarrow> real) \<Rightarrow> bool"
   292   where
   293 "intergrate_named f_f v_v F_F =(
   294   let
   295     t_t = Take (F_F v_v = Integral f_f D v_v)
   296   in (
   297     (Try (Rewrite_Set_Inst [(''bdv'', v_v)] ''simplify_Integral'')) #>
   298     (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'')
   299     ) t_t)"
   300 
   301 method met_diffint_named : "diff/integration/named" =
   302   \<open>{rew_ord'="tless_true", rls'=Atools_erls, calc = [], srls = Rule_Set.empty, prls=Rule_Set.empty,
   303     crls = Atools_erls, errpats = [], nrls = Rule_Set.empty}\<close>
   304   Program: intergrate_named.simps
   305   Given: "functionTerm f_f" "integrateBy v_v"
   306   Find: "antiDerivativeName F_F"
   307 
   308 ML \<open>
   309 \<close> ML \<open>
   310 \<close>
   311 
   312 end