test/Tools/isac/Knowledge/rational.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Tue, 06 Feb 2018 16:18:43 +0100
changeset 59348 ddfabb53082c
parent 59248 5eba5e6d5266
child 59369 5f9f07d37a1e
permissions -rw-r--r--
Isabelle2015->17: transfer changes from src/ to test/
     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 "-------- rls norm_Rational downto fun add_fraction_p_ -----------------------";
    21 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
    22 "-------- rewrite_set_ add_fractions_p from: Mathematik 1 Schalk -------------";
    23 "-------- integration lev.1 -- lev.5: cancel_p_ & add_fractions_p_ -----------";
    24 "-------- reverse rewrite ----------------------------------------------------";
    25 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
    26 "-------- investigate rls norm_Rational --------------------------------------";
    27 "-------- examples: rls norm_Rational ----------------------------------------";
    28 "-------- rational numerals --------------------------------------------------";
    29 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
    30 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
    31 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
    32 "-------- examples common denominator and multiplication from: Schalk --------";
    33 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
    34 "-------- me Schalk I No.186 -------------------------------------------------";
    35 "-------- interSteps ..Simp_Rat_Double_No-1.xml ------------------------------";
    36 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ------------------------------";
    37 "-------- investigate rulesets for cancel_p ----------------------------------";
    38 "-------- fun eval_get_denominator -------------------------------------------";
    39 "-------- several errpats in complicated term --------------------------------";
    40 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
    41 "-----------------------------------------------------------------------------";
    42 "-----------------------------------------------------------------------------";
    43 
    44 
    45 "-------- fun poly_of_term ---------------------------------------------------";
    46 "-------- fun poly_of_term ---------------------------------------------------";
    47 "-------- fun poly_of_term ---------------------------------------------------";
    48 val vs = "12 * x^^^3 * y^^^4 * z^^^6" |> str2term |> vars |> map free2str |> sort string_ord;
    49 
    50 if poly_of_term vs (str2term "12::real") = SOME [(12, [0, 0, 0])]
    51 then () else error "poly_of_term 1 changed";
    52 if poly_of_term vs (str2term "x::real") = SOME [(1, [1, 0, 0])]
    53 then () else error "poly_of_term 2 changed";
    54 if poly_of_term vs (str2term "12 * x^^^3") = SOME [(12, [3, 0, 0])]
    55 then () else error "poly_of_term 3 changed";
    56 if poly_of_term vs (str2term "12 * x^^^3 * y^^^4 * z^^^6") = SOME [(12, [3, 4, 6])]
    57 then () else error "poly_of_term 4 changed";
    58 if poly_of_term vs (str2term "1 + 2 * x^^^3 * y^^^4 * z^^^6 + y") =
    59   SOME [(1, [0, 0, 0]), (1, [0, 1, 0]), (2, [3, 4, 6])]
    60 then () else error "poly_of_term 5 changed";
    61 
    62 (*poly_of_term is quite liberal:*)
    63 (*the coefficient may be somewhere, the order of variables and the parentheses 
    64   within a monomial are arbitrary*)
    65 if poly_of_term vs (str2term "y^^^4 * (x^^^3 * 12 * z^^^6)") = SOME [(12, [3, 4, 6])]
    66 then () else error "poly_of_term 6 changed";
    67 
    68 (*there may even be more than 1 coefficient:*)
    69 if poly_of_term vs (str2term "2 * y^^^4 * (x^^^3 * 6 * z^^^6)") = SOME [(12, [3, 4, 6])]
    70 then () else error "poly_of_term 7 changed";
    71 
    72 (*the order and the parentheses within monomials are arbitrary:*)
    73 if poly_of_term vs (str2term "2 * x^^^3 * y^^^4 * z^^^6 + (7 * y^^^8 + 1)")
    74   = SOME [(1, [0, 0, 0]), (7, [0, 8, 0]), (2, [3, 4, 6])]
    75 then () else error "poly_of_term 8 changed";
    76 
    77 "-------- fun is_poly --------------------------------------------------------";
    78 "-------- fun is_poly --------------------------------------------------------";
    79 "-------- fun is_poly --------------------------------------------------------";
    80 if is_poly (str2term "2 * x^^^3 * y^^^4 * z^^^6 + 7 * y^^^8 + 1")
    81 then () else error "is_poly 1 changed";
    82 if not (is_poly (str2term "2 * (x^^^3 * y^^^4 * z^^^6 + 7) * y^^^8 + 1"))
    83 then () else error "is_poly 2 changed";
    84 
    85 "-------- fun term_of_poly ---------------------------------------------------";
    86 "-------- fun term_of_poly ---------------------------------------------------";
    87 "-------- fun term_of_poly ---------------------------------------------------";
    88 val expT = HOLogic.realT
    89 val Free (_, baseT) = (hd o vars o str2term) "12 * x^^^3 * y^^^4 * z^^^6";
    90 val p = [(1, [0, 0, 0]), (7, [0, 8, 0]), (2, [3, 4, 5])]
    91 val vs = ["x","y","z"]
    92 (*precondition for [(c, es),...]: legth es = length vs*)
    93 ;
    94 if term2str (term_of_poly baseT expT vs p) = "1 + 7 * y ^^^ 8 + 2 * x ^^^ 3 * y ^^^ 4 * z ^^^ 5"
    95 then () else error "term_of_poly 1 changed";
    96 
    97 "-------- integration lev.1 fun factout_p_ -----------------------------------";
    98 "-------- integration lev.1 fun factout_p_ -----------------------------------";
    99 "-------- integration lev.1 fun factout_p_ -----------------------------------";
   100 val t = str2term "(x^^^2 + -1*y^^^2) / (x^^^2 + -1*x*y)"
   101 val SOME (t', asm) = factout_p_ thy t;
   102 if term2str t' = "(x + y) * (x + -1 * y) / (x * (x + -1 * y))"
   103 then () else error ("factout_p_ term 1 changed: " ^ term2str t')
   104 ;
   105 if terms2str asm = "[\"x ~= 0\",\"x + -1 * y ~= 0\"]"
   106 then () else error "factout_p_ asm 1 changed"
   107 ;
   108 val t = str2term "nothing + to_cancel ::real";
   109 if NONE = factout_p_ thy t then () else error "factout_p_ doesn't report non-applicable";
   110 ;
   111 val t = str2term "((3 * x^^^2 + 6 *x + 3) / (2*x + 2))";
   112 val SOME (t', asm) = factout_p_ thy t;
   113 if term2str t' = "(3 + 3 * x) * (1 + x) / (2 * (1 + x))" andalso 
   114   terms2str asm = "[\"1 + x ~= 0\"]"
   115 then () else error "factout_p_ 1 changed";
   116 
   117 "-------- integration lev.1 fun cancel_p_ ------------------------------------";
   118 "-------- integration lev.1 fun cancel_p_ ------------------------------------";
   119 "-------- integration lev.1 fun cancel_p_ ------------------------------------";
   120 val t = str2term "(x^^^2 + -1*y^^^2) / (x^^^2 + -1*x*y)"
   121 val SOME (t', asm) = cancel_p_ thy t;
   122 if (term2str t', terms2str asm) = ("(x + y) / x", "[\"x ~= 0\"]")
   123 then () else error ("cancel_p_ (t', asm) 1 changed: " ^ term2str t')
   124 ;
   125 val t = str2term "nothing + to_cancel ::real";
   126 if NONE = cancel_p_ thy t then () else error "cancel_p_ doesn't report non-applicable";
   127 ;
   128 val t = str2term "((3 * x^^^2 + 6 *x + 3) / (2*x + 2))";
   129 val SOME (t', asm) = cancel_p_ thy t;
   130 if term2str t' = "(3 + 3 * x) / 2" andalso terms2str asm = "[]"
   131 then () else error "cancel_p_ 1 changed";
   132 
   133 "-------- integration lev.1 fun common_nominator_p_ --------------------------";
   134 "-------- integration lev.1 fun common_nominator_p_ --------------------------";
   135 "-------- integration lev.1 fun common_nominator_p_ --------------------------";
   136 val t = str2term ("y / (a*x + b*x + c*x) " ^
   137               (* n1    d1                   *)
   138                 "+ a / (x*y)");
   139               (* n2    d2                   *)
   140 val SOME (t', asm) = common_nominator_p_ thy t;
   141 if term2str t' =
   142       ("y * y / (x * ((a + b + c) * y)) " ^
   143   (*  n1  *d2'/ (c'* ( d1'        *d2')) *)
   144      "+ a * (a + b + c) / (x * ((a + b + c) * y))")
   145    (*  n2 * d1'         / (c'* ( d1'        *d2')) *)
   146 then () else error "common_nominator_p_ term 1 changed";
   147 if terms2str asm = "[\"a + b + c ~= 0\",\"y ~= 0\",\"x ~= 0\"]"
   148 then () else error "common_nominator_p_ asm 1 changed"
   149 
   150 "-------- example in mail Nipkow";
   151 val t = str2term "x/(x^^^2 + -1*y^^^2) + y/(x^^^2 + -1*x*y)";
   152 val SOME (t', asm) = common_nominator_p_ thy t;
   153 if term2str t' = "x * x / " ^ 
   154                  "((x + -1 * y) * ((x + y) * x))" ^
   155             " +\n" ^ 
   156                  "y * (x + y) / " ^ 
   157                  "((x + -1 * y) * ((x + y) * x))"
   158 then () else error "common_nominator_p_ term 2 changed"
   159 ;
   160 if terms2str asm = "[\"x + y ~= 0\",\"x ~= 0\",\"x + -1 * y ~= 0\"]"
   161 then () else error "common_nominator_p_ asm 2 changed"
   162 
   163 "-------- example: applicable tested by SML code";
   164 val t = str2term "nothing / to_add";
   165 if NONE = common_nominator_p_ thy t then () else error "common_nominator_p_ term 3 changed";
   166 ;
   167 val t = str2term "((x + (-1)) / (x + 1)) + ((x + 1) / (x + (-1)))";
   168 val SOME (t', asm) = common_nominator_p_ thy t;
   169 if term2str t' = 
   170    "(x + -1) * (-1 + x) / ((1 + x) * (-1 + x)) +\n(x + 1) * (1 + x) / ((1 + x) * (-1 + x))"
   171   andalso terms2str asm = "[\"1 + x ~= 0\",\"-1 + x ~= 0\"]"
   172 then () else error "common_nominator_p_ 3 changed";
   173 
   174 "-------- integration lev.1 fun add_fraction_p_ ------------------------------";
   175 "-------- integration lev.1 fun add_fraction_p_ ------------------------------";
   176 "-------- integration lev.1 fun add_fraction_p_ ------------------------------";
   177 val t = str2term "((x + (-1)) / (x + 1)) + ((x + 1) / (x + (-1)))";
   178 val SOME (t', asm) = add_fraction_p_ thy t;
   179 if term2str t' = "(2 + 2 * x ^^^ 2) / (-1 + x ^^^ 2)" 
   180 then () else error "add_fraction_p_ 3 changed";
   181 ;
   182 if terms2str asm = "[\"-1 + x ^^^ 2 ~= 0\"]"
   183 then () else error "add_fraction_p_ 3 changed";
   184 ;
   185 val t = str2term "nothing / to_add";
   186 if NONE = add_fraction_p_ thy t then () else error "add_fraction_p_ term 3 changed";
   187 ;
   188 val t = str2term "((x + (-1)) / (x + 1)) + ((x + 1) / (x + (-1)))";
   189 val SOME (t', asm) = add_fraction_p_ thy t;
   190 if term2str t' = "(2 + 2 * x ^^^ 2) / (-1 + x ^^^ 2)" andalso
   191   terms2str asm = "[\"-1 + x ^^^ 2 ~= 0\"]"
   192 then () else error "add_fraction_p_ 3 changed";
   193 
   194 "-------- and app_rev ...traced down from rewrite_set_ until prepats ---------";
   195 "-------- and app_rev ...traced down from rewrite_set_ until prepats ---------";
   196 "-------- and app_rev ...traced down from rewrite_set_ until prepats ---------";
   197 (* trace down until prepats are evaluated 
   198   (which does not to work, because substitution is not done -- compare rew_sub!);
   199   keep this sequence for the case, factout_p, cancel_p, common_nominator_p, add_fraction_p
   200   (again) get prepat = [] changed to <>[]. *)
   201 val t = str2term "(x^^^2 + -1*y^^^2) / (x^^^2 + -1*x*y)";
   202 
   203 (*rewrite_set_ @{theory Isac} true cancel t = NONE; !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   204 "~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) = (thy, false, cancel_p, t);
   205 "~~~~~ fun rewrite__set_, args:"; val (thy, i, _, _, (rrls as Rrls _), t) =
   206   (thy, 1, bool, [], rls, term);
   207 (*val (t', asm, rew) = app_rev thy (i+1) rrls t; rew = false!!!!!!!!!!!!!!!!!!!!!*)
   208 "~~~~~ and app_rev, args:"; val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
   209     fun chk_prepat thy erls [] t = true
   210       | chk_prepat thy erls prepat t =
   211         let
   212           fun chk (pres, pat) =
   213             (let 
   214               val subst: Type.tyenv * Envir.tenv =
   215                 Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
   216              in
   217               snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
   218              end) handle _ => false
   219            fun scan_ f [] = false (*scan_ NEVER called by []*)
   220              | scan_ f (pp::pps) =
   221                if f pp then true else scan_ f pps;
   222         in scan_ chk prepat end;
   223     (* apply the normal_form of a rev-set *)
   224     fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   225       if chk_prepat thy erls prepat t
   226       then ((*tracing("### app_rev': t = "^term2str t);*) normal_form t)
   227       else NONE;
   228 (*  val opt = app_rev' thy rrls t  ..NONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   229 "~~~~~ and app_rev', args:"; val (thy, (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}), t) =
   230   (thy, rrls, t);
   231 (* chk_prepat thy erls prepat t = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   232 (* app_sub thy i rrls t = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   233 "~~~~~ fun chk_prepat, args:"; val (thy, erls, prepat, t) = (thy, erls, prepat, t);
   234           fun chk (pres, pat) =
   235             (let 
   236               val subst: Type.tyenv * Envir.tenv =
   237                 Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
   238              in
   239               snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
   240              end) handle _ => false
   241            fun scan_ f [] = false (*scan_ NEVER called by []*)
   242              | scan_ f (pp::pps) =
   243                if f pp then true else scan_ f pps;
   244 
   245 (*========== inhibit exn WN130823: prepat is empty ====================================
   246 (* scan_ chk prepat = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   247 "~~~~~ fun , args:"; val (f, (pp::pps)) = (chk, prepat);
   248 f;
   249 val ([t1, t2], t) = pp;
   250 term2str t1 = "?r is_expanded";
   251 term2str t2 = "?s is_expanded";
   252 term2str t = "?r / ?s";
   253 (* f pp = false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   254 "~~~~~ fun chk, args:"; val (pres, pat) = (pp);
   255               val subst: Type.tyenv * Envir.tenv =
   256                 Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
   257 (*subst = 
   258   ({}, {(("r", 0), ("real", Var (("r", 0), "real"))), 
   259         (("s", 0), ("real", Var (("s", 0), "real")))}*)
   260 ;
   261               snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
   262 "~~~~~ fun eval__true, args:"; val (thy, i, asms, bdv, rls) =
   263   (thy, (i + 1), (map (Envir.subst_term subst) pres), [], erls);
   264 terms2str asms;                         (* = "[\"?r is_expanded\",\"?s is_expanded\"]"*)
   265 asms = [@{term True}] orelse asms = []; (* = false*)
   266 asms = [@{term False}]                ; (* = false*)
   267 "~~~~~ fun chk, args:"; val (indets, (a::asms)) = ([], asms);
   268 bdv (*= []: _a list*);
   269 val bdv : (term * term) list = [];
   270 rewrite__set_ thy (i+1) false;
   271 term2str a = "?r is_expanded"; (*hier m"usste doch der Numerator eingesetzt sein ??????????????*)
   272 val SOME (Const ("HOL.False", _), []) = rewrite__set_ thy (i+1) false bdv rls a
   273 ============ inhibit exn WN130823: prepat is empty ===================================*)
   274 
   275 "-------- fun rewrite_set_ cancel_p downto fun gcd_poly ----------------------";
   276 "-------- fun rewrite_set_ cancel_p downto fun gcd_poly ----------------------";
   277 "-------- fun rewrite_set_ cancel_p downto fun gcd_poly ----------------------";
   278 val t = str2term "(12 * x * y) / (8 * y^^^2 )";
   279 (* "-------- example 187a": exception Div raised...
   280 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;*)
   281 val t = str2term "(8 * x^^^2 * y * z ) / (18 * x * y^^^2 * z )";
   282 (* "-------- example 187b": doesn't terminate...
   283 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;*)
   284 val t = str2term "(9 * x^^^5 * y^^^2 * z^^^4) / (15 * x^^^6 * y^^^3 * z )";
   285 (* "-------- example 187c": doesn't terminate...
   286 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;*)
   287 "~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) = (@{theory Isac}, false, cancel_p, t);
   288 (* WN130827: exception Div raised...
   289 rewrite__set_ thy 1 bool [] rls term
   290 *)
   291 "~~~~~ and rewrite__set_, args:"; val (thy, i, _, _, (rrls as Rrls _), t) =
   292   (thy, 1, bool, [], rls, term);
   293 (* WN130827: exception Div raised...
   294 	val (t', asm, rew) = app_rev thy (i+1) rrls t
   295 *)
   296 "~~~~~ fun app_rev, args:"; val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
   297 (* WN130827: exception Div raised...
   298     val opt = app_rev' thy rrls t
   299 *)
   300 "~~~~~ fun app_rev', args:"; val (thy, (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}), t) =
   301   (thy, rrls, t);
   302 chk_prepat thy erls prepat t    = true;
   303 (* WN130827: exception Div raised...
   304 normal_form t
   305 *)
   306 (* lookup Rational.thy, cancel_p: normal_form = cancel_p_ thy*)
   307 "~~~~~ fun cancel_p_, args:"; val (t) = (t);
   308 val opt = check_fraction t;
   309 val SOME (numerator, denominator) = opt
   310         val vs = t |> vars |> map free2str |> sort string_ord
   311         val baseT = type_of numerator
   312         val expT = HOLogic.realT
   313 val (SOME a, SOME b) = (poly_of_term vs numerator, poly_of_term vs denominator);
   314 (*"-------- example 187a": exception Div raised...
   315 val a = [(12, [1, 1])]: poly
   316 val b = [(8, [0, 2])]: poly
   317               val ((a', b'), c) = gcd_poly a b
   318 *)
   319 (* "-------- example 187b": doesn't terminate...
   320 val a = [(8, [2, 1, 1])]: poly
   321 val b = [(18, [1, 2, 1])]: poly
   322               val ((a', b'), c) = gcd_poly a b
   323 *)
   324 (* "-------- example 187c": doesn't terminate...
   325 val a = [(9, [5, 2, 4])]: poly
   326 val b = [(15, [6, 3, 1])]: poly
   327               val ((a', b'), c) = gcd_poly a b
   328 *)
   329 
   330 "-------- rls norm_Rational downto fun gcd_poly ------------------------------";
   331 "-------- rls norm_Rational downto fun gcd_poly ------------------------------";
   332 "-------- rls norm_Rational downto fun gcd_poly ------------------------------";
   333 val t = str2term "(x^^^2 - 4)*(3 - y) / ((y^^^2 - 9)*(2+x))";
   334 trace_rewrite := false (*true false*);
   335 (* trace stops with ...: (and then jEdit hangs)..
   336 rewrite_set_ thy false norm_Rational t;
   337 :
   338 ###  rls: cancel_p on: (-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /
   339 (-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)
   340 *)
   341 val t = str2term (*copy from above: "::real" is not required due to "^^^"*)
   342   ("(-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /" ^
   343   "(-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)");
   344 (*cancel_p_ thy t;
   345 exception Div raised*)
   346 
   347 "~~~~~ fun cancel_p_, args:"; val (t) = (t);
   348 val opt = check_fraction t;
   349 val SOME (numerator, denominator) = opt
   350         val vs = t |> vars |> map free2str |> sort string_ord
   351         val baseT = type_of numerator
   352         val expT = HOLogic.realT;
   353 (*default_print_depth 3; 999*)
   354 val (SOME a, SOME b) = (poly_of_term vs numerator, poly_of_term vs denominator);
   355 (*default_print_depth 3; 999*)
   356 (* does not terminate instead of returning ?:
   357         val ((a', b'), c) = gcd_poly a b
   358 val a = [(~12, [0, 0]), (3, [2, 0]), (4, [0, 1]), (~1, [2, 1])]: poly
   359 val b = [(~18, [0, 0]), (~9, [1, 0]), (2, [0, 2]), (1, [1, 2])]: poly
   360 *)
   361 
   362 "-------- rls norm_Rational downto fun add_fraction_p_ -----------------------";
   363 "-------- rls norm_Rational downto fun add_fraction_p_ -----------------------";
   364 "-------- rls norm_Rational downto fun add_fraction_p_ -----------------------";
   365 val thy =  @{theory Isac};
   366 "----- SK060904-2a non-termination of add_fraction_p_";
   367 val t = str2term (" (a + b * x) / (a + -1 * (b * x)) +  " ^
   368 		         " (-1 * a + b * x) / (a + b * x)      ");
   369 (* rewrite_set_ thy false norm_Rational t
   370 exception Div raised*)
   371 (* rewrite_set_ thy false add_fractions_p t;
   372 exception Div raised*)
   373 "~~~~~ fun rewrite_set_, args:"; val (thy, bool, rls, term) =
   374   (@{theory Isac}, false, add_fractions_p, t);
   375 "~~~~~ and rewrite__set_, args:"; val (thy, i, _, _, (rrls as Rrls _), t) =
   376   (thy, 1, bool, [], rls, term);
   377 (* app_rev thy (i+1) rrls t;
   378 exception Div raised*)
   379 "~~~~~ and app_rev, args:"; val (thy, i, rrls, t) = (thy, (i+1), rrls, t);
   380     fun chk_prepat thy erls [] t = true
   381       | chk_prepat thy erls prepat t =
   382         let
   383           fun chk (pres, pat) =
   384             (let 
   385               val subst: Type.tyenv * Envir.tenv =
   386                 Pattern.match thy (pat, t) (Vartab.empty, Vartab.empty)
   387              in
   388               snd (eval__true thy (i + 1) (map (Envir.subst_term subst) pres) [] erls)
   389              end) handle _ => false
   390            fun scan_ f [] = false (*scan_ NEVER called by []*)
   391              | scan_ f (pp::pps) =
   392                if f pp then true else scan_ f pps;
   393         in scan_ chk prepat end;
   394     (* apply the normal_form of a rev-set *)
   395     fun app_rev' thy (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}) t =
   396       if chk_prepat thy erls prepat t
   397       then ((*tracing("### app_rev': t = "^term2str t);*) normal_form t)
   398       else NONE;
   399 (*  val opt = app_rev' thy rrls t;
   400 exception Div raised*)
   401 (*  val opt = app_rev' thy rrls t;
   402 exception Div raised*)
   403 "~~~~~ and app_rev', args:"; val (thy, (Rrls{erls,prepat,scr=Rfuns{normal_form,...},...}), t) =
   404   (thy, rrls, t);
   405 chk_prepat thy erls prepat t = true       = true;
   406 (*normal_form t
   407 exception Div raised*)
   408 (* lookup Rational.thy, val add_fractions_p: normal_form = add_fraction_p_ thy*)
   409 (*add_fraction_p_ thy t
   410 exception Div raised*)
   411 "~~~~~ fun add_fraction_p_, args:"; val ((_: theory), t) = (thy, t);
   412 val SOME ((n1, d1), (n2, d2)) = check_frac_sum t;
   413 term2str n1; term2str d1; term2str n2; term2str d2;
   414       val vs = t |> vars |> map str_of_free_opt (* tolerate Var in simplification *)
   415         |> filter is_some |> map the |> sort string_ord;
   416 (*default_print_depth 3; 999*)
   417 val (SOME _, SOME a, SOME _, SOME b) =
   418   (poly_of_term vs n1, poly_of_term vs d1, poly_of_term vs n2, poly_of_term vs d2);
   419 (*default_print_depth 3; 999*)
   420 (*
   421 val a = [(1, [1, 0, 0]), (~1, [0, 1, 1])]: poly
   422 val b = [(1, [1, 0, 0]), (1, [0, 1, 1])]: poly
   423             val ((a', b'), c) = gcd_poly a b
   424 *)
   425 
   426 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
   427 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
   428 "-------- rewrite_set_ cancel_p from: Mathematik 1 Schalk Reniets Verlag -----";
   429 val thy  = @{theory "Rational"};
   430 "-------- WN";
   431 val t = str2term "(2 + -3 * x) / 9";
   432 if NONE = rewrite_set_ thy false cancel_p t then ()
   433 else error "rewrite_set_ cancel_p must return NONE, if the term cannot be cancelled";
   434 
   435 "-------- example 186a";
   436 val t = str2term "(14 * x * y) / (x * y)";
   437   is_expanded (str2term "14 * x * y");
   438   is_expanded (str2term "x * y");
   439 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   440 if (term2str t', terms2str asm) = ("14 / 1", "[]")
   441 then () else error "rational.sml cancel Schalk 186a";
   442 
   443 "-------- example 186b";
   444 val t = str2term "(60 * a * b) / ( 15 * a  * b )";
   445 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   446 if (term2str t', terms2str asm) = ("4 / 1", "[]")
   447 then () else error "rational.sml cancel Schalk 186b";
   448 
   449 "-------- example 186c";
   450 val t = str2term "(144 * a^^^2 * b * c) / (12 * a * b * c)";
   451 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   452 if (term2str t', terms2str asm) = ("12 * a / 1", "[]")
   453 then () else error "rational.sml cancel Schalk 186c";
   454 
   455 (* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! exception Div raised !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   456   see --- fun rewrite_set_ downto fun gcd_poly ---
   457 "-------- example 187a";
   458 val t = str2term "(12 * x * y) / (8 * y^^^2 )";
   459 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   460 if (term2str t', terms2str asm) = ("3 * x / (2 * y)", "[\"4 * y ~= 0\"]")
   461 then () else error "rational.sml cancel Schalk 187a";
   462 *)
   463 
   464 (* doesn't terminate !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   465   see --- fun rewrite_set_ downto fun gcd_poly ---
   466 "-------- example 187b";
   467 val t = str2term "(8 * x^^^2 * y * z ) / (18 * x * y^^^2 * z )";
   468 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   469 if (term2str t', terms2str asm) = ("4 * x / (9 * y)", "[\"2 * (z * (y * x)) ~= 0\"]")
   470 then () else error "rational.sml cancel Schalk 187b";
   471 *)
   472 
   473 (* doesn't terminate !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   474   see --- fun rewrite_set_ downto fun gcd_poly ---
   475 "-------- example 187c";
   476 val t = str2term "(9 * x^^^5 * y^^^2 * z^^^4) / (15 * x^^^6 * y^^^3 * z )";
   477 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   478 if (term2str t', terms2str asm) = 
   479   ("3 * z ^^^ 3 / (5 * (y * x))", "[\"3 * (z * (y ^^^ 2 * x ^^^ 5)) ~= 0\"]") 
   480 then () else error "rational.sml cancel Schalk 187c";
   481 *)
   482 
   483 "-------- example 188a";
   484 val t = str2term "(-8 + 8 * x) / (-9 + 9 * x)";
   485   is_expanded (str2term "8 * x + -8");
   486 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   487 if (term2str t', terms2str asm) = ("8 / 9", "[]")
   488 then () else error "rational.sml cancel Schalk 188a";
   489 
   490 val t = str2term "(8*((-1) + x))/(9*((-1) + x))";
   491 val SOME (t, _) = rewrite_set_ thy false make_polynomial t;
   492 if (term2str t', terms2str asm) = ("8 / 9", "[]")
   493 then () else error "rational.sml cancel Schalk make_polynomial 1";
   494 
   495 "-------- example 188b";
   496 val t = str2term "(-15 + 5 * x) / (-18 + 6 * x)";
   497 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   498 if (term2str t', terms2str asm) = ("5 / 6", "[]")
   499 then () else error "rational.sml cancel Schalk 188b";
   500 
   501 "-------- example 188c";
   502 val t = str2term "(a + -1 * b) / (b + -1 * a)";
   503 val SOME (t', asm) = rewrite_set_ thy false  cancel_p t;
   504 if (term2str t', terms2str asm) = ("-1 / 1", "[]")
   505 then () else error "rational.sml cancel Schalk 188c";
   506 
   507 is_expanded (str2term "a + -1 * b") = true;
   508 val t = str2term "((-1)*(b + (-1) * a))/(1*(b + (-1) * a))";
   509 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   510 if (term2str t', terms2str asm) = ("(a + -1 * b) / (-1 * a + b)", "[]")
   511 then () else error "rational.sml cancel Schalk make_polynomial 2";
   512 
   513 "-------- example 190a";
   514 val t = str2term "( 27 * a^^^3 + 9 * a^^^2 + 3 * a + 1 ) / ( 27 * a^^^3 + 18 * a^^^2 + 3 * a )";
   515 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   516 if (term2str t', terms2str asm) = 
   517   ("(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)", "[\"3 * a + 9 * a ^^^ 2 ~= 0\"]")
   518 then () else error "rational.sml cancel Schalk 190a";
   519 
   520 "-------- example 190c";
   521 val t = str2term "((1 + 9 * a ^^^ 2)*(1 + 3 * a))/((3 * a + 9 * a ^^^ 2)*(1 + 3 * a))";
   522 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   523 if (term2str t', terms2str asm) = 
   524   ("(1 + 3 * a + 9 * a ^^^ 2 + 27 * a ^^^ 3) /\n(3 * a + 18 * a ^^^ 2 + 27 * a ^^^ 3)", "[]")
   525 then () else error "rational.sml make_polynomial Schalk 190c";
   526 
   527 "-------- example 191a";
   528 val t = str2term "( x^^^2 + -1 * y^^^2 ) / ( x + y )";
   529   is_expanded (str2term "x^^^2 + -1 * y^^^2") = false; (*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*)
   530   is_expanded (str2term "x + y") = true;
   531 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   532 if (term2str t', terms2str asm) = ("(x + -1 * y) / 1", "[]")
   533 then () else error "rational.sml make_polynomial Schalk 191a";
   534 
   535 "-------- example 191b";
   536 val t = str2term "((x + (-1) * y)*(x + y))/((1)*(x + y))";
   537 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   538 if (term2str t', terms2str asm) = ("(x ^^^ 2 + -1 * y ^^^ 2) / (x + y)", "[]")
   539 then () else error "rational.sml make_polynomial Schalk 191b";
   540 
   541 "-------- example 191c";
   542 val t = str2term "( 9 * x^^^2 + -30 * x + 25 ) / ( 9 * x^^^2 + -25 )";
   543   is_expanded (str2term "9 * x^^^2 + -30 * x + 25") = true;
   544   is_expanded (str2term "25 + -30*x + 9*x^^^2") = true;
   545   is_expanded (str2term "-25 + 9*x^^^2") = true;
   546 
   547 val t = str2term "(((-5) + 3 * x)*((-5) + 3 * x))/((5 + 3 * x)*((-5) + 3 * x))";
   548 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   549 if (term2str t', terms2str asm) = ("(25 + -30 * x + 9 * x ^^^ 2) / (-25 + 9 * x ^^^ 2)", "[]")
   550 then () else error "rational.sml make_polynomial Schalk 191c";
   551 
   552 "-------- example 192b";
   553 val t = str2term "( 7 * x^^^3 + -1 * x^^^2 * y ) / ( 7 * x * y^^^2 + -1 *  y^^^3 )";
   554 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   555 if (term2str t', terms2str asm) = ("x ^^^ 2 / y ^^^ 2", "[\"y ^^^ 2 ~= 0\"]")
   556 then () else error "rational.sml cancel_p Schalk 192b";
   557 
   558 val t = str2term "((x ^^^ 2)*(7 * x + (-1) * y))/((y ^^^ 2)*(7 * x + (-1) * y))";
   559 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   560 if (term2str t', terms2str asm) = 
   561   ("(7 * x ^^^ 3 + -1 * x ^^^ 2 * y) / (7 * x * y ^^^ 2 + -1 * y ^^^ 3)", "[]")
   562 then () else error "rational.sml make_polynomial Schalk 192b";
   563 
   564 val t = str2term "((x ^^^ 2)*(7 * x + (-1) * y))/((y ^^^ 2)*(7 * x + (-1) * y))";
   565 val SOME (t', asm) = rewrite_set_ thy false make_polynomial t;
   566 if (term2str t', terms2str asm) = 
   567   ("(7 * x ^^^ 3 + -1 * x ^^^ 2 * y) / (7 * x * y ^^^ 2 + -1 * y ^^^ 3)", "[]")
   568 then () else error "rational.sml make_polynomial Schalk WN050929 not working";
   569 
   570 "-------- example 193a";
   571 val t = str2term "( x^^^2 + -6 * x + 9 ) / ( x^^^2 + -9 )";
   572 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   573 if (term2str t', terms2str asm) = ("(-3 + x) / (3 + x)", "[\"3 + x ~= 0\"]")
   574 then () else error "rational.sml cancel_p Schalk 193a";
   575 
   576 "-------- example 193b";
   577 val t = str2term "( x^^^2 + -8 * x + 16 ) / ( 2 * x^^^2 + -32 )";
   578 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   579 if (term2str t', terms2str asm) = ("(-4 + x) / (8 + 2 * x)", "[\"8 + 2 * x ~= 0\"]")
   580 then () else error "rational.sml cancel_p Schalk 193b";
   581 
   582 "-------- example 193c";
   583 val t = str2term "( 2 * x + -50 * x^^^3 ) / ( 25 * x^^^2 + -10 * x + 1 )";
   584 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   585 if (term2str t', terms2str asm) = 
   586   ("(2 * x + 10 * x ^^^ 2) / (1 + -5 * x)", "[\"1 + -5 * x ~= 0\"]")
   587 then () else error "rational.sml cancel_p Schalk 193c";
   588 
   589 (*WN:*)
   590 val t = str2term "(-25 + 9*x^^^2)/(5 + 3*x)";
   591 val SOME (t, asm) = rewrite_set_ thy false cancel_p t;
   592 if (term2str t', terms2str asm) = ("(2 * x + 10 * x ^^^ 2) / (1 + -5 * x)", "[]")
   593 then () else error "rational.sml cancel WN 1";
   594 
   595 "-------- example heuberger";
   596 val t = str2term ("(x^^^4 + x * y + x^^^3 * y + y^^^2) / " ^
   597   "(x + 5 * x^^^2 + y + 5 * x * y + x^^^2 * y^^^3 + x * y^^^4)");
   598 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   599 if (term2str t', terms2str asm) = 
   600   ("(x ^^^ 3 + y) / (1 + 5 * x + x * y ^^^ 3)", "[\"1 + 5 * x + x * y ^^^ 3 ~= 0\"]")
   601 then () else error "rational.sml cancel_p heuberger";
   602 
   603 "-------- rewrite_set_ add_fractions_p from: Mathematik 1 Schalk -------------";
   604 "-------- rewrite_set_ add_fractions_p from: Mathematik 1 Schalk -------------";
   605 "-------- rewrite_set_ add_fractions_p from: Mathematik 1 Schalk -------------";
   606 (*deleted example 204 ... 236b at update Isabelle2012-->2013*)
   607 
   608 "-------- integration lev.1 -- lev.5: cancel_p_ & add_fractions_p_ -----------";
   609 "-------- integration lev.1 -- lev.5: cancel_p_ & add_fractions_p_ -----------";
   610 "-------- integration lev.1 -- lev.5: cancel_p_ & add_fractions_p_ -----------";
   611 val t = str2term ("123 = (a*x)/(b*x) + (c*x)/(d*x) + (e*x)/(f*x::real)");
   612 "-------- gcd_poly integration level 1: works on exact term";
   613 if NONE = cancel_p_ thy t then () else error "cancel_p_ works on exact fraction";
   614 if NONE = add_fraction_p_ thy t then () else error "add_fraction_p_ works on exact fraction";
   615 
   616 "-------- gcd_poly integration level 2: picks out ONE appropriate subterm";
   617 val SOME (t', asm) = rewrite_set_ thy false cancel_p t;
   618 if term2str t' = "123 = a * x / (b * x) + c * x / (d * x) + e / f" 
   619 then () else error "level 2, rewrite_set_ cancel_p: changed";
   620 val SOME (t', asm) = rewrite_set_ thy false add_fractions_p t;
   621 if term2str t' = "123 = (b * c * x + a * d * x) / (b * d * x) + e * x / (f * x)"
   622 then () else error "level 2, rewrite_set_ add_fractions_p: changed";
   623 
   624 "-------- gcd_poly integration level 3: rewrites all appropriate subterms";
   625 val SOME (t', asm) = rewrite_set_ thy false cancel_p_rls t;
   626 if term2str t' = "123 = a / b + c / d + e / f"
   627 then () else error "level 3, rewrite_set_ cancel_p_rls: changed";
   628 val SOME (t', asm) = rewrite_set_ thy false add_fractions_p_rls t; (*CREATE add_fractions_p_rls*)
   629 if term2str t' = "123 = (b * d * e * x + b * c * f * x + a * d * f * x) / (b * d * f * x)"
   630 then () else error "level 3, rewrite_set_ add_fractions_p_rls: changed";
   631 
   632 "-------- gcd_poly integration level 4: iteration cancel_p -- add_fraction_p";
   633 (* simpler variant *)
   634 val testrls = append_rls "testrls" e_rls [Rls_ cancel_p, Rls_ add_fractions_p]
   635 val SOME (t', asm) = rewrite_set_ thy false testrls t;
   636 (*trace_rewrite := false;
   637 #  rls: testrls on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   638 ##  rls: cancel_p on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   639 ##  rls: add_fractions_p on: 123 = a * x / (b * x) + c * x / (d * x) + e / f 
   640 ##  rls: cancel_p on: 123 = (b * c * x + a * d * x) / (b * d * x) + e / f 
   641 ##  rls: add_fractions_p on: 123 = (b * c + a * d) / (b * d) + e / f 
   642 ##  rls: cancel_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   643 ##  rls: add_fractions_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) *)
   644 if term2str t' = "123 = (b * d * e + b * c * f + a * d * f) / (b * d * f)"
   645 then () else error "level 4, rewrite_set_ *_p: changed";
   646 
   647 (* complicated variant *)
   648 val testrls_rls = append_rls "testrls_rls" e_rls [Rls_ cancel_p_rls, Rls_ add_fractions_p_rls];
   649 val SOME (t', asm) = rewrite_set_ thy false testrls_rls t;
   650 (*#  rls: testrls_rls on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   651 ##  rls: cancel_p_rls on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   652 ###  rls: cancel_p on: 123 = a * x / (b * x) + c * x / (d * x) + e * x / (f * x) 
   653 ###  rls: cancel_p on: 123 = a * x / (b * x) + c * x / (d * x) + e / f 
   654 ###  rls: cancel_p on: 123 = a * x / (b * x) + c / d + e / f 
   655 ###  rls: cancel_p on: 123 = a / b + c / d + e / f 
   656 ##  rls: add_fractions_p_rls on: 123 = a / b + c / d + e / f 
   657 ###  rls: add_fractions_p on: 123 = a / b + c / d + e / f 
   658 ###  rls: add_fractions_p on: 123 = (b * c + a * d) / (b * d) + e / f 
   659 ###  rls: add_fractions_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   660 ##  rls: cancel_p_rls on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   661 ###  rls: cancel_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   662 ##  rls: add_fractions_p_rls on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) 
   663 ###  rls: add_fractions_p on: 123 = (b * d * e + b * c * f + a * d * f) / (b * d * f) *)
   664 if term2str t' = "123 = (b * d * e + b * c * f + a * d * f) / (b * d * f)"
   665 then () else error "level 4, rewrite_set_ *_p_rls: changed"
   666 
   667 "-------- gcd_poly integration level 5: cancel_p & add_fraction_p within norm_Rational";
   668 val SOME (t', asm) = rewrite_set_ thy false norm_Rational t;
   669 if term2str t' = "123 = (a * d * f + b * c * f + b * d * e) / (b * d * f)"
   670 then () else error "level 5, rewrite_set_ norm_Rational: changed"
   671 
   672 "-------- reverse rewrite ----------------------------------------------------";
   673 "-------- reverse rewrite ----------------------------------------------------";
   674 "-------- reverse rewrite ----------------------------------------------------";
   675 (** the term for which reverse rewriting is demonstrated **)
   676 val t = str2term "(9 + -1 * x ^^^ 2) / (9 + 6 * x + x ^^^ 2)";
   677 val Rrls {scr = Rfuns {init_state = ini, locate_rule = loc,
   678   next_rule = nex, normal_form = nor, ...},...} = cancel_p;
   679 
   680 (** normal_form produces the result in ONE step **)
   681   val SOME (t',_) = nor t;
   682 if term2str t' = "(3 + -1 * x) / (3 + x)" then ()
   683 else error "rational.sml normal_form (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   684 
   685 (** initialize the interpreter state used by the 'me' **)
   686   val (t, _, revsets, _) = ini t;
   687 
   688 if length (hd revsets) = 11 then () else error "length of revset changed";
   689 if (revsets |> nth 1 |> nth 1 |> id_of_thm) = 
   690   (@{thm realpow_twoI} |> Thm.get_name_hint |> thmID_of_derivation_name)
   691 then () else error "first element of revset changed";
   692 if
   693 (revsets |> nth 1 |> nth 1 |> rule2str) = "Thm (\"realpow_twoI\",?r1 ^^^ 2 = ?r1 * ?r1)" andalso
   694 (revsets |> nth 1 |> nth 2 |> rule2str) = "Thm (\"#: 9 = 3 ^^^ 2\",9 = 3 ^^^ 2)" andalso
   695 (revsets |> nth 1 |> nth 3 |> rule2str) = "Thm (\"#: 6 * x = 2 * (3 * x)\",6 * x = 2 * (3 * x))" 
   696 andalso
   697 (revsets |> nth 1 |> nth 4 |> rule2str) = "Thm (\"#: -3 * x = -1 * (3 * x)\",-3 * x = -1 * (3 * x))" 
   698 andalso
   699 (revsets |> nth 1 |> nth 5 |> rule2str) = "Thm (\"#: 9 = 3 * 3\",9 = 3 * 3)" andalso
   700 (revsets |> nth 1 |> nth 6 |> rule2str) = "Rls_ (\"sym_order_mult_rls_\")" andalso
   701 (revsets |> nth 1 |> nth 7 |> rule2str) = 
   702   "Thm (\"sym_mult_assoc\",?a * (?b * ?c) = ?a * ?b * ?c)"
   703 then () else error "first 7 elements in revset changed"
   704 
   705 (** find the rule 'r' to apply to term 't' **)
   706 (*/------- WN1309: since cancel_ (accepted "-" between monomials) has been replaced by cancel_p_ 
   707   for Isabelle2013, we don't get a working revset, but non-termination:
   708 
   709   val SOME (r as (Thm (str, thm))) = nex revsets t;
   710   :
   711 ((3 * 3 + -1 * x * x) / (3 * 3 + 2 * 3 * x + x * x), 
   712   Rls_ ("sym_order_mult_rls_"), ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * (3 * x) + x * x), []))","
   713 ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * (3 * x) + x * x), 
   714   Thm ("sym_mult_assoc",""), ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * 3 * x + x * x), []))","
   715 ((3 * 3 + -1 * (x * x)) / (3 * 3 + 2 * 3 * x + x * x), 
   716   Thm ("sym_mult_assoc",""), ((3 * 3 + -1 * x * x) / (3 * 3 + 2 * 3 * x + x * x), []))","
   717 ((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), []))","
   718  :
   719 ### Isabelle2002:
   720   Thm ("sym_#mult_2_3", "6 = 2 * 3")
   721 ### Isabelle2009-2 for cancel_ (not cancel_p_):
   722 if str = "sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))"
   723    andalso string_of_thm thm = 
   724            (string_of_thm o Thm.make_thm o (Thm.global_cterm_of (@{theory "Isac"})))
   725                (Trueprop $ (Thm.term_of o the o (parse thy)) "9 = 3 ^^^ 2") then ()
   726 else error "rational.sml next_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   727 \---------------------------------------------------------------------------------------/*)
   728 
   729 (** check, if the rule 'r' applied by the user to 't' belongs to the ruleset;
   730   if the rule is OK, the term resulting from applying the rule is returned,too;
   731   there might be several rule applications inbetween,
   732   which are listed after the head in reverse order **)
   733 (*/-------------------------------------------- Isabelle2013: this gives "error id_of_thm";
   734   we don't repair this, because interaction within "reverse rewriting" never worked properly:
   735 
   736   val (r, (t, asm))::_ = loc revsets t r;
   737 if term2str t = "(9 - x ^^^ 2) / (3 ^^^ 2 + 6 * x + x ^^^ 2)" andalso asm = []
   738 then () else error "rational.sml locate_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   739 
   740 (* find the next rule to apply *)
   741   val SOME (r as (Thm (str, thm))) = nex revsets t;
   742 if str = "sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))" andalso
   743    string_of_thm thm = (string_of_thm o Thm.make_thm o (Thm.global_cterm_of (@{theory "Isac"})))
   744                 (Trueprop $ (Thm.term_of o the o (parse thy)) "9 = 3 ^^^ 2") then ()
   745 else error "rational.sml next_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   746 
   747 (*check the next rule*)
   748   val (r, (t, asm)) :: _ = loc revsets t r;
   749 if term2str t = "(3 ^^^ 2 - x ^^^ 2) / (3 ^^^ 2 + 6 * x + x ^^^ 2)" then ()
   750 else error "rational.sml locate_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2) II";
   751 
   752 (*find and check the next rules, rewrite*)
   753   val SOME r = nex revsets t;
   754   val (r,(t,asm))::_ = loc revsets t r;
   755 if term2str t = "(3 ^^^ 2 - x ^^^ 2) / (3 ^^^ 2 + 2 * 3 * x + x ^^^ 2)" then ()
   756 else error "rational.sml locate_rule II";
   757 
   758   val SOME r = nex revsets t;
   759   val (r,(t,asm))::_ = loc revsets t r;
   760 if term2str t = "(3 - x) * (3 + x) / (3 ^^^ 2 + 2 * 3 * x + x ^^^ 2)" then ()
   761 else error "rational.sml next_rule II";
   762 
   763   val SOME r = nex revsets t;
   764   val (r,(t,asm))::_ = loc revsets t r;
   765 if term2str t = "(3 - x) * (3 + x) / ((3 + x) * (3 + x))" then ()
   766 else error "rational.sml next_rule III";
   767 
   768   val SOME r = nex revsets t;
   769   val (r, (t, asm)) :: _ = loc revsets t r;
   770   val ss = term2str t;
   771 if ss = "(3 - x) / (3 + x)" andalso terms2str asm = "[\"3 + x ~= 0\"]" then ()
   772 else error "rational.sml: new behav. in rev-set cancel";
   773 \--------------------------------------------------------------------------------------/*)
   774 
   775 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   776 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   777 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   778 (*WN130909: the example below shows, why "reverse rewriting" only worked for
   779   special cases.*)
   780 
   781 (*the term for which reverse rewriting is demonstrated*)
   782 val t = str2term "(9 + (-1)*x^^^2) / (9 + ((-6)*x + x^^^2))";
   783 val Rrls {scr=Rfuns {init_state=ini,locate_rule=loc,
   784 		       next_rule=nex,normal_form=nor,...},...} = cancel_p;
   785 
   786 (*normal_form produces the result in ONE step*)
   787 val SOME (t',_) = nor t; 
   788 term2str t' = "(3 + 1 * x) / (3 + -1 * x)";
   789 
   790 (*initialize the interpreter state used by the 'me'*)
   791 val SOME (t', asm) = cancel_p_ thy t;
   792 term2str t' = "(3 + x) / (3 + -1 * x)" (*true*);
   793 terms2str asm = "[\"3 + -1 * x ~= 0\"]" (*true*);
   794 val (t,_,revsets,_) = ini t;
   795 
   796 (* WN.10.10.02: dieser Fall terminiert nicht 
   797            (make_polynomial enth"alt zu viele rules)
   798 WN060823 'init_state' requires rewriting on specified location in the term
   799 default_print_depth 99; Rfuns; default_print_depth 3;
   800 WN060831 cycling "sym_order_mult_rls_" "sym_mult_assoc"
   801          as was with make_polynomial before ?!?*)
   802 
   803 val SOME r = nex revsets t;
   804 eq_Thm (r, Thm ("sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))", 
   805 		mk_thm thy "9 = 3 ^^^ 2"));
   806 (*WN060831 *** id_of_thm
   807            Exception- ERROR raised ...
   808 val (r,(t,asm))::_ = loc revsets t r;
   809 term2str t;
   810 
   811   val SOME r = nex revsets t;
   812   val (r,(t,asm))::_ = loc revsets t r;
   813   term2str t;
   814 *)                    
   815 
   816 "-------- examples: rls norm_Rational ----------------------------------------";
   817 "-------- examples: rls norm_Rational ----------------------------------------";
   818 "-------- examples: rls norm_Rational ----------------------------------------";
   819 val t = str2term "(3*x+5)/18 - x/2  - -(3*x - 2)/9 = 0";
   820 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   821 if term2str t' = "1 / 18 = 0" then () else error "rational.sml 1";
   822 
   823 val t = str2term "(17*x - 51)/9 - (-(13*x - 3)/6) + 11 - (9*x - 7)/4 = 0";
   824 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   825 if term2str t' = "(237 + 65 * x) / 36 = 0" then () 
   826 else error "rational.sml 2";
   827 
   828 val t = str2term "(1/2 + (5*x)/2)^^^2 - ((13*x)/2 - 5/2)^^^2 - (6*x)^^^2 + 29";
   829 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   830 if term2str t' = "23 + 35 * x + -72 * x ^^^ 2" then ()
   831 else error "rational.sml 3";
   832 
   833 (*trace_rewrite:=true;*)
   834 val t = str2term "Not (6*x is_atom)";
   835 val SOME (t',_) = rewrite_set_ thy false powers_erls t; term2str t';
   836 "HOL.True";
   837 val t = str2term "1 < 2";
   838 val SOME (t',_) = rewrite_set_ thy false powers_erls t; term2str t';
   839 "HOL.True";
   840 
   841 val t = str2term "(6*x)^^^2";
   842 val SOME (t',_) = rewrite_ thy dummy_ord powers_erls false 
   843 			   (num_str @{thm realpow_def_atom}) t;
   844 if term2str t' = "6 * x * (6 * x) ^^^ (2 + -1)" then ()
   845 else error "rational.sml powers_erls (6*x)^^^2";
   846 
   847 val t = str2term "-1 * (-2 * (5 / 2 * (13 * x / 2)))";
   848 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   849 if term2str t' = "65 * x / 2" then () else error "rational.sml 4";
   850 
   851 val t = str2term "1 - ((13*x)/2 - 5/2)^^^2";
   852 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   853 if term2str t' = "(-21 + 130 * x + -169 * x ^^^ 2) / 4" then () 
   854 else error "rational.sml 5";
   855 
   856 (*SRAM Schalk I, p.92 Nr. 609a*)
   857 val t = str2term "2*(3 - x/5)/3 - 4*(1 - x/3) - x/3 - 2*(x/2 - 1/4)/27 +5/54";
   858 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   859 if term2str t' = "(-255 + 112 * x) / 135" then () 
   860 else error "rational.sml 6";
   861 
   862 (*SRAM Schalk I, p.92 Nr. 610c*)
   863 val t = str2term "((x- 1)/(x+1) + 1) / ((x- 1)/(x+1) - (x+1)/(x- 1)) - 2";
   864 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   865 if term2str t' = "(-3 + -1 * x) / 2" then () else error "rational.sml 7";
   866 
   867 (*SRAM Schalk I, p.92 Nr. 476a*)
   868 val t = str2term "(x^^^2/(1 - x^^^2) + 1)/(x/(1 - x) + 1) * (1 + x)";
   869 (*. a/b : c/d translated to a/b * d/c .*)
   870 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   871 if term2str t' = "1" then () else error "rational.sml 8";
   872 
   873 (*Schalk I, p.92 Nr. 472a*)
   874 val t = str2term "((8*x^^^2 - 32*y^^^2)/(2*x + 4*y))/((4*x - 8*y)/(x + y))";
   875 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   876 if term2str t' = "x + y" then () else error "rational.sml p.92 Nr. 472a";
   877 
   878 (*Schalk I, p.70 Nr. 480b: SEE rational.sml --- nonterminating rls norm_Rational ---*)
   879 
   880 (*WN130910 add_fractions_p exception Div raised + history:
   881 ### WN.2.6.03 from rlang.sml 56a 
   882 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)";
   883 val NONE = rewrite_set_ thy false add_fractions_p t;
   884 
   885 THE ERROR ALREADY OCCURS IN THIS PART:
   886 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x)";
   887 val NONE = add_fraction_p_ thy t;
   888 
   889 SEE Test_Some.thy: section {* add_fractions_p downto exception Div raised ===
   890 *)
   891 
   892 "-------- rational numerals --------------------------------------------------";
   893 "-------- rational numerals --------------------------------------------------";
   894 "-------- rational numerals --------------------------------------------------";
   895 (*SRA Schalk I, p.40 Nr. 164b *)
   896 val t = str2term "(47/6 - 76/9 + 13/4)/(35/12)";
   897 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   898 term2str t;
   899 if term2str t = "19 / 21" then ()
   900 else error "rational.sml: diff.behav. in norm_Rational_mg 1";
   901 
   902 (*SRA Schalk I, p.40 Nr. 166a *)
   903 val t = str2term "((5/4)/(4+22/7) + 37/20)*(110/3 - 110/9 * 23/11)";
   904 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   905 term2str t;
   906 if term2str t = "45 / 2" then ()
   907 else error "rational.sml: diff.behav. in norm_Rational_mg 2";
   908 
   909 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   910 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   911 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   912 (* e190c Stefan K.*)
   913 val t = str2term "((1 + 9*a^^^2) * (1 + 3*a)) / ((3*a + 9*a^^^2) * (1 + 3*a))";
   914 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   915 if term2str t = "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
   916 then () else error "rational.sml: diff.behav. in norm_Rational_mg 3";
   917 
   918 (* e192b Stefan K.*)
   919 val t = str2term "(x^^^2 * (7*x + (-1)*y))  /  (y^^^2 * (7*x + (-1)*y))";
   920 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   921 if term2str t = "x ^^^ 2 / y ^^^ 2"
   922 then () else error "rational.sml: diff.behav. in norm_Rational_mg 4";
   923 
   924 (*SRC Schalk I, p.66 Nr. 379c *)
   925 val t = str2term "(a - b)/(b - a)";
   926 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   927 term2str t;
   928 if term2str t = "-1"
   929 then () else error "rational.sml: diff.behav. in norm_Rational_mg 5";
   930 
   931 (*SRC Schalk I, p.66 Nr. 380b *)
   932 val t = str2term "15*(3*x + 3) * (4*x + 9)  /  (12*(2*x + 7) * (5*x + 5))";
   933 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   934 if term2str t = "(27 + 12 * x) / (28 + 8 * x)"
   935 then () else error "rational.sml: diff.behav. in norm_Rational_mg 6";
   936 
   937 (*Schalk I, p.60 Nr. 215c: was not cancelled with Isabelle2002 *)
   938 val t = str2term "(a + b)^^^4 * (x - y)  /  ((x - y)^^^3 * (a + b)^^^2)";
   939 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   940 if term2str t = "(a ^^^ 2 + 2 * a * b + b ^^^ 2) / (x ^^^ 2 + -2 * x * y + y ^^^ 2)"
   941 then () else error "rational.sml: diff.behav. in norm_Rational_mg 7";
   942 
   943 (*SRC Schalk I, p.66 Nr. 381b *)
   944 val t = str2term 
   945 "(4*x^^^2 - 20*x + 25)/(2*x - 5)^^^3";
   946 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   947 if term2str t = "1 / (-5 + 2 * x)"
   948 then () else error "rational.sml: diff.behav. in norm_Rational_mg 9";
   949 
   950 (* e190c Stefan K.*)
   951 val t = str2term "((1 + 9*a^^^2) * (1 + 3*a))  /  ((3*a + 9*a^^^2) * (1 + 3 * a))";
   952 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   953 if term2str t =  "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
   954 then () else error "rational.sml: diff.behav. in norm_Rational_mg 3";
   955 
   956 (* e192b Stefan K.*)
   957 val t = str2term "(x^^^2 * (7*x + (-1)*y))  /  (y^^^2 * (7*x + (-1)*y))";
   958 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   959 if term2str t = "x ^^^ 2 / y ^^^ 2"
   960 then () else error "rational.sml: diff.behav. in norm_Rational_mg 4";
   961 
   962 (*SRC Schalk I, p.66 Nr. 379c *)
   963 val t = str2term "(a - b) / (b - a)";
   964 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   965 if term2str t = "-1"
   966 then () else error "rational.sml: diff.behav. in norm_Rational_mg 5";
   967 
   968 (*SRC Schalk I, p.66 Nr. 380b *)
   969 val t = str2term "15*(3*x + 3) * (4*x + 9)  /  (12*(2*x + 7) * (5*x + 5))";
   970 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   971 if term2str t = "(27 + 12 * x) / (28 + 8 * x)"
   972 then () else error "rational.sml: diff.behav. in norm_Rational_mg 6";
   973 
   974 (*Schalk I, p.60 Nr. 215c *)
   975 val t = str2term "(a + b)^^^4 * (x - y)  /  ((x - y)^^^3 * (a + b)^^^2)";
   976 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   977 if term2str t = "(a ^^^ 2 + 2 * a * b + b ^^^ 2) / (x ^^^ 2 + -2 * x * y + y ^^^ 2)"
   978 then () else error "Schalk I, p.60 Nr. 215c: with Isabelle2002 cancellation incomplete, changed";
   979 
   980 (* extreme example from somewhere *)
   981 val t = str2term 
   982     ("(a^^^4 * x  +  -1*a^^^4 * y  +  4*a^^^3 * b * x  +  -4*a^^^3 * b * y  + " ^
   983       "6*a^^^2 * b^^^2 * x  +  -6*a^^^2 * b^^^2 * y  +  4*a * b^^^3 * x  +  -4*a * b^^^3 * y  + " ^
   984       "b^^^4 * x  +  -1*b^^^4 * y) " ^
   985   " / (a^^^2 * x^^^3  +  -3*a^^^2 * x^^^2 * y  +  3*a^^^2 * x * y^^^2  +  -1*a^^^2 * y^^^3 + " ^
   986       "2*a * b * x^^^3  +  -6*a * b * x^^^2 * y  +  6*a * b * x * y^^^2  +  -2*a * b * y^^^3 + " ^
   987       "b^^^2 * x^^^3  +  -3*b^^^2 * x^^^2 * y  +  3*b^^^2 * x * y^^^2  +  -1*b ^^^ 2 * y ^^^ 3)")
   988 val SOME (t, _) = rewrite_set_ thy false cancel_p t;
   989 if term2str t = "(a ^^^ 2 + 2 * a * b + b ^^^ 2) / (x ^^^ 2 + -2 * x * y + y ^^^ 2)"
   990 then () else error "with Isabelle2002: NONE -- now SOME changed";
   991 
   992 (*Schalk I, p.66 Nr. 381a *)
   993 (* ATTENTION: here the rls is very slow. In Isabelle2002 this required 2 min *)
   994 val t = str2term "18*(a + b)^^^3 * (a - b)^^^2 / (72*(a - b)^^^3 * (a + b)^^^2)";
   995 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   996 if term2str t = "(a + b) / (4 * a + -4 * b)"
   997 then () else error "rational.sml: diff.behav. in norm_Rational_mg 8";
   998 
   999 (*SRC Schalk I, p.66 Nr. 381b *)
  1000 val t = str2term "(4*x^^^2 - 20*x + 25) / (2*x - 5)^^^3";
  1001 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1002 if term2str t = "1 / (-5 + 2 * x)"
  1003 then () else error "rational.sml: diff.behav. in norm_Rational_mg 9";
  1004 
  1005 (*SRC Schalk I, p.66 Nr. 381c *)
  1006 val t = str2term "(27*a^^^3 + 9*a^^^2+3*a+1) / (27*a^^^3 + 18*a^^^2+3*a)";
  1007 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1008 if term2str t = "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
  1009 then () else error "rational.sml: diff.behav. in norm_Rational_mg 10";
  1010 
  1011 (*SRC Schalk I, p.66 Nr. 383a *)
  1012 val t = str2term "(5*a^^^2 - 5*a*b) / (a - b)^^^2";
  1013 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1014 if term2str t = "-5 * a / (-1 * a + b)"
  1015 then () else error "rational.sml: diff.behav. in norm_Rational_mg 11";
  1016 
  1017 "----- NOT TERMINATING ?: worked before 0707xx";
  1018 val t = str2term "(a^^^2 - 1)*(b + 1) / ((b^^^2 - 1)*(a+1))";
  1019 (* WN130911 "exception Div raised" by 
  1020   cancel_p_ thy (str2term ("(-1 + -1 * b + a ^^^ 2 + a ^^^ 2 * b) /" ^
  1021                            "(-1 + -1 * a + b ^^^ 2 + a * b ^^^ 2)"))
  1022 
  1023 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1024 if term2str t = "(1 + -1 * a) / (1 + -1 * b)" then ()
  1025 else error "rational.sml MG tests 3e";
  1026 *)
  1027 
  1028 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1029 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1030 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1031 (*SRA Schalk I, p.67 Nr. 403a *)
  1032 val t = str2term "4/x - 3/y - 1";
  1033 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1034 if term2str t = "(-3 * x + 4 * y + -1 * x * y) / (x * y)"
  1035 then () else error "rational.sml: diff.behav. in norm_Rational_mg 12";
  1036 
  1037 val t = str2term "(2*a+3*b)/(b*c) + (3*c+a)/(a*c) - (2*a^^^2+3*b*c)/(a*b*c)";
  1038 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1039 if term2str t = "4 / c"
  1040 then () else error "rational.sml: diff.behav. in norm_Rational_mg 13";
  1041 
  1042 (*SRA Schalk I, p.67 Nr. 410b *)
  1043 val t = str2term "1/(x+1) + 1/(x+2) - 2/(x+3)";
  1044 (* WN130911 non-termination due to non-termination of
  1045   cancel_p_ thy (str2term "(5 + 3 * x) / (6 + 11 * x + 6 * x ^^^ 2 + x ^^^ 3)")
  1046 
  1047 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1048 if term2str t = "(5 + 3 * x) / (6 + 11 * x + 6 * x ^^^ 2 + x ^^^ 3)"
  1049 then () else error "rational.sml: diff.behav. in norm_Rational_mg 14";
  1050 *)
  1051 
  1052 (*SRA Schalk I, p.67 Nr. 413b *)
  1053 val t = str2term "(1 + x)/(1 - x)  -  (1 - x)/(1 + x)  +  2*x/(1 - x^^^2)";
  1054 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1055 if term2str t = "6 * x / (1 + -1 * x ^^^ 2)"
  1056 then () else error "rational.sml: diff.behav. in norm_Rational_mg 15";
  1057 
  1058 (*SRA Schalk I, p.68 Nr. 414a *)
  1059 val t = str2term "(x + 2)/(x - 1)  +  (x - 3)/(x - 2)  -  (x + 1)/((x - 1)*(x - 2))";
  1060 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1061 if term2str t ="(-2 + -5 * x + 2 * x ^^^ 2) / (2 + -3 * x + x ^^^ 2)"
  1062 then () else error "rational.sml: diff.behav. in norm_Rational_mg 16";
  1063 
  1064 (*SRA Schalk I, p.68 Nr. 428b *)
  1065 val t = str2term 
  1066   "1/(a - b)^^^2  +  1/(a + b)^^^2  -  2/(a^^^2 - b^^^2)  -  4*(b^^^2 - 1)/(a^^^2 - b^^^2)^^^2";
  1067 (* WN130911 non-termination due to non-termination of
  1068   cancel_p_ thy (str2term "(4 + -4 * b ^^^ 2) / (a ^^^ 4 + -2 * (a ^^^ 2 * b ^^^ 2) + b ^^^ 4)")
  1069 
  1070 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1071 if term2str t = "4 / (a ^^^ 4 + -2 * a ^^^ 2 * b ^^^ 2 + b ^^^ 4)"
  1072 then () else error "rational.sml: diff.behav. in norm_Rational_mg 18";
  1073 *)
  1074 
  1075 (*SRA Schalk I, p.68 Nr. 430b *)
  1076 val t = str2term 
  1077   "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";
  1078 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1079 if term2str t = "a + 3 * b"
  1080 then () else error "rational.sml: diff.behav. in norm_Rational_mg 19";
  1081 
  1082 (*SRA Schalk I, p.68 Nr. 432 *)
  1083 val t = str2term 
  1084   ("(a^^^2 + a*b) / (a^^^2 - b^^^2)  -  (b^^^2 - a*b) / (b^^^2 - a^^^2)  +  " ^
  1085   "a^^^2*(a - b) / (a^^^3 - a^^^2*b)  -  2*a*(a^^^2 - b^^^2) / (a^^^3 - a*b^^^2)  -  " ^
  1086   "2*b^^^2 / (a^^^2 - b^^^2)");
  1087 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1088 term2str t;
  1089 if term2str t = "0"
  1090 then () else error "rational.sml: diff.behav. in norm_Rational_mg 20";
  1091 
  1092 (* some example *)
  1093 val t = str2term "3*a / (a*b)  +  x/y";
  1094 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1095 if term2str t = "(3 * y + b * x) / (b * y)"
  1096 then () else error "rational.sml: diff.behav. in norm_Rational_mg 21";
  1097 
  1098 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1099 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1100 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1101 (*------- SRM Schalk I, p.68 Nr. 436a *)
  1102 val t = str2term "3*(x+y) / (15*(x - y))  *   25*(x - y)^^^2 / (18*(x + y)^^^2)";
  1103 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1104 if term2str t = "(-5 * x + 5 * y) / (-18 * x + -18 * y)"
  1105 then () else error "rational.sml: diff.behav. in norm_Rational_mg 22";
  1106 
  1107 (*------- SRM.test Schalk I, p.68 Nr. 436b *)
  1108 val t = str2term "5*a*(a - b)^^^2*(a + b)^^^3/(7*b*(a - b)^^^3) * 7*b/(a + b)^^^3";
  1109 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1110 if term2str t = "5 * a / (a + -1 * b)"
  1111 then () else error "rational.sml: diff.behav. in norm_Rational_mg 23";
  1112 
  1113 (*------- Schalk I, p.68 Nr. 437a *)
  1114 val t = str2term "(3*a - 4*b) / (4*c+3*e)  *  (3*a+4*b)/(9*a^^^2 - 16*b^^^2)";
  1115 (* raises an exception for unclear reasons:
  1116 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1117 :
  1118 ###  rls: cancel_p on: (9 * a ^^^ 2 + -16 * b ^^^ 2) / (4 * c + 3 * e) /
  1119 (9 * a ^^^ 2 + -16 * b ^^^ 2) 
  1120 exception Div raised
  1121 
  1122 BUT
  1123 val t = str2term 
  1124   ("(9 * a ^^^ 2 + -16 * b ^^^ 2) / (4 * c + 3 * e) /" ^
  1125   "(9 * a ^^^ 2 + -16 * b ^^^ 2)");
  1126 NONE = cancel_p_ thy t;
  1127 
  1128 if term2str t = "1 / (4 * c + 3 * e)" then ()
  1129 else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1130 *)
  1131 
  1132 "----- S.K. corrected non-termination 060904";
  1133 val t = str2term "(3*a - 4*b) * (3*a+4*b)/((4*c+3*e)*(9*a^^^2 - 16*b^^^2))";
  1134 val SOME (t, _) = rewrite_set_ thy false make_polynomial t;
  1135 if term2str t = 
  1136   "(9 * a ^^^ 2 + -16 * b ^^^ 2) /\n(36 * a ^^^ 2 * c + 27 * a ^^^ 2 * e + -64 * b ^^^ 2 * c +\n -48 * b ^^^ 2 * e)"
  1137 (*"(9 * a ^^^ 2 + -16 * b ^^^ 2) / (36 * a ^^^ 2 * c + 27 * a ^^^ 2 * e + -64 * b ^^^ 2 * c + -48 * b ^^^ 2 * e)"*)
  1138 then () else error "rational.sml: S.K.8..corrected 060904-6";
  1139 
  1140 "----- S.K. corrected non-termination of cancel_p_";
  1141 val t'' = str2term ("(9 * a ^^^ 2 + -16 * b ^^^ 2) /" ^
  1142   "(36 * a^^^2 * c + (27 * a^^^2 * e + (-64 * b^^^2 * c + -48 * b^^^2 * e)))");
  1143 val SOME (t',_) = rewrite_set_ thy false cancel_p t'';
  1144 if term2str t' = "1 / (4 * c + 3 * e)"
  1145 then () else error "rational.sml: diff.behav. in cancel_p S.K.8";
  1146 
  1147 (*------- Schalk I, p.68 Nr. 437b*)
  1148 val t = str2term "(a + b)/(x^^^2 - y^^^2) * ((x - y)^^^2/(a^^^2 - b^^^2))";
  1149 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1150 :
  1151 ####  rls: cancel_p on: (a * x ^^^ 2 + -2 * (a * (x * y)) + a * y ^^^ 2 + b * x ^^^ 2 +
  1152  -2 * (b * (x * y)) +
  1153  b * y ^^^ 2) /
  1154 (a ^^^ 2 * x ^^^ 2 + -1 * (a ^^^ 2 * y ^^^ 2) + -1 * (b ^^^ 2 * x ^^^ 2) +
  1155  b ^^^ 2 * y ^^^ 2) 
  1156 exception Div raised
  1157 *)
  1158 
  1159 (*------- SRM Schalk I, p.68 Nr. 438a *)
  1160 val t = str2term "x*y / (x*y - y^^^2)  *  (x^^^2 - x*y)";
  1161 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1162 if term2str t = "x ^^^ 2"
  1163 then () else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1164 
  1165 (*------- SRM Schalk I, p.68 Nr. 439b *)
  1166 val t = str2term "(4*x^^^2 + 4*x + 1)  *  ((x^^^2 - 2*x^^^3) / (4*x^^^2 + 2*x))";
  1167 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1168 if term2str t = "(x + -4 * x ^^^ 3) / 2"
  1169 then () else error "rational.sml: diff.behav. in norm_Rational_mg 25";
  1170 
  1171 (*------- SRM Schalk I, p.68 Nr. 440a *)
  1172 val t = str2term "(x^^^2 - 2*x) / (x^^^2 - 3*x)  *  (x - 3)^^^2 / (x^^^2 - 4)";
  1173 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1174 if term2str t = "(-3 + x) / (2 + x)"
  1175 then () else error "rational.sml: diff.behav. in norm_Rational_mg 26";
  1176 
  1177 "----- Schalk I, p.68 Nr. 440b SK11 works since 0707xx";
  1178 val t = str2term "(a^^^3 - 9*a) / (a^^^3*b - a*b^^^3)  *  (a^^^2*b + a*b^^^2) / (a+3)";
  1179 (* WN130911 non-termination for unclear reasons:
  1180 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1181 
  1182 ... ENDS WITH THIS TRACE:
  1183 :
  1184 ###  rls: cancel_p on: (-9 * (a ^^^ 3 * b) + -9 * (a ^^^ 2 * b ^^^ 2) + a ^^^ 5 * b +
  1185  a ^^^ 4 * b ^^^ 2) /
  1186 (a ^^^ 3 * b + -1 * (a * b ^^^ 3)) /
  1187 (3 + a)
  1188 BUT THIS IS CORRECTLY RECOGNISED 
  1189 val t = str2term 
  1190   ("(-9 * (a^^^3 * b) + -9 * (a^^^2 * b^^^2) + a^^^5 * b + a^^^4 * b^^^2)  /" ^
  1191   "(a^^^3 * b + -1 * (a * b^^^3))  /  (3 + (a::real))");
  1192 AS
  1193 NONE = cancel_p_ thy t;
  1194 
  1195 if term2str t = "(-3 * a + a ^^^ 2) / (a + -1 * b)" then ()
  1196 else error "rational.sml: diff.behav. in norm_Rational 27";
  1197 *)
  1198 
  1199 "----- SK12 works since 0707xx";
  1200 val t = str2term "(a^^^3 - 9*a) * (a^^^2*b+a*b^^^2)  /  ((a^^^3*b - a*b^^^3) * (a+3))";
  1201 (* WN130911 non-termination due to non-termination of
  1202   cancel_p_ thy (str2term "(4 + -4 * b ^^^ 2) / (a ^^^ 4 + -2 * (a ^^^ 2 * b ^^^ 2) + b ^^^ 4)")
  1203 
  1204 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1205 if term2str t' = "(-3 * a + a ^^^ 2) / (a + -1 * b)" then ()
  1206 else error "rational.sml: diff.behav. in norm_Rational 28";
  1207 *)
  1208 
  1209 "-------- examples common denominator and multiplication from: Schalk --------";
  1210 "-------- examples common denominator and multiplication from: Schalk --------";
  1211 "-------- examples common denominator and multiplication from: Schalk --------";
  1212 (*------- SRAM Schalk I, p.69 Nr. 441b *)
  1213 val t = str2term "(4*a/3 + 3*b^^^2/a^^^3 + b/(4*a))*(4*b/(3*a))";
  1214 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1215 if term2str t = "(36 * b ^^^ 3 + 3 * a ^^^ 2 * b ^^^ 2 + 16 * a ^^^ 4 * b) / (9 * a ^^^ 4)"
  1216 then () else error "rational.sml: diff.behav. in norm_Rational_mg 28";
  1217 
  1218 (*------- SRAM Schalk I, p.69 Nr. 442b *)
  1219 val t = str2term ("(15*a^^^2/x^^^3 - 5*b^^^4/x^^^2 + 25*c^^^2/x) * " ^
  1220   "(x^^^3/(5*a*b^^^3*c^^^3)) + 1/c^^^3 * (b*x/a - 3*a/b^^^3)");
  1221 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1222 if term2str t = "5 * x ^^^ 2 / (a * b ^^^ 3 * c)"
  1223 then () else error "rational.sml: diff.behav. in norm_Rational_mg 29";
  1224 
  1225 (*------- SRAM Schalk I, p.69 Nr. 443b *)
  1226 val t = str2term "(a/2 + b/3) * (b/3 - a/2)";
  1227 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1228 if term2str t = "(-9 * a ^^^ 2 + 4 * b ^^^ 2) / 36"
  1229 then () else error "rational.sml: diff.behav. in norm_Rational_mg 30";
  1230 
  1231 (*------- SRAM Schalk I, p.69 Nr. 445b *)
  1232 val t = str2term "(a^^^2/9 + 2*a/(3*b) + 4/b^^^2)*(a/3 - 2/b) + 8/b^^^3";
  1233 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1234 if term2str t = "a ^^^ 3 / 27"
  1235 then () else error "rational.sml: diff.behav. in norm_Rational_mg 31";
  1236 
  1237 (*------- SRAM Schalk I, p.69 Nr. 446b *)
  1238 val t = str2term "(x/(5*x + 4*y) - y/(5*x - 4*y) + 1)*(25*x^^^2 - 16*y^^^2)";
  1239 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1240 if term2str t = "30 * x ^^^ 2 + -9 * x * y + -20 * y ^^^ 2"
  1241 then () else error "rational.sml: diff.behav. in norm_Rational_mg 32";
  1242 
  1243 (*------- SRAM Schalk I, p.69 Nr. 449a *)(*Achtung: rechnet ca 8 Sekunden*)
  1244 val t = str2term 
  1245 "(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)";
  1246 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1247 if term2str t = "(-81 * x ^^^ 4 + 16 * x ^^^ 8 * y ^^^ 4) / (81 * y ^^^ 8)"
  1248 then () else error "rational.sml: diff.behav. in norm_Rational_mg 33";
  1249 
  1250 (*------- SRAM Schalk I, p.69 Nr. 450a *)
  1251 val t = str2term 
  1252 "(4*x/(3*y)+2*y/(3*x))^^^2 - (2*y/(3*x) - 2*x/y)*(2*y/(3*x)+2*x/y)";
  1253 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1254 if term2str t = "(52 * x ^^^ 2 + 16 * y ^^^ 2) / (9 * y ^^^ 2)"
  1255 then () else error "rational.sml: diff.behav. in norm_Rational_mg 34";
  1256 
  1257 (*------- SRAM Schalk I, p.69 Nr. 442b --- abgewandelt*)
  1258 val t = str2term 
  1259   ("(15*a^^^4/(a*x^^^3)  -  5*a*((b^^^4 - 5*c^^^2*x) / x^^^2))  *  " ^
  1260   "(x^^^3/(5*a*b^^^3*c^^^3))   +   a/c^^^3 * (x*(b/a) - 3*b*(a/b^^^4))");
  1261 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1262 if term2str t = "5 * x ^^^ 2 / (b ^^^ 3 * c)"
  1263 then () else error "rational.sml: diff.behav. in norm_Rational_mg 53";
  1264 
  1265 
  1266 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1267 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1268 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1269 "----- SRD Schalk I, p.69 Nr. 454b";
  1270 val t = str2term "((2 - x)/(2*a)) / (2*a/(x - 2))";
  1271 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1272 if term2str t = "(-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2)"
  1273 then () else error "rational.sml: diff.behav. in norm_Rational_mg 35";
  1274 
  1275 "----- SRD Schalk I, p.69 Nr. 455a";
  1276 val t = str2term "(a^^^2 + 1)/(a^^^2 - 1) / ((a+1)/(a - 1))";
  1277 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1278 if term2str t = "(1 + a ^^^ 2) / (1 + 2 * a + a ^^^ 2)" then ()
  1279 else error "rational.sml: diff.behav. in norm_Rational_mg 36";
  1280 
  1281 "----- Schalk I, p.69 Nr. 455b";
  1282 val t = str2term "(x^^^2 - 4)/(y^^^2 - 9)/((2+x)/(3 - y))";
  1283 (* WN130911 non-termination due to non-termination of
  1284   cancel_p_ thy (str2term ("(-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /" ^
  1285                            "(-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)"))
  1286 
  1287 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1288 if term2str t = "(2 + -1 * x) / (3 + y)" then ()
  1289 else error "rational.sml: diff.behav. in norm_Rational_mg 37";
  1290 *)
  1291 
  1292 "----- SK060904-1a non-termination of cancel_p_ ?: worked before 0707xx";
  1293 val t = str2term "(x^^^2 - 4)*(3 - y) / ((y^^^2 - 9)*(2+x))";
  1294 (* WN130911 non-termination due to non-termination of
  1295   cancel_p_ thy (str2term ("(-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /" ^
  1296                            "(-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)"))
  1297 
  1298 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1299 if term2str t = "(2 + -1 * x) / (3 + y)" then ()
  1300 else error "rational.sml: diff.behav. in norm_Rational_mg 37b";
  1301 *)
  1302 
  1303 "----- ?: worked before 0707xx";
  1304 val t = str2term "(3 + -1 * y) / (-9 + y ^^^ 2)";
  1305 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1306 if term2str t = "-1 / (3 + y)"
  1307 then () else error "rational.sml: -1 / (3 + y) norm_Rational";
  1308 
  1309 "----- SRD Schalk I, p.69 Nr. 456b";
  1310 val t = str2term "(b^^^3 - b^^^2) / (b^^^2+b) / (b^^^2 - 1)";
  1311 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1312 if term2str t = "b / (1 + 2 * b + b ^^^ 2)"
  1313 then () else error "rational.sml: diff.behav. in norm_Rational_mg 38";
  1314 
  1315 "----- SRD Schalk I, p.69 Nr. 457b";
  1316 val t = str2term "(16*a^^^2 - 9*b^^^2)/(2*a+3*a*b) / ((4*a+3*b)/(4*a^^^2 - 9*a^^^2*b^^^2))";
  1317 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1318 if term2str t = "8 * a ^^^ 2 + -6 * a * b + -12 * a ^^^ 2 * b + 9 * a * b ^^^ 2"
  1319 then () else error "rational.sml: diff.behav. in norm_Rational_mg 39";
  1320 
  1321 "----- Schalk I, p.69 Nr. 458b works since 0707";
  1322 val t = str2term "(2*a^^^2*x - a^^^2) / (a*x - b*x) / (b^^^2*(2*x - 1) / (x*(a - b)))";
  1323 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1324 :
  1325 ###  rls: cancel_p on: (-1 * a ^^^ 2 + 2 * (a ^^^ 2 * x)) / (a * x + -1 * (b * x)) /
  1326 ((-1 * b ^^^ 2 + 2 * (b ^^^ 2 * x)) / (a * x + -1 * (b * x))) 
  1327 exception Div raised
  1328 
  1329 BUT
  1330 val t = str2term 
  1331   ("(-1 * a ^^^ 2 + 2 * (a ^^^ 2 * x)) / (a * x + -1 * (b * x)) /" ^
  1332   "((-1 * b ^^^ 2 + 2 * (b ^^^ 2 * x)) / (a * x + -1 * (b * x)))");
  1333 NONE = cancel_p_ thy t;
  1334 
  1335 if term2str t = "a ^^^ 2 / b ^^^ 2" then ()
  1336 else error "rational.sml: diff.behav. in norm_Rational_mg 39b";
  1337 *)
  1338 
  1339 "----- SRD Schalk I, p.69 Nr. 459b";
  1340 val t = str2term "(a^^^2 - b^^^2)/(a*b) / (4*(a+b)^^^2/a)";
  1341 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1342 if term2str t = "(a + -1 * b) / (4 * a * b + 4 * b ^^^ 2)" then ()
  1343 else error "rational.sml: diff.behav. in norm_Rational_mg 41";
  1344 
  1345 "----- Schalk I, p.69 Nr. 460b nonterm.SK";
  1346 val t = str2term "(9*(x^^^2 - 8*x + 16) / (4*(y^^^2 - 2*y + 1))) / ((3*x - 12) / (16*y - 16))";
  1347 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1348 exception Div raised
  1349 
  1350 BUT
  1351 val t = str2term 
  1352   ("(144 + -72 * x + 9 * x ^^^ 2) / (4 + -8 * y + 4 * y ^^^ 2) /" ^
  1353   "((-12 + 3 * x) / (-16 + 16 * y))");
  1354 NONE = cancel_p_ thy t;
  1355 
  1356 if term2str t = !!!!!!!!!!!!!!!!!!!!!!!!!
  1357 then () else error "rational.sml: diff.behav. in norm_Rational_mg 42";
  1358 *)
  1359 
  1360 "----- some variant of the above; was non-terminating before";
  1361 val t = str2term "9*(x^^^2 - 8*x+16)*(16*y - 16)/(4*(y^^^2 - 2*y+1)*(3*x - 12))";
  1362 val SOME (t , _) = rewrite_set_ thy false norm_Rational t;
  1363 if term2str t = "(48 + -12 * x) / (1 + -1 * y)"
  1364 then () else error "some variant of the above; was non-terminating before";
  1365 
  1366 "----- SRD Schalk I, p.70 Nr. 472a";
  1367 val t = str2term ("((8*x^^^2 - 32*y^^^2) / (2*x + 4*y))  /  ((4*x - 8*y) / (x + y))");
  1368 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1369 if term2str t = "x + y"
  1370 then () else error "rational.sml: diff.behav. in norm_Rational_mg 43";
  1371 
  1372 "----- Schalk I, p.70 Nr. 478b ----- Rechenzeit: 5 sec";
  1373 val t = str2term ("(a - (a*b + b^^^2)/(a+b))/(b+(a - b)/(1+(a+b)/(a - b))) / " ^
  1374 		 "((a - a^^^2/(a+b))/(a+(a*b)/(a - b)))");
  1375 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1376 if term2str t = "(2 * a ^^^ 3 + 2 * a ^^^ 2 * b) / (a ^^^ 2 * b + b ^^^ 3)"
  1377 then () else error "rational.sml: diff.behav. in norm_Rational_mg 51";
  1378 
  1379 (*SRD Schalk I, p.69 Nr. 461a *)
  1380 val t = str2term "(2/(x+3) + 2/(x - 3)) / (8*x/(x^^^2 - 9))";
  1381 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1382 if term2str t = "1 / 2"
  1383 then () else error "rational.sml: diff.behav. in norm_Rational_mg 44";
  1384 
  1385 (*SRD Schalk I, p.69 Nr. 464b *)
  1386 val t = str2term "(a - a/(a - 2)) / (a + a/(a - 2))";
  1387 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1388 if term2str t = "(-3 + a) / (-1 + a)"
  1389 then () else error "rational.sml: diff.behav. in norm_Rational_mg 45";
  1390 
  1391 (*SRD Schalk I, p.69 Nr. 465b *)
  1392 val t = str2term "((x+3*y)/9 + (4*y^^^2 - 9*z^^^2)/(16*x))   /   (x/9 + y/6 + z/4)";
  1393 (* WN130911 non-termination due to non-termination of
  1394   cancel_p_ thy (str2term 
  1395     ("("(576 * x ^^^ 2 + 1728 * (x * y) + 1296 * y ^^^ 2 + -2916 * z ^^^ 2) /" ^
  1396       "(576 * x ^^^ 2 + 864 * (x * y) + 1296 * (x * z))"))
  1397 
  1398 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1399 if term2str t = "(4 * x + 6 * y + -9 * z) / (4 * x)"
  1400 then () else error "rational.sml: diff.behav. in norm_Rational_mg 46";
  1401 *)
  1402 
  1403 (*SRD Schalk I, p.69 Nr. 466b *)
  1404 val t = str2term "((1 - 7*(x - 2)/(x^^^2 - 4)) / (6/(x+2))) / (3/(x+5)+30/(x^^^2 - 25))";
  1405 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1406 if term2str t = "(25 + -10 * x + x ^^^ 2) / 18"
  1407 then () else error "rational.sml: diff.behav. in norm_Rational_mg 47";
  1408 
  1409 (*SRD Schalk I, p.70 Nr. 469 *)
  1410 val t = str2term ("3*b^^^2 / (4*a^^^2 - 8*a*b + 4*b^^^2) / " ^
  1411   "(a / (a^^^2*b - b^^^3)  +  (a - b) / (4*a*b^^^2 + 4*b^^^3)  -  1 / (4*b^^^2))");
  1412 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1413 if term2str t = "-3 * b ^^^ 3 / (-2 * a + 2 * b)"
  1414 then () else error "rational.sml: diff.behav. in norm_Rational_mg 48";
  1415 
  1416 "-------- me Schalk I No.186 -------------------------------------------------";
  1417 "-------- me Schalk I No.186 -------------------------------------------------";
  1418 "-------- me Schalk I No.186 -------------------------------------------------";
  1419 val fmz = ["Term ((14 * x * y) / ( x * y ))", "normalform N"];
  1420 val (dI',pI',mI') =
  1421   ("Rational",["rational","simplification"],
  1422    ["simplification","of_rationals"]);
  1423 val p = e_pos'; val c = []; 
  1424 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
  1425 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1426 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1427 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1428 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1429 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1430 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1431 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1432 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
  1433 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
  1434 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;(*++ for explicit script*)
  1435 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;(*++ for explicit script*)
  1436 case (f2str f, nxt) of
  1437     ("14", ("End_Proof'", _)) => ()
  1438   | _ => error "rational.sml diff.behav. in me Schalk I No.186";
  1439 
  1440 "-------- interSteps ..Simp_Rat_Double_No-1.xml ------------------------------";
  1441 "-------- interSteps ..Simp_Rat_Double_No-1.xml ------------------------------";
  1442 "-------- interSteps ..Simp_Rat_Double_No-1.xml ------------------------------";
  1443 reset_states ();
  1444 CalcTree [(["Term (((2 - x)/(2*a)) / (2*a/(x - 2)))", "normalform N"], 
  1445   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1446 Iterator 1;
  1447 moveActiveRoot 1;
  1448 autoCalculate 1 CompleteCalc;
  1449 val ((pt, p), _) = get_calc 1; 
  1450 (*
  1451 show_pt pt;
  1452 [
  1453 (([], Frm), Simplify ((2 - x) / (2 * a) / (2 * a / (x - 2)))),
  1454 (([1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1455 (([1], Res), (2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1456 (([2], Res), (2 + -1 * x) / (2 * a) / (2 * a / (-2 + x))),
  1457 (([3], Res), (2 + -1 * x) * (-2 + x) / (2 * a * (2 * a))),
  1458 (([4], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2)),
  1459 (([], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2))] 
  1460 *)
  1461 interSteps 1 ([1], Res);
  1462 val ((pt, p), _) = get_calc 1; 
  1463 (*show_pt pt;
  1464 [
  1465 (([], Frm), Simplify ((2 - x) / (2 * a) / (2 * a / (x - 2)))),
  1466 (([1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1467 (([1,1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1468 (([1,1], Res), (2 - x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1469 (([1,2], Res), (2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1470 (([1], Res), (2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1471 (([2], Res), (2 + -1 * x) / (2 * a) / (2 * a / (-2 + x))),
  1472 (([3], Res), (2 + -1 * x) * (-2 + x) / (2 * a * (2 * a))),
  1473 (([4], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2)),
  1474 (([], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2))] 
  1475 *)
  1476 val (t, asm) = get_obj g_result pt [1, 1];
  1477 if term2str t = "(2 - x) / (2 * a) / (2 * a / (x + -1 * 2))" andalso terms2str asm = "[]"
  1478 then () else error "2nd interSteps ..Simp_Rat_Double_No-1 changed on [1, 1]";
  1479 val (t, asm) = get_obj g_result pt [1, 2];
  1480 if term2str t = "(2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))" andalso terms2str asm = "[]"
  1481 then () else error "3rd interSteps ..Simp_Rat_Double_No-1 changed on [1, 2]";
  1482 
  1483 
  1484 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ------------------------------";
  1485 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ------------------------------";
  1486 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ------------------------------";
  1487 reset_states ();
  1488 CalcTree [(["Term ((a^2 + -1*b^2) / (a^2 + -2*a*b + b^2))", "normalform N"], 
  1489   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1490 Iterator 1;
  1491 moveActiveRoot 1;
  1492 autoCalculate 1 CompleteCalc;
  1493 val ((pt, p), _) = get_calc 1;
  1494 (*show_pt pt;
  1495 [
  1496 (([], Frm), Simplify ((a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2))),
  1497 (([1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2)),
  1498 (([1], Res), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1499 (([2], Res), (a + b) / (a + -1 * b)),
  1500 (([], Res), (a + b) / (a + -1 * b))] 
  1501 *)
  1502 interSteps 1 ([2], Res);
  1503 val ((pt, p), _) = get_calc 1;
  1504 (*show_pt pt;
  1505 [
  1506 (([], Frm), Simplify ((a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2))),
  1507 (([1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2)),
  1508 (([1], Res), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1509 (([2,1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1510 (([2,1], Res), (a + b) / (a + -1 * b)),
  1511 (([2], Res), (a + b) / (a + -1 * b)),
  1512 (([], Res), (a + b) / (a + -1 * b))] 
  1513 *)
  1514 interSteps 1 ([2,1],Res);
  1515 val ((pt, p), _) = get_calc 1; 
  1516 (*show_pt pt;
  1517 [
  1518 (([], Frm), Simplify ((a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2))),
  1519 (([1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2)),
  1520 (([1], Res), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1521 (([2,1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1522 (([2,1,1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1523 (([2,1,1], Res), (a ^^^ 2 + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1524 (a ^^^ 2 + -2 * (a * b) + 1 * b ^^^ 2)),
  1525 (([2,1,2], Res), (a ^^^ 2 + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1526 (a ^^^ 2 + -2 * (a * b) + -1 ^^^ 2 * b ^^^ 2)),
  1527 (([2,1,3], Res), (a ^^^ 2 + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1528 (a ^^^ 2 + -2 * (a * b) + (-1 * b) ^^^ 2)),
  1529 (([2,1,4], Res), (a * a + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1530 (a ^^^ 2 + -2 * (a * b) + (-1 * b) ^^^ 2)),
  1531 (([2,1,5], Res), (a * a + -1 * (a * b) + a * b + -1 * (b * b)) /
  1532 (a ^^^ 2 + -2 * (a * b) + (-1 * b) ^^^ 2)),
  1533 (([2,1,6], Res), (a * a + -1 * (a * b) + a * b + -1 * (b * b)) /
  1534 (a ^^^ 2 + -1 * (2 * (a * b)) + (-1 * b) ^^^ 2)),
  1535 (([2,1,7], Res), (a * a + a * (-1 * b) + (b * a + b * (-1 * b))) /
  1536 (a ^^^ 2 + 2 * (a * (-1 * b)) + (-1 * b) ^^^ 2)),
  1537 (([2,1,8], Res), (a * a + a * (-1 * b) + (b * a + b * (-1 * b))) /
  1538 (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1539 (([2,1,9], Res), (a * (a + -1 * b) + (b * a + b * (-1 * b))) /
  1540 (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1541 (([2,1,10], Res), (a * (a + -1 * b) + b * (a + -1 * b)) /
  1542 (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1543 (([2,1,11], Res), (a + b) * (a + -1 * b) / (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1544 (([2,1,12], Res), (a + b) * (a + -1 * b) / ((a + -1 * b) * (a + -1 * b))),
  1545 (([2,1,13], Res), (a + b) / (a + -1 * b)),
  1546 (([2,1], Res), (a + b) / (a + -1 * b)),
  1547 (([2], Res), (a + b) / (a + -1 * b)),
  1548 (([], Res), (a + b) / (a + -1 * b))] 
  1549 *)
  1550 val newnds = children (get_nd pt [2,1]) (*see "fun detailrls"*);
  1551 if length newnds = 13 then () else error "rational.sml: interSteps cancel_p rev_rew_p";
  1552 
  1553 val p = ([2,1,9],Res);
  1554 getTactic 1 p;
  1555 val (_, tac, _) = pt_extract (pt, p);
  1556 case tac of SOME (Rewrite ("sym_distrib_left", _)) => ()
  1557 | _ => error "rational.sml: getTactic, sym_real_plus_binom_times1";
  1558 
  1559 
  1560 "-------- investigate rulesets for cancel_p ----------------------------------";
  1561 "-------- investigate rulesets for cancel_p ----------------------------------";
  1562 "-------- investigate rulesets for cancel_p ----------------------------------";
  1563 val thy = @{theory "Rational"};
  1564 val t = str2term "(a^^^2 + -1*b^^^2) / (a^^^2 + -2*a*b + b^^^2)";
  1565 val tt = str2term "(1 * a + 1 * b) * (1 * a + -1 * b)"(*numerator only*);
  1566 
  1567 "----- with rewrite_set_";
  1568 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1569 if term2str tt'= "a ^^^ 2 + -1 * b ^^^ 2" then () else error "rls chancel_p 1";
  1570 val tt = str2term "((1 * a + -1 * b) * (1 * a + -1 * b))"(*denominator only*);
  1571 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1572 if term2str tt' = "a ^^^ 2 + -2 * a * b + b ^^^ 2" then () else error "rls chancel_p 2";
  1573 
  1574 "----- with make_deriv; WN1130912 not investigated further, will be discontinued";
  1575 val SOME (tt, _) = factout_p_ thy t; 
  1576 if term2str tt = "(a + b) * (a + -1 * b) / ((a + -1 * b) * (a + -1 * b))"
  1577 then () else error "rls chancel_p 3";
  1578 term2str tt = "(1 * a + 1 * b) * (1 * a + -1 * b) / ((1 * a + -1 * b) * (1 * a + -1 * b))";
  1579 
  1580 "--- with simpler ruleset";
  1581 val {rules, rew_ord= (_, ro), ...} = rep_rls (assoc_rls "rev_rew_p");
  1582 val der = make_deriv thy Atools_erls rules ro NONE tt;
  1583 if length der = 12 then () else error "WN1130912 rls chancel_p 4";
  1584 (*default_print_depth 99;*) writeln (deriv2str der); (*default_print_depth 3;*)
  1585 
  1586 (*default_print_depth 99;*) map (term2str o #1) der; (*default_print_depth 3;*)
  1587 "...,(-1 * b ^^^ 2 + a ^^^ 2) / (-2 * (a * b) + a ^^^ 2 + (-1 * b) ^^^ 2) ]";
  1588 (*default_print_depth 99;*) map (rule2str o #2) der; (*default_print_depth 3;*)
  1589 (*default_print_depth 99;*) map (term2str o #1 o #3) der; (*default_print_depth 3;*)
  1590 
  1591 val der = make_deriv thy Atools_erls rules ro NONE 
  1592 	(str2term "(1 * a + 1 * b) * (1 * a + -1 * b)");
  1593 (*default_print_depth 99;*) writeln (deriv2str der); (*default_print_depth 3;*)
  1594 
  1595 val {rules, rew_ord=(_,ro),...} = rep_rls (assoc_rls "rev_rew_p");
  1596 val der = make_deriv thy Atools_erls rules ro NONE 
  1597 	(str2term "(1 * a + -1 * b) * (1 * a + -1 * b)");
  1598 (*default_print_depth 99;*) writeln (deriv2str der); (*default_print_depth 3;*)
  1599 (*default_print_depth 99;*) map (term2str o #1) der; (*default_print_depth 3;*)
  1600 (*WN060829 ...postponed*)
  1601 
  1602 
  1603 "-------- fun eval_get_denominator -------------------------------------------";
  1604 "-------- fun eval_get_denominator -------------------------------------------";
  1605 "-------- fun eval_get_denominator -------------------------------------------";
  1606 val thy = @{theory Isac};
  1607 val t = Thm.term_of (the (parse thy "get_denominator ((a +x)/b)"));
  1608 val SOME (_, t') = eval_get_denominator "" 0 t thy;
  1609 if term2str t' = "get_denominator ((a + x) / b) = b"
  1610 then () else error "get_denominator ((a + x) / b) = b"
  1611 
  1612 
  1613 "-------- several errpats in complicated term --------------------------------";
  1614 "-------- several errpats in complicated term --------------------------------";
  1615 "-------- several errpats in complicated term --------------------------------";
  1616 (*WN12xxxx TODO: instead of Gabriella's example here (27.Jul.12) find a simpler one
  1617   WN130912: kept this test, although not clear what for*)
  1618 reset_states ();
  1619 CalcTree [(["Term ((5*b + 25)/(a^2 - b^2) * (a - b)/(5*b))", "normalform N"], 
  1620   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1621 Iterator 1;
  1622 moveActiveRoot 1;
  1623 autoCalculate 1 CompleteCalc;
  1624 val ((pt, p), _) = get_calc 1;
  1625 (*show_pt pt;
  1626 [
  1627 (([], Frm), Simplify ((5 * b + 25) / (a ^^^ 2 - b ^^^ 2) * (a - b) / (5 * b))),
  1628 (([1], Frm), (5 * b + 25) / (a ^^^ 2 - b ^^^ 2) * (a - b) / (5 * b)),
  1629 (([1], Res), (5 * b + 25) / (a ^^^ 2 + -1 * b ^^^ 2) * (a + -1 * b) / (5 * b)),
  1630 (([2], Res), (5 * b + 25) * (a + -1 * b) / (a ^^^ 2 + -1 * b ^^^ 2) / (5 * b)),
  1631 (([3], Res), (25 * a + -25 * b + 5 * (a * b) + -5 * b ^^^ 2) / (a ^^^ 2 + -1 * b ^^^ 2) /
  1632 (5 * b)),
  1633 (([4], Res), (25 + 5 * b) / (a + b) / (5 * b)),
  1634 (([5], Res), (25 + 5 * b) / ((a + b) * (5 * b))),
  1635 (([6], Res), (25 + 5 * b) / (5 * (a * b) + 5 * b ^^^ 2)),
  1636 (([7], Res), (5 + b) / (a * b + b ^^^ 2)),
  1637 (([], Res), (5 + b) / (a * b + b ^^^ 2))] *)
  1638 
  1639 
  1640 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1641 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1642 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1643 (*------- Schalk I, p.70 Nr. 480b; a/b : c/d translated to a/b * d/c*)
  1644 val t = str2term 
  1645   ("((12*x*y / (9*x^^^2 - y^^^2))  /  (1 / (3*x - y)^^^2 - 1 / (3*x + y)^^^2))  *  " ^
  1646 	"((1/(x - 5*y)^^^2  -  1/(x + 5*y)^^^2)  /  (20*x*y / (x^^^2 - 25*y^^^2)))");
  1647 
  1648 (*1st factor separately simplified *)
  1649 val t = str2term "((12*x*y / (9*x^^^2 - y^^^2))  /  (1 / (3*x - y)^^^2 - 1 / (3*x + y)^^^2))";
  1650 val SOME (t', _) = rewrite_set_ thy false norm_Rational t; 
  1651 if term2str t' = "(-9 * x ^^^ 2 + y ^^^ 2) / -1" then () else error "Nr. 480b lhs changed";
  1652 (*2nd factor separately simplified *)
  1653 val t = str2term "((1/(x - 5*y)^^^2  -  1/(x + 5*y)^^^2)  /  (20*x*y / (x^^^2 - 25*y^^^2)))";
  1654 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
  1655 if term2str t' = "-1 / (-1 * x ^^^ 2 + 25 * y ^^^ 2)" then () else error "Nr. 480b rhs changed";
  1656 
  1657 "-------- Schalk I, p.70 Nr. 477a: terms are exploding ?!?";
  1658 val t = str2term ("b*y/(b - 2*y)/((b^^^2 - y^^^2)/(b+2*y))  /" ^
  1659 		 "(b^^^2*y + b*y^^^2) * (a+x)^^^2  /  ((b^^^2 - 4*y^^^2) * (a+2*x)^^^2)");
  1660 (*val SOME (t',_) = rewrite_set_ thy false norm_Rational t;
  1661 :
  1662 ###  rls: cancel_p on: (a ^^^ 2 * (b * y) + 2 * (a * (b * (x * y))) + b * (x ^^^ 2 * y)) /
  1663 (b + -2 * y) /
  1664 ((b ^^^ 2 + -1 * y ^^^ 2) / (b + 2 * y)) /
  1665 (b ^^^ 2 * y + b * y ^^^ 2) /
  1666 (a ^^^ 2 * b ^^^ 2 + -4 * (a ^^^ 2 * y ^^^ 2) + 4 * (a * (b ^^^ 2 * x)) +
  1667  -16 * (a * (x * y ^^^ 2)) +
  1668  4 * (b ^^^ 2 * x ^^^ 2) +
  1669  -16 * (x ^^^ 2 * y ^^^ 2)) 
  1670 exception Div raised
  1671 
  1672 BUT
  1673 val t = str2term 
  1674   ("(a ^^^ 2 * (b * y) + 2 * (a * (b * (x * y))) + b * (x ^^^ 2 * y)) /" ^
  1675   "(b + -2 * y) /" ^
  1676   "((b ^^^ 2 + -1 * y ^^^ 2) / (b + 2 * y)) /" ^
  1677   "(b ^^^ 2 * y + b * y ^^^ 2) /" ^
  1678   "(a ^^^ 2 * b ^^^ 2 + -4 * (a ^^^ 2 * y ^^^ 2) + 4 * (a * (b ^^^ 2 * x)) +" ^
  1679   "-16 * (a * (x * y ^^^ 2)) +" ^
  1680   "4 * (b ^^^ 2 * x ^^^ 2) +" ^
  1681   "-16 * (x ^^^ 2 * y ^^^ 2))");
  1682 NONE = cancel_p_ thy t;
  1683 *)
  1684 
  1685 (*------- Schalk I, p.70 Nr. 476b in 2003 this worked using 10 sec. *)
  1686 val t = str2term 
  1687   ("((a^^^2 - b^^^2)/(2*a*b) + 2*a*b/(a^^^2 - b^^^2))  /  ((a^^^2 + b^^^2)/(2*a*b) + 1)    / " ^
  1688    "((a^^^2 + b^^^2)^^^2  /  (a + b)^^^2)");
  1689 (*
  1690 trace_rewrite := true;
  1691 rewrite_set_ thy false norm_Rational t;
  1692 :
  1693 ####  rls: cancel_p on: (2 * (a ^^^ 7 * b) + 4 * (a ^^^ 6 * b ^^^ 2) + 6 * (a ^^^ 5 * b ^^^ 3) +
  1694  8 * (a ^^^ 4 * b ^^^ 4) +
  1695  6 * (a ^^^ 3 * b ^^^ 5) +
  1696  4 * (a ^^^ 2 * b ^^^ 6) +
  1697  2 * (a * b ^^^ 7)) /
  1698 (2 * (a ^^^ 9 * b) + 4 * (a ^^^ 8 * b ^^^ 2) +
  1699  2 * (2 * (a ^^^ 7 * b ^^^ 3)) +
  1700  4 * (a ^^^ 6 * b ^^^ 4) +
  1701  -4 * (a ^^^ 4 * b ^^^ 6) +
  1702  -4 * (a ^^^ 3 * b ^^^ 7) +
  1703  -4 * (a ^^^ 2 * b ^^^ 8) +
  1704  -2 * (a * b ^^^ 9))
  1705 
  1706 if term2str t = "1 / (a ^^^ 2 + -1 * b ^^^ 2)" then ()
  1707 else error "rational.sml: diff.behav. in norm_Rational_mg 49";
  1708 *)
  1709 
  1710 "-------- Schalk I, p.70 Nr. 480a: terms are exploding ?!?";
  1711 val t = str2term ("(1/x + 1/y + 1/z)  /  (1/x - 1/y - 1/z)  /  " ^
  1712   "(2*x^^^2 / (x^^^2 - z^^^2) / (x / (x + z)  +  x / (x - z)))");
  1713 (*
  1714 trace_rewrite := true;
  1715 rewrite_set_ thy false norm_Rational t;
  1716 :
  1717 ####  rls: cancel_p on: (2 * (x ^^^ 6 * (y ^^^ 2 * z)) + 2 * (x ^^^ 6 * (y * z ^^^ 2)) +
  1718  2 * (x ^^^ 5 * (y ^^^ 2 * z ^^^ 2)) +
  1719  -2 * (x ^^^ 4 * (y ^^^ 2 * z ^^^ 3)) +
  1720  -2 * (x ^^^ 4 * (y * z ^^^ 4)) +
  1721  -2 * (x ^^^ 3 * (y ^^^ 2 * z ^^^ 4))) /
  1722 (-2 * (x ^^^ 6 * (y ^^^ 2 * z)) + -2 * (x ^^^ 6 * (y * z ^^^ 2)) +
  1723  2 * (x ^^^ 5 * (y ^^^ 2 * z ^^^ 2)) +
  1724  2 * (x ^^^ 4 * (y ^^^ 2 * z ^^^ 3)) +
  1725  2 * (x ^^^ 4 * (y * z ^^^ 4)) +
  1726  -2 * (x ^^^ 3 * (y ^^^ 2 * z ^^^ 4)))
  1727 *)
  1728 
  1729 "-------- Schalk I, p.60 Nr. 215d: terms are exploding, internal loop does not terminate";
  1730 val t = str2term "(a-b)^^^3 * (x+y)^^^4 / ((x+y)^^^2 * (a-b)^^^5)";
  1731 (* Kein Wunder, denn Z???ler und Nenner extra als Polynom dargestellt ergibt:
  1732 
  1733 val t = str2term "(a-b)^^^3 * (x+y)^^^4";
  1734 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1735 term2str t;
  1736 "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";
  1737 val t = str2term "((x+y)^^^2 * (a-b)^^^5)";
  1738 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1739 term2str t;
  1740 "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";
  1741 
  1742 anscheinend macht dem Rechner das Krzen diese Bruches keinen Spass mehr ...*)
  1743 
  1744 "-------- Schalk I, p.70 Nr. 480b: terms are exploding, trace_rewrite stops at";
  1745 val t = str2term ("((12*x*y/(9*x^^^2 - y^^^2))/" ^
  1746 		 "(1/(3*x - y)^^^2 - 1/(3*x + y)^^^2)) *" ^
  1747 		 "(1/(x - 5*y)^^^2 - 1/(x + 5*y)^^^2)/" ^
  1748 		 "(20*x*y/(x^^^2 - 25*y^^^2))");
  1749 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1750 :
  1751 ####  rls: cancel_p on: (19440 * (x ^^^ 8 * y ^^^ 2) + -490320 * (x ^^^ 6 * y ^^^ 4) +
  1752  108240 * (x ^^^ 4 * y ^^^ 6) +
  1753  -6000 * (x ^^^ 2 * y ^^^ 8)) /
  1754 (2160 * (x ^^^ 8 * y ^^^ 2) + -108240 * (x ^^^ 6 * y ^^^ 4) +
  1755  1362000 * (x ^^^ 4 * y ^^^ 6) +
  1756  -150000 * (x ^^^ 2 * y ^^^ 8))
  1757 *)
  1758