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