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