src/Tools/isac/Knowledge/Rational.thy
author Jan Rocnik <jan.rocnik@student.tugraz.at>
Thu, 06 Oct 2011 16:15:29 +0200
branchdecompose-isar
changeset 42300 bd8307d618d2
parent 42211 51c3c007d7fd
child 42301 93083d4e05d8
permissions -rwxr-xr-x
intermed. errors?
neuper@37906
     1
(* rationals, i.e. fractions of multivariate polynomials over the real field
neuper@37906
     2
   author: isac team
neuper@37906
     3
   Copyright (c) isac team 2002
neuper@37906
     4
   Use is subject to license terms.
neuper@37906
     5
neuper@37906
     6
   depends on Poly (and not on Atools), because 
neuper@37906
     7
   fractions with _normalized_ polynomials are canceled, added, etc.
neuper@41978
     8
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
neuper@41978
     9
        10        20        30        40        50        60        70        80         90      100
neuper@37906
    10
*)
neuper@37906
    11
neuper@37950
    12
theory Rational imports Poly begin
neuper@37906
    13
neuper@37906
    14
consts
neuper@37906
    15
jan@42300
    16
  is'_expanded    :: "real => bool" ("_ is'_expanded")     (*RL->Poly.thy*)
jan@42300
    17
  is'_ratpolyexp  :: "real => bool" ("_ is'_ratpolyexp") 
jan@42300
    18
  get_denominator :: "real => real"
neuper@37906
    19
t@42211
    20
axioms(*axiomatization where*) (*.not contained in Isabelle2002,
neuper@37950
    21
          stated as axioms, TODO?: prove as theorems*)
neuper@37950
    22
t@42211
    23
  mult_cross:      "[| b ~= 0; d ~= 0 |] ==> (a / b = c / d) = (a * d = b * c)" (*and*)
t@42211
    24
  mult_cross1:     "   b ~= 0            ==> (a / b = c    ) = (a     = b * c)" (*and*)
t@42211
    25
  mult_cross2:     "           d ~= 0    ==> (a     = c / d) = (a * d =     c)" (*and*)
neuper@37950
    26
                  
t@42211
    27
  add_minus:       "a + b - b = a"(*RL->Poly.thy*) (*and*)
t@42211
    28
  add_minus1:      "a - b + b = a"(*RL->Poly.thy*) (*and*)
neuper@37950
    29
                  
t@42211
    30
  rat_mult:        "a / b * (c / d) = a * c / (b * d)"(*?Isa02*)  (*and*)
t@42211
    31
  rat_mult2:       "a / b *  c      = a * c /  b     "(*?Isa02*) (*and*)
t@42211
    32
t@42211
    33
  rat_mult_poly_l: "c is_polyexp ==> c * (a / b) = c * a /  b" (*and*)
t@42211
    34
  rat_mult_poly_r: "c is_polyexp ==> (a / b) * c = a * c /  b" (*and*)
neuper@37906
    35
neuper@37906
    36
(*real_times_divide1_eq .. Isa02*) 
t@42211
    37
  real_times_divide_1_eq:  "-1    * (c / d) =-1 * c /      d " (*and*)
neuper@37978
    38
  real_times_divide_num:   "a is_const ==> 
t@42211
    39
	          	   a     * (c / d) = a * c /      d " (*and*)
t@42211
    40
t@42211
    41
  real_mult_div_cancel2:   "k ~= 0 ==> m * k / (n * k) = m / n" (*and*)
neuper@37978
    42
(*real_mult_div_cancel1:   "k ~= 0 ==> k * m / (k * n) = m / n"..Isa02*)
neuper@37906
    43
			  
t@42211
    44
  real_divide_divide1:     "y ~= 0 ==> (u / v) / (y / z) = (u / v) * (z / y)" (*and*)
t@42211
    45
  real_divide_divide1_mg:  "y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)" (*and*)
neuper@37978
    46
(*real_divide_divide2_eq:  "x / y / z = x / (y * z)"..Isa02*)
neuper@37906
    47
			  
t@42211
    48
  rat_power:               "(a / b)^^^n = (a^^^n) / (b^^^n)" (*and*)
neuper@37978
    49
neuper@37978
    50
neuper@37978
    51
  rat_add:         "[| a is_const; b is_const; c is_const; d is_const |] ==> 
t@42211
    52
	           a / c + b / d = (a * d + b * c) / (c * d)" (*and*)
neuper@37978
    53
  rat_add_assoc:   "[| a is_const; b is_const; c is_const; d is_const |] ==> 
t@42211
    54
	           a / c +(b / d + e) = (a * d + b * c)/(d * c) + e" (*and*)
neuper@37978
    55
  rat_add1:        "[| a is_const; b is_const; c is_const |] ==> 
t@42211
    56
	           a / c + b / c = (a + b) / c" (*and*)
neuper@37978
    57
  rat_add1_assoc:   "[| a is_const; b is_const; c is_const |] ==> 
t@42211
    58
	           a / c + (b / c + e) = (a + b) / c + e" (*and*)
neuper@37978
    59
  rat_add2:        "[| a is_const; b is_const; c is_const |] ==> 
t@42211
    60
	           a / c + b = (a + b * c) / c" (*and*)
neuper@37978
    61
  rat_add2_assoc:  "[| a is_const; b is_const; c is_const |] ==> 
t@42211
    62
	           a / c + (b + e) = (a + b * c) / c + e" (*and*)
neuper@37978
    63
  rat_add3:        "[| a is_const; b is_const; c is_const |] ==> 
t@42211
    64
	           a + b / c = (a * c + b) / c" (*and*)
neuper@37978
    65
  rat_add3_assoc:   "[| a is_const; b is_const; c is_const |] ==> 
neuper@37950
    66
	           a + (b / c + e) = (a * c + b) / c + e"
neuper@37950
    67
neuper@37950
    68
text {*calculate in rationals: gcd, lcm, etc.
neuper@37950
    69
      (c) Stefan Karnel 2002
neuper@37950
    70
      Institute for Mathematics D and Institute for Software Technology, 
neuper@37950
    71
      TU-Graz SS 2002 *}
neuper@37950
    72
neuper@37950
    73
text {* Remark on notions in the documentation below:
neuper@37950
    74
    referring to the remark on 'polynomials' in Poly.sml we use
neuper@37950
    75
    [2] 'polynomial' normalform (Polynom)
neuper@37950
    76
    [3] 'expanded_term' normalform (Ausmultiplizierter Term),
neuper@37950
    77
    where normalform [2] is a special case of [3], i.e. [3] implies [2].
neuper@37950
    78
    Instead of 
neuper@37950
    79
      'fraction with numerator and nominator both in normalform [2]'
neuper@37950
    80
      'fraction with numerator and nominator both in normalform [3]' 
neuper@37950
    81
    we say: 
neuper@37950
    82
      'fraction in normalform [2]'
neuper@37950
    83
      'fraction in normalform [3]' 
neuper@37950
    84
    or
neuper@37950
    85
      'fraction [2]'
neuper@37950
    86
      'fraction [3]'.
neuper@37950
    87
    a 'simple fraction' is a term with '/' as outmost operator and
neuper@37950
    88
    numerator and nominator in normalform [2] or [3].
neuper@37950
    89
*}
neuper@37950
    90
neuper@37979
    91
