src/Tools/isac/Knowledge/Rational.thy
author Walther Neuper <neuper@ist.tugraz.at>
Fri, 12 Oct 2012 16:03:07 +0200
changeset 48760 5e1e45b3ddef
parent 42516 461f6d9d5390
child 48764 fd9145fbe471
permissions -rwxr-xr-x
2011-->2012:

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