test/Tools/isac/Knowledge/rational.sml
author Walther Neuper <neuper@ist.tugraz.at>
Mon, 16 Sep 2013 11:28:43 +0200
changeset 52104 83166e7c7e52
parent 52101 c3f399ce32af
child 52105 2786cc9704c8
permissions -rw-r--r--
gcd_poly requires special handling for a monomial argument
     1 (* Title: tests for rationals
     2    Author: Walther Neuper
     3    Use is subject to license terms.
     4 *)
     5 
     6 "-----------------------------------------------------------------------------";
     7 "-----------------------------------------------------------------------------";
     8 "table of contents -----------------------------------------------------------";
     9 "-----------------------------------------------------------------------------";
    10 "-------- fun poly_of_term ---------------------------------------------------";
    11 "-------- fun is_poly --------------------------------------------------------";
    12 "-------- fun term_of_poly ---------------------------------------------------";
    13 "-------- integration lev.1 fun factout_p_ -----------------------------------";
    14 "-------- integration lev.1 fun cancel_p_ ------------------------------------";
    15 "-------- integration lev.1 fun common_nominator_p_ --------------------------";
    16 "-------- integration lev.1 fun add_fraction_p_ ------------------------------";
    17 "-------- and app_rev ...traced down from rewrite_set_ until prepats ---------";
    18 "-------- fun rewrite_set_ cancel_p downto fun gcd_poly ----------------------";
    19 "-------- rls norm_Rational downto fun gcd_poly ------------------------------";
    20 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
    21 "-------- rewrite_set_ common_nominator_p from: Mathematik 1 Schalk ----------";
    22 "-------- integration lev.1 -- lev.5: cancel_p_ & common_nominator_p_ --------";
    23 "-------- reverse rewrite ----------------------------------------------------";
    24 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
    25 "-------- investigate rls norm_Rational --------------------------------------";
    26 "-------- examples: rls norm_Rational ----------------------------------------";
    27 "-------- numeral rationals -----------------------------";
    28 "-------- cancellation ----------------------------------";
    29 "-------- common denominator ----------------------------";
    30 "-------- multiply and cancel ---------------------------";
    31 "-------- common denominator and multiplication ---------";
    32 "-------- double fractions ------------------------------";
    33 "-------- crucial examples ------------------------------";
    34 "-------- examples for Stefan Karnels thesis ------------";
    35 "-------- me Schalk I No.186 ----------------------------";
    36 "-------- interSteps ..Simp_Rat_Double_No-1.xml ---------";
    37 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ---------";
    38 "-------- investigate rulesets for cancel_p -------------";
    39 "-------- investigate format of factout_ and factout_p_ -";
    40 "-------- how to stepwise construct Scripts -------------";
    41 "----------- get_denominator ----------------------------";
    42 "--------- several errpats in complicated term -------------------";
    43 "--------------------------------------------------------";
    44 "-------- nonterminating cancel_p, norm_Rational 2002 ---";
    45 "--------------------------------------------------------";
    46 "--------------------------------------------------------";
    47 
    48 
    49 "-------- fun poly_of_term ---------------------------------------------------";
    50 "-------- fun poly_of_term ---------------------------------------------------";
    51 "-------- fun poly_of_term ---------------------------------------------------";
    52 val vs = "12 * x^^^3 * y^^^4 * z^^^6" |> str2term |> vars |> map free2str |> sort string_ord;
    53 
    54 if poly_of_term vs (str2term "12::real") = SOME [(12, [0, 0, 0])]
    55 then () else error "poly_of_term 1 changed";
    56 if poly_of_term vs (str2term "x::real") = SOME [(1, [1, 0, 0])]
    57 then () else error "poly_of_term 2 changed";
    58 if poly_of_term vs (str2term "12 * x^^^3") = SOME [(12, [3, 0, 0])]
    59 then () else error "poly_of_term 3 changed";
    60 if poly_of_term vs (str2term "12 * x^^^3 * y^^^4 * z^^^6") = SOME [(12, [3, 4, 6])]
    61 then () else error "poly_of_term 4 changed";
    62 if poly_of_term vs (str2term "1 + 2 * x^^^3 * y^^^4 * z^^^6 + y") =
    63   SOME [(1, [0, 0, 0]), (1, [0, 1, 0]), (2, [3, 4, 6])]
    64 then () else error "poly_of_term 5 changed";
    65 
    66 (*poly_of_term is quite liberal:*)
    67 (*the coefficient may be somewhere, the order of variables and the parentheses 
    68   within a monomial are arbitrary*)
    69 if poly_of_term vs (str2term "y^^^4 * (x^^^3 * 12 * z^^^6)") = SOME [(12, [3, 4, 6])]
    70 then () else error "poly_of_term 6 changed";
    71 
    72 (*there may even be more than 1 coefficient:*)
    73 if poly_of_term vs (str2term "2 * y^^^4 * (x^^^3 * 6 * z^^^6)") = SOME [(12, [3, 4, 6])]
    74 then () else error "poly_of_term 7 changed";
    75 
    76 (*the order and the parentheses within monomials are arbitrary:*)
    77 if poly_of_term vs (str2term "2 * x^^^3 * y^^^4 * z^^^6 + (7 * y^^^8 + 1)")
    78   = SOME [(1, [0, 0, 0]), (7, [0, 8, 0]), (2, [3, 4, 6])]
    79 then () else error "poly_of_term 8 changed";
    80 
    81 "-------- fun is_poly --------------------------------------------------------";
    82 "-------- fun is_poly --------------------------------------------------------";
    83 "-------- fun is_poly --------------------------------------------------------";
    84 if is_poly (str2term "2 * x^^^3 * y^^^4 * z^^^6 + 7 * y^^^8 + 1")
    85 then () else error "is_poly 1 changed";
    86 if not (is_poly (str2term "2 * (x^^^3 * y^^^4 * z^^^6 + 7) * y^^^8 + 1"))
    87 then () else error "is_poly 2 changed";
    88 
    89 "-------- fun term_of_poly ---------------------------------------------------";
    90 "-------- fun term_of_poly ---------------------------------------------------";
    91 "-------- fun term_of_poly ---------------------------------------------------";
    92 val expT = HOLogic.realT
    93 val Free (_, baseT) = (hd o vars o str2term) "12 * x^^^3 * y^^^4 * z^^^6";
    94 val p = [(1, [0, 0, 0]), (7, [0, 8, 0]), (2, [3, 4, 5])]
    95 val vs = ["x","y","z"]
    96 (*precondition for [(c, es),...]: legth es = length vs*)
    97 ;
    98 if term2str (term_of_poly baseT expT vs p) = "1 + 7 * y ^^^ 8 + 2 * x ^^^ 3 * y ^^^ 4 * z ^^^ 5"
    99 then () else error "term_of_poly 1 changed";
   100 
   101 "-------- integration lev.1 fun factout_p_ -----------------------------------";
   102 "-------- integration lev.1 fun factout_p_ -----------------------------------";
   103 "-------- integration lev.1 fun factout_p_ -----------------------------------";
   104 val t = str2term "(x^^^2 + -1*y^^^2) / (x^^^2 + -1*x*y)"
   105 val SOME (t', asm) = factout_p_ thy t;
   106 if term2str t' = "(x + y) * (x + -1 * y) / (x * (x + -1 * y))"
   107 then () else error ("factout_p_ term 1 changed: " ^ term2str t')
   108 ;
   109 if terms2str asm = "[\"x ~= 0\",\"x + -1 * y ~= 0\"]"
   110 then () else error "factout_p_ asm 1 changed"
   111 ;
   112 val t = str2term "nothing + to_cancel ::real";
   113 if NONE = factout_p_ thy t then () else error "factout_p_ doesn't report non-applicable";
   114 ;
   115 val t = str2term "((3 * x^^^2 + 6 *x + 3) / (2*x + 2))";
   116 val SOME (t', asm) = factout_p_ thy t;
   117 if term2str t' = "(3 + 3 * x) * (1 + x) / (2 * (1 + x))" andalso 
   118   terms2str asm = "[\"1 + x ~= 0\"]"
   119 then () else error "factout_p_ 1 changed";
   120 
   121 "-------- integration lev.1 fun cancel_p_ ------------------------------------";
   122 "-------- integration lev.1 fun cancel_p_ ------------------------------------";
   123 "-------- integration lev.1 fun cancel_p_ ------------------------------------";
   124 val t = str2term "(x^^^2 + -1*y^^^2) / (x^^^2 + -1*x*y)"
   125 val SOME (t', asm) = cancel_p_ thy t;
   126 if (term2str t', terms2str asm) = ("(x + y) / x", "[\"x ~= 0\"]")
   127 then () else error ("cancel_p_ (t', asm) 1 changed: " ^ term2str t')
   128 ;
   129 val t = str2term "nothing + to_cancel ::real";
   130 if NONE = cancel_p_ thy t then () else error "cancel_p_ doesn't report non-applicable";
   131 ;
   132 val t = str2term "((3 * x^^^2 + 6 *x + 3) / (2*x + 2))";
   133 val SOME (t', asm) = cancel_p_ thy t;
   134 if term2str t' = "(3 + 3 * x) / 2" andalso terms2str asm = "[]"
   135 then () else error "cancel_p_ 1 changed";
   136 
   137 "-------- integration lev.1 fun common_nominator_p_ --------------------------";
   138 "-------- integration lev.1 fun common_nominator_p_ --------------------------";
   139 "-------- integration lev.1 fun common_nominator_p_ --------------------------";
   140 val t = str2term ("y / (a*x + b*x + c*x) " ^
   141               (* n1    d1                   *)
   142                 "+ a / (x*y)");
   143               (* n2    d2                   *)
   144 val SOME (t', asm) = common_nominator_p_ thy t;
   145 if term2str t' =
   146       ("y * y / (x * ((a + b + c) * y)) " ^
   147   (*  n1  *d2'/ (c'* ( d1'        *d2')) *)
   148      "+ a * (a + b + c) / (x * ((a + b + c) * y))")
   149    (*  n2 * d1'         / (c'* ( d1'        *d2')) *)
   150 then () else error "common_nominator_p_ term 1 changed";
   151 if terms2str asm = "[\"a + b + c ~= 0\",\"y ~= 0\",\"x ~= 0\"]"
   152 then () else error "common_nominator_p_ asm 1 changed"
   153 
   154 "-------- example in mail Nipkow";
   155 val t = str2term "x/(x^^^2 + -1*y^^^2) + y/(x^^^2 + -1*x*y)";
   156 val SOME (t', asm) = common_nominator_p_ thy t;
   157 if term2str t' = "x * x / " ^ 
   158                  "((x + -1 * y) * ((x + y) * x))" ^
   159             " +\n" ^ 
   160                  "y * (x + y) / " ^ 
   161                  "((x + -1 * y) * ((x + y) * x))"
   162 then () else error "common_nominator_p_ term 2 changed"
   163 ;
   164 if terms2str asm = "[\"x + y ~= 0\",\"x ~= 0\",\"x + -1 * y ~= 0\"]"
   165 then () else error "common_nominator_p_ asm 2 changed"
   166 
   167 "-------- example: applicable tested by SML code";
   168 val t = str2term "nothing / to_add";
   169 if NONE = common_nominator_p_ thy t then () else error "common_nominator_p_ term 3 changed";
   170 ;
   171 val t = str2term "((x + (-1)) / (x + 1)) + ((x + 1) / (x + (-1)))";
   172 val SOME (t', asm) = common_nominator_p_ thy t;
   173 if term2str t' = 
   174    "(x + -1) * (-1 + x) / ((1 + x) * (-1 + x)) +\n(x + 1) * (1 + x) / ((1 + x) * (-1 + x))"
   175   andalso terms2str asm = "[\"1 + x ~= 0\",\"-1 + x ~= 0\"]"
   176 then () else error "common_nominator_p_ 3 changed";
   177 
   178 "-------- integration lev.1 fun add_fraction_p_ ------------------------------";
   179 "-------- integration lev.1 fun add_fraction_p_ ------------------------------";
   180 "-------- integration lev.1 fun add_fraction_p_ ------------------------------";
   181 val t = str2term "((x + (-1)) / (x + 1)) + ((x + 1) / (x + (-1)))";
   182 val SOME (t', asm) = add_fraction_p_ thy t;
   183 if term2str t' = "(2 + 2 * x ^^^ 2) / (-1 + x ^^^ 2)" 
   184 then () else error "add_fraction_p_ 3 changed";
   185 ;
   186 if terms2str asm = "[\"-1 + x ^^^ 2 ~= 0\"]"
   187 then () else error "add_fraction_p_ 3 changed";
   188 ;
   189 val t = str2term "nothing / to_add";
   190 if NONE = add_fraction_p_ thy t then () else error "add_fraction_p_ term 3 changed";
   191 ;
   192 val t = str2term "((x + (-1)) / (x + 1)) + ((x + 1) / (x + (-1)))";
   193 val SOME (t', asm) = add_fraction_p_ thy t;
   194 if term2str t' = "(2 + 2 * x ^^^ 2) / (-1 + x ^^^ 2)" andalso
   195   terms2str asm = "[\"-1 + x ^^^ 2 ~= 0\"]"
   196 then () else error "add_fraction_p_ 3 changed";
   197 
   198 "-------- and app_rev ...traced down from rewrite_set_ until prepats ---------";
   199 "-------- and app_rev ...traced down from rewrite_set_ until prepats ---------";
   200 "-------- and app_rev ...traced down from rewrite_set_ until prepats ---------";
   201 (* trace down until prepats are evaluated 
   202   (which does not to work, because substitution is not done -- compare rew_sub!);
   203   keep this sequence for the case, factout_p, cancel_p, common_nominator_p, add_fraction_p
   204   (again) get prepat = [] changed to <>[]. *)
   205 val t = str2term "(x^^^2 + -1*y^^^2) / (x^^^2 + -1*x*y)";
   206 
   207 (*rewrite_set_ @{theory Isac} true cancel t = NONE; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   208 "~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) = (thy, false, cancel_p, t);
   209 "~~~~~ fun rewrite__set_, args:"; val (thy, i, _, _, (rrls as Rrls _), t) =
   210   (thy, 1, bool, [], rls, term);
   211 (*val (t', asm, rew) = app_rev thy (i+1) rrls t; rew = false!!!!!!!!!!!!!!!!!!!!!*)
   212 "~~~~~ and app_rev, args:"; val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
   213     fun chk_prepat thy erls [] t = true
   214       | chk_prepat thy erls prepat t =
   215         let
   216           fun chk (pres, pat) =
   217             (let 
   218               val subst: Type.tyenv * Envir.tenv =
   219                 Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
   220              in
   221               snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
   222              end) handle _ => false
   223            fun scan_ f [] = false (*scan_ NEVER called by []*)
   224              | scan_ f (pp::pps) =
   225                if f pp then true else scan_ f pps;
   226         in scan_ chk prepat end;
   227     (* apply the normal_form of a rev-set *)
   228     fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   229       if chk_prepat thy erls prepat t
   230       then ((*tracing("### app_rev': t = "^(term2str t));*) normal_form t)
   231       else NONE;
   232 (*  val opt = app_rev' thy rrls t  ..NONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   233 "~~~~~ and app_rev', args:"; val (thy, (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}), t) =
   234   (thy, rrls, t);
   235 (* chk_prepat thy erls prepat t = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   236 (* app_sub thy i rrls t = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   237 "~~~~~ fun chk_prepat, args:"; val (thy, erls, prepat, t) = (thy, erls, prepat, t);
   238           fun chk (pres, pat) =
   239             (let 
   240               val subst: Type.tyenv * Envir.tenv =
   241                 Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
   242              in
   243               snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
   244              end) handle _ => false
   245            fun scan_ f [] = false (*scan_ NEVER called by []*)
   246              | scan_ f (pp::pps) =
   247                if f pp then true else scan_ f pps;
   248 
   249 (*========== inhibit exn WN130823: prepat is empty ====================================
   250 (* scan_ chk prepat = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   251 "~~~~~ fun , args:"; val (f, (pp::pps)) = (chk, prepat);
   252 f;
   253 val ([t1, t2], t) = pp;
   254 term2str t1 = "?r is_expanded";
   255 term2str t2 = "?s is_expanded";
   256 term2str t = "?r / ?s";
   257 (* f pp = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   258 "~~~~~ fun chk, args:"; val (pres, pat) = (pp);
   259               val subst: Type.tyenv * Envir.tenv =
   260                 Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
   261 (*subst = 
   262   ({}, {(("r", 0), ("real", Var (("r", 0), "real"))), 
   263         (("s", 0), ("real", Var (("s", 0), "real")))}*)
   264 ;
   265               snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
   266 "~~~~~ fun eval__true, args:"; val (thy, i, asms, bdv, rls) =
   267   (thy, (i + 1), (map (Envir.subst_term subst) pres), [], erls);
   268 terms2str asms;                         (* = "[\"?r is_expanded\",\"?s is_expanded\"]"*)
   269 asms = [@{term True}] orelse asms = []; (* = false*)
   270 asms = [@{term False}]                ; (* = false*)
   271 "~~~~~ fun chk, args:"; val (indets, (a::asms)) = ([], asms);
   272 bdv (*= []: _a list*);
   273 val bdv : (term * term) list = [];
   274 rewrite__set_ thy (i+1) false;
   275 term2str a = "?r is_expanded"; (*hier m"usste doch der Numerator eingesetzt sein ??????????????*)
   276 val SOME (Const ("HOL.False", _), []) = rewrite__set_ thy (i+1) false bdv rls a
   277 ============ inhibit exn WN130823: prepat is empty ===================================*)
   278 
   279 "-------- fun rewrite_set_ cancel_p downto fun gcd_poly ----------------------";
   280 "-------- fun rewrite_set_ cancel_p downto fun gcd_poly ----------------------";
   281 "-------- fun rewrite_set_ cancel_p downto fun gcd_poly ----------------------";
   282 val t = str2term "(12 * x * y) / (8 * y^^^2 )";
   283 (* "-------- example 187a": exception Div raised...
   284 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;*)
   285 val t = str2term "(8 * x^^^2 * y * z ) / (18 * x * y^^^2 * z )";
   286 (* "-------- example 187b": doesn't terminate...
   287 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;*)
   288 val t = str2term "(9 * x^^^5 * y^^^2 * z^^^4) / (15 * x^^^6 * y^^^3 * z )";
   289 (* "-------- example 187c": doesn't terminate...
   290 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;*)
   291 "~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) = (@{theory Isac}, false, cancel_p, t);
   292 (* WN130827: exception Div raised...
   293 rewrite__set_ thy 1 bool [] rls term
   294 *)
   295 "~~~~~ and rewrite__set_, args:"; val (thy, i, _, _, (rrls as Rrls _), t) =
   296   (thy, 1, bool, [], rls, term);
   297 (* WN130827: exception Div raised...
   298 	val (t', asm, rew) = app_rev thy (i+1) rrls t
   299 *)
   300 "~~~~~ fun app_rev, args:"; val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
   301 (* WN130827: exception Div raised...
   302     val opt = app_rev' thy rrls t
   303 *)
   304 "~~~~~ fun app_rev', args:"; val (thy, (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}), t) =
   305   (thy, rrls, t);
   306 chk_prepat thy erls prepat t    = true;
   307 (* WN130827: exception Div raised...
   308 normal_form t
   309 *)
   310 (* lookup Rational.thy, cancel_p: normal_form = cancel_p_ thy*)
   311 "~~~~~ fun cancel_p_, args:"; val (t) = (t);
   312 val opt = check_fraction t;
   313 val SOME (numerator, denominator) = opt
   314         val vs = t |> vars |> map free2str |> sort string_ord
   315         val baseT = type_of numerator
   316         val expT = HOLogic.realT
   317 val (SOME a, SOME b) = (poly_of_term vs numerator, poly_of_term vs denominator);
   318 (*"-------- example 187a": exception Div raised...
   319 val a = [(12, [1, 1])]: poly
   320 val b = [(8, [0, 2])]: poly
   321               val ((a', b'), c) = gcd_poly a b
   322 *)
   323 (* "-------- example 187b": doesn't terminate...
   324 val a = [(8, [2, 1, 1])]: poly
   325 val b = [(18, [1, 2, 1])]: poly
   326               val ((a', b'), c) = gcd_poly a b
   327 *)
   328 (* "-------- example 187c": doesn't terminate...
   329 val a = [(9, [5, 2, 4])]: poly
   330 val b = [(15, [6, 3, 1])]: poly
   331               val ((a', b'), c) = gcd_poly a b
   332 *)
   333 
   334 "-------- rls norm_Rational downto fun gcd_poly ------------------------------";
   335 "-------- rls norm_Rational downto fun gcd_poly ------------------------------";
   336 "-------- rls norm_Rational downto fun gcd_poly ------------------------------";
   337 val t = str2term "(x^^^2 - 4)*(3 - y) / ((y^^^2 - 9)*(2+x))";
   338 trace_rewrite := false (*true false*);
   339 (* trace stops with ...: (and then jEdit hangs)..
   340 rewrite_set_ thy false norm_Rational t;
   341 :
   342 ###  rls: cancel_p on: (-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /
   343 (-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)
   344 *)
   345 val t = str2term (*copy from above: "::real" is not required due to "^^^"*)
   346   ("(-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /" ^
   347   "(-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)");
   348 (*cancel_p_ thy t;
   349 exception Div raised*)
   350 
   351 "~~~~~ fun cancel_p_, args:"; val (t) = (t);
   352 val opt = check_fraction t;
   353 val SOME (numerator, denominator) = opt
   354         val vs = t |> vars |> map free2str |> sort string_ord
   355         val baseT = type_of numerator
   356         val expT = HOLogic.realT;
   357 print_depth 3; (*999*)
   358 val (SOME a, SOME b) = (poly_of_term vs numerator, poly_of_term vs denominator);
   359 print_depth 3; (*999*)
   360 (* does not terminate instead of returning ?:
   361         val ((a', b'), c) = gcd_poly a b
   362 val a = [(~12, [0, 0]), (3, [2, 0]), (4, [0, 1]), (~1, [2, 1])]: poly
   363 val b = [(~18, [0, 0]), (~9, [1, 0]), (2, [0, 2]), (1, [1, 2])]: poly
   364 *)
   365 
   366 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
   367 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
   368 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
   369 val thy  = @{theory "Rational"};
   370 "-------- WN";
   371 val t = str2term "(2 + -3 * x) / 9";
   372 if NONE = rewrite_set_ thy false cancel_p t then ()
   373 else error "rewrite_set_ cancel_p must return NONE, if the term cannot be cancelled";
   374 
   375 "-------- example 186a";
   376 val t = str2term "(14 * x * y) / (x * y)";
   377   is_expanded (str2term "14 * x * y");
   378   is_expanded (str2term "x * y");
   379 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   380 if (term2str t', terms2str asm) = ("14 / 1", "[]")
   381 then () else error "rational.sml cancel Schalk 186a";
   382 
   383 "-------- example 186b";
   384 val t = str2term "(60 * a * b) / ( 15 * a  * b )";
   385 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   386 if (term2str t', terms2str asm) = ("4 / 1", "[]")
   387 then () else error "rational.sml cancel Schalk 186b";
   388 
   389 "-------- example 186c";
   390 val t = str2term "(144 * a^^^2 * b * c) / (12 * a * b * c)";
   391 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   392 if (term2str t', terms2str asm) = ("12 * a / 1", "[]")
   393 then () else error "rational.sml cancel Schalk 186c";
   394 
   395 (* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! exception Div raised !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   396   see --- fun rewrite_set_ downto fun gcd_poly ---
   397 "-------- example 187a";
   398 val t = str2term "(12 * x * y) / (8 * y^^^2 )";
   399 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   400 if (term2str t', terms2str asm) = ("3 * x / (2 * y)", "[\"4 * y ~= 0\"]")
   401 then () else error "rational.sml cancel Schalk 187a";
   402 *)
   403 
   404 (* doesn't terminate !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   405   see --- fun rewrite_set_ downto fun gcd_poly ---
   406 "-------- example 187b";
   407 val t = str2term "(8 * x^^^2 * y * z ) / (18 * x * y^^^2 * z )";
   408 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   409 if (term2str t', terms2str asm) = ("4 * x / (9 * y)", "[\"2 * (z * (y * x)) ~= 0\"]")
   410 then () else error "rational.sml cancel Schalk 187b";
   411 *)
   412 
   413 (* doesn't terminate !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   414   see --- fun rewrite_set_ downto fun gcd_poly ---
   415 "-------- example 187c";
   416 val t = str2term "(9 * x^^^5 * y^^^2 * z^^^4) / (15 * x^^^6 * y^^^3 * z )";
   417 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   418 if (term2str t', terms2str asm) = 
   419   ("3 * z ^^^ 3 / (5 * (y * x))", "[\"3 * (z * (y ^^^ 2 * x ^^^ 5)) ~= 0\"]") 
   420 then () else error "rational.sml cancel Schalk 187c";
   421 *)
   422 
   423 "-------- example 188a";
   424 val t = str2term "(-8 + 8 * x) / (-9 + 9 * x)";
   425   is_expanded (str2term "8 * x + -8");
   426 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   427 if (term2str t', terms2str asm) = ("8 / 9", "[]")
   428 then () else error "rational.sml cancel Schalk 188a";
   429 
   430 val t = str2term "(8*((-1) + x))/(9*((-1) + x))";
   431 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
   432 if (term2str t', terms2str asm) = ("8 / 9", "[]")
   433 then () else error "rational.sml cancel Schalk make_polynomial 1";
   434 
   435 "-------- example 188b";
   436 val t = str2term "(-15 + 5 * x) / (-18 + 6 * x)";
   437 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   438 if (term2str t', terms2str asm) = ("5 / 6", "[]")
   439 then () else error "rational.sml cancel Schalk 188b";
   440 
   441 "-------- example 188c";
   442 val t = str2term "(a + -1 * b) / (b + -1 * a)";
   443 val SOME (t', asm) = rewrite_set_ thy false  cancel_p t;
   444 if (term2str t', terms2str asm) = ("-1 / 1", "[]")
   445 then () else error "rational.sml cancel Schalk 188c";
   446 
   447 is_expanded (str2term "a + -1 * b") = true;
   448 val t = str2term "((-1)*(b + (-1) * a))/(1*(b + (-1) * a))";
   449 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   450 if (term2str t', terms2str asm) = ("(a + -1 * b) / (-1 * a + b)", "[]")
   451 then () else error "rational.sml cancel Schalk make_polynomial 2";
   452 
   453 "-------- example 190a";
   454 val t = str2term "( 27 * a^^^3 + 9 * a^^^2 + 3 * a + 1 ) / ( 27 * a^^^3 + 18 * a^^^2 + 3 * a )";
   455 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   456 if (term2str t', terms2str asm) = 
   457   ("(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)", "[\"3 * a + 9 * a ^^^ 2 ~= 0\"]")
   458 then () else error "rational.sml cancel Schalk 190a";
   459 
   460 "-------- example 190c";
   461 val t = str2term "((1 + 9 * a ^^^ 2)*(1 + 3 * a))/((3 * a + 9 * a ^^^ 2)*(1 + 3 * a))";
   462 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   463 if (term2str t', terms2str asm) = 
   464   ("(1 + 3 * a + 9 * a ^^^ 2 + 27 * a ^^^ 3) /\n(3 * a + 18 * a ^^^ 2 + 27 * a ^^^ 3)", "[]")
   465 then () else error "rational.sml make_polynomial Schalk 190c";
   466 
   467 "-------- example 191a";
   468 val t = str2term "( x^^^2 + -1 * y^^^2 ) / ( x + y )";
   469   is_expanded (str2term "x^^^2 + -1 * y^^^2") = false; (*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   470   is_expanded (str2term "x + y") = true;
   471 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   472 if (term2str t', terms2str asm) = ("(x + -1 * y) / 1", "[]")
   473 then () else error "rational.sml make_polynomial Schalk 191a";
   474 
   475 "-------- example 191b";
   476 val t = str2term "((x + (-1) * y)*(x + y))/((1)*(x + y))";
   477 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   478 if (term2str t', terms2str asm) = ("(x ^^^ 2 + -1 * y ^^^ 2) / (x + y)", "[]")
   479 then () else error "rational.sml make_polynomial Schalk 191b";
   480 
   481 "-------- example 191c";
   482 val t = str2term "( 9 * x^^^2 + -30 * x + 25 ) / ( 9 * x^^^2 + -25 )";
   483   is_expanded (str2term "9 * x^^^2 + -30 * x + 25") = true;
   484   is_expanded (str2term "25 + -30*x + 9*x^^^2") = true;
   485   is_expanded (str2term "-25 + 9*x^^^2") = true;
   486 
   487 val t = str2term "(((-5) + 3 * x)*((-5) + 3 * x))/((5 + 3 * x)*((-5) + 3 * x))";
   488 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   489 if (term2str t', terms2str asm) = ("(25 + -30 * x + 9 * x ^^^ 2) / (-25 + 9 * x ^^^ 2)", "[]")
   490 then () else error "rational.sml make_polynomial Schalk 191c";
   491 
   492 "-------- example 192b";
   493 val t = str2term "( 7 * x^^^3 + -1 * x^^^2 * y ) / ( 7 * x * y^^^2 + -1 *  y^^^3 )";
   494 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   495 if (term2str t', terms2str asm) = ("x ^^^ 2 / y ^^^ 2", "[\"y ^^^ 2 ~= 0\"]")
   496 then () else error "rational.sml cancel_p Schalk 192b";
   497 
   498 val t = str2term "((x ^^^ 2)*(7 * x + (-1) * y))/((y ^^^ 2)*(7 * x + (-1) * y))";
   499 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   500 if (term2str t', terms2str asm) = 
   501   ("(7 * x ^^^ 3 + -1 * x ^^^ 2 * y) / (7 * x * y ^^^ 2 + -1 * y ^^^ 3)", "[]")
   502 then () else error "rational.sml make_polynomial Schalk 192b";
   503 
   504 val t = str2term "((x ^^^ 2)*(7 * x + (-1) * y))/((y ^^^ 2)*(7 * x + (-1) * y))";
   505 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   506 if (term2str t', terms2str asm) = 
   507   ("(7 * x ^^^ 3 + -1 * x ^^^ 2 * y) / (7 * x * y ^^^ 2 + -1 * y ^^^ 3)", "[]")
   508 then () else error "rational.sml make_polynomial Schalk WN050929 not working";
   509 
   510 "-------- example 193a";
   511 val t = str2term "( x^^^2 + -6 * x + 9 ) / ( x^^^2 + -9 )";
   512 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   513 if (term2str t', terms2str asm) = ("(-3 + x) / (3 + x)", "[\"3 + x ~= 0\"]")
   514 then () else error "rational.sml cancel_p Schalk 193a";
   515 
   516 "-------- example 193b";
   517 val t = str2term "( x^^^2 + -8 * x + 16 ) / ( 2 * x^^^2 + -32 )";
   518 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   519 if (term2str t', terms2str asm) = ("(-4 + x) / (8 + 2 * x)", "[\"8 + 2 * x ~= 0\"]")
   520 then () else error "rational.sml cancel_p Schalk 193b";
   521 
   522 "-------- example 193c";
   523 val t = str2term "( 2 * x + -50 * x^^^3 ) / ( 25 * x^^^2 + -10 * x + 1 )";
   524 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   525 if (term2str t', terms2str asm) = 
   526   ("(2 * x + 10 * x ^^^ 2) / (1 + -5 * x)", "[\"1 + -5 * x ~= 0\"]")
   527 then () else error "rational.sml cancel_p Schalk 193c";
   528 
   529 (*WN:*)
   530 val t = str2term "(-25 + 9*x^^^2)/(5 + 3*x)";
   531 val SOME (t, asm) = rewrite_set_ thy false cancel_p t;
   532 if (term2str t', terms2str asm) = ("(2 * x + 10 * x ^^^ 2) / (1 + -5 * x)", "[]")
   533 then () else error "rational.sml cancel WN 1";
   534 
   535 "-------- example heuberger";
   536 val t = str2term ("(x^^^4 + x * y + x^^^3 * y + y^^^2) / " ^
   537   "(x + 5 * x^^^2 + y + 5 * x * y + x^^^2 * y^^^3 + x * y^^^4)");
   538 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   539 if (term2str t', terms2str asm) = 
   540   ("(x ^^^ 3 + y) / (1 + 5 * x + x * y ^^^ 3)", "[\"1 + 5 * x + x * y ^^^ 3 ~= 0\"]")
   541 then () else error "rational.sml cancel_p heuberger";
   542 
   543 "-------- rewrite_set_ common_nominator_p from: Mathematik 1 Schalk ----------";
   544 "-------- rewrite_set_ common_nominator_p from: Mathematik 1 Schalk ----------";
   545 "-------- rewrite_set_ common_nominator_p from: Mathematik 1 Schalk ----------";
   546 (*deleted example 204 ... 236b at update Isabelle2012-->2013*)
   547 
   548 "-------- integration lev.1 -- lev.5: cancel_p_ & common_nominator_p_ --------";
   549 "-------- integration lev.1 -- lev.5: cancel_p_ & common_nominator_p_ --------";
   550 "-------- integration lev.1 -- lev.5: cancel_p_ & common_nominator_p_ --------";
   551 val t = str2term ("123 = (a*x)/(b*x) + (c*x)/(d*x) + (e*x)/(f*x::real)");
   552 "-------- gcd_poly integration level 1: works on exact term";
   553 if NONE = cancel_p_ thy t then () else error "cancel_p_ works on exact fraction";
   554 if NONE = add_fraction_p_ thy t then () else error "add_fraction_p_ works on exact fraction";
   555 
   556 "-------- gcd_poly integration level 2: picks out ONE appropriate subterm";
   557 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   558 if term2str t' = "123 = a * x / (b * x) + c * x / (d * x) + e / f" 
   559 then () else error "level 2, rewrite_set_ cancel_p: changed";
   560 val SOME (t', asm) = rewrite_set_ thy false add_fractions_p t;
   561 if term2str t' = "123 = (b * c * x + a * d * x) / (b * d * x) + e * x / (f * x)"
   562 then () else error "level 2, rewrite_set_ add_fractions_p: changed";
   563 
   564 "-------- gcd_poly integration level 3: rewrites all appropriate subterms";
   565 val SOME (t', asm) = rewrite_set_ thy false cancel_p_rls t;
   566 if term2str t' = "123 = a / b + c / d + e / f"
   567 then () else error "level 3, rewrite_set_ cancel_p_rls: changed";
   568 val SOME (t', asm) = rewrite_set_ thy false common_nominator_p_rls t; (*CREATE add_fractions_p_rls*)
   569 if term2str t' = "123 = (b * d * e * x + b * c * f * x + a * d * f * x) / (b * d * f * x)"
   570 then () else error "level 3, rewrite_set_ add_fractions_p_rls: changed";
   571 
   572 "-------- gcd_poly integration level 4: iteration cancel_p -- add_fraction_p";
   573 (* simpler variant *)
   574 val testrls = append_rls "testrls" e_rls [Rls_ cancel_p, Rls_ add_fractions_p]
   575 val SOME (t', asm) = rewrite_set_ thy false testrls t;
   576 (*trace_rewrite := false;
   577 #  rls: testrls on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   578 ##  rls: cancel_p on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   579 ##  rls: common_nominator_p on: 123 = a * x / (b * x) + c * x / (d * x) + e / f 
   580 ##  rls: cancel_p on: 123 = (b * c * x + a * d * x) / (b * d * x) + e / f 
   581 ##  rls: common_nominator_p on: 123 = (b * c + a * d) / (b * d) + e / f 
   582 ##  rls: cancel_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   583 ##  rls: common_nominator_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) *)
   584 if term2str t' = "123 = (b * d * e + b * c * f + a * d * f) / (b * d * f)"
   585 then () else error "level 4, rewrite_set_ *_p: changed";
   586 
   587 (* complicated variant *)
   588 val testrls_rls = append_rls "testrls_rls" e_rls [Rls_ cancel_p_rls, Rls_ common_nominator_p_rls];
   589 val SOME (t', asm) = rewrite_set_ thy false testrls_rls t;
   590 (*#  rls: testrls_rls on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   591 ##  rls: cancel_p_rls on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   592 ###  rls: cancel_p on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   593 ###  rls: cancel_p on: 123 = a * x / (b * x) + c * x / (d * x) + e / f 
   594 ###  rls: cancel_p on: 123 = a * x / (b * x) + c / d + e / f 
   595 ###  rls: cancel_p on: 123 = a / b + c / d + e / f 
   596 ##  rls: common_nominator_p_rls on: 123 = a / b + c / d + e / f 
   597 ###  rls: common_nominator_p on: 123 = a / b + c / d + e / f 
   598 ###  rls: common_nominator_p on: 123 = (b * c + a * d) / (b * d) + e / f 
   599 ###  rls: common_nominator_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   600 ##  rls: cancel_p_rls on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   601 ###  rls: cancel_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   602 ##  rls: common_nominator_p_rls on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   603 ###  rls: common_nominator_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) *)
   604 if term2str t' = "123 = (b * d * e + b * c * f + a * d * f) / (b * d * f)"
   605 then () else error "level 4, rewrite_set_ *_p_rls: changed"
   606 
   607 "-------- gcd_poly integration level 5: cancel_p & add_fraction_p within norm_Rational";
   608 val SOME (t', asm) = rewrite_set_ thy false norm_Rational t;
   609 if term2str t' = "123 = (a * d * f + b * c * f + b * d * e) / (b * d * f)"
   610 then () else error "level 5, rewrite_set_ norm_Rational: changed"
   611 
   612 "-------- reverse rewrite ----------------------------------------------------";
   613 "-------- reverse rewrite ----------------------------------------------------";
   614 "-------- reverse rewrite ----------------------------------------------------";
   615 (** the term for which reverse rewriting is demonstrated **)
   616 val t = str2term "(9 + -1 * x ^^^ 2) / (9 + 6 * x + x ^^^ 2)";
   617 val Rrls {scr = Rfuns {init_state = ini, locate_rule = loc,
   618   next_rule = nex, normal_form = nor, ...},...} = cancel_p;
   619 
   620 (** normal_form produces the result in ONE step **)
   621   val SOME (t',_) = nor t;
   622 if term2str t' = "(3 + -1 * x) / (3 + x)" then ()
   623 else error "rational.sml normal_form (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   624 
   625 (** initialize the interpreter state used by the 'me' **)
   626   val (t, _, revsets, _) = ini t;
   627 
   628 if length (hd revsets) = 11 then () else error "length of revset changed";
   629 if (revsets |> nth 1 |> nth 1 |> id_of_thm) = 
   630   (@{thm realpow_twoI} |> string_of_thm |> thmID_of_derivation_name)
   631 then () else error "first element of revset changed";
   632 if
   633 (revsets |> nth 1 |> nth 1 |> rule2str) = "Thm (\"realpow_twoI\",??.unknown)" andalso
   634 (revsets |> nth 1 |> nth 2 |> rule2str) = 
   635   "Thm (\"sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))\",??.unknown)" andalso
   636 (revsets |> nth 1 |> nth 3 |> rule2str) = 
   637   "Thm (\"sym_#mult_Float ((2,0), (0,0)) __ ((3,0), (0,0))\",??.unknown)" andalso
   638 (revsets |> nth 1 |> nth 4 |> rule2str) = 
   639   "Thm (\"sym_#mult_Float ((~1,0), (0,0)) __ ((3,0), (0,0))\",??.unknown)" andalso
   640 (revsets |> nth 1 |> nth 5 |> rule2str) = 
   641   "Thm (\"sym_#mult_Float ((3,0), (0,0)) __ ((3,0), (0,0))\",??.unknown)" andalso
   642 (revsets |> nth 1 |> nth 6 |> rule2str) = "Rls_ (\"sym_order_mult_rls_\")" andalso
   643 (revsets |> nth 1 |> nth 7 |> rule2str) = 
   644   "Thm (\"sym_mult_assoc\",Groups.semigroup_mult_class.mult_assoc)"
   645 then () else error "first 7 elements in revset changed"
   646 
   647 (** find the rule 'r' to apply to term 't' **)
   648 (*/------- since cancel_ (accepted "-" between monomials) has been replaced by cancel_p_ 
   649   for Isabelle2013, we don't get a working revset, but non-termination:
   650 
   651   val SOME (r as (Thm (str, thm))) = nex revsets t;
   652   :
   653 ((3 * 3 + -1 * x * x) / (3 * 3 + 2 * 3 * x + x * x), 
   654   Rls_ ("sym_order_mult_rls_"), ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * (3 * x) + x * x), []))","
   655 ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * (3 * x) + x * x), 
   656   Thm ("sym_mult_assoc",""), ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * 3 * x + x * x), []))","
   657 ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * 3 * x + x * x), 
   658   Thm ("sym_mult_assoc",""), ((3 * 3 + -1 * x * x) / (3 * 3 + 2 * 3 * x + x * x), []))","
   659 ((3 * 3 + -1 * x * x) / (3 * 3 + 2 * 3 * x + x * x), Rls_ ("sym_order_mult_rls_"), ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * (3 * x) + x * x), []))","
   660  :
   661 ### Isabelle2002:
   662   Thm ("sym_#mult_2_3", "6 = 2 * 3")
   663 ### Isabelle2009-2 for cancel_ (not cancel_p_):
   664 if str = "sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))"
   665    andalso string_of_thm thm = 
   666            (string_of_thm o make_thm o (cterm_of (@{theory "Isac"})))
   667                (Trueprop $ (term_of o the o (parse thy)) "9 = 3 ^^^ 2") then ()
   668 else error "rational.sml next_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   669 \---------------------------------------------------------------------------------------/*)
   670 
   671 (** check, if the rule 'r' applied by the user to 't' belongs to the ruleset;
   672   if the rule is OK, the term resulting from applying the rule is returned,too;
   673   there might be several rule applications inbetween,
   674   which are listed after the head in reverse order **)
   675 (*/-------------------------------------------- Isabelle2013: this gives "error id_of_thm";
   676   we don't repair this, because interaction within "reverse rewriting" never worked properly:
   677 
   678   val (r, (t, asm))::_ = loc revsets t r;
   679 if term2str t = "(9 - x ^^^ 2) / (3 ^^^ 2 + 6 * x + x ^^^ 2)" andalso asm = []
   680 then () else error "rational.sml locate_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   681 
   682 (* find the next rule to apply *)
   683   val SOME (r as (Thm (str, thm))) = nex revsets t;
   684 if str = "sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))" andalso
   685    string_of_thm thm = (string_of_thm o make_thm o (cterm_of (@{theory "Isac"})))
   686                 (Trueprop $ (term_of o the o (parse thy)) "9 = 3 ^^^ 2") then ()
   687 else error "rational.sml next_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   688 
   689 (*check the next rule*)
   690   val (r, (t, asm)) :: _ = loc revsets t r;
   691 if term2str t = "(3 ^^^ 2 - x ^^^ 2) / (3 ^^^ 2 + 6 * x + x ^^^ 2)" then ()
   692 else error "rational.sml locate_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2) II";
   693 
   694 (*find and check the next rules, rewrite*)
   695   val SOME r = nex revsets t;
   696   val (r,(t,asm))::_ = loc revsets t r;
   697 if term2str t = "(3 ^^^ 2 - x ^^^ 2) / (3 ^^^ 2 + 2 * 3 * x + x ^^^ 2)" then ()
   698 else error "rational.sml locate_rule II";
   699 
   700   val SOME r = nex revsets t;
   701   val (r,(t,asm))::_ = loc revsets t r;
   702 if term2str t = "(3 - x) * (3 + x) / (3 ^^^ 2 + 2 * 3 * x + x ^^^ 2)" then ()
   703 else error "rational.sml next_rule II";
   704 
   705   val SOME r = nex revsets t;
   706   val (r,(t,asm))::_ = loc revsets t r;
   707 if term2str t = "(3 - x) * (3 + x) / ((3 + x) * (3 + x))" then ()
   708 else error "rational.sml next_rule III";
   709 
   710   val SOME r = nex revsets t;
   711   val (r, (t, asm)) :: _ = loc revsets t r;
   712   val ss = term2str t;
   713 if ss = "(3 - x) / (3 + x)" andalso terms2str asm = "[\"3 + x ~= 0\"]" then ()
   714 else error "rational.sml: new behav. in rev-set cancel";
   715 \--------------------------------------------------------------------------------------/*)
   716 
   717 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   718 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   719 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   720 (*Isabelle2013: the example below shows, why "reverse rewriting" only worked for
   721   special cases.*)
   722 
   723 (*the term for which reverse rewriting is demonstrated*)
   724 val t = str2term "(9 + (-1)*x^^^2) / (9 + ((-6)*x + x^^^2))";
   725 val Rrls {scr=Rfuns {init_state=ini,locate_rule=loc,
   726 		       next_rule=nex,normal_form=nor,...},...} = cancel_p;
   727 
   728 (*normal_form produces the result in ONE step*)
   729 val SOME (t',_) = nor t; 
   730 term2str t' = "(3 + 1 * x) / (3 + -1 * x)";
   731 
   732 (*initialize the interpreter state used by the 'me'*)
   733 val SOME (t', asm) = cancel_p_ thy t;
   734 term2str t' = "(3 + x) / (3 + -1 * x)" (*true*);
   735 terms2str asm = "[\"3 + -1 * x ~= 0\"]" (*true*);
   736 val (t,_,revsets,_) = ini t;
   737 
   738 (* WN.10.10.02: dieser Fall terminiert nicht 
   739            (make_polynomial enth"alt zu viele rules)
   740 WN060823 'init_state' requires rewriting on specified location in the term
   741 print_depth 99; Rfuns; print_depth 3;
   742 WN060831 cycling "sym_order_mult_rls_" "sym_mult_assoc"
   743          as was with make_polynomial before ?!?*)
   744 
   745 val SOME r = nex revsets t;
   746 eq_Thm (r, Thm ("sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))", 
   747 		mk_thm thy "9 = 3 ^^^ 2"));
   748 (*WN060831 *** id_of_thm
   749            Exception- ERROR raised ...
   750 val (r,(t,asm))::_ = loc revsets t r;
   751 term2str t;
   752 
   753   val SOME r = nex revsets t;
   754   val (r,(t,asm))::_ = loc revsets t r;
   755   term2str t;
   756 *)                    
   757 
   758 (*========== inhibit exn WN130824 TODO =======================================================
   759 "-------- examples: rls norm_Rational ----------------------------------------";
   760 "-------- examples: rls norm_Rational ----------------------------------------";
   761 "-------- examples: rls norm_Rational ----------------------------------------";
   762 val t = str2term "(3*x+5)/18 - x/2  - -(3*x - 2)/9 = 0";
   763 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   764 if term2str t' = "1 / 18 = 0" then () else error "rational.sml 1";
   765 
   766 val t = str2term "(17*x - 51)/9 - (-(13*x - 3)/6) + 11 - (9*x - 7)/4 = 0";
   767 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   768 if term2str t' = "(237 + 65 * x) / 36 = 0" then () 
   769 else error "rational.sml 2";
   770 
   771 val t = str2term "(1/2 + (5*x)/2)^^^2 - ((13*x)/2 - 5/2)^^^2 - (6*x)^^^2 + 29";
   772 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   773 if term2str t' = "23 + 35 * x + -72 * x ^^^ 2" then ()
   774 else error "rational.sml 3";
   775 
   776 (*trace_rewrite:=true;*)
   777 val t = str2term "Not (6*x is_atom)";
   778 val SOME (t',_) = rewrite_set_ thy false powers_erls t; term2str t';
   779 "HOL.True";
   780 val t = str2term "1 < 2";
   781 val SOME (t',_) = rewrite_set_ thy false powers_erls t; term2str t';
   782 "HOL.True";
   783 
   784 val t = str2term "(6*x)^^^2";
   785 val SOME (t',_) = rewrite_ thy dummy_ord powers_erls false 
   786 			   (num_str @{thm realpow_def_atom}) t;
   787 if term2str t' = "6 * x * (6 * x) ^^^ (2 + -1)" then ()
   788 else error "rational.sml powers_erls (6*x)^^^2";
   789 
   790 val t = str2term "-1 * (-2 * (5 / 2 * (13 * x / 2)))";
   791 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   792 if term2str t' = "65 * x / 2" then () else error "rational.sml 4";
   793 
   794 val t = str2term "1 - ((13*x)/2 - 5/2)^^^2";
   795 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   796 (*bef.040209: if term2str t' = "(-21 + (130 * x + -169 * x ^^^ 2)) / 4"then()*)
   797 if term2str t' = "(-21 + 130 * x + -169 * x ^^^ 2) / 4" then () 
   798 else error "rational.sml 5";
   799 
   800 (*SRAM Schalk I, p.92 Nr. 609a*)
   801 val t = str2term "2*(3 - x/5)/3 - 4*(1 - x/3) - x/3 - 2*(x/2 - 1/4)/27 +5/54";
   802 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   803 if term2str t' = "(-255 + 112 * x) / 135" then () 
   804 else error "rational.sml 6";
   805 
   806 (*SRAM Schalk I, p.92 Nr. 610c*)
   807 val t = str2term "((x- 1)/(x+1) + 1) / ((x- 1)/(x+1) - (x+1)/(x- 1)) - 2";
   808 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   809 if term2str t' = "(-3 + -1 * x) / 2" then () else error "rational.sml 7";
   810 
   811 (*SRAM Schalk I, p.92 Nr. 476a*)
   812 val t = str2term "(x^^^2/(1 - x^^^2) + 1)/(x/(1 - x) + 1) * (1 + x)";
   813 (*. a/b : c/d translated to a/b * d/c .*)
   814 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   815 (*if term2str t' = "1 / 1" then () else error "rational.sml 8";3.6.03*)
   816 if term2str t' = "1" then () else error "rational.sml 8";
   817 
   818 (*............................vvv---TODO: sollte gehen mit poly_order *)
   819 (*Schalk I, p.92 Nr. 472a*)
   820 val t = str2term "((8*x^^^2 - 32*y^^^2)/(2*x + 4*y))/((4*x - 8*y)/(x + y))";
   821 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   822 if term2str t' = "x + y" then () else error "rational.sml p.92 Nr. 472a";
   823 
   824 (*Schalk I, p.70 Nr. 480b; a/b : c/d translated to a/b * d/c*)
   825 val t = str2term ("((12*x*y/(9*x^^^2 - y^^^2))/" ^
   826 		 "(1/(3*x - y)^^^2 - 1/(3*x + y)^^^2)) *" ^
   827 		 "(1/(x - 5*y)^^^2 - 1/(x + 5*y)^^^2)/" ^
   828 		 "(20*x*y/(x^^^2 - 25*y^^^2))");
   829 (*... nicht simpl, zerlegt ...*)
   830 val t = str2term ("((12*x*y/(9*x^^^2 - y^^^2))/" ^
   831 		 "(1/(3*x - y)^^^2 - 1/(3*x + y)^^^2))");
   832 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   833 "(-12 * (x * y ^^^ 3) + 108 * (x * (y * x ^^^ 2))) / (12 * (x * y))";
   834 (*                             ~~~~~~~~~~ poly_order notwendig!*)
   835 val t = str2term ("(1/(x - 5*y)^^^2 - 1/(x + 5*y)^^^2)/" ^
   836 		 "(20*x*y/(x^^^2 - 25*y^^^2))");
   837 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   838 if term2str t' = "1 / (x ^^^ 2 + -25 * y ^^^ 2)" then ()
   839 else error "rational.sml norm_Rational 1 / (x ^^^ 2 + -25 * y ^^^ 2)";
   840 
   841 "nonterm.SK6 ----- SK060904-2a non-termination of add_fraction_p_";
   842 (*WN.2.6.03 from rlang.sml 56a 
   843 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x) = 4 * (a * b) / (a ^^^ 2 + -1 * b ^^^ 2)";
   844 val NONE = rewrite_set_ thy false common_nominator_p t;
   845 
   846 WN060831 nonterm.SK7 
   847 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x)";
   848 val NONE = add_fraction_p_ thy t; 
   849 *)
   850 
   851 
   852 (* ------------------------------------------------------------------- *)
   853 (*---------vvv------------ MG: ab 1.7.03 ----------------vvv-----------*)
   854 (*                 Simplifier fuer beliebige Buchterme                 *) 
   855 (* ------------------------------------------------------------------- *)
   856 (*----------------------- norm_Rational_mg ----------------------------*)
   857 (* ------------------------------------------------------------------- *)
   858 
   859 "-------- numeral rationals -----------------------------";
   860 "-------- numeral rationals -----------------------------";
   861 "-------- numeral rationals -----------------------------";
   862 (*SRA Schalk I, p.40 Nr. 164b *)
   863 val t = str2term "(47/6 - 76/9 + 13/4)/(35/12)";
   864 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   865 term2str t;
   866 if (term2str t) = "19 / 21" then ()
   867 else error "rational.sml: diff.behav. in norm_Rational_mg 1";
   868 
   869 (*SRA Schalk I, p.40 Nr. 166a *)
   870 val t = str2term "((5/4)/(4+22/7) + 37/20)*(110/3 - 110/9 * 23/11)";
   871 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   872 term2str t;
   873 if (term2str t) = "45 / 2" then ()
   874 else error "rational.sml: diff.behav. in norm_Rational_mg 2";
   875 
   876 
   877 "-------- cancellation ----------------------------------";
   878 "-------- cancellation ----------------------------------";
   879 "-------- cancellation ----------------------------------";
   880 (* e190c Stefan K.*)
   881 val t = str2term
   882 "((1 + 9 * a ^^^ 2)*(1 + 3 * a))/((3 * a + 9 * a ^^^ 2)*(1 + 3 * a))";
   883 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   884 term2str t;
   885 if (term2str t) = 
   886 "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
   887 then ()
   888 else error "rational.sml: diff.behav. in norm_Rational_mg 3";
   889 
   890 (* e192b Stefan K.*)
   891 val t = str2term
   892 "((x ^^^ 2)*(7 * x + (-1) * y))/((y ^^^ 2)*(7 * x + (-1) * y))";
   893 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   894 term2str t;
   895 if (term2str t) = 
   896 "x ^^^ 2 / y ^^^ 2"
   897 then ()
   898 else error "rational.sml: diff.behav. in norm_Rational_mg 4";
   899 
   900 (*SRC Schalk I, p.66 Nr. 379c *)
   901 val t = str2term 
   902 "(a - b)/(b - a)";
   903 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   904 term2str t;
   905 if (term2str t) =
   906 "-1"
   907 then ()
   908 else error "rational.sml: diff.behav. in norm_Rational_mg 5";
   909 
   910 (*SRC Schalk I, p.66 Nr. 380b *)
   911 val t = str2term 
   912 "15*(3*x+3)*(4*x+9)/(12*(2*x+7)*(5*x+5))";
   913 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   914 term2str t;
   915 if (term2str t) =
   916 "(27 + 12 * x) / (28 + 8 * x)"
   917 then ()
   918 else error "rational.sml: diff.behav. in norm_Rational_mg 6";
   919 
   920 (*Schalk I, p.60 Nr. 215c *)
   921 (* Falsches Ergebnis: rechnet lange und cancel_p kann nicht weiter krzen!!!*)
   922 (* WN060831????MG1 
   923 val t = str2term "(a+b)^^^4*(x - y)/((x - y)^^^3*(a+b)^^^2)";
   924 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   925 term2str t;
   926 if (term2str t) =
   927 "(a ^^^ 4 * x + -1 * a ^^^ 4 * y + 4 * a ^^^ 3 * b * x + -4 * a ^^^ 3 * b * y + 6 * a ^^^ 2 * b ^^^ 2 * x + -6 * a ^^^ 2 * b ^^^ 2 * y + 4 * a * b ^^^ 3 * x + -4 * a * b ^^^ 3 * y + b ^^^ 4 * x + -1 * b ^^^ 4 * y) /(a ^^^ 2 * x ^^^ 3 + -3 * a ^^^ 2 * x ^^^ 2 * y + 3 * a ^^^ 2 * x * y ^^^ 2 + -1 * a ^^^ 2 * y ^^^ 3 + 2 * a * b * x ^^^ 3 + -6 * a * b * x ^^^ 2 * y + 6 * a * b * x * y ^^^ 2 + -2 * a * b * y ^^^ 3 + b ^^^ 2 * x ^^^ 3 + -3 * b ^^^ 2 * x ^^^ 2 * y + 3 * b ^^^ 2 * x * y ^^^ 2 + -1 * b ^^^ 2 * y ^^^ 3)"
   928 then ()
   929 else error "rational.sml: diff.behav. in norm_Rational_mg 7";
   930 *)
   931 (*val t = str2term 
   932 "(a ^^^ 4 * x + -1 * a ^^^ 4 * y + 4 * a ^^^ 3 * b * x + -4 * a ^^^ 3 * b * y + 6 * a ^^^ 2 * b ^^^ 2 * x + -6 * a ^^^ 2 * b ^^^ 2 * y + 4 * a * b ^^^ 3 * x + -4 * a * b ^^^ 3 * y + b ^^^ 4 * x + -1 * b ^^^ 4 * y) /(a ^^^ 2 * x ^^^ 3 + -3 * a ^^^ 2 * x ^^^ 2 * y + 3 * a ^^^ 2 * x * y ^^^ 2 + -1 * a ^^^ 2 * y ^^^ 3 + 2 * a * b * x ^^^ 3 + -6 * a * b * x ^^^ 2 * y + 6 * a * b * x * y ^^^ 2 + -2 * a * b * y ^^^ 3 + b ^^^ 2 * x ^^^ 3 + -3 * b ^^^ 2 * x ^^^ 2 * y + 3 * b ^^^ 2 * x * y ^^^ 2 + -1 * b ^^^ 2 * y ^^^ 3)"
   933 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
   934 term2str t;*)
   935 (* uncaught exception nonexhaustive binding failure
   936    raised at: stdIn:93.1-93.51 *)
   937 
   938 (*Schalk I, p.66 Nr. 381a *)
   939 (* ACHTUNG: rechnet ca. 2 Minuten !!! *)
   940 (* WN060831???MG2
   941 val t = str2term "18*(a+b)^^^3*(a - b)^^^2/(72*(a - b)^^^3*(a+b)^^^2)";
   942 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   943 term2str t;
   944 if (term2str t) =
   945 "(a + b) / (4 * a + -4 * b)"
   946 then () else error "rational.sml: diff.behav. in norm_Rational_mg 8";
   947 *)
   948 
   949 (*SRC Schalk I, p.66 Nr. 381b *)
   950 val t = str2term 
   951 "(4*x^^^2 - 20*x + 25)/(2*x - 5)^^^3";
   952 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   953 term2str t;
   954 if (term2str t) =
   955 "-1 / (5 + -2 * x)"
   956 then ()
   957 else error "rational.sml: diff.behav. in norm_Rational_mg 9";
   958 
   959 (*SRC Schalk I, p.66 Nr. 381c *)
   960 val t = str2term 
   961 "(27*a^^^3+9*a^^^2+3*a+1)/(27*a^^^3+18*a^^^2+3*a)";
   962 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   963 term2str t;
   964 if (term2str t) =
   965 "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
   966 then ()
   967 else error "rational.sml: diff.behav. in norm_Rational_mg 10";
   968 
   969 (*SRC Schalk I, p.66 Nr. 383a *)
   970 val t = str2term 
   971 "(5*a^^^2 - 5*a*b)/(a - b)^^^2";
   972 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   973 term2str t;
   974 if (term2str t) =
   975 "5 * a / (a + -1 * b)"
   976 then ()
   977 else error "rational.sml: diff.behav. in norm_Rational_mg 11";
   978 
   979 
   980 "-------- common denominator ----------------------------";
   981 "-------- common denominator ----------------------------";
   982 "-------- common denominator ----------------------------";
   983 (*SRA Schalk I, p.67 Nr. 403a *)
   984 val t = str2term 
   985 "4/x - 3/y - 1";
   986 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   987 term2str t;
   988 if (term2str t) =
   989 "(-3 * x + 4 * y + -1 * x * y) / (x * y)"
   990 then ()
   991 else error "rational.sml: diff.behav. in norm_Rational_mg 12";
   992 
   993 (*SRA Schalk I, p.67 Nr. 407b *)
   994 val t = str2term 
   995 "(2*a+3*b)/(b*c) + (3*c+a)/(a*c) - (2*a^^^2+3*b*c)/(a*b*c)";
   996 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
   997 term2str t;
   998 if (term2str t) =
   999 "4 / c"
  1000 then ()
  1001 else error "rational.sml: diff.behav. in norm_Rational_mg 13";
  1002 
  1003 (*SRA Schalk I, p.67 Nr. 410b *)
  1004 val t = str2term 
  1005 "1/(x+1) + 1/(x+2) - 2/(x+3)";
  1006 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1007 term2str t;
  1008 if (term2str t) =
  1009 "(5 + 3 * x) / (6 + 11 * x + 6 * x ^^^ 2 + x ^^^ 3)"
  1010 then ()
  1011 else error "rational.sml: diff.behav. in norm_Rational_mg 14";
  1012 
  1013 (*SRA Schalk I, p.67 Nr. 413b *)
  1014 val t = str2term 
  1015 "(1+x)/(1 - x) - (1 - x)/(1+x) + 2*x/(1 - x^^^2)";
  1016 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1017 term2str t;
  1018 if (term2str t) =
  1019 "6 * x / (1 + -1 * x ^^^ 2)"
  1020 then ()
  1021 else error "rational.sml: diff.behav. in norm_Rational_mg 15";
  1022 
  1023 (*SRA Schalk I, p.68 Nr. 414a *)
  1024 val t = str2term 
  1025 "(x + 2)/(x - 1) + (x - 3)/(x - 2) - (x + 1)/((x - 1)*(x - 2))";
  1026 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1027 term2str t;
  1028 if (term2str t) =
  1029 "(-2 + -5 * x + 2 * x ^^^ 2) / (2 + -3 * x + x ^^^ 2)"
  1030 then ()
  1031 else error "rational.sml: diff.behav. in norm_Rational_mg 16";
  1032 
  1033 (*SRA Schalk I, p.68 Nr. 423a *)
  1034 val t = str2term 
  1035 "(2*x+3*y)/x + (4*x^^^3 - x*y^^^2 - 3*y^^^3)/(x^^^3 - 2*x^^^2*y+x*y^^^2) - (5*x+6*y)/(x - y)";
  1036 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1037 term2str t;
  1038 if (term2str t) =
  1039 "1"
  1040 then ()
  1041 else error "rational.sml: diff.behav. in norm_Rational_mg 17";
  1042 
  1043 (*SRA Schalk I, p.68 Nr. 428b *)
  1044 val t = str2term 
  1045 "1/(a - b)^^^2 + 1/(a+b)^^^2 - 2/(a^^^2 - b^^^2) - 4*(b^^^2 - 1)/(a^^^2 - b^^^2)^^^2";
  1046 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1047 term2str t;
  1048 if (term2str t) =
  1049 "4 / (a ^^^ 4 + -2 * a ^^^ 2 * b ^^^ 2 + b ^^^ 4)"
  1050 then ()
  1051 else error "rational.sml: diff.behav. in norm_Rational_mg 18";
  1052 
  1053 (*SRA Schalk I, p.68 Nr. 430b *)
  1054 val t = str2term 
  1055 "a^^^2/(a - 3*b) - 108*a*b^^^3/((a+3*b)*(a^^^2 - 9*b^^^2)) - 9*b^^^2*(a - 3*b)/(a+3*b)^^^2";
  1056 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1057 term2str t;
  1058 if (term2str t) =
  1059 "a + 3 * b"
  1060 then ()
  1061 else error "rational.sml: diff.behav. in norm_Rational_mg 19";
  1062 
  1063 
  1064 (*SRA Schalk I, p.68 Nr. 432 *)
  1065 val t = str2term 
  1066 "(a^^^2+a*b)/(a^^^2 - b^^^2) - (b^^^2 - a*b)/(b^^^2 - a^^^2) + a^^^2*(a - b)/(a^^^3 - a^^^2*b) - 2*a*(a^^^2 - b^^^2)/(a^^^3 - a*b^^^2) - 2*b^^^2/(a^^^2 - b^^^2)";
  1067 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1068 term2str t;
  1069 if (term2str t) =
  1070 "0"
  1071 then ()
  1072 else error "rational.sml: diff.behav. in norm_Rational_mg 20";
  1073 
  1074 (*Eigenes*)
  1075 val t = str2term 
  1076 "3*a/(a*b) + x/y";
  1077 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1078 term2str t;
  1079 if (term2str t) =
  1080 "(3 * y + b * x) / (b * y)"
  1081 then ()
  1082 else error "rational.sml: diff.behav. in norm_Rational_mg 21";
  1083 
  1084 
  1085 "-------- multiply and cancel ---------------------------";
  1086 "-------- multiply and cancel ---------------------------";
  1087 "-------- multiply and cancel ---------------------------";
  1088 (*SRM Schalk I, p.68 Nr. 436a *)
  1089 val t = str2term 
  1090 "3*(x+y)/(15*(x - y)) * 25*(x - y)^^^2/(18*(x+y)^^^2)";
  1091 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1092 term2str t;
  1093 if (term2str t) =
  1094 "(5 * x + -5 * y) / (18 * x + 18 * y)"
  1095 then ()
  1096 else error "rational.sml: diff.behav. in norm_Rational_mg 22";
  1097 
  1098 (*SRM.test Schalk I, p.68 Nr. 436b *)
  1099 (*WN060420???MG3 crashes with method 'simplify' in 
  1100   IsacCore > Simplification > Rational Terms > Multiplication > No.2*)
  1101 val t = str2term "5*a*(a - b)^^^2*(a + b)^^^3/(7*b*(a - b)^^^3) * 7*b/(a + b)^^^3";
  1102 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1103 term2str t;
  1104 if (term2str t) =
  1105 "5 * a / (a + -1 * b)"
  1106 then ()
  1107 else error "rational.sml: diff.behav. in norm_Rational_mg 23";
  1108 
  1109 (*Schalk I, p.68 Nr. 437a *)
  1110 val t = str2term "(3*a - 4*b)/(4*c+3*e) * (3*a+4*b)/(9*a^^^2 - 16*b^^^2)";
  1111 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1112 if (term2str t) = "1 / (4 * c + 3 * e)" then ()
  1113 else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1114 
  1115 "----- S.K. corrected non-termination 060904";
  1116 val t = str2term "(3*a - 4*b) * (3*a+4*b)/((4*c+3*e)*(9*a^^^2 - 16*b^^^2))";
  1117 val SOME (t',_) = rewrite_set_ thy false make_polynomial t;
  1118 term2str t';
  1119 if term2str t' = 
  1120   "(9 * a ^^^ 2 + -16 * b ^^^ 2) /\n(36 * a ^^^ 2 * c + 27 * a ^^^ 2 * e + -64 * b ^^^ 2 * c +\n -48 * b ^^^ 2 * e)"
  1121 (*"(9 * a ^^^ 2 + -16 * b ^^^ 2) / (36 * a ^^^ 2 * c + 27 * a ^^^ 2 * e + -64 * b ^^^ 2 * c + -48 * b ^^^ 2 * e)"*)
  1122 then () else error "rational.sml: S.K.8..corrected 060904-6";
  1123 
  1124 "----- S.K. corrected non-termination of cancel_p_";
  1125 val t'' = str2term ("(9 * a ^^^ 2 + -16 * b ^^^ 2) /" ^
  1126 "(36 * a^^^2 * c + (27 * a^^^2 * e + (-64 * b^^^2 * c + -48 * b^^^2 * e)))");
  1127 val SOME (t',_) = rewrite_set_ thy false cancel_p t'';
  1128 if term2str t' = "1 / (4 * c + 3 * e)" then ()
  1129 else error "rational.sml: diff.behav. in cancel_p S.K.8";
  1130 
  1131 (*Schalk I, p.68 Nr. 437b *)
  1132 (* nonterm.SK9 loops: cancel_p kann nicht weiter kuerzen!!! *)
  1133 val t'' = str2term "(a + b)/(x^^^2 - y^^^2) * ((x - y)^^^2/(a^^^2 - b^^^2))";
  1134 (* val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t'';
  1135    *)
  1136 
  1137 "================delete===";
  1138 (*a casual output from above*)
  1139 val t = str2term 
  1140 "(a * x ^^^ 2 + -2 * a * x * y + a * y ^^^ 2 + b * x ^^^ 2 + -2 * b * x * y + b * y ^^^ 2) /(a ^^^ 2 * x ^^^ 2 + -1 * a ^^^ 2 * y ^^^ 2 + -1 * b ^^^ 2 * x ^^^ 2 + b ^^^ 2 * y ^^^ 2)"; 
  1141 (* WN060831 nonterm.SK10 
  1142 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
  1143 term2str t;
  1144 *)
  1145 
  1146 (*SRM Schalk I, p.68 Nr. 438a *)
  1147 val t = str2term 
  1148 "x*y/(x*y - y^^^2)*(x^^^2 - x*y)";
  1149 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1150 term2str t;
  1151 if (term2str t) =
  1152 "x ^^^ 2"
  1153 then ()
  1154 else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1155 
  1156 (*SRM Schalk I, p.68 Nr. 439b *)
  1157 val t = str2term 
  1158 "(4*x^^^2+4*x+1)*((x^^^2 - 2*x^^^3)/(4*x^^^2+2*x))";
  1159 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1160 term2str t;
  1161 if (term2str t) =
  1162 "(x + -4 * x ^^^ 3) / 2"
  1163 then ()
  1164 else error "rational.sml: diff.behav. in norm_Rational_mg 25";
  1165 
  1166 (*SRM Schalk I, p.68 Nr. 440a *)
  1167 val t = str2term 
  1168 "(x^^^2 - 2*x)/(x^^^2 - 3*x) * (x - 3)^^^2/(x^^^2 - 4)";
  1169 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1170 term2str t;
  1171 if (term2str t) =
  1172 "(-3 + x) / (2 + x)"
  1173 then ()
  1174 else error "rational.sml: diff.behav. in norm_Rational_mg 26";
  1175 
  1176 "----- Schalk I, p.68 Nr. 440b SK11 works since 0707xx";
  1177 val t = str2term 
  1178 "(a^^^3 - 9*a)/(a^^^3*b - a*b^^^3)*(a^^^2*b+a*b^^^2)/(a+3)";
  1179 val SOME (t,_) = rewrite_set_ thy false norm_Rational t;
  1180 if term2str t = "(-3 * a + a ^^^ 2) / (a + -1 * b)" then ()
  1181 else error "rational.sml: diff.behav. in norm_Rational 27";
  1182 
  1183 "----- SK12 works since 0707xx";
  1184 val t = str2term "(a^^^3 - 9*a)*(a^^^2*b+a*b^^^2)/((a^^^3*b - a*b^^^3)*(a+3))";
  1185 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
  1186 if term2str t' = "(-3 * a + a ^^^ 2) / (a + -1 * b)" then ()
  1187 else error "rational.sml: diff.behav. in norm_Rational 28";
  1188 
  1189 
  1190 "-------- common denominator and multiplication ---------";
  1191 "-------- common denominator and multiplication ---------";
  1192 "-------- common denominator and multiplication ---------";
  1193 (*SRAM Schalk I, p.69 Nr. 441b *)
  1194 val t = str2term "(4*a/3 + 3*b^^^2/a^^^3 + b/(4*a))*(4*b/(3*a))";
  1195 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1196 term2str t;
  1197 if (term2str t) =
  1198   "(36 * b ^^^ 3 + 3 * a ^^^ 2 * b ^^^ 2 + 16 * a ^^^ 4 * b) / (9 * a ^^^ 4)"
  1199 then () else error "rational.sml: diff.behav. in norm_Rational_mg 28";
  1200 
  1201 (*SRAM Schalk I, p.69 Nr. 442b *)
  1202 val t = str2term "(15*a^^^2/x^^^3 - 5*b^^^4/x^^^2 + 25*c^^^2/x)*(x^^^3/(5*a*b^^^3*c^^^3)) + 1/c^^^3 * (b*x/a - 3*a/b^^^3)";
  1203 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1204 term2str t;
  1205 if (term2str t) =
  1206 "5 * x ^^^ 2 / (a * b ^^^ 3 * c)"
  1207 then () else error "rational.sml: diff.behav. in norm_Rational_mg 29";
  1208 
  1209 (*SRAM Schalk I, p.69 Nr. 443b *)
  1210 val t = str2term "(a/2 + b/3)*(b/3 - a/2)";
  1211 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1212 term2str t;
  1213 if (term2str t) =
  1214 "(-9 * a ^^^ 2 + 4 * b ^^^ 2) / 36"
  1215 then () else error "rational.sml: diff.behav. in norm_Rational_mg 30";
  1216 
  1217 (*SRAM Schalk I, p.69 Nr. 445b *)
  1218 val t = str2term "(a^^^2/9 + 2*a/(3*b) + 4/b^^^2)*(a/3 - 2/b) + 8/b^^^3";
  1219 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1220 term2str t;
  1221 if (term2str t) =
  1222 "a ^^^ 3 / 27"
  1223 then () else error "rational.sml: diff.behav. in norm_Rational_mg 31";
  1224 
  1225 (*SRAM Schalk I, p.69 Nr. 446b *)
  1226 val t = str2term "(x/(5*x + 4*y) - y/(5*x - 4*y) + 1)*(25*x^^^2 - 16*y^^^2)";
  1227 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1228 term2str t;
  1229 if (term2str t) =
  1230 "30 * x ^^^ 2 + -9 * x * y + -20 * y ^^^ 2"
  1231 then ()
  1232 else error "rational.sml: diff.behav. in norm_Rational_mg 32";
  1233 
  1234 (*SRAM Schalk I, p.69 Nr. 449a *)(*Achtung: rechnet ca 8 Sekunden*)
  1235 val t = str2term 
  1236 "(2*x^^^2/(3*y)+x/y^^^2)*(4*x^^^4/(9*y^^^2)+x^^^2/y^^^4)*(2*x^^^2/(3*y) - x/y^^^2)";
  1237 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1238 term2str t;
  1239 if (term2str t) = "(-81 * x ^^^ 4 + 16 * x ^^^ 8 * y ^^^ 4) / (81 * y ^^^ 8)"
  1240 then () else error "rational.sml: diff.behav. in norm_Rational_mg 33";
  1241 
  1242 
  1243 (*SRAM Schalk I, p.69 Nr. 450a *)
  1244 val t = str2term 
  1245 "(4*x/(3*y)+2*y/(3*x))^^^2 - (2*y/(3*x) - 2*x/y)*(2*y/(3*x)+2*x/y)";
  1246 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1247 term2str t;
  1248 if (term2str t) =
  1249 "(52 * x ^^^ 2 + 16 * y ^^^ 2) / (9 * y ^^^ 2)"
  1250 then ()
  1251 else error "rational.sml: diff.behav. in norm_Rational_mg 34";
  1252 
  1253 
  1254 "-------- double fractions ------------------------------";
  1255 "-------- double fractions ------------------------------";
  1256 "-------- double fractions ------------------------------";
  1257 (*SRD Schalk I, p.69 Nr. 454b *)
  1258 val t = str2term 
  1259 "((2 - x)/(2*a)) / (2*a/(x - 2))";
  1260 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1261 term2str t;
  1262 if (term2str t) = 
  1263 "(-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2)"
  1264 then ()
  1265 else error "rational.sml: diff.behav. in norm_Rational_mg 35";
  1266 
  1267 (*SRD Schalk I, p.69 Nr. 455a *)
  1268 val t = str2term 
  1269 "(a^^^2 + 1)/(a^^^2 - 1) / ((a+1)/(a - 1))";
  1270 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1271 term2str t;
  1272 if (term2str t) = 
  1273 "(1 + a ^^^ 2) / (1 + 2 * a + a ^^^ 2)" then ()
  1274 else error "rational.sml: diff.behav. in norm_Rational_mg 36";
  1275 
  1276 
  1277 "----- Schalk I, p.69 Nr. 455b";
  1278 val t = str2term "(x^^^2 - 4)/(y^^^2 - 9)/((2+x)/(3 - y))";
  1279 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1280 if term2str t = "(2 + -1 * x) / (3 + y)" then ()
  1281 else error "rational.sml: diff.behav. in norm_Rational_mg 37";
  1282 
  1283 "----- SK060904-1a non-termination of cancel_p_ ?: worked before 0707xx";
  1284 val t = str2term "(x^^^2 - 4)*(3 - y)/((y^^^2 - 9)*(2+x))";
  1285 val SOME (t,_) = rewrite_set_ thy false norm_Rational t;
  1286 if term2str t = "(2 + -1 * x) / (3 + y)" then ()
  1287 else error "rational.sml: diff.behav. in norm_Rational_mg 37b";
  1288 
  1289 "----- ?: worked before 0707xx";
  1290 val t = str2term "(3 + -1 * y) / (-9 + y ^^^ 2)";
  1291 val SOME (t,_) = rewrite_set_ thy false norm_Rational t;
  1292 if term2str t = "-1 / (3 + y)" then ()
  1293 else error "rational.sml: -1 / (3 + y) norm_Rational";
  1294 
  1295 (*SRD Schalk I, p.69 Nr. 456b *)
  1296 val t = str2term 
  1297 "(b^^^3 - b^^^2)/(b^^^2+b)/(b^^^2 - 1)";
  1298 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1299 term2str t;
  1300 if (term2str t) = "b / (1 + 2 * b + b ^^^ 2)" then ()
  1301 else error "rational.sml: diff.behav. in norm_Rational_mg 38";
  1302 
  1303 (*SRD Schalk I, p.69 Nr. 457b *)
  1304 val t = str2term 
  1305 "(16*a^^^2 - 9*b^^^2)/(2*a+3*a*b) / ((4*a+3*b)/(4*a^^^2 - 9*a^^^2*b^^^2))";
  1306 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1307 term2str t;
  1308 if (term2str t) = 
  1309   "8 * a ^^^ 2 + -6 * a * b + -12 * a ^^^ 2 * b + 9 * a * b ^^^ 2"
  1310 then () else error "rational.sml: diff.behav. in norm_Rational_mg 39";
  1311 
  1312 "----- Schalk I, p.69 Nr. 458b works since 0707";
  1313 val t = str2term 
  1314 "(2*a^^^2*x - a^^^2)/(a*x - b*x) / (b^^^2*(2*x - 1)/(x*(a - b)))";
  1315 val SOME (t,_) = rewrite_set_ thy false norm_Rational t;
  1316 if term2str t = "a ^^^ 2 / b ^^^ 2" then ()
  1317 else error "rational.sml: diff.behav. in norm_Rational_mg 39b";
  1318 
  1319 (*SRD Schalk I, p.69 Nr. 459b *)
  1320 val t = str2term "(a^^^2 - b^^^2)/(a*b) / (4*(a+b)^^^2/a)";
  1321 val SOME (t,_) = rewrite_set_ thy false norm_Rational t;
  1322 if term2str t = "(a + -1 * b) / (4 * a * b + 4 * b ^^^ 2)" then ()
  1323 else error "rational.sml: diff.behav. in norm_Rational_mg 41";
  1324 
  1325 
  1326 (*Schalk I, p.69 Nr. 460b nonterm.SK
  1327 val t = str2term 
  1328 "(9*(x^^^2 - 8*x+16)/(4*(y^^^2 - 2*y+1)))/((3*x - 12)/(16*y - 16))";
  1329 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1330 if term2str t = 
  1331 then ()
  1332 else error "rational.sml: diff.behav. in norm_Rational_mg 42";
  1333 
  1334 val t = str2term 
  1335 "9*(x^^^2 - 8*x+16)*(16*y - 16)/(4*(y^^^2 - 2*y+1)*(3*x - 12))";
  1336 val SOME (t',_) = rewrite_set_ thy false norm_Rational t;
  1337 ... non terminating.
  1338 val SOME (t',_) = rewrite_set_ thy false make_polynomial t;
  1339 "(-2304 + 1152 * x + 2304 * y + -144 * x ^^^ 2 + -1152 * x * y + 144 * x ^^^ 2 * y) /(-48 + 12 * x + 96 * y + -24 * x * y + -48 * y ^^^ 2 + 12 * x * y ^^^ 2)";
  1340 val SOME (t,_) = rewrite_set_ thy false cancel_p t';
  1341 ... non terminating.*)
  1342 
  1343 (*SRD Schalk I, p.70 Nr. 472a *)
  1344 val t = str2term ("((8*x^^^2 - 32*y^^^2)/(2*x + 4*y))/" ^
  1345 		 "((4*x - 8*y)/(x + y))");
  1346 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1347 term2str t;
  1348 if (term2str t) = 
  1349 "x + y"
  1350 then ()
  1351 else error "rational.sml: diff.behav. in norm_Rational_mg 43";
  1352 
  1353 
  1354 (*----------------------------------------------------------------------*)
  1355 (*---------------------- Einfache Dppelbrche --------------------------*)
  1356 (*----------------------------------------------------------------------*)
  1357 
  1358 (*SRD Schalk I, p.69 Nr. 461a *)
  1359 val t = str2term 
  1360 "(2/(x+3) + 2/(x - 3)) / (8*x/(x^^^2 - 9))";
  1361 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1362 term2str t;
  1363 if (term2str t) = 
  1364 "1 / 2"
  1365 then ()
  1366 else error "rational.sml: diff.behav. in norm_Rational_mg 44";
  1367 
  1368 (*SRD Schalk I, p.69 Nr. 464b *)
  1369 val t = str2term 
  1370 "(a - a/(a - 2)) / (a + a/(a - 2))";
  1371 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1372 term2str t;
  1373 if (term2str t) = 
  1374 "(3 + -1 * a) / (1 + -1 * a)"
  1375 then ()
  1376 else error "rational.sml: diff.behav. in norm_Rational_mg 45";
  1377 
  1378 (*SRD Schalk I, p.69 Nr. 465b *)
  1379 val t = str2term 
  1380 "((x+3*y)/9 + (4*y^^^2 - 9*z^^^2)/(16*x)) /(x/9+y/6+z/4)";
  1381 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1382 term2str t;
  1383 if (term2str t) = 
  1384 "(4 * x + 6 * y + -9 * z) / (4 * x)"
  1385 then ()
  1386 else error "rational.sml: diff.behav. in norm_Rational_mg 46";
  1387 
  1388 (*SRD Schalk I, p.69 Nr. 466b *)
  1389 val t = str2term 
  1390 "((1 - 7*(x - 2)/(x^^^2 - 4)) / (6/(x+2))) / (3/(x+5)+30/(x^^^2 - 25))";
  1391 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1392 term2str t;
  1393 if (term2str t) = 
  1394 "(25 + -10 * x + x ^^^ 2) / 18"
  1395 then ()
  1396 else error "rational.sml: diff.behav. in norm_Rational_mg 47";
  1397 
  1398 (*SRD Schalk I, p.70 Nr. 469 *)
  1399 val t = str2term 
  1400 "3*b^^^2/(4*a^^^2 - 8*a*b + 4*b^^^2)/(a/(a^^^2*b - b^^^3) + (a - b)/(4*a*b^^^2+4*b^^^3) - 1/(4*b^^^2))";
  1401 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1402 term2str t;
  1403 if (term2str t) = 
  1404 "3 * b ^^^ 3 / (2 * a + -2 * b)"
  1405 then ()
  1406 else error "rational.sml: diff.behav. in norm_Rational_mg 48";
  1407 
  1408 (*----------------------------------------------------------------------*)
  1409 (*---------------------- Mehrfache Dppelbrueche ------------------------*)
  1410 (*----------------------------------------------------------------------*)
  1411 
  1412 (*SRD.test Schalk I, p.70 Nr. 476b *) (* Rechenzeit: 10 sec *)
  1413 (*WN060419 crashes with method 'simplify' ????SK*)
  1414 val t = str2term 
  1415 "((a^^^2 - b^^^2)/(2*a*b)+2*a*b/(a^^^2 - b^^^2))/((a^^^2+b^^^2)/(2*a*b)+1) / ((a^^^2+b^^^2)^^^2/(a+b)^^^2)";
  1416 val SOME (t,_) = rewrite_set_ thy false norm_Rational t;
  1417 if term2str t = "1 / (a ^^^ 2 + -1 * b ^^^ 2)" then ()
  1418 else error "rational.sml: diff.behav. in norm_Rational_mg 49";
  1419 
  1420 "----- Schalk I, p.70 Nr. 477a";
  1421 (* MG Achtung: terme explodieren; Bsp zu komplex; 
  1422    L???ung sollte (ziemlich grosser) Faktorisierter Ausdruck sein 
  1423 val t = str2term "b*y/(b - 2*y)/((b^^^2 - y^^^2)/(b+2*y)) /" ^
  1424 		 "(b^^^2*y+b*y^^^2)*(a+x)^^^2/((b^^^2 - 4*y^^^2)*(a+2*x)^^^2)";
  1425 val SOME (t',_) = rewrite_set_ thy false norm_Rational t;
  1426 
  1427 
  1428 val t = str2term "b*y*(b+2*y)*(b^^^2 - 4*y^^^2)*(a+2*x)^^^2 / " ^
  1429 		 "((b - 2*y)*(b^^^2 - y^^^2)*(b^^^2*y+b*y^^^2)*(a+x)^^^2)";
  1430 val SOME (t,_) = rewrite_set_ thy false make_polynomial t;
  1431 ????SK ???MG*)
  1432 
  1433 
  1434 "----- Schalk I, p.70 Nr. 478b ----- Rechenzeit: 5 sec";
  1435 val t = str2term ("(a - (a*b+b^^^2)/(a+b))/(b+(a - b)/(1+(a+b)/(a - b))) / " ^
  1436 		 "((a - a^^^2/(a+b))/(a+(a*b)/(a - b)))");
  1437 val SOME (t',_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1438 if term2str t' = 
  1439 "(2 * a ^^^ 3 + 2 * a ^^^ 2 * b) / (a ^^^ 2 * b + b ^^^ 3)"
  1440 then ()
  1441 else error "rational.sml: diff.behav. in norm_Rational_mg 51";
  1442 
  1443 (* TODO.new_c:WN050820 STOP_REW_SUB introduced gave ...
  1444 if term2str t' = "(a ^^^ 4 + -1 * a ^^^ 2 * b ^^^ 2) /(a * b * (b + (a * (a + -1 * b) + -1 * b * (a + -1 * b)) / (2 * a)) * (a + -1 * b))" then ()
  1445 else error "rational.sml: works again";
  1446 re-outcommented with TODO.new_c: cvs before 071227, 11:50*)
  1447 
  1448 
  1449 
  1450 (*Schalk I, p.70 Nr. 480a *)
  1451 (* Achtung: rechnet ewig; cancel_p kann nicht krzen: WN060831 nonterm.SK00
  1452 val t = str2term 
  1453 "(1/x+1/y+1/z)/(1/x - 1/y - 1/z) / (2*x^^^2/(x^^^2 - z^^^2)/(x/(x+z)+x/(x - z)))";
  1454 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1455 term2str t;
  1456 if (term2str t) = 
  1457 
  1458 then ()
  1459 else error "rational.sml: diff.behav. in norm_Rational_mg 52";
  1460 
  1461 (*MG Berechne Zwischenergebnisse: WN060831 nonterm.SK00*)
  1462 val t = str2term 
  1463 "(1/x+1/y+1/z)/(1/x - 1/y - 1/z)";
  1464 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1465 term2str t;
  1466 "(x ^^^ 2 * y ^^^ 2 * z + x ^^^ 2 * y * z ^^^ 2 + x * y ^^^ 2 * z ^^^ 2) /
  1467 (-1 * x ^^^ 2 * y ^^^ 2 * z + -1 * x ^^^ 2 * y * z ^^^ 2 + x * y ^^^ 2 * z ^^^ 2)";
  1468 val t = str2term 
  1469 "2*x^^^2/(x^^^2 - z^^^2)/(x/(x+z)+x/(x - z))";
  1470 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1471 term2str t;
  1472 "1"
  1473 
  1474 (* SK 1. Ausdruck kann nicht weiter gekrzt werden; cancel_p !!!*)
  1475 ###  rls: cancel_p on: 
  1476 (x ^^^ 2 * (y ^^^ 2 * z) + x ^^^ 2 * (y * z ^^^ 2) + x * (y ^^^ 2 * z ^^^ 2)) /
  1477 (-1 * (x ^^^ 2 * (y ^^^ 2 * z)) + -1 * (x ^^^ 2 * (y * z ^^^ 2)) + x * (y ^^^ 2 * z ^^^ 2))
  1478 GC #3.61.81.101.197.17503:   (0 ms)
  1479 *** RATIONALS_DIRECT_CANCEL_EXCEPTION: Invalid fraction
  1480 
  1481 val t = str2term 
  1482 "(x^^^2 * (y^^^2 * z) + x^^^2 * (y * z^^^2) + x * (y^^^2 * z^^^2)) / (-1 * (x^^^2 * (y^^^2 * z)) + -1 * (x^^^2 * (y * z^^^2)) + x * (y^^^2 * z^^^2))";
  1483 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
  1484 term2str t;
  1485 (*uncaught exception nonexhaustive binding failure*)
  1486 
  1487 (* Das kann er aber krzen !!????: *)
  1488 val t = str2term 
  1489 "(x^^^2 * (y^^^2 * z) +  x * (y^^^2 * z^^^2)) / (-1 * (x^^^2 * (y * z^^^2)) + x * (y^^^2 * z^^^2))";
  1490 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
  1491 term2str t;
  1492 "(-1 * (y * x) + -1 * (z * y)) / (1 * (z * x) + -1 * (z * y))";
  1493 *)
  1494 
  1495 
  1496 "-------- crucial examples ------------------------------";
  1497 "-------- crucial examples ------------------------------";
  1498 "-------- crucial examples ------------------------------";
  1499 (*Schalk I, p.60 Nr. 215d *)
  1500 (* Achtung: rechnet ewig ...
  1501 val t = str2term "(a-b)^^^3 * (x+y)^^^4 / ((x+y)^^^2 * (a-b)^^^5)";
  1502 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1503 term2str t; noterm.SK
  1504 *)
  1505 
  1506 (* Kein Wunder, denn Z???ler und Nenner extra als Polynom dargestellt ergibt:*)
  1507 (*
  1508 val t = str2term "(a-b)^^^3 * (x+y)^^^4";
  1509 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1510 term2str t;
  1511 "a^^^3 * x^^^4 + 4 * a^^^3 * x^^^3 * y +6 * a^^^3 * x^^^2 * y^^^2 +4 * a^^^3 * x * y^^^3 +a^^^3 * y^^^4 +-3 * a^^^2 * b * x^^^4 +-12 * a^^^2 * b * x^^^3 * y +-18 * a^^^2 * b * x^^^2 * y^^^2 +-12 * a^^^2 * b * x * y^^^3 +-3 * a^^^2 * b * y^^^4 +3 * a * b^^^2 * x^^^4 +12 * a * b^^^2 * x^^^3 * y +18 * a * b^^^2 * x^^^2 * y^^^2 +12 * a * b^^^2 * x * y^^^3 +3 * a * b^^^2 * y^^^4 +-1 * b^^^3 * x^^^4 +-4 * b^^^3 * x^^^3 * y +-6 * b^^^3 * x^^^2 * y^^^2 +-4 * b^^^3 * x * y^^^3 +-1 * b^^^3 * y^^^4";
  1512 val t = str2term "((x+y)^^^2 * (a-b)^^^5)";
  1513 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1514 term2str t;
  1515 "a^^^5 * x^^^2 + 2 * a^^^5 * x * y + a^^^5 * y^^^2 +-5 * a^^^4 * b * x^^^2 +-10 * a^^^4 * b * x * y +-5 * a^^^4 * b * y^^^2 +10 * a^^^3 * b^^^2 * x^^^2 +20 * a^^^3 * b^^^2 * x * y +10 * a^^^3 * b^^^2 * y^^^2 +-10 * a^^^2 * b^^^3 * x^^^2 +-20 * a^^^2 * b^^^3 * x * y +-10 * a^^^2 * b^^^3 * y^^^2 +5 * a * b^^^4 * x^^^2 +10 * a * b^^^4 * x * y +5 * a * b^^^4 * y^^^2 +-1 * b^^^5 * x^^^2 +-2 * b^^^5 * x * y +-1 * b^^^5 * y^^^2";
  1516 *)
  1517 (*anscheinend macht dem Rechner das Krzen diese Bruches keinen Spass mehr ...*)
  1518 
  1519 (*--------------------------------------------------------------------*)
  1520 (*Schalk I, p.70 Nr. 480b 
  1521 val t = str2term "((12*x*y/(9*x^^^2 - y^^^2))/" ^
  1522 		 "(1/(3*x - y)^^^2 - 1/(3*x + y)^^^2)) *" ^
  1523 		 "(1/(x - 5*y)^^^2 - 1/(x + 5*y)^^^2)/" ^
  1524 		 "(20*x*y/(x^^^2 - 25*y^^^2))";
  1525 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1526 SK.nonterm
  1527 Kann nicht weiter vereinfacht werden !!!!?? *)
  1528 
  1529 (*--------------------------------------------------------------------*)
  1530 "---- MGs test set";
  1531 val t = str2term " (1 + x^^^5) / (y + x) + x^^^3 / x ";
  1532 val SOME (t,_) = rewrite_set_ thy false common_nominator_p t;
  1533 if term2str t = "(1 + x ^^^ 3 + x ^^^ 5 + y * x ^^^ 2) / (x + y)" then()
  1534 else error "";
  1535 
  1536 (*--------------------------------------------------------------------*)
  1537 (* cancel_p liefert nicht immer Polynomnormalform (2): WN060831???SK3b
  1538    ---> Sortierung FALSCH !!  *)
  1539 val t = str2term "b^^^3 * a^^^5/a ";
  1540 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
  1541 term2str t;
  1542 "1 * (a^^^4 * b^^^3) / 1"; (*OK*)
  1543 
  1544 val t = str2term "b^^^3 * a^^^5/b ";
  1545 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
  1546 term2str t;
  1547 "1 * (b^^^2 * a^^^5) / 1"; (*cancel_p sortiert hier falsch um!*)
  1548 
  1549 (* Problem liegt NICHT bei ord_make_polynomial! (siehe folgende Bsple) *)
  1550 (*
  1551 val x = str2term "x"; val bdv = str2term "bdv";
  1552 val t1 = str2term "b^^^2 * a^^^5";
  1553 val t2 = str2term "a^^^5 * b^^^2 ";
  1554 ord_make_polynomial false Rational.thy [(x,bdv)] (t1,t2); (*false*)
  1555 *)
  1556 (* ==> "b^^^2 * a^^^5" > "a^^^5 * b^^^2 " ... OK!*)
  1557 
  1558 (*--------------------------------------------------------------------*)
  1559 (* cancel_p liefert nicht immer Polynomnormalform (2): WN060831???SK3c
  1560    ---> erzeugt berflssige "1 * ..."
  1561    
  1562 val t = str2term "-1 / (3 + y)";
  1563 (*~~         *)
  1564 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
  1565 term2str t;
  1566 "-1 / (3 + 1 * y)";
  1567 (********* Das ist das PROBLEM !!!!!!!??? *******************)
  1568 (* -1 im Z???ler der Angabe verursacht das Problem !*)
  1569 *)
  1570 
  1571 (* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *)
  1572 "----- MGs test set";
  1573 val t = str2term "(a^^^2 + -1)/(a+1)";
  1574 val SOME (t',_) = rewrite_set_ thy false cancel_p t;
  1575 if term2str t' = "(-1 + a) / 1" then ()
  1576 else error "rational.sml MG tests 3d";
  1577 
  1578 "----- NOT TERMINATING ?: worked before 0707xx";
  1579 val t = str2term "(a^^^2 - 1)*(b + 1) / ((b^^^2 - 1)*(a+1))";
  1580 val SOME (t'',_) = rewrite_set_ thy false norm_Rational t;
  1581 if term2str t'' = "(1 + -1 * a) / (1 + -1 * b)" then ()
  1582 else error "rational.sml MG tests 3e";
  1583 
  1584 "----- corrected SK060905";
  1585 val t = str2term "(4*x^^^2 - 20*x + 25)/(2*x - 5)^^^3";
  1586 val SOME (t',_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1587 if term2str t' = "-1 / (5 + -2 * x)" then ()
  1588 else error "rational.sml corrected SK060905";
  1589 
  1590 
  1591 "-------- examples for Stefan Karnels thesis ------------";
  1592 "-------- examples for Stefan Karnels thesis ------------";
  1593 "-------- examples for Stefan Karnels thesis ------------";
  1594 (*SRAM Schalk I, p.69 Nr. 442b --- abgewandelt*)
  1595 val t = str2term 
  1596 "(15*a^^^4/(a*x^^^3) - 5*a*((b^^^4 - 5*c^^^2*x)/x^^^2))*(x^^^3/(5*a*b^^^3*c^^^3)) + a/c^^^3 * (x*(b/a) - 3*b*(a/b^^^4))";
  1597 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1598 term2str t;
  1599 if (term2str t) =
  1600 "5 * x ^^^ 2 / (b ^^^ 3 * c)"
  1601 then ()
  1602 else error "rational.sml: diff.behav. in norm_Rational_mg 53";
  1603 
  1604 
  1605 "-------- me Schalk I No.186 ----------------------------";
  1606 "-------- me Schalk I No.186 ----------------------------";
  1607 "-------- me Schalk I No.186 ----------------------------";
  1608 val fmz = ["Term ((14 * x * y) / ( x * y ))",
  1609 	   "normalform N"];
  1610 val (dI',pI',mI') =
  1611   ("Rational",["rational","simplification"],
  1612    ["simplification","of_rationals"]);
  1613 val p = e_pos'; val c = []; 
  1614 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
  1615 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1616 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1617 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1618 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1619 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1620 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1621 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1622 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
  1623 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
  1624 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;(*++ for explicit script*)
  1625 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;(*++ for explicit script*)
  1626 case (f2str f, nxt) of
  1627     ("14", ("End_Proof'", _)) => ()
  1628   | _ => error "rational.sml diff.behav. in me Schalk I No.186";
  1629 
  1630 
  1631 "-------- interSteps ..Simp_Rat_Double_No-1.xml ---------";
  1632 "-------- interSteps ..Simp_Rat_Double_No-1.xml ---------";
  1633 "-------- interSteps ..Simp_Rat_Double_No-1.xml ---------";
  1634 states:=[];
  1635 CalcTree
  1636 [(["Term (((2 - x)/(2*a)) / (2*a/(x - 2)))", "normalform N"], 
  1637   ("Rational",["rational","simplification"],
  1638   ["simplification","of_rationals"]))];
  1639 Iterator 1;
  1640 moveActiveRoot 1;
  1641 autoCalculate 1 CompleteCalc;
  1642 val ((pt,p),_) = get_calc 1; show_pt pt;
  1643 
  1644 interSteps 1 ([1],Res);
  1645 val ((pt,p),_) = get_calc 1; show_pt pt;
  1646 
  1647 
  1648 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ---------";
  1649 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ---------";
  1650 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ---------";
  1651 states:=[];
  1652 CalcTree
  1653 [(["Term ((a^2 + -1*b^2) / (a^2 + -2*a*b + b^2))", "normalform N"], 
  1654   ("Rational",["rational","simplification"],
  1655   ["simplification","of_rationals"]))];
  1656 Iterator 1;
  1657 moveActiveRoot 1;
  1658 autoCalculate 1 CompleteCalc;
  1659 val ((pt,p),_) = get_calc 1; show_pt pt;
  1660 (*========== inhibit exn 110314 ================================================
  1661 (*with explicit script done already... and removed [1,..] at below...
  1662 interSteps 1 ([1],Res);
  1663 val ((pt,p),_) = get_calc 1; show_pt pt;
  1664 *)
  1665 interSteps 1 ([2],Res);
  1666 val ((pt,p),_) = get_calc 1; show_pt pt;
  1667 
  1668 interSteps 1 ([2,1],Res);
  1669 val ((pt,p),_) = get_calc 1; show_pt pt;
  1670 val newnds = children (get_nd pt [2,1]) (*see "fun detailrls"*);
  1671 (*if length newnds = 12 then () WN060905*)
  1672 if length newnds = 13 then ()
  1673 else error "rational.sml: interSteps cancel_p rev_rew_p";
  1674 
  1675 val p = ([2,1,9],Res);
  1676 getTactic 1 p;
  1677 val (_, tac, _) = pt_extract (pt, p);
  1678 (*case tac of SOME (Rewrite ("sym_real_plus_binom_times1", _)) => ()
  1679 WN060905*)
  1680 case tac of SOME (Rewrite ("sym_real_add_mult_distrib2", _)) => ()
  1681 | _ => error "rational.sml: getTactic, sym_real_plus_binom_times1";
  1682 ============ inhibit exn 110314 ==============================================*)
  1683 
  1684 
  1685 "-------- investigate rulesets for cancel_p -------------";
  1686 "-------- investigate rulesets for cancel_p -------------";
  1687 "-------- investigate rulesets for cancel_p -------------";
  1688 val thy = @{theory "Rational"};
  1689 "---------------- (a^^^2 + -1*b^^^2) / (a^^^2 + -2*a*b + b^^^2)";
  1690 val t = str2term "(a^^^2 + -1*b^^^2) / (a^^^2 + -2*a*b + b^^^2)";
  1691 val tt = str2term "(1 * a + 1 * b) * (1 * a + -1 * b)"(*numerator only*);
  1692 "----- with rewrite_set_";
  1693 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1694 term2str tt'= "a ^^^ 2 + -1 * b ^^^ 2" (*true*);
  1695 val tt = str2term "((1 * a + -1 * b) * (1 * a + -1 * b))"(*denominator only*);
  1696 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1697 term2str tt' = "a ^^^ 2 + -2 * a * b + b ^^^ 2" (*true*);
  1698 
  1699 "----- with make_deriv";
  1700 val SOME (tt, _) = factout_p_ thy t; term2str tt =
  1701 "(1 * a + 1 * b) * (1 * a + -1 * b) / ((1 * a + -1 * b) * (1 * a + -1 * b))";
  1702 (*
  1703 "--- with ruleset as before 060829";
  1704 val {rules, rew_ord=(_,ro),...} =
  1705     rep_rls (assoc_rls "make_polynomial");
  1706 val der = make_deriv thy Atools_erls rules ro NONE tt;
  1707 print_depth 99; map (term2str o #1) der; print_depth 3;
  1708 print_depth 99; map (rule2str o #2) der; print_depth 3;
  1709 ... did not terminate*)
  1710 "--- with simpler ruleset";
  1711 val {rules, rew_ord=(_,ro),...} =
  1712     rep_rls (assoc_rls "rev_rew_p");
  1713 val der = make_deriv thy Atools_erls rules ro NONE tt;
  1714 print_depth 99; writeln (deriv2str der); print_depth 3;
  1715 
  1716 print_depth 99; map (term2str o #1) der; print_depth 3;
  1717 "...,(-1 * b ^^^ 2 + a ^^^ 2) / (-2 * (a * b) + a ^^^ 2 + (-1 * b) ^^^ 2) ]";
  1718 print_depth 99; map (rule2str o #2) der; print_depth 3;
  1719 print_depth 99; map (term2str o #1 o #3) der; print_depth 3;
  1720 
  1721 val der = make_deriv thy Atools_erls rules ro NONE 
  1722 		     (str2term "(1 * a + 1 * b) * (1 * a + -1 * b)");
  1723 print_depth 99; writeln (deriv2str der); print_depth 3;
  1724 
  1725 val {rules, rew_ord=(_,ro),...} =
  1726     rep_rls (assoc_rls "rev_rew_p");
  1727 val der = make_deriv thy Atools_erls rules ro NONE 
  1728 		     (str2term "(1 * a + -1 * b) * (1 * a + -1 * b)");
  1729 print_depth 99; writeln (deriv2str der); print_depth 3;
  1730 print_depth 99; map (term2str o #1) der; print_depth 3;
  1731 (*WN060829 ...postponed*)
  1732 
  1733 
  1734 "-------- investigate format of factout_ and factout_p_ -";
  1735 "-------- investigate format of factout_ and factout_p_ -";
  1736 "-------- investigate format of factout_ and factout_p_ -";
  1737 val {rules, rew_ord = (_,ro),...} = rep_rls (assoc_rls "make_polynomial");
  1738 val (thy, eval_rls) = (@{theory "Rational"}, Atools_erls)(*see 'fun init_state'*);
  1739 val Rrls {scr = Rfuns {init_state,...},...} = assoc_rls "cancel_p";
  1740 
  1741 "----- see Rational.ML, local cancel_p, fun init_state";
  1742 val t = str2term "(a^^^2 + (-1)*b^^^2) / (a^^^2 + (-2)*a*b + b^^^2)";
  1743 val SOME (t',_) = factout_p_ thy t; term2str t';
  1744 (*
  1745 val rtas = reverse_deriv thy eval_rls rules ro NONE t';
  1746 writeln(trtas2str rst);
  1747 *)
  1748 
  1749 
  1750 "----- see Rational.ML, local cancel_p, fun init_state";
  1751 val t = str2term "a^^^2 / a";
  1752 val SOME (t',_) = factout_p_ thy t; 
  1753 term2str t' = "a * a / (1 * a)" (*true*); 
  1754 (*... can be canceled with
  1755 real_mult_div_cancel2 ?k ~= 0 ==> ?m * ?k / (?n * ?k) = ?m / ?n"*)
  1756 (* sml/ME/rewtools.sml:
  1757 val rtas = reverse_deriv thy Atools_erls rules ro NONE t';
  1758 writeln (deri2str rtas);
  1759 *)
  1760 
  1761 
  1762 "-------- SK 060904 ----------------------------------------------";
  1763 "----- order on polynomials -- input + output";
  1764 val thy = @{theory "Isac"};
  1765 val t = str2term "(a + -1 * b) / (-1 * a + b)";
  1766 val SOME (t', _) = factout_p_ thy t; term2str t';
  1767 val SOME (t', _) = cancel_p_ thy t; term2str t';
  1768 
  1769 val t = str2term "a*b*c*d / (d*e*f*g)";
  1770 val SOME (t', _) = cancel_p_ thy t; term2str t';
  1771 
  1772 val t = str2term "a*(b*(c*d)) / (b*(e*(f*g)))";
  1773 val SOME (t', _) = cancel_p_ thy t; term2str t';
  1774 (*???order.SK  ???*)
  1775 
  1776 "----- SK060904-1a non-termination of cancel_p_ ? worked before 0707xx";
  1777 val t = str2term "(x^^^2 - 4)*(3 - y) / ((y^^^2 - 9)*(2+x))";
  1778 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; 
  1779 if term2str t' = "(2 + -1 * x) / (3 + y)" then ()
  1780 else error "rational.sml SK060904-1a worked since 0707xx";
  1781 
  1782 "----- SK060904-1b non-termination of cancel_p_ ... worked before 0707xx";
  1783 val t = str2term ("(9 * a ^^^ 2 + -16 * b ^^^ 2) /" ^
  1784 "(36 * a^^^2 * c + (27 * a^^^2 * e + (-64 * b^^^2 * c + -48 * b^^^2 * e)))");
  1785 val SOME (t',_) = cancel_p_ thy t; 
  1786 if term2str t' = "1 / (4 * c + 3 * e)" then ()
  1787 else error "rational.sml SK060904-1b";
  1788 
  1789 
  1790 "----- SK060904-2a non-termination of add_fraction_p_";
  1791 val t = str2term (" (a + b * x) / (a + -1 * (b * x)) +  " ^
  1792 		  " (-1 * a + b * x) / (a + b * x)      ");
  1793 (* nonterm.SK
  1794 val SOME (t',_) = rewrite_set_ thy false common_nominator_p t;
  1795 
  1796 common_nominator_p_ thy t;
  1797 " (a + b * x)*(a + b * x) / ((a + -1 * (b * x))*(a + -1 * (b * x))) +  " ^
  1798 " (-1 * a + b * x)*(a + -1 * (b * x)) / ((a + b * x)*(-1 * a + b * x)) ";
  1799 
  1800 add_fraction_p_ thy t; 
  1801 " ((a + b * x)*(a + b * x)  +  (-1 * a + b * x)*(a + -1 * (b * x))) /" ^
  1802 " ((a + b * x)*(-1 * a + b * x))                                     ";
  1803 *)
  1804 
  1805 
  1806 "-------- how to stepwise construct Scripts -------------";
  1807 "-------- how to stepwise construct Scripts -------------";
  1808 "-------- how to stepwise construct Scripts -------------";
  1809 val thy = @{theory "Rational"};
  1810 (*no trailing _*)
  1811 val p1 = parse thy (
  1812 "Script SimplifyScript (t_t::real) =                             " ^
  1813 "  (Rewrite_Set discard_minus False                   " ^
  1814 "   t_t)");
  1815 
  1816 (*required (): (Rewrite_Set discard_minus False)*)
  1817 val p2 = parse thy (
  1818 "Script SimplifyScript (t_t::real) =                             " ^
  1819 "  (Rewrite_Set discard_minus False                   " ^
  1820 "   t_t)");
  1821 
  1822 val p3 = parse thy (
  1823 "Script SimplifyScript (t_t::real) =                             " ^
  1824 "  ((Rewrite_Set discard_minus False)                   " ^
  1825 "   t_t)");
  1826 
  1827 val p4 = parse thy (
  1828 "Script SimplifyScript (t_t::real) =                             " ^
  1829 "  ((Rewrite_Set discard_minus False)                   " ^
  1830 "   t_t)");
  1831 
  1832 val p5 = parse thy (
  1833 "Script SimplifyScript (t_t::real) =                             " ^
  1834 "  ((Try (Rewrite_Set discard_minus False)                   " ^
  1835 "    Try (Rewrite_Set discard_parentheses False))               " ^
  1836 "   t_t)");
  1837 
  1838 val p6 = parse thy (
  1839 "Script SimplifyScript (t_t::real) =                             " ^
  1840 "  ((Try (Rewrite_Set discard_minus False) @@                   " ^
  1841 "    Try (Rewrite_Set rat_mult_poly False) @@                    " ^
  1842 "    Try (Rewrite_Set make_rat_poly_with_parentheses False) @@   " ^
  1843 "    Try (Rewrite_Set cancel_p_rls False) @@                     " ^
  1844 "    (Repeat                                                     " ^
  1845 "     ((Try (Rewrite_Set common_nominator_p_rls False) @@        " ^
  1846 "       Try (Rewrite_Set rat_mult_div_pow False) @@              " ^
  1847 "       Try (Rewrite_Set make_rat_poly_with_parentheses False) @@" ^
  1848 "       Try (Rewrite_Set cancel_p_rls False) @@                  " ^
  1849 "       Try (Rewrite_Set rat_reduce_1 False)))) @@               " ^
  1850 "    Try (Rewrite_Set discard_parentheses False))               " ^
  1851 "   t_t)"
  1852 );
  1853 
  1854 "----------- get_denominator ----------------------------";
  1855 "----------- get_denominator ----------------------------";
  1856 "----------- get_denominator ----------------------------";
  1857 val thy = @{theory Isac};
  1858 val t = term_of (the (parse thy "get_denominator ((a +x)/b)"));
  1859 val SOME (_, t') = eval_get_denominator "" 0 t thy;
  1860 if term2str t' = "get_denominator ((a + x) / b) = b" then ()
  1861 else error "get_denominator ((a + x) / b) = b"
  1862 
  1863 
  1864 "--------- several errpats in complicated term -------------------";
  1865 "--------- several errpats in complicated term -------------------";
  1866 "--------- several errpats in complicated term -------------------";
  1867 (*TODO: instead of Gabriella's example here (27.Jul.12) find a simpler one*)
  1868 states:=[];
  1869 CalcTree
  1870 [(["Term ((5*b + 25)/(a^2 - b^2) * (a - b)/(5*b))", "normalform N"], 
  1871   ("Rational",["rational","simplification"], ["simplification","of_rationals"]))];
  1872 Iterator 1;
  1873 moveActiveRoot 1;
  1874 autoCalculate 1 CompleteCalc;
  1875 val ((pt,p),_) = get_calc 1; show_pt pt;
  1876 
  1877 "-------- nonterminating cancel_p, norm_Rational 2002 ---";
  1878 "-------- nonterminating cancel_p, norm_Rational 2002 ---";
  1879 "-------- nonterminating cancel_p, norm_Rational 2002 ---";
  1880 (*------------------------------------------------------------------------------------\
  1881 "--- WN121204: searched rational.sml for "nonterm", added new numbering" ^
  1882 "              and thoroughly tested with this numbering subsequently";
  1883 "--- 1 ---";
  1884 WN.2.6.03 from rlang.sml 56a 
  1885 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x) = 4 * (a * b) / (a ^^^ 2 + -1 * b ^^^ 2)";
  1886 val NONE = rewrite_set_ thy false common_nominator_p t;
  1887 "--- 2 ---";
  1888 WN060831 nonterm.SK7 
  1889 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x)";
  1890 val NONE = add_fraction_p_ thy t;
  1891 "--- 3 ---";
  1892 nonterm.SK9 loops: cancel_p kann nicht weiter kuerzen!!! *)
  1893 val t'' = str2term "(a + b)/(x^^^2 - y^^^2) * ((x - y)^^^2/(a^^^2 - b^^^2))";
  1894 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t'';
  1895 "--- 4 ---";
  1896 val t = str2term 
  1897 "(a * x ^^^ 2 + -2 * a * x * y + a * y ^^^ 2 + b * x ^^^ 2 + -2 * b * x * y + b * y ^^^ 2) /(a ^^^ 2 * x ^^^ 2 + -1 * a ^^^ 2 * y ^^^ 2 + -1 * b ^^^ 2 * x ^^^ 2 + b ^^^ 2 * y ^^^ 2)"; 
  1898 WN060831 nonterm.SK10 
  1899 val SOME (t,_) = rewrite_set_ thy false cancel_p t;
  1900 term2str t;
  1901 "--- 5 ---";
  1902 val t = str2term 
  1903 "(9*(x^^^2 - 8*x+16)/(4*(y^^^2 - 2*y+1)))/((3*x - 12)/(16*y - 16))";
  1904 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1905 if term2str t = 
  1906 then ()
  1907 else error "rational.sml: diff.behav. in norm_Rational_mg 42";
  1908 "--- 6 ---";
  1909 val t = str2term 
  1910 "9*(x^^^2 - 8*x+16)*(16*y - 16)/(4*(y^^^2 - 2*y+1)*(3*x - 12))";
  1911 val SOME (t',_) = rewrite_set_ thy false norm_Rational t;
  1912 ... non terminating.
  1913 "--- 7 ---";
  1914 val SOME (t',_) = rewrite_set_ thy false make_polynomial t;
  1915 "(-2304 + 1152 * x + 2304 * y + -144 * x ^^^ 2 + -1152 * x * y + 144 * x ^^^ 2 * y) /(-48 + 12 * x + 96 * y + -24 * x * y + -48 * y ^^^ 2 + 12 * x * y ^^^ 2)";
  1916 val SOME (t,_) = rewrite_set_ thy false cancel_p t';
  1917 "--- 8 ---";
  1918 val t = str2term "(a-b)^^^3 * (x+y)^^^4 / ((x+y)^^^2 * (a-b)^^^5)";
  1919 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1920 "--- 9 ---";
  1921 val t = str2term "((12*x*y/(9*x^^^2 - y^^^2))/" ^
  1922 		 "(1/(3*x - y)^^^2 - 1/(3*x + y)^^^2)) *" ^
  1923 		 "(1/(x - 5*y)^^^2 - 1/(x + 5*y)^^^2)/" ^
  1924 		 "(20*x*y/(x^^^2 - 25*y^^^2))";
  1925 val SOME (t,_) = rewrite_set_ thy false norm_Rational(*_mg*) t;
  1926 "--- 10 ---";
  1927 SK060904-2a non-termination of add_fraction_p_";
  1928 val t = str2term (" (a + b * x) / (a + -1 * (b * x)) +  " ^
  1929 		  " (-1 * a + b * x) / (a + b * x)      ");
  1930 (* nonterm.SK = WN130830
  1931 val SOME (t',_) = rewrite_set_ thy false common_nominator_p t;
  1932 "--- 11 ---";
  1933 common_nominator_p_ thy t;
  1934 " (a + b * x)*(a + b * x) / ((a + -1 * (b * x))*(a + -1 * (b * x))) +  " ^
  1935 " (-1 * a + b * x)*(a + -1 * (b * x)) / ((a + b * x)*(-1 * a + b * x)) ";
  1936 "--- 12 ---";                             
  1937 add_fraction_p_ thy t; 
  1938 " ((a + b * x)*(a + b * x)  +  (-1 * a + b * x)*(a + -1 * (b * x))) /" ^
  1939 " ((a + b * x)*(-1 * a + b * x))                                     ";
  1940 --------------------------------------------------------------------------------------/*)
  1941 
  1942 (*------------------------------------------------------------------------------------\
  1943 "WN121205: thoroughly tested with the above numbering.";
  1944 "  errors in cancel_p: --- 4,5,6,7.";
  1945 "  error in common_nominator_p, common_nominator_p_: --- 10; 1,2?.";
  1946 "  errors caused by ruleset norm_Rational: --- 8,9.";
  1947 trace_rewrite := false;
  1948 
  1949 "--- 1 ---: non-terminating with ### add_fract: done t22 "; = WN130830
  1950 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x) = 4 * (a * b) / (a ^^^ 2 + -1 * b ^^^ 2)";
  1951 trace_rewrite := false;
  1952 rewrite_set_ thy false common_nominator_p t;
  1953 
  1954 "--- 2 ---: non-terminating with ### add_fract: done t22 ";
  1955 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x)";
  1956 add_fraction_p_ thy t;
  1957 
  1958 "--- 3 ---: norm_Rational calls Rrls cancel_p with non-normalised polys";
  1959 val t = str2term "(a + b)/(x^^^2 - y^^^2) * ((x - y)^^^2/(a^^^2 - b^^^2))";
  1960 rewrite_set_ thy false norm_Rational t;
  1961 (*tracing end...####  rls: cancel_p on: 
  1962 (a * x ^^^ 2 + -2 * (a * (x * y)) + a * y ^^^ 2 + b * x ^^^ 2 + -2 * (b * (x * y)) + b * y ^^^ 2) /
  1963 (a ^^^ 2 * x ^^^ 2 + -1 * (a ^^^ 2 * y ^^^ 2) + -1 * (b ^^^ 2 * x ^^^ 2) + b ^^^ 2 * y ^^^ 2) *)
  1964 
  1965 "--- 4 ---: non-terminating with Rrls cancel_p on plausible input";
  1966 val t = str2term (
  1967 "(a * x ^^^ 2 + -2 * a * x * y + a * y ^^^ 2 + b * x ^^^ 2 + -2 * b * x * y + b * y ^^^ 2) /"^
  1968 "(a ^^^ 2 * x ^^^ 2 + -1 * a ^^^ 2 * y ^^^ 2 + -1 * b ^^^ 2 * x ^^^ 2 + b ^^^ 2 * y ^^^ 2)"); 
  1969 rewrite_set_ thy false cancel_p t;
  1970 (*#  rls: cancel_p on: 
  1971 (a * x ^^^ 2 + -2 * a * x * y + a * y ^^^ 2 + b * x ^^^ 2 + -2 * b * x * y + b * y ^^^ 2) /
  1972 (a ^^^ 2 * x ^^^ 2 + -1 * a ^^^ 2 * y ^^^ 2 + -1 * b ^^^ 2 * x ^^^ 2 + b ^^^ 2 * y ^^^ 2) *)
  1973 
  1974 "--- 5 ---: non-terminating with Rrls cancel_p on plausible input";
  1975 val t = str2term "(9*(x^^^2 - 8*x+16)/(4*(y^^^2 - 2*y+1)))/((3*x - 12)/(16*y - 16))";
  1976 rewrite_set_ thy false norm_Rational t;
  1977 (*####  rls: cancel_p on: 
  1978 (2304 + -1152 * x + -2304 * y + 144 * x ^^^ 2 + 1152 * (x * y) + -144 * (x ^^^ 2 * y)) /
  1979 (48 + -12 * x + -96 * y + 24 * (x * y) + 48 * y ^^^ 2 + -12 * (x * y ^^^ 2))  *)
  1980 
  1981 "--- 6 ---: non-terminating with Rrls cancel_p on plausible input";
  1982 val t = str2term 
  1983 "9*(x^^^2 - 8*x+16)*(16*y - 16)/(4*(y^^^2 - 2*y+1)*(3*x - 12))";
  1984 rewrite_set_ thy false norm_Rational t;
  1985 (*###  rls: cancel_p on: (-2304 + 1152 * x + 2304 * y + -144 * x ^^^ 2 + -1152 * (x * y) +
  1986  144 * (x ^^^ 2 * y)) /
  1987 (-48 + 12 * x + 96 * y + -24 * (x * y) + -48 * y ^^^ 2 + 12 * (x * y ^^^ 2)) *)
  1988 
  1989 "--- 7 ---: non-terminating with Rrls cancel_p on plausible input";
  1990 val t' = str2term (
  1991 "(-2304 + 1152 * x + 2304 * y + -144 * x ^^^ 2 + -1152 * x * y + 144 * x ^^^ 2 * y) /"^
  1992 "(-48 + 12 * x + 96 * y + -24 * x * y + -48 * y ^^^ 2 + 12 * x * y ^^^ 2)");
  1993 rewrite_set_ thy false cancel_p t';
  1994 
  1995 "--- 8 ---: bottom of output cannot be looked ad (due to looping?)";
  1996 val t = str2term "(a-b)^^^3 * (x+y)^^^4 / ((x+y)^^^2 * (a-b)^^^5)";
  1997 val SOME (t,_) = rewrite_set_ thy false norm_Rational t;
  1998 
  1999 "--- 9 ---: probably error in norm_Rational";
  2000 val t = str2term (
  2001 "((12*x*y / (9*x^^^2 - y^^^2)) / (1/(3*x - y)^^^2 - 1/(3*x + y)^^^2)) *" ^
  2002 		"(1/(x - 5*y)^^^2 - 1/(x + 5*y)^^^2) / (20*x*y/(x^^^2 - 25*y^^^2))");
  2003 rewrite_set_ thy false norm_Rational t;
  2004 (*####  rls: cancel_p on: (19440 * (x ^^^ 8 * y ^^^ 2) + -490320 * (x ^^^ 6 * y ^^^ 4) +
  2005  108240 * (x ^^^ 4 * y ^^^ 6) +
  2006  -6000 * (x ^^^ 2 * y ^^^ 8)) /
  2007 (2160 * (x ^^^ 8 * y ^^^ 2) + -108240 * (x ^^^ 6 * y ^^^ 4) +
  2008  1362000 * (x ^^^ 4 * y ^^^ 6) +
  2009  -150000 * (x ^^^ 2 * y ^^^ 8)) *)
  2010 
  2011 "--- 10 ---: non-terminating with ### add_fract: done t22: error in common_nominator_p ";
  2012 val t = str2term (" (a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x)");
  2013 rewrite_set_ thy false common_nominator_p t; (*### add_fract: done t22 *)
  2014 common_nominator_p_ thy t;                   (*loops without output*)
  2015 "--- reformulated 10:";
  2016 val t = str2term "(a + -1 * (b * x)) / (a + b * x)";
  2017 rewrite_set_ thy false cancel_p t = NONE;
  2018 "--- 11 ---";
  2019 "--- 12 ---";                             
  2020 "...both are to be considered after common_nominator_p_ is improved";
  2021 --------------------------------------------------------------------------------------/*)
  2022 ============ inhibit exn WN130824 TODO ======================================================*)
  2023