ML {* 
neuper@37972
    92
val thy = @{theory};
neuper@37972
    93
neuper@37950
    94
signature RATIONALI =
neuper@37950
    95
sig
neuper@37950
    96
  type mv_monom
neuper@37950
    97
  type mv_poly 
neuper@37950
    98
  val add_fraction_ : theory -> term -> (term * term list) option      
neuper@37950
    99
  val add_fraction_p_ : theory -> term -> (term * term list) option       
neuper@37950
   100
  val calculate_Rational : rls
neuper@37950
   101
  val calc_rat_erls:rls
neuper@37950
   102
  val cancel : rls
neuper@37950
   103
  val cancel_ : theory -> term -> (term * term list) option    
neuper@37950
   104
  val cancel_p : rls   
neuper@37950
   105
  val cancel_p_ : theory -> term -> (term * term list) option
neuper@37950
   106
  val common_nominator : rls              
neuper@37950
   107
  val common_nominator_ : theory -> term -> (term * term list) option
neuper@37950
   108
  val common_nominator_p : rls              
neuper@37950
   109
  val common_nominator_p_ : theory -> term -> (term * term list) option
neuper@37950
   110
  val eval_is_expanded : string -> 'a -> term -> theory -> 
neuper@37950
   111
			 (string * term) option                    
neuper@37950
   112
  val expanded2polynomial : term -> term option
neuper@37950
   113
  val factout_ : theory -> term -> (term * term list) option
neuper@37950
   114
  val factout_p_ : theory -> term -> (term * term list) option
neuper@37950
   115
  val is_expanded : term -> bool
neuper@37950
   116
  val is_polynomial : term -> bool
neuper@37950
   117
neuper@37950
   118
  val mv_gcd : (int * int list) list -> mv_poly -> mv_poly
neuper@37950
   119
  val mv_lcm : mv_poly -> mv_poly -> mv_poly
neuper@37950
   120
neuper@37950
   121
  val norm_expanded_rat_ : theory -> term -> (term * term list) option
neuper@37950
   122
(*WN0602.2.6.pull into struct !!!
neuper@37950
   123
  val norm_Rational : rls(*.normalizes an arbitrary rational term without
neuper@37950
   124
                           roots into a simple and canceled fraction
neuper@37950
   125
                           with normalform [2].*)
neuper@37950
   126
*)
neuper@37950
   127
(*val norm_rational_p : 19.10.02 missing FIXXXXXXXXXXXXME
neuper@37950
   128
      rls               (*.normalizes an rational term [2] without
neuper@37950
   129
                           roots into a simple and canceled fraction
neuper@37950
   130
                           with normalform [2].*)
neuper@37950
   131
*)
neuper@37950
   132
  val norm_rational_ : theory -> term -> (term * term list) option
neuper@37950
   133
  val polynomial2expanded : term -> term option
neuper@37950
   134
  val rational_erls : 
neuper@37950
   135
      rls             (*.evaluates an arbitrary rational term with numerals.*)
neuper@37950
   136
neuper@37950
   137
(*WN0210???SK: fehlen Funktionen, die exportiert werden sollen ? *)
neuper@37906
   138
end
neuper@37950
   139
neuper@37950
   140
(*.**************************************************************************
neuper@37950
   141
survey on the functions
neuper@37950
   142
~~~~~~~~~~~~~~~~~~~~~~~
neuper@37950
   143
 [2] 'polynomial'   :rls               | [3]'expanded_term':rls
neuper@37950
   144
--------------------:------------------+-------------------:-----------------
neuper@37950
   145
 factout_p_         :                  | factout_          :
neuper@37950
   146
 cancel_p_          :                  | cancel_           :
neuper@37950
   147
                    :cancel_p          |                   :cancel
neuper@37950
   148
--------------------:------------------+-------------------:-----------------
neuper@37950
   149
 common_nominator_p_:                  | common_nominator_ :
neuper@37950
   150
                    :common_nominator_p|                   :common_nominator
neuper@37950
   151
 add_fraction_p_    :                  | add_fraction_     :
neuper@37950
   152
--------------------:------------------+-------------------:-----------------
neuper@37950
   153
???SK                 :norm_rational_p   |                   :norm_rational
neuper@37950
   154
neuper@37950
   155
This survey shows only the principal functions for reuse, and the identifiers 
neuper@37950
   156
of the rls exported. The list below shows some more useful functions.
neuper@37950
   157
neuper@37950
   158
neuper@37950
   159
conversion from Isabelle-term to internal representation
neuper@37950
   160
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
neuper@37950
   161
neuper@37950
   162
... BITTE FORTSETZEN ...
neuper@37950
   163
neuper@37950
   164
polynomial2expanded = ...
neuper@37950
   165
expanded2polynomial = ...
neuper@37950
   166
neuper@37950
   167
remark: polynomial2expanded o expanded2polynomial = I, 
neuper@37950
   168
        where 'o' is function chaining, and 'I' is identity WN0210???SK
neuper@37950
   169
neuper@37950
   170
functions for greatest common divisor and canceling
neuper@37950
   171
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
neuper@38036
   172
################################################################################
neuper@38036
   173
##   search Isabelle2009-2/src/HOL/Multivariate_Analysis
neuper@38036
   174
##   Amine Chaieb, Robert Himmelmann, John Harrison.
neuper@38036
   175
################################################################################
neuper@37950
   176
mv_gcd
neuper@37950
   177
factout_
neuper@37950
   178
factout_p_
neuper@37950
   179
cancel_
neuper@37950
   180
cancel_p_
neuper@37950
   181
neuper@37950
   182
functions for least common multiple and addition of fractions
neuper@37950
   183
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
neuper@37950
   184
mv_lcm
neuper@37950
   185
common_nominator_
neuper@37950
   186
common_nominator_p_
neuper@37950
   187
add_fraction_       (*.add 2 or more fractions.*)
neuper@37950
   188
add_fraction_p_     (*.add 2 or more fractions.*)
neuper@37950
   189
neuper@37950
   190
functions for normalform of rationals
neuper@37950
   191
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
neuper@37950
   192
WN0210???SK interne Funktionen f"ur norm_rational: 
neuper@37950
   193
          schaffen diese SML-Funktionen wirklich ganz allgemeine Terme ?
neuper@37950
   194
neuper@37950
   195
norm_rational_
neuper@37950
   196
norm_expanded_rat_
neuper@37950
   197
neuper@37950
   198
**************************************************************************.*)
neuper@37950
   199
neuper@37950
   200
neuper@37950
   201
(*##*)
neuper@37950
   202
structure RationalI : RATIONALI = 
neuper@37950
   203
struct 
neuper@37950
   204
(*##*)
neuper@37950
   205
neuper@37950
   206
infix mem ins union; (*WN100819 updating to Isabelle2009-2*)
neuper@37950
   207
fun x mem [] = false
neuper@37950
   208
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37950
   209
fun (x ins xs) = if x mem xs then xs else x :: xs;
neuper@37950
   210
fun xs union [] = xs
neuper@37950
   211
  | [] union ys = ys
neuper@37950
   212
  | (x :: xs) union ys = xs union (x ins ys);
neuper@37950
   213
neuper@37950
   214
(*. gcd of integers .*)
neuper@37950
   215
(* die gcd Funktion von Isabelle funktioniert nicht richtig !!! *)
neuper@37950
   216
fun gcd_int a b = if b=0 then a
neuper@37950
   217
		  else gcd_int b (a mod b);
neuper@37950
   218
neuper@37950
   219
(*. univariate polynomials (uv) .*)
neuper@37950
   220
(*. univariate polynomials are represented as a list 
neuper@37950
   221
    of the coefficent in reverse maximum degree order .*)
neuper@37950
   222
(*. 5 * x^5 + 4 * x^3 + 2 * x^2 + x + 19 => [19,1,2,4,0,5] .*)
neuper@37950
   223
type uv_poly = int list;
neuper@37950
   224
neuper@37950
   225
(*. adds two uv polynomials .*)
neuper@37950
   226
fun uv_mod_add_poly ([]:uv_poly,p2:uv_poly) = p2:uv_poly 
neuper@37950
   227
  | uv_mod_add_poly (p1,[]) = p1
neuper@37950
   228
  | uv_mod_add_poly (x::p1,y::p2) = (x+y)::(uv_mod_add_poly(p1,p2)); 
neuper@37950
   229
neuper@37950
   230
(*. multiplies a uv polynomial with a skalar s .*)
neuper@37950
   231
fun uv_mod_smul_poly ([]:uv_poly,s:int) = []:uv_poly 
neuper@37950
   232
  | uv_mod_smul_poly (x::p,s) = (x*s)::(uv_mod_smul_poly(p,s)); 
neuper@37950
   233
neuper@37950
   234
(*. calculates the remainder of a polynomial divided by a skalar s .*)
neuper@37950
   235
fun uv_mod_rem_poly ([]:uv_poly,s) = []:uv_poly 
neuper@37950
   236
  | uv_mod_rem_poly (x::p,s) = (x mod s)::(uv_mod_smul_poly(p,s)); 
neuper@37950
   237
neuper@37950
   238
(*. calculates the degree of a uv polynomial .*)
neuper@37950
   239
fun uv_mod_deg ([]:uv_poly) = 0  
neuper@37950
   240
  | uv_mod_deg p = length(p)-1;
neuper@37950
   241
neuper@37950
   242
(*. calculates the remainder of x/p and represents it as 
neuper@37950
   243
    value between -p/2 and p/2 .*)
neuper@37950
   244
fun uv_mod_mod2(x,p)=
neuper@37950
   245
    let
neuper@37950
   246
	val y=(x mod p);
neuper@37950
   247
    in
neuper@37950
   248
	if (y)>(p div 2) then (y)-p else 
neuper@37950
   249
	    (
neuper@37950
   250
	     if (y)<(~p div 2) then p+(y) else (y)
neuper@37950
   251
	     )
neuper@37950
   252
    end;
neuper@37950
   253
neuper@37950
   254
(*.calculates the remainder for each element of a integer list divided by p.*)  
neuper@37950
   255
fun uv_mod_list_modp [] p = [] 
neuper@37950
   256
  | uv_mod_list_modp (x::xs) p = (uv_mod_mod2(x,p))::(uv_mod_list_modp xs p);
neuper@37950
   257
neuper@37950
   258
(*. appends an integer at the end of a integer list .*)
neuper@37950
   259
fun uv_mod_null (p1:int list,0) = p1 
neuper@37950
   260
  | uv_mod_null (p1:int list,n1:int) = uv_mod_null(p1,n1-1) @ [0];
neuper@37950
   261
neuper@37950
   262
(*. uv polynomial division, result is (quotient, remainder) .*)
neuper@37950
   263
(*. only for uv_mod_divides .*)
neuper@37950
   264
(* FIXME: Division von x^9+x^5+1 durch x-1000 funktioniert nicht,
neuper@37950
   265
   integer zu klein  *)
neuper@37950
   266
fun uv_mod_pdiv (p1:uv_poly) ([]:uv_poly) = 
neuper@38031
   267
    error ("RATIONALS_UV_MOD_PDIV_EXCEPTION: division by zero")
neuper@37950
   268
  | uv_mod_pdiv p1 [x] = 
neuper@37950
   269
    let
neuper@38006
   270
	val xs= Unsynchronized.ref  [];
neuper@37950
   271
    in
neuper@37950
   272
	if x<>0 then 
neuper@37950
   273
	    (
neuper@37950
   274
	     xs:=(uv_mod_rem_poly(p1,x));
neuper@37950
   275
	     while length(!xs)>0 andalso hd(!xs)=0 do xs:=tl(!xs)
neuper@37950
   276
	     )
neuper@38031
   277
	else error ("RATIONALS_UV_MOD_PDIV_EXCEPTION: division by zero");
neuper@37950
   278
	([]:uv_poly,!xs:uv_poly)
neuper@37950
   279
    end
neuper@37950
   280
  | uv_mod_pdiv p1 p2 =  
neuper@37950
   281
    let
neuper@37950
   282
	val n= uv_mod_deg(p2);
neuper@38006
   283
	val m= Unsynchronized.ref (uv_mod_deg(p1));
neuper@38006
   284
	val p1'= Unsynchronized.ref  (rev(p1));
neuper@37950
   285
	val p2'=(rev(p2));
neuper@37950
   286
	val lc2=hd(p2');
neuper@38006
   287
	val q= Unsynchronized.ref  [];
neuper@38006
   288
	val c= Unsynchronized.ref  0;
neuper@38006
   289
	val output= Unsynchronized.ref  ([],[]);
neuper@37950
   290
    in
neuper@37950
   291
	(
neuper@37950
   292
	 if (!m)=0 orelse p2=[0] 
neuper@38031
   293
         then error ("RATIONALS_UV_MOD_PDIV_EXCEPTION: Division by zero") 
neuper@37950
   294
	 else
neuper@37950
   295
	     (
neuper@37950
   296
	      if (!m)<n then 
neuper@37950
   297
		  (
neuper@37950
   298
		   output:=([0],p1) 
neuper@37950
   299
		   ) 
neuper@37950
   300
	      else
neuper@37950
   301
		  (
neuper@37950
   302
		   while (!m)>=n do
neuper@37950
   303
		       (
neuper@37950
   304
			c:=hd(!p1') div hd(p2');
neuper@37950
   305
			if !c<>0 then
neuper@37950
   306
			    (
neuper@37950
   307
			     p1':=uv_mod_add_poly(!p1',uv_mod_null(uv_mod_smul_poly(p2',~(!c)),!m-n));
neuper@37950
   308
			     while length(!p1')>0 andalso hd(!p1')=0  do p1':= tl(!p1');
neuper@37950
   309
			     m:=uv_mod_deg(!p1')
neuper@37950
   310
			     )
neuper@37950
   311
			else m:=0
neuper@37950
   312
			);
neuper@37950
   313
    		   output:=(rev(!q),rev(!p1'))
neuper@37950
   314
		   )
neuper@37950
   315
	      );
neuper@37950
   316
	     !output
neuper@37950
   317
	 )
neuper@37950
   318
    end;
neuper@37950
   319
neuper@37950
   320
(*. divides p1 by p2 in Zp .*)
neuper@37950
   321
fun uv_mod_pdivp (p1:uv_poly) (p2:uv_poly) p =  
neuper@37950
   322
    let
neuper@37950
   323
	val n=uv_mod_deg(p2);
neuper@38006
   324
	val m= Unsynchronized.ref  (uv_mod_deg(uv_mod_list_modp p1 p));
neuper@38006
   325
	val p1'= Unsynchronized.ref  (rev(p1));
neuper@37950
   326
	val p2'=(rev(uv_mod_list_modp p2 p));
neuper@37950
   327
	val lc2=hd(p2');
neuper@38006
   328
	val q= Unsynchronized.ref  [];
neuper@38006
   329
	val c= Unsynchronized.ref  0;
neuper@38006
   330
	val output= Unsynchronized.ref  ([],[]);
neuper@37950
   331
    in
neuper@37950
   332
	(
neuper@38031
   333
	 if (!m)=0 orelse p2=[0] then error ("RATIONALS_UV_MOD_PDIVP_EXCEPTION: Division by zero") 
neuper@37950
   334
	 else
neuper@37950
   335
	     (
neuper@37950
   336
	      if (!m)<n then 
neuper@37950
   337
		  (
neuper@37950
   338
		   output:=([0],p1) 
neuper@37950
   339
		   ) 
neuper@37950
   340
	      else
neuper@37950
   341
		  (
neuper@37950
   342
		   while (!m)>=n do
neuper@37950
   343
		       (
neuper@37950
   344
			c:=uv_mod_mod2(hd(!p1')*(power lc2 1), p);
neuper@37950
   345
			q:=(!c)::(!q);
neuper@37950
   346
			p1':=uv_mod_list_modp(tl(uv_mod_add_poly(uv_mod_smul_poly(!p1',lc2),
neuper@37950
   347
								  uv_mod_smul_poly(uv_mod_smul_poly(p2',hd(!p1')),~1)))) p;
neuper@37950
   348
			m:=(!m)-1
neuper@37950
   349
			);
neuper@37950
   350
		   
neuper@37950
   351
		   while !p1'<>[] andalso hd(!p1')=0 do
neuper@37950
   352
		       (
neuper@37950
   353
			p1':=tl(!p1')
neuper@37950
   354
			);
neuper@37950
   355
neuper@37950
   356
    		   output:=(rev(uv_mod_list_modp (!q) (p)),rev(!p1'))
neuper@37950
   357
		   )
neuper@37950
   358
	      );
neuper@37950
   359
	     !output:uv_poly * uv_poly
neuper@37950
   360
	 )
neuper@37950
   361
    end;
neuper@37950
   362
neuper@37950
   363
(*. calculates the remainder of p1/p2 .*)
neuper@38031
   364
fun uv_mod_prest (p1:uv_poly) ([]:uv_poly) = error("UV_MOD_PREST_EXCEPTION: Division by zero") 
neuper@37950
   365
  | uv_mod_prest [] p2 = []:uv_poly
neuper@37950
   366
  | uv_mod_prest p1 p2 = (#2(uv_mod_pdiv p1 p2));
neuper@37950
   367
neuper@37950
   368
(*. calculates the remainder of p1/p2 in Zp .*)
neuper@38031
   369
fun uv_mod_prestp (p1:uv_poly) ([]:uv_poly) p= error("UV_MOD_PRESTP_EXCEPTION: Division by zero") 
neuper@37950
   370
  | uv_mod_prestp [] p2 p= []:uv_poly 
neuper@37950
   371
  | uv_mod_prestp p1 p2 p = #2(uv_mod_pdivp p1 p2 p); 
neuper@37950
   372
neuper@37950
   373
(*. calculates the content of a uv polynomial .*)
neuper@37950
   374
fun uv_mod_cont ([]:uv_poly) = 0  
neuper@37950
   375
  | uv_mod_cont (x::p)= gcd_int x (uv_mod_cont(p));
neuper@37950
   376
neuper@37950
   377
(*. divides each coefficient of a uv polynomial by y .*)
neuper@38031
   378
fun uv_mod_div_list (p:uv_poly,0) = error("UV_MOD_DIV_LIST_EXCEPTION: Division by zero") 
neuper@37950
   379
  | uv_mod_div_list ([],y)   = []:uv_poly
neuper@37950
   380
  | uv_mod_div_list (x::p,y) = (x div y)::uv_mod_div_list(p,y); 
neuper@37950
   381
neuper@37950
   382
(*. calculates the primitiv part of a uv polynomial .*)
neuper@37950
   383
fun uv_mod_pp ([]:uv_poly) = []:uv_poly
neuper@37950
   384
  | uv_mod_pp p =  
neuper@37950
   385
    let
neuper@38006
   386
	val c= Unsynchronized.ref  0;
neuper@37950
   387
    in
neuper@37950
   388
	(
neuper@37950
   389
	 c:=uv_mod_cont(p);
neuper@37950
   390
	 
neuper@38031
   391
	 if !c=0 then error ("RATIONALS_UV_MOD_PP_EXCEPTION: content is 0")
neuper@37950
   392
	 else uv_mod_div_list(p,!c)
neuper@37950
   393
	)
neuper@37950
   394
    end;
neuper@37950
   395
neuper@37950
   396
(*. gets the leading coefficient of a uv polynomial .*)
neuper@37950
   397
fun uv_mod_lc ([]:uv_poly) = 0 
neuper@37950
   398
  | uv_mod_lc p  = hd(rev(p)); 
neuper@37950
   399
neuper@37950
   400
(*. calculates the euklidean polynomial remainder sequence in Zp .*)
neuper@37950
   401
fun uv_mod_prs_euklid_p(p1:uv_poly,p2:uv_poly,p)= 
neuper@37950
   402
    let
neuper@38006
   403
	val f = Unsynchronized.ref  [];
neuper@38006
   404
	val f'= Unsynchronized.ref  p2;
neuper@38006
   405
	val fi= Unsynchronized.ref  [];
neuper@37950
   406
    in
neuper@37950
   407
	( 
neuper@37950
   408
	 f:=p2::p1::[]; 
neuper@37950
   409
 	 while uv_mod_deg(!f')>0 do
neuper@37950
   410
	     (
neuper@37950
   411
	      f':=uv_mod_prestp (hd(tl(!f))) (hd(!f)) p;
neuper@37950
   412
	      if (!f')<>[] then 
neuper@37950
   413
		  (
neuper@37950
   414
		   fi:=(!f');
neuper@37950
   415
		   f:=(!fi)::(!f)
neuper@37950
   416
		   )
neuper@37950
   417
	      else ()
neuper@37950
   418
	      );
neuper@37950
   419
	      (!f)
neuper@37950
   420
	 
neuper@37950
   421
	 )
neuper@37950
   422
    end;
neuper@37950
   423
neuper@37950
   424
(*. calculates the gcd of p1 and p2 in Zp .*)
neuper@37950
   425
fun uv_mod_gcd_modp ([]:uv_poly) (p2:uv_poly) p = p2:uv_poly 
neuper@37950
   426
  | uv_mod_gcd_modp p1 [] p= p1
neuper@37950
   427
  | uv_mod_gcd_modp p1 p2 p=
neuper@37950
   428
    let
neuper@38006
   429
	val p1'= Unsynchronized.ref [];
neuper@38006
   430
	val p2'= Unsynchronized.ref [];
neuper@38006
   431
	val pc= Unsynchronized.ref [];
neuper@38006
   432
	val g= Unsynchronized.ref  [];
neuper@38006
   433
	val d= Unsynchronized.ref  0;
neuper@38006
   434
	val prs= Unsynchronized.ref  [];
neuper@37950
   435
    in
neuper@37950
   436
	(
neuper@37950
   437
	 if uv_mod_deg(p1)>=uv_mod_deg(p2) then
neuper@37950
   438
	     (
neuper@37950
   439
	      p1':=uv_mod_list_modp (uv_mod_pp(p1)) p;
neuper@37950
   440
	      p2':=uv_mod_list_modp (uv_mod_pp(p2)) p
neuper@37950
   441
	      )
neuper@37950
   442
	 else 
neuper@37950
   443
	     (
neuper@37950
   444
	      p1':=uv_mod_list_modp (uv_mod_pp(p2)) p;
neuper@37950
   445
	      p2':=uv_mod_list_modp (uv_mod_pp(p1)) p
neuper@37950
   446
	      );
neuper@37950
   447
	 d:=uv_mod_mod2((gcd_int (uv_mod_cont(p1))) (uv_mod_cont(p2)), p) ;
neuper@37950
   448
	 if !d>(p div 2) then d:=(!d)-p else ();
neuper@37950
   449
	 
neuper@37950
   450
	 prs:=uv_mod_prs_euklid_p(!p1',!p2',p);
neuper@37950
   451
neuper@37950
   452
	 if hd(!prs)=[] then pc:=hd(tl(!prs))
neuper@37950
   453
	 else pc:=hd(!prs);
neuper@37950
   454
neuper@37950
   455
	 g:=uv_mod_smul_poly(uv_mod_pp(!pc),!d);
neuper@37950
   456
	 !g
neuper@37950
   457
	 )
neuper@37950
   458
    end;
neuper@37950
   459
neuper@37950
   460
(*. calculates the minimum of two real values x and y .*)
neuper@37978
   461
fun uv_mod_r_min(x,y):Real.real = if x>y then y else x;
neuper@37950
   462
neuper@37950
   463
(*. calculates the minimum of two integer values x and y .*)
neuper@37950
   464
fun uv_mod_min(x,y) = if x>y then y else x;
neuper@37950
   465
neuper@37950
   466
(*. adds the squared values of a integer list .*)
neuper@37950
   467
fun uv_mod_add_qu [] = 0.0 
neuper@37978
   468
  | uv_mod_add_qu (x::p) =  Real.fromInt(x)*Real.fromInt(x) + uv_mod_add_qu p;
neuper@37950
   469
neuper@37950
   470
(*. calculates the euklidean norm .*)
neuper@37950
   471
fun uv_mod_norm ([]:uv_poly) = 0.0
neuper@37950
   472
  | uv_mod_norm p = Math.sqrt(uv_mod_add_qu(p));
neuper@37950
   473
neuper@37950
   474
(*. multipies two values a and b .*)
neuper@37950
   475
fun uv_mod_multi a b = a * b;
neuper@37950
   476
neuper@37950
   477
(*. decides if x is a prim, the list contains all primes which are lower then x .*)
neuper@37950
   478
fun uv_mod_prim(x,[])= false 
neuper@37950
   479
  | uv_mod_prim(x,[y])=if ((x mod y) <> 0) then true
neuper@37950
   480
		else false
neuper@37950
   481
  | uv_mod_prim(x,y::ys) = if uv_mod_prim(x,[y])
neuper@37950
   482
			then 
neuper@37950
   483
			    if uv_mod_prim(x,ys) then true 
neuper@37950
   484
			    else false
neuper@37950
   485
		    else false;
neuper@37950
   486
neuper@37950
   487
(*. gets the first prime, which is greater than p and does not divide g .*)
neuper@37950
   488
fun uv_mod_nextprime(g,p)= 
neuper@37950
   489
    let
neuper@38006
   490
	val list= Unsynchronized.ref  [2];
neuper@38006
   491
	val exit= Unsynchronized.ref  0;
neuper@38006
   492
	val i = Unsynchronized.ref 2
neuper@37950
   493
    in
neuper@37950
   494
	while (!i<p) do (* calculates the primes lower then p *)
neuper@37950
   495
	    (
neuper@37950
   496
	     if uv_mod_prim(!i,!list) then
neuper@37950
   497
		 (
neuper@37950
   498
		  if (p mod !i <> 0)
neuper@37950
   499
		      then
neuper@37950
   500
			  (
neuper@37950
   501
			   list:= (!i)::(!list);
neuper@37950
   502
			   i:= (!i)+1
neuper@37950
   503
			   )
neuper@37950
   504
		  else i:=(!i)+1
neuper@37950
   505
		  )
neuper@37950
   506
	     else i:= (!i)+1
neuper@37950
   507
		 );
neuper@37950
   508
	    i:=(p+1);
neuper@37950
   509
	    while (!exit=0) do   (* calculate next prime which does not divide g *)
neuper@37950
   510
	    (
neuper@37950
   511
	     if uv_mod_prim(!i,!list) then
neuper@37950
   512
		 (
neuper@37950
   513
		  if (g mod !i <> 0)
neuper@37950
   514
		      then
neuper@37950
   515
			  (
neuper@37950
   516
			   list:= (!i)::(!list);
neuper@37950
   517
			   exit:= (!i)
neuper@37950
   518
			   )
neuper@37950
   519
		  else i:=(!i)+1
neuper@37950
   520
		      )
neuper@37950
   521
	     else i:= (!i)+1
neuper@37950
   522
		 ); 
neuper@37950
   523
	    !exit
neuper@37950
   524
    end;
neuper@37950
   525
neuper@37950
   526
(*. decides if p1 is a factor of p2 in Zp .*)
neuper@38031
   527
fun uv_mod_dividesp ([]:uv_poly) (p2:uv_poly) p= error("UV_MOD_DIVIDESP: Division by zero") 
neuper@37950
   528
  | uv_mod_dividesp p1 p2 p= if uv_mod_prestp p2 p1 p = [] then true else false;
neuper@37950
   529
neuper@37950
   530
(*. decides if p1 is a factor of p2 .*)
neuper@38031
   531
fun uv_mod_divides ([]:uv_poly) (p2:uv_poly) = error("UV_MOD_DIVIDES: Division by zero")
neuper@37950
   532
  | uv_mod_divides p1 p2 = if uv_mod_prest p2 p1  = [] then true else false;
neuper@37950
   533
neuper@37950
   534
(*. chinese remainder algorithm .*)
neuper@37950
   535
fun uv_mod_cra2(r1,r2,m1,m2)=     
neuper@37950
   536
    let 
neuper@38006
   537
	val c= Unsynchronized.ref  0;
neuper@38006
   538
	val r1'= Unsynchronized.ref  0;
neuper@38006
   539
	val d= Unsynchronized.ref  0;
neuper@38006
   540
	val a= Unsynchronized.ref  0;
neuper@37950
   541
    in
neuper@37950
   542
	(
neuper@37950
   543
	 while (uv_mod_mod2((!c)*m1,m2))<>1 do 
neuper@37950
   544
	     (
neuper@37950
   545
	      c:=(!c)+1
neuper@37950
   546
	      );
neuper@37950
   547
	 r1':= uv_mod_mod2(r1,m1);
neuper@37950
   548
	 d:=uv_mod_mod2(((r2-(!r1'))*(!c)),m2);
neuper@37950
   549
	 !r1'+(!d)*m1    
neuper@37950
   550
	 )
neuper@37950
   551
    end;
neuper@37950
   552
neuper@37950
   553
(*. applies the chinese remainder algorithmen to the coefficients of x1 and x2 .*)
neuper@37950
   554
fun uv_mod_cra_2 ([],[],m1,m2) = [] 
neuper@38031
   555
  | uv_mod_cra_2 ([],x2,m1,m2) = error("UV_MOD_CRA_2_EXCEPTION: invalid call x1")
neuper@38031
   556
  | uv_mod_cra_2 (x1,[],m1,m2) = error("UV_MOD_CRA_2_EXCEPTION: invalid call x2")
neuper@37950
   557
  | uv_mod_cra_2 (x1::x1s,x2::x2s,m1,m2) = (uv_mod_cra2(x1,x2,m1,m2))::(uv_mod_cra_2(x1s,x2s,m1,m2));
neuper@37950
   558
neuper@37950
   559
(*. calculates the gcd of two uv polynomials p1' and p2' with the modular algorithm .*)
neuper@37950
   560
fun uv_mod_gcd (p1':uv_poly) (p2':uv_poly) =
neuper@37950
   561
    let 
neuper@38006
   562
	val p1= Unsynchronized.ref  (uv_mod_pp(p1'));
neuper@38006
   563
	val p2= Unsynchronized.ref  (uv_mod_pp(p2'));
neuper@37950
   564
	val c=gcd_int (uv_mod_cont(p1')) (uv_mod_cont(p2'));
neuper@38006
   565
	val temp= Unsynchronized.ref  [];
neuper@38006
   566
	val cp= Unsynchronized.ref  [];
neuper@38006
   567
	val qp= Unsynchronized.ref  [];
neuper@38006
   568
	val q= Unsynchronized.ref [];
neuper@38006
   569
	val pn= Unsynchronized.ref  0;
neuper@38006
   570
	val d= Unsynchronized.ref  0;
neuper@38006
   571
	val g1= Unsynchronized.ref  0;
neuper@38006
   572
	val p= Unsynchronized.ref  0;    
neuper@38006
   573
	val m= Unsynchronized.ref  0;
neuper@38006
   574
	val exit= Unsynchronized.ref  0;
neuper@38006
   575
	val i= Unsynchronized.ref  1;
neuper@37950
   576
    in
neuper@37950
   577
	if length(!p1)>length(!p2) then ()
neuper@37950
   578
	else 
neuper@37950
   579
	    (
neuper@37950
   580
	     temp:= !p1;
neuper@37950
   581
	     p1:= !p2;
neuper@37950
   582
	     p2:= !temp
neuper@37950
   583
	     );
neuper@37950
   584
neuper@37950
   585
	 
neuper@37950
   586
	d:=gcd_int (uv_mod_lc(!p1)) (uv_mod_lc(!p2));
neuper@37950
   587
	g1:=uv_mod_lc(!p1)*uv_mod_lc(!p2);
neuper@37950
   588
	p:=4;
neuper@37950
   589
	
neuper@37978
   590
	m:=Real.ceil(2.0 * Real.fromInt(!d) *
neuper@37978
   591
	  Real.fromInt(power 2 (uv_mod_min(uv_mod_deg(!p2),uv_mod_deg(!p1)))) *
neuper@37978
   592
	  Real.fromInt(!d) * 
neuper@37978
   593
	  uv_mod_r_min(uv_mod_norm(!p1) / Real.fromInt(abs(uv_mod_lc(!p1))),
neuper@37978
   594
	  uv_mod_norm(!p2) / Real.fromInt(abs(uv_mod_lc(!p2))))); 
neuper@37950
   595
neuper@37950
   596
	while (!exit=0) do  
neuper@37950
   597
	    (
neuper@37950
   598
	     p:=uv_mod_nextprime(!d,!p);
neuper@37950
   599
	     cp:=(uv_mod_gcd_modp (uv_mod_list_modp(!p1) (!p)) (uv_mod_list_modp(!p2) (!p)) (!p)) ;
neuper@37950
   600
	     if abs(uv_mod_lc(!cp))<>1 then  (* leading coefficient = 1 ? *)
neuper@37950
   601
		 (
neuper@37950
   602
		  i:=1;
neuper@37950
   603
		  while (!i)<(!p) andalso (abs(uv_mod_mod2((uv_mod_lc(!cp)*(!i)),(!p)))<>1) do
neuper@37950
   604
		      (
neuper@37950
   605
		       i:=(!i)+1
neuper@37950
   606
		       );
neuper@37950
   607
		      cp:=uv_mod_list_modp (map (uv_mod_multi (!i)) (!cp)) (!p) 
neuper@37950
   608
		  )
neuper@37950
   609
	     else ();
neuper@37950
   610
neuper@37950
   611
	     qp:= ((map (uv_mod_multi (uv_mod_mod2(!d,!p)))) (!cp));
neuper@37950
   612
neuper@37950
   613
	     if uv_mod_deg(!qp)=0 then (q:=[1]; exit:=1) else ();
neuper@37950
   614
neuper@37950
   615
	     pn:=(!p);
neuper@37950
   616
	     q:=(!qp);
neuper@37950
   617
neuper@37950
   618
	     while !pn<= !m andalso !m>(!p) andalso !exit=0 do
neuper@37950
   619
		 (
neuper@37950
   620
		  p:=uv_mod_nextprime(!d,!p);
neuper@37950
   621
 		  cp:=(uv_mod_gcd_modp (uv_mod_list_modp(!p1) (!p)) (uv_mod_list_modp(!p2) (!p)) (!p)); 
neuper@37950
   622
 		  if uv_mod_lc(!cp)<>1 then  (* leading coefficient = 1 ? *)
neuper@37950
   623
 		      (
neuper@37950
   624
 		       i:=1;
neuper@37950
   625
 		       while (!i)<(!p) andalso ((uv_mod_mod2((uv_mod_lc(!q)*(!i)),(!p)))<>1) do
neuper@37950
   626
 			   (
neuper@37950
   627
 			    i:=(!i)+1
neuper@37950
   628
		           );
neuper@37950
   629
		       cp:=uv_mod_list_modp (map (uv_mod_multi (!i)) (!cp)) (!p)
neuper@37950
   630
 		      )
neuper@37950
   631
 		  else ();    
neuper@37950
   632
 		 
neuper@37950
   633
		  qp:=uv_mod_list_modp ((map (uv_mod_multi (uv_mod_mod2(!d,!p)))) (!cp)  ) (!p);
neuper@37950
   634
 		  if uv_mod_deg(!qp)>uv_mod_deg(!q) then
neuper@37950
   635
 		      (
neuper@37950
   636
 		       (*print("degree to high!!!\n")*)
neuper@37950
   637
 		       )
neuper@37950
   638
 		  else
neuper@37950
   639
 		      (
neuper@37950
   640
 		       if uv_mod_deg(!qp)=uv_mod_deg(!q) then
neuper@37950
   641
 			   (
neuper@37950
   642
 			    q:=uv_mod_cra_2(!q,!qp,!pn,!p);
neuper@37950
   643
			    pn:=(!pn) * !p;
neuper@37950
   644
			    q:=uv_mod_pp(uv_mod_list_modp (!q) (!pn)); (* found already gcd ? *)
neuper@37950
   645
			    if (uv_mod_divides (!q) (p1')) andalso (uv_mod_divides (!q) (p2')) then (exit:=1) else ()
neuper@37950
   646
		 	    )
neuper@37950
   647
		       else
neuper@37950
   648
			   (
neuper@37950
   649
			    if  uv_mod_deg(!qp)<uv_mod_deg(!q) then
neuper@37950
   650
				(
neuper@37950
   651
				 pn:= !p;
neuper@37950
   652
				 q:= !qp
neuper@37950
   653
				 )
neuper@37950
   654
			    else ()
neuper@37950
   655
			    )
neuper@37950
   656
		       )
neuper@37950
   657
		  );
neuper@37950
   658
 	     q:=uv_mod_pp(uv_mod_list_modp (!q) (!pn));
neuper@37950
   659
	     if (uv_mod_divides (!q) (p1')) andalso (uv_mod_divides (!q) (p2')) then exit:=1 else ()
neuper@37950
   660
	     );
neuper@37950
   661
	    uv_mod_smul_poly(!q,c):uv_poly
neuper@37950
   662
    end;
neuper@37950
   663
neuper@37950
   664
(*. multivariate polynomials .*)
neuper@37950
   665
(*. multivariate polynomials are represented as a list of the pairs, 
neuper@37950
   666
 first is the coefficent and the second is a list of the exponents .*)
neuper@37950
   667
(*. 5 * x^5 * y^3 + 4 * x^3 * z^2 + 2 * x^2 * y * z^3 - z - 19 
neuper@37950
   668
 => [(5,[5,3,0]),(4,[3,0,2]),(2,[2,1,3]),(~1,[0,0,1]),(~19,[0,0,0])] .*)
neuper@37950
   669
neuper@37950
   670
(*. global variables .*)
neuper@37950
   671
(*. order indicators .*)
neuper@37950
   672
val LEX_=0; (* lexicographical term order *)
neuper@37950
   673
val GGO_=1; (* greatest degree order *)
neuper@37950
   674
neuper@37950
   675
(*. datatypes for internal representation.*)
neuper@37950
   676
type mv_monom = (int *      (*.coefficient or the monom.*)
neuper@37950
   677
		 int list); (*.list of exponents)      .*)
neuper@37950
   678
fun mv_monom2str (i, is) = "("^ int2str i^"," ^ ints2str' is ^ ")";
neuper@37950
   679
neuper@37950
   680
type mv_poly = mv_monom list; 
neuper@37950
   681
fun mv_poly2str p = (strs2str' o (map mv_monom2str)) p;
neuper@37950
   682
neuper@37950
   683
(*. help function for monom_greater and geq .*)
neuper@37950
   684
fun mv_mg_hlp([]) = EQUAL 
neuper@37950
   685
  | mv_mg_hlp(x::list)=if x<0 then LESS
neuper@37950
   686
		    else if x>0 then GREATER
neuper@37950
   687
			 else mv_mg_hlp(list);
neuper@37950
   688
neuper@37950
   689
(*. adds a list of values .*)
neuper@37950
   690
fun mv_addlist([]) = 0
neuper@37950
   691
  | mv_addlist(p1) = hd(p1)+mv_addlist(tl(p1));
neuper@37950
   692
			   
neuper@37950
   693
(*. tests if the monomial M1 is greater as the monomial M2 and returns a boolean value .*)
neuper@37950
   694
(*. 2 orders are implemented LEX_/GGO_ (lexigraphical/greatest degree order) .*)
neuper@37950
   695
fun mv_monom_greater((M1x,M1l):mv_monom,(M2x,M2l):mv_monom,order)=
neuper@37950
   696
    if order=LEX_ then
neuper@37950
   697
	( 
neuper@38031
   698
	 if length(M1l)<>length(M2l) then error ("RATIONALS_MV_MONOM_GREATER_EXCEPTION: Order error")
neuper@37950
   699
	 else if (mv_mg_hlp((map op- (M1l~~M2l)))<>GREATER) then false else true
neuper@37950
   700
	     )
neuper@37950
   701
    else
neuper@37950
   702
	if order=GGO_ then
neuper@37950
   703
	    ( 
neuper@38031
   704
	     if length(M1l)<>length(M2l) then error ("RATIONALS_MV_MONOM_GREATER_EXCEPTION: Order error")
neuper@37950
   705
	     else 
neuper@37950
   706
		 if mv_addlist(M1l)=mv_addlist(M2l)  then if (mv_mg_hlp((map op- (M1l~~M2l)))<>GREATER) then false else true
neuper@37950
   707
		 else if mv_addlist(M1l)>mv_addlist(M2l) then true else false
neuper@37950
   708
	     )
neuper@38031
   709
	else error ("RATIONALS_MV_MONOM_GREATER_EXCEPTION: Wrong Order");
neuper@37950
   710
		   
neuper@37950
   711
(*. tests if the monomial X is greater as the monomial Y and returns a order value (GREATER,EQUAL,LESS) .*)
neuper@37950
   712
(*. 2 orders are implemented LEX_/GGO_ (lexigraphical/greatest degree order) .*)
neuper@37950
   713
fun mv_geq order ((x1,x):mv_monom,(x2,y):mv_monom) =
neuper@37950
   714
let 
neuper@38006
   715
    val temp= Unsynchronized.ref  EQUAL;
neuper@37950
   716
in
neuper@37950
   717
    if order=LEX_ then
neuper@37950
   718
	(
neuper@37950
   719
	 if length(x)<>length(y) then 
neuper@38031
   720
	     error ("RATIONALS_MV_GEQ_EXCEPTION: Order error")
neuper@37950
   721
	 else 
neuper@37950
   722
	     (
neuper@37950
   723
	      temp:=mv_mg_hlp((map op- (x~~y)));
neuper@37950
   724
	      if !temp=EQUAL then 
neuper@37950
   725
		  ( if x1=x2 then EQUAL 
neuper@37950
   726
		    else if x1>x2 then GREATER
neuper@37950
   727
			 else LESS
neuper@37950
   728
			     )
neuper@37950
   729
	      else (!temp)
neuper@37950
   730
	      )
neuper@37950
   731
	     )
neuper@37950
   732
    else 
neuper@37950
   733
	if order=GGO_ then 
neuper@37950
   734
	    (
neuper@37950
   735
	     if length(x)<>length(y) then 
neuper@38031
   736
	      error ("RATIONALS_MV_GEQ_EXCEPTION: Order error")
neuper@37950
   737
	     else 
neuper@37950
   738
		 if mv_addlist(x)=mv_addlist(y) then 
neuper@37950
   739
		     (mv_mg_hlp((map op- (x~~y))))
neuper@37950
   740
		 else if mv_addlist(x)>mv_addlist(y) then GREATER else LESS
neuper@37950
   741
		     )
neuper@38031
   742
	else error ("RATIONALS_MV_GEQ_EXCEPTION: Wrong Order")
neuper@37950
   743
end;
neuper@37950
   744
neuper@37950
   745
(*. cuts the first variable from a polynomial .*)
neuper@37950
   746
fun mv_cut([]:mv_poly)=[]:mv_poly
neuper@38031
   747
  | mv_cut((x,[])::list) = error ("RATIONALS_MV_CUT_EXCEPTION: Invalid list ")
neuper@37950
   748
  | mv_cut((x,y::ys)::list)=(x,ys)::mv_cut(list);
neuper@37950
   749
	    
neuper@37950
   750
(*. leading power product .*)
neuper@37950
   751
fun mv_lpp([]:mv_poly,order)  = []
neuper@37950
   752
  | mv_lpp([(x,y)],order) = y
neuper@37950
   753
  | mv_lpp(p1,order)  = #2(hd(rev(sort (mv_geq order) p1)));
neuper@37950
   754
    
neuper@37950
   755
(*. leading monomial .*)
neuper@37950
   756
fun mv_lm([]:mv_poly,order)  = (0,[]):mv_monom
neuper@37950
   757
  | mv_lm([x],order) = x 
neuper@37950
   758
  | mv_lm(p1,order)  = hd(rev(sort (mv_geq order) p1));
neuper@37950
   759
    
neuper@37950
   760
(*. leading coefficient in term order .*)
neuper@37950
   761
fun mv_lc2([]:mv_poly,order)  = 0
neuper@37950
   762
  | mv_lc2([(x,y)],order) = x
neuper@37950
   763
  | mv_lc2(p1,order)  = #1(hd(rev(sort (mv_geq order) p1)));
neuper@37950
   764
neuper@37950
   765
neuper@37950
   766
(*. reverse the coefficients in mv polynomial .*)
neuper@37950
   767
fun mv_rev_to([]:mv_poly) = []:mv_poly
neuper@37950
   768
  | mv_rev_to((c,e)::xs) = (c,rev(e))::mv_rev_to(xs);
neuper@37950
   769
neuper@37950
   770
(*. leading coefficient in reverse term order .*)
neuper@37950
   771
fun mv_lc([]:mv_poly,order)  = []:mv_poly 
neuper@37950
   772
  | mv_lc([(x,y)],order) = mv_rev_to(mv_cut(mv_rev_to([(x,y)])))
neuper@37950
   773
  | mv_lc(p1,order)  = 
neuper@37950
   774
    let
neuper@38006
   775
	val p1o= Unsynchronized.ref  (rev(sort (mv_geq order) (mv_rev_to(p1))));
neuper@37950
   776
	val lp=hd(#2(hd(!p1o)));
neuper@38006
   777
	val lc= Unsynchronized.ref  [];
neuper@37950
   778
    in
neuper@37950
   779
	(
neuper@37950
   780
	 while (length(!p1o)>0 andalso hd(#2(hd(!p1o)))=lp) do
neuper@37950
   781
	     (
neuper@37950
   782
	      lc:=hd(mv_cut([hd(!p1o)]))::(!lc);
neuper@37950
   783
	      p1o:=tl(!p1o)
neuper@37950
   784
	      );
neuper@38031
   785
	 if !lc=[] then error ("RATIONALS_MV_LC_EXCEPTION: lc is empty") else ();
neuper@37950
   786
	 mv_rev_to(!lc)
neuper@37950
   787
	 )
neuper@37950
   788
    end;
neuper@37950
   789
neuper@37950
   790
(*. compares two powerproducts .*)
neuper@37950
   791
fun mv_monom_equal((_,xlist):mv_monom,(_,ylist):mv_monom) = (foldr and_) (((map op=) (xlist~~ylist)),true);
neuper@37950
   792
    
neuper@37950
   793
(*. help function for mv_add .*)
neuper@37950
   794
fun mv_madd([]:mv_poly,[]:mv_poly,order) = []:mv_poly
neuper@37950
   795
  | mv_madd([(0,_)],p2,order) = p2
neuper@37950
   796
  | mv_madd(p1,[(0,_)],order) = p1  
neuper@37950
   797
  | mv_madd([],p2,order) = p2
neuper@37950
   798
  | mv_madd(p1,[],order) = p1
neuper@37950
   799
  | mv_madd(p1,p2,order) = 
neuper@37950
   800
    (
neuper@37950
   801
     if mv_monom_greater(hd(p1),hd(p2),order) 
neuper@37950
   802
	 then hd(p1)::mv_madd(tl(p1),p2,order)
neuper@37950
   803
     else if mv_monom_equal(hd(p1),hd(p2)) 
neuper@37950
   804
	      then if mv_lc2(p1,order)+mv_lc2(p2,order)<>0 
neuper@37950
   805
		       then (mv_lc2(p1,order)+mv_lc2(p2,order),mv_lpp(p1,order))::mv_madd(tl(p1),tl(p2),order)
neuper@37950
   806
		   else mv_madd(tl(p1),tl(p2),order)
neuper@37950
   807
	  else hd(p2)::mv_madd(p1,tl(p2),order)
neuper@37950
   808
	      )
neuper@37950
   809
	      
neuper@37950
   810
(*. adds two multivariate polynomials .*)
neuper@37950
   811
fun mv_add([]:mv_poly,p2:mv_poly,order) = p2
neuper@37950
   812
  | mv_add(p1,[],order) = p1
neuper@37950
   813
  | mv_add(p1,p2,order) = mv_madd(rev(sort (mv_geq order) p1),rev(sort (mv_geq order) p2), order);
neuper@37950
   814
neuper@37950
   815
(*. monom multiplication .*)
neuper@37950
   816
fun mv_mmul((x1,y1):mv_monom,(x2,y2):mv_monom)=(x1*x2,(map op+) (y1~~y2)):mv_monom;
neuper@37950
   817
neuper@37950
   818
(*. deletes all monomials with coefficient 0 .*)
neuper@37950
   819
fun mv_shorten([]:mv_poly,order) = []:mv_poly
neuper@37950
   820
  | mv_shorten(x::xs,order)=mv_madd([x],mv_shorten(xs,order),order);
neuper@37950
   821
neuper@37950
   822
(*. zeros a list .*)
neuper@37950
   823
fun mv_null2([])=[]
neuper@37950
   824
  | mv_null2(x::l)=0::mv_null2(l);
neuper@37950
   825
neuper@37950
   826
(*. multiplies two multivariate polynomials .*)
neuper@37950
   827
fun mv_mul([]:mv_poly,[]:mv_poly,_) = []:mv_poly
neuper@37950
   828
  | mv_mul([],y::p2,_) = [(0,mv_null2(#2(y)))]
neuper@37950
   829
  | mv_mul(x::p1,[],_) = [(0,mv_null2(#2(x)))] 
neuper@37950
   830
  | mv_mul(x::p1,y::p2,order) = mv_shorten(rev(sort (mv_geq order) (mv_mmul(x,y) :: (mv_mul(p1,y::p2,order) @
neuper@37950
   831
									    mv_mul([x],p2,order)))),order);
neuper@37950
   832
neuper@37950
   833
(*. gets the maximum value of a list .*)
neuper@37950
   834
fun mv_getmax([])=0
neuper@37950
   835
  | mv_getmax(x::p1)= let 
neuper@37950
   836
		       val m=mv_getmax(p1);
neuper@37950
   837
		   in
neuper@37950
   838
		       if m>x then m
neuper@37950
   839
		       else x
neuper@37950
   840
		   end;
neuper@37950
   841
(*. calculates the maximum degree of an multivariate polynomial .*)
neuper@37950
   842
fun mv_grad([]:mv_poly) = 0 
neuper@37950
   843
  | mv_grad(p1:mv_poly)= mv_getmax((map mv_addlist) ((map #2) p1));
neuper@37950
   844
neuper@37950
   845
(*. converts the sign of a value .*)
neuper@37950
   846
fun mv_minus(x)=(~1) * x;
neuper@37950
   847
neuper@37950
   848
(*. converts the sign of all coefficients of a polynomial .*)
neuper@37950
   849
fun mv_minus2([]:mv_poly)=[]:mv_poly
neuper@37950
   850
  | mv_minus2(p1)=(mv_minus(#1(hd(p1))),#2(hd(p1)))::(mv_minus2(tl(p1)));
neuper@37950
   851
neuper@37950
   852
(*. searches for a negativ value in a list .*)
neuper@37950
   853
fun mv_is_negativ([])=false
neuper@37950
   854
  | mv_is_negativ(x::xs)=if x<0 then true else mv_is_negativ(xs);
neuper@37950
   855
neuper@37950
   856
(*. division of monomials .*)
neuper@37950
   857
fun mv_mdiv((0,[]):mv_monom,_:mv_monom)=(0,[]):mv_monom
neuper@38031
   858
  | mv_mdiv(_,(0,[]))= error ("RATIONALS_MV_MDIV_EXCEPTION Division by 0 ")
neuper@37950
   859
  | mv_mdiv(p1:mv_monom,p2:mv_monom)= 
neuper@37950
   860
    let
neuper@38006
   861
	val c= Unsynchronized.ref  (#1(p2));
neuper@38006
   862
	val pp= Unsynchronized.ref  [];
neuper@37950
   863
    in 
neuper@37950
   864
	(
neuper@38031
   865
	 if !c=0 then error("MV_MDIV_EXCEPTION Dividing by zero")
neuper@37950
   866
	 else c:=(#1(p1) div #1(p2));
neuper@37950
   867
	     if #1(p2)<>0 then 
neuper@37950
   868
		 (
neuper@37950
   869
		  pp:=(#2(mv_mmul((1,#2(p1)),(1,(map mv_minus) (#2(p2))))));
neuper@37950
   870
		  if mv_is_negativ(!pp) then (0,!pp)
neuper@37950
   871
		  else (!c,!pp) 
neuper@37950
   872
		      )
neuper@38031
   873
	     else error("MV_MDIV_EXCEPTION Dividing by empty Polynom")
neuper@37950
   874
		 )
neuper@37950
   875
    end;
neuper@37950
   876
neuper@37950
   877
(*. prints a polynom for (internal use only) .*)
neuper@38015
   878
fun mv_print_poly([]:mv_poly)=tracing("[]\n")
neuper@38015
   879
  | mv_print_poly((x,y)::[])= tracing("("^Int.toString(x)^","^ints2str(y)^")\n")
neuper@38015
   880
  | mv_print_poly((x,y)::p1) = (tracing("("^Int.toString(x)^","^ints2str(y)^"),");mv_print_poly(p1));
neuper@37950
   881
neuper@37950
   882
neuper@37950
   883
(*. division of two multivariate polynomials .*) 
neuper@37950
   884
fun mv_division([]:mv_poly,g:mv_poly,order)=([]:mv_poly,[]:mv_poly)
neuper@38031
   885
  | mv_division(f,[],order)= error ("RATIONALS_MV_DIVISION_EXCEPTION Division by zero")
neuper@37950
   886
  | mv_division(f,g,order)=
neuper@37950
   887
    let 
neuper@38006
   888
	val r= Unsynchronized.ref  [];
neuper@38006
   889
	val q= Unsynchronized.ref  [];
neuper@38006
   890
	val g'= Unsynchronized.ref  ([] : mv_monom list);
neuper@38006
   891
	val k= Unsynchronized.ref  0;
neuper@38006
   892
	val m= Unsynchronized.ref  (0,[0]);
neuper@38006
   893
	val exit= Unsynchronized.ref  0;
neuper@37950
   894
    in
neuper@37950
   895
	r := rev(sort (mv_geq order) (mv_shorten(f,order)));
neuper@37950
   896
	g':= rev(sort (mv_geq order) (mv_shorten(g,order)));
neuper@38031
   897
	if #1(hd(!g'))=0 then error("RATIONALS_MV_DIVISION_EXCEPTION: Dividing by zero") else ();
neuper@37950
   898
	if  (mv_monom_greater (hd(!g'),hd(!r),order)) then ([(0,mv_null2(#2(hd(f))))],(!r))
neuper@37950
   899
	else
neuper@37950
   900
	    (
neuper@37950
   901
	     exit:=0;
neuper@37950
   902
	     while (if (!exit)=0 then not(mv_monom_greater (hd(!g'),hd(!r),order)) else false) do
neuper@37950
   903
		 (
neuper@37950
   904
		  if (#1(mv_lm(!g',order)))<>0 then m:=mv_mdiv(mv_lm(!r,order),mv_lm(!g',order))
neuper@38031
   905
		  else error ("RATIONALS_MV_DIVISION_EXCEPTION: Dividing by zero");	  
neuper@37950
   906
		  if #1(!m)<>0 then
neuper@37950
   907
		      ( 
neuper@37950
   908
		       q:=(!m)::(!q);
neuper@37950
   909
		       r:=mv_add((!r),mv_minus2(mv_mul(!g',[!m],order)),order)
neuper@37950
   910
		       )
neuper@37950
   911
		  else exit:=1;
neuper@37950
   912
		  if (if length(!r)<>0 then length(!g')<>0 else false) then ()
neuper@37950
   913
		  else (exit:=1)
neuper@37950
   914
		  );
neuper@37950
   915
		 (rev(!q),!r)
neuper@37950
   916
		 )
neuper@37950
   917
    end;
neuper@37950
   918
neuper@37950
   919
(*. multiplies a polynomial with an integer .*)
neuper@37950
   920
fun mv_skalar_mul([]:mv_poly,c) = []:mv_poly
neuper@37950
   921
  | mv_skalar_mul((x,y)::p1,c) = ((x * c),y)::mv_skalar_mul(p1,c); 
neuper@37950
   922
neuper@37950
   923
(*. inserts the a first variable into an polynomial with exponent v .*)
neuper@37950
   924
fun mv_correct([]:mv_poly,v:int)=[]:mv_poly
neuper@37950
   925
  | mv_correct((x,y)::list,v:int)=(x,v::y)::mv_correct(list,v);
neuper@37950
   926
neuper@37950
   927
(*. multivariate case .*)
neuper@37950
   928
neuper@37950
   929
(*. decides if x is a factor of y .*)
neuper@38031
   930
fun mv_divides([]:mv_poly,[]:mv_poly)=  error("RATIONALS_MV_DIVIDES_EXCEPTION: division by zero")
neuper@38031
   931
  | mv_divides(x,[]) =  error("RATIONALS_MV_DIVIDES_EXCEPTION: division by zero")
neuper@37950
   932
  | mv_divides(x:mv_poly,y:mv_poly) = #2(mv_division(y,x,LEX_))=[];
neuper@37950
   933
neuper@37950
   934
(*. gets the maximum of a and b .*)
neuper@37950
   935
fun mv_max(a,b) = if a>b then a else b;
neuper@37950
   936
neuper@37950
   937
(*. gets the maximum exponent of a mv polynomial in the lexicographic term order .*)
neuper@37950
   938
fun mv_deg([]:mv_poly) = 0  
neuper@37950
   939
  | mv_deg(p1)=
neuper@37950
   940
    let
neuper@37950
   941
	val p1'=mv_shorten(p1,LEX_);
neuper@37950
   942
    in
neuper@37950
   943
	if length(p1')=0 then 0 
neuper@37950
   944
	else mv_max(hd(#2(hd(p1'))),mv_deg(tl(p1')))
neuper@37950
   945
    end;
neuper@37950
   946
neuper@37950
   947
(*. gets the maximum exponent of a mv polynomial in the reverse lexicographic term order .*)
neuper@37950
   948
fun mv_deg2([]:mv_poly) = 0
neuper@37950
   949
  | mv_deg2(p1)=
neuper@37950
   950
    let
neuper@37950
   951
	val p1'=mv_shorten(p1,LEX_);
neuper@37950
   952
    in
neuper@37950
   953
	if length(p1')=0 then 0 
neuper@37950
   954
	else mv_max(hd(rev(#2(hd(p1')))),mv_deg2(tl(p1')))
neuper@37950
   955
    end;
neuper@37950
   956
neuper@37950
   957
(*. evaluates the mv polynomial at the value v of the main variable .*)
neuper@37950
   958
fun mv_subs([]:mv_poly,v) = []:mv_poly
neuper@37950
   959
  | mv_subs((c,e)::p1:mv_poly,v) = mv_skalar_mul(mv_cut([(c,e)]),power v (hd(e))) @ mv_subs(p1,v);
neuper@37950
   960
neuper@37950
   961
(*. calculates the content of a uv-polynomial in mv-representation .*)
neuper@37950
   962
fun uv_content2([]:mv_poly) = 0
neuper@37950
   963
  | uv_content2((c,e)::p1) = (gcd_int c (uv_content2(p1)));
neuper@37950
   964
neuper@37950
   965
(*. converts a uv-polynomial from mv-representation to  uv-representation .*)
neuper@37950
   966
fun uv_to_list ([]:mv_poly)=[]:uv_poly
neuper@37950
   967
  | uv_to_list ((c1,e1)::others) = 
neuper@37950
   968
    let
neuper@38006
   969
	val count= Unsynchronized.ref  0;
neuper@37950
   970
	val max=mv_grad((c1,e1)::others); 
neuper@38006
   971
	val help= Unsynchronized.ref  ((c1,e1)::others);
neuper@38006
   972
	val list= Unsynchronized.ref  [];
neuper@37950
   973
    in
neuper@38031
   974
	if length(e1)>1 then error ("RATIONALS_TO_LIST_EXCEPTION: not univariate")
neuper@37950
   975
	else if length(e1)=0 then [c1]
neuper@37950
   976
	     else
neuper@37950
   977
		 (
neuper@37950
   978
		  count:=0;
neuper@37950
   979
		  while (!count)<=max do
neuper@37950
   980
		      (
neuper@37950
   981
		       if length(!help)>0 andalso hd(#2(hd(!help)))=max-(!count) then 
neuper@37950
   982
			   (
neuper@37950
   983
			    list:=(#1(hd(!help)))::(!list);		       
neuper@37950
   984
			    help:=tl(!help) 
neuper@37950
   985
			    )
neuper@37950
   986
		       else 
neuper@37950
   987
			   (
neuper@37950
   988
			    list:= 0::(!list)
neuper@37950
   989
			    );
neuper@37950
   990
		       count := (!count) + 1
neuper@37950
   991
		       );
neuper@37950
   992
		      (!list)
neuper@37950
   993
		      )
neuper@37950
   994
    end;
neuper@37950
   995
neuper@37950
   996
(*. converts a uv-polynomial from uv-representation to mv-representation .*)
neuper@37950
   997
fun uv_to_poly ([]:uv_poly) = []:mv_poly
neuper@37950
   998
  | uv_to_poly p1 = 
neuper@37950
   999
    let
neuper@38006
  1000
	val count= Unsynchronized.ref  0;
neuper@38006
  1001
	val help= Unsynchronized.ref  p1;
neuper@38006
  1002
	val list= Unsynchronized.ref  [];
neuper@37950
  1003
    in
neuper@37950
  1004
	while length(!help)>0 do
neuper@37950
  1005
	    (
neuper@37950
  1006
	     if hd(!help)=0 then ()
neuper@37950
  1007
	     else list:=(hd(!help),[!count])::(!list);
neuper@37950
  1008
	     count:=(!count)+1;
neuper@37950
  1009
	     help:=tl(!help)
neuper@37950
  1010
	     );
neuper@37950
  1011
	    (!list)
neuper@37950
  1012
    end;
neuper@37950
  1013
neuper@37950
  1014
(*. univariate gcd calculation from polynomials in multivariate representation .*)
neuper@37950
  1015
fun uv_gcd ([]:mv_poly) p2 = p2
neuper@37950
  1016
  | uv_gcd p1 ([]:mv_poly) = p1
neuper@37950
  1017
  | uv_gcd p1 [(c,[e])] = 
neuper@37950
  1018
    let 
neuper@38006
  1019
	val list= Unsynchronized.ref  (rev(sort (mv_geq LEX_) (mv_shorten(p1,LEX_))));
neuper@37950
  1020
	val min=uv_mod_min(e,(hd(#2(hd(rev(!list))))));
neuper@37950
  1021
    in
neuper@37950
  1022
	[(gcd_int (uv_content2(p1)) c,[min])]
neuper@37950
  1023
    end
neuper@37950
  1024
  | uv_gcd [(c,[e])] p2 = 
neuper@37950
  1025
    let 
neuper@38006
  1026
	val list= Unsynchronized.ref  (rev(sort (mv_geq LEX_) (mv_shorten(p2,LEX_))));
neuper@37950
  1027
	val min=uv_mod_min(e,(hd(#2(hd(rev(!list))))));
neuper@37950
  1028
    in
neuper@37950
  1029
	[(gcd_int (uv_content2(p2)) c,[min])]
neuper@37950
  1030
    end 
neuper@37950
  1031
  | uv_gcd p11 p22 = uv_to_poly(uv_mod_gcd (uv_to_list(mv_shorten(p11,LEX_))) (uv_to_list(mv_shorten(p22,LEX_))));
neuper@37950
  1032
neuper@37950
  1033
(*. help function for the newton interpolation .*)
neuper@37950
  1034
fun mv_newton_help ([]:mv_poly list,k:int) = []:mv_poly list
neuper@37950
  1035
  | mv_newton_help (pl:mv_poly list,k) = 
neuper@37950
  1036
    let
neuper@38006
  1037
	val x= Unsynchronized.ref  (rev(pl));
neuper@38006
  1038
	val t= Unsynchronized.ref  [];
neuper@38006
  1039
	val y= Unsynchronized.ref  [];
neuper@38006
  1040
	val n= Unsynchronized.ref  1;
neuper@38006
  1041
	val n1= Unsynchronized.ref [];
neuper@37950
  1042
    in
neuper@37950
  1043
	(
neuper@37950
  1044
	 while length(!x)>1 do 
neuper@37950
  1045
	     (
neuper@37950
  1046
	      if length(hd(!x))>0 then n1:=mv_null2(#2(hd(hd(!x))))
neuper@37950
  1047
	      else if length(hd(tl(!x)))>0 then n1:=mv_null2(#2(hd(hd(tl(!x)))))
neuper@37950
  1048
		   else n1:=[]; 
neuper@37950
  1049
	      t:= #1(mv_division(mv_add(hd(!x),mv_skalar_mul(hd(tl(!x)),~1),LEX_),[(k,!n1)],LEX_)); 
neuper@37950
  1050
	      y:=(!t)::(!y);
neuper@37950
  1051
	      x:=tl(!x)
neuper@37950
  1052
	      );
neuper@37950
  1053
	 (!y)
neuper@37950
  1054
	 )
neuper@37950
  1055
    end;
neuper@37950
  1056
    
neuper@37950
  1057
(*. help function for the newton interpolation .*)
neuper@37950
  1058
fun mv_newton_add ([]:mv_poly list) t = []:mv_poly
neuper@37950
  1059
  | mv_newton_add [x:mv_poly] t = x 
neuper@37950
  1060
  | mv_newton_add (pl:mv_poly list) t = 
neuper@37950
  1061
    let
neuper@38006
  1062
	val expos= Unsynchronized.ref  [];
neuper@38006
  1063
	val pll= Unsynchronized.ref  pl;
neuper@37950
  1064
    in
neuper@37950
  1065
	(
neuper@37950
  1066
neuper@37950
  1067
	 while length(!pll)>0 andalso hd(!pll)=[]  do 
neuper@37950
  1068
	     ( 
neuper@37950
  1069
	      pll:=tl(!pll)
neuper@37950
  1070
	      ); 
neuper@37950
  1071
	 if length(!pll)>0 then expos:= #2(hd(hd(!pll))) else expos:=[];
neuper@37950
  1072
	 mv_add(hd(pl),
neuper@37950
  1073
		mv_mul(
neuper@37950
  1074
		       mv_add(mv_correct(mv_cut([(1,mv_null2(!expos))]),1),[(~t,mv_null2(!expos))],LEX_),
neuper@37950
  1075
		       mv_newton_add (tl(pl)) (t+1),
neuper@37950
  1076
		       LEX_
neuper@37950
  1077
		       ),
neuper@37950
  1078
		LEX_)
neuper@37950
  1079
	 )
neuper@37950
  1080
    end;
neuper@37950
  1081
neuper@37950
  1082
(*. calculates the newton interpolation with polynomial coefficients .*)
neuper@37950
  1083
(*. step-depth is 1 and if the result is not an integerpolynomial .*)
neuper@37950
  1084
(*. this function returns [] .*)
neuper@37950
  1085
fun mv_newton ([]:(mv_poly) list) = []:mv_poly 
neuper@37950
  1086
  | mv_newton ([mp]:(mv_poly) list) = mp:mv_poly
neuper@37950
  1087
  | mv_newton pl =
neuper@37950
  1088
    let
neuper@38006
  1089
	val c= Unsynchronized.ref  pl;
neuper@38006
  1090
	val c1= Unsynchronized.ref  [];
neuper@37950
  1091
	val n=length(pl);
neuper@38006
  1092
	val k= Unsynchronized.ref  1;
neuper@38006
  1093
	val i= Unsynchronized.ref  n;
neuper@38006
  1094
	val ppl= Unsynchronized.ref  [];
neuper@37950
  1095
    in
neuper@37950
  1096
	c1:=hd(pl)::[];
neuper@37950
  1097
	c:=mv_newton_help(!c,!k);
neuper@37950
  1098
	c1:=(hd(!c))::(!c1);
neuper@37950
  1099
	while(length(!c)>1 andalso !k<n) do
neuper@37950
  1100
	    (	 
neuper@37950
  1101
	     k:=(!k)+1; 
neuper@37950
  1102
	     while  length(!c)>0 andalso hd(!c)=[] do c:=tl(!c); 	  
neuper@37950
  1103
	     if !c=[] then () else c:=mv_newton_help(!c,!k);
neuper@37950
  1104
	     ppl:= !c;
neuper@37950
  1105
	     if !c=[] then () else  c1:=(hd(!c))::(!c1)
neuper@37950
  1106
	     );
neuper@37950
  1107
	while  hd(!c1)=[] do c1:=tl(!c1);
neuper@37950
  1108
	c1:=rev(!c1);
neuper@37950
  1109
	ppl:= !c1;
neuper@37950
  1110
	mv_newton_add (!c1) 1
neuper@37950
  1111
    end;
neuper@37950
  1112
neuper@37950
  1113
(*. sets the exponents of the first variable to zero .*)
neuper@37950
  1114
fun mv_null3([]:mv_poly)    = []:mv_poly
neuper@37950
  1115
  | mv_null3((x,y)::xs) = (x,0::tl(y))::mv_null3(xs);
neuper@37950
  1116
neuper@37950
  1117
(*. calculates the minimum exponents of a multivariate polynomial .*)
neuper@37950
  1118
fun mv_min_pp([]:mv_poly)=[]
neuper@37950
  1119
  | mv_min_pp((c,e)::xs)=
neuper@37950
  1120
    let
neuper@38006
  1121
	val y= Unsynchronized.ref  xs;
neuper@38006
  1122
	val x= Unsynchronized.ref  [];
neuper@37950
  1123
    in
neuper@37950
  1124
	(
neuper@37950
  1125
	 x:=e;
neuper@37950
  1126
	 while length(!y)>0 do
neuper@37950
  1127
	     (
neuper@37950
  1128
	      x:=(map uv_mod_min) ((!x) ~~ (#2(hd(!y))));
neuper@37950
  1129
	      y:=tl(!y)
neuper@37950
  1130
	      );
neuper@37950
  1131
	 !x
neuper@37950
  1132
	 )
neuper@37950
  1133
    end;
neuper@37950
  1134
neuper@37950
  1135
(*. checks if all elements of the list have value zero .*)
neuper@37950
  1136
fun list_is_null [] = true 
neuper@37950
  1137
  | list_is_null (x::xs) = (x=0 andalso list_is_null(xs)); 
neuper@37950
  1138
neuper@37950
  1139
(* check if main variable is zero*)
neuper@37950
  1140
fun main_zero (ms : mv_poly) = (list_is_null o (map (hd o #2))) ms;
neuper@37950
  1141
neuper@37950
  1142
(*. calculates the content of an polynomial .*)
neuper@37950
  1143
fun mv_content([]:mv_poly) = []:mv_poly
neuper@37950
  1144
  | mv_content(p1) = 
neuper@37950
  1145
    let
neuper@38006
  1146
	val list= Unsynchronized.ref  (rev(sort (mv_geq LEX_) (mv_shorten(p1,LEX_))));
neuper@38006
  1147
	val test= Unsynchronized.ref  (hd(#2(hd(!list))));
neuper@38006
  1148
	val result= Unsynchronized.ref  []; 
neuper@37950
  1149
	val min=(hd(#2(hd(rev(!list)))));
neuper@37950
  1150
    in
neuper@37950
  1151
	(
neuper@37950
  1152
	 if length(!list)>1 then
neuper@37950
  1153
	     (
neuper@37950
  1154
	      while (if length(!list)>0 then (hd(#2(hd(!list)))=(!test)) else false) do
neuper@37950
  1155
		  (
neuper@37950
  1156
		   result:=(#1(hd(!list)),tl(#2(hd(!list))))::(!result);
neuper@37950
  1157
		   
neuper@37950
  1158
		   if length(!list)<1 then list:=[]
neuper@37950
  1159
		   else list:=tl(!list) 
neuper@37950
  1160
		       
neuper@37950
  1161
		       );		  
neuper@37950
  1162
		  if length(!list)>0 then  
neuper@37950
  1163
		   ( 
neuper@37950
  1164
		    list:=mv_gcd (!result) (mv_cut(mv_content(!list))) 
neuper@37950
  1165
		    ) 
neuper@37950
  1166
		  else list:=(!result); 
neuper@37950
  1167
		  list:=mv_correct(!list,0);  
neuper@37950
  1168
		  (!list) 
neuper@37950
  1169
		  )
neuper@37950
  1170
	 else
neuper@37950
  1171
	     (
neuper@37950
  1172
	      mv_null3(!list) 
neuper@37950
  1173
	      )
neuper@37950
  1174
	     )
neuper@37950
  1175
    end
neuper@37950
  1176
neuper@37950
  1177
(*. calculates the primitiv part of a polynomial .*)
neuper@37950
  1178
and mv_pp([]:mv_poly) = []:mv_poly
neuper@37950
  1179
  | mv_pp(p1) = let
neuper@38006
  1180
		    val cont= Unsynchronized.ref  []; 
neuper@38006
  1181
		    val pp= Unsynchronized.ref [];
neuper@37950
  1182
		in
neuper@37950
  1183
		    cont:=mv_content(p1);
neuper@37950
  1184
		    pp:=(#1(mv_division(p1,!cont,LEX_)));
neuper@37950
  1185
		    if !pp=[] 
neuper@38031
  1186
			then error("RATIONALS_MV_PP_EXCEPTION: Invalid Content ")
neuper@37950
  1187
		    else (!pp)
neuper@37950
  1188
		end
neuper@37950
  1189
neuper@37950
  1190
(*. calculates the gcd of two multivariate polynomials with a modular approach .*)
neuper@37950
  1191
and mv_gcd ([]:mv_poly) ([]:mv_poly) :mv_poly= []:mv_poly
neuper@37950
  1192
  | mv_gcd ([]:mv_poly) (p2) :mv_poly= p2:mv_poly
neuper@37950
  1193
  | mv_gcd (p1:mv_poly) ([]) :mv_poly= p1:mv_poly
neuper@37950
  1194
  | mv_gcd ([(x,xs)]:mv_poly) ([(y,ys)]):mv_poly = 
neuper@37950
  1195
     let
neuper@37950
  1196
      val xpoly:mv_poly = [(x,xs)];
neuper@37950
  1197
      val ypoly:mv_poly = [(y,ys)];
neuper@37950
  1198
     in 
neuper@37950
  1199
	(
neuper@37950
  1200
	 if xs=ys then [((gcd_int x y),xs)]
neuper@37950
  1201
	 else [((gcd_int x y),(map uv_mod_min)(xs~~ys))]:mv_poly
neuper@37950
  1202
        )
neuper@37950
  1203
    end 
neuper@37950
  1204
  | mv_gcd (p1:mv_poly) ([(y,ys)]) :mv_poly= 
neuper@37950
  1205
	(
neuper@37950
  1206
	 [(gcd_int (uv_content2(p1)) (y),(map  uv_mod_min)(mv_min_pp(p1)~~ys))]:mv_poly
neuper@37950
  1207
	)
neuper@37950
  1208
  | mv_gcd ([(y,ys)]:mv_poly) (p2):mv_poly = 
neuper@37950
  1209
	(
neuper@37950
  1210
         [(gcd_int (uv_content2(p2)) (y),(map  uv_mod_min)(mv_min_pp(p2)~~ys))]:mv_poly
neuper@37950
  1211
        )
neuper@37950
  1212
  | mv_gcd (p1':mv_poly) (p2':mv_poly):mv_poly=
neuper@37950
  1213
    let
neuper@37950
  1214
	val vc=length(#2(hd(p1')));
neuper@37950
  1215
	val cont = 
neuper@37950
  1216
		  (
neuper@37950
  1217
                   if main_zero(mv_content(p1')) andalso 
neuper@37950
  1218
                     (main_zero(mv_content(p2'))) then
neuper@37950
  1219
                     mv_correct((mv_gcd (mv_cut(mv_content(p1'))) (mv_cut(mv_content(p2')))),0)
neuper@37950
  1220
                   else 
neuper@37950
  1221
                     mv_gcd (mv_content(p1')) (mv_content(p2'))
neuper@37950
  1222
                  );
neuper@37950
  1223
	val p1= #1(mv_division(p1',mv_content(p1'),LEX_));
neuper@37950
  1224
	val p2= #1(mv_division(p2',mv_content(p2'),LEX_)); 
neuper@38006
  1225
	val gcd= Unsynchronized.ref  [];
neuper@38006
  1226
	val candidate= Unsynchronized.ref  [];
neuper@38006
  1227
	val interpolation_list= Unsynchronized.ref  [];
neuper@38006
  1228
	val delta= Unsynchronized.ref  [];
neuper@38006
  1229
        val p1r = Unsynchronized.ref [];
neuper@38006
  1230
        val p2r = Unsynchronized.ref [];
neuper@38006
  1231
        val p1r' = Unsynchronized.ref [];
neuper@38006
  1232
        val p2r' = Unsynchronized.ref [];
neuper@38006
  1233
	val factor= Unsynchronized.ref  [];
neuper@38006
  1234
	val r= Unsynchronized.ref  0;
neuper@38006
  1235
	val gcd_r= Unsynchronized.ref  [];
neuper@38006
  1236
	val d= Unsynchronized.ref  0;
neuper@38006
  1237
	val exit= Unsynchronized.ref  0;
neuper@38006
  1238
	val current_degree= Unsynchronized.ref  99999; (*. FIXME: unlimited ! .*)
neuper@37950
  1239
    in
neuper@37950
  1240
	(
neuper@37950
  1241
	 if vc<2 then (* areUnivariate(p1',p2') *)
neuper@37950
  1242
	     (
neuper@37950
  1243
	      gcd:=uv_gcd (mv_shorten(p1',LEX_)) (mv_shorten(p2',LEX_))
neuper@37950
  1244
	      )
neuper@37950
  1245
	 else
neuper@37950
  1246
	     (
neuper@37950
  1247
	      while !exit=0 do
neuper@37950
  1248
		  (
neuper@37950
  1249
		   r:=(!r)+1;
neuper@37950
  1250
                   p1r := mv_lc(p1,LEX_);
neuper@37950
  1251
		   p2r := mv_lc(p2,LEX_);
neuper@37950
  1252
                   if main_zero(!p1r) andalso
neuper@37950
  1253
                      main_zero(!p2r) 
neuper@37950
  1254
                   then
neuper@37950
  1255
                       (
neuper@37950
  1256
                        delta := mv_correct((mv_gcd (mv_cut (!p1r)) (mv_cut (!p2r))),0)
neuper@37950
  1257
                       )
neuper@37950
  1258
                   else
neuper@37950
  1259
                       (
neuper@37950
  1260
		        delta := mv_gcd (!p1r) (!p2r)
neuper@37950
  1261
                       );
neuper@37950
  1262
		   (*if mv_shorten(mv_subs(!p1r,!r),LEX_)=[] andalso 
neuper@37950
  1263
		      mv_shorten(mv_subs(!p2r,!r),LEX_)=[] *)
neuper@37950
  1264
		   if mv_lc2(mv_shorten(mv_subs(!p1r,!r),LEX_),LEX_)=0 andalso 
neuper@37950
  1265
		      mv_lc2(mv_shorten(mv_subs(!p2r,!r),LEX_),LEX_)=0 
neuper@37950
  1266
                   then 
neuper@37950
  1267
                       (
neuper@37950
  1268
		       )
neuper@37950
  1269
		   else 
neuper@37950
  1270
		       (
neuper@37950
  1271
			gcd_r:=mv_shorten(mv_gcd (mv_shorten(mv_subs(p1,!r),LEX_)) 
neuper@37950
  1272
					         (mv_shorten(mv_subs(p2,!r),LEX_)) ,LEX_);
neuper@37950
  1273
			gcd_r:= #1(mv_division(mv_mul(mv_correct(mv_subs(!delta,!r),0),!gcd_r,LEX_),
neuper@37950
  1274
					       mv_correct(mv_lc(!gcd_r,LEX_),0),LEX_));
neuper@37950
  1275
			d:=mv_deg2(!gcd_r); (* deg(gcd_r,z) *)
neuper@37950
  1276
			if (!d < !current_degree) then 
neuper@37950
  1277
			    (
neuper@37950
  1278
			     current_degree:= !d;
neuper@37950
  1279
			     interpolation_list:=mv_correct(!gcd_r,0)::(!interpolation_list)
neuper@37950
  1280
			     )
neuper@37950
  1281
			else
neuper@37950
  1282
			    (
neuper@37950
  1283
			     if (!d = !current_degree) then
neuper@37950
  1284
				 (
neuper@37950
  1285
				  interpolation_list:=mv_correct(!gcd_r,0)::(!interpolation_list)
neuper@37950
  1286
				  )
neuper@37950
  1287
			     else () 
neuper@37950
  1288
				 )
neuper@37950
  1289
			    );
neuper@37950
  1290
		      if length(!interpolation_list)> uv_mod_min(mv_deg(p1),mv_deg(p2)) then 
neuper@37950
  1291
			  (
neuper@37950
  1292
			   candidate := mv_newton(rev(!interpolation_list));
neuper@37950
  1293
			   if !candidate=[] then ()
neuper@37950
  1294
			   else
neuper@37950
  1295
			       (
neuper@37950
  1296
				candidate:=mv_pp(!candidate);
neuper@37950
  1297
				if mv_divides(!candidate,p1) andalso mv_divides(!candidate,p2) then
neuper@37950
  1298
				    (
neuper@37950
  1299
				     gcd:= mv_mul(!candidate,cont,LEX_);
neuper@37950
  1300
				     exit:=1
neuper@37950
  1301
				     )
neuper@37950
  1302
				else ()
neuper@37950
  1303
				    );
neuper@37950
  1304
			       interpolation_list:=[mv_correct(!gcd_r,0)]
neuper@37950
  1305
			       )
neuper@37950
  1306
		      else ()
neuper@37950
  1307
			  )
neuper@37950
  1308
	     );
neuper@37950
  1309
	     (!gcd):mv_poly
neuper@37950
  1310
	     )
neuper@37950
  1311
    end;	
neuper@37950
  1312
neuper@37950
  1313
neuper@37950
  1314
(*. calculates the least common divisor of two polynomials .*)
neuper@37950
  1315
fun mv_lcm (p1:mv_poly) (p2:mv_poly) :mv_poly = 
neuper@37950
  1316
    (
neuper@37950
  1317
     #1(mv_division(mv_mul(p1,p2,LEX_),mv_gcd p1 p2,LEX_))
neuper@37950
  1318
     );
neuper@37950
  1319
neuper@37950
  1320
(*. gets the variables (strings) of a term .*)
neuper@37950
  1321
fun get_vars(term1) = (map free2str) (vars term1); (*["a","b","c"]; *)
neuper@37950
  1322
neuper@37950
  1323
(*. counts the negative coefficents in a polynomial .*)
neuper@37950
  1324
fun count_neg ([]:mv_poly) = 0 
neuper@37950
  1325
  | count_neg ((c,e)::xs) = if c<0 then 1+count_neg xs
neuper@37950
  1326
			  else count_neg xs;
neuper@37950
  1327
neuper@37950
  1328
(*. help function for is_polynomial  
neuper@37950
  1329
    checks the order of the operators .*)
neuper@37950
  1330
fun test_polynomial (Const ("uminus",_) $ Free (str,_)) _ = true (*WN.13.3.03*)
neuper@37950
  1331
  | test_polynomial (t as Free(str,_)) v = true
neuper@38034
  1332
  | test_polynomial (t as Const ("Groups.times_class.times",_) $ t1 $ t2) v = if v="^" then false
neuper@37950
  1333
						     else (test_polynomial t1 "*") andalso (test_polynomial t2 "*")
neuper@38014
  1334
  | test_polynomial (t as Const ("Groups.plus_class.plus",_) $ t1 $ t2) v = if v="*" orelse v="^" then false 
neuper@37950
  1335
							  else (test_polynomial t1 " ") andalso (test_polynomial t2 " ")
neuper@37950
  1336
  | test_polynomial (t as Const ("Atools.pow",_) $ t1 $ t2) v = (test_polynomial t1 "^") andalso (test_polynomial t2 "^")
neuper@37950
  1337
  | test_polynomial _ v = false;  
neuper@37950
  1338
neuper@37950
  1339
(*. tests if a term is a polynomial .*)  
neuper@37950
  1340
fun is_polynomial t = test_polynomial t " ";
neuper@37950
  1341
neuper@37950
  1342
(*. help function for is_expanded 
neuper@37950
  1343
    checks the order of the operators .*)
neuper@37950
  1344
fun test_exp (t as Free(str,_)) v = true 
neuper@38034
  1345
  | test_exp (t as Const ("Groups.times_class.times",_) $ t1 $ t2) v = if v="^" then false
neuper@37950
  1346
						     else (test_exp t1 "*") andalso (test_exp t2 "*")
neuper@38014
  1347
  | test_exp (t as Const ("Groups.plus_class.plus",_) $ t1 $ t2) v = if v="*" orelse v="^" then false 
neuper@37950
  1348
							  else (test_exp t1 " ") andalso (test_exp t2 " ") 
neuper@38014
  1349
  | test_exp (t as Const ("Groups.minus_class.minus",_) $ t1 $ t2) v = if v="*" orelse v="^" then false 
neuper@37950
  1350
							  else (test_exp t1 " ") andalso (test_exp t2 " ")
neuper@37950
  1351
  | test_exp (t as Const ("Atools.pow",_) $ t1 $ t2) v = (test_exp t1 "^") andalso (test_exp t2 "^")
neuper@37950
  1352
  | test_exp  _ v = false;
neuper@37950
  1353
neuper@37950
  1354
neuper@37950
  1355
(*. help function for check_coeff: 
neuper@37950
  1356
    converts the term to a list of coefficients .*) 
neuper@37950
  1357
fun term2coef' (t as Free(str,_(*typ*))) v :mv_poly option = 
neuper@37950
  1358
    let
neuper@38006
  1359
	val x= Unsynchronized.ref  NONE;
neuper@38006
  1360
	val len= Unsynchronized.ref  0;
neuper@38006
  1361
	val vl= Unsynchronized.ref  [];
neuper@38006
  1362
	val vh= Unsynchronized.ref  [];
neuper@38006
  1363
	val i= Unsynchronized.ref  0;
neuper@37950
  1364
    in 
neuper@37950
  1365
	if is_numeral str then
neuper@37950
  1366
	    (
neuper@37950
  1367
	     SOME [(((the o int_of_str) str),mv_null2(v))] handle _ => NONE
neuper@37950
  1368
		 )
neuper@37950
  1369
	else (* variable *)
neuper@37950
  1370
	    (
neuper@37950
  1371
	     len:=length(v);
neuper@37950
  1372
	     vh:=v;
neuper@37950
  1373
	     while ((!len)>(!i)) do
neuper@37950
  1374
		 (
neuper@37950
  1375
		  if str=hd((!vh)) then
neuper@37950
  1376
		      (
neuper@37950
  1377
		       vl:=1::(!vl)
neuper@37950
  1378
		       )
neuper@37950
  1379
		  else 
neuper@37950
  1380
		      (
neuper@37950
  1381
		       vl:=0::(!vl)
neuper@37950
  1382
		       );
neuper@37950
  1383
		      vh:=tl(!vh);
neuper@37950
  1384
		      i:=(!i)+1    
neuper@37950
  1385
		      );		
neuper@37950
  1386
		 SOME [(1,rev(!vl))] handle _ => NONE
neuper@37950
  1387
	    )
neuper@37950
  1388
    end
neuper@38034
  1389
  | term2coef' (Const ("Groups.times_class.times",_) $ t1 $ t2) v :mv_poly option= 
neuper@37950
  1390
    let
neuper@38006
  1391
	val t1pp= Unsynchronized.ref  [];
neuper@38006
  1392
	val t2pp= Unsynchronized.ref  [];
neuper@38006
  1393
	val t1c= Unsynchronized.ref  0;
neuper@38006
  1394
	val t2c= Unsynchronized.ref  0;
neuper@37950
  1395
    in
neuper@37950
  1396
	(
neuper@37950
  1397
	 t1pp:=(#2(hd(the(term2coef' t1 v))));
neuper@37950
  1398
	 t2pp:=(#2(hd(the(term2coef' t2 v))));
neuper@37950
  1399
	 t1c:=(#1(hd(the(term2coef' t1 v))));
neuper@37950
  1400
	 t2c:=(#1(hd(the(term2coef' t2 v))));
neuper@37950
  1401
	
neuper@37950
  1402
	 SOME [( (!t1c)*(!t2c) ,( (map op+) ((!t1pp)~~(!t2pp)) ) )] handle _ => NONE 
neuper@37950
  1403
		
neuper@37950
  1404
	 )
neuper@37950
  1405
    end
neuper@37950
  1406
  | term2coef' (Const ("Atools.pow",_) $ (t1 as Free (str1,_)) $ (t2 as Free (str2,_))) v :mv_poly option= 
neuper@37950
  1407
    let
neuper@38006
  1408
	val x= Unsynchronized.ref  NONE;
neuper@38006
  1409
	val len= Unsynchronized.ref  0;
neuper@38006
  1410
	val vl= Unsynchronized.ref  [];
neuper@38006
  1411
	val vh= Unsynchronized.ref  [];
neuper@38006
  1412
	val vtemp= Unsynchronized.ref  [];
neuper@38006
  1413
	val i= Unsynchronized.ref  0;	 
neuper@37950
  1414
    in
neuper@37950
  1415
    (
neuper@37950
  1416
     if (not o is_numeral) str1 andalso is_numeral str2 then
neuper@37950
  1417
	 (
neuper@37950
  1418
	  len:=length(v);
neuper@37950
  1419
	  vh:=v;
neuper@37950
  1420
neuper@37950
  1421
	  while ((!len)>(!i)) do
neuper@37950
  1422
	      (
neuper@37950
  1423
	       if str1=hd((!vh)) then
neuper@37950
  1424
		   (
neuper@37950
  1425
		    vl:=((the o int_of_str) str2)::(!vl)
neuper@37950
  1426
		    )
neuper@37950
  1427
	       else 
neuper@37950
  1428
		   (
neuper@37950
  1429
		    vl:=0::(!vl)
neuper@37950
  1430
		    );
neuper@37950
  1431
		   vh:=tl(!vh);
neuper@37950
  1432
		   i:=(!i)+1     
neuper@37950
  1433
		   );
neuper@37950
  1434
	      SOME [(1,rev(!vl))] handle _ => NONE
neuper@37950
  1435
	      )
neuper@38031
  1436
     else error ("RATIONALS_TERM2COEF_EXCEPTION 1: Invalid term")
neuper@37950
  1437
	 )
neuper@37950
  1438
    end
neuper@38014
  1439
  | term2coef' (Const ("Groups.plus_class.plus",_) $ t1 $ t2) v :mv_poly option= 
neuper@37950
  1440
    (
neuper@37950
  1441
     SOME ((the(term2coef' t1 v)) @ (the(term2coef' t2 v))) handle _ => NONE
neuper@37950
  1442
	 )
neuper@38014
  1443
  | term2coef' (Const ("Groups.minus_class.minus",_) $ t1 $ t2) v :mv_poly option= 
neuper@37950
  1444
    (
neuper@37950
  1445
     SOME ((the(term2coef' t1 v)) @ mv_skalar_mul((the(term2coef' t2 v)),1)) handle _ => NONE
neuper@37950
  1446
	 )
neuper@38031
  1447
  | term2coef' (term) v = error ("RATIONALS_TERM2COEF_EXCEPTION 2: Invalid term");
neuper@37950
  1448
neuper@37950
  1449
(*. checks if all coefficients of a polynomial are positiv (except the first) .*)
neuper@37950
  1450
fun check_coeff t = (* erste Koeffizient kann <0 sein !!! *)
neuper@37950
  1451
    if count_neg(tl(the(term2coef' t (get_vars(t)))))=0 then true 
neuper@37950
  1452
    else false;
neuper@37950
  1453
neuper@37950
  1454
(*. checks for expanded term [3] .*)
neuper@37950
  1455
fun is_expanded t = test_exp t " " andalso check_coeff(t); 
neuper@37950
  1456
neuper@37950
  1457
(*WN.7.3.03 Hilfsfunktion f"ur term2poly'*)
neuper@37950
  1458
fun mk_monom v' p vs = 
neuper@37950
  1459
    let fun conv p (v: string) = if v'= v then p else 0
neuper@37950
  1460
    in map (conv p) vs end;
neuper@37950
  1461
(* mk_monom "y" 5 ["a","b","x","y","z"];
neuper@37950
  1462
val it = [0,0,0,5,0] : int list*)
neuper@37950
  1463
neuper@37950
  1464
(*. this function converts the term representation into the internal representation mv_poly .*)
neuper@37950
  1465
fun term2poly' (Const ("uminus",_) $ Free (str,_)) v = (*WN.7.3.03*)
neuper@37950
  1466
    if is_numeral str 
neuper@37950
  1467
    then SOME [((the o int_of_str) ("-"^str), mk_monom "#" 0 v)]
neuper@37950
  1468
    else SOME [(~1, mk_monom str 1 v)]
neuper@37950
  1469
neuper@37950
  1470
  | term2poly' (Free(str,_)) v :mv_poly option = 
neuper@37950
  1471
    let
neuper@38006
  1472
	val x= Unsynchronized.ref  NONE;
neuper@38006
  1473
	val len= Unsynchronized.ref  0;
neuper@38006
  1474
	val vl= Unsynchronized.ref  [];
neuper@38006
  1475
	val vh= Unsynchronized.ref  [];
neuper@38006
  1476
	val i= Unsynchronized.ref  0;
neuper@37950
  1477
    in 
neuper@37950
  1478
	if is_numeral str then
neuper@37950
  1479
	    (
neuper@37950
  1480
	     SOME [(((the o int_of_str) str),mv_null2 v)] handle _ => NONE
neuper@37950
  1481
		 )
neuper@37950
  1482
	else (* variable *)
neuper@37950
  1483
	    (
neuper@37950
  1484
	     len:=length v;
neuper@37950
  1485
	     vh:= v;
neuper@37950
  1486
	     while ((!len)>(!i)) do
neuper@37950
  1487
		 (
neuper@37950
  1488
		  if str=hd((!vh)) then
neuper@37950
  1489
		      (
neuper@37950
  1490
		       vl:=1::(!vl)
neuper@37950
  1491
		       )
neuper@37950
  1492
		  else 
neuper@37950
  1493
		      (
neuper@37950
  1494
		       vl:=0::(!vl)
neuper@37950
  1495
		       );
neuper@37950
  1496
		      vh:=tl(!vh);
neuper@37950
  1497
		      i:=(!i)+1    
neuper@37950
  1498
		      );		
neuper@37950
  1499
		 SOME [(1,rev(!vl))] handle _ => NONE
neuper@37950
  1500
	    )
neuper@37950
  1501
    end
neuper@38034
  1502
  | term2poly' (Const ("Groups.times_class.times",_) $ t1 $ t2) v :mv_poly option= 
neuper@37950
  1503
    let
neuper@38006
  1504
	val t1pp= Unsynchronized.ref  [];
neuper@38006
  1505
	val t2pp= Unsynchronized.ref  [];
neuper@38006
  1506
	val t1c= Unsynchronized.ref  0;
neuper@38006
  1507
	val t2c= Unsynchronized.ref  0;
neuper@37950
  1508
    in
neuper@37950
  1509
	(
neuper@37950
  1510
	 t1pp:=(#2(hd(the(term2poly' t1 v))));
neuper@37950
  1511
	 t2pp:=(#2(hd(the(term2poly' t2 v))));
neuper@37950
  1512
	 t1c:=(#1(hd(the(term2poly' t1 v))));
neuper@37950
  1513
	 t2c:=(#1(hd(the(term2poly' t2 v))));
neuper@37950
  1514
	
neuper@37950
  1515
	 SOME [( (!t1c)*(!t2c) ,( (map op+) ((!t1pp)~~(!t2pp)) ) )] 
neuper@37950
  1516
	 handle _ => NONE 
neuper@37950
  1517
		
neuper@37950
  1518
	 )
neuper@37950
  1519
    end
neuper@37950
  1520
  | term2poly' (Const ("Atools.pow",_) $ (t1 as Free (str1,_)) $ 
neuper@37950
  1521
		      (t2 as Free (str2,_))) v :mv_poly option= 
neuper@37950
  1522
    let
neuper@38006
  1523
	val x= Unsynchronized.ref  NONE;
neuper@38006
  1524
	val len= Unsynchronized.ref  0;
neuper@38006
  1525
	val vl= Unsynchronized.ref  [];
neuper@38006
  1526
	val vh= Unsynchronized.ref  [];
neuper@38006
  1527
	val vtemp= Unsynchronized.ref  [];
neuper@38006
  1528
	val i= Unsynchronized.ref  0;	 
neuper@37950
  1529
    in
neuper@37950
  1530
    (
neuper@37950
  1531
     if (not o is_numeral) str1 andalso is_numeral str2 then
neuper@37950
  1532
	 (
neuper@37950
  1533
	  len:=length(v);
neuper@37950
  1534
	  vh:=v;
neuper@37950
  1535
neuper@37950
  1536
	  while ((!len)>(!i)) do
neuper@37950
  1537
	      (
neuper@37950
  1538
	       if str1=hd((!vh)) then
neuper@37950
  1539
		   (
neuper@37950
  1540
		    vl:=((the o int_of_str) str2)::(!vl)
neuper@37950
  1541
		    )
neuper@37950
  1542
	       else 
neuper@37950
  1543
		   (
neuper@37950
  1544
		    vl:=0::(!vl)
neuper@37950
  1545
		    );
neuper@37950
  1546
		   vh:=tl(!vh);
neuper@37950
  1547
		   i:=(!i)+1     
neuper@37950
  1548
		   );
neuper@37950
  1549
	      SOME [(1,rev(!vl))] handle _ => NONE
neuper@37950
  1550
	      )
neuper@38031
  1551
     else error ("RATIONALS_TERM2POLY_EXCEPTION 1: Invalid term")
neuper@37950
  1552
	 )
neuper@37950
  1553
    end
neuper@38014
  1554
  | term2poly' (Const ("Groups.plus_class.plus",_) $ t1 $ t2) v :mv_poly option = 
neuper@37950
  1555
    (
neuper@37950
  1556
     SOME ((the(term2poly' t1 v)) @ (the(term2poly' t2 v))) handle _ => NONE
neuper@37950
  1557
	 )
neuper@38014
  1558
  | term2poly' (Const ("Groups.minus_class.minus",_) $ t1 $ t2) v :mv_poly option = 
neuper@37950
  1559
    (
neuper@37950
  1560
     SOME ((the(term2poly' t1 v)) @ mv_skalar_mul((the(term2poly' t2 v)),~1)) handle _ => NONE
neuper@37950
  1561
	 )
neuper@38031
  1562
  | term2poly' (term) v = error ("RATIONALS_TERM2POLY_EXCEPTION 2: Invalid term");
neuper@37950
  1563
neuper@37950
  1564
(*. translates an Isabelle term into internal representation.
neuper@37950
  1565
    term2poly
neuper@37950
  1566
    fn : term ->              (*normalform [2]                    *)
neuper@37950
  1567
    	 string list ->       (*for ...!!! BITTE DIE ERKLÄRUNG, 
neuper@37950
  1568
    			       DIE DU MIR LETZTES MAL GEGEBEN HAST*)
neuper@37950
  1569
    	 mv_monom list        (*internal representation           *)
neuper@37950
  1570
    		  option      (*the translation may fail with NONE*)
neuper@37950
  1571
.*)
neuper@37950
  1572
fun term2poly (t:term) v = 
neuper@37950
  1573
     if is_polynomial t then term2poly' t v
neuper@38031
  1574
     else error ("term2poly: invalid = "^(term2str t));
neuper@37950
  1575
neuper@37950
  1576
(*. same as term2poly with automatic detection of the variables .*)
neuper@37950
  1577
fun term2polyx t = term2poly t (((map free2str) o vars) t); 
neuper@37950
  1578
neuper@37950
  1579
(*. checks if the term is in expanded polynomial form and converts it into the internal representation .*)
neuper@37950
  1580
fun expanded2poly (t:term) v = 
neuper@37950
  1581
    (*if is_expanded t then*) term2poly' t v
neuper@38031
  1582
    (*else error ("RATIONALS_EXPANDED2POLY_EXCEPTION: Invalid Polynomial")*);
neuper@37950
  1583
neuper@37950
  1584
(*. same as expanded2poly with automatic detection of the variables .*)
neuper@37950
  1585
fun expanded2polyx t = expanded2poly t (((map free2str) o vars) t);
neuper@37950
  1586
neuper@37950
  1587
(*. converts a powerproduct into term representation .*)
neuper@37950
  1588
fun powerproduct2term(xs,v) =  
neuper@37950
  1589
    let
neuper@38006
  1590
	val xss= Unsynchronized.ref  xs;
neuper@38006
  1591
	val vv= Unsynchronized.ref  v;
neuper@37950
  1592
    in
neuper@37950
  1593
	(
neuper@37950
  1594
	 while hd(!xss)=0 do 
neuper@37950
  1595
	     (
neuper@37950
  1596
	      xss:=tl(!xss);
neuper@37950
  1597
	      vv:=tl(!vv)
neuper@37950
  1598
	      );
neuper@37950
  1599
	     
neuper@37950
  1600
	 if list_is_null(tl(!xss)) then 
neuper@37950
  1601
	     (
neuper@37950
  1602
	      if hd(!xss)=1 then Free(hd(!vv), HOLogic.realT)
neuper@37950
  1603
	      else
neuper@37950
  1604
		  (
neuper@37950
  1605
		   Const("Atools.pow",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1606
		   Free(hd(!vv), HOLogic.realT) $  Free(str_of_int (hd(!xss)),HOLogic.realT)
neuper@37950
  1607
		   )
neuper@37950
  1608
	      )
neuper@37950
  1609
	 else
neuper@37950
  1610
	     (
neuper@37950
  1611
	      if hd(!xss)=1 then 
neuper@37950
  1612
		  ( 
neuper@38034
  1613
		   Const("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1614
		   Free(hd(!vv), HOLogic.realT) $
neuper@37950
  1615
		   powerproduct2term(tl(!xss),tl(!vv))
neuper@37950
  1616
		   )
neuper@37950
  1617
	      else
neuper@37950
  1618
		  (
neuper@38034
  1619
		   Const("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1620
		   (
neuper@37950
  1621
		    Const("Atools.pow",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1622
		    Free(hd(!vv), HOLogic.realT) $  Free(str_of_int (hd(!xss)),HOLogic.realT)
neuper@37950
  1623
		    ) $
neuper@37950
  1624
		    powerproduct2term(tl(!xss),tl(!vv))
neuper@37950
  1625
		   )
neuper@37950
  1626
	      )
neuper@37950
  1627
	 )
neuper@37950
  1628
    end;
neuper@37950
  1629
neuper@37950
  1630
(*. converts a monom into term representation .*)
neuper@37950
  1631
(*fun monom2term ((c,e):mv_monom, v:string list) = 
neuper@37950
  1632
    if c=0 then Free(str_of_int 0,HOLogic.realT)  
neuper@37950
  1633
    else
neuper@37950
  1634
	(
neuper@37950
  1635
	 if list_is_null(e) then
neuper@37950
  1636
	     ( 
neuper@37950
  1637
	      Free(str_of_int c,HOLogic.realT)  
neuper@37950
  1638
	      )
neuper@37950
  1639
	 else
neuper@37950
  1640
	     (
neuper@37950
  1641
	      if c=1 then 
neuper@37950
  1642
		  (
neuper@37950
  1643
		   powerproduct2term(e,v)
neuper@37950
  1644
		   )
neuper@37950
  1645
	      else
neuper@37950
  1646
		  (
neuper@38034
  1647
		   Const("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $
neuper@37950
  1648
		   Free(str_of_int c,HOLogic.realT)  $
neuper@37950
  1649
		   powerproduct2term(e,v)
neuper@37950
  1650
		   )
neuper@37950
  1651
		  )
neuper@37950
  1652
	     );*)
neuper@37950
  1653
neuper@37950
  1654
neuper@37950
  1655
(*fun monom2term ((i, is):mv_monom, v) = 
neuper@37950
  1656
    if list_is_null is 
neuper@37950
  1657
    then 
neuper@37950
  1658
	if i >= 0 
neuper@37950
  1659
	then Free (str_of_int i, HOLogic.realT)
neuper@37950
  1660
	else Const ("uminus", HOLogic.realT --> HOLogic.realT) $
neuper@37950
  1661
		   Free ((str_of_int o abs) i, HOLogic.realT)
neuper@37950
  1662
    else
neuper@37950
  1663
	if i > 0 
neuper@38034
  1664
	then Const ("Groups.times_class.times", [HOLogic.realT,HOLogic.realT]---> HOLogic.realT) $
neuper@37950
  1665
		   (Free (str_of_int i, HOLogic.realT)) $
neuper@37950
  1666
		   powerproduct2term(is, v)
neuper@38034
  1667
	else Const ("Groups.times_class.times", [HOLogic.realT,HOLogic.realT]---> HOLogic.realT) $
neuper@37950
  1668
		   (Const ("uminus", HOLogic.realT --> HOLogic.realT) $
neuper@37950
  1669
		   Free ((str_of_int o abs) i, HOLogic.realT)) $
neuper@37950
  1670
		   powerproduct2term(is, vs);---------------------------*)
neuper@37950
  1671
fun monom2term ((i, is) : mv_monom, vs) = 
neuper@37950
  1672
    if list_is_null is 
neuper@37950
  1673
    then Free (str_of_int i, HOLogic.realT)
neuper@37950
  1674
    else if i = 1
neuper@37950
  1675
    then powerproduct2term (is, vs)
neuper@38034
  1676
    else Const ("Groups.times_class.times", [HOLogic.realT, HOLogic.realT] ---> HOLogic.realT) $
neuper@37950
  1677
	       (Free (str_of_int i, HOLogic.realT)) $
neuper@37950
  1678
	       powerproduct2term (is, vs);
neuper@37950
  1679
    
neuper@37950
  1680
(*. converts the internal polynomial representation into an Isabelle term.*)
neuper@37950
  1681
fun poly2term' ([] : mv_poly, vs) = Free(str_of_int 0, HOLogic.realT)  
neuper@37950
  1682
  | poly2term' ([(c, e) : mv_monom], vs) = monom2term ((c, e), vs)
neuper@37950
  1683
  | poly2term' ((c, e) :: ces, vs) =  
neuper@38014
  1684
    Const("Groups.plus_class.plus", [HOLogic.realT, HOLogic.realT] ---> HOLogic.realT) $
neuper@37950
  1685
         poly2term (ces, vs) $ monom2term ((c, e), vs)
neuper@37950
  1686
and poly2term (xs, vs) = poly2term' (rev (sort (mv_geq LEX_) (xs)), vs);
neuper@37950
  1687
neuper@37950
  1688
neuper@37950
  1689
(*. converts a monom into term representation .*)
neuper@37950
  1690
(*. ignores the sign of the coefficients => use only for exp-poly functions .*)
neuper@37950
  1691
fun monom2term2((c,e):mv_monom, v:string list) =  
neuper@37950
  1692
    if c=0 then Free(str_of_int 0,HOLogic.realT)  
neuper@37950
  1693
    else
neuper@37950
  1694
	(
neuper@37950
  1695
	 if list_is_null(e) then
neuper@37950
  1696
	     ( 
neuper@37950
  1697
	      Free(str_of_int (abs(c)),HOLogic.realT)  
neuper@37950
  1698
	      )
neuper@37950
  1699
	 else
neuper@37950
  1700
	     (
neuper@37950
  1701
	      if abs(c)=1 then 
neuper@37950
  1702
		  (
neuper@37950
  1703
		   powerproduct2term(e,v)
neuper@37950
  1704
		   )
neuper@37950
  1705
	      else
neuper@37950
  1706
		  (
neuper@38034
  1707
		   Const("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $
neuper@37950
  1708
		   Free(str_of_int (abs(c)),HOLogic.realT)  $
neuper@37950
  1709
		   powerproduct2term(e,v)
neuper@37950
  1710
		   )
neuper@37950
  1711
		  )
neuper@37950
  1712
	     );
neuper@37950
  1713
neuper@37950
  1714
(*. converts the expanded polynomial representation into the term representation .*)
neuper@37950
  1715
fun exp2term' ([]:mv_poly,vars) =  Free(str_of_int 0,HOLogic.realT)  
neuper@37950
  1716
  | exp2term' ([(c,e)],vars) =     monom2term((c,e),vars) 			     
neuper@37950
  1717
  | exp2term' ((c1,e1)::others,vars) =  
neuper@37950
  1718
    if c1<0 then 	
neuper@38014
  1719
	Const("Groups.minus_class.minus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $
neuper@37950
  1720
	exp2term'(others,vars) $
neuper@37950
  1721
	( 
neuper@37950
  1722
	 monom2term2((c1,e1),vars)
neuper@37950
  1723
	 ) 
neuper@37950
  1724
    else
neuper@38014
  1725
	Const("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $
neuper@37950
  1726
	exp2term'(others,vars) $
neuper@37950
  1727
	( 
neuper@37950
  1728
	 monom2term2((c1,e1),vars)
neuper@37950
  1729
	 );
neuper@37950
  1730
	
neuper@37950
  1731
(*. sorts the powerproduct by lexicographic termorder and converts them into 
neuper@37950
  1732
    a term in polynomial representation .*)
neuper@37950
  1733
fun poly2expanded (xs,vars) = exp2term'(rev(sort (mv_geq LEX_) (xs)),vars);
neuper@37950
  1734
neuper@37950
  1735
(*. converts a polynomial into expanded form .*)
neuper@37950
  1736
fun polynomial2expanded t =  
neuper@37950
  1737
    (let 
neuper@37950
  1738
	val vars=(((map free2str) o vars) t);
neuper@37950
  1739
    in
neuper@37950
  1740
	SOME (poly2expanded (the (term2poly t vars), vars))
neuper@37950
  1741
    end) handle _ => NONE;
neuper@37950
  1742
neuper@37950
  1743
(*. converts a polynomial into polynomial form .*)
neuper@37950
  1744
fun expanded2polynomial t =  
neuper@37950
  1745
    (let 
neuper@37950
  1746
	val vars=(((map free2str) o vars) t);
neuper@37950
  1747
    in
neuper@37950
  1748
	SOME (poly2term (the (expanded2poly t vars), vars))
neuper@37950
  1749
    end) handle _ => NONE;
neuper@37950
  1750
neuper@37950
  1751
neuper@37950
  1752
(*. calculates the greatest common divisor of numerator and denominator and seperates it from each .*)
neuper@38014
  1753
fun step_cancel (t as Const ("Rings.inverse_class.divide",_) $ p1 $ p2) = 
neuper@37950
  1754
    let
neuper@38006
  1755
	val p1' = Unsynchronized.ref [];
neuper@38006
  1756
	val p2' = Unsynchronized.ref [];
neuper@38006
  1757
	val p3  = Unsynchronized.ref []
neuper@37950
  1758
	val vars = rev(get_vars(p1) union get_vars(p2));
neuper@37950
  1759
    in
neuper@37950
  1760
	(
neuper@37950
  1761
         p1':= sort (mv_geq LEX_) (the (term2poly p1 vars ));
neuper@37950
  1762
       	 p2':= sort (mv_geq LEX_) (the (term2poly p2 vars ));
neuper@37950
  1763
	 p3:= sort (mv_geq LEX_) (mv_gcd (!p1') (!p2'));
neuper@37950
  1764
	 if (!p3)=[(1,mv_null2(vars))] then 
neuper@37950
  1765
	     (
neuper@38014
  1766
	      Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ p1 $ p2
neuper@37950
  1767
	      )
neuper@37950
  1768
	 else
neuper@37950
  1769
	     (
neuper@37950
  1770
neuper@37950
  1771
	      p1':=sort (mv_geq LEX_) (#1(mv_division((!p1'),(!p3),LEX_)));
neuper@37950
  1772
	      p2':=sort (mv_geq LEX_) (#1(mv_division((!p2'),(!p3),LEX_)));
neuper@37950
  1773
	      
neuper@37950
  1774
	      if #1(hd(sort (mv_geq LEX_) (!p2'))) (*mv_lc2(!p2',LEX_)*)>0 then
neuper@37950
  1775
	      (
neuper@38014
  1776
	       Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  1777
	       $ 
neuper@37950
  1778
	       (
neuper@38034
  1779
		Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1780
		poly2term(!p1',vars) $ 
neuper@37950
  1781
		poly2term(!p3,vars) 
neuper@37950
  1782
		) 
neuper@37950
  1783
	       $ 
neuper@37950
  1784
	       (
neuper@38034
  1785
		Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1786
		poly2term(!p2',vars) $ 
neuper@37950
  1787
		poly2term(!p3,vars)
neuper@37950
  1788
		) 	
neuper@37950
  1789
	       )	
neuper@37950
  1790
	      else
neuper@37950
  1791
	      (
neuper@37950
  1792
	       p1':=mv_skalar_mul(!p1',~1);
neuper@37950
  1793
	       p2':=mv_skalar_mul(!p2',~1);
neuper@37950
  1794
	       p3:=mv_skalar_mul(!p3,~1);
neuper@37950
  1795
	       (
neuper@38014
  1796
		Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  1797
		$ 
neuper@37950
  1798
		(
neuper@38034
  1799
		 Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1800
		 poly2term(!p1',vars) $ 
neuper@37950
  1801
		 poly2term(!p3,vars) 
neuper@37950
  1802
		 ) 
neuper@37950
  1803
		$ 
neuper@37950
  1804
		(
neuper@38034
  1805
		 Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1806
		 poly2term(!p2',vars) $ 
neuper@37950
  1807
		 poly2term(!p3,vars)
neuper@37950
  1808
		 ) 	
neuper@37950
  1809
		)	
neuper@37950
  1810
	       )	  
neuper@37950
  1811
	      )
neuper@37950
  1812
	     )
neuper@37950
  1813
    end
neuper@38031
  1814
| step_cancel _ = error ("RATIONALS_STEP_CANCEL_EXCEPTION: Invalid fraction"); 
neuper@37950
  1815
neuper@37950
  1816
neuper@37950
  1817
(*. same as step_cancel, this time for expanded forms (input+output) .*)
neuper@38014
  1818
fun step_cancel_expanded (t as Const ("Rings.inverse_class.divide",_) $ p1 $ p2) = 
neuper@37950
  1819
    let
neuper@38006
  1820
	val p1' = Unsynchronized.ref [];
neuper@38006
  1821
	val p2' = Unsynchronized.ref [];
neuper@38006
  1822
	val p3  = Unsynchronized.ref []
neuper@37950
  1823
	val vars = rev(get_vars(p1) union get_vars(p2));
neuper@37950
  1824
    in
neuper@37950
  1825
	(
neuper@37950
  1826
         p1':= sort (mv_geq LEX_) (the (expanded2poly p1 vars ));
neuper@37950
  1827
       	 p2':= sort (mv_geq LEX_) (the (expanded2poly p2 vars ));
neuper@37950
  1828
	 p3:= sort (mv_geq LEX_) (mv_gcd (!p1') (!p2'));
neuper@37950
  1829
	 if (!p3)=[(1,mv_null2(vars))] then 
neuper@37950
  1830
	     (
neuper@38014
  1831
	      Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ p1 $ p2
neuper@37950
  1832
	      )
neuper@37950
  1833
	 else
neuper@37950
  1834
	     (
neuper@37950
  1835
neuper@37950
  1836
	      p1':=sort (mv_geq LEX_) (#1(mv_division((!p1'),(!p3),LEX_)));
neuper@37950
  1837
	      p2':=sort (mv_geq LEX_) (#1(mv_division((!p2'),(!p3),LEX_)));
neuper@37950
  1838
	      
neuper@37950
  1839
	      if #1(hd(sort (mv_geq LEX_) (!p2')))(* mv_lc2(!p2',LEX_)*)>0 then
neuper@37950
  1840
	      (
neuper@38014
  1841
	       Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  1842
	       $ 
neuper@37950
  1843
	       (
neuper@38034
  1844
		Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1845
		poly2expanded(!p1',vars) $ 
neuper@37950
  1846
		poly2expanded(!p3,vars) 
neuper@37950
  1847
		) 
neuper@37950
  1848
	       $ 
neuper@37950
  1849
	       (
neuper@38034
  1850
		Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1851
		poly2expanded(!p2',vars) $ 
neuper@37950
  1852
		poly2expanded(!p3,vars)
neuper@37950
  1853
		) 	
neuper@37950
  1854
	       )	
neuper@37950
  1855
	      else
neuper@37950
  1856
	      (
neuper@37950
  1857
	       p1':=mv_skalar_mul(!p1',~1);
neuper@37950
  1858
	       p2':=mv_skalar_mul(!p2',~1);
neuper@37950
  1859
	       p3:=mv_skalar_mul(!p3,~1);
neuper@37950
  1860
	       (
neuper@38014
  1861
		Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  1862
		$ 
neuper@37950
  1863
		(
neuper@38034
  1864
		 Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1865
		 poly2expanded(!p1',vars) $ 
neuper@37950
  1866
		 poly2expanded(!p3,vars) 
neuper@37950
  1867
		 ) 
neuper@37950
  1868
		$ 
neuper@37950
  1869
		(
neuper@38034
  1870
		 Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  1871
		 poly2expanded(!p2',vars) $ 
neuper@37950
  1872
		 poly2expanded(!p3,vars)
neuper@37950
  1873
		 ) 	
neuper@37950
  1874
		)	
neuper@37950
  1875
	       )	  
neuper@37950
  1876
	      )
neuper@37950
  1877
	     )
neuper@37950
  1878
    end
neuper@38031
  1879
| step_cancel_expanded _ = error ("RATIONALS_STEP_CANCEL_EXCEPTION: Invalid fraction"); 
neuper@37950
  1880
neuper@37950
  1881
(*. calculates the greatest common divisor of numerator and denominator and divides each through it .*)
neuper@38014
  1882
fun direct_cancel (t as Const ("Rings.inverse_class.divide",_) $ p1 $ p2) = 
neuper@37950
  1883
    let
neuper@38006
  1884
	val p1' = Unsynchronized.ref [];
neuper@38006
  1885
	val p2' = Unsynchronized.ref [];
neuper@38006
  1886
	val p3  = Unsynchronized.ref []
neuper@37950
  1887
	val vars = rev(get_vars(p1) union get_vars(p2));
neuper@37950
  1888
    in
neuper@37950
  1889
	(
neuper@37950
  1890
	 p1':=sort (mv_geq LEX_) (mv_shorten((the (term2poly p1 vars )),LEX_));
neuper@37950
  1891
	 p2':=sort (mv_geq LEX_) (mv_shorten((the (term2poly p2 vars )),LEX_));	 
neuper@37950
  1892
	 p3 :=sort (mv_geq LEX_) (mv_gcd (!p1') (!p2'));
neuper@37950
  1893
neuper@37950
  1894
	 if (!p3)=[(1,mv_null2(vars))] then 
neuper@37950
  1895
	     (
neuper@38014
  1896
	      (Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ p1 $ p2,[])
neuper@37950
  1897
	      )
neuper@37950
  1898
	 else
neuper@37950
  1899
	     (
neuper@37950
  1900
	      p1':=sort (mv_geq LEX_) (#1(mv_division((!p1'),(!p3),LEX_)));
neuper@37950
  1901
	      p2':=sort (mv_geq LEX_) (#1(mv_division((!p2'),(!p3),LEX_)));
neuper@37950
  1902
	      if #1(hd(sort (mv_geq LEX_) (!p2'))) (*mv_lc2(!p2',LEX_)*)>0 then	      
neuper@37950
  1903
	      (
neuper@37950
  1904
	       (
neuper@38014
  1905
		Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  1906
		$ 
neuper@37950
  1907
		(
neuper@37950
  1908
		 poly2term((!p1'),vars)
neuper@37950
  1909
		 ) 
neuper@37950
  1910
		$ 
neuper@37950
  1911
		( 
neuper@37950
  1912
		 poly2term((!p2'),vars)
neuper@37950
  1913
		 ) 	
neuper@37950
  1914
		)
neuper@37950
  1915
	       ,
neuper@37950
  1916
	       if mv_grad(!p3)>0 then 
neuper@37950
  1917
		   [
neuper@37950
  1918
		    (
neuper@41929
  1919
		     Const ("HOL.Not",[bool]--->bool) $
neuper@37950
  1920
		     (
neuper@41922
  1921
		      Const("HOL.eq",[HOLogic.realT,HOLogic.realT]--->bool) $
neuper@37950
  1922
		      poly2term((!p3),vars) $
neuper@37950
  1923
		      Free("0",HOLogic.realT)
neuper@37950
  1924
		      )
neuper@37950
  1925
		     )
neuper@37950
  1926
		    ]
neuper@37950
  1927
	       else
neuper@37950
  1928
		   []
neuper@37950
  1929
		   )
neuper@37950
  1930
	      else
neuper@37950
  1931
		  (
neuper@37950
  1932
		   p1':=mv_skalar_mul(!p1',~1);
neuper@37950
  1933
		   p2':=mv_skalar_mul(!p2',~1);
neuper@37950
  1934
		   if length(!p3)> 2*(count_neg(!p3)) then () else p3 :=mv_skalar_mul(!p3,~1); 
neuper@37950
  1935
		       (
neuper@38014
  1936
			Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  1937
			$ 
neuper@37950
  1938
			(
neuper@37950
  1939
			 poly2term((!p1'),vars)
neuper@37950
  1940
			 ) 
neuper@37950
  1941
			$ 
neuper@37950
  1942
			( 
neuper@37950
  1943
			 poly2term((!p2'),vars)
neuper@37950
  1944
			 ) 	
neuper@37950
  1945
			,
neuper@37950
  1946
			if mv_grad(!p3)>0 then 
neuper@37950
  1947
			    [
neuper@37950
  1948
			     (
neuper@41929
  1949
			      Const ("HOL.Not",[bool]--->bool) $
neuper@37950
  1950
			      (
neuper@41922
  1951
			       Const("HOL.eq",[HOLogic.realT,HOLogic.realT]--->bool) $
neuper@37950
  1952
			       poly2term((!p3),vars) $
neuper@37950
  1953
			       Free("0",HOLogic.realT)
neuper@37950
  1954
			       )
neuper@37950
  1955
			      )
neuper@37950
  1956
			     ]
neuper@37950
  1957
			else
neuper@37950
  1958
			    []
neuper@37950
  1959
			    )
neuper@37950
  1960
		       )
neuper@37950
  1961
		  )
neuper@37950
  1962
	     )
neuper@37950
  1963
    end
neuper@38031
  1964
  | direct_cancel _ = error ("RATIONALS_DIRECT_CANCEL_EXCEPTION: Invalid fraction"); 
neuper@37950
  1965
neuper@37950
  1966
(*. same es direct_cancel, this time for expanded forms (input+output).*) 
neuper@38014
  1967
fun direct_cancel_expanded (t as Const ("Rings.inverse_class.divide",_) $ p1 $ p2) =  
neuper@37950
  1968
    let
neuper@38006
  1969
	val p1' = Unsynchronized.ref [];
neuper@38006
  1970
	val p2' = Unsynchronized.ref [];
neuper@38006
  1971
	val p3  = Unsynchronized.ref []
neuper@37950
  1972
	val vars = rev(get_vars(p1) union get_vars(p2));
neuper@37950
  1973
    in
neuper@37950
  1974
	(
neuper@37950
  1975
	 p1':=sort (mv_geq LEX_) (mv_shorten((the (expanded2poly p1 vars )),LEX_));
neuper@37950
  1976
	 p2':=sort (mv_geq LEX_) (mv_shorten((the (expanded2poly p2 vars )),LEX_));	 
neuper@37950
  1977
	 p3 :=sort (mv_geq LEX_) (mv_gcd (!p1') (!p2'));
neuper@37950
  1978
neuper@37950
  1979
	 if (!p3)=[(1,mv_null2(vars))] then 
neuper@37950
  1980
	     (
neuper@38014
  1981
	      (Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ p1 $ p2,[])
neuper@37950
  1982
	      )
neuper@37950
  1983
	 else
neuper@37950
  1984
	     (
neuper@37950
  1985
	      p1':=sort (mv_geq LEX_) (#1(mv_division((!p1'),(!p3),LEX_)));
neuper@37950
  1986
	      p2':=sort (mv_geq LEX_) (#1(mv_division((!p2'),(!p3),LEX_)));
neuper@37950
  1987
	      if #1(hd(sort (mv_geq LEX_) (!p2'))) (*mv_lc2(!p2',LEX_)*)>0 then	      
neuper@37950
  1988
	      (
neuper@37950
  1989
	       (
neuper@38014
  1990
		Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  1991
		$ 
neuper@37950
  1992
		(
neuper@37950
  1993
		 poly2expanded((!p1'),vars)
neuper@37950
  1994
		 ) 
neuper@37950
  1995
		$ 
neuper@37950
  1996
		( 
neuper@37950
  1997
		 poly2expanded((!p2'),vars)
neuper@37950
  1998
		 ) 	
neuper@37950
  1999
		)
neuper@37950
  2000
	       ,
neuper@37950
  2001
	       if mv_grad(!p3)>0 then 
neuper@37950
  2002
		   [
neuper@37950
  2003
		    (
neuper@41929
  2004
		     Const ("HOL.Not",[bool]--->bool) $
neuper@37950
  2005
		     (
neuper@41922
  2006
		      Const("HOL.eq",[HOLogic.realT,HOLogic.realT]--->bool) $
neuper@37950
  2007
		      poly2expanded((!p3),vars) $
neuper@37950
  2008
		      Free("0",HOLogic.realT)
neuper@37950
  2009
		      )
neuper@37950
  2010
		     )
neuper@37950
  2011
		    ]
neuper@37950
  2012
	       else
neuper@37950
  2013
		   []
neuper@37950
  2014
		   )
neuper@37950
  2015
	      else
neuper@37950
  2016
		  (
neuper@37950
  2017
		   p1':=mv_skalar_mul(!p1',~1);
neuper@37950
  2018
		   p2':=mv_skalar_mul(!p2',~1);
neuper@37950
  2019
		   if length(!p3)> 2*(count_neg(!p3)) then () else p3 :=mv_skalar_mul(!p3,~1); 
neuper@37950
  2020
		       (
neuper@38014
  2021
			Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2022
			$ 
neuper@37950
  2023
			(
neuper@37950
  2024
			 poly2expanded((!p1'),vars)
neuper@37950
  2025
			 ) 
neuper@37950
  2026
			$ 
neuper@37950
  2027
			( 
neuper@37950
  2028
			 poly2expanded((!p2'),vars)
neuper@37950
  2029
			 ) 	
neuper@37950
  2030
			,
neuper@37950
  2031
			if mv_grad(!p3)>0 then 
neuper@37950
  2032
			    [
neuper@37950
  2033
			     (
neuper@41929
  2034
			      Const ("HOL.Not",[bool]--->bool) $
neuper@37950
  2035
			      (
neuper@41922
  2036
			       Const("HOL.eq",[HOLogic.realT,HOLogic.realT]--->bool) $
neuper@37950
  2037
			       poly2expanded((!p3),vars) $
neuper@37950
  2038
			       Free("0",HOLogic.realT)
neuper@37950
  2039
			       )
neuper@37950
  2040
			      )
neuper@37950
  2041
			     ]
neuper@37950
  2042
			else
neuper@37950
  2043
			    []
neuper@37950
  2044
			    )
neuper@37950
  2045
		       )
neuper@37950
  2046
		  )
neuper@37950
  2047
	     )
neuper@37950
  2048
    end
neuper@38031
  2049
  | direct_cancel_expanded _ = error ("RATIONALS_DIRECT_CANCEL_EXCEPTION: Invalid fraction"); 
neuper@37950
  2050
neuper@37950
  2051
neuper@37950
  2052
(*. adds two fractions .*)
neuper@38014
  2053
fun add_fract ((Const("Rings.inverse_class.divide",_) $ t11 $ t12),(Const("Rings.inverse_class.divide",_) $ t21 $ t22)) =
neuper@37950
  2054
    let
neuper@37950
  2055
	val vars=get_vars(t11) union get_vars(t12) union get_vars(t21) union get_vars(t22);
neuper@38006
  2056
	val t11'= Unsynchronized.ref  (the(term2poly t11 vars));
neuper@38015
  2057
val _= tracing"### add_fract: done t11"
neuper@38006
  2058
	val t12'= Unsynchronized.ref  (the(term2poly t12 vars));
neuper@38015
  2059
val _= tracing"### add_fract: done t12"
neuper@38006
  2060
	val t21'= Unsynchronized.ref  (the(term2poly t21 vars));
neuper@38015
  2061
val _= tracing"### add_fract: done t21"
neuper@38006
  2062
	val t22'= Unsynchronized.ref  (the(term2poly t22 vars));
neuper@38015
  2063
val _= tracing"### add_fract: done t22"
neuper@38006
  2064
	val den= Unsynchronized.ref  [];
neuper@38006
  2065
	val nom= Unsynchronized.ref  [];
neuper@38006
  2066
	val m1= Unsynchronized.ref  [];
neuper@38006
  2067
	val m2= Unsynchronized.ref  [];
neuper@37950
  2068
    in
neuper@37950
  2069
	
neuper@37950
  2070
	(
neuper@37950
  2071
	 den :=sort (mv_geq LEX_) (mv_lcm (!t12') (!t22'));
neuper@38015
  2072
tracing"### add_fract: done sort mv_lcm";
neuper@37950
  2073
	 m1  :=sort (mv_geq LEX_) (#1(mv_division(!den,!t12',LEX_)));
neuper@38015
  2074
tracing"### add_fract: done sort mv_division t12";
neuper@37950
  2075
	 m2  :=sort (mv_geq LEX_) (#1(mv_division(!den,!t22',LEX_)));
neuper@38015
  2076
tracing"### add_fract: done sort mv_division t22";
neuper@37950
  2077
	 nom :=sort (mv_geq LEX_) 
neuper@37950
  2078
		    (mv_shorten(mv_add(mv_mul(!t11',!m1,LEX_),
neuper@37950
  2079
				       mv_mul(!t21',!m2,LEX_),
neuper@37950
  2080
				       LEX_),
neuper@37950
  2081
				LEX_));
neuper@38015
  2082
tracing"### add_fract: done sort mv_add";
neuper@37950
  2083
	 (
neuper@38014
  2084
	  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2085
	  $ 
neuper@37950
  2086
	  (
neuper@37950
  2087
	   poly2term((!nom),vars)
neuper@37950
  2088
	   ) 
neuper@37950
  2089
	  $ 
neuper@37950
  2090
	  ( 
neuper@37950
  2091
	   poly2term((!den),vars)
neuper@37950
  2092
	   )	      
neuper@37950
  2093
	  )
neuper@37950
  2094
	 )	     
neuper@37950
  2095
    end 
neuper@38031
  2096
  | add_fract (_,_) = error ("RATIONALS_ADD_FRACTION_EXCEPTION: Invalid add_fraction call");
neuper@37950
  2097
neuper@37950
  2098
(*. adds two expanded fractions .*)
neuper@38014
  2099
fun add_fract_exp ((Const("Rings.inverse_class.divide",_) $ t11 $ t12),(Const("Rings.inverse_class.divide",_) $ t21 $ t22)) =
neuper@37950
  2100
    let
neuper@37950
  2101
	val vars=get_vars(t11) union get_vars(t12) union get_vars(t21) union get_vars(t22);
neuper@38006
  2102
	val t11'= Unsynchronized.ref  (the(expanded2poly t11 vars));
neuper@38006
  2103
	val t12'= Unsynchronized.ref  (the(expanded2poly t12 vars));
neuper@38006
  2104
	val t21'= Unsynchronized.ref  (the(expanded2poly t21 vars));
neuper@38006
  2105
	val t22'= Unsynchronized.ref  (the(expanded2poly t22 vars));
neuper@38006
  2106
	val den= Unsynchronized.ref  [];
neuper@38006
  2107
	val nom= Unsynchronized.ref  [];
neuper@38006
  2108
	val m1= Unsynchronized.ref  [];
neuper@38006
  2109
	val m2= Unsynchronized.ref  [];
neuper@37950
  2110
    in
neuper@37950
  2111
	
neuper@37950
  2112
	(
neuper@37950
  2113
	 den :=sort (mv_geq LEX_) (mv_lcm (!t12') (!t22'));
neuper@37950
  2114
	 m1  :=sort (mv_geq LEX_) (#1(mv_division(!den,!t12',LEX_)));
neuper@37950
  2115
	 m2  :=sort (mv_geq LEX_) (#1(mv_division(!den,!t22',LEX_)));
neuper@37950
  2116
	 nom :=sort (mv_geq LEX_) (mv_shorten(mv_add(mv_mul(!t11',!m1,LEX_),mv_mul(!t21',!m2,LEX_),LEX_),LEX_));
neuper@37950
  2117
	 (
neuper@38014
  2118
	  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2119
	  $ 
neuper@37950
  2120
	  (
neuper@37950
  2121
	   poly2expanded((!nom),vars)
neuper@37950
  2122
	   ) 
neuper@37950
  2123
	  $ 
neuper@37950
  2124
	  ( 
neuper@37950
  2125
	   poly2expanded((!den),vars)
neuper@37950
  2126
	   )	      
neuper@37950
  2127
	  )
neuper@37950
  2128
	 )	     
neuper@37950
  2129
    end 
neuper@38031
  2130
  | add_fract_exp (_,_) = error ("RATIONALS_ADD_FRACTION_EXP_EXCEPTION: Invalid add_fraction call");
neuper@37950
  2131
neuper@37950
  2132
(*. adds a list of terms .*)
neuper@37950
  2133
fun add_list_of_fractions []= (Free("0",HOLogic.realT),[])
neuper@37950
  2134
  | add_list_of_fractions [x]= direct_cancel x
neuper@37950
  2135
  | add_list_of_fractions (x::y::xs) = 
neuper@37950
  2136
    let
neuper@37950
  2137
	val (t1a,rest1)=direct_cancel(x);
neuper@38015
  2138
val _= tracing"### add_list_of_fractions xs: has done direct_cancel(x)";
neuper@37950
  2139
	val (t2a,rest2)=direct_cancel(y);
neuper@38015
  2140
val _= tracing"### add_list_of_fractions xs: has done direct_cancel(y)";
neuper@37950
  2141
	val (t3a,rest3)=(add_list_of_fractions (add_fract(t1a,t2a)::xs));
neuper@38015
  2142
val _= tracing"### add_list_of_fractions xs: has done add_list_of_fraction xs";
neuper@37950
  2143
	val (t4a,rest4)=direct_cancel(t3a);
neuper@38015
  2144
val _= tracing"### add_list_of_fractions xs: has done direct_cancel(t3a)";
neuper@37950
  2145
	val rest=rest1 union rest2 union rest3 union rest4;
neuper@37950
  2146
    in
neuper@38015
  2147
	(tracing"### add_list_of_fractions in";
neuper@37950
  2148
	 (
neuper@37950
  2149
	 (t4a,rest) 
neuper@37950
  2150
	 )
neuper@37950
  2151
	 )
neuper@37950
  2152
    end;
neuper@37950
  2153
neuper@37950
  2154
(*. adds a list of expanded terms .*)
neuper@37950
  2155
fun add_list_of_fractions_exp []= (Free("0",HOLogic.realT),[])
neuper@37950
  2156
  | add_list_of_fractions_exp [x]= direct_cancel_expanded x
neuper@37950
  2157
  | add_list_of_fractions_exp (x::y::xs) = 
neuper@37950
  2158
    let
neuper@37950
  2159
	val (t1a,rest1)=direct_cancel_expanded(x);
neuper@37950
  2160
	val (t2a,rest2)=direct_cancel_expanded(y);
neuper@37950
  2161
	val (t3a,rest3)=(add_list_of_fractions_exp (add_fract_exp(t1a,t2a)::xs));
neuper@37950
  2162
	val (t4a,rest4)=direct_cancel_expanded(t3a);
neuper@37950
  2163
	val rest=rest1 union rest2 union rest3 union rest4;
neuper@37950
  2164
    in
neuper@37950
  2165
	(
neuper@37950
  2166
	 (t4a,rest) 
neuper@37950
  2167
	 )
neuper@37950
  2168
    end;
neuper@37950
  2169
neuper@37950
  2170
(*. calculates the lcm of a list of mv_poly .*)
neuper@37950
  2171
fun calc_lcm ([x],var)= (x,var) 
neuper@37950
  2172
  | calc_lcm ((x::xs),var) = (mv_lcm x (#1(calc_lcm (xs,var))),var);
neuper@37950
  2173
neuper@37950
  2174
(*. converts a list of terms to a list of mv_poly .*)
neuper@37950
  2175
fun t2d([],_)=[] 
neuper@38014
  2176
  | t2d((t as (Const("Rings.inverse_class.divide",_) $ p1 $ p2))::xs,vars)= (the(term2poly p2 vars)) :: t2d(xs,vars); 
neuper@37950
  2177
neuper@37950
  2178
(*. same as t2d, this time for expanded forms .*)
neuper@37950
  2179
fun t2d_exp([],_)=[]  
neuper@38014
  2180
  | t2d_exp((t as (Const("Rings.inverse_class.divide",_) $ p1 $ p2))::xs,vars)= (the(expanded2poly p2 vars)) :: t2d_exp(xs,vars);
neuper@37950
  2181
neuper@37950
  2182
(*. converts a list of fract terms to a list of their denominators .*)
neuper@37950
  2183
fun termlist2denominators [] = ([],[])
neuper@37950
  2184
  | termlist2denominators xs = 
neuper@37950
  2185
    let	
neuper@38006
  2186
	val xxs= Unsynchronized.ref  xs;
neuper@38006
  2187
	val var= Unsynchronized.ref  [];
neuper@37950
  2188
    in
neuper@37950
  2189
	var:=[];
neuper@37950
  2190
	while length(!xxs)>0 do
neuper@37950
  2191
	    (
neuper@37950
  2192
	     let 
neuper@38014
  2193
		 val (t as Const ("Rings.inverse_class.divide",_) $ p1x $ p2x)=hd(!xxs);
neuper@37950
  2194
	     in
neuper@37950
  2195
		 (
neuper@37950
  2196
		  xxs:=tl(!xxs);
neuper@37950
  2197
		  var:=((get_vars(p2x)) union (get_vars(p1x)) union (!var))
neuper@37950
  2198
		  )
neuper@37950
  2199
	     end
neuper@37950
  2200
	     );
neuper@37950
  2201
	    (t2d(xs,!var),!var)
neuper@37950
  2202
    end;
neuper@37950
  2203
neuper@37950
  2204
(*. calculates the lcm of a list of mv_poly .*)
neuper@37950
  2205
fun calc_lcm ([x],var)= (x,var) 
neuper@37950
  2206
  | calc_lcm ((x::xs),var) = (mv_lcm x (#1(calc_lcm (xs,var))),var);
neuper@37950
  2207
neuper@37950
  2208
(*. converts a list of terms to a list of mv_poly .*)
neuper@37950
  2209
fun t2d([],_)=[] 
neuper@38014
  2210
  | t2d((t as (Const("Rings.inverse_class.divide",_) $ p1 $ p2))::xs,vars)= (the(term2poly p2 vars)) :: t2d(xs,vars); 
neuper@37950
  2211
neuper@37950
  2212
(*. same as t2d, this time for expanded forms .*)
neuper@37950
  2213
fun t2d_exp([],_)=[]  
neuper@38014
  2214
  | t2d_exp((t as (Const("Rings.inverse_class.divide",_) $ p1 $ p2))::xs,vars)= (the(expanded2poly p2 vars)) :: t2d_exp(xs,vars);
neuper@37950
  2215
neuper@37950
  2216
(*. converts a list of fract terms to a list of their denominators .*)
neuper@37950
  2217
fun termlist2denominators [] = ([],[])
neuper@37950
  2218
  | termlist2denominators xs = 
neuper@37950
  2219
    let	
neuper@38006
  2220
	val xxs= Unsynchronized.ref  xs;
neuper@38006
  2221
	val var= Unsynchronized.ref  [];
neuper@37950
  2222
    in
neuper@37950
  2223
	var:=[];
neuper@37950
  2224
	while length(!xxs)>0 do
neuper@37950
  2225
	    (
neuper@37950
  2226
	     let 
neuper@38014
  2227
		 val (t as Const ("Rings.inverse_class.divide",_) $ p1x $ p2x)=hd(!xxs);
neuper@37950
  2228
	     in
neuper@37950
  2229
		 (
neuper@37950
  2230
		  xxs:=tl(!xxs);
neuper@37950
  2231
		  var:=((get_vars(p2x)) union (get_vars(p1x)) union (!var))
neuper@37950
  2232
		  )
neuper@37950
  2233
	     end
neuper@37950
  2234
	     );
neuper@37950
  2235
	    (t2d(xs,!var),!var)
neuper@37950
  2236
    end;
neuper@37950
  2237
neuper@37950
  2238
(*. same as termlist2denminators, this time for expanded forms .*)
neuper@37950
  2239
fun termlist2denominators_exp [] = ([],[])
neuper@37950
  2240
  | termlist2denominators_exp xs = 
neuper@37950
  2241
    let	
neuper@38006
  2242
	val xxs= Unsynchronized.ref  xs;
neuper@38006
  2243
	val var= Unsynchronized.ref  [];
neuper@37950
  2244
    in
neuper@37950
  2245
	var:=[];
neuper@37950
  2246
	while length(!xxs)>0 do
neuper@37950
  2247
	    (
neuper@37950
  2248
	     let 
neuper@38014
  2249
		 val (t as Const ("Rings.inverse_class.divide",_) $ p1x $ p2x)=hd(!xxs);
neuper@37950
  2250
	     in
neuper@37950
  2251
		 (
neuper@37950
  2252
		  xxs:=tl(!xxs);
neuper@37950
  2253
		  var:=((get_vars(p2x)) union (get_vars(p1x)) union (!var))
neuper@37950
  2254
		  )
neuper@37950
  2255
	     end
neuper@37950
  2256
	     );
neuper@37950
  2257
	    (t2d_exp(xs,!var),!var)
neuper@37950
  2258
    end;
neuper@37950
  2259
neuper@37950
  2260
(*. reduces all fractions to the least common denominator .*)
neuper@37950
  2261
fun com_den(x::xs,denom,den,var)=
neuper@37950
  2262
    let 
neuper@38014
  2263
	val (t as Const ("Rings.inverse_class.divide",_) $ p1' $ p2')=x;
neuper@37950
  2264
	val p2= sort (mv_geq LEX_) (the(term2poly p2' var));
neuper@37950
  2265
	val p3= #1(mv_division(denom,p2,LEX_));
neuper@37950
  2266
	val p1var=get_vars(p1');
neuper@37950
  2267
    in     
neuper@37950
  2268
	if length(xs)>0 then 
neuper@37950
  2269
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2270
		(
neuper@38014
  2271
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT)
neuper@37950
  2272
		 $ 
neuper@37950
  2273
		 (
neuper@38014
  2274
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2275
		  $ 
neuper@37950
  2276
		  poly2term(the (term2poly p1' p1var),p1var)
neuper@37950
  2277
		  $ 
neuper@37950
  2278
		  den	
neuper@37950
  2279
		  )    
neuper@37950
  2280
		 $ 
neuper@37950
  2281
		 #1(com_den(xs,denom,den,var))
neuper@37950
  2282
		,
neuper@37950
  2283
		[]
neuper@37950
  2284
		)
neuper@37950
  2285
	    else
neuper@37950
  2286
		(
neuper@38014
  2287
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2288
		 $ 
neuper@37950
  2289
		 (
neuper@38014
  2290
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2291
		  $ 
neuper@37950
  2292
		  (
neuper@38034
  2293
		   Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2294
		   poly2term(the (term2poly p1' p1var),p1var) $ 
neuper@37950
  2295
		   poly2term(p3,var)
neuper@37950
  2296
		   ) 
neuper@37950
  2297
		  $ 
neuper@37950
  2298
		  (
neuper@37950
  2299
		   den
neuper@37950
  2300
		   ) 	
neuper@37950
  2301
		  )
neuper@37950
  2302
		 $ 
neuper@37950
  2303
		 #1(com_den(xs,denom,den,var))
neuper@37950
  2304
		,
neuper@37950
  2305
		[]
neuper@37950
  2306
		)
neuper@37950
  2307
	else
neuper@37950
  2308
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2309
		(
neuper@37950
  2310
		 (
neuper@38014
  2311
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2312
		  $ 
neuper@37950
  2313
		  poly2term(the (term2poly p1' p1var),p1var)
neuper@37950
  2314
		  $ 
neuper@37950
  2315
		  den	
neuper@37950
  2316
		  )
neuper@37950
  2317
		 ,
neuper@37950
  2318
		 []
neuper@37950
  2319
		 )
neuper@37950
  2320
	     else
neuper@37950
  2321
		 (
neuper@38014
  2322
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2323
		  $ 
neuper@37950
  2324
		  (
neuper@38034
  2325
		   Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2326
		   poly2term(the (term2poly p1' p1var),p1var) $ 
neuper@37950
  2327
		   poly2term(p3,var)
neuper@37950
  2328
		   ) 
neuper@37950
  2329
		  $ 
neuper@37950
  2330
		  den 	
neuper@37950
  2331
		  ,
neuper@37950
  2332
		  []
neuper@37950
  2333
		  )
neuper@37950
  2334
    end;
neuper@37950
  2335
neuper@37950
  2336
(*. same as com_den, this time for expanded forms .*)
neuper@37950
  2337
fun com_den_exp(x::xs,denom,den,var)=
neuper@37950
  2338
    let 
neuper@38014
  2339
	val (t as Const ("Rings.inverse_class.divide",_) $ p1' $ p2')=x;
neuper@37950
  2340
	val p2= sort (mv_geq LEX_) (the(expanded2poly p2' var));
neuper@37950
  2341
	val p3= #1(mv_division(denom,p2,LEX_));
neuper@37950
  2342
	val p1var=get_vars(p1');
neuper@37950
  2343
    in     
neuper@37950
  2344
	if length(xs)>0 then 
neuper@37950
  2345
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2346
		(
neuper@38014
  2347
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT)
neuper@37950
  2348
		 $ 
neuper@37950
  2349
		 (
neuper@38014
  2350
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2351
		  $ 
neuper@37950
  2352
		  poly2expanded(the(expanded2poly p1' p1var),p1var)
neuper@37950
  2353
		  $ 
neuper@37950
  2354
		  den	
neuper@37950
  2355
		  )    
neuper@37950
  2356
		 $ 
neuper@37950
  2357
		 #1(com_den_exp(xs,denom,den,var))
neuper@37950
  2358
		,
neuper@37950
  2359
		[]
neuper@37950
  2360
		)
neuper@37950
  2361
	    else
neuper@37950
  2362
		(
neuper@38014
  2363
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2364
		 $ 
neuper@37950
  2365
		 (
neuper@38014
  2366
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2367
		  $ 
neuper@37950
  2368
		  (
neuper@38034
  2369
		   Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2370
		   poly2expanded(the(expanded2poly p1' p1var),p1var) $ 
neuper@37950
  2371
		   poly2expanded(p3,var)
neuper@37950
  2372
		   ) 
neuper@37950
  2373
		  $ 
neuper@37950
  2374
		  (
neuper@37950
  2375
		   den
neuper@37950
  2376
		   ) 	
neuper@37950
  2377
		  )
neuper@37950
  2378
		 $ 
neuper@37950
  2379
		 #1(com_den_exp(xs,denom,den,var))
neuper@37950
  2380
		,
neuper@37950
  2381
		[]
neuper@37950
  2382
		)
neuper@37950
  2383
	else
neuper@37950
  2384
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2385
		(
neuper@37950
  2386
		 (
neuper@38014
  2387
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2388
		  $ 
neuper@37950
  2389
		  poly2expanded(the(expanded2poly p1' p1var),p1var)
neuper@37950
  2390
		  $ 
neuper@37950
  2391
		  den	
neuper@37950
  2392
		  )
neuper@37950
  2393
		 ,
neuper@37950
  2394
		 []
neuper@37950
  2395
		 )
neuper@37950
  2396
	     else
neuper@37950
  2397
		 (
neuper@38014
  2398
		  Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) 
neuper@37950
  2399
		  $ 
neuper@37950
  2400
		  (
neuper@38034
  2401
		   Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2402
		   poly2expanded(the(expanded2poly p1' p1var),p1var) $ 
neuper@37950
  2403
		   poly2expanded(p3,var)
neuper@37950
  2404
		   ) 
neuper@37950
  2405
		  $ 
neuper@37950
  2406
		  den 	
neuper@37950
  2407
		  ,
neuper@37950
  2408
		  []
neuper@37950
  2409
		  )
neuper@37950
  2410
    end;
neuper@37950
  2411
neuper@37950
  2412
(* wird aktuell nicht mehr gebraucht, bei rückänderung schon 
neuper@37950
  2413
-------------------------------------------------------------
neuper@37950
  2414
(* WN0210???SK brauch ma des überhaupt *)
neuper@37950
  2415
fun com_den2(x::xs,denom,den,var)=
neuper@37950
  2416
    let 
neuper@38014
  2417
	val (t as Const ("Rings.inverse_class.divide",_) $ p1' $ p2')=x;
neuper@37950
  2418
	val p2= sort (mv_geq LEX_) (the(term2poly p2' var));
neuper@37950
  2419
	val p3= #1(mv_division(denom,p2,LEX_));
neuper@37950
  2420
	val p1var=get_vars(p1');
neuper@37950
  2421
    in     
neuper@37950
  2422
	if length(xs)>0 then 
neuper@37950
  2423
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2424
		(
neuper@38014
  2425
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2426
		 poly2term(the(term2poly p1' p1var),p1var) $ 
neuper@37950
  2427
		 com_den2(xs,denom,den,var)
neuper@37950
  2428
		)
neuper@37950
  2429
	    else
neuper@37950
  2430
		(
neuper@38014
  2431
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2432
		 (
neuper@37950
  2433
		   let 
neuper@37950
  2434
		       val p3'=poly2term(p3,var);
neuper@37950
  2435
		       val vars= (((map free2str) o vars) p1') union (((map free2str) o vars) p3');
neuper@37950
  2436
		   in
neuper@37950
  2437
		       poly2term(sort (mv_geq LEX_) (mv_mul(the(term2poly p1' vars) ,the(term2poly p3' vars),LEX_)),vars)
neuper@37950
  2438
		   end
neuper@37950
  2439
		  ) $ 
neuper@37950
  2440
		 com_den2(xs,denom,den,var)
neuper@37950
  2441
		)
neuper@37950
  2442
	else
neuper@37950
  2443
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2444
		(
neuper@37950
  2445
		 poly2term(the(term2poly p1' p1var),p1var)
neuper@37950
  2446
		 )
neuper@37950
  2447
	     else
neuper@37950
  2448
		 (
neuper@37950
  2449
		   let 
neuper@37950
  2450
		       val p3'=poly2term(p3,var);
neuper@37950
  2451
		       val vars= (((map free2str) o vars) p1') union (((map free2str) o vars) p3');
neuper@37950
  2452
		   in
neuper@37950
  2453
		       poly2term(sort (mv_geq LEX_) (mv_mul(the(term2poly p1' vars) ,the(term2poly p3' vars),LEX_)),vars)
neuper@37950
  2454
		   end
neuper@37950
  2455
		  )
neuper@37950
  2456
    end;
neuper@37950
  2457
neuper@37950
  2458
(* WN0210???SK brauch ma des überhaupt *)
neuper@37950
  2459
fun com_den_exp2(x::xs,denom,den,var)=
neuper@37950
  2460
    let 
neuper@38014
  2461
	val (t as Const ("Rings.inverse_class.divide",_) $ p1' $ p2')=x;
neuper@37950
  2462
	val p2= sort (mv_geq LEX_) (the(expanded2poly p2' var));
neuper@37950
  2463
	val p3= #1(mv_division(denom,p2,LEX_));
neuper@37950
  2464
	val p1var=get_vars p1';
neuper@37950
  2465
    in     
neuper@37950
  2466
	if length(xs)>0 then 
neuper@37950
  2467
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2468
		(
neuper@38014
  2469
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2470
		 poly2expanded(the (expanded2poly p1' p1var),p1var) $ 
neuper@37950
  2471
		 com_den_exp2(xs,denom,den,var)
neuper@37950
  2472
		)
neuper@37950
  2473
	    else
neuper@37950
  2474
		(
neuper@38014
  2475
		 Const ("Groups.plus_class.plus",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2476
		 (
neuper@37950
  2477
		   let 
neuper@37950
  2478
		       val p3'=poly2expanded(p3,var);
neuper@37950
  2479
		       val vars= (((map free2str) o vars) p1') union (((map free2str) o vars) p3');
neuper@37950
  2480
		   in
neuper@37950
  2481
		       poly2expanded(sort (mv_geq LEX_) (mv_mul(the(expanded2poly p1' vars) ,the(expanded2poly p3' vars),LEX_)),vars)
neuper@37950
  2482
		   end
neuper@37950
  2483
		  ) $ 
neuper@37950
  2484
		 com_den_exp2(xs,denom,den,var)
neuper@37950
  2485
		)
neuper@37950
  2486
	else
neuper@37950
  2487
	    if p3=[(1,mv_null2(var))] then
neuper@37950
  2488
		(
neuper@37950
  2489
		 poly2expanded(the (expanded2poly p1' p1var),p1var)
neuper@37950
  2490
		 )
neuper@37950
  2491
	     else
neuper@37950
  2492
		 (
neuper@37950
  2493
		   let 
neuper@37950
  2494
		       val p3'=poly2expanded(p3,var);
neuper@37950
  2495
		       val vars= (((map free2str) o vars) p1') union (((map free2str) o vars) p3');
neuper@37950
  2496
		   in
neuper@37950
  2497
		       poly2expanded(sort (mv_geq LEX_) (mv_mul(the(expanded2poly p1' vars) ,the(expanded2poly p3' vars),LEX_)),vars)
neuper@37950
  2498
		   end
neuper@37950
  2499
		  )
neuper@37950
  2500
    end;
neuper@37950
  2501
---------------------------------------------------------*)
neuper@37950
  2502
neuper@37950
  2503
neuper@37950
  2504
(*. searches for an element y of a list ys, which has an gcd not 1 with x .*) 
neuper@37950
  2505
fun exists_gcd (x,[]) = false 
neuper@37950
  2506
  | exists_gcd (x,y::ys) = if mv_gcd x y = [(1,mv_null2(#2(hd(x))))] then  exists_gcd (x,ys)
neuper@37950
  2507
			   else true;
neuper@37950
  2508
neuper@37950
  2509
(*. divides each element of the list xs with y .*)
neuper@37950
  2510
fun list_div ([],y) = [] 
neuper@37950
  2511
  | list_div (x::xs,y) = 
neuper@37950
  2512
    let
neuper@37950
  2513
	val (d,r)=mv_division(x,y,LEX_);
neuper@37950
  2514
    in
neuper@37950
  2515
	if r=[] then 
neuper@37950
  2516
	    d::list_div(xs,y)
neuper@37950
  2517
	else x::list_div(xs,y)
neuper@37950
  2518
    end;
neuper@37950
  2519
    
neuper@37950
  2520
(*. checks if x is in the list ys .*)
neuper@37950
  2521
fun in_list (x,[]) = false 
neuper@37950
  2522
  | in_list (x,y::ys) = if x=y then true
neuper@37950
  2523
			else in_list(x,ys);
neuper@37950
  2524
neuper@37950
  2525
(*. deletes all equal elements of the list xs .*)
neuper@37950
  2526
fun kill_equal [] = [] 
neuper@37950
  2527
  | kill_equal (x::xs) = if in_list(x,xs) orelse x=[(1,mv_null2(#2(hd(x))))] then kill_equal(xs)
neuper@37950
  2528
			 else x::kill_equal(xs);
neuper@37950
  2529
neuper@37950
  2530
(*. searches for new factors .*)
neuper@37950
  2531
fun new_factors [] = []
neuper@37950
  2532
  | new_factors (list:mv_poly list):mv_poly list = 
neuper@37950
  2533
    let
neuper@37950
  2534
	val l = kill_equal list;
neuper@37950
  2535
	val len = length(l);
neuper@37950
  2536
    in
neuper@37950
  2537
	if len>=2 then
neuper@37950
  2538
	    (
neuper@37950
  2539
	     let
neuper@37950
  2540
		 val x::y::xs=l;
neuper@37950
  2541
		 val gcd=mv_gcd x y;
neuper@37950
  2542
	     in
neuper@37950
  2543
		 if gcd=[(1,mv_null2(#2(hd(x))))] then 
neuper@37950
  2544
		     ( 
neuper@37950
  2545
		      if exists_gcd(x,xs) then new_factors (y::xs @ [x])
neuper@37950
  2546
		      else x::new_factors(y::xs)
neuper@37950
  2547
	             )
neuper@37950
  2548
		 else gcd::new_factors(kill_equal(list_div(x::y::xs,gcd)))
neuper@37950
  2549
	     end
neuper@37950
  2550
	     )
neuper@37950
  2551
	else
neuper@37950
  2552
	    if len=1 then [hd(l)]
neuper@37950
  2553
	    else []
neuper@37950
  2554
    end;
neuper@37950
  2555
neuper@37950
  2556
(*. gets the factors of a list .*)
neuper@37950
  2557
fun get_factors x = new_factors x; 
neuper@37950
  2558
neuper@37950
  2559
(*. multiplies the elements of the list .*)
neuper@37950
  2560
fun multi_list [] = []
neuper@37950
  2561
  | multi_list (x::xs) = if xs=[] then x
neuper@37950
  2562
			 else mv_mul(x,multi_list xs,LEX_);
neuper@37950
  2563
neuper@37950
  2564
(*. makes a term out of the elements of the list (polynomial representation) .*)
neuper@37950
  2565
fun make_term ([],vars) = Free(str_of_int 0,HOLogic.realT) 
neuper@37950
  2566
  | make_term ((x::xs),vars) = if length(xs)=0 then poly2term(sort (mv_geq LEX_) (x),vars)
neuper@37950
  2567
			       else
neuper@37950
  2568
				   (
neuper@38034
  2569
				    Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2570
				    poly2term(sort (mv_geq LEX_) (x),vars) $ 
neuper@37950
  2571
				    make_term(xs,vars)
neuper@37950
  2572
				    );
neuper@37950
  2573
neuper@37950
  2574
(*. factorizes the denominator (polynomial representation) .*)				
neuper@37950
  2575
fun factorize_den (l,den,vars) = 
neuper@37950
  2576
    let
neuper@37950
  2577
	val factor_list=kill_equal( (get_factors l));
neuper@37950
  2578
	val mlist=multi_list(factor_list);
neuper@37950
  2579
	val (last,rest)=mv_division(den,multi_list(factor_list),LEX_);
neuper@37950
  2580
    in
neuper@37950
  2581
	if rest=[] then
neuper@37950
  2582
	    (
neuper@37950
  2583
	     if last=[(1,mv_null2(vars))] then make_term(factor_list,vars)
neuper@37950
  2584
	     else make_term(last::factor_list,vars)
neuper@37950
  2585
	     )
neuper@38031
  2586
	else error ("RATIONALS_FACTORIZE_DEN_EXCEPTION: Invalid factor by division")
neuper@37950
  2587
    end; 
neuper@37950
  2588
neuper@37950
  2589
(*. makes a term out of the elements of the list (expanded polynomial representation) .*)
neuper@37950
  2590
fun make_exp ([],vars) = Free(str_of_int 0,HOLogic.realT) 
neuper@37950
  2591
  | make_exp ((x::xs),vars) = if length(xs)=0 then poly2expanded(sort (mv_geq LEX_) (x),vars)
neuper@37950
  2592
			       else
neuper@37950
  2593
				   (
neuper@38034
  2594
				    Const ("Groups.times_class.times",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2595
				    poly2expanded(sort (mv_geq LEX_) (x),vars) $ 
neuper@37950
  2596
				    make_exp(xs,vars)
neuper@37950
  2597
				    );
neuper@37950
  2598
neuper@37950
  2599
(*. factorizes the denominator (expanded polynomial representation) .*)	
neuper@37950
  2600
fun factorize_den_exp (l,den,vars) = 
neuper@37950
  2601
    let
neuper@37950
  2602
	val factor_list=kill_equal( (get_factors l));
neuper@37950
  2603
	val mlist=multi_list(factor_list);
neuper@37950
  2604
	val (last,rest)=mv_division(den,multi_list(factor_list),LEX_);
neuper@37950
  2605
    in
neuper@37950
  2606
	if rest=[] then
neuper@37950
  2607
	    (
neuper@37950
  2608
	     if last=[(1,mv_null2(vars))] then make_exp(factor_list,vars)
neuper@37950
  2609
	     else make_exp(last::factor_list,vars)
neuper@37950
  2610
	     )
neuper@38031
  2611
	else error ("RATIONALS_FACTORIZE_DEN_EXP_EXCEPTION: Invalid factor by division")
neuper@37950
  2612
    end; 
neuper@37950
  2613
neuper@37950
  2614
(*. calculates the common denominator of all elements of the list and multiplies .*)
neuper@37950
  2615
(*. the nominators and denominators with the correct factor .*)
neuper@37950
  2616
(*. (polynomial representation) .*)
neuper@37950
  2617
fun step_add_list_of_fractions []=(Free("0",HOLogic.realT),[]:term list)
neuper@38031
  2618
  | step_add_list_of_fractions [x]= error ("RATIONALS_STEP_ADD_LIST_OF_FRACTIONS_EXCEPTION: Nothing to add")
neuper@37950
  2619
  | step_add_list_of_fractions (xs) = 
neuper@37950
  2620
    let
neuper@37950
  2621
        val den_list=termlist2denominators (xs); (* list of denominators *)
neuper@37950
  2622
	val (denom,var)=calc_lcm(den_list);      (* common denominator *)
neuper@37950
  2623
	val den=factorize_den(#1(den_list),denom,var); (* faktorisierter Nenner !!! *)
neuper@37950
  2624
    in
neuper@37950
  2625
	com_den(xs,denom,den,var)
neuper@37950
  2626
    end;
neuper@37950
  2627
neuper@37950
  2628
(*. calculates the common denominator of all elements of the list and multiplies .*)
neuper@37950
  2629
(*. the nominators and denominators with the correct factor .*)
neuper@37950
  2630
(*. (expanded polynomial representation) .*)
neuper@37950
  2631
fun step_add_list_of_fractions_exp []  = (Free("0",HOLogic.realT),[]:term list)
neuper@38031
  2632
  | step_add_list_of_fractions_exp [x] = error ("RATIONALS_STEP_ADD_LIST_OF_FRACTIONS_EXP_EXCEPTION: Nothing to add")
neuper@37950
  2633
  | step_add_list_of_fractions_exp (xs)= 
neuper@37950
  2634
    let
neuper@37950
  2635
        val den_list=termlist2denominators_exp (xs); (* list of denominators *)
neuper@37950
  2636
	val (denom,var)=calc_lcm(den_list);      (* common denominator *)
neuper@37950
  2637
	val den=factorize_den_exp(#1(den_list),denom,var); (* faktorisierter Nenner !!! *)
neuper@37950
  2638
    in
neuper@37950
  2639
	com_den_exp(xs,denom,den,var)
neuper@37950
  2640
    end;
neuper@37950
  2641
neuper@37950
  2642
(* wird aktuell nicht mehr gebraucht, bei rückänderung schon 
neuper@37950
  2643
-------------------------------------------------------------
neuper@37950
  2644
(* WN0210???SK brauch ma des überhaupt *)
neuper@37950
  2645
fun step_add_list_of_fractions2 []=(Free("0",HOLogic.realT),[]:term list)
neuper@37950
  2646
  | step_add_list_of_fractions2 [x]=(x,[])
neuper@37950
  2647
  | step_add_list_of_fractions2 (xs) = 
neuper@37950
  2648
    let
neuper@37950
  2649
        val den_list=termlist2denominators (xs); (* list of denominators *)
neuper@37950
  2650
	val (denom,var)=calc_lcm(den_list);      (* common denominator *)
neuper@37950
  2651
	val den=factorize_den(#1(den_list),denom,var);  (* faktorisierter Nenner !!! *)
neuper@37950
  2652
    in
neuper@37950
  2653
	(
neuper@38014
  2654
	 Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2655
	 com_den2(xs,denom, poly2term(denom,var)(*den*),var) $
neuper@37950
  2656
	 poly2term(denom,var)
neuper@37950
  2657
	,
neuper@37950
  2658
	[]
neuper@37950
  2659
	)
neuper@37950
  2660
    end;
neuper@37950
  2661
neuper@37950
  2662
(* WN0210???SK brauch ma des überhaupt *)
neuper@37950
  2663
fun step_add_list_of_fractions2_exp []=(Free("0",HOLogic.realT),[]:term list)
neuper@37950
  2664
  | step_add_list_of_fractions2_exp [x]=(x,[])
neuper@37950
  2665
  | step_add_list_of_fractions2_exp (xs) = 
neuper@37950
  2666
    let
neuper@37950
  2667
        val den_list=termlist2denominators_exp (xs); (* list of denominators *)
neuper@37950
  2668
	val (denom,var)=calc_lcm(den_list);      (* common denominator *)
neuper@37950
  2669
	val den=factorize_den_exp(#1(den_list),denom,var);  (* faktorisierter Nenner !!! *)
neuper@37950
  2670
    in
neuper@37950
  2671
	(
neuper@38014
  2672
	 Const ("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2673
	 com_den_exp2(xs,denom, poly2term(denom,var)(*den*),var) $
neuper@37950
  2674
	 poly2expanded(denom,var)
neuper@37950
  2675
	,
neuper@37950
  2676
	[]
neuper@37950
  2677
	)
neuper@37950
  2678
    end;
neuper@37950
  2679
---------------------------------------------- *)
neuper@37950
  2680
neuper@37950
  2681
neuper@41933
  2682
(* converts a term, which contains several terms seperated by +, into a list of these terms .*)
neuper@38014
  2683
fun term2list (t as (Const("Rings.inverse_class.divide",_) $ _ $ _)) = [t]
neuper@37950
  2684
  | term2list (t as (Const("Atools.pow",_) $ _ $ _)) = 
neuper@41933
  2685
      [Const ("Rings.inverse_class.divide", 
neuper@41933
  2686
        [HOLogic.realT,HOLogic.realT] ---> HOLogic.realT) $ 
neuper@37950
  2687
	  t $ Free("1",HOLogic.realT)
neuper@37950
  2688
     ]
neuper@37950
  2689
  | term2list (t as (Free(_,_))) = 
neuper@38014
  2690
    [Const("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2691
	  t $  Free("1",HOLogic.realT)
neuper@37950
  2692
     ]
neuper@38034
  2693
  | term2list (t as (Const("Groups.times_class.times",_) $ _ $ _)) = 
neuper@38014
  2694
    [Const("Rings.inverse_class.divide",[HOLogic.realT,HOLogic.realT]--->HOLogic.realT) $ 
neuper@37950
  2695
	  t $ Free("1",HOLogic.realT)
neuper@37950
  2696
     ]
neuper@38014
  2697
  | term2list (Const("Groups.plus_class.plus",_) $ t1 $ t2) = term2list(t1) @ term2list(t2)
neuper@38014
  2698
  | term2list (Const("Groups.minus_class.minus",_) $ t1 $ t2) = 
neuper@38031
  2699
    error ("RATIONALS_TERM2LIST_EXCEPTION: - not implemented yet")
neuper@38031
  2700
  | term2list _ = error ("RATIONALS_TERM2LIST_EXCEPTION: invalid term");
neuper@37950
  2701
neuper@37950
  2702
(*.factors out the gcd of nominator and denominator:
neuper@37950
  2703
   a/b = (a' * gcd)/(b' * gcd),  a,b,gcd  are poly[2].*)
neuper@37950
  2704
fun factout_p_  (thy:theory) t = SOME (step_cancel t,[]:term list); 
neuper@37950
  2705
fun factout_ (thy:theory) t = SOME (step_cancel_expanded t,[]:term list); 
neuper@37950
  2706
neuper@37950
  2707
(*.cancels a single fraction with normalform [2]
neuper@37950
  2708
   resulting in a canceled fraction [2], see factout_ .*)
neuper@37950
  2709
fun cancel_p_ (thy:theory) t = (*WN.2.6.03 no rewrite -> NONE !*)
neuper@37950
  2710
    (let val (t',asm) = direct_cancel(*_expanded ... corrected MG.21.8.03*) t
neuper@37950
  2711
     in if t = t' then NONE else SOME (t',asm) 
neuper@37950
  2712
     end) handle _ => NONE;
neuper@37950
  2713
(*.the same as above with normalform [3]
neuper@37950
  2714
  val cancel_ :
neuper@37950
  2715
      theory ->        (*10.02 unused                                    *)
neuper@37950
  2716
      term -> 	       (*fraction in normalform [3]                      *)
neuper@37950
  2717
      (term * 	       (*fraction in normalform [3]                      *)
neuper@37950
  2718
       term list)      (*casual asumptions in normalform [3]             *)
neuper@37950
  2719
	  option       (*NONE: the function is not applicable            *).*)
neuper@37950
  2720
fun cancel_ (thy:theory) t = SOME (direct_cancel_expanded t) handle _ => NONE;
neuper@37950
  2721
neuper@37950
  2722
(*.transforms sums of at least 2 fractions [3] to
neuper@37950
  2723
   sums with the least common multiple as nominator.*)
neuper@37950
  2724
fun common_nominator_p_ (thy:theory) t =
neuper@38015
  2725
((*tracing("### common_nominator_p_ called");*)
neuper@37950
  2726
    SOME (step_add_list_of_fractions(term2list(t))) handle _ => NONE
neuper@37950
  2727
);
neuper@37950
  2728
fun common_nominator_ (thy:theory) t =
neuper@37950
  2729
    SOME (step_add_list_of_fractions_exp(term2list(t))) handle _ => NONE;
neuper@37950
  2730
neuper@37950
  2731
(*.add 2 or more fractions
neuper@37950
  2732
val add_fraction_p_ :
neuper@37950
  2733
      theory ->        (*10.02 unused                                    *)
neuper@37950
  2734
      term -> 	       (*2 or more fractions with normalform [2]         *)
neuper@37950
  2735
      (term * 	       (*one fraction with normalform [2]                *)
neuper@37950
  2736
       term list)      (*casual assumptions in normalform [2] WN0210???SK  *)
neuper@37950
  2737
	  option       (*NONE: the function is not applicable            *).*)
neuper@37950
  2738
fun add_fraction_p_ (thy:theory) t = 
neuper@38015
  2739
(tracing("### add_fraction_p_ called");
neuper@37950
  2740
    (let val ts = term2list t
neuper@37950
  2741
     in if 1 < length ts
neuper@37950
  2742
	then SOME (add_list_of_fractions ts)
neuper@38031
  2743
	else NONE (*error ("RATIONALS_ADD_EXCEPTION: nothing to add")*)
neuper@37950
  2744
     end) handle _ => NONE
neuper@37950
  2745
);
neuper@37950
  2746
(*.same as add_fraction_p_ but with normalform [3].*)
neuper@37950
  2747
(*SOME (step_add_list_of_fractions2(term2list(t))); *)
neuper@37950
  2748
fun add_fraction_ (thy:theory) t = 
neuper@41933
  2749
    if length (term2list t) > 1 
neuper@37950
  2750
    then SOME (add_list_of_fractions_exp(term2list(t))) handle _ => NONE
neuper@38031
  2751
    else (*error ("RATIONALS_ADD_FRACTION_EXCEPTION: nothing to add")*)
neuper@37950
  2752
	NONE;
neuper@37950
  2753
fun add_fraction_ (thy:theory) t = 
neuper@37950
  2754
    (if 1 < length (term2list t)
neuper@37950
  2755
     then SOME (add_list_of_fractions_exp (term2list t))
neuper@38031
  2756
     else (*error ("RATIONALS_ADD_FRACTION_EXCEPTION: nothing to add")*)
neuper@37950
  2757
	 NONE) handle _ => NONE;
neuper@37950
  2758
neuper@37950
  2759
(*SOME (step_add_list_of_fractions2_exp(term2list(t))); *)
neuper@37950
  2760
neuper@37950
  2761
(*. brings the term into a normal form .*)
neuper@37950
  2762
fun norm_rational_ (thy:theory) t = 
neuper@37950
  2763
    SOME (add_list_of_fractions(term2list(t))) handle _ => NONE; 
neuper@37950
  2764
fun norm_expanded_rat_ (thy:theory) t = 
neuper@37950
  2765
    SOME (add_list_of_fractions_exp(term2list(t))) handle _ => NONE; 
neuper@37950
  2766
neuper@37950
  2767
neuper@37950
  2768
(*.evaluates conditions in calculate_Rational.*)
neuper@37950
  2769
(*make local with FIXX@ME result:term *term list*)
neuper@37950
  2770
val calc_rat_erls = prep_rls(
neuper@37950
  2771
  Rls {id = "calc_rat_erls", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  2772
	 erls = e_rls, srls = Erls, calc = [], (*asm_thm = [], *)
neuper@37950
  2773
	 rules = 
neuper@41922
  2774
	 [Calc ("HOL.eq",eval_equal "#equal_"),
neuper@37950
  2775
	  Calc ("Atools.is'_const",eval_const "#is_const_"),
neuper@37978
  2776
	  Thm ("not_true",num_str @{thm not_true}),
neuper@37978
  2777
	  Thm ("not_false",num_str @{thm not_false})
neuper@37950
  2778
	  ], 
neuper@37950
  2779
	 scr = EmptyScr});
neuper@37950
  2780
neuper@37950
  2781
neuper@37950
  2782
(*.simplifies expressions with numerals;
neuper@37950
  2783
   does NOT rearrange the term by AC-rewriting; thus terms with variables 
neuper@37950
  2784
   need to have constants to be commuted together respectively.*)
neuper@37950
  2785
val calculate_Rational = prep_rls(
neuper@37950
  2786
    merge_rls "calculate_Rational"
neuper@37950
  2787
	(Rls {id = "divide", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  2788
	      erls = calc_rat_erls, srls = Erls, (*asm_thm = [],*) 
neuper@37950
  2789
	      calc = [], 
neuper@37950
  2790
	      rules = 
neuper@38014
  2791
	      [Calc ("Rings.inverse_class.divide"  ,eval_cancel "#divide_e"),
neuper@37950
  2792
	       
neuper@37965
  2793
	       Thm ("minus_divide_left",
neuper@37978
  2794
		    num_str (@{thm minus_divide_left} RS @{thm sym})),
neuper@37950
  2795
	       (*SYM - ?x / ?y = - (?x / ?y)  may come from subst*)
neuper@37950
  2796
	       
neuper@37969
  2797
	       Thm ("rat_add",num_str @{thm rat_add}),
neuper@37950
  2798
	       (*"[| a is_const; b is_const; c is_const; d is_const |] ==> \
neuper@37950
  2799
		 \"a / c + b / d = (a * d) / (c * d) + (b * c ) / (d * c)"*)
neuper@37969
  2800
	       Thm ("rat_add1",num_str @{thm rat_add1}),
neuper@37950
  2801
	       (*"[| a is_const; b is_const; c is_const |] ==> \
neuper@37950
  2802
		 \"a / c + b / c = (a + b) / c"*)
neuper@37969
  2803
	       Thm ("rat_add2",num_str @{thm rat_add2}),
neuper@37950
  2804
	       (*"[| ?a is_const; ?b is_const; ?c is_const |] ==> \
neuper@37950
  2805
		 \?a / ?c + ?b = (?a + ?b * ?c) / ?c"*)
neuper@37969
  2806
	       Thm ("rat_add3",num_str @{thm rat_add3}),
neuper@37950
  2807
	       (*"[| a is_const; b is_const; c is_const |] ==> \
neuper@37950
  2808
		 \"a + b / c = (a * c) / c + b / c"\
neuper@37950
  2809
		 \.... is_const to be omitted here FIXME*)
neuper@37950
  2810
	       
neuper@37969
  2811
	       Thm ("rat_mult",num_str @{thm rat_mult}),
neuper@37950
  2812
	       (*a / b * (c / d) = a * c / (b * d)*)
neuper@37965
  2813
	       Thm ("times_divide_eq_right",num_str @{thm times_divide_eq_right}),
neuper@37950
  2814
	       (*?x * (?y / ?z) = ?x * ?y / ?z*)
neuper@37965
  2815
	       Thm ("times_divide_eq_left",num_str @{thm times_divide_eq_left}),
neuper@37950
  2816
	       (*?y / ?z * ?x = ?y * ?x / ?z*)
neuper@37950
  2817
	       
neuper@37969
  2818
	       Thm ("real_divide_divide1",num_str @{thm real_divide_divide1}),
neuper@37950
  2819
	       (*"?y ~= 0 ==> ?u / ?v / (?y / ?z) = ?u / ?v * (?z / ?y)"*)
neuper@37965
  2820
	       Thm ("divide_divide_eq_left",num_str @{thm divide_divide_eq_left}),
neuper@37950
  2821
	       (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
neuper@37950
  2822
	       
neuper@37969
  2823
	       Thm ("rat_power", num_str @{thm rat_power}),
neuper@37950
  2824
	       (*"(?a / ?b) ^^^ ?n = ?a ^^^ ?n / ?b ^^^ ?n"*)
neuper@37950
  2825
	       
neuper@37969
  2826
	       Thm ("mult_cross",num_str @{thm mult_cross}),
neuper@37950
  2827
	       (*"[| b ~= 0; d ~= 0 |] ==> (a / b = c / d) = (a * d = b * c)*)
neuper@37969
  2828
	       Thm ("mult_cross1",num_str @{thm mult_cross1}),
neuper@37950
  2829
	       (*"   b ~= 0            ==> (a / b = c    ) = (a     = b * c)*)
neuper@37969
  2830
	       Thm ("mult_cross2",num_str @{thm mult_cross2})
neuper@37950
  2831
	       (*"           d ~= 0    ==> (a     = c / d) = (a * d =     c)*)
neuper@37950
  2832
	       ], scr = EmptyScr})
neuper@37950
  2833
	calculate_Poly);
neuper@37950
  2834
neuper@37950
  2835
(*("is_expanded", ("Rational.is'_expanded", eval_is_expanded ""))*)
neuper@37950
  2836
fun eval_is_expanded (thmid:string) _ 
neuper@37950
  2837
		       (t as (Const("Rational.is'_expanded", _) $ arg)) thy = 
neuper@37950
  2838
    if is_expanded arg
neuper@37950
  2839
    then SOME (mk_thmid thmid "" 
neuper@38053
  2840
			(Print_Mode.setmp [] (Syntax.string_of_term
neuper@38053
  2841
                                              (thy2ctxt thy)) arg) "", 
neuper@37950
  2842
	       Trueprop $ (mk_equality (t, HOLogic.true_const)))
neuper@37950
  2843
    else SOME (mk_thmid thmid "" 
neuper@38053
  2844
			(Print_Mode.setmp [] (Syntax.string_of_term
neuper@38053
  2845
                                              (thy2ctxt thy)) arg) "", 
neuper@37950
  2846
	       Trueprop $ (mk_equality (t, HOLogic.false_const)))
neuper@37950
  2847
  | eval_is_expanded _ _ _ _ = NONE; 
neuper@37950
  2848
neuper@37950
  2849
val rational_erls = 
neuper@37950
  2850
    merge_rls "rational_erls" calculate_Rational 
neuper@37950
  2851
	      (append_rls "is_expanded" Atools_erls 
neuper@37950
  2852
			  [Calc ("Rational.is'_expanded", eval_is_expanded "")
neuper@37950
  2853
			   ]);
neuper@37950
  2854
neuper@37950
  2855
neuper@37950
  2856
(*.3 'reverse-rewrite-sets' for symbolic computation on rationals:
neuper@37950
  2857
 =================================================================
neuper@37950
  2858
 A[2] 'cancel_p': .
neuper@37950
  2859
 A[3] 'cancel': .
neuper@37950
  2860
 B[2] 'common_nominator_p': transforms summands in a term [2]
neuper@37950
  2861
         to fractions with the (least) common multiple as nominator.
neuper@37950
  2862
 B[3] 'norm_rational': normalizes arbitrary algebraic terms (without 
neuper@37950
  2863
         radicals and transzendental functions) to one canceled fraction,
neuper@37950
  2864
	 nominator and denominator in polynomial form.
neuper@37950
  2865
neuper@37950
  2866
In order to meet isac's requirements for interactive and stepwise calculation,
neuper@37950
  2867
each 'reverse-rewerite-set' consists of an initialization for the interpreter 
neuper@37950
  2868
state and of 4 functions, each of which employs rewriting as much as possible.
neuper@37950
  2869
The signature of these functions are the same in each 'reverse-rewrite-set' 
neuper@37950
  2870
respectively.*)
neuper@37950
  2871
neuper@37950
  2872
(* ************************************************************************* *)
neuper@37950
  2873
neuper@37950
  2874
local(*. cancel_p
neuper@37950
  2875
------------------------
neuper@37950
  2876
cancels a single fraction consisting of two (uni- or multivariate)
neuper@37950
  2877
polynomials WN0609???SK[2] into another such a fraction; examples:
neuper@37950
  2878
neuper@37950
  2879
	   a^2 + -1*b^2         a + b
neuper@37950
  2880
        -------------------- = ---------
neuper@37950
  2881
	a^2 + -2*a*b + b^2     a + -1*b
neuper@37950
  2882
neuper@37950
  2883
        a^2    a
neuper@37950
  2884
        --- = ---
neuper@37950
  2885
         a     1
neuper@37950
  2886
neuper@37950
  2887
Remark: the reverse ruleset does _NOT_ work properly with other input !.*)
neuper@37950
  2888
(*WN020824 wir werden "uberlegen, wie wir ungeeignete inputs zur"uckweisen*)
neuper@37950
  2889
neuper@37950
  2890
val {rules, rew_ord=(_,ro),...} =
neuper@37950
  2891
    rep_rls (assoc_rls "make_polynomial");
neuper@37950
  2892
(*WN060829 ... make_deriv does not terminate with 1st expl above,
neuper@37950
  2893
           see rational.sml --- investigate rulesets for cancel_p ---*)
neuper@37950
  2894
val {rules, rew_ord=(_,ro),...} =
neuper@37950
  2895
    rep_rls (assoc_rls "rev_rew_p");
neuper@37950
  2896
neuper@37950
  2897
(*.init_state = fn : term -> istate
neuper@37950
  2898
initialzies the state of the script interpreter. The state is:
neuper@37950
  2899
neuper@37950
  2900
type rrlsstate =      (*state for reverse rewriting*)
neuper@37950
  2901
     (term *          (*the current formula*)
neuper@37950
  2902
      term *          (*the final term*)
neuper@37950
  2903
      rule list       (*'reverse rule list' (#)*)
neuper@37950
  2904
	    list *    (*may be serveral, eg. in norm_rational*)
neuper@37950
  2905
      (rule *         (*Thm (+ Thm generated from Calc) resulting in ...*)
neuper@37950
  2906
       (term *        (*... rewrite with ...*)
neuper@37950
  2907
	term list))   (*... assumptions*)
neuper@37950
  2908
	  list);      (*derivation from given term to normalform
neuper@37950
  2909
		       in reverse order with sym_thm;
neuper@37950
  2910
                       (#) could be extracted from here by (map #1)*).*)
neuper@37950
  2911
(* val {rules, rew_ord=(_,ro),...} =
neuper@37950
  2912
       rep_rls (assoc_rls "rev_rew_p")        (*USE ALWAYS, SEE val cancel_p*);
neuper@37972
  2913
   val (thy, eval_rls, ro) =(thy, Atools_erls, ro) (*..val cancel_p*);
neuper@37950
  2914
   val t = t;
neuper@37950
  2915
   *)
neuper@37950
  2916
fun init_state thy eval_rls ro t =
neuper@37950
  2917
    let val SOME (t',_) = factout_p_ thy t
neuper@37950
  2918
        val SOME (t'',asm) = cancel_p_ thy t
neuper@37950
  2919
        val der = reverse_deriv thy eval_rls rules ro NONE t'
neuper@37950
  2920
        val der = der @ [(Thm ("real_mult_div_cancel2",
neuper@37969
  2921
			       num_str @{thm real_mult_div_cancel2}),
neuper@37950
  2922
			  (t'',asm))]
neuper@37950
  2923
        val rs = (distinct_Thm o (map #1)) der
neuper@37950
  2924
	val rs = filter_out (eq_Thms ["sym_real_add_zero_left",
neuper@37950
  2925
				      "sym_real_mult_0",
neuper@37950
  2926
				      "sym_real_mult_1"
neuper@37950
  2927
				      (*..insufficient,eg.make_Polynomial*)])rs
neuper@37950
  2928
    in (t,t'',[rs(*here only _ONE_ to ease locate_rule*)],der) end;
neuper@37950
  2929
neuper@37950
  2930
(*.locate_rule = fn : rule list -> term -> rule
neuper@37950
  2931
		      -> (rule * (term * term list) option) list.
neuper@37950
  2932
  checks a rule R for being a cancel-rule, and if it is,
neuper@37950
  2933
  then return the list of rules (+ the terms they are rewriting to)
neuper@37950
  2934
  which need to be applied before R should be applied.
neuper@37950
  2935
  precondition: the rule is applicable to the argument-term.
neuper@37950
  2936
arguments:
neuper@37950
  2937
  rule list: the reverse rule list
neuper@37950
  2938
  -> term  : ... to which the rule shall be applied
neuper@37950
  2939
  -> rule  : ... to be applied to term
neuper@37950
  2940
value:
neuper@37950
  2941
  -> (rule           : a rule rewriting to ...
neuper@37950
  2942
      * (term        : ... the resulting term ...
neuper@37950
  2943
         * term list): ... with the assumptions ( //#0).
neuper@37950
  2944
      ) list         : there may be several such rules;
neuper@37950
  2945
		       the list is empty, if the rule has nothing to do
neuper@37950
  2946
		       with cancelation.*)
neuper@37950
  2947
(* val () = ();
neuper@37950
  2948
   *)
neuper@37950
  2949
fun locate_rule thy eval_rls ro [rs] t r =
neuper@37950
  2950
    if (id_of_thm r) mem (map (id_of_thm)) rs
neuper@37950
  2951
    then let val ropt =
neuper@37950
  2952
		 rewrite_ thy ro eval_rls true (thm_of_thm r) t;
neuper@37950
  2953
	 in case ropt of
neuper@37950
  2954
		SOME ta => [(r, ta)]
neuper@38015
  2955
	      | NONE => (tracing("### locate_rule:  rewrite "^
neuper@37950
  2956
				 (id_of_thm r)^" "^(term2str t)^" = NONE");
neuper@37950
  2957
			 []) end
neuper@38015
  2958
    else (tracing("### locate_rule:  "^(id_of_thm r)^" not mem rrls");[])
neuper@37950
  2959
  | locate_rule _ _ _ _ _ _ =
neuper@38031
  2960
    error ("locate_rule: doesnt match rev-sets in istate");
neuper@37950
  2961
neuper@37950
  2962
(*.next_rule = fn : rule list -> term -> rule option
neuper@37950
  2963
  for a given term return the next rules to be done for cancelling.
neuper@37950
  2964
arguments:
neuper@37950
  2965
  rule list     : the reverse rule list
neuper@37950
  2966
  term          : the term for which ...
neuper@37950
  2967
value:
neuper@37950
  2968
  -> rule option: ... this rule is appropriate for cancellation;
neuper@37950
  2969
		  there may be no such rule (if the term is canceled already.*)
neuper@37972
  2970
(* val thy = thy;
neuper@37950
  2971
   val Rrls {rew_ord=(_,ro),...} = cancel;
neuper@37950
  2972
   val ([rs],t) = (rss,f);
neuper@37950
  2973
   next_rule thy eval_rls ro [rs] t;(*eval fun next_rule ... before!*)
neuper@37950
  2974
neuper@37972
  2975
   val (thy, [rs]) = (thy, revsets);
neuper@37950
  2976
   val Rrls {rew_ord=(_,ro),...} = cancel;
neuper@37950
  2977
   nex [rs] t;
neuper@37950
  2978
   *)
neuper@37950
  2979
fun next_rule thy eval_rls ro [rs] t =
neuper@37950
  2980
    let val der = make_deriv thy eval_rls rs ro NONE t;
neuper@37950
  2981
    in case der of
neuper@37950
  2982
(* val (_,r,_)::_ = der;
neuper@37950
  2983
   *)
neuper@37950
  2984
	   (_,r,_)::_ => SOME r
neuper@37950
  2985
	 | _ => NONE
neuper@37950
  2986
    end
neuper@37950
  2987
  | next_rule _ _ _ _ _ =
neuper@38031
  2988
    error ("next_rule: doesnt match rev-sets in istate");
neuper@37950
  2989
neuper@37950
  2990
(*.val attach_form = f : rule list -> term -> term
neuper@37950
  2991
			 -> (rule * (term * term list)) list
neuper@37950
  2992
  checks an input term TI, if it may belong to a current cancellation, by
neuper@37950
  2993
  trying to derive it from the given term TG.
neuper@37950
  2994
arguments:
neuper@37950
  2995
  term   : TG, the last one in the cancellation agreed upon by user + math-eng
neuper@37950
  2996
  -> term: TI, the next one input by the user
neuper@37950
  2997
value:
neuper@37950
  2998
  -> (rule           : the rule to be applied in order to reach TI
neuper@37950
  2999
      * (term        : ... obtained by applying the rule ...
neuper@37950
  3000
         * term list): ... and the respective assumptions.
neuper@37950
  3001
      ) list         : there may be several such rules;
neuper@37950
  3002
                       the list is empty, if the users term does not belong
neuper@37950
  3003
		       to a cancellation of the term last agreed upon.*)
neuper@37950
  3004
fun attach_form (_:rule list list) (_:term) (_:term) = (*still missing*)
neuper@37950
  3005
    []:(rule * (term * term list)) list;
neuper@37950
  3006
neuper@37950
  3007
in
neuper@37950
  3008
neuper@37950
  3009
val cancel_p =
neuper@37950
  3010
    Rrls {id = "cancel_p", prepat=[],
neuper@37950
  3011
	  rew_ord=("ord_make_polynomial",
neuper@37972
  3012
		   ord_make_polynomial false thy),
neuper@37950
  3013
	  erls = rational_erls,
neuper@38014
  3014
	  calc = [("PLUS"    ,("Groups.plus_class.plus"        ,eval_binop "#add_")),
neuper@38034
  3015
		  ("TIMES"   ,("Groups.times_class.times"        ,eval_binop "#mult_")),
neuper@38014
  3016
		  ("DIVIDE" ,("Rings.inverse_class.divide"  ,eval_cancel "#divide_e")),
neuper@37950
  3017
		  ("POWER"  ,("Atools.pow"  ,eval_binop "#power_"))],
neuper@37950
  3018
	  (*asm_thm=[("real_mult_div_cancel2","")],*)
neuper@37950
  3019
	  scr=Rfuns {init_state  = init_state thy Atools_erls ro,
neuper@37950
  3020
		     normal_form = cancel_p_ thy,
neuper@37950
  3021
		     locate_rule = locate_rule thy Atools_erls ro,
neuper@37950
  3022
		     next_rule   = next_rule thy Atools_erls ro,
neuper@37950
  3023
		     attach_form = attach_form}}
neuper@37950
  3024
end;(*local*)
neuper@37950
  3025
neuper@37950
  3026
local(*.ad (1) 'cancel'
neuper@37950
  3027
------------------------------
neuper@37950
  3028
cancels a single fraction consisting of two (uni- or multivariate)
neuper@37950
  3029
polynomials WN0609???SK[3] into another such a fraction; examples:
neuper@37950
  3030
neuper@37950
  3031
	   a^2 - b^2           a + b
neuper@37950
  3032
        -------------------- = ---------
neuper@37950
  3033
	a^2 - 2*a*b + b^2      a - *b
neuper@37950
  3034
neuper@37950
  3035
Remark: the reverse ruleset does _NOT_ work properly with other input !.*)
neuper@37950
  3036
(*WN 24.8.02: wir werden "uberlegen, wie wir ungeeignete inputs zur"uckweisen*)
neuper@37950
  3037
neuper@37950
  3038
(*
neuper@37950
  3039
val SOME (Rls {rules=rules,rew_ord=(_,ro),...}) = 
neuper@37950
  3040
    assoc'(!ruleset',"expand_binoms");
neuper@37950
  3041
*)
neuper@37950
  3042
val {rules=rules,rew_ord=(_,ro),...} =
neuper@37950
  3043
    rep_rls (assoc_rls "expand_binoms");
neuper@37972
  3044
val thy = thy;
neuper@37950
  3045
neuper@37950
  3046
fun init_state thy eval_rls ro t =
neuper@37950
  3047
    let val SOME (t',_) = factout_ thy t;
neuper@37950
  3048
        val SOME (t'',asm) = cancel_ thy t;
neuper@37950
  3049
        val der = reverse_deriv thy eval_rls rules ro NONE t';
neuper@37950
  3050
        val der = der @ [(Thm ("real_mult_div_cancel2",
neuper@37969
  3051
			       num_str @{thm real_mult_div_cancel2}),
neuper@37950
  3052
			  (t'',asm))]
neuper@37950
  3053
        val rs = map #1 der;
neuper@37950
  3054
    in (t,t'',[rs],der) end;
neuper@37950
  3055
neuper@37950
  3056
fun locate_rule thy eval_rls ro [rs] t r =
neuper@37950
  3057
    if (id_of_thm r) mem (map (id_of_thm)) rs
neuper@37950
  3058
    then let val ropt = 
neuper@37950
  3059
		 rewrite_ thy ro eval_rls true (thm_of_thm r) t;
neuper@37950
  3060
	 in case ropt of
neuper@37950
  3061
		SOME ta => [(r, ta)]
neuper@38015
  3062
	      | NONE => (tracing("### locate_rule:  rewrite "^
neuper@37950
  3063
				 (id_of_thm r)^" "^(term2str t)^" = NONE");
neuper@37950
  3064
			 []) end
neuper@38015
  3065
    else (tracing("### locate_rule:  "^(id_of_thm r)^" not mem rrls");[])
neuper@37950
  3066
  | locate_rule _ _ _ _ _ _ = 
neuper@38031
  3067
    error ("locate_rule: doesnt match rev-sets in istate");
neuper@37950
  3068
neuper@37950
  3069
fun next_rule thy eval_rls ro [rs] t =
neuper@37950
  3070
    let val der = make_deriv thy eval_rls rs ro NONE t;
neuper@37950
  3071
    in case der of 
neuper@37950
  3072
(* val (_,r,_)::_ = der;
neuper@37950
  3073
   *)
neuper@37950
  3074
	   (_,r,_)::_ => SOME r
neuper@37950
  3075
	 | _ => NONE
neuper@37950
  3076
    end
neuper@37950
  3077
  | next_rule _ _ _ _ _ = 
neuper@38031
  3078
    error ("next_rule: doesnt match rev-sets in istate");
neuper@37950
  3079
neuper@37950
  3080
fun attach_form (_:rule list list) (_:term) (_:term) = (*still missing*)
neuper@37950
  3081
    []:(rule * (term * term list)) list;
neuper@37950
  3082
neuper@38036
  3083
(* pat matched with the current term gives an environment 
neuper@38036
  3084
   (or not: hen the Rrls not applied);
neuper@38036
  3085
   if pre1 and pre2 evaluate to HOLogic.true_const in this environment,
neuper@38036
  3086
   then the Rrls is applied. *)
neuper@38037
  3087
val pat = parse_patt thy "?r/?s :: real";
neuper@37979
  3088
val pre1 = parse_patt thy "?r is_expanded";
neuper@37979
  3089
val pre2 = parse_patt thy "?s is_expanded";
neuper@37950
  3090
val prepat = [([pre1, pre2], pat)];
neuper@37950
  3091
neuper@37950
  3092
in
neuper@37950
  3093
neuper@37950
  3094
neuper@37950
  3095
val cancel = 
neuper@37950
  3096
    Rrls {id = "cancel", prepat=prepat,
neuper@37950
  3097
	  rew_ord=("ord_make_polynomial",
neuper@37972
  3098
		   ord_make_polynomial false thy),
neuper@37950
  3099
	  erls = rational_erls, 
neuper@38014
  3100
	  calc = [("PLUS"    ,("Groups.plus_class.plus"        ,eval_binop "#add_")),
neuper@38034
  3101
		  ("TIMES"   ,("Groups.times_class.times"        ,eval_binop "#mult_")),
neuper@38014
  3102
		  ("DIVIDE" ,("Rings.inverse_class.divide"  ,eval_cancel "#divide_e")),
neuper@37950
  3103
		  ("POWER"  ,("Atools.pow"  ,eval_binop "#power_"))],
neuper@37950
  3104
	  scr=Rfuns {init_state  = init_state thy Atools_erls ro,
neuper@37950
  3105
		     normal_form = cancel_ thy, 
neuper@37950
  3106
		     locate_rule = locate_rule thy Atools_erls ro,
neuper@37950
  3107
		     next_rule   = next_rule thy Atools_erls ro,
neuper@37950
  3108
		     attach_form = attach_form}}
neuper@37950
  3109
end;(*local*)
neuper@37950
  3110
neuper@37950
  3111
local(*.ad [2] 'common_nominator_p'
neuper@37950
  3112
---------------------------------
neuper@37950
  3113
FIXME Beschreibung .*)
neuper@37950
  3114
neuper@37950
  3115
neuper@37950
  3116
val {rules=rules,rew_ord=(_,ro),...} =
neuper@37950
  3117
    rep_rls (assoc_rls "make_polynomial");
neuper@37950
  3118
(*WN060829 ... make_deriv does not terminate with 1st expl above,
neuper@37950
  3119
           see rational.sml --- investigate rulesets for cancel_p ---*)
neuper@37950
  3120
val {rules, rew_ord=(_,ro),...} =
neuper@37950
  3121
    rep_rls (assoc_rls "rev_rew_p");
neuper@37972
  3122
val thy = thy;
neuper@37950
  3123
neuper@37950
  3124
neuper@37950
  3125
(*.common_nominator_p_ = fn : theory -> term -> (term * term list) option
neuper@37950
  3126
  as defined above*)
neuper@37950
  3127
neuper@37950
  3128
(*.init_state = fn : term -> istate
neuper@37950
  3129
initialzies the state of the interactive interpreter. The state is:
neuper@37950
  3130
neuper@37950
  3131
type rrlsstate =      (*state for reverse rewriting*)
neuper@37950
  3132
     (term *          (*the current formula*)
neuper@37950
  3133
      term *          (*the final term*)
neuper@37950
  3134
      rule list       (*'reverse rule list' (#)*)
neuper@37950
  3135
	    list *    (*may be serveral, eg. in norm_rational*)
neuper@37950
  3136
      (rule *         (*Thm (+ Thm generated from Calc) resulting in ...*)
neuper@37950
  3137
       (term *        (*... rewrite with ...*)
neuper@37950
  3138
	term list))   (*... assumptions*)
neuper@37950
  3139
	  list);      (*derivation from given term to normalform
neuper@37950
  3140
		       in reverse order with sym_thm;
neuper@37950
  3141
                       (#) could be extracted from here by (map #1)*).*)
neuper@37950
  3142
fun init_state thy eval_rls ro t =
neuper@37950
  3143
    let val SOME (t',_) = common_nominator_p_ thy t;
neuper@37950
  3144
        val SOME (t'',asm) = add_fraction_p_ thy t;
neuper@37950
  3145
        val der = reverse_deriv thy eval_rls rules ro NONE t';
neuper@37950
  3146
        val der = der @ [(Thm ("real_mult_div_cancel2",
neuper@37969
  3147
			       num_str @{thm real_mult_div_cancel2}),
neuper@37950
  3148
			  (t'',asm))]
neuper@37950
  3149
        val rs = (distinct_Thm o (map #1)) der;
neuper@37950
  3150
	val rs = filter_out (eq_Thms ["sym_real_add_zero_left",
neuper@37950
  3151
				      "sym_real_mult_0",
neuper@37950
  3152
				      "sym_real_mult_1"]) rs;
neuper@37950
  3153
    in (t,t'',[rs(*here only _ONE_*)],der) end;
neuper@37950
  3154
neuper@37950
  3155
(* use"knowledge/Rational.ML";
neuper@37950
  3156
   *)
neuper@37950
  3157
neuper@37950
  3158
(*.locate_rule = fn : rule list -> term -> rule
neuper@37950
  3159
		      -> (rule * (term * term list) option) list.
neuper@37950
  3160
  checks a rule R for being a cancel-rule, and if it is,
neuper@37950
  3161
  then return the list of rules (+ the terms they are rewriting to)
neuper@37950
  3162
  which need to be applied before R should be applied.
neuper@37950
  3163
  precondition: the rule is applicable to the argument-term.
neuper@37950
  3164
arguments:
neuper@37950
  3165
  rule list: the reverse rule list
neuper@37950
  3166
  -> term  : ... to which the rule shall be applied
neuper@37950
  3167
  -> rule  : ... to be applied to term
neuper@37950
  3168
value:
neuper@37950
  3169
  -> (rule           : a rule rewriting to ...
neuper@37950
  3170
      * (term        : ... the resulting term ...
neuper@37950
  3171
         * term list): ... with the assumptions ( //#0).
neuper@37950
  3172
      ) list         : there may be several such rules;
neuper@37950
  3173
		       the list is empty, if the rule has nothing to do
neuper@37950
  3174
		       with cancelation.*)
neuper@37950
  3175
(* val () = ();
neuper@37950
  3176
   *)
neuper@37950
  3177
fun locate_rule thy eval_rls ro [rs] t r =
neuper@37950
  3178
    if (id_of_thm r) mem (map (id_of_thm)) rs
neuper@37950
  3179
    then let val ropt =
neuper@37950
  3180
		 rewrite_ thy ro eval_rls true (thm_of_thm r) t;
neuper@37950
  3181
	 in case ropt of
neuper@37950
  3182
		SOME ta => [(r, ta)]
neuper@38015
  3183
	      | NONE => (tracing("### locate_rule:  rewrite "^
neuper@37950
  3184
				 (id_of_thm r)^" "^(term2str t)^" = NONE");
neuper@37950
  3185
			 []) end
neuper@38015
  3186
    else (tracing("### locate_rule:  "^(id_of_thm r)^" not mem rrls");[])
neuper@37950
  3187
  | locate_rule _ _ _ _ _ _ =
neuper@38031
  3188
    error ("locate_rule: doesnt match rev-sets in istate");
neuper@37950
  3189
neuper@37950
  3190
(*.next_rule = fn : rule list -> term -> rule option
neuper@37950
  3191
  for a given term return the next rules to be done for cancelling.
neuper@37950
  3192
arguments:
neuper@37950
  3193
  rule list     : the reverse rule list
neuper@37950
  3194
  term          : the term for which ...
neuper@37950
  3195
value:
neuper@37950
  3196
  -> rule option: ... this rule is appropriate for cancellation;
neuper@37950
  3197
		  there may be no such rule (if the term is canceled already.*)
neuper@37972
  3198
(* val thy = thy;
neuper@37950
  3199
   val Rrls {rew_ord=(_,ro),...} = cancel;
neuper@37950
  3200
   val ([rs],t) = (rss,f);
neuper@37950
  3201
   next_rule thy eval_rls ro [rs] t;(*eval fun next_rule ... before!*)
neuper@37950
  3202
neuper@37972
  3203
   val (thy, [rs]) = (thy, revsets);
neuper@37950
  3204
   val Rrls {rew_ord=(_,ro),...} = cancel;
neuper@37950
  3205
   nex [rs] t;
neuper@37950
  3206
   *)
neuper@37950
  3207
fun next_rule thy eval_rls ro [rs] t =
neuper@37950
  3208
    let val der = make_deriv thy eval_rls rs ro NONE t;
neuper@37950
  3209
    in case der of
neuper@37950
  3210
(* val (_,r,_)::_ = der;
neuper@37950
  3211
   *)
neuper@37950
  3212
	   (_,r,_)::_ => SOME r
neuper@37950
  3213
	 | _ => NONE
neuper@37950
  3214
    end
neuper@37950
  3215
  | next_rule _ _ _ _ _ =
neuper@38031
  3216
    error ("next_rule: doesnt match rev-sets in istate");
neuper@37950
  3217
neuper@37950
  3218
(*.val attach_form = f : rule list -> term -> term
neuper@37950
  3219
			 -> (rule * (term * term list)) list
neuper@37950
  3220
  checks an input term TI, if it may belong to a current cancellation, by
neuper@37950
  3221
  trying to derive it from the given term TG.
neuper@37950
  3222
arguments:
neuper@37950
  3223
  term   : TG, the last one in the cancellation agreed upon by user + math-eng
neuper@37950
  3224
  -> term: TI, the next one input by the user
neuper@37950
  3225
value:
neuper@37950
  3226
  -> (rule           : the rule to be applied in order to reach TI
neuper@37950
  3227
      * (term        : ... obtained by applying the rule ...
neuper@37950
  3228
         * term list): ... and the respective assumptions.
neuper@37950
  3229
      ) list         : there may be several such rules;
neuper@37950
  3230
                       the list is empty, if the users term does not belong
neuper@37950
  3231
		       to a cancellation of the term last agreed upon.*)
neuper@37950
  3232
fun attach_form (_:rule list list) (_:term) (_:term) = (*still missing*)
neuper@37950
  3233
    []:(rule * (term * term list)) list;
neuper@37950
  3234
neuper@38036
  3235
(* if each pat* matches with the current term, the Rrls is applied
neuper@38036
  3236
   (there are no preconditions to be checked, they are HOLogic.true_const) *)
neuper@38037
  3237
val pat0 = parse_patt thy "?r/?s+?u/?v :: real";
neuper@38037
  3238
val pat1 = parse_patt thy "?r/?s+?u    :: real";
neuper@38037
  3239
val pat2 = parse_patt thy "?r   +?u/?v :: real";
neuper@37950
  3240
val prepat = [([HOLogic.true_const], pat0),
neuper@37950
  3241
	      ([HOLogic.true_const], pat1),
neuper@37950
  3242
	      ([HOLogic.true_const], pat2)];
neuper@37950
  3243
in
neuper@37950
  3244
neuper@37950
  3245
(*11.02 schnelle L"osung f"ur RL: Bruch auch gek"urzt;
neuper@37950
  3246
  besser w"are: auf 1 gemeinsamen Bruchstrich, Nenner und Z"ahler unvereinfacht
neuper@37950
  3247
  dh. wie common_nominator_p_, aber auf 1 Bruchstrich*)
neuper@37950
  3248
val common_nominator_p =
neuper@37950
  3249
    Rrls {id = "common_nominator_p", prepat=prepat,
neuper@37950
  3250
	  rew_ord=("ord_make_polynomial",
neuper@37972
  3251
		   ord_make_polynomial false thy),
neuper@37950
  3252
	  erls = rational_erls,
neuper@38014
  3253
	  calc = [("PLUS"    ,("Groups.plus_class.plus"        ,eval_binop "#add_")),
neuper@38034
  3254
		  ("TIMES"   ,("Groups.times_class.times"        ,eval_binop "#mult_")),
neuper@38014
  3255
		  ("DIVIDE" ,("Rings.inverse_class.divide"  ,eval_cancel "#divide_e")),
neuper@37950
  3256
		  ("POWER"  ,("Atools.pow"  ,eval_binop "#power_"))],
neuper@37950
  3257
	  scr=Rfuns {init_state  = init_state thy Atools_erls ro,
neuper@37950
  3258
		     normal_form = add_fraction_p_ thy,(*FIXME.WN0211*)
neuper@37950
  3259
		     locate_rule = locate_rule thy Atools_erls ro,
neuper@37950
  3260
		     next_rule   = next_rule thy Atools_erls ro,
neuper@37950
  3261
		     attach_form = attach_form}}
neuper@37950
  3262
end;(*local*)
neuper@37950
  3263
neuper@37950
  3264
local(*.ad [2] 'common_nominator'
neuper@37950
  3265
---------------------------------
neuper@37950
  3266
FIXME Beschreibung .*)
neuper@37950
  3267
neuper@37950
  3268
neuper@37950
  3269
val {rules=rules,rew_ord=(_,ro),...} =
neuper@37950
  3270
    rep_rls (assoc_rls "make_polynomial");
neuper@37972
  3271
val thy = thy;
neuper@37950
  3272
neuper@37950
  3273
neuper@37950
  3274
(*.common_nominator_ = fn : theory -> term -> (term * term list) option
neuper@37950
  3275
  as defined above*)
neuper@37950
  3276
neuper@37950
  3277
(*.init_state = fn : term -> istate
neuper@37950
  3278
initialzies the state of the interactive interpreter. The state is:
neuper@37950
  3279
type rrlsstate =      (*state for reverse rewriting*)
neuper@37950
  3280
     (term *          (*the current formula*)
neuper@37950
  3281
      term *          (*the final term*)
neuper@37950
  3282
      rule list       (*'reverse rule list' (#)*)
neuper@37950
  3283
	    list *    (*may be serveral, eg. in norm_rational*)
neuper@37950
  3284
      (rule *         (*Thm (+ Thm generated from Calc) resulting in ...*)
neuper@37950
  3285
       (term *        (*... rewrite with ...*)
neuper@37950
  3286
	term list))   (*... assumptions*)
neuper@37950
  3287
	  list);      (*derivation from given term to normalform
neuper@37950
  3288
		       in reverse order with sym_thm;
neuper@37950
  3289
                       (#) could be extracted from here by (map #1)*).*)
neuper@37950
  3290
fun init_state thy eval_rls ro t =
neuper@37950
  3291
    let val SOME (t',_) = common_nominator_ thy t;
neuper@37950
  3292
        val SOME (t'',asm) = add_fraction_ thy t;
neuper@37950
  3293
        val der = reverse_deriv thy eval_rls rules ro NONE t';
neuper@37950
  3294
        val der = der @ [(Thm ("real_mult_div_cancel2",
neuper@37969
  3295
			       num_str @{thm real_mult_div_cancel2}),
neuper@37950
  3296
			  (t'',asm))]
neuper@37950
  3297
        val rs = (distinct_Thm o (map #1)) der;
neuper@37950
  3298
	val rs = filter_out (eq_Thms ["sym_real_add_zero_left",
neuper@37950
  3299
				      "sym_real_mult_0",
neuper@37950
  3300
				      "sym_real_mult_1"]) rs;
neuper@37950
  3301
    in (t,t'',[rs(*here only _ONE_*)],der) end;
neuper@37950
  3302
neuper@37950
  3303
(*.locate_rule = fn : rule list -> term -> rule
neuper@37950
  3304
		      -> (rule * (term * term list) option) list.
neuper@37950
  3305
  checks a rule R for being a cancel-rule, and if it is,
neuper@37950
  3306
  then return the list of rules (+ the terms they are rewriting to)
neuper@37950
  3307
  which need to be applied before R should be applied.
neuper@37950
  3308
  precondition: the rule is applicable to the argument-term.
neuper@37950
  3309
arguments:
neuper@37950
  3310
  rule list: the reverse rule list
neuper@37950
  3311
  -> term  : ... to which the rule shall be applied
neuper@37950
  3312
  -> rule  : ... to be applied to term
neuper@37950
  3313
value:
neuper@37950
  3314
  -> (rule           : a rule rewriting to ...
neuper@37950
  3315
      * (term        : ... the resulting term ...
neuper@37950
  3316
         * term list): ... with the assumptions ( //#0).
neuper@37950
  3317
      ) list         : there may be several such rules;
neuper@37950
  3318
		       the list is empty, if the rule has nothing to do
neuper@37950
  3319
		       with cancelation.*)
neuper@37950
  3320
(* val () = ();
neuper@37950
  3321
   *)
neuper@37950
  3322
fun locate_rule thy eval_rls ro [rs] t r =
neuper@37950
  3323
    if (id_of_thm r) mem (map (id_of_thm)) rs
neuper@37950
  3324
    then let val ropt =
neuper@37950
  3325
		 rewrite_ thy ro eval_rls true (thm_of_thm r) t;
neuper@37950
  3326
	 in case ropt of
neuper@37950
  3327
		SOME ta => [(r, ta)]
neuper@38015
  3328
	      | NONE => (tracing("### locate_rule:  rewrite "^
neuper@37950
  3329
				 (id_of_thm r)^" "^(term2str t)^" = NONE");
neuper@37950
  3330
			 []) end
neuper@38015
  3331
    else (tracing("### locate_rule:  "^(id_of_thm r)^" not mem rrls");[])
neuper@37950
  3332
  | locate_rule _ _ _ _ _ _ =
neuper@38031
  3333
    error ("locate_rule: doesnt match rev-sets in istate");
neuper@37950
  3334
neuper@37950
  3335
(*.next_rule = fn : rule list -> term -> rule option
neuper@37950
  3336
  for a given term return the next rules to be done for cancelling.
neuper@37950
  3337
arguments:
neuper@37950
  3338
  rule list     : the reverse rule list
neuper@37950
  3339
  term          : the term for which ...
neuper@37950
  3340
value:
neuper@37950
  3341
  -> rule option: ... this rule is appropriate for cancellation;
neuper@37950
  3342
		  there may be no such rule (if the term is canceled already.*)
neuper@37972
  3343
(* val thy = thy;
neuper@37950
  3344
   val Rrls {rew_ord=(_,ro),...} = cancel;
neuper@37950
  3345
   val ([rs],t) = (rss,f);
neuper@37950
  3346
   next_rule thy eval_rls ro [rs] t;(*eval fun next_rule ... before!*)
neuper@37950
  3347
neuper@37972
  3348
   val (thy, [rs]) = (thy, revsets);
neuper@37950
  3349
   val Rrls {rew_ord=(_,ro),...} = cancel_p;
neuper@37950
  3350
   nex [rs] t;
neuper@37950
  3351
   *)
neuper@37950
  3352
fun next_rule thy eval_rls ro [rs] t =
neuper@37950
  3353
    let val der = make_deriv thy eval_rls rs ro NONE t;
neuper@37950
  3354
    in case der of
neuper@37950
  3355
(* val (_,r,_)::_ = der;
neuper@37950
  3356
   *)
neuper@37950
  3357
	   (_,r,_)::_ => SOME r
neuper@37950
  3358
	 | _ => NONE
neuper@37950
  3359
    end
neuper@37950
  3360
  | next_rule _ _ _ _ _ =
neuper@38031
  3361
    error ("next_rule: doesnt match rev-sets in istate");
neuper@37950
  3362
neuper@37950
  3363
(*.val attach_form = f : rule list -> term -> term
neuper@37950
  3364
			 -> (rule * (term * term list)) list
neuper@37950
  3365
  checks an input term TI, if it may belong to a current cancellation, by
neuper@37950
  3366
  trying to derive it from the given term TG.
neuper@37950
  3367
arguments:
neuper@37950
  3368
  term   : TG, the last one in the cancellation agreed upon by user + math-eng
neuper@37950
  3369
  -> term: TI, the next one input by the user
neuper@37950
  3370
value:
neuper@37950
  3371
  -> (rule           : the rule to be applied in order to reach TI
neuper@37950
  3372
      * (term        : ... obtained by applying the rule ...
neuper@37950
  3373
         * term list): ... and the respective assumptions.
neuper@37950
  3374
      ) list         : there may be several such rules;
neuper@37950
  3375
                       the list is empty, if the users term does not belong
neuper@37950
  3376
		       to a cancellation of the term last agreed upon.*)
neuper@37950
  3377
fun attach_form (_:rule list list) (_:term) (_:term) = (*still missing*)
neuper@37950
  3378
    []:(rule * (term * term list)) list;
neuper@37950
  3379
neuper@38036
  3380
(* if each pat* matches with the current term, the Rrls is applied
neuper@38036
  3381
   (there are no preconditions to be checked, they are HOLogic.true_const) *)
neuper@38037
  3382
val pat0 =  parse_patt thy "?r/?s+?u/?v :: real";
neuper@38037
  3383
val pat01 = parse_patt thy "?r/?s-?u/?v :: real";
neuper@38037
  3384
val pat1 =  parse_patt thy "?r/?s+?u    :: real";
neuper@38037
  3385
val pat11 = parse_patt thy "?r/?s-?u    :: real";
neuper@38037
  3386
val pat2 =  parse_patt thy "?r   +?u/?v :: real";
neuper@38037
  3387
val pat21 = parse_patt thy "?r   -?u/?v :: real";
neuper@37950
  3388
val prepat = [([HOLogic.true_const], pat0),
neuper@37950
  3389
	      ([HOLogic.true_const], pat01),
neuper@37950
  3390
	      ([HOLogic.true_const], pat1),
neuper@37950
  3391
	      ([HOLogic.true_const], pat11),
neuper@37950
  3392
	      ([HOLogic.true_const], pat2),
neuper@37950
  3393
	      ([HOLogic.true_const], pat21)];
neuper@37950
  3394
neuper@37950
  3395
neuper@37950
  3396
in
neuper@37950
  3397
neuper@37950
  3398
val common_nominator =
neuper@37950
  3399
    Rrls {id = "common_nominator", prepat=prepat,
neuper@37950
  3400
	  rew_ord=("ord_make_polynomial",
neuper@37972
  3401
		   ord_make_polynomial false thy),
neuper@37950
  3402
	  erls = rational_erls,
neuper@38014
  3403
	  calc = [("PLUS"    ,("Groups.plus_class.plus"        ,eval_binop "#add_")),
neuper@38034
  3404
		  ("TIMES"   ,("Groups.times_class.times"        ,eval_binop "#mult_")),
neuper@38014
  3405
		  ("DIVIDE" ,("Rings.inverse_class.divide"  ,eval_cancel "#divide_e")),
neuper@37950
  3406
		  ("POWER"  ,("Atools.pow"  ,eval_binop "#power_"))],
neuper@37950
  3407
	  (*asm_thm=[("real_mult_div_cancel2","")],*)
neuper@37950
  3408
	  scr=Rfuns {init_state  = init_state thy Atools_erls ro,
neuper@37950
  3409
		     normal_form = add_fraction_ (*NOT common_nominator_*) thy,
neuper@37950
  3410
		     locate_rule = locate_rule thy Atools_erls ro,
neuper@37950
  3411
		     next_rule   = next_rule thy Atools_erls ro,
neuper@37950
  3412
		     attach_form = attach_form}}
neuper@37950
  3413
neuper@37950
  3414
end;(*local*)
neuper@37950
  3415
end;(*struct*)
neuper@37950
  3416
neuper@37950
  3417
open RationalI;
neuper@37950
  3418
(*##*)
neuper@37950
  3419
neuper@37950
  3420
(*.the expression contains + - * ^ / only ?.*)
neuper@37950
  3421
fun is_ratpolyexp (Free _) = true
neuper@38014
  3422
  | is_ratpolyexp (Const ("Groups.plus_class.plus",_) $ Free _ $ Free _) = true
neuper@38014
  3423
  | is_ratpolyexp (Const ("Groups.minus_class.minus",_) $ Free _ $ Free _) = true
neuper@38034
  3424
  | is_ratpolyexp (Const ("Groups.times_class.times",_) $ Free _ $ Free _) = true
neuper@37950
  3425
  | is_ratpolyexp (Const ("Atools.pow",_) $ Free _ $ Free _) = true
neuper@38014
  3426
  | is_ratpolyexp (Const ("Rings.inverse_class.divide",_) $ Free _ $ Free _) = true
neuper@38014
  3427
  | is_ratpolyexp (Const ("Groups.plus_class.plus",_) $ t1 $ t2) = 
neuper@37950
  3428
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@38014
  3429
  | is_ratpolyexp (Const ("Groups.minus_class.minus",_) $ t1 $ t2) = 
neuper@37950
  3430
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@38034
  3431
  | is_ratpolyexp (Const ("Groups.times_class.times",_) $ t1 $ t2) = 
neuper@37950
  3432
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@37950
  3433
  | is_ratpolyexp (Const ("Atools.pow",_) $ t1 $ t2) = 
neuper@37950
  3434
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@38014
  3435
  | is_ratpolyexp (Const ("Rings.inverse_class.divide",_) $ t1 $ t2) = 
neuper@37950
  3436
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@37950
  3437
  | is_ratpolyexp _ = false;
neuper@37950
  3438
neuper@37950
  3439
(*("is_ratpolyexp", ("Rational.is'_ratpolyexp", eval_is_ratpolyexp ""))*)
neuper@37950
  3440
fun eval_is_ratpolyexp (thmid:string) _ 
neuper@37950
  3441
		       (t as (Const("Rational.is'_ratpolyexp", _) $ arg)) thy =
neuper@37950
  3442
    if is_ratpolyexp arg
neuper@37950
  3443
    then SOME (mk_thmid thmid "" 
neuper@38053
  3444
			(Print_Mode.setmp [] (Syntax.string_of_term
neuper@38053
  3445
                                              (thy2ctxt thy)) arg) "", 
neuper@37950
  3446
	       Trueprop $ (mk_equality (t, HOLogic.true_const)))
neuper@37950
  3447
    else SOME (mk_thmid thmid "" 
neuper@38053
  3448
			(Print_Mode.setmp [] (Syntax.string_of_term
neuper@38053
  3449
                                              (thy2ctxt thy)) arg) "", 
neuper@37950
  3450
	       Trueprop $ (mk_equality (t, HOLogic.false_const)))
neuper@37950
  3451
  | eval_is_ratpolyexp _ _ _ _ = NONE; 
neuper@37950
  3452
jan@42300
  3453
(*("get_denominator", ("Rational.get'_denominator", eval_get_denominator ""))*)
jan@42300
  3454
fun eval_get_denominator (thmid:string) _ 
jan@42300
  3455
		      (t as Const ("Rational.get'_denominator", _) $
jan@42300
  3456
              (Const ("Rings.inverse_class.divide", _) $ num $
jan@42300
  3457
                denom)) thy = 
jan@42300
  3458
jan@42300
  3459
    SOME (mk_thmid thmid "" 
jan@42300
  3460
            (Print_Mode.setmp [] (Syntax.string_of_term (thy2ctxt thy)) denom) "", 
jan@42300
  3461
	          Trueprop $ (mk_equality (t, denom)))
jan@42300
  3462
jan@42300
  3463
  | eval_get_denominator _ _ _ _ = NONE; 
neuper@37950
  3464
neuper@37950
  3465
neuper@37950
  3466
(*-------------------18.3.03 --> struct <-----------vvv--*)
neuper@37950
  3467
val add_fractions_p = common_nominator_p; (*FIXXXME:eilig f"ur norm_Rational*)
neuper@37950
  3468
neuper@37980
  3469
(*WN100906 removed in favour of discard_minus = discard_minus_ formerly 
neuper@37980
  3470
   discard binary minus, shift unary minus into -1*; 
neuper@37950
  3471
   unary minus before numerals are put into the numeral by parsing;
neuper@37980
  3472
   contains absolute minimum of thms for context in norm_Rational
neuper@37950
  3473
val discard_minus = prep_rls(
neuper@37950
  3474
  Rls {id = "discard_minus", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
neuper@37980
  3475
      erls = e_rls, srls = Erls, calc = [],
neuper@37969
  3476
      rules = [Thm ("real_diff_minus", num_str @{thm real_diff_minus}),
neuper@37950
  3477
	       (*"a - b = a + -1 * b"*)
neuper@37969
  3478
	       Thm ("sym_real_mult_minus1",
neuper@37969
  3479
                     num_str (@{thm real_mult_minus1} RS @{thm sym}))
neuper@37950
  3480
	       (*- ?z = "-1 * ?z"*)
neuper@37950
  3481
	       ],
neuper@37979
  3482
      scr = EmptyScr
neuper@37950
  3483
      }):rls;
neuper@37980
  3484
 *)
neuper@37980
  3485
neuper@37950
  3486
(*erls for calculate_Rational; make local with FIXX@ME result:term *term list*)
neuper@37950
  3487
val powers_erls = prep_rls(
neuper@37950
  3488
  Rls {id = "powers_erls", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3489
      erls = e_rls, srls = Erls, calc = [], (*asm_thm = [],*)
neuper@37950
  3490
      rules = [Calc ("Atools.is'_atom",eval_is_atom "#is_atom_"),
neuper@37950
  3491
	       Calc ("Atools.is'_even",eval_is_even "#is_even_"),
neuper@38045
  3492
	       Calc ("Orderings.ord_class.less",eval_equ "#less_"),
neuper@37979
  3493
	       Thm ("not_false", num_str @{thm not_false}),
neuper@37979
  3494
	       Thm ("not_true", num_str @{thm not_true}),
neuper@38014
  3495
	       Calc ("Groups.plus_class.plus",eval_binop "#add_")
neuper@37950
  3496
	       ],
neuper@37979
  3497
      scr = EmptyScr
neuper@37950
  3498
      }:rls);
neuper@37950
  3499
(*.all powers over + distributed; atoms over * collected, other distributed
neuper@37950
  3500
   contains absolute minimum of thms for context in norm_Rational .*)
neuper@37950
  3501
val powers = prep_rls(
neuper@37950
  3502
  Rls {id = "powers", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3503
      erls = powers_erls, srls = Erls, calc = [], (*asm_thm = [],*)
neuper@37969
  3504
      rules = [Thm ("realpow_multI", num_str @{thm realpow_multI}),
neuper@37950
  3505
	       (*"(r * s) ^^^ n = r ^^^ n * s ^^^ n"*)
neuper@37969
  3506
	       Thm ("realpow_pow",num_str @{thm realpow_pow}),
neuper@37950
  3507
	       (*"(a ^^^ b) ^^^ c = a ^^^ (b * c)"*)
neuper@37969
  3508
	       Thm ("realpow_oneI",num_str @{thm realpow_oneI}),
neuper@37950
  3509
	       (*"r ^^^ 1 = r"*)
neuper@37969
  3510
	       Thm ("realpow_minus_even",num_str @{thm realpow_minus_even}),
neuper@37950
  3511
	       (*"n is_even ==> (- r) ^^^ n = r ^^^ n" ?-->discard_minus?*)
neuper@37969
  3512
	       Thm ("realpow_minus_odd",num_str @{thm realpow_minus_odd}),
neuper@37950
  3513
	       (*"Not (n is_even) ==> (- r) ^^^ n = -1 * r ^^^ n"*)
neuper@37950
  3514
	       
neuper@37950
  3515
	       (*----- collect atoms over * -----*)
neuper@37969
  3516
	       Thm ("realpow_two_atom",num_str @{thm realpow_two_atom}),	
neuper@37950
  3517
	       (*"r is_atom ==> r * r = r ^^^ 2"*)
neuper@37969
  3518
	       Thm ("realpow_plus_1",num_str @{thm realpow_plus_1}),		
neuper@37950
  3519
	       (*"r is_atom ==> r * r ^^^ n = r ^^^ (n + 1)"*)
neuper@37969
  3520
	       Thm ("realpow_addI_atom",num_str @{thm realpow_addI_atom}),
neuper@37950
  3521
	       (*"r is_atom ==> r ^^^ n * r ^^^ m = r ^^^ (n + m)"*)
neuper@37950
  3522
neuper@37950
  3523
	       (*----- distribute none-atoms -----*)
neuper@37969
  3524
	       Thm ("realpow_def_atom",num_str @{thm realpow_def_atom}),
neuper@37950
  3525
	       (*"[| 1 < n; not(r is_atom) |]==>r ^^^ n = r * r ^^^ (n + -1)"*)
neuper@37969
  3526
	       Thm ("realpow_eq_oneI",num_str @{thm realpow_eq_oneI}),
neuper@37950
  3527
	       (*"1 ^^^ n = 1"*)
neuper@38014
  3528
	       Calc ("Groups.plus_class.plus",eval_binop "#add_")
neuper@37950
  3529
	       ],
neuper@37979
  3530
      scr = EmptyScr
neuper@37950
  3531
      }:rls);
neuper@37950
  3532
(*.contains absolute minimum of thms for context in norm_Rational.*)
neuper@37950
  3533
val rat_mult_divide = prep_rls(
neuper@37950
  3534
  Rls {id = "rat_mult_divide", preconds = [], 
neuper@37950
  3535
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3536
      erls = e_rls, srls = Erls, calc = [], (*asm_thm = [],*)
neuper@37969
  3537
      rules = [Thm ("rat_mult",num_str @{thm rat_mult}),
neuper@37950
  3538
	       (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
neuper@37965
  3539
	       Thm ("times_divide_eq_right",num_str @{thm times_divide_eq_right}),
neuper@37950
  3540
	       (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
neuper@37950
  3541
	       otherwise inv.to a / b / c = ...*)
neuper@37965
  3542
	       Thm ("times_divide_eq_left",num_str @{thm times_divide_eq_left}),
neuper@37950
  3543
	       (*"?a / ?b * ?c = ?a * ?c / ?b" order weights x^^^n too much
neuper@37950
  3544
		     and does not commute a / b * c ^^^ 2 !*)
neuper@37950
  3545
	       
neuper@37979
  3546
	       Thm ("divide_divide_eq_right", 
neuper@37979
  3547
                     num_str @{thm divide_divide_eq_right}),
neuper@37950
  3548
	       (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
neuper@37979
  3549
	       Thm ("divide_divide_eq_left",
neuper@37979
  3550
                     num_str @{thm divide_divide_eq_left}),
neuper@37950
  3551
	       (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
neuper@38014
  3552
	       Calc ("Rings.inverse_class.divide"  ,eval_cancel "#divide_e")
neuper@37950
  3553
	       ],
neuper@37979
  3554
      scr = EmptyScr
neuper@37950
  3555
      }:rls);
neuper@37979
  3556
neuper@37950
  3557
(*.contains absolute minimum of thms for context in norm_Rational.*)
neuper@37950
  3558
val reduce_0_1_2 = prep_rls(
neuper@37950
  3559
  Rls{id = "reduce_0_1_2", preconds = [], rew_ord = ("dummy_ord", dummy_ord),
neuper@37950
  3560
      erls = e_rls,srls = Erls,calc = [],(*asm_thm = [],*)
neuper@37965
  3561
      rules = [(*Thm ("divide_1",num_str @{thm divide_1}),
neuper@37950
  3562
		 "?x / 1 = ?x" unnecess.for normalform*)
neuper@37965
  3563
	       Thm ("mult_1_left",num_str @{thm mult_1_left}),                 
neuper@37950
  3564
	       (*"1 * z = z"*)
neuper@37969
  3565
	       (*Thm ("real_mult_minus1",num_str @{thm real_mult_minus1}),
neuper@37950
  3566
	       "-1 * z = - z"*)
neuper@37969
  3567
	       (*Thm ("real_minus_mult_cancel",num_str @{thm real_minus_mult_cancel}),
neuper@37950
  3568
	       "- ?x * - ?y = ?x * ?y"*)
neuper@37950
  3569
neuper@37965
  3570
	       Thm ("mult_zero_left",num_str @{thm mult_zero_left}),        
neuper@37950
  3571
	       (*"0 * z = 0"*)
neuper@37965
  3572
	       Thm ("add_0_left",num_str @{thm add_0_left}),
neuper@37950
  3573
	       (*"0 + z = z"*)
neuper@37965
  3574
	       (*Thm ("right_minus",num_str @{thm right_minus}),
neuper@37950
  3575
	       "?z + - ?z = 0"*)
neuper@37950
  3576
neuper@37969
  3577
	       Thm ("sym_real_mult_2",
neuper@37969
  3578
                     num_str (@{thm real_mult_2} RS @{thm sym})),	
neuper@37950
  3579
	       (*"z1 + z1 = 2 * z1"*)
neuper@37969
  3580
	       Thm ("real_mult_2_assoc",num_str @{thm real_mult_2_assoc}),
neuper@37950
  3581
	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
neuper@37950
  3582
neuper@37965
  3583
	       Thm ("divide_zero_left",num_str @{thm divide_zero_left})
neuper@37950
  3584
	       (*"0 / ?x = 0"*)
neuper@37950
  3585
	       ], scr = EmptyScr}:rls);
neuper@37950
  3586
neuper@37950
  3587
(*erls for calculate_Rational; 
neuper@37950
  3588
  make local with FIXX@ME result:term *term list WN0609???SKMG*)
neuper@37950
  3589
val norm_rat_erls = prep_rls(
neuper@37950
  3590
  Rls {id = "norm_rat_erls", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3591
      erls = e_rls, srls = Erls, calc = [], (*asm_thm = [],*)
neuper@37950
  3592
      rules = [Calc ("Atools.is'_const",eval_const "#is_const_")
neuper@37979
  3593
	       ], scr = EmptyScr}:rls);
neuper@37979
  3594
neuper@37950
  3595
(*.consists of rls containing the absolute minimum of thms.*)
neuper@37950
  3596
(*040209: this version has been used by RL for his equations,
neuper@37950
  3597
which is now replaced by MGs version below
neuper@37950
  3598
vvv OLD VERSION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
neuper@37950
  3599
val norm_Rational = prep_rls(
neuper@37950
  3600
  Rls {id = "norm_Rational", preconds = [], rew_ord = ("dummy_ord",dummy_ord), 
neuper@37979
  3601
      erls = norm_rat_erls, srls = Erls, calc = [],
neuper@37950
  3602
      rules = [(*sequence given by operator precedence*)
neuper@37950
  3603
	       Rls_ discard_minus,
neuper@37950
  3604
	       Rls_ powers,
neuper@37950
  3605
	       Rls_ rat_mult_divide,
neuper@37950
  3606
	       Rls_ expand,
neuper@37950
  3607
	       Rls_ reduce_0_1_2,
neuper@37950
  3608
	       (*^^^^^^^^^ from RL -- not the latest one vvvvvvvvv*)
neuper@37950
  3609
	       Rls_ order_add_mult,
neuper@37950
  3610
	       Rls_ collect_numerals,
neuper@37950
  3611
	       Rls_ add_fractions_p,
neuper@37950
  3612
	       Rls_ cancel_p
neuper@37950
  3613
	       ],
neuper@37979
  3614
      scr = EmptyScr}:rls);
neuper@37979
  3615
neuper@37950
  3616
val norm_Rational_parenthesized = prep_rls(
neuper@37950
  3617
  Seq {id = "norm_Rational_parenthesized", preconds = []:term list, 
neuper@37950
  3618
       rew_ord = ("dummy_ord", dummy_ord),
neuper@37950
  3619
      erls = Atools_erls, srls = Erls,
neuper@37979
  3620
      calc = [],
neuper@37950
  3621
      rules = [Rls_  norm_Rational, (*from RL -- not the latest one*)
neuper@37950
  3622
	       Rls_ discard_parentheses
neuper@37950
  3623
	       ],
neuper@37979
  3624
      scr = EmptyScr}:rls);      
neuper@37950
  3625
neuper@37950
  3626
(*WN030318???SK: simplifies all but cancel and common_nominator*)
neuper@37950
  3627
val simplify_rational = 
neuper@37950
  3628
    merge_rls "simplify_rational" expand_binoms
neuper@37950
  3629
    (append_rls "divide" calculate_Rational
neuper@37965
  3630
		[Thm ("divide_1",num_str @{thm divide_1}),
neuper@37950
  3631
		 (*"?x / 1 = ?x"*)
neuper@37978
  3632
		 Thm ("rat_mult",num_str @{thm rat_mult}),
neuper@37950
  3633
		 (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
neuper@37965
  3634
		 Thm ("times_divide_eq_right",num_str @{thm times_divide_eq_right}),
neuper@37950
  3635
		 (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
neuper@37950
  3636
		 otherwise inv.to a / b / c = ...*)
neuper@37965
  3637
		 Thm ("times_divide_eq_left",num_str @{thm times_divide_eq_left}),
neuper@37950
  3638
		 (*"?a / ?b * ?c = ?a * ?c / ?b"*)
neuper@37969
  3639
		 Thm ("add_minus",num_str @{thm add_minus}),
neuper@37950
  3640
		 (*"?a + ?b - ?b = ?a"*)
neuper@37969
  3641
		 Thm ("add_minus1",num_str @{thm add_minus1}),
neuper@37950
  3642
		 (*"?a - ?b + ?b = ?a"*)
neuper@37978
  3643
		 Thm ("divide_minus1",num_str @{thm divide_minus1})
neuper@37950
  3644
		 (*"?x / -1 = - ?x"*)
neuper@37950
  3645
(*
neuper@37950
  3646
,
neuper@37969
  3647
		 Thm ("",num_str @{thm })
neuper@37950
  3648
*)
neuper@37950
  3649
		 ]);
neuper@37950
  3650
neuper@37950
  3651
(*---------vvv-------------MG ab 1.07.2003--------------vvv-----------*)
neuper@37950
  3652
neuper@37950
  3653
(* ------------------------------------------------------------------ *)
neuper@37950
  3654
(*                  Simplifier für beliebige Buchterme                *) 
neuper@37950
  3655
(* ------------------------------------------------------------------ *)
neuper@37950
  3656
(*----------------------- norm_Rational_mg ---------------------------*)
neuper@37950
  3657
(*. description of the simplifier see MG-DA.p.56ff .*)
neuper@37950
  3658
(* ------------------------------------------------------------------- *)
neuper@37950
  3659
val common_nominator_p_rls = prep_rls(
neuper@37950
  3660
  Rls {id = "common_nominator_p_rls", preconds = [],
neuper@37950
  3661
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3662
	 erls = e_rls, srls = Erls, calc = [],
neuper@37950
  3663
	 rules = 
neuper@37950
  3664
	 [Rls_ common_nominator_p
neuper@37950
  3665
	  (*FIXME.WN0401 ? redesign Rrls - use exhaustively on a term ?
neuper@37950
  3666
	    FIXME.WN0510 unnecessary nesting: introduce RRls_ : rls -> rule*)
neuper@37950
  3667
	  ], 
neuper@37950
  3668
	 scr = EmptyScr});
neuper@37950
  3669
(* ------------------------------------------------------------------- *)
neuper@37950
  3670
val cancel_p_rls = prep_rls(
neuper@37950
  3671
  Rls {id = "cancel_p_rls", preconds = [],
neuper@37950
  3672
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3673
	 erls = e_rls, srls = Erls, calc = [],
neuper@37950
  3674
	 rules = 
neuper@37950
  3675
	 [Rls_ cancel_p
neuper@37950
  3676
	  (*FIXME.WN.0401 ? redesign Rrls - use exhaustively on a term ?*)
neuper@37950
  3677
	  ], 
neuper@37950
  3678
	 scr = EmptyScr});
neuper@37950
  3679
(* -------------------------------------------------------------------- *)
neuper@37950
  3680
(*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
neuper@37950
  3681
    used in initial part norm_Rational_mg, see example DA-M02-main.p.60.*)
neuper@37950
  3682
val rat_mult_poly = prep_rls(
neuper@37950
  3683
  Rls {id = "rat_mult_poly", preconds = [],
neuper@37950
  3684
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3685
	 erls =  append_rls "e_rls-is_polyexp" e_rls
neuper@37950
  3686
	         [Calc ("Poly.is'_polyexp", eval_is_polyexp "")], 
neuper@37950
  3687
	 srls = Erls, calc = [],
neuper@37950
  3688
	 rules = 
neuper@37969
  3689
	 [Thm ("rat_mult_poly_l",num_str @{thm rat_mult_poly_l}),
neuper@37950
  3690
	  (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
neuper@37969
  3691
	  Thm ("rat_mult_poly_r",num_str @{thm rat_mult_poly_r})
neuper@37950
  3692
	  (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
neuper@37950
  3693
	  ], 
neuper@37950
  3694
	 scr = EmptyScr});
neuper@37979
  3695
neuper@37950
  3696
(* ------------------------------------------------------------------ *)
neuper@37950
  3697
(*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
neuper@37950
  3698
    used in looping part norm_Rational_rls, see example DA-M02-main.p.60 
neuper@37950
  3699
    .. WHERE THE LATTER DOES ALWAYS WORK, BECAUSE erls = e_rls, 
neuper@37950
  3700
    I.E. THE RESPECTIVE ASSUMPTION IS STORED AND Thm APPLIED; WN051028 
neuper@37950
  3701
    ... WN0609???MG.*)
neuper@37950
  3702
val rat_mult_div_pow = prep_rls(
neuper@37950
  3703
  Rls {id = "rat_mult_div_pow", preconds = [], 
neuper@37950
  3704
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3705
       erls = e_rls,
neuper@37950
  3706
       (*FIXME.WN051028 append_rls "e_rls-is_polyexp" e_rls
neuper@37950
  3707
			[Calc ("Poly.is'_polyexp", eval_is_polyexp "")],
neuper@37950
  3708
         with this correction ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ we get 
neuper@37950
  3709
	 error "rational.sml.sml: diff.behav. in norm_Rational_mg 29" etc.
neuper@37950
  3710
         thus we decided to go on with this flaw*)
neuper@37950
  3711
		 srls = Erls, calc = [],
neuper@37969
  3712
      rules = [Thm ("rat_mult",num_str @{thm rat_mult}),
neuper@37950
  3713
	       (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
neuper@37969
  3714
	       Thm ("rat_mult_poly_l",num_str @{thm rat_mult_poly_l}),
neuper@37950
  3715
	       (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
neuper@37969
  3716
	       Thm ("rat_mult_poly_r",num_str @{thm rat_mult_poly_r}),
neuper@37950
  3717
	       (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
neuper@37950
  3718
neuper@37979
  3719
	       Thm ("real_divide_divide1_mg",
neuper@37979
  3720
                     num_str @{thm real_divide_divide1_mg}),
neuper@37950
  3721
	       (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
neuper@37979
  3722
	       Thm ("divide_divide_eq_right",
neuper@37979
  3723
                     num_str @{thm divide_divide_eq_right}),
neuper@37950
  3724
	       (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
neuper@37979
  3725
	       Thm ("divide_divide_eq_left",
neuper@37979
  3726
                     num_str @{thm divide_divide_eq_left}),
neuper@37950
  3727
	       (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
neuper@38014
  3728
	       Calc ("Rings.inverse_class.divide"  ,eval_cancel "#divide_e"),
neuper@37950
  3729
	      
neuper@37969
  3730
	       Thm ("rat_power", num_str @{thm rat_power})
neuper@37950
  3731
		(*"(?a / ?b) ^^^ ?n = ?a ^^^ ?n / ?b ^^^ ?n"*)
neuper@37950
  3732
	       ],
neuper@37979
  3733
      scr = EmptyScr}:rls);
neuper@37950
  3734
(* ------------------------------------------------------------------ *)
neuper@37950
  3735
val rat_reduce_1 = prep_rls(
neuper@37950
  3736
  Rls {id = "rat_reduce_1", preconds = [], 
neuper@37950
  3737
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3738
       erls = e_rls, srls = Erls, calc = [], 
neuper@37965
  3739
       rules = [Thm ("divide_1",num_str @{thm divide_1}),
neuper@37950
  3740
		(*"?x / 1 = ?x"*)
neuper@37965
  3741
		Thm ("mult_1_left",num_str @{thm mult_1_left})           
neuper@37950
  3742
		(*"1 * z = z"*)
neuper@37950
  3743
		],
neuper@37979
  3744
       scr = EmptyScr}:rls);
neuper@37950
  3745
(* ------------------------------------------------------------------ *)
neuper@37950
  3746
(*. looping part of norm_Rational(*_mg*) .*)
neuper@37950
  3747
val norm_Rational_rls = prep_rls(
neuper@37950
  3748
   Rls {id = "norm_Rational_rls", preconds = [], 
neuper@37950
  3749
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3750
       erls = norm_rat_erls, srls = Erls, calc = [],
neuper@37950
  3751
       rules = [Rls_ common_nominator_p_rls,
neuper@37950
  3752
		Rls_ rat_mult_div_pow,
neuper@37950
  3753
		Rls_ make_rat_poly_with_parentheses,
neuper@37950
  3754
		Rls_ cancel_p_rls,(*FIXME:cancel_p does NOT order sometimes*)
neuper@37950
  3755
		Rls_ rat_reduce_1
neuper@37950
  3756
		],
neuper@37979
  3757
       scr = EmptyScr}:rls);
neuper@37950
  3758
(* ------------------------------------------------------------------ *)
neuper@37950
  3759
(*040109 'norm_Rational'(by RL) replaced by 'norm_Rational_mg'(MG)
neuper@37950
  3760
 just be renaming:*)
neuper@37950
  3761
val norm_Rational(*_mg*) = prep_rls(
neuper@37950
  3762
   Seq {id = "norm_Rational"(*_mg*), preconds = [], 
neuper@37950
  3763
       rew_ord = ("dummy_ord",dummy_ord), 
neuper@37950
  3764
       erls = norm_rat_erls, srls = Erls, calc = [],
neuper@37980
  3765
       rules = [Rls_ discard_minus,
neuper@37950
  3766
		Rls_ rat_mult_poly,(* removes double fractions like a/b/c    *)
neuper@37950
  3767
		Rls_ make_rat_poly_with_parentheses, (*WN0510 also in(#)below*)
neuper@37950
  3768
		Rls_ cancel_p_rls, (*FIXME.MG:cancel_p does NOT order sometim*)
neuper@37950
  3769
		Rls_ norm_Rational_rls,   (* the main rls, looping (#)       *)
neuper@37979
  3770
		Rls_ discard_parentheses1 (* mult only                       *)
neuper@37950
  3771
		],
neuper@37979
  3772
       scr = EmptyScr}:rls);
neuper@37950
  3773
(* ------------------------------------------------------------------ *)
neuper@37950
  3774
neuper@37967
  3775
ruleset' := overwritelthy @{theory} (!ruleset',
neuper@37950
  3776
  [("calculate_Rational", calculate_Rational),
neuper@37950
  3777
   ("calc_rat_erls",calc_rat_erls),
neuper@37950
  3778
   ("rational_erls", rational_erls),
neuper@37950
  3779
   ("cancel_p", cancel_p),
neuper@37950
  3780
   ("cancel", cancel),
neuper@37950
  3781
   ("common_nominator_p", common_nominator_p),
neuper@37950
  3782
   ("common_nominator_p_rls", common_nominator_p_rls),
neuper@37950
  3783
   ("common_nominator"  , common_nominator),
neuper@37950
  3784
   ("discard_minus", discard_minus),
neuper@37950
  3785
   ("powers_erls", powers_erls),
neuper@37950
  3786
   ("powers", powers),
neuper@37950
  3787
   ("rat_mult_divide", rat_mult_divide),
neuper@37950
  3788
   ("reduce_0_1_2", reduce_0_1_2),
neuper@37950
  3789
   ("rat_reduce_1", rat_reduce_1),
neuper@37950
  3790
   ("norm_rat_erls", norm_rat_erls),
neuper@37950
  3791
   ("norm_Rational", norm_Rational),
neuper@37950
  3792
   ("norm_Rational_rls", norm_Rational_rls),
neuper@37950
  3793
   ("norm_Rational_parenthesized", norm_Rational_parenthesized),
neuper@37950
  3794
   ("rat_mult_poly", rat_mult_poly),
neuper@37950
  3795
   ("rat_mult_div_pow", rat_mult_div_pow),
neuper@37950
  3796
   ("cancel_p_rls", cancel_p_rls)
neuper@37950
  3797
   ]);
neuper@37950
  3798
neuper@37950
  3799
calclist':= overwritel (!calclist', 
neuper@37950
  3800
   [("is_expanded", ("Rational.is'_expanded", eval_is_expanded ""))
neuper@37950
  3801
    ]);
neuper@37950
  3802
neuper@37950
  3803
(** problems **)
neuper@37950
  3804
neuper@37950
  3805
store_pbt
neuper@37972
  3806
 (prep_pbt thy "pbl_simp_rat" [] e_pblID
neuper@37950
  3807
 (["rational","simplification"],
neuper@38083
  3808
  [("#Given" ,["Term t_t"]),
neuper@37979
  3809
   ("#Where" ,["t_t is_ratpolyexp"]),
neuper@37979
  3810
   ("#Find"  ,["normalform n_n"])
neuper@37950
  3811
  ],
neuper@37950
  3812
  append_rls "e_rls" e_rls [(*for preds in where_*)], 
neuper@38066
  3813
  SOME "Simplify t_t", 
neuper@37950
  3814
  [["simplification","of_rationals"]]));
neuper@37950
  3815
neuper@37950
  3816
(** methods **)
neuper@37979
  3817
val --------------------------------------------------+++++-+++++ = "00000";
neuper@37950
  3818
neuper@37950
  3819
(*WN061025 this methods script is copied from (auto-generated) script
neuper@37950
  3820
  of norm_Rational in order to ease repair on inform*)
neuper@37950
  3821
store_met
neuper@37972
  3822
    (prep_met thy "met_simp_rat" [] e_metID
neuper@37950
  3823
	      (["simplification","of_rationals"],
neuper@38083
  3824
	       [("#Given" ,["Term t_t"]),
neuper@37979
  3825
		("#Where" ,["t_t is_ratpolyexp"]),
neuper@37979
  3826
		("#Find"  ,["normalform n_n"])
neuper@37950
  3827
		],
neuper@37950
  3828
	       {rew_ord'="tless_true",
neuper@37950
  3829
		rls' = e_rls,
neuper@37950
  3830
		calc = [], srls = e_rls, 
neuper@37950
  3831
		prls = append_rls "simplification_of_rationals_prls" e_rls 
neuper@37950
  3832
				[(*for preds in where_*)
neuper@37950
  3833
				 Calc ("Rational.is'_ratpolyexp", 
neuper@37950
  3834
				       eval_is_ratpolyexp "")],
neuper@37950
  3835
		crls = e_rls, nrls = norm_Rational_rls},
neuper@37979
  3836
"Script SimplifyScript (t_t::real) =                             " ^
neuper@37980
  3837
"  ((Try (Rewrite_Set discard_minus False) @@                   " ^
neuper@37950
  3838
"    Try (Rewrite_Set rat_mult_poly False) @@                    " ^
neuper@37950
  3839
"    Try (Rewrite_Set make_rat_poly_with_parentheses False) @@   " ^
neuper@37950
  3840
"    Try (Rewrite_Set cancel_p_rls False) @@                     " ^
neuper@37950
  3841
"    (Repeat                                                     " ^
neuper@37950
  3842
"     ((Try (Rewrite_Set common_nominator_p_rls False) @@        " ^
neuper@37950
  3843
"       Try (Rewrite_Set rat_mult_div_pow False) @@              " ^
neuper@37950
  3844
"       Try (Rewrite_Set make_rat_poly_with_parentheses False) @@" ^
neuper@37950
  3845
"       Try (Rewrite_Set cancel_p_rls False) @@                  " ^
neuper@37950
  3846
"       Try (Rewrite_Set rat_reduce_1 False)))) @@               " ^
neuper@37979
  3847
"    Try (Rewrite_Set discard_parentheses1 False))               " ^
neuper@37979
  3848
"   t_t)"
neuper@37950
  3849
	       ));
neuper@37979
  3850
neuper@37950
  3851
*}
neuper@37950
  3852
neuper@37950
  3853
end