src/Tools/isac/Knowledge/Integrate.thy
author wenzelm
Fri, 11 Jun 2021 11:49:34 +0200
changeset 60294 6623f5cdcb19
parent 60291 52921aa0e14a
child 60297 73e7175a7d3f
permissions -rw-r--r--
ML antiquotation for formally checked Rule.Eval;
     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 \<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 ("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;
    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 ((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 ("Integrate.is_f_x", _)
    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 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_Def.Repeat {id="integration_rules", preconds = [], 
   114 	 rew_ord = ("termlessI",termlessI), 
   115 	 erls = Rule_Def.Repeat {id="conditions_in_integration_rules", 
   116 		     preconds = [], 
   117 		     rew_ord = ("termlessI",termlessI), 
   118 		     erls = Rule_Set.Empty, 
   119 		     srls = Rule_Set.Empty, calc = [], errpatts = [],
   120 		     rules = [(*for rewriting conditions in Thm's*)
   121 			      \<^rule_eval>\<open>Prog_Expr.occurs_in\<close> (Prog_Expr.eval_occurs_in "#occurs_in_"),
   122 			      Rule.Thm ("not_true", ThmC.numerals_to_Free @{thm not_true}),
   123 			      Rule.Thm ("not_false",@{thm not_false})
   124 			      ],
   125 		     scr = Rule.Empty_Prog}, 
   126 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   127 	 rules = [
   128 		  Rule.Thm ("integral_const", ThmC.numerals_to_Free @{thm integral_const}),
   129 		  Rule.Thm ("integral_var", ThmC.numerals_to_Free @{thm integral_var}),
   130 		  Rule.Thm ("integral_add", ThmC.numerals_to_Free @{thm integral_add}),
   131 		  Rule.Thm ("integral_mult", ThmC.numerals_to_Free @{thm integral_mult}),
   132 		  Rule.Thm ("integral_pow", ThmC.numerals_to_Free @{thm integral_pow}),
   133 		  \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")(*for n+1*)
   134 		  ],
   135 	 scr = Rule.Empty_Prog};
   136 \<close>
   137 ML \<open>
   138 val add_new_c = 
   139     Rule_Set.Sequence {id="add_new_c", preconds = [], 
   140 	 rew_ord = ("termlessI",termlessI), 
   141 	 erls = Rule_Def.Repeat {id="conditions_in_add_new_c", 
   142 		     preconds = [], 
   143 		     rew_ord = ("termlessI",termlessI), 
   144 		     erls = Rule_Set.Empty, 
   145 		     srls = Rule_Set.Empty, calc = [], errpatts = [],
   146 		     rules = [\<^rule_eval>\<open>Prog_Expr.matches\<close> (Prog_Expr.eval_matches""),
   147 			      \<^rule_eval>\<open>Integrate.is_f_x\<close> (eval_is_f_x "is_f_x_"),
   148 			      Rule.Thm ("not_true", ThmC.numerals_to_Free @{thm not_true}),
   149 			      Rule.Thm ("not_false", ThmC.numerals_to_Free @{thm not_false})
   150 			      ],
   151 		     scr = Rule.Empty_Prog}, 
   152 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   153 	 rules = [ (*Rule.Thm ("call_for_new_c", ThmC.numerals_to_Free @{thm call_for_new_c}),*)
   154 		   Rule.Cal1 ("Integrate.add_new_c", eval_add_new_c "new_c_")
   155 		   ],
   156 	 scr = Rule.Empty_Prog};
   157 \<close>
   158 ML \<open>
   159 
   160 (*.rulesets for simplifying Integrals.*)
   161 
   162 (*.for simplify_Integral adapted from 'norm_Rational_rls'.*)
   163 val norm_Rational_rls_noadd_fractions = 
   164 Rule_Def.Repeat {id = "norm_Rational_rls_noadd_fractions", preconds = [], 
   165      rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
   166      erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   167      rules = [(*Rule.Rls_ add_fractions_p_rls,!!!*)
   168 	      Rule.Rls_ (*rat_mult_div_pow original corrected WN051028*)
   169 		  (Rule_Def.Repeat {id = "rat_mult_div_pow", preconds = [], 
   170 		       rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
   171 		       erls = (*FIXME.WN051028 Rule_Set.empty,*)
   172 		       Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty
   173 				  [\<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp "")],
   174 				  srls = Rule_Set.Empty, calc = [], errpatts = [],
   175 				  rules = [Rule.Thm ("rat_mult", ThmC.numerals_to_Free @{thm rat_mult}),
   176 	       (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
   177 	       Rule.Thm ("rat_mult_poly_l", ThmC.numerals_to_Free @{thm rat_mult_poly_l}),
   178 	       (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
   179 	       Rule.Thm ("rat_mult_poly_r", ThmC.numerals_to_Free @{thm rat_mult_poly_r}),
   180 	       (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
   181 
   182 	       Rule.Thm ("real_divide_divide1_mg",
   183                      ThmC.numerals_to_Free @{thm real_divide_divide1_mg}),
   184 	       (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
   185 	       Rule.Thm ("divide_divide_eq_right", 
   186                      ThmC.numerals_to_Free @{thm divide_divide_eq_right}),
   187 	       (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
   188 	       Rule.Thm ("divide_divide_eq_left",
   189                      ThmC.numerals_to_Free @{thm divide_divide_eq_left}),
   190 	       (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
   191 	       \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e"),
   192 	      
   193 	       Rule.Thm ("rat_power", ThmC.numerals_to_Free @{thm rat_power})
   194 		(*"(?a / ?b)  \<up>  ?n = ?a  \<up>  ?n / ?b  \<up>  ?n"*)
   195 	       ],
   196       scr = Rule.Empty_Prog
   197       }),
   198 		Rule.Rls_ make_rat_poly_with_parentheses,
   199 		Rule.Rls_ cancel_p_rls,(*FIXME:cancel_p does NOT order sometimes*)
   200 		Rule.Rls_ rat_reduce_1
   201 		],
   202        scr = Rule.Empty_Prog
   203        };
   204 
   205 (*.for simplify_Integral adapted from 'norm_Rational'.*)
   206 val norm_Rational_noadd_fractions = 
   207    Rule_Set.Sequence {id = "norm_Rational_noadd_fractions", preconds = [], 
   208        rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
   209        erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
   210        rules = [Rule.Rls_ discard_minus,
   211 		Rule.Rls_ rat_mult_poly,(* removes double fractions like a/b/c    *)
   212 		Rule.Rls_ make_rat_poly_with_parentheses, (*WN0510 also in(#)below*)
   213 		Rule.Rls_ cancel_p_rls, (*FIXME.MG:cancel_p does NOT order sometim*)
   214 		Rule.Rls_ norm_Rational_rls_noadd_fractions,(* the main rls (#)   *)
   215 		Rule.Rls_ discard_parentheses1 (* mult only                       *)
   216 		],
   217        scr = Rule.Empty_Prog
   218        };
   219 
   220 (*.simplify terms before and after Integration such that  
   221    ..a.x^2/2 + b.x^3/3.. is made to ..a/2.x^2 + b/3.x^3.. (and NO
   222    common denominator as done by norm_Rational or make_ratpoly_in.
   223    This is a copy from 'make_ratpoly_in' with respective reduction of rules and
   224    *1* expand the term, ie. distribute * and / over +
   225 .*)
   226 val separate_bdv2 =
   227     Rule_Set.append_rules "separate_bdv2"
   228 	       collect_bdv
   229 	       [Rule.Thm ("separate_bdv", ThmC.numerals_to_Free @{thm separate_bdv}),
   230 		(*"?a * ?bdv / ?b = ?a / ?b * ?bdv"*)
   231 		Rule.Thm ("separate_bdv_n", ThmC.numerals_to_Free @{thm separate_bdv_n}),
   232 		Rule.Thm ("separate_1_bdv",  ThmC.numerals_to_Free @{thm separate_1_bdv}),
   233 		(*"?bdv / ?b = (1 / ?b) * ?bdv"*)
   234 		Rule.Thm ("separate_1_bdv_n",  ThmC.numerals_to_Free @{thm separate_1_bdv_n})(*,
   235 			  (*"?bdv  \<up>  ?n / ?b = 1 / ?b * ?bdv  \<up>  ?n"*)
   236 			  *****Rule.Thm ("add_divide_distrib", 
   237 			  ***** ThmC.numerals_to_Free @{thm add_divide_distrib})
   238 			  (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)----------*)
   239 		];
   240 val simplify_Integral = 
   241   Rule_Set.Sequence {id = "simplify_Integral", preconds = []:term list, 
   242        rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
   243       erls = Atools_erls, srls = Rule_Set.Empty,
   244       calc = [],  errpatts = [],
   245       rules = [Rule.Thm ("distrib_right", ThmC.numerals_to_Free @{thm distrib_right}),
   246  	       (*"(?z1.0 + ?z2.0) * ?w = ?z1.0 * ?w + ?z2.0 * ?w"*)
   247 	       Rule.Thm ("add_divide_distrib", ThmC.numerals_to_Free @{thm add_divide_distrib}),
   248  	       (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)
   249 	       (*^^^^^ *1* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*)
   250 	       Rule.Rls_ norm_Rational_noadd_fractions,
   251 	       Rule.Rls_ order_add_mult_in,
   252 	       Rule.Rls_ discard_parentheses,
   253 	       (*Rule.Rls_ collect_bdv, from make_polynomial_in*)
   254 	       Rule.Rls_ separate_bdv2,
   255 	       \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e")
   256 	       ],
   257       scr = Rule.Empty_Prog};      
   258 
   259 
   260 (*simplify terms before and after Integration such that  
   261    ..a.x^2/2 + b.x^3/3.. is made to ..a/2.x^2 + b/3.x^3.. (and NO
   262    common denominator as done by norm_Rational or make_ratpoly_in.
   263    This is a copy from 'make_polynomial_in' with insertions from 
   264    'make_ratpoly_in' 
   265 THIS IS KEPT FOR COMPARISON ............................................   
   266 * val simplify_Integral = prep_rls'(
   267 *   Rule_Set.Sequence {id = "", preconds = []:term list, 
   268 *        rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
   269 *       erls = Atools_erls, srls = Rule_Set.Empty,
   270 *       calc = [], (*asm_thm = [],*)
   271 *       rules = [Rule.Rls_ expand_poly,
   272 * 	       Rule.Rls_ order_add_mult_in,
   273 * 	       Rule.Rls_ simplify_power,
   274 * 	       Rule.Rls_ collect_numerals,
   275 * 	       Rule.Rls_ reduce_012,
   276 * 	       Rule.Thm ("realpow_oneI", ThmC.numerals_to_Free @{thm realpow_oneI}),
   277 * 	       Rule.Rls_ discard_parentheses,
   278 * 	       Rule.Rls_ collect_bdv,
   279 * 	       (*below inserted from 'make_ratpoly_in'*)
   280 * 	       Rule.Rls_ (Rule_Set.append_rules "separate_bdv"
   281 * 			 collect_bdv
   282 * 			 [Rule.Thm ("separate_bdv", ThmC.numerals_to_Free @{thm separate_bdv}),
   283 * 			  (*"?a * ?bdv / ?b = ?a / ?b * ?bdv"*)
   284 * 			  Rule.Thm ("separate_bdv_n", ThmC.numerals_to_Free @{thm separate_bdv_n}),
   285 * 			  Rule.Thm ("separate_1_bdv", ThmC.numerals_to_Free @{thm separate_1_bdv}),
   286 * 			  (*"?bdv / ?b = (1 / ?b) * ?bdv"*)
   287 * 			  Rule.Thm ("separate_1_bdv_n", ThmC.numerals_to_Free @{thm separate_1_bdv_n})(*,
   288 * 			  (*"?bdv  \<up>  ?n / ?b = 1 / ?b * ?bdv  \<up>  ?n"*)
   289 * 			  Rule.Thm ("add_divide_distrib", 
   290 * 				  ThmC.numerals_to_Free @{thm add_divide_distrib})
   291 * 			   (*"(?x + ?y) / ?z = ?x / ?z + ?y / ?z"*)*)
   292 * 			  ]),
   293 * 	       \<^rule_eval>\<open>divide\<close> (eval_cancel "#divide_e")
   294 * 	       ],
   295 *       scr = Rule.Empty_Prog
   296 *       }); 
   297 .......................................................................*)
   298 
   299 val integration = 
   300     Rule_Set.Sequence {id="integration", preconds = [], 
   301 	 rew_ord = ("termlessI",termlessI), 
   302 	 erls = Rule_Def.Repeat {id="conditions_in_integration", 
   303 		     preconds = [], 
   304 		     rew_ord = ("termlessI",termlessI), 
   305 		     erls = Rule_Set.Empty, 
   306 		     srls = Rule_Set.Empty, calc = [], errpatts = [],
   307 		     rules = [],
   308 		     scr = Rule.Empty_Prog}, 
   309 	 srls = Rule_Set.Empty, calc = [], errpatts = [],
   310 	 rules = [ Rule.Rls_ integration_rules,
   311 		   Rule.Rls_ add_new_c,
   312 		   Rule.Rls_ simplify_Integral
   313 		   ],
   314 	 scr = Rule.Empty_Prog};
   315 
   316 val prep_rls' = Auto_Prog.prep_rls @{theory};
   317 \<close>
   318 rule_set_knowledge
   319   integration_rules = \<open>prep_rls' integration_rules\<close> and
   320   add_new_c = \<open>prep_rls' add_new_c\<close> and
   321   simplify_Integral = \<open>prep_rls' simplify_Integral\<close> and
   322   integration = \<open>prep_rls' integration\<close> and
   323   separate_bdv2 = \<open>prep_rls' separate_bdv2\<close> and
   324   norm_Rational_noadd_fractions = \<open>prep_rls' norm_Rational_noadd_fractions\<close> and
   325   norm_Rational_rls_noadd_fractions = \<open>prep_rls' norm_Rational_rls_noadd_fractions\<close>
   326 
   327 (** problems **)
   328 setup \<open>KEStore_Elems.add_pbts
   329   [(Problem.prep_input @{theory} "pbl_fun_integ" [] Problem.id_empty
   330       (["integrate", "function"],
   331         [("#Given" ,["functionTerm f_f", "integrateBy v_v"]),
   332           ("#Find"  ,["antiDerivative F_F"])],
   333         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)], 
   334         SOME "Integrate (f_f, v_v)", 
   335         [["diff", "integration"]])),
   336     (*here "named" is used differently from Differentiation"*)
   337     (Problem.prep_input @{theory} "pbl_fun_integ_nam" [] Problem.id_empty
   338       (["named", "integrate", "function"],
   339         [("#Given" ,["functionTerm f_f", "integrateBy v_v"]),
   340           ("#Find"  ,["antiDerivativeName F_F"])],
   341         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)], 
   342         SOME "Integrate (f_f, v_v)", 
   343         [["diff", "integration", "named"]]))]\<close>
   344 
   345 (** methods **)
   346 
   347 partial_function (tailrec) integrate :: "real \<Rightarrow> real \<Rightarrow> real"
   348   where
   349 "integrate f_f v_v = (
   350   let
   351     t_t = Take (Integral f_f D v_v)
   352   in
   353     (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'') t_t)"
   354 setup \<open>KEStore_Elems.add_mets
   355     [MethodC.prep_input @{theory} "met_diffint" [] MethodC.id_empty
   356 	    (["diff", "integration"],
   357 	      [("#Given" ,["functionTerm f_f", "integrateBy v_v"]), ("#Find"  ,["antiDerivative F_F"])],
   358 	      {rew_ord'="tless_true", rls'=Atools_erls, calc = [], srls = Rule_Set.empty, prls=Rule_Set.empty,
   359 	        crls = Atools_erls, errpats = [], nrls = Rule_Set.empty},
   360 	      @{thm integrate.simps})]
   361 \<close>
   362 
   363 partial_function (tailrec) intergrate_named :: "real \<Rightarrow> real \<Rightarrow> (real \<Rightarrow> real) \<Rightarrow> bool"
   364   where
   365 "intergrate_named f_f v_v F_F =(
   366   let
   367     t_t = Take (F_F v_v = Integral f_f D v_v)
   368   in (
   369     (Try (Rewrite_Set_Inst [(''bdv'', v_v)] ''simplify_Integral'')) #>
   370     (Rewrite_Set_Inst [(''bdv'', v_v)] ''integration'')
   371     ) t_t)"
   372 setup \<open>KEStore_Elems.add_mets
   373     [MethodC.prep_input @{theory} "met_diffint_named" [] MethodC.id_empty
   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_Set.empty, prls=Rule_Set.empty,
   378           crls = Atools_erls, errpats = [], nrls = Rule_Set.empty},
   379         @{thm intergrate_named.simps})]
   380 \<close> ML \<open>
   381 \<close> ML \<open>
   382 \<close>
   383 
   384 end