src/Tools/isac/Knowledge/Poly.thy
author Walther Neuper <neuper@ist.tugraz.at>
Fri, 03 Sep 2010 17:19:20 +0200
branchisac-update-Isa09-2
changeset 37979 4f11d7840684
parent 37978 352b7044d4fb
child 37980 c0a9d6bdc1d6
permissions -rw-r--r--
updated Knowledge/Rational

renamed
find . -type f -exec sed -i s/discard_minus_/discard_minus1/g {} \;
find . -type f -exec sed -i s/discard_parentheses_/discard_parentheses1/g {} \;
TODO: reconsider naming of similar rls
     1 (* WN.020812: theorems in the Reals,
     2    necessary for special rule sets, in addition to Isabelle2002.
     3    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     4    !!! THIS IS THE _least_ NUMBER OF ADDITIONAL THEOREMS !!!
     5    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     6    xxxI contain ^^^ instead of ^ in the respective theorem xxx in 2002
     7    changed by: Richard Lang 020912
     8 *)
     9 
    10 theory Poly imports Simplify begin
    11 
    12 consts
    13 
    14   is'_expanded'_in :: "[real, real] => bool" ("_ is'_expanded'_in _") 
    15   is'_poly'_in     :: "[real, real] => bool" ("_ is'_poly'_in _")   (*RL DA *)
    16   has'_degree'_in  :: "[real, real] => real" ("_ has'_degree'_in _")(*RL DA *)
    17   is'_polyrat'_in  :: "[real, real] => bool" ("_ is'_polyrat'_in _")(*RL030626*)
    18 
    19   is'_multUnordered:: "real => bool" ("_ is'_multUnordered") 
    20   is'_addUnordered :: "real => bool" ("_ is'_addUnordered") (*WN030618*)
    21   is'_polyexp      :: "real => bool" ("_ is'_polyexp") 
    22 
    23   Expand'_binoms
    24              :: "['y, 
    25 		    'y] => 'y"
    26                ("((Script Expand'_binoms (_ =))// 
    27                     (_))" 9)
    28 
    29 (*-------------------- rules------------------------------------------------*)
    30 axioms (*.not contained in Isabelle2002,
    31          stated as axioms, TODO: prove as theorems;
    32          theorem-IDs 'xxxI' with ^^^ instead of ^ in 'xxx' in Isabelle2002.*)
    33 
    34   realpow_pow:             "(a ^^^ b) ^^^ c = a ^^^ (b * c)"
    35   realpow_addI:            "r ^^^ (n + m) = r ^^^ n * r ^^^ m"
    36   realpow_addI_assoc_l:    "r ^^^ n * (r ^^^ m * s) = r ^^^ (n + m) * s"
    37   realpow_addI_assoc_r:    "s * r ^^^ n * r ^^^ m = s * r ^^^ (n + m)"
    38 		  
    39   realpow_oneI:            "r ^^^ 1 = r"
    40   realpow_zeroI:            "r ^^^ 0 = 1"
    41   realpow_eq_oneI:         "1 ^^^ n = 1"
    42   realpow_multI:           "(r * s) ^^^ n = r ^^^ n * s ^^^ n" 
    43   realpow_multI_poly:      "[| r is_polyexp; s is_polyexp |] ==>
    44 			      (r * s) ^^^ n = r ^^^ n * s ^^^ n" 
    45   realpow_minus_oneI:      "-1 ^^^ (2 * n) = 1"  
    46 
    47   realpow_twoI:            "r ^^^ 2 = r * r"
    48   realpow_twoI_assoc_l:	  "r * (r * s) = r ^^^ 2 * s"
    49   realpow_twoI_assoc_r:	  "s * r * r = s * r ^^^ 2"
    50   realpow_two_atom:        "r is_atom ==> r * r = r ^^^ 2"
    51   realpow_plus_1:          "r * r ^^^ n = r ^^^ (n + 1)"         
    52   realpow_plus_1_assoc_l:  "r * (r ^^^ m * s) = r ^^^ (1 + m) * s" 
    53   realpow_plus_1_assoc_l2: "r ^^^ m * (r * s) = r ^^^ (1 + m) * s" 
    54   realpow_plus_1_assoc_r:  "s * r * r ^^^ m = s * r ^^^ (1 + m)"
    55   realpow_plus_1_atom:     "r is_atom ==> r * r ^^^ n = r ^^^ (1 + n)"
    56   realpow_def_atom:        "[| Not (r is_atom); 1 < n |]
    57 			   ==> r ^^^ n = r * r ^^^ (n + -1)"
    58   realpow_addI_atom:       "r is_atom ==> r ^^^ n * r ^^^ m = r ^^^ (n + m)"
    59 
    60 
    61   realpow_minus_even:	  "n is_even ==> (- r) ^^^ n = r ^^^ n"
    62   realpow_minus_odd:       "Not (n is_even) ==> (- r) ^^^ n = -1 * r ^^^ n"
    63 
    64 
    65 (* RL 020914 *)
    66   real_pp_binom_times:     "(a + b)*(c + d) = a*c + a*d + b*c + b*d"
    67   real_pm_binom_times:     "(a + b)*(c - d) = a*c - a*d + b*c - b*d"
    68   real_mp_binom_times:     "(a - b)*(c + d) = a*c + a*d - b*c - b*d"
    69   real_mm_binom_times:     "(a - b)*(c - d) = a*c - a*d - b*c + b*d"
    70   real_plus_binom_pow3:    "(a + b)^^^3 = a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3"
    71   real_plus_binom_pow3_poly: "[| a is_polyexp; b is_polyexp |] ==> 
    72 			    (a + b)^^^3 = a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3"
    73   real_minus_binom_pow3:   "(a - b)^^^3 = a^^^3 - 3*a^^^2*b + 3*a*b^^^2 - b^^^3"
    74   real_minus_binom_pow3_p: "(a + -1 * b)^^^3 = a^^^3 + -3*a^^^2*b + 3*a*b^^^2 +
    75                            -1*b^^^3"
    76 (* real_plus_binom_pow:        "[| n is_const;  3 < n |] ==>
    77 			       (a + b)^^^n = (a + b) * (a + b)^^^(n - 1)" *)
    78   real_plus_binom_pow4:   "(a + b)^^^4 = (a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3)
    79                            *(a + b)"
    80   real_plus_binom_pow4_poly: "[| a is_polyexp; b is_polyexp |] ==> 
    81 			   (a + b)^^^4 = (a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3)
    82                            *(a + b)"
    83   real_plus_binom_pow5:    "(a + b)^^^5 = (a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3)
    84                            *(a^^^2 + 2*a*b + b^^^2)"
    85   real_plus_binom_pow5_poly: "[| a is_polyexp; b is_polyexp |] ==> 
    86 			        (a + b)^^^5 = (a^^^3 + 3*a^^^2*b + 3*a*b^^^2 
    87                                 + b^^^3)*(a^^^2 + 2*a*b + b^^^2)"
    88   real_diff_plus:          "a - b = a + -b" (*17.3.03: do_NOT_use*)
    89   real_diff_minus:         "a - b = a + -1 * b"
    90   real_plus_binom_times:   "(a + b)*(a + b) = a^^^2 + 2*a*b + b^^^2"
    91   real_minus_binom_times:  "(a - b)*(a - b) = a^^^2 - 2*a*b + b^^^2"
    92   (*WN071229 changed for Schaerding -----vvv*)
    93   (*real_plus_binom_pow2:  "(a + b)^^^2 = a^^^2 + 2*a*b + b^^^2"*)
    94   real_plus_binom_pow2:    "(a + b)^^^2 = (a + b) * (a + b)"
    95   (*WN071229 changed for Schaerding -----^^^*)
    96   real_plus_binom_pow2_poly: "[| a is_polyexp; b is_polyexp |] ==>
    97 			       (a + b)^^^2 = a^^^2 + 2*a*b + b^^^2"
    98   real_minus_binom_pow2:      "(a - b)^^^2 = a^^^2 - 2*a*b + b^^^2"
    99   real_minus_binom_pow2_p:    "(a - b)^^^2 = a^^^2 + -2*a*b + b^^^2"
   100   real_plus_minus_binom1:     "(a + b)*(a - b) = a^^^2 - b^^^2"
   101   real_plus_minus_binom1_p:   "(a + b)*(a - b) = a^^^2 + -1*b^^^2"
   102   real_plus_minus_binom1_p_p: "(a + b)*(a + -1 * b) = a^^^2 + -1*b^^^2"
   103   real_plus_minus_binom2:     "(a - b)*(a + b) = a^^^2 - b^^^2"
   104   real_plus_minus_binom2_p:   "(a - b)*(a + b) = a^^^2 + -1*b^^^2"
   105   real_plus_minus_binom2_p_p: "(a + -1 * b)*(a + b) = a^^^2 + -1*b^^^2"
   106   real_plus_binom_times1:     "(a +  1*b)*(a + -1*b) = a^^^2 + -1*b^^^2"
   107   real_plus_binom_times2:     "(a + -1*b)*(a +  1*b) = a^^^2 + -1*b^^^2"
   108 
   109   real_num_collect:           "[| l is_const; m is_const |] ==>
   110 			      l * n + m * n = (l + m) * n"
   111 (* FIXME.MG.0401: replace 'real_num_collect_assoc' 
   112 	by 'real_num_collect_assoc_l' ... are equal, introduced by MG ! *)
   113   real_num_collect_assoc:     "[| l is_const; m is_const |] ==> 
   114 			      l * n + (m * n + k) = (l + m) * n + k"
   115   real_num_collect_assoc_l:   "[| l is_const; m is_const |] ==>
   116 			      l * n + (m * n + k) = (l + m)
   117 				* n + k"
   118   real_num_collect_assoc_r:   "[| l is_const; m is_const |] ==>
   119 			      (k + m * n) + l * n = k + (l + m) * n"
   120   real_one_collect:           "m is_const ==> n + m * n = (1 + m) * n"
   121 (* FIXME.MG.0401: replace 'real_one_collect_assoc' 
   122 	by 'real_one_collect_assoc_l' ... are equal, introduced by MG ! *)
   123   real_one_collect_assoc:     "m is_const ==> n + (m * n + k) = (1 + m)* n + k"
   124 
   125   real_one_collect_assoc_l:   "m is_const ==> n + (m * n + k) = (1 + m) * n + k"
   126   real_one_collect_assoc_r:  "m is_const ==> (k + n) +  m * n = k + (1 + m) * n"
   127 
   128 (* FIXME.MG.0401: replace 'real_mult_2_assoc' 
   129 	by 'real_mult_2_assoc_l' ... are equal, introduced by MG ! *)
   130   real_mult_2_assoc:          "z1 + (z1 + k) = 2 * z1 + k"
   131   real_mult_2_assoc_l:        "z1 + (z1 + k) = 2 * z1 + k"
   132   real_mult_2_assoc_r:        "(k + z1) + z1 = k + 2 * z1"
   133 
   134   real_add_mult_distrib_poly: "w is_polyexp ==> (z1 + z2) * w = z1 * w + z2 * w"
   135   real_add_mult_distrib2_poly:"w is_polyexp ==> w * (z1 + z2) = w * z1 + w * z2"
   136 
   137 text {* remark on 'polynomials'
   138         WN020919
   139 *** there are 5 kinds of expanded normalforms ***
   140 
   141 [1] 'complete polynomial' (Komplettes Polynom), univariate
   142    a_0 + a_1.x^1 +...+ a_n.x^n   not (a_n = 0)
   143 	        not (a_n = 0), some a_i may be zero (DON'T disappear),
   144                 variables in monomials lexicographically ordered and complete,
   145                 x written as 1*x^1, ...
   146 [2] 'polynomial' (Polynom), univariate and multivariate
   147    a_0 + a_1.x +...+ a_n.x^n   not (a_n = 0)
   148    a_0 + a_1.x_1.x_2^n_12...x_m^n_1m +...+  a_n.x_1^n.x_2^n_n2...x_m^n_nm
   149 	        not (a_n = 0), some a_i may be zero (ie. monomials disappear),
   150                 exponents and coefficients equal 1 are not (WN060904.TODO in cancel_p_)shown,
   151                 and variables in monomials are lexicographically ordered  
   152    examples: [1]: "1 + (-10) * x ^^^ 1 + 25 * x ^^^ 2"
   153 	     [1]: "11 + 0 * x ^^^ 1 + 1 * x ^^^ 2"
   154 	     [2]: "x + (-50) * x ^^^ 3"
   155 	     [2]: "(-1) * x * y ^^^ 2 + 7 * x ^^^ 3"
   156 
   157 [3] 'expanded_term' (Ausmultiplizierter Term):
   158    pull out unary minus to binary minus, 
   159    as frequently exercised in schools; other conditions for [2] hold however
   160    examples: "a ^^^ 2 - 2 * a * b + b ^^^ 2"
   161 	     "4 * x ^^^ 2 - 9 * y ^^^ 2"
   162 [4] 'polynomial_in' (Polynom in): 
   163    polynomial in 1 variable with arbitrary coefficients
   164    examples: "2 * x + (-50) * x ^^^ 3"                     (poly in x)
   165 	     "(u + v) + (2 * u ^^^ 2) * a + (-u) * a ^^^ 2 (poly in a)
   166 [5] 'expanded_in' (Ausmultiplizierter Termin in): 
   167    analoguous to [3] with binary minus like [3]
   168    examples: "2 * x - 50 * x ^^^ 3"                     (expanded in x)
   169 	     "(u + v) + (2 * u ^^^ 2) * a - u * a ^^^ 2 (expanded in a)
   170 *}
   171 
   172 ML {*
   173 val thy = @{theory};
   174 
   175 (* is_polyrat_in becomes true, if no bdv is in the denominator of a fraction*)
   176 fun is_polyrat_in t v = 
   177     let fun coeff_in c v = member op = (vars c) v;
   178    	fun finddivide (_ $ _ $ _ $ _) v = raise error("is_polyrat_in:")
   179 	    (* at the moment there is no term like this, but ....*)
   180 	  | finddivide (t as (Const ("HOL.divide",_) $ _ $ b)) v = 
   181             not(coeff_in b v)
   182 	  | finddivide (_ $ t1 $ t2) v = 
   183             (finddivide t1 v) orelse (finddivide t2 v)
   184 	  | finddivide (_ $ t1) v = (finddivide t1 v)
   185 	  | finddivide _ _ = false;
   186      in finddivide t v end;
   187     
   188 fun eval_is_polyrat_in _ _(p as (Const ("Poly.is'_polyrat'_in",_) $ t $ v)) _  =
   189     if is_polyrat_in t v 
   190     then SOME ((term2str p) ^ " = True",
   191 	        Trueprop $ (mk_equality (p, HOLogic.true_const)))
   192     else SOME ((term2str p) ^ " = True",
   193 	        Trueprop $ (mk_equality (p, HOLogic.false_const)))
   194   | eval_is_polyrat_in _ _ _ _ = ((*writeln"### no matches";*) NONE);
   195 
   196 local
   197     (*.a 'c is coefficient of v' if v does NOT occur in c.*)
   198     fun coeff_in c v = not (member op = (vars c) v);
   199     (* FIXME.WN100826 shift this into test--------------
   200      val v = (term_of o the o (parse thy)) "x";
   201      val t = (term_of o the o (parse thy)) "1";
   202      coeff_in t v;
   203      (*val it = true : bool*)
   204      val t = (term_of o the o (parse thy)) "a*b+c";
   205      coeff_in t v;
   206      (*val it = true : bool*)
   207      val t = (term_of o the o (parse thy)) "a*x+c";
   208      coeff_in t v;
   209      (*val it = false : bool*)
   210     ----------------------------------------------------*)
   211     (*. a 'monomial t in variable v' is a term t with
   212       either (1) v NOT existent in t, or (2) v contained in t,
   213       if (1) then degree 0
   214       if (2) then v is a factor on the very right, ev. with exponent.*)
   215     fun factor_right_deg (*case 2*)
   216     	    (t as Const ("op *",_) $ t1 $ 
   217     	       (Const ("Atools.pow",_) $ vv $ Free (d,_))) v =
   218     	if ((vv = v) andalso (coeff_in t1 v)) then SOME (int_of_str' d) else NONE
   219       | factor_right_deg (t as Const ("Atools.pow",_) $ vv $ Free (d,_)) v =
   220     	if (vv = v) then SOME (int_of_str' d) else NONE
   221       | factor_right_deg (t as Const ("op *",_) $ t1 $ vv) v = 
   222     	if ((vv = v) andalso (coeff_in t1 v))then SOME 1 else NONE
   223       | factor_right_deg vv v =
   224     	if (vv = v) then SOME 1 else NONE;    
   225     fun mono_deg_in m v =
   226     	if coeff_in m v then (*case 1*) SOME 0
   227     	else factor_right_deg m v;
   228     (* FIXME.WN100826 shift this into test-----------------------------
   229      val v = (term_of o the o (parse thy)) "x";
   230      val t = (term_of o the o (parse thy)) "(a*b+c)*x^^^7";
   231      mono_deg_in t v;
   232      (*val it = SOME 7*)
   233      val t = (term_of o the o (parse thy)) "x^^^7";
   234      mono_deg_in t v;
   235      (*val it = SOME 7*)
   236      val t = (term_of o the o (parse thy)) "(a*b+c)*x";
   237      mono_deg_in t v;
   238      (*val it = SOME 1*)
   239      val t = (term_of o the o (parse thy)) "(a*b+x)*x";
   240      mono_deg_in t v;
   241      (*val it = NONE*)
   242      val t = (term_of o the o (parse thy)) "x";
   243      mono_deg_in t v;
   244      (*val it = SOME 1*)
   245      val t = (term_of o the o (parse thy)) "(a*b+c)";
   246      mono_deg_in t v;
   247      (*val it = SOME 0*)
   248      val t = (term_of o the o (parse thy)) "ab - (a*b)*x";
   249      mono_deg_in t v;
   250      (*val it = NONE*)
   251     ------------------------------------------------------------------*)
   252     fun expand_deg_in t v =
   253     	let fun edi ~1 ~1 (Const ("op +",_) $ t1 $ t2) =
   254     		(case mono_deg_in t2 v of (* $ is left associative*)
   255     		     SOME d' => edi d' d' t1
   256 		   | NONE => NONE)
   257     	      | edi ~1 ~1 (Const ("op -",_) $ t1 $ t2) =
   258     		(case mono_deg_in t2 v of
   259     		     SOME d' => edi d' d' t1
   260 		   | NONE => NONE)
   261     	      | edi d dmax (Const ("op -",_) $ t1 $ t2) =
   262     		(case mono_deg_in t2 v of
   263 		(*RL  orelse ((d=0) andalso (d'=0)) need to handle 3+4-...4 +x*)
   264     		     SOME d' => if ((d > d') orelse ((d=0) andalso (d'=0))) 
   265                      then edi d' dmax t1 else NONE
   266 		   | NONE => NONE)
   267     	      | edi d dmax (Const ("op +",_) $ t1 $ t2) =
   268     		(case mono_deg_in t2 v of
   269 		(*RL  orelse ((d=0) andalso (d'=0)) need to handle 3+4-...4 +x*)
   270     		     SOME d' => if ((d > d') orelse ((d=0) andalso (d'=0))) 
   271                      then edi d' dmax t1 else NONE
   272 		   | NONE => NONE)
   273     	      | edi ~1 ~1 t = (case mono_deg_in t v of
   274     		     d as SOME _ => d
   275 		   | NONE => NONE)
   276     	      | edi d dmax t = (*basecase last*)
   277     		(case mono_deg_in t v of
   278     		     SOME d' => if ((d > d') orelse ((d=0) andalso (d'=0)))  
   279                      then SOME dmax else NONE
   280 		   | NONE => NONE)
   281     	in edi ~1 ~1 t end;
   282     (* FIXME.WN100826 shift this into test-----------------------------
   283      val v = (term_of o the o (parse thy)) "x";
   284      val t = (term_of o the o (parse thy)) "a+b";
   285      expand_deg_in t v;
   286      (*val it = SOME 0*)   
   287      val t = (term_of o the o (parse thy)) "(a+b)*x";
   288      expand_deg_in t v;
   289      (*SOME 1*)   
   290      val t = (term_of o the o (parse thy)) "a*b - (a+b)*x";
   291      expand_deg_in t v;
   292      (*SOME 1*)   
   293      val t = (term_of o the o (parse thy)) "a*b + (a-b)*x";
   294      expand_deg_in t v;
   295      (*SOME 1*)   
   296      val t = (term_of o the o (parse thy)) "a*b + (a+b)*x + x^^^2";
   297      expand_deg_in t v;
   298     -------------------------------------------------------------------*)   
   299     fun poly_deg_in t v =
   300     	let fun edi ~1 ~1 (Const ("op +",_) $ t1 $ t2) =
   301     		(case mono_deg_in t2 v of (* $ is left associative*)
   302     		     SOME d' => edi d' d' t1
   303 		   | NONE => NONE)
   304     	      | edi d dmax (Const ("op +",_) $ t1 $ t2) =
   305     		(case mono_deg_in t2 v of
   306  		(*RL  orelse ((d=0) andalso (d'=0)) need to handle 3+4-...4 +x*)
   307    		     SOME d' => if ((d > d') orelse ((d=0) andalso (d'=0))) 
   308                                 then edi d' dmax t1 else NONE
   309 		   | NONE => NONE)
   310     	      | edi ~1 ~1 t = (case mono_deg_in t v of
   311     		     d as SOME _ => d
   312 		   | NONE => NONE)
   313     	      | edi d dmax t = (*basecase last*)
   314     		(case mono_deg_in t v of
   315     		     SOME d' => if ((d > d') orelse ((d=0) andalso (d'=0))) 
   316                      then SOME dmax else NONE
   317 		   | NONE => NONE)
   318     	in edi ~1 ~1 t end;
   319 in
   320 
   321 fun is_expanded_in t v =
   322     case expand_deg_in t v of SOME _ => true | NONE => false;
   323 fun is_poly_in t v =
   324     case poly_deg_in t v of SOME _ => true | NONE => false;
   325 fun has_degree_in t v =
   326     case expand_deg_in t v of SOME d => d | NONE => ~1;
   327 end;
   328 (* FIXME.WN100826 shift this into test-----------------------------
   329  val v = (term_of o the o (parse thy)) "x";
   330  val t = (term_of o the o (parse thy)) "a*b - (a+b)*x + x^^^2";
   331  has_degree_in t v;
   332  (*val it = 2*)
   333  val t = (term_of o the o (parse thy)) "-8 - 2*x + x^^^2";
   334  has_degree_in t v;
   335  (*val it = 2*)
   336  val t = (term_of o the o (parse thy)) "6 + 13*x + 6*x^^^2";
   337  has_degree_in t v;
   338  (*val it = 2*)
   339 -------------------------------------------------------------------*)
   340 
   341 (*("is_expanded_in", ("Poly.is'_expanded'_in", eval_is_expanded_in ""))*)
   342 fun eval_is_expanded_in _ _ 
   343        (p as (Const ("Poly.is'_expanded'_in",_) $ t $ v)) _ =
   344     if is_expanded_in t v
   345     then SOME ((term2str p) ^ " = True",
   346 	        Trueprop $ (mk_equality (p, HOLogic.true_const)))
   347     else SOME ((term2str p) ^ " = True",
   348 	        Trueprop $ (mk_equality (p, HOLogic.false_const)))
   349   | eval_is_expanded_in _ _ _ _ = NONE;
   350 (*
   351  val t = (term_of o the o (parse thy)) "(-8 - 2*x + x^^^2) is_expanded_in x";
   352  val SOME (id, t') = eval_is_expanded_in 0 0 t 0;
   353  (*val id = "Poly.is'_expanded'_in (-8 - 2 * x + x ^^^ 2) x = True"*)
   354  term2str t';
   355  (*val it = "Poly.is'_expanded'_in (-8 - 2 * x + x ^^^ 2) x = True"*)
   356 *)
   357 
   358 (*("is_poly_in", ("Poly.is'_poly'_in", eval_is_poly_in ""))*)
   359 fun eval_is_poly_in _ _ 
   360        (p as (Const ("Poly.is'_poly'_in",_) $ t $ v)) _ =
   361     if is_poly_in t v
   362     then SOME ((term2str p) ^ " = True",
   363 	        Trueprop $ (mk_equality (p, HOLogic.true_const)))
   364     else SOME ((term2str p) ^ " = True",
   365 	        Trueprop $ (mk_equality (p, HOLogic.false_const)))
   366   | eval_is_poly_in _ _ _ _ = NONE;
   367 (*
   368  val t = (term_of o the o (parse thy)) "(8 + 2*x + x^^^2) is_poly_in x";
   369  val SOME (id, t') = eval_is_poly_in 0 0 t 0;
   370  (*val id = "Poly.is'_poly'_in (8 + 2 * x + x ^^^ 2) x = True"*)
   371  term2str t';
   372  (*val it = "Poly.is'_poly'_in (8 + 2 * x + x ^^^ 2) x = True"*)
   373 *)
   374 
   375 (*("has_degree_in", ("Poly.has'_degree'_in", eval_has_degree_in ""))*)
   376 fun eval_has_degree_in _ _ 
   377 	     (p as (Const ("Poly.has'_degree'_in",_) $ t $ v)) _ =
   378     let val d = has_degree_in t v
   379 	val d' = term_of_num HOLogic.realT d
   380     in SOME ((term2str p) ^ " = " ^ (string_of_int d),
   381 	      Trueprop $ (mk_equality (p, d')))
   382     end
   383   | eval_has_degree_in _ _ _ _ = NONE;
   384 (*
   385 > val t = (term_of o the o (parse thy)) "(-8 - 2*x + x^^^2) has_degree_in x";
   386 > val SOME (id, t') = eval_has_degree_in 0 0 t 0;
   387 val id = "Poly.has'_degree'_in (-8 - 2 * x + x ^^^ 2) x = 2" : string
   388 > term2str t';
   389 val it = "Poly.has'_degree'_in (-8 - 2 * x + x ^^^ 2) x = 2" : string
   390 *)
   391 
   392 (*..*)
   393 val calculate_Poly =
   394     append_rls "calculate_PolyFIXXXME.not.impl." e_rls
   395 	       [];
   396 
   397 (*.for evaluation of conditions in rewrite rules.*)
   398 val Poly_erls = 
   399     append_rls "Poly_erls" Atools_erls
   400                [ Calc ("op =",eval_equal "#equal_"),
   401 		 Thm  ("real_unari_minus",num_str @{thm real_unari_minus}),
   402                  Calc ("op +",eval_binop "#add_"),
   403 		 Calc ("op -",eval_binop "#sub_"),
   404 		 Calc ("op *",eval_binop "#mult_"),
   405 		 Calc ("Atools.pow" ,eval_binop "#power_")
   406 		 ];
   407 
   408 val poly_crls = 
   409     append_rls "poly_crls" Atools_crls
   410                [ Calc ("op =",eval_equal "#equal_"),
   411 		 Thm  ("real_unari_minus",num_str @{thm real_unari_minus}),
   412                  Calc ("op +",eval_binop "#add_"),
   413 		 Calc ("op -",eval_binop "#sub_"),
   414 		 Calc ("op *",eval_binop "#mult_"),
   415 		 Calc ("Atools.pow" ,eval_binop "#power_")
   416 		 ];
   417 
   418 local (*. for make_polynomial .*)
   419 
   420 open Term;  (* for type order = EQUAL | LESS | GREATER *)
   421 
   422 fun pr_ord EQUAL = "EQUAL"
   423   | pr_ord LESS  = "LESS"
   424   | pr_ord GREATER = "GREATER";
   425 
   426 fun dest_hd' (Const (a, T)) =                          (* ~ term.ML *)
   427   (case a of
   428      "Atools.pow" => ((("|||||||||||||", 0), T), 0)    (*WN greatest string*)
   429    | _ => (((a, 0), T), 0))
   430   | dest_hd' (Free (a, T)) = (((a, 0), T), 1)
   431   | dest_hd' (Var v) = (v, 2)
   432   | dest_hd' (Bound i) = ((("", i), dummyT), 3)
   433   | dest_hd' (Abs (_, T, _)) = ((("", 0), T), 4);
   434 
   435 fun get_order_pow (t $ (Free(order,_))) = (* RL FIXXXME:geht zufaellig?WN*)
   436     	(case int_of_str (order) of
   437 	             SOME d => d
   438 		   | NONE   => 0)
   439   | get_order_pow _ = 0;
   440 
   441 fun size_of_term' (Const(str,_) $ t) =
   442   if "Atools.pow"= str then 1000 + size_of_term' t else 1+size_of_term' t(*WN*)
   443   | size_of_term' (Abs (_,_,body)) = 1 + size_of_term' body
   444   | size_of_term' (f$t) = size_of_term' f  +  size_of_term' t
   445   | size_of_term' _ = 1;
   446 
   447 fun term_ord' pr thy (Abs (_, T, t), Abs(_, U, u)) =       (* ~ term.ML *)
   448       (case term_ord' pr thy (t, u) of EQUAL => Term_Ord.typ_ord (T, U) | ord => ord)
   449   | term_ord' pr thy (t, u) =
   450       (if pr then 
   451 	 let
   452 	   val (f, ts) = strip_comb t and (g, us) = strip_comb u;
   453 	   val _=writeln("t= f@ts= \""^
   454 	      ((Syntax.string_of_term (thy2ctxt thy)) f)^"\" @ \"["^
   455 	      (commas(map(Syntax.string_of_term (thy2ctxt thy))ts))^"]\"");
   456 	   val _=writeln("u= g@us= \""^
   457 	      ((Syntax.string_of_term (thy2ctxt thy)) g)^"\" @ \"["^
   458 	      (commas(map(Syntax.string_of_term (thy2ctxt thy))us))^"]\"");
   459 	   val _=writeln("size_of_term(t,u)= ("^
   460 	      (string_of_int(size_of_term' t))^", "^
   461 	      (string_of_int(size_of_term' u))^")");
   462 	   val _=writeln("hd_ord(f,g)      = "^((pr_ord o hd_ord)(f,g)));
   463 	   val _=writeln("terms_ord(ts,us) = "^
   464 			   ((pr_ord o terms_ord str false)(ts,us)));
   465 	   val _=writeln("-------");
   466 	 in () end
   467        else ();
   468 	 case int_ord (size_of_term' t, size_of_term' u) of
   469 	   EQUAL =>
   470 	     let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
   471 	       (case hd_ord (f, g) of EQUAL => (terms_ord str pr) (ts, us) 
   472 	     | ord => ord)
   473 	     end
   474 	 | ord => ord)
   475 and hd_ord (f, g) =                                        (* ~ term.ML *)
   476   prod_ord (prod_ord Term_Ord.indexname_ord Term_Ord.typ_ord) int_ord (dest_hd' f, dest_hd' g)
   477 and terms_ord str pr (ts, us) = 
   478     list_ord (term_ord' pr (assoc_thy "Isac.thy"))(ts, us);
   479 in
   480 
   481 fun ord_make_polynomial (pr:bool) thy (_:subst) tu = 
   482     (term_ord' pr thy(***) tu = LESS );
   483 
   484 end;(*local*)
   485 
   486 
   487 rew_ord' := overwritel (!rew_ord',
   488 [("termlessI", termlessI),
   489  ("ord_make_polynomial", ord_make_polynomial false thy)
   490  ]);
   491 
   492 
   493 val expand =
   494   Rls{id = "expand", preconds = [], rew_ord = ("dummy_ord", dummy_ord),
   495       erls = e_rls,srls = Erls, calc = [],
   496       rules = [Thm ("left_distrib" ,num_str @{thm left_distrib}),
   497 	       (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
   498 	       Thm ("right_distrib",num_str @{thm right_distrib})
   499 	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
   500 	       ], scr = EmptyScr}:rls;
   501 
   502 (*----------------- Begin: rulesets for make_polynomial_ -----------------
   503   'rlsIDs' redefined by MG as 'rlsIDs_' 
   504                                     ^^^*)
   505 
   506 val discard_minus1 = 
   507   Rls{id = "discard_minus1", preconds = [], 
   508       rew_ord = ("dummy_ord", dummy_ord),
   509       erls = e_rls,srls = Erls,
   510       calc = [],
   511       (*asm_thm = [],*)
   512       rules = [Thm ("real_diff_minus",num_str @{thm real_diff_minus}),
   513 	       (*"a - b = a + -1 * b"*)
   514 	       Thm ("sym_real_mult_minus1",
   515                      num_str (@{thm real_mult_minus1} RS @{thm sym}))
   516 	       (*- ?z = "-1 * ?z"*)
   517 	       ], scr = EmptyScr}:rls;
   518 val expand_poly_ = 
   519   Rls{id = "expand_poly_", preconds = [], 
   520       rew_ord = ("dummy_ord", dummy_ord),
   521       erls = e_rls,srls = Erls,
   522       calc = [],
   523       (*asm_thm = [],*)
   524       rules = [Thm ("real_plus_binom_pow4",num_str @{thm real_plus_binom_pow4}),
   525 	       (*"(a + b)^^^4 = ... "*)
   526 	       Thm ("real_plus_binom_pow5",num_str @{thm real_plus_binom_pow5}),
   527 	       (*"(a + b)^^^5 = ... "*)
   528 	       Thm ("real_plus_binom_pow3",num_str @{thm real_plus_binom_pow3}),
   529 	       (*"(a + b)^^^3 = a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3" *)
   530 	       (*WN071229 changed/removed for Schaerding -----vvv*)
   531 	       (*Thm ("real_plus_binom_pow2",num_str @{thm real_plus_binom_pow2}),*)
   532 	       (*"(a + b)^^^2 = a^^^2 + 2*a*b + b^^^2"*)
   533 	       Thm ("real_plus_binom_pow2",num_str @{thm real_plus_binom_pow2}),
   534 	       (*"(a + b)^^^2 = (a + b) * (a + b)"*)
   535 	       (*Thm ("real_plus_minus_binom1_p_p",
   536 		    num_str @{thm real_plus_minus_binom1_p_p}),*)
   537 	       (*"(a + b)*(a + -1 * b) = a^^^2 + -1*b^^^2"*)
   538 	       (*Thm ("real_plus_minus_binom2_p_p",
   539 		    num_str @{thm real_plus_minus_binom2_p_p}),*)
   540 	       (*"(a + -1 * b)*(a + b) = a^^^2 + -1*b^^^2"*)
   541 	       (*WN071229 changed/removed for Schaerding -----^^^*)
   542 	      
   543 	       Thm ("left_distrib" ,num_str @{thm left_distrib}),
   544 	       (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
   545 	       Thm ("right_distrib",num_str @{thm right_distrib}),
   546 	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
   547 	       
   548 	       Thm ("realpow_multI", num_str @{thm realpow_multI}),
   549 	       (*"(r * s) ^^^ n = r ^^^ n * s ^^^ n"*)
   550 	       Thm ("realpow_pow",num_str @{thm realpow_pow})
   551 	       (*"(a ^^^ b) ^^^ c = a ^^^ (b * c)"*)
   552 	       ], scr = EmptyScr}:rls;
   553 
   554 (*.the expression contains + - * ^ only ?
   555    this is weaker than 'is_polynomial' !.*)
   556 fun is_polyexp (Free _) = true
   557   | is_polyexp (Const ("op +",_) $ Free _ $ Free _) = true
   558   | is_polyexp (Const ("op -",_) $ Free _ $ Free _) = true
   559   | is_polyexp (Const ("op *",_) $ Free _ $ Free _) = true
   560   | is_polyexp (Const ("Atools.pow",_) $ Free _ $ Free _) = true
   561   | is_polyexp (Const ("op +",_) $ t1 $ t2) = 
   562                ((is_polyexp t1) andalso (is_polyexp t2))
   563   | is_polyexp (Const ("op -",_) $ t1 $ t2) = 
   564                ((is_polyexp t1) andalso (is_polyexp t2))
   565   | is_polyexp (Const ("op *",_) $ t1 $ t2) = 
   566                ((is_polyexp t1) andalso (is_polyexp t2))
   567   | is_polyexp (Const ("Atools.pow",_) $ t1 $ t2) = 
   568                ((is_polyexp t1) andalso (is_polyexp t2))
   569   | is_polyexp _ = false;
   570 
   571 (*("is_polyexp", ("Poly.is'_polyexp", eval_is_polyexp ""))*)
   572 fun eval_is_polyexp (thmid:string) _ 
   573 		       (t as (Const("Poly.is'_polyexp", _) $ arg)) thy = 
   574     if is_polyexp arg
   575     then SOME (mk_thmid thmid "" 
   576 			((Syntax.string_of_term (thy2ctxt thy)) arg) "", 
   577 	       Trueprop $ (mk_equality (t, HOLogic.true_const)))
   578     else SOME (mk_thmid thmid "" 
   579 			((Syntax.string_of_term (thy2ctxt thy)) arg) "", 
   580 	       Trueprop $ (mk_equality (t, HOLogic.false_const)))
   581   | eval_is_polyexp _ _ _ _ = NONE; 
   582 
   583 val expand_poly_rat_ = 
   584   Rls{id = "expand_poly_rat_", preconds = [], 
   585       rew_ord = ("dummy_ord", dummy_ord),
   586       erls =  append_rls "e_rls-is_polyexp" e_rls
   587 	        [Calc ("Poly.is'_polyexp", eval_is_polyexp "")
   588 		 ],
   589       srls = Erls,
   590       calc = [],
   591       (*asm_thm = [],*)
   592       rules = 
   593         [Thm ("real_plus_binom_pow4_poly", num_str @{thm real_plus_binom_pow4_poly}),
   594 	     (*"[| a is_polyexp; b is_polyexp |] ==> (a + b)^^^4 = ... "*)
   595 	 Thm ("real_plus_binom_pow5_poly", num_str @{thm real_plus_binom_pow5_poly}),
   596 	     (*"[| a is_polyexp; b is_polyexp |] ==> (a + b)^^^5 = ... "*)
   597 	 Thm ("real_plus_binom_pow2_poly",num_str @{thm real_plus_binom_pow2_poly}),
   598 	     (*"[| a is_polyexp; b is_polyexp |] ==>
   599 		            (a + b)^^^2 = a^^^2 + 2*a*b + b^^^2"*)
   600 	 Thm ("real_plus_binom_pow3_poly",num_str @{thm real_plus_binom_pow3_poly}),
   601 	     (*"[| a is_polyexp; b is_polyexp |] ==> 
   602 			(a + b)^^^3 = a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3" *)
   603 	 Thm ("real_plus_minus_binom1_p_p",num_str @{thm real_plus_minus_binom1_p_p}),
   604 	     (*"(a + b)*(a + -1 * b) = a^^^2 + -1*b^^^2"*)
   605 	 Thm ("real_plus_minus_binom2_p_p",num_str @{thm real_plus_minus_binom2_p_p}),
   606 	     (*"(a + -1 * b)*(a + b) = a^^^2 + -1*b^^^2"*)
   607 	      
   608 	 Thm ("real_add_mult_distrib_poly",
   609                num_str @{thm real_add_mult_distrib_poly}),
   610 	       (*"w is_polyexp ==> (z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
   611 	 Thm("real_add_mult_distrib2_poly",
   612               num_str @{thm real_add_mult_distrib2_poly}),
   613 	     (*"w is_polyexp ==> w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
   614 	       
   615 	 Thm ("realpow_multI_poly", num_str @{thm realpow_multI_poly}),
   616 	     (*"[| r is_polyexp; s is_polyexp |] ==> 
   617 		            (r * s) ^^^ n = r ^^^ n * s ^^^ n"*)
   618 	  Thm ("realpow_pow",num_str @{thm realpow_pow})
   619 	      (*"(a ^^^ b) ^^^ c = a ^^^ (b * c)"*)
   620 	 ], scr = EmptyScr}:rls;
   621 
   622 val simplify_power_ = 
   623   Rls{id = "simplify_power_", preconds = [], 
   624       rew_ord = ("dummy_ord", dummy_ord),
   625       erls = e_rls, srls = Erls,
   626       calc = [],
   627       (*asm_thm = [],*)
   628       rules = [(*MG: Reihenfolge der folgenden 2 Thm muss so bleiben, wegen
   629 		a*(a*a) --> a*a^^^2 und nicht a*(a*a) --> a^^^2*a *)
   630 	       Thm ("sym_realpow_twoI",
   631                      num_str (@{thm realpow_twoI} RS @{thm sym})),	
   632 	       (*"r * r = r ^^^ 2"*)
   633 	       Thm ("realpow_twoI_assoc_l",num_str @{thm realpow_twoI_assoc_l}),
   634 	       (*"r * (r * s) = r ^^^ 2 * s"*)
   635 
   636 	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),		
   637 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   638 	       Thm ("realpow_plus_1_assoc_l",
   639                      num_str @{thm realpow_plus_1_assoc_l}),
   640 	       (*"r * (r ^^^ m * s) = r ^^^ (1 + m) * s"*)
   641 	       (*MG 9.7.03: neues Thm wegen a*(a*(a*b)) --> a^^^2*(a*b) *)
   642 	       Thm ("realpow_plus_1_assoc_l2",
   643                      num_str @{thm realpow_plus_1_assoc_l2}),
   644 	       (*"r ^^^ m * (r * s) = r ^^^ (1 + m) * s"*)
   645 
   646 	       Thm ("sym_realpow_addI",
   647                num_str (@{thm realpow_addI} RS @{thm sym})),
   648 	       (*"r ^^^ n * r ^^^ m = r ^^^ (n + m)"*)
   649 	       Thm ("realpow_addI_assoc_l",num_str @{thm realpow_addI_assoc_l}),
   650 	       (*"r ^^^ n * (r ^^^ m * s) = r ^^^ (n + m) * s"*)
   651 	       
   652 	       (* ist in expand_poly - wird hier aber auch gebraucht, wegen: 
   653 		  "r * r = r ^^^ 2" wenn r=a^^^b*)
   654 	       Thm ("realpow_pow",num_str @{thm realpow_pow})
   655 	       (*"(a ^^^ b) ^^^ c = a ^^^ (b * c)"*)
   656 	       ], scr = EmptyScr}:rls;
   657 
   658 val calc_add_mult_pow_ = 
   659   Rls{id = "calc_add_mult_pow_", preconds = [], 
   660       rew_ord = ("dummy_ord", dummy_ord),
   661       erls = Atools_erls(*erls3.4.03*),srls = Erls,
   662       calc = [("PLUS"  , ("op +", eval_binop "#add_")), 
   663 	      ("TIMES" , ("op *", eval_binop "#mult_")),
   664 	      ("POWER", ("Atools.pow", eval_binop "#power_"))
   665 	      ],
   666       (*asm_thm = [],*)
   667       rules = [Calc ("op +", eval_binop "#add_"),
   668 	       Calc ("op *", eval_binop "#mult_"),
   669 	       Calc ("Atools.pow", eval_binop "#power_")
   670 	       ], scr = EmptyScr}:rls;
   671 
   672 val reduce_012_mult_ = 
   673   Rls{id = "reduce_012_mult_", preconds = [], 
   674       rew_ord = ("dummy_ord", dummy_ord),
   675       erls = e_rls,srls = Erls,
   676       calc = [],
   677       (*asm_thm = [],*)
   678       rules = [(* MG: folgende Thm müssen hier stehen bleiben: *)
   679                Thm ("mult_1_right",num_str @{thm mult_1_right}),
   680 	       (*"z * 1 = z"*) (*wegen "a * b * b^^^(-1) + a"*) 
   681 	       Thm ("realpow_zeroI",num_str @{thm realpow_zeroI}),
   682 	       (*"r ^^^ 0 = 1"*) (*wegen "a*a^^^(-1)*c + b + c"*)
   683 	       Thm ("realpow_oneI",num_str @{thm realpow_oneI}),
   684 	       (*"r ^^^ 1 = r"*)
   685 	       Thm ("realpow_eq_oneI",num_str @{thm realpow_eq_oneI})
   686 	       (*"1 ^^^ n = 1"*)
   687 	       ], scr = EmptyScr}:rls;
   688 
   689 val collect_numerals_ = 
   690   Rls{id = "collect_numerals_", preconds = [], 
   691       rew_ord = ("dummy_ord", dummy_ord),
   692       erls = Atools_erls, srls = Erls,
   693       calc = [("PLUS"  , ("op +", eval_binop "#add_"))
   694 	      ],
   695       rules = 
   696         [Thm ("real_num_collect",num_str @{thm real_num_collect}), 
   697 	     (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
   698 	 Thm ("real_num_collect_assoc_r",num_str @{thm real_num_collect_assoc_r}),
   699 	     (*"[| l is_const; m is_const |] ==>  \
   700 					\(k + m * n) + l * n = k + (l + m)*n"*)
   701 	 Thm ("real_one_collect",num_str @{thm real_one_collect}),	
   702 	     (*"m is_const ==> n + m * n = (1 + m) * n"*)
   703 	 Thm ("real_one_collect_assoc_r",num_str @{thm real_one_collect_assoc_r}), 
   704 	     (*"m is_const ==> (k + n) + m * n = k + (m + 1) * n"*)
   705 
   706          Calc ("op +", eval_binop "#add_"),
   707 
   708 	 (*MG: Reihenfolge der folgenden 2 Thm muss so bleiben, wegen
   709 		     (a+a)+a --> a + 2*a --> 3*a and not (a+a)+a --> 2*a + a *)
   710          Thm ("real_mult_2_assoc_r",num_str @{thm real_mult_2_assoc_r}),
   711 	     (*"(k + z1) + z1 = k + 2 * z1"*)
   712 	 Thm ("sym_real_mult_2",num_str (@{thm real_mult_2} RS @{thm sym}))
   713 	     (*"z1 + z1 = 2 * z1"*)
   714 	], scr = EmptyScr}:rls;
   715 
   716 val reduce_012_ = 
   717   Rls{id = "reduce_012_", preconds = [], 
   718       rew_ord = ("dummy_ord", dummy_ord),
   719       erls = e_rls,srls = Erls, calc = [],
   720       rules = [Thm ("mult_1_left",num_str @{thm mult_1_left}),                 
   721 	       (*"1 * z = z"*)
   722 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),        
   723 	       (*"0 * z = 0"*)
   724 	       Thm ("mult_zero_right",num_str @{thm mult_zero_right}),
   725 	       (*"z * 0 = 0"*)
   726 	       Thm ("add_0_left",num_str @{thm add_0_left}),
   727 	       (*"0 + z = z"*)
   728 	       Thm ("add_0_right",num_str @{thm add_0_right}),
   729 	       (*"z + 0 = z"*) (*wegen a+b-b --> a+(1-1)*b --> a+0 --> a*)
   730 
   731 	       (*Thm ("realpow_oneI",num_str @{thm realpow_oneI})*)
   732 	       (*"?r ^^^ 1 = ?r"*)
   733 	       Thm ("divide_zero_left",num_str @{thm divide_zero_left})(*WN060914*)
   734 	       (*"0 / ?x = 0"*)
   735 	       ], scr = EmptyScr}:rls;
   736 
   737 (*ein Hilfs-'ruleset' (benutzt das leere 'ruleset')*)
   738 val discard_parentheses1 = 
   739     append_rls "discard_parentheses1" e_rls 
   740 	       [Thm ("sym_real_mult_assoc",
   741                       num_str (@{thm real_mult_assoc} RS @{thm sym}))
   742 		(*"?z1.1 * (?z2.1 * ?z3.1) = ?z1.1 * ?z2.1 * ?z3.1"*)
   743 		(*Thm ("sym_add_assoc",
   744                         num_str (@{thm add_assoc} RS @{thm sym}))*)
   745 		(*"?z1.1 + (?z2.1 + ?z3.1) = ?z1.1 + ?z2.1 + ?z3.1"*)
   746 		 ];
   747 
   748 (*----------------- End: rulesets for make_polynomial_ -----------------*)
   749 
   750 (*MG.0401 ev. for use in rls with ordered rewriting ?
   751 val collect_numerals_left = 
   752   Rls{id = "collect_numerals", preconds = [], 
   753       rew_ord = ("dummy_ord", dummy_ord),
   754       erls = Atools_erls(*erls3.4.03*),srls = Erls,
   755       calc = [("PLUS"  , ("op +", eval_binop "#add_")), 
   756 	      ("TIMES" , ("op *", eval_binop "#mult_")),
   757 	      ("POWER", ("Atools.pow", eval_binop "#power_"))
   758 	      ],
   759       (*asm_thm = [],*)
   760       rules = [Thm ("real_num_collect",num_str @{thm real_num_collect}), 
   761 	       (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
   762 	       Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),
   763 	       (*"[| l is_const; m is_const |] ==>  
   764 				l * n + (m * n + k) =  (l + m) * n + k"*)
   765 	       Thm ("real_one_collect",num_str @{thm real_one_collect}),	
   766 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   767 	       Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}), 
   768 	       (*"m is_const ==> n + (m * n + k) = (1 + m) * n + k"*)
   769 	       
   770 	       Calc ("op +", eval_binop "#add_"),
   771 
   772 	       (*MG am 2.5.03: 2 Theoreme aus reduce_012 hierher verschoben*)
   773 	       Thm ("sym_real_mult_2",
   774                      num_str (@{thm real_mult_2} RS @{thm sym})),	
   775 	       (*"z1 + z1 = 2 * z1"*)
   776 	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc})
   777 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   778 	       ], scr = EmptyScr}:rls;*)
   779 
   780 val expand_poly = 
   781   Rls{id = "expand_poly", preconds = [], 
   782       rew_ord = ("dummy_ord", dummy_ord),
   783       erls = e_rls,srls = Erls,
   784       calc = [],
   785       (*asm_thm = [],*)
   786       rules = [Thm ("left_distrib" ,num_str @{thm left_distrib}),
   787 	       (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
   788 	       Thm ("right_distrib",num_str @{thm right_distrib}),
   789 	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
   790 	       (*Thm ("left_distrib1",num_str @{thm left_distrib}1),
   791 		....... 18.3.03 undefined???*)
   792 
   793 	       Thm ("real_plus_binom_pow2",num_str @{thm real_plus_binom_pow2}),
   794 	       (*"(a + b)^^^2 = a^^^2 + 2*a*b + b^^^2"*)
   795 	       Thm ("real_minus_binom_pow2_p",num_str @{thm real_minus_binom_pow2_p}),
   796 	       (*"(a - b)^^^2 = a^^^2 + -2*a*b + b^^^2"*)
   797 	       Thm ("real_plus_minus_binom1_p",
   798 		    num_str @{thm real_plus_minus_binom1_p}),
   799 	       (*"(a + b)*(a - b) = a^^^2 + -1*b^^^2"*)
   800 	       Thm ("real_plus_minus_binom2_p",
   801 		    num_str @{thm real_plus_minus_binom2_p}),
   802 	       (*"(a - b)*(a + b) = a^^^2 + -1*b^^^2"*)
   803 
   804 	       Thm ("minus_minus",num_str @{thm minus_minus}),
   805 	       (*"- (- ?z) = ?z"*)
   806 	       Thm ("real_diff_minus",num_str @{thm real_diff_minus}),
   807 	       (*"a - b = a + -1 * b"*)
   808 	       Thm ("sym_real_mult_minus1",
   809                      num_str (@{thm real_mult_minus1} RS @{thm sym}))
   810 	       (*- ?z = "-1 * ?z"*)
   811 
   812 	       (*Thm ("real_minus_add_distrib",
   813 		      num_str @{thm real_minus_add_distrib}),*)
   814 	       (*"- (?x + ?y) = - ?x + - ?y"*)
   815 	       (*Thm ("real_diff_plus",num_str @{thm real_diff_plus})*)
   816 	       (*"a - b = a + -b"*)
   817 	       ], scr = EmptyScr}:rls;
   818 
   819 val simplify_power = 
   820   Rls{id = "simplify_power", preconds = [], 
   821       rew_ord = ("dummy_ord", dummy_ord),
   822       erls = e_rls, srls = Erls,
   823       calc = [],
   824       (*asm_thm = [],*)
   825       rules = [Thm ("realpow_multI", num_str @{thm realpow_multI}),
   826 	       (*"(r * s) ^^^ n = r ^^^ n * s ^^^ n"*)
   827 	       
   828 	       Thm ("sym_realpow_twoI",
   829                      num_str( @{thm realpow_twoI} RS @{thm sym})),	
   830 	       (*"r1 * r1 = r1 ^^^ 2"*)
   831 	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),		
   832 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
   833 	       Thm ("realpow_pow",num_str @{thm realpow_pow}),
   834 	       (*"(a ^^^ b) ^^^ c = a ^^^ (b * c)"*)
   835 	       Thm ("sym_realpow_addI",
   836                      num_str (@{thm realpow_addI} RS @{thm sym})),
   837 	       (*"r ^^^ n * r ^^^ m = r ^^^ (n + m)"*)
   838 	       Thm ("realpow_oneI",num_str @{thm realpow_oneI}),
   839 	       (*"r ^^^ 1 = r"*)
   840 	       Thm ("realpow_eq_oneI",num_str @{thm realpow_eq_oneI})
   841 	       (*"1 ^^^ n = 1"*)
   842 	       ], scr = EmptyScr}:rls;
   843 (*MG.0401: termorders for multivariate polys dropped due to principal problems:
   844   (total-degree-)ordering of monoms NOT possible with size_of_term GIVEN*)
   845 val order_add_mult = 
   846   Rls{id = "order_add_mult", preconds = [], 
   847       rew_ord = ("ord_make_polynomial",ord_make_polynomial false thy),
   848       erls = e_rls,srls = Erls,
   849       calc = [],
   850       (*asm_thm = [],*)
   851       rules = [Thm ("real_mult_commute",num_str @{thm real_mult_commute}),
   852 	       (* z * w = w * z *)
   853 	       Thm ("real_mult_left_commute",num_str @{thm real_mult_left_commute}),
   854 	       (*z1.0 * (z2.0 * z3.0) = z2.0 * (z1.0 * z3.0)*)
   855 	       Thm ("real_mult_assoc",num_str @{thm real_mult_assoc}),		
   856 	       (*z1.0 * z2.0 * z3.0 = z1.0 * (z2.0 * z3.0)*)
   857 	       Thm ("add_commute",num_str @{thm add_commute}),	
   858 	       (*z + w = w + z*)
   859 	       Thm ("add_left_commute",num_str @{thm add_left_commute}),
   860 	       (*x + (y + z) = y + (x + z)*)
   861 	       Thm ("add_assoc",num_str @{thm add_assoc})	               
   862 	       (*z1.0 + z2.0 + z3.0 = z1.0 + (z2.0 + z3.0)*)
   863 	       ], scr = EmptyScr}:rls;
   864 (*MG.0401: termorders for multivariate polys dropped due to principal problems:
   865   (total-degree-)ordering of monoms NOT possible with size_of_term GIVEN*)
   866 val order_mult = 
   867   Rls{id = "order_mult", preconds = [], 
   868       rew_ord = ("ord_make_polynomial",ord_make_polynomial false thy),
   869       erls = e_rls,srls = Erls,
   870       calc = [],
   871       (*asm_thm = [],*)
   872       rules = [Thm ("real_mult_commute",num_str @{thm real_mult_commute}),
   873 	       (* z * w = w * z *)
   874 	       Thm ("real_mult_left_commute",num_str @{thm real_mult_left_commute}),
   875 	       (*z1.0 * (z2.0 * z3.0) = z2.0 * (z1.0 * z3.0)*)
   876 	       Thm ("real_mult_assoc",num_str @{thm real_mult_assoc})	
   877 	       (*z1.0 * z2.0 * z3.0 = z1.0 * (z2.0 * z3.0)*)
   878 	       ], scr = EmptyScr}:rls;
   879 val collect_numerals = 
   880   Rls{id = "collect_numerals", preconds = [], 
   881       rew_ord = ("dummy_ord", dummy_ord),
   882       erls = Atools_erls(*erls3.4.03*),srls = Erls,
   883       calc = [("PLUS"  , ("op +", eval_binop "#add_")), 
   884 	      ("TIMES" , ("op *", eval_binop "#mult_")),
   885 	      ("POWER", ("Atools.pow", eval_binop "#power_"))
   886 	      ],
   887       (*asm_thm = [],*)
   888       rules = [Thm ("real_num_collect",num_str @{thm real_num_collect}), 
   889 	       (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
   890 	       Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),
   891 	       (*"[| l is_const; m is_const |] ==>  
   892 				l * n + (m * n + k) =  (l + m) * n + k"*)
   893 	       Thm ("real_one_collect",num_str @{thm real_one_collect}),	
   894 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
   895 	       Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}), 
   896 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
   897 	       Calc ("op +", eval_binop "#add_"), 
   898 	       Calc ("op *", eval_binop "#mult_"),
   899 	       Calc ("Atools.pow", eval_binop "#power_")
   900 	       ], scr = EmptyScr}:rls;
   901 val reduce_012 = 
   902   Rls{id = "reduce_012", preconds = [], 
   903       rew_ord = ("dummy_ord", dummy_ord),
   904       erls = e_rls,srls = Erls,
   905       calc = [],
   906       (*asm_thm = [],*)
   907       rules = [Thm ("mult_1_left",num_str @{thm mult_1_left}),                 
   908 	       (*"1 * z = z"*)
   909 	       (*Thm ("real_mult_minus1",num_str @{thm real_mult_minus1}),14.3.03*)
   910 	       (*"-1 * z = - z"*)
   911 	       Thm ("minus_mult_left", 
   912 		    num_str (@{thm minus_mult_left} RS @{thm sym})),
   913 	       (*- (?x * ?y) = "- ?x * ?y"*)
   914 	       (*Thm ("real_minus_mult_cancel",
   915                        num_str @{thm real_minus_mult_cancel}),
   916 	       (*"- ?x * - ?y = ?x * ?y"*)---*)
   917 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),        
   918 	       (*"0 * z = 0"*)
   919 	       Thm ("add_0_left",num_str @{thm add_0_left}),
   920 	       (*"0 + z = z"*)
   921 	       Thm ("right_minus",num_str @{thm right_minus}),
   922 	       (*"?z + - ?z = 0"*)
   923 	       Thm ("sym_real_mult_2",
   924                      num_str (@{thm real_mult_2} RS @{thm sym})),	
   925 	       (*"z1 + z1 = 2 * z1"*)
   926 	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc})
   927 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
   928 	       ], scr = EmptyScr}:rls;
   929 (*ein Hilfs-'ruleset' (benutzt das leere 'ruleset')*)
   930 val discard_parentheses = 
   931     append_rls "discard_parentheses" e_rls 
   932 	       [Thm ("sym_real_mult_assoc",
   933                       num_str (@{thm real_mult_assoc} RS @{thm sym})),
   934 		Thm ("sym_add_assoc",
   935                       num_str (@{thm add_assoc} RS @{thm sym}))];
   936 
   937 val scr_make_polynomial = 
   938 "Script Expand_binoms t_t =                                  " ^
   939 "(Repeat                                                    " ^
   940 "((Try (Repeat (Rewrite real_diff_minus         False))) @@ " ^ 
   941 
   942 " (Try (Repeat (Rewrite left_distrib   False))) @@ " ^	 
   943 " (Try (Repeat (Rewrite right_distrib  False))) @@ " ^	
   944 " (Try (Repeat (Rewrite left_diff_distrib  False))) @@ " ^	
   945 " (Try (Repeat (Rewrite right_diff_distrib False))) @@ " ^	
   946 
   947 " (Try (Repeat (Rewrite mult_1_left             False))) @@ " ^		   
   948 " (Try (Repeat (Rewrite mult_zero_left             False))) @@ " ^		   
   949 " (Try (Repeat (Rewrite add_0_left      False))) @@ " ^	 
   950 
   951 " (Try (Repeat (Rewrite real_mult_commute       False))) @@ " ^		
   952 " (Try (Repeat (Rewrite real_mult_left_commute  False))) @@ " ^	
   953 " (Try (Repeat (Rewrite real_mult_assoc         False))) @@ " ^		
   954 " (Try (Repeat (Rewrite add_commute        False))) @@ " ^		
   955 " (Try (Repeat (Rewrite add_left_commute   False))) @@ " ^	 
   956 " (Try (Repeat (Rewrite add_assoc          False))) @@ " ^	 
   957 
   958 " (Try (Repeat (Rewrite sym_realpow_twoI        False))) @@ " ^	 
   959 " (Try (Repeat (Rewrite realpow_plus_1          False))) @@ " ^	 
   960 " (Try (Repeat (Rewrite sym_real_mult_2         False))) @@ " ^		
   961 " (Try (Repeat (Rewrite real_mult_2_assoc       False))) @@ " ^		
   962 
   963 " (Try (Repeat (Rewrite real_num_collect        False))) @@ " ^		
   964 " (Try (Repeat (Rewrite real_num_collect_assoc  False))) @@ " ^	
   965 
   966 " (Try (Repeat (Rewrite real_one_collect        False))) @@ " ^		
   967 " (Try (Repeat (Rewrite real_one_collect_assoc  False))) @@ " ^   
   968 
   969 " (Try (Repeat (Calculate PLUS  ))) @@                      " ^
   970 " (Try (Repeat (Calculate TIMES ))) @@                      " ^
   971 " (Try (Repeat (Calculate POWER))))                        " ^  
   972 " t_t)";
   973 
   974 (*version used by MG.02/03, overwritten by version AG in 04 below
   975 val make_polynomial = prep_rls(
   976   Seq{id = "make_polynomial", preconds = []:term list, 
   977       rew_ord = ("dummy_ord", dummy_ord),
   978       erls = Atools_erls, srls = Erls,
   979       calc = [],(*asm_thm = [],*)
   980       rules = [Rls_ expand_poly,
   981 	       Rls_ order_add_mult,
   982 	       Rls_ simplify_power,   (*realpow_eq_oneI, eg. x^1 --> x *)
   983 	       Rls_ collect_numerals, (*eg. x^(2+ -1) --> x^1          *)
   984 	       Rls_ reduce_012,
   985 	       Thm ("realpow_oneI",num_str @{thm realpow_oneI}),(*in --^*) 
   986 	       Rls_ discard_parentheses
   987 	       ],
   988       scr = EmptyScr
   989       }:rls);   *)
   990 
   991 val scr_expand_binoms =
   992 "Script Expand_binoms t_t =" ^
   993 "(Repeat                       " ^
   994 "((Try (Repeat (Rewrite real_plus_binom_pow2    False))) @@ " ^
   995 " (Try (Repeat (Rewrite real_plus_binom_times   False))) @@ " ^
   996 " (Try (Repeat (Rewrite real_minus_binom_pow2   False))) @@ " ^
   997 " (Try (Repeat (Rewrite real_minus_binom_times  False))) @@ " ^
   998 " (Try (Repeat (Rewrite real_plus_minus_binom1  False))) @@ " ^
   999 " (Try (Repeat (Rewrite real_plus_minus_binom2  False))) @@ " ^
  1000 
  1001 " (Try (Repeat (Rewrite mult_1_left             False))) @@ " ^
  1002 " (Try (Repeat (Rewrite mult_zero_left             False))) @@ " ^
  1003 " (Try (Repeat (Rewrite add_0_left      False))) @@ " ^
  1004 
  1005 " (Try (Repeat (Calculate PLUS  ))) @@ " ^
  1006 " (Try (Repeat (Calculate TIMES ))) @@ " ^
  1007 " (Try (Repeat (Calculate POWER))) @@ " ^
  1008 
  1009 " (Try (Repeat (Rewrite sym_realpow_twoI        False))) @@ " ^
  1010 " (Try (Repeat (Rewrite realpow_plus_1          False))) @@ " ^
  1011 " (Try (Repeat (Rewrite sym_real_mult_2         False))) @@ " ^
  1012 " (Try (Repeat (Rewrite real_mult_2_assoc       False))) @@ " ^
  1013 
  1014 " (Try (Repeat (Rewrite real_num_collect        False))) @@ " ^
  1015 " (Try (Repeat (Rewrite real_num_collect_assoc  False))) @@ " ^
  1016 
  1017 " (Try (Repeat (Rewrite real_one_collect        False))) @@ " ^
  1018 " (Try (Repeat (Rewrite real_one_collect_assoc  False))) @@ " ^ 
  1019 
  1020 " (Try (Repeat (Calculate PLUS  ))) @@ " ^
  1021 " (Try (Repeat (Calculate TIMES ))) @@ " ^
  1022 " (Try (Repeat (Calculate POWER)))) " ^  
  1023 " t_t)";
  1024 
  1025 val expand_binoms = 
  1026   Rls{id = "expand_binoms", preconds = [], rew_ord = ("termlessI",termlessI),
  1027       erls = Atools_erls, srls = Erls,
  1028       calc = [("PLUS"  , ("op +", eval_binop "#add_")), 
  1029 	      ("TIMES" , ("op *", eval_binop "#mult_")),
  1030 	      ("POWER", ("Atools.pow", eval_binop "#power_"))
  1031 	      ],
  1032       rules = [Thm ("real_plus_binom_pow2",
  1033                      num_str @{thm real_plus_binom_pow2}),     
  1034 	       (*"(a + b) ^^^ 2 = a ^^^ 2 + 2 * a * b + b ^^^ 2"*)
  1035 	       Thm ("real_plus_binom_times",
  1036                      num_str @{thm real_plus_binom_times}),    
  1037 	      (*"(a + b)*(a + b) = ...*)
  1038 	       Thm ("real_minus_binom_pow2",
  1039                      num_str @{thm real_minus_binom_pow2}),   
  1040 	       (*"(a - b) ^^^ 2 = a ^^^ 2 - 2 * a * b + b ^^^ 2"*)
  1041 	       Thm ("real_minus_binom_times",
  1042                      num_str @{thm real_minus_binom_times}),   
  1043 	       (*"(a - b)*(a - b) = ...*)
  1044 	       Thm ("real_plus_minus_binom1",
  1045                      num_str @{thm real_plus_minus_binom1}),   
  1046 		(*"(a + b) * (a - b) = a ^^^ 2 - b ^^^ 2"*)
  1047 	       Thm ("real_plus_minus_binom2",
  1048                      num_str @{thm real_plus_minus_binom2}),   
  1049 		(*"(a - b) * (a + b) = a ^^^ 2 - b ^^^ 2"*)
  1050 	       (*RL 020915*)
  1051 	       Thm ("real_pp_binom_times",num_str @{thm real_pp_binom_times}), 
  1052 		(*(a + b)*(c + d) = a*c + a*d + b*c + b*d*)
  1053                Thm ("real_pm_binom_times",num_str @{thm real_pm_binom_times}), 
  1054 		(*(a + b)*(c - d) = a*c - a*d + b*c - b*d*)
  1055                Thm ("real_mp_binom_times",num_str @{thm real_mp_binom_times}), 
  1056 		(*(a - b)*(c + d) = a*c + a*d - b*c - b*d*)
  1057                Thm ("real_mm_binom_times",num_str @{thm real_mm_binom_times}), 
  1058 		(*(a - b)*(c - d) = a*c - a*d - b*c + b*d*)
  1059 	       Thm ("realpow_multI",num_str @{thm realpow_multI}),
  1060 		(*(a*b)^^^n = a^^^n * b^^^n*)
  1061 	       Thm ("real_plus_binom_pow3",num_str @{thm real_plus_binom_pow3}),
  1062 	        (* (a + b)^^^3 = a^^^3 + 3*a^^^2*b + 3*a*b^^^2 + b^^^3 *)
  1063 	       Thm ("real_minus_binom_pow3",
  1064                      num_str @{thm real_minus_binom_pow3}),
  1065 	        (* (a - b)^^^3 = a^^^3 - 3*a^^^2*b + 3*a*b^^^2 - b^^^3 *)
  1066 
  1067 
  1068               (*Thm ("left_distrib" ,num_str @{thm left_distrib}),	
  1069 		(*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
  1070 	       Thm ("right_distrib",num_str @{thm right_distrib}),	
  1071 	       (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
  1072 	       Thm ("left_diff_distrib" ,num_str @{thm left_diff_distrib}),	
  1073 	       (*"(z1.0 - z2.0) * w = z1.0 * w - z2.0 * w"*)
  1074 	       Thm ("left_diff_distrib2",num_str @{thm left_diff_distrib2}),	
  1075 	       (*"w * (z1.0 - z2.0) = w * z1.0 - w * z2.0"*)
  1076 	      *)
  1077 	       Thm ("mult_1_left",num_str @{thm mult_1_left}),
  1078                (*"1 * z = z"*)
  1079 	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),
  1080                (*"0 * z = 0"*)
  1081 	       Thm ("add_0_left",num_str @{thm add_0_left}),(*"0 + z = z"*)
  1082 
  1083 	       Calc ("op +", eval_binop "#add_"), 
  1084 	       Calc ("op *", eval_binop "#mult_"),
  1085 	       Calc ("Atools.pow", eval_binop "#power_"),
  1086               (*Thm ("real_mult_commute",num_str @{thm real_mult_commute}),
  1087 		(*AC-rewriting*)
  1088 	       Thm ("real_mult_left_commute",
  1089                      num_str @{thm real_mult_left_commute}),
  1090 	       Thm ("real_mult_assoc",num_str @{thm real_mult_assoc}),
  1091 	       Thm ("add_commute",num_str @{thm add_commute}),
  1092 	       Thm ("add_left_commute",num_str @{thm add_left_commute}),
  1093 	       Thm ("add_assoc",num_str @{thm add_assoc}),
  1094 	      *)
  1095 	       Thm ("sym_realpow_twoI",
  1096                      num_str (@{thm realpow_twoI} RS @{thm sym})),
  1097 	       (*"r1 * r1 = r1 ^^^ 2"*)
  1098 	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),			
  1099 	       (*"r * r ^^^ n = r ^^^ (n + 1)"*)
  1100 	       (*Thm ("sym_real_mult_2",
  1101                        num_str (@{thm real_mult_2} RS @{thm sym})),		
  1102 	       (*"z1 + z1 = 2 * z1"*)*)
  1103 	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),		
  1104 	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
  1105 
  1106 	       Thm ("real_num_collect",num_str @{thm real_num_collect}), 
  1107 	       (*"[| l is_const; m is_const |] ==>l * n + m * n = (l + m) * n"*)
  1108 	       Thm ("real_num_collect_assoc",
  1109                      num_str @{thm real_num_collect_assoc}),	
  1110 	       (*"[| l is_const; m is_const |] ==>  
  1111                                        l * n + (m * n + k) =  (l + m) * n + k"*)
  1112 	       Thm ("real_one_collect",num_str @{thm real_one_collect}),
  1113 	       (*"m is_const ==> n + m * n = (1 + m) * n"*)
  1114 	       Thm ("real_one_collect_assoc",
  1115                      num_str @{thm real_one_collect_assoc}), 
  1116 	       (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
  1117 
  1118 	       Calc ("op +", eval_binop "#add_"), 
  1119 	       Calc ("op *", eval_binop "#mult_"),
  1120 	       Calc ("Atools.pow", eval_binop "#power_")
  1121 	       ],
  1122       scr = Script ((term_of o the o (parse thy)) scr_expand_binoms)
  1123       }:rls;      
  1124 
  1125 
  1126 (**. MG.03: make_polynomial_ ... uses SML-fun for ordering .**)
  1127 
  1128 (*FIXME.0401: make SML-order local to make_polynomial(_) *)
  1129 (*FIXME.0401: replace 'make_polynomial'(old) by 'make_polynomial_'(MG) *)
  1130 (* Polynom --> List von Monomen *) 
  1131 fun poly2list (Const ("op +",_) $ t1 $ t2) = 
  1132     (poly2list t1) @ (poly2list t2)
  1133   | poly2list t = [t];
  1134 
  1135 (* Monom --> Liste von Variablen *)
  1136 fun monom2list (Const ("op *",_) $ t1 $ t2) = 
  1137     (monom2list t1) @ (monom2list t2)
  1138   | monom2list t = [t];
  1139 
  1140 (* liefert Variablenname (String) einer Variablen und Basis bei Potenz *)
  1141 fun get_basStr (Const ("Atools.pow",_) $ Free (str, _) $ _) = str
  1142   | get_basStr (Free (str, _)) = str
  1143   | get_basStr t = "|||"; (* gross gewichtet; für Brüch ect. *)
  1144 (*| get_basStr t = 
  1145     raise error("get_basStr: called with t= "^(term2str t));*)
  1146 
  1147 (* liefert Hochzahl (String) einer Variablen bzw Gewichtstring (zum Sortieren) *)
  1148 fun get_potStr (Const ("Atools.pow",_) $ Free _ $ Free (str, _)) = str
  1149   | get_potStr (Const ("Atools.pow",_) $ Free _ $ _ ) = "|||" (* gross gewichtet *)
  1150   | get_potStr (Free (str, _)) = "---" (* keine Hochzahl --> kleinst gewichtet *)
  1151   | get_potStr t = "||||||"; (* gross gewichtet; für Brüch ect. *)
  1152 (*| get_potStr t = 
  1153     raise error("get_potStr: called with t= "^(term2str t));*)
  1154 
  1155 (* Umgekehrte string_ord *)
  1156 val string_ord_rev =  rev_order o string_ord;
  1157 		
  1158  (* Ordnung zum lexikographischen Vergleich zweier Variablen (oder Potenzen) 
  1159     innerhalb eines Monomes:
  1160     - zuerst lexikographisch nach Variablenname 
  1161     - wenn gleich: nach steigender Potenz *)
  1162 fun var_ord (a,b: term) = prod_ord string_ord string_ord 
  1163     ((get_basStr a, get_potStr a), (get_basStr b, get_potStr b));
  1164 
  1165 (* Ordnung zum lexikographischen Vergleich zweier Variablen (oder Potenzen); 
  1166    verwendet zum Sortieren von Monomen mittels Gesamtgradordnung:
  1167    - zuerst lexikographisch nach Variablenname 
  1168    - wenn gleich: nach sinkender Potenz*)
  1169 fun var_ord_revPow (a,b: term) = prod_ord string_ord string_ord_rev 
  1170     ((get_basStr a, get_potStr a), (get_basStr b, get_potStr b));
  1171 
  1172 
  1173 (* Ordnet ein Liste von Variablen (und Potenzen) lexikographisch *)
  1174 val sort_varList = sort var_ord;
  1175 
  1176 (* Entfernet aeussersten Operator (Wurzel) aus einem Term und schreibt 
  1177    Argumente in eine Liste *)
  1178 fun args u : term list =
  1179     let fun stripc (f$t, ts) = stripc (f, t::ts)
  1180 	  | stripc (t as Free _, ts) = (t::ts)
  1181 	  | stripc (_, ts) = ts
  1182     in stripc (u, []) end;
  1183                                     
  1184 (* liefert True, falls der Term (Liste von Termen) nur Zahlen 
  1185    (keine Variablen) enthaelt *)
  1186 fun filter_num [] = true
  1187   | filter_num [Free x] = if (is_num (Free x)) then true
  1188 				else false
  1189   | filter_num ((Free _)::_) = false
  1190   | filter_num ts =
  1191     (filter_num o (filter_out is_num) o flat o (map args)) ts;
  1192 
  1193 (* liefert True, falls der Term nur Zahlen (keine Variablen) enthaelt 
  1194    dh. er ist ein numerischer Wert und entspricht einem Koeffizienten *)
  1195 fun is_nums t = filter_num [t];
  1196 
  1197 (* Berechnet den Gesamtgrad eines Monoms *)
  1198 local 
  1199     fun counter (n, []) = n
  1200       | counter (n, x :: xs) = 
  1201 	if (is_nums x) then
  1202 	    counter (n, xs) 
  1203 	else 
  1204 	    (case x of 
  1205 		 (Const ("Atools.pow", _) $ Free (str_b, _) $ Free (str_h, T)) => 
  1206 		     if (is_nums (Free (str_h, T))) then
  1207 			 counter (n + (the (int_of_str str_h)), xs)
  1208 		     else counter (n + 1000, xs) (*FIXME.MG?!*)
  1209 	       | (Const ("Atools.pow", _) $ Free (str_b, _) $ _ ) => 
  1210 		     counter (n + 1000, xs) (*FIXME.MG?!*)
  1211 	       | (Free (str, _)) => counter (n + 1, xs)
  1212 	     (*| _ => raise error("monom_degree: called with factor: "^(term2str x)))*)
  1213 	       | _ => counter (n + 10000, xs)) (*FIXME.MG?! ... Brüche ect.*)
  1214 in  
  1215     fun monom_degree l = counter (0, l) 
  1216 end;
  1217 
  1218 (* wie Ordnung dict_ord (lexicographische Ordnung zweier Listen, mit Vergleich 
  1219    der Listen-Elemente mit elem_ord) - Elemente die Bedingung cond erfuellen, 
  1220    werden jedoch dabei ignoriert (uebersprungen)  *)
  1221 fun dict_cond_ord _ _ ([], []) = EQUAL
  1222   | dict_cond_ord _ _ ([], _ :: _) = LESS
  1223   | dict_cond_ord _ _ (_ :: _, []) = GREATER
  1224   | dict_cond_ord elem_ord cond (x :: xs, y :: ys) =
  1225     (case (cond x, cond y) of 
  1226 	 (false, false) => (case elem_ord (x, y) of 
  1227 				EQUAL => dict_cond_ord elem_ord cond (xs, ys) 
  1228 			      | ord => ord)
  1229        | (false, true)  => dict_cond_ord elem_ord cond (x :: xs, ys)
  1230        | (true, false)  => dict_cond_ord elem_ord cond (xs, y :: ys)
  1231        | (true, true)  =>  dict_cond_ord elem_ord cond (xs, ys) );
  1232 
  1233 (* Gesamtgradordnung zum Vergleich von Monomen (Liste von Variablen/Potenzen):
  1234    zuerst nach Gesamtgrad, bei gleichem Gesamtgrad lexikographisch ordnen - 
  1235    dabei werden Koeffizienten ignoriert (2*3*a^^^2*4*b gilt wie a^^^2*b) *)
  1236 fun degree_ord (xs, ys) =
  1237 	    prod_ord int_ord (dict_cond_ord var_ord_revPow is_nums) 
  1238 	    ((monom_degree xs, xs), (monom_degree ys, ys));
  1239 
  1240 fun hd_str str = substring (str, 0, 1);
  1241 fun tl_str str = substring (str, 1, (size str) - 1);
  1242 
  1243 (* liefert nummerischen Koeffizienten eines Monoms oder NONE *)
  1244 fun get_koeff_of_mon [] =  raise error("get_koeff_of_mon: called with l = []")
  1245   | get_koeff_of_mon (l as x::xs) = if is_nums x then SOME x
  1246 				    else NONE;
  1247 
  1248 (* wandelt Koeffizient in (zum sortieren geeigneten) String um *)
  1249 fun koeff2ordStr (SOME x) = (case x of 
  1250 				 (Free (str, T)) => 
  1251 				     if (hd_str str) = "-" then (tl_str str)^"0" (* 3 < -3 *)
  1252 				     else str
  1253 			       | _ => "aaa") (* "num.Ausdruck" --> gross *)
  1254   | koeff2ordStr NONE = "---"; (* "kein Koeff" --> kleinste *)
  1255 
  1256 (* Order zum Vergleich von Koeffizienten (strings): 
  1257    "kein Koeff" < "0" < "1" < "-1" < "2" < "-2" < ... < "num.Ausdruck" *)
  1258 fun compare_koeff_ord (xs, ys) = 
  1259     string_ord ((koeff2ordStr o get_koeff_of_mon) xs,
  1260 		(koeff2ordStr o get_koeff_of_mon) ys);
  1261 
  1262 (* Gesamtgradordnung degree_ord + Ordnen nach Koeffizienten falls EQUAL *)
  1263 fun koeff_degree_ord (xs, ys) =
  1264 	    prod_ord degree_ord compare_koeff_ord ((xs, xs), (ys, ys));
  1265 
  1266 (* Ordnet ein Liste von Monomen (Monom = Liste von Variablen) mittels 
  1267    Gesamtgradordnung *)
  1268 val sort_monList = sort koeff_degree_ord;
  1269 
  1270 (* Alternativ zu degree_ord koennte auch die viel einfachere und 
  1271    kuerzere Ordnung simple_ord verwendet werden - ist aber nicht 
  1272    fuer unsere Zwecke geeignet!
  1273 
  1274 fun simple_ord (al,bl: term list) = dict_ord string_ord 
  1275 	 (map get_basStr al, map get_basStr bl); 
  1276 
  1277 val sort_monList = sort simple_ord; *)
  1278 
  1279 (* aus 2 Variablen wird eine Summe bzw ein Produkt erzeugt 
  1280    (mit gewuenschtem Typen T) *)
  1281 fun plus T = Const ("op +", [T,T] ---> T);
  1282 fun mult T = Const ("op *", [T,T] ---> T);
  1283 fun binop op_ t1 t2 = op_ $ t1 $ t2;
  1284 fun create_prod T (a,b) = binop (mult T) a b;
  1285 fun create_sum T (a,b) = binop (plus T) a b;
  1286 
  1287 (* löscht letztes Element einer Liste *)
  1288 fun drop_last l = take ((length l)-1,l);
  1289 
  1290 (* Liste von Variablen --> Monom *)
  1291 fun create_monom T vl = foldr (create_prod T) (drop_last vl, last_elem vl);
  1292 (* Bemerkung: 
  1293    foldr bewirkt rechtslastige Klammerung des Monoms - ist notwendig, damit zwei 
  1294    gleiche Monome zusammengefasst werden können (collect_numerals)! 
  1295    zB: 2*(x*(y*z)) + 3*(x*(y*z)) --> (2+3)*(x*(y*z))*)
  1296 
  1297 (* Liste von Monomen --> Polynom *)	
  1298 fun create_polynom T ml = foldl (create_sum T) (hd ml, tl ml);
  1299 (* Bemerkung: 
  1300    foldl bewirkt linkslastige Klammerung des Polynoms (der Summanten) - 
  1301    bessere Darstellung, da keine Klammern sichtbar! 
  1302    (und discard_parentheses in make_polynomial hat weniger zu tun) *)
  1303 
  1304 (* sorts the variables (faktors) of an expanded polynomial lexicographical *)
  1305 fun sort_variables t = 
  1306     let
  1307 	val ll =  map monom2list (poly2list t);
  1308 	val lls = map sort_varList ll; 
  1309 	val T = type_of t;
  1310 	val ls = map (create_monom T) lls;
  1311     in create_polynom T ls end;
  1312 
  1313 (* sorts the monoms of an expanded and variable-sorted polynomial 
  1314    by total_degree *)
  1315 fun sort_monoms t = 
  1316     let
  1317 	val ll =  map monom2list (poly2list t);
  1318 	val lls = sort_monList ll;
  1319 	val T = type_of t;
  1320 	val ls = map (create_monom T) lls;
  1321     in create_polynom T ls end;
  1322 
  1323 (* auch Klammerung muss übereinstimmen; 
  1324    sort_variables klammert Produkte rechtslastig*)
  1325 fun is_multUnordered t = ((is_polyexp t) andalso not (t = sort_variables t));
  1326 
  1327 fun eval_is_multUnordered (thmid:string) _ 
  1328 		       (t as (Const("Poly.is'_multUnordered", _) $ arg)) thy = 
  1329     if is_multUnordered arg
  1330     then SOME (mk_thmid thmid "" 
  1331 			((Syntax.string_of_term (thy2ctxt thy)) arg) "", 
  1332 	       Trueprop $ (mk_equality (t, HOLogic.true_const)))
  1333     else SOME (mk_thmid thmid "" 
  1334 			((Syntax.string_of_term (thy2ctxt thy)) arg) "", 
  1335 	       Trueprop $ (mk_equality (t, HOLogic.false_const)))
  1336   | eval_is_multUnordered _ _ _ _ = NONE; 
  1337 
  1338 
  1339 fun attach_form (_:rule list list) (_:term) (_:term) = (*still missing*)
  1340     []:(rule * (term * term list)) list;
  1341 fun init_state (_:term) = e_rrlsstate;
  1342 fun locate_rule (_:rule list list) (_:term) (_:rule) =
  1343     ([]:(rule * (term * term list)) list);
  1344 fun next_rule (_:rule list list) (_:term) = (NONE:rule option);
  1345 fun normal_form t = SOME (sort_variables t,[]:term list);
  1346 
  1347 val order_mult_ =
  1348     Rrls {id = "order_mult_", 
  1349 	  prepat = 
  1350 	  [([(term_of o the o (parse thy)) "p is_multUnordered"], 
  1351 	    parse_patt thy "?p" )],
  1352 	  rew_ord = ("dummy_ord", dummy_ord),
  1353 	  erls = append_rls "e_rls-is_multUnordered" e_rls(*MG: poly_erls*)
  1354 			    [Calc ("Poly.is'_multUnordered", 
  1355                                     eval_is_multUnordered "")],
  1356 	  calc = [("PLUS"  , ("op +"      , eval_binop "#add_")),
  1357 		  ("TIMES" , ("op *"      , eval_binop "#mult_")),
  1358 		  ("DIVIDE", ("HOL.divide", eval_cancel "#divide_")),
  1359 		  ("POWER" , ("Atools.pow", eval_binop "#power_"))],
  1360 	  scr=Rfuns {init_state  = init_state,
  1361 		     normal_form = normal_form,
  1362 		     locate_rule = locate_rule,
  1363 		     next_rule   = next_rule,
  1364 		     attach_form = attach_form}};
  1365 val order_mult_rls_ = 
  1366   Rls{id = "order_mult_rls_", preconds = [], 
  1367       rew_ord = ("dummy_ord", dummy_ord),
  1368       erls = e_rls,srls = Erls,
  1369       calc = [],
  1370       rules = [Rls_ order_mult_
  1371 	       ], scr = EmptyScr}:rls;
  1372 
  1373 fun is_addUnordered t = ((is_polyexp t) andalso not (t = sort_monoms t));
  1374 
  1375 (*WN.18.6.03 *)
  1376 (*("is_addUnordered", ("Poly.is'_addUnordered", eval_is_addUnordered ""))*)
  1377 fun eval_is_addUnordered (thmid:string) _ 
  1378 		       (t as (Const("Poly.is'_addUnordered", _) $ arg)) thy = 
  1379     if is_addUnordered arg
  1380     then SOME (mk_thmid thmid "" 
  1381 			((Syntax.string_of_term (thy2ctxt thy)) arg) "", 
  1382 	       Trueprop $ (mk_equality (t, HOLogic.true_const)))
  1383     else SOME (mk_thmid thmid "" 
  1384 			((Syntax.string_of_term (thy2ctxt thy)) arg) "", 
  1385 	       Trueprop $ (mk_equality (t, HOLogic.false_const)))
  1386   | eval_is_addUnordered _ _ _ _ = NONE; 
  1387 
  1388 fun attach_form (_:rule list list) (_:term) (_:term) = (*still missing*)
  1389     []:(rule * (term * term list)) list;
  1390 fun init_state (_:term) = e_rrlsstate;
  1391 fun locate_rule (_:rule list list) (_:term) (_:rule) =
  1392     ([]:(rule * (term * term list)) list);
  1393 fun next_rule (_:rule list list) (_:term) = (NONE:rule option);
  1394 fun normal_form t = SOME (sort_monoms t,[]:term list);
  1395 
  1396 val order_add_ =
  1397     Rrls {id = "order_add_", 
  1398 	  prepat = (*WN.18.6.03 Preconditions und Pattern,
  1399 		    die beide passen muessen, damit das Rrls angewandt wird*)
  1400 	  [([(term_of o the o (parse thy)) "p is_addUnordered"], 
  1401 	    parse_patt thy "?p" 
  1402 	    (*WN.18.6.03 also KEIN pattern, dieses erzeugt nur das Environment 
  1403 	      fuer die Evaluation der Precondition "p is_addUnordered"*))],
  1404 	  rew_ord = ("dummy_ord", dummy_ord),
  1405 	  erls = append_rls "e_rls-is_addUnordered" e_rls(*MG: poly_erls*)
  1406 			    [Calc ("Poly.is'_addUnordered", eval_is_addUnordered "")
  1407 			     (*WN.18.6.03 definiert in thy,
  1408                                evaluiert prepat*)],
  1409 	  calc = [("PLUS"    ,("op +"        ,eval_binop "#add_")),
  1410 		  ("TIMES"   ,("op *"        ,eval_binop "#mult_")),
  1411 		  ("DIVIDE" ,("HOL.divide"  ,eval_cancel "#divide_")),
  1412 		  ("POWER"  ,("Atools.pow"  ,eval_binop "#power_"))],
  1413 	  (*asm_thm=[],*)
  1414 	  scr=Rfuns {init_state  = init_state,
  1415 		     normal_form = normal_form,
  1416 		     locate_rule = locate_rule,
  1417 		     next_rule   = next_rule,
  1418 		     attach_form = attach_form}};
  1419 
  1420 val order_add_rls_ = 
  1421   Rls{id = "order_add_rls_", preconds = [], 
  1422       rew_ord = ("dummy_ord", dummy_ord),
  1423       erls = e_rls,srls = Erls,
  1424       calc = [],
  1425       (*asm_thm = [],*)
  1426       rules = [Rls_ order_add_
  1427 	       ], scr = EmptyScr}:rls;
  1428 
  1429 (*. see MG-DA.p.52ff .*)
  1430 val make_polynomial(*MG.03, overwrites version from above, 
  1431     previously 'make_polynomial_'*) =
  1432   Seq {id = "make_polynomial", preconds = []:term list, 
  1433       rew_ord = ("dummy_ord", dummy_ord),
  1434       erls = Atools_erls, srls = Erls,calc = [],
  1435       rules = [Rls_ discard_minus1,
  1436 	       Rls_ expand_poly_,
  1437 	       Calc ("op *", eval_binop "#mult_"),
  1438 	       Rls_ order_mult_rls_,
  1439 	       Rls_ simplify_power_, 
  1440 	       Rls_ calc_add_mult_pow_, 
  1441 	       Rls_ reduce_012_mult_,
  1442 	       Rls_ order_add_rls_,
  1443 	       Rls_ collect_numerals_, 
  1444 	       Rls_ reduce_012_,
  1445 	       Rls_ discard_parentheses1
  1446 	       ],
  1447       scr = EmptyScr
  1448       }:rls;
  1449 val norm_Poly(*=make_polynomial*) = 
  1450   Seq {id = "norm_Poly", preconds = []:term list, 
  1451       rew_ord = ("dummy_ord", dummy_ord),
  1452       erls = Atools_erls, srls = Erls, calc = [],
  1453       rules = [Rls_ discard_minus1,
  1454 	       Rls_ expand_poly_,
  1455 	       Calc ("op *", eval_binop "#mult_"),
  1456 	       Rls_ order_mult_rls_,
  1457 	       Rls_ simplify_power_, 
  1458 	       Rls_ calc_add_mult_pow_, 
  1459 	       Rls_ reduce_012_mult_,
  1460 	       Rls_ order_add_rls_,
  1461 	       Rls_ collect_numerals_, 
  1462 	       Rls_ reduce_012_,
  1463 	       Rls_ discard_parentheses1
  1464 	       ],
  1465       scr = EmptyScr
  1466       }:rls;
  1467 
  1468 (* MG:03 Like make_polynomial_ but without Rls_ discard_parentheses1 
  1469    and expand_poly_rat_ instead of expand_poly_, see MG-DA.p.56ff*)
  1470 (* MG necessary  for termination of norm_Rational(*_mg*) in Rational.ML*)
  1471 val make_rat_poly_with_parentheses =
  1472   Seq{id = "make_rat_poly_with_parentheses", preconds = []:term list, 
  1473       rew_ord = ("dummy_ord", dummy_ord),
  1474       erls = Atools_erls, srls = Erls, calc = [],
  1475       rules = [Rls_ discard_minus1,
  1476 	       Rls_ expand_poly_rat_,(*ignors rationals*)
  1477 	       Calc ("op *", eval_binop "#mult_"),
  1478 	       Rls_ order_mult_rls_,
  1479 	       Rls_ simplify_power_, 
  1480 	       Rls_ calc_add_mult_pow_, 
  1481 	       Rls_ reduce_012_mult_,
  1482 	       Rls_ order_add_rls_,
  1483 	       Rls_ collect_numerals_, 
  1484 	       Rls_ reduce_012_
  1485 	       (*Rls_ discard_parentheses1 *)
  1486 	       ],
  1487       scr = EmptyScr
  1488       }:rls;
  1489 
  1490 (*.a minimal ruleset for reverse rewriting of factions [2];
  1491    compare expand_binoms.*)
  1492 val rev_rew_p = 
  1493 Seq{id = "reverse_rewriting", preconds = [], rew_ord = ("termlessI",termlessI),
  1494     erls = Atools_erls, srls = Erls,
  1495     calc = [(*("PLUS"  , ("op +", eval_binop "#add_")), 
  1496 	    ("TIMES" , ("op *", eval_binop "#mult_")),
  1497 	    ("POWER", ("Atools.pow", eval_binop "#power_"))*)
  1498 	    ],
  1499     rules = [Thm ("real_plus_binom_times" ,num_str @{thm real_plus_binom_times}),
  1500 	     (*"(a + b)*(a + b) = a ^ 2 + 2 * a * b + b ^ 2*)
  1501 	     Thm ("real_plus_binom_times1" ,num_str @{thm real_plus_binom_times1}),
  1502 	     (*"(a +  1*b)*(a + -1*b) = a^^^2 + -1*b^^^2"*)
  1503 	     Thm ("real_plus_binom_times2" ,num_str @{thm real_plus_binom_times2}),
  1504 	     (*"(a + -1*b)*(a +  1*b) = a^^^2 + -1*b^^^2"*)
  1505 
  1506 	     Thm ("mult_1_left",num_str @{thm mult_1_left}),(*"1 * z = z"*)
  1507 
  1508              Thm ("left_distrib" ,num_str @{thm left_distrib}),
  1509 	     (*"(z1.0 + z2.0) * w = z1.0 * w + z2.0 * w"*)
  1510 	     Thm ("right_distrib",num_str @{thm right_distrib}),
  1511 	     (*"w * (z1.0 + z2.0) = w * z1.0 + w * z2.0"*)
  1512 	       
  1513 	     Thm ("real_mult_assoc", num_str @{thm real_mult_assoc}),
  1514 	     (*"?z1.1 * ?z2.1 * ?z3. =1 ?z1.1 * (?z2.1 * ?z3.1)"*)
  1515 	     Rls_ order_mult_rls_,
  1516 	     (*Rls_ order_add_rls_,*)
  1517 
  1518 	     Calc ("op +", eval_binop "#add_"), 
  1519 	     Calc ("op *", eval_binop "#mult_"),
  1520 	     Calc ("Atools.pow", eval_binop "#power_"),
  1521 	     
  1522 	     Thm ("sym_realpow_twoI",
  1523                    num_str (@{thm realpow_twoI} RS @{thm sym})),
  1524 	     (*"r1 * r1 = r1 ^^^ 2"*)
  1525 	     Thm ("sym_real_mult_2",
  1526                    num_str (@{thm real_mult_2} RS @{thm sym})),
  1527 	     (*"z1 + z1 = 2 * z1"*)
  1528 	     Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),
  1529 	     (*"z1 + (z1 + k) = 2 * z1 + k"*)
  1530 
  1531 	     Thm ("real_num_collect",num_str @{thm real_num_collect}), 
  1532 	     (*"[| l is_const; m is_const |]==>l * n + m * n = (l + m) * n"*)
  1533 	     Thm ("real_num_collect_assoc",num_str @{thm real_num_collect_assoc}),
  1534 	     (*"[| l is_const; m is_const |] ==>  
  1535                                      l * n + (m * n + k) =  (l + m) * n + k"*)
  1536 	     Thm ("real_one_collect",num_str @{thm real_one_collect}),
  1537 	     (*"m is_const ==> n + m * n = (1 + m) * n"*)
  1538 	     Thm ("real_one_collect_assoc",num_str @{thm real_one_collect_assoc}), 
  1539 	     (*"m is_const ==> k + (n + m * n) = k + (1 + m) * n"*)
  1540 
  1541 	     Thm ("realpow_multI", num_str @{thm realpow_multI}),
  1542 	     (*"(r * s) ^^^ n = r ^^^ n * s ^^^ n"*)
  1543 
  1544 	     Calc ("op +", eval_binop "#add_"), 
  1545 	     Calc ("op *", eval_binop "#mult_"),
  1546 	     Calc ("Atools.pow", eval_binop "#power_"),
  1547 
  1548 	     Thm ("mult_1_left",num_str @{thm mult_1_left}),(*"1 * z = z"*)
  1549 	     Thm ("mult_zero_left",num_str @{thm mult_zero_left}),(*"0 * z = 0"*)
  1550 	     Thm ("add_0_left",num_str @{thm add_0_left})(*0 + z = z*)
  1551 
  1552 	     (*Rls_ order_add_rls_*)
  1553 	     ],
  1554 
  1555     scr = EmptyScr}:rls;      
  1556 
  1557 ruleset' := 
  1558 overwritelthy @{theory} (!ruleset',
  1559 		   [("norm_Poly", prep_rls norm_Poly),
  1560 		    ("Poly_erls",Poly_erls)(*FIXXXME:del with rls.rls'*),
  1561 		    ("expand", prep_rls expand),
  1562 		    ("expand_poly", prep_rls expand_poly),
  1563 		    ("simplify_power", prep_rls simplify_power),
  1564 		    ("order_add_mult", prep_rls order_add_mult),
  1565 		    ("collect_numerals", prep_rls collect_numerals),
  1566 		    ("collect_numerals_", prep_rls collect_numerals_),
  1567 		    ("reduce_012", prep_rls reduce_012),
  1568 		    ("discard_parentheses", prep_rls discard_parentheses),
  1569 		    ("make_polynomial", prep_rls make_polynomial),
  1570 		    ("expand_binoms", prep_rls expand_binoms),
  1571 		    ("rev_rew_p", prep_rls rev_rew_p),
  1572 		    ("discard_minus1", prep_rls discard_minus1),
  1573 		    ("expand_poly_", prep_rls expand_poly_),
  1574 		    ("expand_poly_rat_", prep_rls expand_poly_rat_),
  1575 		    ("simplify_power_", prep_rls simplify_power_),
  1576 		    ("calc_add_mult_pow_", prep_rls calc_add_mult_pow_),
  1577 		    ("reduce_012_mult_", prep_rls reduce_012_mult_),
  1578 		    ("reduce_012_", prep_rls reduce_012_),
  1579 		    ("discard_parentheses1",prep_rls discard_parentheses1),
  1580 		    ("order_mult_rls_", prep_rls order_mult_rls_),
  1581 		    ("order_add_rls_", prep_rls order_add_rls_),
  1582 		    ("make_rat_poly_with_parentheses", 
  1583 		     prep_rls make_rat_poly_with_parentheses)
  1584 		    ]);
  1585 
  1586 calclist':= overwritel (!calclist', 
  1587    [("is_polyrat_in", ("Poly.is'_polyrat'_in", 
  1588 		       eval_is_polyrat_in "#eval_is_polyrat_in")),
  1589     ("is_expanded_in", ("Poly.is'_expanded'_in", eval_is_expanded_in "")),
  1590     ("is_poly_in", ("Poly.is'_poly'_in", eval_is_poly_in "")),
  1591     ("has_degree_in", ("Poly.has'_degree'_in", eval_has_degree_in "")),
  1592     ("is_polyexp", ("Poly.is'_polyexp", eval_is_polyexp "")),
  1593     ("is_multUnordered", ("Poly.is'_multUnordered", eval_is_multUnordered"")),
  1594     ("is_addUnordered", ("Poly.is'_addUnordered", eval_is_addUnordered ""))
  1595     ]);
  1596 
  1597 val ------------------------------------------------------ = "11111";
  1598 
  1599 (** problems **)
  1600 
  1601 store_pbt
  1602  (prep_pbt thy "pbl_simp_poly" [] e_pblID
  1603  (["polynomial","simplification"],
  1604   [("#Given" ,["TERM t_t"]),
  1605    ("#Where" ,["t_t is_polyexp"]),
  1606    ("#Find"  ,["normalform n_n"])
  1607   ],
  1608   append_rls "e_rls" e_rls [(*for preds in where_*)
  1609 			    Calc ("Poly.is'_polyexp", eval_is_polyexp "")], 
  1610   SOME "Simplify t_t", 
  1611   [["simplification","for_polynomials"]]));
  1612 
  1613 (** methods **)
  1614 
  1615 store_met
  1616     (prep_met thy "met_simp_poly" [] e_metID
  1617 	      (["simplification","for_polynomials"],
  1618 	       [("#Given" ,["TERM t_t"]),
  1619 		("#Where" ,["t_t is_polyexp"]),
  1620 		("#Find"  ,["normalform n_n"])
  1621 		],
  1622 	       {rew_ord'="tless_true",
  1623 		rls' = e_rls,
  1624 		calc = [], 
  1625 		srls = e_rls, 
  1626 		prls = append_rls "simplification_for_polynomials_prls" e_rls 
  1627 				  [(*for preds in where_*)
  1628 				   Calc ("Poly.is'_polyexp",eval_is_polyexp"")],
  1629 		crls = e_rls, nrls = norm_Poly},
  1630 	       "Script SimplifyScript (t_t::real) =                " ^
  1631 	       "  ((Rewrite_Set norm_Poly False) t_t)"
  1632 	       ));
  1633 *}
  1634 
  1635 end