test/Tools/isac/Knowledge/rational.sml
author Walther Neuper <neuper@ist.tugraz.at>
Mon, 16 Sep 2013 12:27:20 +0200
changeset 52106 7f3760f39bdc
parent 52105 2786cc9704c8
child 55445 33b0f6db720c
permissions -rw-r--r--
review of examples for non-termination of rls norm_Rational
     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 print_depth 3; (*999*)
   354 val (SOME a, SOME b) = (poly_of_term vs numerator, poly_of_term vs denominator);
   355 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 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 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} |> string_of_thm |> 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\",??.unknown)" andalso
   694 (revsets |> nth 1 |> nth 2 |> rule2str) = 
   695   "Thm (\"sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))\",??.unknown)" andalso
   696 (revsets |> nth 1 |> nth 3 |> rule2str) = 
   697   "Thm (\"sym_#mult_Float ((2,0), (0,0)) __ ((3,0), (0,0))\",??.unknown)" andalso
   698 (revsets |> nth 1 |> nth 4 |> rule2str) = 
   699   "Thm (\"sym_#mult_Float ((~1,0), (0,0)) __ ((3,0), (0,0))\",??.unknown)" andalso
   700 (revsets |> nth 1 |> nth 5 |> rule2str) = 
   701   "Thm (\"sym_#mult_Float ((3,0), (0,0)) __ ((3,0), (0,0))\",??.unknown)" andalso
   702 (revsets |> nth 1 |> nth 6 |> rule2str) = "Rls_ (\"sym_order_mult_rls_\")" andalso
   703 (revsets |> nth 1 |> nth 7 |> rule2str) = 
   704   "Thm (\"sym_mult_assoc\",Groups.semigroup_mult_class.mult_assoc)"
   705 then () else error "first 7 elements in revset changed"
   706 
   707 (** find the rule 'r' to apply to term 't' **)
   708 (*/------- WN1309: since cancel_ (accepted "-" between monomials) has been replaced by cancel_p_ 
   709   for Isabelle2013, we don't get a working revset, but non-termination:
   710 
   711   val SOME (r as (Thm (str, thm))) = nex revsets t;
   712   :
   713 ((3 * 3 + -1 * x * x) / (3 * 3 + 2 * 3 * x + x * x), 
   714   Rls_ ("sym_order_mult_rls_"), ((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), 
   718   Thm ("sym_mult_assoc",""), ((3 * 3 + -1 * x * x) / (3 * 3 + 2 * 3 * x + x * x), []))","
   719 ((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), []))","
   720  :
   721 ### Isabelle2002:
   722   Thm ("sym_#mult_2_3", "6 = 2 * 3")
   723 ### Isabelle2009-2 for cancel_ (not cancel_p_):
   724 if str = "sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))"
   725    andalso string_of_thm thm = 
   726            (string_of_thm o make_thm o (cterm_of (@{theory "Isac"})))
   727                (Trueprop $ (term_of o the o (parse thy)) "9 = 3 ^^^ 2") then ()
   728 else error "rational.sml next_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   729 \---------------------------------------------------------------------------------------/*)
   730 
   731 (** check, if the rule 'r' applied by the user to 't' belongs to the ruleset;
   732   if the rule is OK, the term resulting from applying the rule is returned,too;
   733   there might be several rule applications inbetween,
   734   which are listed after the head in reverse order **)
   735 (*/-------------------------------------------- Isabelle2013: this gives "error id_of_thm";
   736   we don't repair this, because interaction within "reverse rewriting" never worked properly:
   737 
   738   val (r, (t, asm))::_ = loc revsets t r;
   739 if term2str t = "(9 - x ^^^ 2) / (3 ^^^ 2 + 6 * x + x ^^^ 2)" andalso asm = []
   740 then () else error "rational.sml locate_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   741 
   742 (* find the next rule to apply *)
   743   val SOME (r as (Thm (str, thm))) = nex revsets t;
   744 if str = "sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))" andalso
   745    string_of_thm thm = (string_of_thm o make_thm o (cterm_of (@{theory "Isac"})))
   746                 (Trueprop $ (term_of o the o (parse thy)) "9 = 3 ^^^ 2") then ()
   747 else error "rational.sml next_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2)";
   748 
   749 (*check the next rule*)
   750   val (r, (t, asm)) :: _ = loc revsets t r;
   751 if term2str t = "(3 ^^^ 2 - x ^^^ 2) / (3 ^^^ 2 + 6 * x + x ^^^ 2)" then ()
   752 else error "rational.sml locate_rule (9 - x ^^^ 2) / (9 - 6 * x + x ^^^ 2) II";
   753 
   754 (*find and check the next rules, rewrite*)
   755   val SOME r = nex revsets t;
   756   val (r,(t,asm))::_ = loc revsets t r;
   757 if term2str t = "(3 ^^^ 2 - x ^^^ 2) / (3 ^^^ 2 + 2 * 3 * x + x ^^^ 2)" then ()
   758 else error "rational.sml locate_rule II";
   759 
   760   val SOME r = nex revsets t;
   761   val (r,(t,asm))::_ = loc revsets t r;
   762 if term2str t = "(3 - x) * (3 + x) / (3 ^^^ 2 + 2 * 3 * x + x ^^^ 2)" then ()
   763 else error "rational.sml next_rule II";
   764 
   765   val SOME r = nex revsets t;
   766   val (r,(t,asm))::_ = loc revsets t r;
   767 if term2str t = "(3 - x) * (3 + x) / ((3 + x) * (3 + x))" then ()
   768 else error "rational.sml next_rule III";
   769 
   770   val SOME r = nex revsets t;
   771   val (r, (t, asm)) :: _ = loc revsets t r;
   772   val ss = term2str t;
   773 if ss = "(3 - x) / (3 + x)" andalso terms2str asm = "[\"3 + x ~= 0\"]" then ()
   774 else error "rational.sml: new behav. in rev-set cancel";
   775 \--------------------------------------------------------------------------------------/*)
   776 
   777 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   778 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   779 "-------- 'reverse-ruleset' cancel_p -----------------------------------------";
   780 (*WN130909: the example below shows, why "reverse rewriting" only worked for
   781   special cases.*)
   782 
   783 (*the term for which reverse rewriting is demonstrated*)
   784 val t = str2term "(9 + (-1)*x^^^2) / (9 + ((-6)*x + x^^^2))";
   785 val Rrls {scr=Rfuns {init_state=ini,locate_rule=loc,
   786 		       next_rule=nex,normal_form=nor,...},...} = cancel_p;
   787 
   788 (*normal_form produces the result in ONE step*)
   789 val SOME (t',_) = nor t; 
   790 term2str t' = "(3 + 1 * x) / (3 + -1 * x)";
   791 
   792 (*initialize the interpreter state used by the 'me'*)
   793 val SOME (t', asm) = cancel_p_ thy t;
   794 term2str t' = "(3 + x) / (3 + -1 * x)" (*true*);
   795 terms2str asm = "[\"3 + -1 * x ~= 0\"]" (*true*);
   796 val (t,_,revsets,_) = ini t;
   797 
   798 (* WN.10.10.02: dieser Fall terminiert nicht 
   799            (make_polynomial enth"alt zu viele rules)
   800 WN060823 'init_state' requires rewriting on specified location in the term
   801 print_depth 99; Rfuns; print_depth 3;
   802 WN060831 cycling "sym_order_mult_rls_" "sym_mult_assoc"
   803          as was with make_polynomial before ?!?*)
   804 
   805 val SOME r = nex revsets t;
   806 eq_Thm (r, Thm ("sym_#power_Float ((3,0), (0,0)) __ ((2,0), (0,0))", 
   807 		mk_thm thy "9 = 3 ^^^ 2"));
   808 (*WN060831 *** id_of_thm
   809            Exception- ERROR raised ...
   810 val (r,(t,asm))::_ = loc revsets t r;
   811 term2str t;
   812 
   813   val SOME r = nex revsets t;
   814   val (r,(t,asm))::_ = loc revsets t r;
   815   term2str t;
   816 *)                    
   817 
   818 "-------- examples: rls norm_Rational ----------------------------------------";
   819 "-------- examples: rls norm_Rational ----------------------------------------";
   820 "-------- examples: rls norm_Rational ----------------------------------------";
   821 val t = str2term "(3*x+5)/18 - x/2  - -(3*x - 2)/9 = 0";
   822 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   823 if term2str t' = "1 / 18 = 0" then () else error "rational.sml 1";
   824 
   825 val t = str2term "(17*x - 51)/9 - (-(13*x - 3)/6) + 11 - (9*x - 7)/4 = 0";
   826 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   827 if term2str t' = "(237 + 65 * x) / 36 = 0" then () 
   828 else error "rational.sml 2";
   829 
   830 val t = str2term "(1/2 + (5*x)/2)^^^2 - ((13*x)/2 - 5/2)^^^2 - (6*x)^^^2 + 29";
   831 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   832 if term2str t' = "23 + 35 * x + -72 * x ^^^ 2" then ()
   833 else error "rational.sml 3";
   834 
   835 (*trace_rewrite:=true;*)
   836 val t = str2term "Not (6*x is_atom)";
   837 val SOME (t',_) = rewrite_set_ thy false powers_erls t; term2str t';
   838 "HOL.True";
   839 val t = str2term "1 < 2";
   840 val SOME (t',_) = rewrite_set_ thy false powers_erls t; term2str t';
   841 "HOL.True";
   842 
   843 val t = str2term "(6*x)^^^2";
   844 val SOME (t',_) = rewrite_ thy dummy_ord powers_erls false 
   845 			   (num_str @{thm realpow_def_atom}) t;
   846 if term2str t' = "6 * x * (6 * x) ^^^ (2 + -1)" then ()
   847 else error "rational.sml powers_erls (6*x)^^^2";
   848 
   849 val t = str2term "-1 * (-2 * (5 / 2 * (13 * x / 2)))";
   850 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   851 if term2str t' = "65 * x / 2" then () else error "rational.sml 4";
   852 
   853 val t = str2term "1 - ((13*x)/2 - 5/2)^^^2";
   854 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   855 if term2str t' = "(-21 + 130 * x + -169 * x ^^^ 2) / 4" then () 
   856 else error "rational.sml 5";
   857 
   858 (*SRAM Schalk I, p.92 Nr. 609a*)
   859 val t = str2term "2*(3 - x/5)/3 - 4*(1 - x/3) - x/3 - 2*(x/2 - 1/4)/27 +5/54";
   860 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   861 if term2str t' = "(-255 + 112 * x) / 135" then () 
   862 else error "rational.sml 6";
   863 
   864 (*SRAM Schalk I, p.92 Nr. 610c*)
   865 val t = str2term "((x- 1)/(x+1) + 1) / ((x- 1)/(x+1) - (x+1)/(x- 1)) - 2";
   866 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   867 if term2str t' = "(-3 + -1 * x) / 2" then () else error "rational.sml 7";
   868 
   869 (*SRAM Schalk I, p.92 Nr. 476a*)
   870 val t = str2term "(x^^^2/(1 - x^^^2) + 1)/(x/(1 - x) + 1) * (1 + x)";
   871 (*. a/b : c/d translated to a/b * d/c .*)
   872 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   873 if term2str t' = "1" then () else error "rational.sml 8";
   874 
   875 (*Schalk I, p.92 Nr. 472a*)
   876 val t = str2term "((8*x^^^2 - 32*y^^^2)/(2*x + 4*y))/((4*x - 8*y)/(x + y))";
   877 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
   878 if term2str t' = "x + y" then () else error "rational.sml p.92 Nr. 472a";
   879 
   880 (*Schalk I, p.70 Nr. 480b: SEE rational.sml --- nonterminating rls norm_Rational ---*)
   881 
   882 (*WN130910 add_fractions_p exception Div raised + history:
   883 ### WN.2.6.03 from rlang.sml 56a 
   884 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)";
   885 val NONE = rewrite_set_ thy false add_fractions_p t;
   886 
   887 THE ERROR ALREADY OCCURS IN THIS PART:
   888 val t = str2term "(a + b * x) / (a + -1 * (b * x)) + (-1 * a + b * x) / (a + b * x)";
   889 val NONE = add_fraction_p_ thy t;
   890 
   891 SEE Test_Some.thy: section {* add_fractions_p downto exception Div raised ===
   892 *)
   893 
   894 "-------- rational numerals --------------------------------------------------";
   895 "-------- rational numerals --------------------------------------------------";
   896 "-------- rational numerals --------------------------------------------------";
   897 (*SRA Schalk I, p.40 Nr. 164b *)
   898 val t = str2term "(47/6 - 76/9 + 13/4)/(35/12)";
   899 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   900 term2str t;
   901 if term2str t = "19 / 21" then ()
   902 else error "rational.sml: diff.behav. in norm_Rational_mg 1";
   903 
   904 (*SRA Schalk I, p.40 Nr. 166a *)
   905 val t = str2term "((5/4)/(4+22/7) + 37/20)*(110/3 - 110/9 * 23/11)";
   906 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   907 term2str t;
   908 if term2str t = "45 / 2" then ()
   909 else error "rational.sml: diff.behav. in norm_Rational_mg 2";
   910 
   911 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   912 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   913 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   914 (* e190c Stefan K.*)
   915 val t = str2term "((1 + 9*a^^^2) * (1 + 3*a)) / ((3*a + 9*a^^^2) * (1 + 3*a))";
   916 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   917 if term2str t = "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
   918 then () else error "rational.sml: diff.behav. in norm_Rational_mg 3";
   919 
   920 (* e192b Stefan K.*)
   921 val t = str2term "(x^^^2 * (7*x + (-1)*y))  /  (y^^^2 * (7*x + (-1)*y))";
   922 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   923 if term2str t = "x ^^^ 2 / y ^^^ 2"
   924 then () else error "rational.sml: diff.behav. in norm_Rational_mg 4";
   925 
   926 (*SRC Schalk I, p.66 Nr. 379c *)
   927 val t = str2term "(a - b)/(b - a)";
   928 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   929 term2str t;
   930 if term2str t = "-1"
   931 then () else error "rational.sml: diff.behav. in norm_Rational_mg 5";
   932 
   933 (*SRC Schalk I, p.66 Nr. 380b *)
   934 val t = str2term "15*(3*x + 3) * (4*x + 9)  /  (12*(2*x + 7) * (5*x + 5))";
   935 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   936 if term2str t = "(27 + 12 * x) / (28 + 8 * x)"
   937 then () else error "rational.sml: diff.behav. in norm_Rational_mg 6";
   938 
   939 (*Schalk I, p.60 Nr. 215c: was not cancelled with Isabelle2002 *)
   940 val t = str2term "(a + b)^^^4 * (x - y)  /  ((x - y)^^^3 * (a + b)^^^2)";
   941 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   942 if term2str t = "(a ^^^ 2 + 2 * a * b + b ^^^ 2) / (x ^^^ 2 + -2 * x * y + y ^^^ 2)"
   943 then () else error "rational.sml: diff.behav. in norm_Rational_mg 7";
   944 
   945 (*SRC Schalk I, p.66 Nr. 381b *)
   946 val t = str2term 
   947 "(4*x^^^2 - 20*x + 25)/(2*x - 5)^^^3";
   948 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   949 if term2str t = "1 / (-5 + 2 * x)"
   950 then () else error "rational.sml: diff.behav. in norm_Rational_mg 9";
   951 
   952 (* e190c Stefan K.*)
   953 val t = str2term "((1 + 9*a^^^2) * (1 + 3*a))  /  ((3*a + 9*a^^^2) * (1 + 3 * a))";
   954 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   955 if term2str t =  "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
   956 then () else error "rational.sml: diff.behav. in norm_Rational_mg 3";
   957 
   958 (* e192b Stefan K.*)
   959 val t = str2term "(x^^^2 * (7*x + (-1)*y))  /  (y^^^2 * (7*x + (-1)*y))";
   960 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   961 if term2str t = "x ^^^ 2 / y ^^^ 2"
   962 then () else error "rational.sml: diff.behav. in norm_Rational_mg 4";
   963 
   964 (*SRC Schalk I, p.66 Nr. 379c *)
   965 val t = str2term "(a - b) / (b - a)";
   966 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   967 if term2str t = "-1"
   968 then () else error "rational.sml: diff.behav. in norm_Rational_mg 5";
   969 
   970 (*SRC Schalk I, p.66 Nr. 380b *)
   971 val t = str2term "15*(3*x + 3) * (4*x + 9)  /  (12*(2*x + 7) * (5*x + 5))";
   972 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   973 if term2str t = "(27 + 12 * x) / (28 + 8 * x)"
   974 then () else error "rational.sml: diff.behav. in norm_Rational_mg 6";
   975 
   976 (*Schalk I, p.60 Nr. 215c *)
   977 val t = str2term "(a + b)^^^4 * (x - y)  /  ((x - y)^^^3 * (a + b)^^^2)";
   978 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   979 if term2str t = "(a ^^^ 2 + 2 * a * b + b ^^^ 2) / (x ^^^ 2 + -2 * x * y + y ^^^ 2)"
   980 then () else error "Schalk I, p.60 Nr. 215c: with Isabelle2002 cancellation incomplete, changed";
   981 
   982 (* extreme example from somewhere *)
   983 val t = str2term 
   984     ("(a^^^4 * x  +  -1*a^^^4 * y  +  4*a^^^3 * b * x  +  -4*a^^^3 * b * y  + " ^
   985       "6*a^^^2 * b^^^2 * x  +  -6*a^^^2 * b^^^2 * y  +  4*a * b^^^3 * x  +  -4*a * b^^^3 * y  + " ^
   986       "b^^^4 * x  +  -1*b^^^4 * y) " ^
   987   " / (a^^^2 * x^^^3  +  -3*a^^^2 * x^^^2 * y  +  3*a^^^2 * x * y^^^2  +  -1*a^^^2 * y^^^3 + " ^
   988       "2*a * b * x^^^3  +  -6*a * b * x^^^2 * y  +  6*a * b * x * y^^^2  +  -2*a * b * y^^^3 + " ^
   989       "b^^^2 * x^^^3  +  -3*b^^^2 * x^^^2 * y  +  3*b^^^2 * x * y^^^2  +  -1*b ^^^ 2 * y ^^^ 3)")
   990 val SOME (t, _) = rewrite_set_ thy false cancel_p t;
   991 if term2str t = "(a ^^^ 2 + 2 * a * b + b ^^^ 2) / (x ^^^ 2 + -2 * x * y + y ^^^ 2)"
   992 then () else error "with Isabelle2002: NONE -- now SOME changed";
   993 
   994 (*Schalk I, p.66 Nr. 381a *)
   995 (* ATTENTION: here the rls is very slow. In Isabelle2002 this required 2 min *)
   996 val t = str2term "18*(a + b)^^^3 * (a - b)^^^2 / (72*(a - b)^^^3 * (a + b)^^^2)";
   997 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   998 if term2str t = "(a + b) / (4 * a + -4 * b)"
   999 then () else error "rational.sml: diff.behav. in norm_Rational_mg 8";
  1000 
  1001 (*SRC Schalk I, p.66 Nr. 381b *)
  1002 val t = str2term "(4*x^^^2 - 20*x + 25) / (2*x - 5)^^^3";
  1003 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1004 if term2str t = "1 / (-5 + 2 * x)"
  1005 then () else error "rational.sml: diff.behav. in norm_Rational_mg 9";
  1006 
  1007 (*SRC Schalk I, p.66 Nr. 381c *)
  1008 val t = str2term "(27*a^^^3 + 9*a^^^2+3*a+1) / (27*a^^^3 + 18*a^^^2+3*a)";
  1009 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1010 if term2str t = "(1 + 9 * a ^^^ 2) / (3 * a + 9 * a ^^^ 2)"
  1011 then () else error "rational.sml: diff.behav. in norm_Rational_mg 10";
  1012 
  1013 (*SRC Schalk I, p.66 Nr. 383a *)
  1014 val t = str2term "(5*a^^^2 - 5*a*b) / (a - b)^^^2";
  1015 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1016 if term2str t = "-5 * a / (-1 * a + b)"
  1017 then () else error "rational.sml: diff.behav. in norm_Rational_mg 11";
  1018 
  1019 "----- NOT TERMINATING ?: worked before 0707xx";
  1020 val t = str2term "(a^^^2 - 1)*(b + 1) / ((b^^^2 - 1)*(a+1))";
  1021 (* WN130911 "exception Div raised" by 
  1022   cancel_p_ thy (str2term ("(-1 + -1 * b + a ^^^ 2 + a ^^^ 2 * b) /" ^
  1023                            "(-1 + -1 * a + b ^^^ 2 + a * b ^^^ 2)"))
  1024 
  1025 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1026 if term2str t = "(1 + -1 * a) / (1 + -1 * b)" then ()
  1027 else error "rational.sml MG tests 3e";
  1028 *)
  1029 
  1030 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1031 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1032 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1033 (*SRA Schalk I, p.67 Nr. 403a *)
  1034 val t = str2term "4/x - 3/y - 1";
  1035 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1036 if term2str t = "(-3 * x + 4 * y + -1 * x * y) / (x * y)"
  1037 then () else error "rational.sml: diff.behav. in norm_Rational_mg 12";
  1038 
  1039 val t = str2term "(2*a+3*b)/(b*c) + (3*c+a)/(a*c) - (2*a^^^2+3*b*c)/(a*b*c)";
  1040 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1041 if term2str t = "4 / c"
  1042 then () else error "rational.sml: diff.behav. in norm_Rational_mg 13";
  1043 
  1044 (*SRA Schalk I, p.67 Nr. 410b *)
  1045 val t = str2term "1/(x+1) + 1/(x+2) - 2/(x+3)";
  1046 (* WN130911 non-termination due to non-termination of
  1047   cancel_p_ thy (str2term "(5 + 3 * x) / (6 + 11 * x + 6 * x ^^^ 2 + x ^^^ 3)")
  1048 
  1049 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1050 if term2str t = "(5 + 3 * x) / (6 + 11 * x + 6 * x ^^^ 2 + x ^^^ 3)"
  1051 then () else error "rational.sml: diff.behav. in norm_Rational_mg 14";
  1052 *)
  1053 
  1054 (*SRA Schalk I, p.67 Nr. 413b *)
  1055 val t = str2term "(1 + x)/(1 - x)  -  (1 - x)/(1 + x)  +  2*x/(1 - x^^^2)";
  1056 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1057 if term2str t = "6 * x / (1 + -1 * x ^^^ 2)"
  1058 then () else error "rational.sml: diff.behav. in norm_Rational_mg 15";
  1059 
  1060 (*SRA Schalk I, p.68 Nr. 414a *)
  1061 val t = str2term "(x + 2)/(x - 1)  +  (x - 3)/(x - 2)  -  (x + 1)/((x - 1)*(x - 2))";
  1062 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1063 if term2str t ="(-2 + -5 * x + 2 * x ^^^ 2) / (2 + -3 * x + x ^^^ 2)"
  1064 then () else error "rational.sml: diff.behav. in norm_Rational_mg 16";
  1065 
  1066 (*SRA Schalk I, p.68 Nr. 428b *)
  1067 val t = str2term 
  1068   "1/(a - b)^^^2  +  1/(a + b)^^^2  -  2/(a^^^2 - b^^^2)  -  4*(b^^^2 - 1)/(a^^^2 - b^^^2)^^^2";
  1069 (* WN130911 non-termination due to non-termination of
  1070   cancel_p_ thy (str2term "(4 + -4 * b ^^^ 2) / (a ^^^ 4 + -2 * (a ^^^ 2 * b ^^^ 2) + b ^^^ 4)")
  1071 
  1072 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1073 if term2str t = "4 / (a ^^^ 4 + -2 * a ^^^ 2 * b ^^^ 2 + b ^^^ 4)"
  1074 then () else error "rational.sml: diff.behav. in norm_Rational_mg 18";
  1075 *)
  1076 
  1077 (*SRA Schalk I, p.68 Nr. 430b *)
  1078 val t = str2term 
  1079   "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";
  1080 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1081 if term2str t = "a + 3 * b"
  1082 then () else error "rational.sml: diff.behav. in norm_Rational_mg 19";
  1083 
  1084 (*SRA Schalk I, p.68 Nr. 432 *)
  1085 val t = str2term 
  1086   ("(a^^^2 + a*b) / (a^^^2 - b^^^2)  -  (b^^^2 - a*b) / (b^^^2 - a^^^2)  +  " ^
  1087   "a^^^2*(a - b) / (a^^^3 - a^^^2*b)  -  2*a*(a^^^2 - b^^^2) / (a^^^3 - a*b^^^2)  -  " ^
  1088   "2*b^^^2 / (a^^^2 - b^^^2)");
  1089 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1090 term2str t;
  1091 if term2str t = "0"
  1092 then () else error "rational.sml: diff.behav. in norm_Rational_mg 20";
  1093 
  1094 (* some example *)
  1095 val t = str2term "3*a / (a*b)  +  x/y";
  1096 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1097 if term2str t = "(3 * y + b * x) / (b * y)"
  1098 then () else error "rational.sml: diff.behav. in norm_Rational_mg 21";
  1099 
  1100 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1101 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1102 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1103 (*------- SRM Schalk I, p.68 Nr. 436a *)
  1104 val t = str2term "3*(x+y) / (15*(x - y))  *   25*(x - y)^^^2 / (18*(x + y)^^^2)";
  1105 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1106 if term2str t = "(-5 * x + 5 * y) / (-18 * x + -18 * y)"
  1107 then () else error "rational.sml: diff.behav. in norm_Rational_mg 22";
  1108 
  1109 (*------- SRM.test Schalk I, p.68 Nr. 436b *)
  1110 val t = str2term "5*a*(a - b)^^^2*(a + b)^^^3/(7*b*(a - b)^^^3) * 7*b/(a + b)^^^3";
  1111 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1112 if term2str t = "5 * a / (a + -1 * b)"
  1113 then () else error "rational.sml: diff.behav. in norm_Rational_mg 23";
  1114 
  1115 (*------- Schalk I, p.68 Nr. 437a *)
  1116 val t = str2term "(3*a - 4*b) / (4*c+3*e)  *  (3*a+4*b)/(9*a^^^2 - 16*b^^^2)";
  1117 (* raises an exception for unclear reasons:
  1118 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1119 :
  1120 ###  rls: cancel_p on: (9 * a ^^^ 2 + -16 * b ^^^ 2) / (4 * c + 3 * e) /
  1121 (9 * a ^^^ 2 + -16 * b ^^^ 2) 
  1122 exception Div raised
  1123 
  1124 BUT
  1125 val t = str2term 
  1126   ("(9 * a ^^^ 2 + -16 * b ^^^ 2) / (4 * c + 3 * e) /" ^
  1127   "(9 * a ^^^ 2 + -16 * b ^^^ 2)");
  1128 NONE = cancel_p_ thy t;
  1129 
  1130 if term2str t = "1 / (4 * c + 3 * e)" then ()
  1131 else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1132 *)
  1133 
  1134 "----- S.K. corrected non-termination 060904";
  1135 val t = str2term "(3*a - 4*b) * (3*a+4*b)/((4*c+3*e)*(9*a^^^2 - 16*b^^^2))";
  1136 val SOME (t, _) = rewrite_set_ thy false make_polynomial t;
  1137 if term2str t = 
  1138   "(9 * a ^^^ 2 + -16 * b ^^^ 2) /\n(36 * a ^^^ 2 * c + 27 * a ^^^ 2 * e + -64 * b ^^^ 2 * c +\n -48 * b ^^^ 2 * e)"
  1139 (*"(9 * a ^^^ 2 + -16 * b ^^^ 2) / (36 * a ^^^ 2 * c + 27 * a ^^^ 2 * e + -64 * b ^^^ 2 * c + -48 * b ^^^ 2 * e)"*)
  1140 then () else error "rational.sml: S.K.8..corrected 060904-6";
  1141 
  1142 "----- S.K. corrected non-termination of cancel_p_";
  1143 val t'' = str2term ("(9 * a ^^^ 2 + -16 * b ^^^ 2) /" ^
  1144   "(36 * a^^^2 * c + (27 * a^^^2 * e + (-64 * b^^^2 * c + -48 * b^^^2 * e)))");
  1145 val SOME (t',_) = rewrite_set_ thy false cancel_p t'';
  1146 if term2str t' = "1 / (4 * c + 3 * e)"
  1147 then () else error "rational.sml: diff.behav. in cancel_p S.K.8";
  1148 
  1149 (*------- Schalk I, p.68 Nr. 437b*)
  1150 val t = str2term "(a + b)/(x^^^2 - y^^^2) * ((x - y)^^^2/(a^^^2 - b^^^2))";
  1151 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1152 :
  1153 ####  rls: cancel_p on: (a * x ^^^ 2 + -2 * (a * (x * y)) + a * y ^^^ 2 + b * x ^^^ 2 +
  1154  -2 * (b * (x * y)) +
  1155  b * y ^^^ 2) /
  1156 (a ^^^ 2 * x ^^^ 2 + -1 * (a ^^^ 2 * y ^^^ 2) + -1 * (b ^^^ 2 * x ^^^ 2) +
  1157  b ^^^ 2 * y ^^^ 2) 
  1158 exception Div raised
  1159 *)
  1160 
  1161 (*------- SRM Schalk I, p.68 Nr. 438a *)
  1162 val t = str2term "x*y / (x*y - y^^^2)  *  (x^^^2 - x*y)";
  1163 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1164 if term2str t = "x ^^^ 2"
  1165 then () else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1166 
  1167 (*------- SRM Schalk I, p.68 Nr. 439b *)
  1168 val t = str2term "(4*x^^^2 + 4*x + 1)  *  ((x^^^2 - 2*x^^^3) / (4*x^^^2 + 2*x))";
  1169 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1170 if term2str t = "(x + -4 * x ^^^ 3) / 2"
  1171 then () else error "rational.sml: diff.behav. in norm_Rational_mg 25";
  1172 
  1173 (*------- SRM Schalk I, p.68 Nr. 440a *)
  1174 val t = str2term "(x^^^2 - 2*x) / (x^^^2 - 3*x)  *  (x - 3)^^^2 / (x^^^2 - 4)";
  1175 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1176 if term2str t = "(-3 + x) / (2 + x)"
  1177 then () else error "rational.sml: diff.behav. in norm_Rational_mg 26";
  1178 
  1179 "----- Schalk I, p.68 Nr. 440b SK11 works since 0707xx";
  1180 val t = str2term "(a^^^3 - 9*a) / (a^^^3*b - a*b^^^3)  *  (a^^^2*b + a*b^^^2) / (a+3)";
  1181 (* WN130911 non-termination for unclear reasons:
  1182 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1183 
  1184 ... ENDS WITH THIS TRACE:
  1185 :
  1186 ###  rls: cancel_p on: (-9 * (a ^^^ 3 * b) + -9 * (a ^^^ 2 * b ^^^ 2) + a ^^^ 5 * b +
  1187  a ^^^ 4 * b ^^^ 2) /
  1188 (a ^^^ 3 * b + -1 * (a * b ^^^ 3)) /
  1189 (3 + a)
  1190 BUT THIS IS CORRECTLY RECOGNISED 
  1191 val t = str2term 
  1192   ("(-9 * (a^^^3 * b) + -9 * (a^^^2 * b^^^2) + a^^^5 * b + a^^^4 * b^^^2)  /" ^
  1193   "(a^^^3 * b + -1 * (a * b^^^3))  /  (3 + (a::real))");
  1194 AS
  1195 NONE = cancel_p_ thy t;
  1196 
  1197 if term2str t = "(-3 * a + a ^^^ 2) / (a + -1 * b)" then ()
  1198 else error "rational.sml: diff.behav. in norm_Rational 27";
  1199 *)
  1200 
  1201 "----- SK12 works since 0707xx";
  1202 val t = str2term "(a^^^3 - 9*a) * (a^^^2*b+a*b^^^2)  /  ((a^^^3*b - a*b^^^3) * (a+3))";
  1203 (* WN130911 non-termination due to non-termination of
  1204   cancel_p_ thy (str2term "(4 + -4 * b ^^^ 2) / (a ^^^ 4 + -2 * (a ^^^ 2 * b ^^^ 2) + b ^^^ 4)")
  1205 
  1206 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1207 if term2str t' = "(-3 * a + a ^^^ 2) / (a + -1 * b)" then ()
  1208 else error "rational.sml: diff.behav. in norm_Rational 28";
  1209 *)
  1210 
  1211 "-------- examples common denominator and multiplication from: Schalk --------";
  1212 "-------- examples common denominator and multiplication from: Schalk --------";
  1213 "-------- examples common denominator and multiplication from: Schalk --------";
  1214 (*------- SRAM Schalk I, p.69 Nr. 441b *)
  1215 val t = str2term "(4*a/3 + 3*b^^^2/a^^^3 + b/(4*a))*(4*b/(3*a))";
  1216 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1217 if term2str t = "(36 * b ^^^ 3 + 3 * a ^^^ 2 * b ^^^ 2 + 16 * a ^^^ 4 * b) / (9 * a ^^^ 4)"
  1218 then () else error "rational.sml: diff.behav. in norm_Rational_mg 28";
  1219 
  1220 (*------- SRAM Schalk I, p.69 Nr. 442b *)
  1221 val t = str2term ("(15*a^^^2/x^^^3 - 5*b^^^4/x^^^2 + 25*c^^^2/x) * " ^
  1222   "(x^^^3/(5*a*b^^^3*c^^^3)) + 1/c^^^3 * (b*x/a - 3*a/b^^^3)");
  1223 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1224 if term2str t = "5 * x ^^^ 2 / (a * b ^^^ 3 * c)"
  1225 then () else error "rational.sml: diff.behav. in norm_Rational_mg 29";
  1226 
  1227 (*------- SRAM Schalk I, p.69 Nr. 443b *)
  1228 val t = str2term "(a/2 + b/3) * (b/3 - a/2)";
  1229 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1230 if term2str t = "(-9 * a ^^^ 2 + 4 * b ^^^ 2) / 36"
  1231 then () else error "rational.sml: diff.behav. in norm_Rational_mg 30";
  1232 
  1233 (*------- SRAM Schalk I, p.69 Nr. 445b *)
  1234 val t = str2term "(a^^^2/9 + 2*a/(3*b) + 4/b^^^2)*(a/3 - 2/b) + 8/b^^^3";
  1235 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1236 if term2str t = "a ^^^ 3 / 27"
  1237 then () else error "rational.sml: diff.behav. in norm_Rational_mg 31";
  1238 
  1239 (*------- SRAM Schalk I, p.69 Nr. 446b *)
  1240 val t = str2term "(x/(5*x + 4*y) - y/(5*x - 4*y) + 1)*(25*x^^^2 - 16*y^^^2)";
  1241 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1242 if term2str t = "30 * x ^^^ 2 + -9 * x * y + -20 * y ^^^ 2"
  1243 then () else error "rational.sml: diff.behav. in norm_Rational_mg 32";
  1244 
  1245 (*------- SRAM Schalk I, p.69 Nr. 449a *)(*Achtung: rechnet ca 8 Sekunden*)
  1246 val t = str2term 
  1247 "(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)";
  1248 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1249 if term2str t = "(-81 * x ^^^ 4 + 16 * x ^^^ 8 * y ^^^ 4) / (81 * y ^^^ 8)"
  1250 then () else error "rational.sml: diff.behav. in norm_Rational_mg 33";
  1251 
  1252 (*------- SRAM Schalk I, p.69 Nr. 450a *)
  1253 val t = str2term 
  1254 "(4*x/(3*y)+2*y/(3*x))^^^2 - (2*y/(3*x) - 2*x/y)*(2*y/(3*x)+2*x/y)";
  1255 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1256 if term2str t = "(52 * x ^^^ 2 + 16 * y ^^^ 2) / (9 * y ^^^ 2)"
  1257 then () else error "rational.sml: diff.behav. in norm_Rational_mg 34";
  1258 
  1259 (*------- SRAM Schalk I, p.69 Nr. 442b --- abgewandelt*)
  1260 val t = str2term 
  1261   ("(15*a^^^4/(a*x^^^3)  -  5*a*((b^^^4 - 5*c^^^2*x) / x^^^2))  *  " ^
  1262   "(x^^^3/(5*a*b^^^3*c^^^3))   +   a/c^^^3 * (x*(b/a) - 3*b*(a/b^^^4))");
  1263 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1264 if term2str t = "5 * x ^^^ 2 / (b ^^^ 3 * c)"
  1265 then () else error "rational.sml: diff.behav. in norm_Rational_mg 53";
  1266 
  1267 
  1268 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1269 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1270 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1271 "----- SRD Schalk I, p.69 Nr. 454b";
  1272 val t = str2term "((2 - x)/(2*a)) / (2*a/(x - 2))";
  1273 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1274 if term2str t = "(-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2)"
  1275 then () else error "rational.sml: diff.behav. in norm_Rational_mg 35";
  1276 
  1277 "----- SRD Schalk I, p.69 Nr. 455a";
  1278 val t = str2term "(a^^^2 + 1)/(a^^^2 - 1) / ((a+1)/(a - 1))";
  1279 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1280 if term2str t = "(1 + a ^^^ 2) / (1 + 2 * a + a ^^^ 2)" then ()
  1281 else error "rational.sml: diff.behav. in norm_Rational_mg 36";
  1282 
  1283 "----- Schalk I, p.69 Nr. 455b";
  1284 val t = str2term "(x^^^2 - 4)/(y^^^2 - 9)/((2+x)/(3 - y))";
  1285 (* WN130911 non-termination due to non-termination of
  1286   cancel_p_ thy (str2term ("(-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /" ^
  1287                            "(-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)"))
  1288 
  1289 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1290 if term2str t = "(2 + -1 * x) / (3 + y)" then ()
  1291 else error "rational.sml: diff.behav. in norm_Rational_mg 37";
  1292 *)
  1293 
  1294 "----- SK060904-1a non-termination of cancel_p_ ?: worked before 0707xx";
  1295 val t = str2term "(x^^^2 - 4)*(3 - y) / ((y^^^2 - 9)*(2+x))";
  1296 (* WN130911 non-termination due to non-termination of
  1297   cancel_p_ thy (str2term ("(-12 + 4 * y + 3 * x ^^^ 2 + -1 * (x ^^^ 2 * y)) /" ^
  1298                            "(-18 + -9 * x + 2 * y ^^^ 2 + x * y ^^^ 2)"))
  1299 
  1300 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1301 if term2str t = "(2 + -1 * x) / (3 + y)" then ()
  1302 else error "rational.sml: diff.behav. in norm_Rational_mg 37b";
  1303 *)
  1304 
  1305 "----- ?: worked before 0707xx";
  1306 val t = str2term "(3 + -1 * y) / (-9 + y ^^^ 2)";
  1307 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1308 if term2str t = "-1 / (3 + y)"
  1309 then () else error "rational.sml: -1 / (3 + y) norm_Rational";
  1310 
  1311 "----- SRD Schalk I, p.69 Nr. 456b";
  1312 val t = str2term "(b^^^3 - b^^^2) / (b^^^2+b) / (b^^^2 - 1)";
  1313 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1314 if term2str t = "b / (1 + 2 * b + b ^^^ 2)"
  1315 then () else error "rational.sml: diff.behav. in norm_Rational_mg 38";
  1316 
  1317 "----- SRD Schalk I, p.69 Nr. 457b";
  1318 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))";
  1319 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1320 if term2str t = "8 * a ^^^ 2 + -6 * a * b + -12 * a ^^^ 2 * b + 9 * a * b ^^^ 2"
  1321 then () else error "rational.sml: diff.behav. in norm_Rational_mg 39";
  1322 
  1323 "----- Schalk I, p.69 Nr. 458b works since 0707";
  1324 val t = str2term "(2*a^^^2*x - a^^^2) / (a*x - b*x) / (b^^^2*(2*x - 1) / (x*(a - b)))";
  1325 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1326 :
  1327 ###  rls: cancel_p on: (-1 * a ^^^ 2 + 2 * (a ^^^ 2 * x)) / (a * x + -1 * (b * x)) /
  1328 ((-1 * b ^^^ 2 + 2 * (b ^^^ 2 * x)) / (a * x + -1 * (b * x))) 
  1329 exception Div raised
  1330 
  1331 BUT
  1332 val t = str2term 
  1333   ("(-1 * a ^^^ 2 + 2 * (a ^^^ 2 * x)) / (a * x + -1 * (b * x)) /" ^
  1334   "((-1 * b ^^^ 2 + 2 * (b ^^^ 2 * x)) / (a * x + -1 * (b * x)))");
  1335 NONE = cancel_p_ thy t;
  1336 
  1337 if term2str t = "a ^^^ 2 / b ^^^ 2" then ()
  1338 else error "rational.sml: diff.behav. in norm_Rational_mg 39b";
  1339 *)
  1340 
  1341 "----- SRD Schalk I, p.69 Nr. 459b";
  1342 val t = str2term "(a^^^2 - b^^^2)/(a*b) / (4*(a+b)^^^2/a)";
  1343 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1344 if term2str t = "(a + -1 * b) / (4 * a * b + 4 * b ^^^ 2)" then ()
  1345 else error "rational.sml: diff.behav. in norm_Rational_mg 41";
  1346 
  1347 "----- Schalk I, p.69 Nr. 460b nonterm.SK";
  1348 val t = str2term "(9*(x^^^2 - 8*x + 16) / (4*(y^^^2 - 2*y + 1))) / ((3*x - 12) / (16*y - 16))";
  1349 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1350 exception Div raised
  1351 
  1352 BUT
  1353 val t = str2term 
  1354   ("(144 + -72 * x + 9 * x ^^^ 2) / (4 + -8 * y + 4 * y ^^^ 2) /" ^
  1355   "((-12 + 3 * x) / (-16 + 16 * y))");
  1356 NONE = cancel_p_ thy t;
  1357 
  1358 if term2str t = !!!!!!!!!!!!!!!!!!!!!!!!!
  1359 then () else error "rational.sml: diff.behav. in norm_Rational_mg 42";
  1360 *)
  1361 
  1362 "----- some variant of the above; was non-terminating before";
  1363 val t = str2term "9*(x^^^2 - 8*x+16)*(16*y - 16)/(4*(y^^^2 - 2*y+1)*(3*x - 12))";
  1364 val SOME (t , _) = rewrite_set_ thy false norm_Rational t;
  1365 if term2str t = "(48 + -12 * x) / (1 + -1 * y)"
  1366 then () else error "some variant of the above; was non-terminating before";
  1367 
  1368 "----- SRD Schalk I, p.70 Nr. 472a";
  1369 val t = str2term ("((8*x^^^2 - 32*y^^^2) / (2*x + 4*y))  /  ((4*x - 8*y) / (x + y))");
  1370 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1371 if term2str t = "x + y"
  1372 then () else error "rational.sml: diff.behav. in norm_Rational_mg 43";
  1373 
  1374 "----- Schalk I, p.70 Nr. 478b ----- Rechenzeit: 5 sec";
  1375 val t = str2term ("(a - (a*b + b^^^2)/(a+b))/(b+(a - b)/(1+(a+b)/(a - b))) / " ^
  1376 		 "((a - a^^^2/(a+b))/(a+(a*b)/(a - b)))");
  1377 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1378 if term2str t = "(2 * a ^^^ 3 + 2 * a ^^^ 2 * b) / (a ^^^ 2 * b + b ^^^ 3)"
  1379 then () else error "rational.sml: diff.behav. in norm_Rational_mg 51";
  1380 
  1381 (*SRD Schalk I, p.69 Nr. 461a *)
  1382 val t = str2term "(2/(x+3) + 2/(x - 3)) / (8*x/(x^^^2 - 9))";
  1383 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1384 if term2str t = "1 / 2"
  1385 then () else error "rational.sml: diff.behav. in norm_Rational_mg 44";
  1386 
  1387 (*SRD Schalk I, p.69 Nr. 464b *)
  1388 val t = str2term "(a - a/(a - 2)) / (a + a/(a - 2))";
  1389 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1390 if term2str t = "(-3 + a) / (-1 + a)"
  1391 then () else error "rational.sml: diff.behav. in norm_Rational_mg 45";
  1392 
  1393 (*SRD Schalk I, p.69 Nr. 465b *)
  1394 val t = str2term "((x+3*y)/9 + (4*y^^^2 - 9*z^^^2)/(16*x))   /   (x/9 + y/6 + z/4)";
  1395 (* WN130911 non-termination due to non-termination of
  1396   cancel_p_ thy (str2term 
  1397     ("("(576 * x ^^^ 2 + 1728 * (x * y) + 1296 * y ^^^ 2 + -2916 * z ^^^ 2) /" ^
  1398       "(576 * x ^^^ 2 + 864 * (x * y) + 1296 * (x * z))"))
  1399 
  1400 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1401 if term2str t = "(4 * x + 6 * y + -9 * z) / (4 * x)"
  1402 then () else error "rational.sml: diff.behav. in norm_Rational_mg 46";
  1403 *)
  1404 
  1405 (*SRD Schalk I, p.69 Nr. 466b *)
  1406 val t = str2term "((1 - 7*(x - 2)/(x^^^2 - 4)) / (6/(x+2))) / (3/(x+5)+30/(x^^^2 - 25))";
  1407 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1408 if term2str t = "(25 + -10 * x + x ^^^ 2) / 18"
  1409 then () else error "rational.sml: diff.behav. in norm_Rational_mg 47";
  1410 
  1411 (*SRD Schalk I, p.70 Nr. 469 *)
  1412 val t = str2term ("3*b^^^2 / (4*a^^^2 - 8*a*b + 4*b^^^2) / " ^
  1413   "(a / (a^^^2*b - b^^^3)  +  (a - b) / (4*a*b^^^2 + 4*b^^^3)  -  1 / (4*b^^^2))");
  1414 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1415 if term2str t = "-3 * b ^^^ 3 / (-2 * a + 2 * b)"
  1416 then () else error "rational.sml: diff.behav. in norm_Rational_mg 48";
  1417 
  1418 "-------- me Schalk I No.186 -------------------------------------------------";
  1419 "-------- me Schalk I No.186 -------------------------------------------------";
  1420 "-------- me Schalk I No.186 -------------------------------------------------";
  1421 val fmz = ["Term ((14 * x * y) / ( x * y ))", "normalform N"];
  1422 val (dI',pI',mI') =
  1423   ("Rational",["rational","simplification"],
  1424    ["simplification","of_rationals"]);
  1425 val p = e_pos'; val c = []; 
  1426 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
  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;
  1433 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  1434 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
  1435 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;
  1436 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;(*++ for explicit script*)
  1437 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;(*++ for explicit script*)
  1438 case (f2str f, nxt) of
  1439     ("14", ("End_Proof'", _)) => ()
  1440   | _ => error "rational.sml diff.behav. in me Schalk I No.186";
  1441 
  1442 "-------- interSteps ..Simp_Rat_Double_No-1.xml ------------------------------";
  1443 "-------- interSteps ..Simp_Rat_Double_No-1.xml ------------------------------";
  1444 "-------- interSteps ..Simp_Rat_Double_No-1.xml ------------------------------";
  1445 states:=[];
  1446 CalcTree [(["Term (((2 - x)/(2*a)) / (2*a/(x - 2)))", "normalform N"], 
  1447   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1448 Iterator 1;
  1449 moveActiveRoot 1;
  1450 autoCalculate 1 CompleteCalc;
  1451 val ((pt, p), _) = get_calc 1; 
  1452 (*
  1453 show_pt pt;
  1454 [
  1455 (([], Frm), Simplify ((2 - x) / (2 * a) / (2 * a / (x - 2)))),
  1456 (([1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1457 (([1], Res), (2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1458 (([2], Res), (2 + -1 * x) / (2 * a) / (2 * a / (-2 + x))),
  1459 (([3], Res), (2 + -1 * x) * (-2 + x) / (2 * a * (2 * a))),
  1460 (([4], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2)),
  1461 (([], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2))] 
  1462 *)
  1463 interSteps 1 ([1], Res);
  1464 val ((pt, p), _) = get_calc 1; 
  1465 (*show_pt pt;
  1466 [
  1467 (([], Frm), Simplify ((2 - x) / (2 * a) / (2 * a / (x - 2)))),
  1468 (([1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1469 (([1,1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1470 (([1,1], Res), (2 - x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1471 (([1,2], Res), (2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1472 (([1], Res), (2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))),
  1473 (([2], Res), (2 + -1 * x) / (2 * a) / (2 * a / (-2 + x))),
  1474 (([3], Res), (2 + -1 * x) * (-2 + x) / (2 * a * (2 * a))),
  1475 (([4], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2)),
  1476 (([], Res), (-4 + 4 * x + -1 * x ^^^ 2) / (4 * a ^^^ 2))] 
  1477 *)
  1478 val (t, asm) = get_obj g_result pt [1, 1];
  1479 if term2str t = "(2 - x) / (2 * a) / (2 * a / (x + -1 * 2))" andalso terms2str asm = "[]"
  1480 then () else error "2nd interSteps ..Simp_Rat_Double_No-1 changed on [1, 1]";
  1481 val (t, asm) = get_obj g_result pt [1, 2];
  1482 if term2str t = "(2 + -1 * x) / (2 * a) / (2 * a / (x + -1 * 2))" andalso terms2str asm = "[]"
  1483 then () else error "3rd interSteps ..Simp_Rat_Double_No-1 changed on [1, 2]";
  1484 
  1485 
  1486 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ------------------------------";
  1487 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ------------------------------";
  1488 "-------- interSteps ..Simp_Rat_Cancel_No-1.xml ------------------------------";
  1489 states:=[];
  1490 CalcTree [(["Term ((a^2 + -1*b^2) / (a^2 + -2*a*b + b^2))", "normalform N"], 
  1491   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1492 Iterator 1;
  1493 moveActiveRoot 1;
  1494 autoCalculate 1 CompleteCalc;
  1495 val ((pt, p), _) = get_calc 1;
  1496 (*show_pt pt;
  1497 [
  1498 (([], Frm), Simplify ((a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2))),
  1499 (([1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2)),
  1500 (([1], Res), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1501 (([2], Res), (a + b) / (a + -1 * b)),
  1502 (([], Res), (a + b) / (a + -1 * b))] 
  1503 *)
  1504 interSteps 1 ([2], Res);
  1505 val ((pt, p), _) = get_calc 1;
  1506 (*show_pt pt;
  1507 [
  1508 (([], Frm), Simplify ((a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2))),
  1509 (([1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2)),
  1510 (([1], Res), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1511 (([2,1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1512 (([2,1], Res), (a + b) / (a + -1 * b)),
  1513 (([2], Res), (a + b) / (a + -1 * b)),
  1514 (([], Res), (a + b) / (a + -1 * b))] 
  1515 *)
  1516 interSteps 1 ([2,1],Res);
  1517 val ((pt, p), _) = get_calc 1; 
  1518 (*show_pt pt;
  1519 [
  1520 (([], Frm), Simplify ((a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2))),
  1521 (([1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * a * b + b ^^^ 2)),
  1522 (([1], Res), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1523 (([2,1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1524 (([2,1,1], Frm), (a ^^^ 2 + -1 * b ^^^ 2) / (a ^^^ 2 + -2 * (a * b) + b ^^^ 2)),
  1525 (([2,1,1], Res), (a ^^^ 2 + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1526 (a ^^^ 2 + -2 * (a * b) + 1 * b ^^^ 2)),
  1527 (([2,1,2], Res), (a ^^^ 2 + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1528 (a ^^^ 2 + -2 * (a * b) + -1 ^^^ 2 * b ^^^ 2)),
  1529 (([2,1,3], Res), (a ^^^ 2 + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1530 (a ^^^ 2 + -2 * (a * b) + (-1 * b) ^^^ 2)),
  1531 (([2,1,4], Res), (a * a + -1 * (a * b) + a * b + -1 * b ^^^ 2) /
  1532 (a ^^^ 2 + -2 * (a * b) + (-1 * b) ^^^ 2)),
  1533 (([2,1,5], Res), (a * a + -1 * (a * b) + a * b + -1 * (b * b)) /
  1534 (a ^^^ 2 + -2 * (a * b) + (-1 * b) ^^^ 2)),
  1535 (([2,1,6], Res), (a * a + -1 * (a * b) + a * b + -1 * (b * b)) /
  1536 (a ^^^ 2 + -1 * (2 * (a * b)) + (-1 * b) ^^^ 2)),
  1537 (([2,1,7], Res), (a * a + a * (-1 * b) + (b * a + b * (-1 * b))) /
  1538 (a ^^^ 2 + 2 * (a * (-1 * b)) + (-1 * b) ^^^ 2)),
  1539 (([2,1,8], Res), (a * a + a * (-1 * b) + (b * a + b * (-1 * b))) /
  1540 (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1541 (([2,1,9], Res), (a * (a + -1 * b) + (b * a + b * (-1 * b))) /
  1542 (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1543 (([2,1,10], Res), (a * (a + -1 * b) + b * (a + -1 * b)) /
  1544 (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1545 (([2,1,11], Res), (a + b) * (a + -1 * b) / (a ^^^ 2 + 2 * a * (-1 * b) + (-1 * b) ^^^ 2)),
  1546 (([2,1,12], Res), (a + b) * (a + -1 * b) / ((a + -1 * b) * (a + -1 * b))),
  1547 (([2,1,13], Res), (a + b) / (a + -1 * b)),
  1548 (([2,1], Res), (a + b) / (a + -1 * b)),
  1549 (([2], Res), (a + b) / (a + -1 * b)),
  1550 (([], Res), (a + b) / (a + -1 * b))] 
  1551 *)
  1552 val newnds = children (get_nd pt [2,1]) (*see "fun detailrls"*);
  1553 if length newnds = 13 then () else error "rational.sml: interSteps cancel_p rev_rew_p";
  1554 
  1555 val p = ([2,1,9],Res);
  1556 getTactic 1 p;
  1557 val (_, tac, _) = pt_extract (pt, p);
  1558 case tac of SOME (Rewrite ("sym_distrib_left", _)) => ()
  1559 | _ => error "rational.sml: getTactic, sym_real_plus_binom_times1";
  1560 
  1561 
  1562 "-------- investigate rulesets for cancel_p ----------------------------------";
  1563 "-------- investigate rulesets for cancel_p ----------------------------------";
  1564 "-------- investigate rulesets for cancel_p ----------------------------------";
  1565 val thy = @{theory "Rational"};
  1566 val t = str2term "(a^^^2 + -1*b^^^2) / (a^^^2 + -2*a*b + b^^^2)";
  1567 val tt = str2term "(1 * a + 1 * b) * (1 * a + -1 * b)"(*numerator only*);
  1568 
  1569 "----- with rewrite_set_";
  1570 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1571 if term2str tt'= "a ^^^ 2 + -1 * b ^^^ 2" then () else error "rls chancel_p 1";
  1572 val tt = str2term "((1 * a + -1 * b) * (1 * a + -1 * b))"(*denominator only*);
  1573 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1574 if term2str tt' = "a ^^^ 2 + -2 * a * b + b ^^^ 2" then () else error "rls chancel_p 2";
  1575 
  1576 "----- with make_deriv; WN1130912 not investigated further, will be discontinued";
  1577 val SOME (tt, _) = factout_p_ thy t; 
  1578 if term2str tt = "(a + b) * (a + -1 * b) / ((a + -1 * b) * (a + -1 * b))"
  1579 then () else error "rls chancel_p 3";
  1580 term2str tt = "(1 * a + 1 * b) * (1 * a + -1 * b) / ((1 * a + -1 * b) * (1 * a + -1 * b))";
  1581 
  1582 "--- with simpler ruleset";
  1583 val {rules, rew_ord= (_, ro), ...} = rep_rls (assoc_rls "rev_rew_p");
  1584 val der = make_deriv thy Atools_erls rules ro NONE tt;
  1585 if length der = 12 then () else error "WN1130912 rls chancel_p 4";
  1586 print_depth 99; writeln (deriv2str der); print_depth 3;
  1587 
  1588 print_depth 99; map (term2str o #1) der; print_depth 3;
  1589 "...,(-1 * b ^^^ 2 + a ^^^ 2) / (-2 * (a * b) + a ^^^ 2 + (-1 * b) ^^^ 2) ]";
  1590 print_depth 99; map (rule2str o #2) der; print_depth 3;
  1591 print_depth 99; map (term2str o #1 o #3) der; print_depth 3;
  1592 
  1593 val der = make_deriv thy Atools_erls rules ro NONE 
  1594 	(str2term "(1 * a + 1 * b) * (1 * a + -1 * b)");
  1595 print_depth 99; writeln (deriv2str der); print_depth 3;
  1596 
  1597 val {rules, rew_ord=(_,ro),...} = rep_rls (assoc_rls "rev_rew_p");
  1598 val der = make_deriv thy Atools_erls rules ro NONE 
  1599 	(str2term "(1 * a + -1 * b) * (1 * a + -1 * b)");
  1600 print_depth 99; writeln (deriv2str der); print_depth 3;
  1601 print_depth 99; map (term2str o #1) der; print_depth 3;
  1602 (*WN060829 ...postponed*)
  1603 
  1604 
  1605 "-------- fun eval_get_denominator -------------------------------------------";
  1606 "-------- fun eval_get_denominator -------------------------------------------";
  1607 "-------- fun eval_get_denominator -------------------------------------------";
  1608 val thy = @{theory Isac};
  1609 val t = term_of (the (parse thy "get_denominator ((a +x)/b)"));
  1610 val SOME (_, t') = eval_get_denominator "" 0 t thy;
  1611 if term2str t' = "get_denominator ((a + x) / b) = b"
  1612 then () else error "get_denominator ((a + x) / b) = b"
  1613 
  1614 
  1615 "-------- several errpats in complicated term --------------------------------";
  1616 "-------- several errpats in complicated term --------------------------------";
  1617 "-------- several errpats in complicated term --------------------------------";
  1618 (*WN12xxxx TODO: instead of Gabriella's example here (27.Jul.12) find a simpler one
  1619   WN130912: kept this test, although not clear what for*)
  1620 states := [];
  1621 CalcTree [(["Term ((5*b + 25)/(a^2 - b^2) * (a - b)/(5*b))", "normalform N"], 
  1622   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1623 Iterator 1;
  1624 moveActiveRoot 1;
  1625 autoCalculate 1 CompleteCalc;
  1626 val ((pt, p), _) = get_calc 1;
  1627 (*show_pt pt;
  1628 [
  1629 (([], Frm), Simplify ((5 * b + 25) / (a ^^^ 2 - b ^^^ 2) * (a - b) / (5 * b))),
  1630 (([1], Frm), (5 * b + 25) / (a ^^^ 2 - b ^^^ 2) * (a - b) / (5 * b)),
  1631 (([1], Res), (5 * b + 25) / (a ^^^ 2 + -1 * b ^^^ 2) * (a + -1 * b) / (5 * b)),
  1632 (([2], Res), (5 * b + 25) * (a + -1 * b) / (a ^^^ 2 + -1 * b ^^^ 2) / (5 * b)),
  1633 (([3], Res), (25 * a + -25 * b + 5 * (a * b) + -5 * b ^^^ 2) / (a ^^^ 2 + -1 * b ^^^ 2) /
  1634 (5 * b)),
  1635 (([4], Res), (25 + 5 * b) / (a + b) / (5 * b)),
  1636 (([5], Res), (25 + 5 * b) / ((a + b) * (5 * b))),
  1637 (([6], Res), (25 + 5 * b) / (5 * (a * b) + 5 * b ^^^ 2)),
  1638 (([7], Res), (5 + b) / (a * b + b ^^^ 2)),
  1639 (([], Res), (5 + b) / (a * b + b ^^^ 2))] *)
  1640 
  1641 
  1642 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1643 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1644 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1645 (*------- Schalk I, p.70 Nr. 480b; a/b : c/d translated to a/b * d/c*)
  1646 val t = str2term 
  1647   ("((12*x*y / (9*x^^^2 - y^^^2))  /  (1 / (3*x - y)^^^2 - 1 / (3*x + y)^^^2))  *  " ^
  1648 	"((1/(x - 5*y)^^^2  -  1/(x + 5*y)^^^2)  /  (20*x*y / (x^^^2 - 25*y^^^2)))");
  1649 
  1650 (*1st factor separately simplified *)
  1651 val t = str2term "((12*x*y / (9*x^^^2 - y^^^2))  /  (1 / (3*x - y)^^^2 - 1 / (3*x + y)^^^2))";
  1652 val SOME (t', _) = rewrite_set_ thy false norm_Rational t; 
  1653 if term2str t' = "(-9 * x ^^^ 2 + y ^^^ 2) / -1" then () else error "Nr. 480b lhs changed";
  1654 (*2nd factor separately simplified *)
  1655 val t = str2term "((1/(x - 5*y)^^^2  -  1/(x + 5*y)^^^2)  /  (20*x*y / (x^^^2 - 25*y^^^2)))";
  1656 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; term2str t';
  1657 if term2str t' = "-1 / (-1 * x ^^^ 2 + 25 * y ^^^ 2)" then () else error "Nr. 480b rhs changed";
  1658 
  1659 "-------- Schalk I, p.70 Nr. 477a: terms are exploding ?!?";
  1660 val t = str2term ("b*y/(b - 2*y)/((b^^^2 - y^^^2)/(b+2*y))  /" ^
  1661 		 "(b^^^2*y + b*y^^^2) * (a+x)^^^2  /  ((b^^^2 - 4*y^^^2) * (a+2*x)^^^2)");
  1662 (*val SOME (t',_) = rewrite_set_ thy false norm_Rational t;
  1663 :
  1664 ###  rls: cancel_p on: (a ^^^ 2 * (b * y) + 2 * (a * (b * (x * y))) + b * (x ^^^ 2 * y)) /
  1665 (b + -2 * y) /
  1666 ((b ^^^ 2 + -1 * y ^^^ 2) / (b + 2 * y)) /
  1667 (b ^^^ 2 * y + b * y ^^^ 2) /
  1668 (a ^^^ 2 * b ^^^ 2 + -4 * (a ^^^ 2 * y ^^^ 2) + 4 * (a * (b ^^^ 2 * x)) +
  1669  -16 * (a * (x * y ^^^ 2)) +
  1670  4 * (b ^^^ 2 * x ^^^ 2) +
  1671  -16 * (x ^^^ 2 * y ^^^ 2)) 
  1672 exception Div raised
  1673 
  1674 BUT
  1675 val t = str2term 
  1676   ("(a ^^^ 2 * (b * y) + 2 * (a * (b * (x * y))) + b * (x ^^^ 2 * y)) /" ^
  1677   "(b + -2 * y) /" ^
  1678   "((b ^^^ 2 + -1 * y ^^^ 2) / (b + 2 * y)) /" ^
  1679   "(b ^^^ 2 * y + b * y ^^^ 2) /" ^
  1680   "(a ^^^ 2 * b ^^^ 2 + -4 * (a ^^^ 2 * y ^^^ 2) + 4 * (a * (b ^^^ 2 * x)) +" ^
  1681   "-16 * (a * (x * y ^^^ 2)) +" ^
  1682   "4 * (b ^^^ 2 * x ^^^ 2) +" ^
  1683   "-16 * (x ^^^ 2 * y ^^^ 2))");
  1684 NONE = cancel_p_ thy t;
  1685 *)
  1686 
  1687 (*------- Schalk I, p.70 Nr. 476b in 2003 this worked using 10 sec. *)
  1688 val t = str2term 
  1689   ("((a^^^2 - b^^^2)/(2*a*b) + 2*a*b/(a^^^2 - b^^^2))  /  ((a^^^2 + b^^^2)/(2*a*b) + 1)    / " ^
  1690    "((a^^^2 + b^^^2)^^^2  /  (a + b)^^^2)");
  1691 (*
  1692 trace_rewrite := true;
  1693 rewrite_set_ thy false norm_Rational t;
  1694 :
  1695 ####  rls: cancel_p on: (2 * (a ^^^ 7 * b) + 4 * (a ^^^ 6 * b ^^^ 2) + 6 * (a ^^^ 5 * b ^^^ 3) +
  1696  8 * (a ^^^ 4 * b ^^^ 4) +
  1697  6 * (a ^^^ 3 * b ^^^ 5) +
  1698  4 * (a ^^^ 2 * b ^^^ 6) +
  1699  2 * (a * b ^^^ 7)) /
  1700 (2 * (a ^^^ 9 * b) + 4 * (a ^^^ 8 * b ^^^ 2) +
  1701  2 * (2 * (a ^^^ 7 * b ^^^ 3)) +
  1702  4 * (a ^^^ 6 * b ^^^ 4) +
  1703  -4 * (a ^^^ 4 * b ^^^ 6) +
  1704  -4 * (a ^^^ 3 * b ^^^ 7) +
  1705  -4 * (a ^^^ 2 * b ^^^ 8) +
  1706  -2 * (a * b ^^^ 9))
  1707 
  1708 if term2str t = "1 / (a ^^^ 2 + -1 * b ^^^ 2)" then ()
  1709 else error "rational.sml: diff.behav. in norm_Rational_mg 49";
  1710 *)
  1711 
  1712 "-------- Schalk I, p.70 Nr. 480a: terms are exploding ?!?";
  1713 val t = str2term ("(1/x + 1/y + 1/z)  /  (1/x - 1/y - 1/z)  /  " ^
  1714   "(2*x^^^2 / (x^^^2 - z^^^2) / (x / (x + z)  +  x / (x - z)))");
  1715 (*
  1716 trace_rewrite := true;
  1717 rewrite_set_ thy false norm_Rational t;
  1718 :
  1719 ####  rls: cancel_p on: (2 * (x ^^^ 6 * (y ^^^ 2 * z)) + 2 * (x ^^^ 6 * (y * z ^^^ 2)) +
  1720  2 * (x ^^^ 5 * (y ^^^ 2 * z ^^^ 2)) +
  1721  -2 * (x ^^^ 4 * (y ^^^ 2 * z ^^^ 3)) +
  1722  -2 * (x ^^^ 4 * (y * z ^^^ 4)) +
  1723  -2 * (x ^^^ 3 * (y ^^^ 2 * z ^^^ 4))) /
  1724 (-2 * (x ^^^ 6 * (y ^^^ 2 * z)) + -2 * (x ^^^ 6 * (y * z ^^^ 2)) +
  1725  2 * (x ^^^ 5 * (y ^^^ 2 * z ^^^ 2)) +
  1726  2 * (x ^^^ 4 * (y ^^^ 2 * z ^^^ 3)) +
  1727  2 * (x ^^^ 4 * (y * z ^^^ 4)) +
  1728  -2 * (x ^^^ 3 * (y ^^^ 2 * z ^^^ 4)))
  1729 *)
  1730 
  1731 "-------- Schalk I, p.60 Nr. 215d: terms are exploding, internal loop does not terminate";
  1732 val t = str2term "(a-b)^^^3 * (x+y)^^^4 / ((x+y)^^^2 * (a-b)^^^5)";
  1733 (* Kein Wunder, denn Z???ler und Nenner extra als Polynom dargestellt ergibt:
  1734 
  1735 val t = str2term "(a-b)^^^3 * (x+y)^^^4";
  1736 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1737 term2str t;
  1738 "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";
  1739 val t = str2term "((x+y)^^^2 * (a-b)^^^5)";
  1740 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1741 term2str t;
  1742 "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";
  1743 
  1744 anscheinend macht dem Rechner das Krzen diese Bruches keinen Spass mehr ...*)
  1745 
  1746 "-------- Schalk I, p.70 Nr. 480b: terms are exploding, trace_rewrite stops at";
  1747 val t = str2term ("((12*x*y/(9*x^^^2 - y^^^2))/" ^
  1748 		 "(1/(3*x - y)^^^2 - 1/(3*x + y)^^^2)) *" ^
  1749 		 "(1/(x - 5*y)^^^2 - 1/(x + 5*y)^^^2)/" ^
  1750 		 "(20*x*y/(x^^^2 - 25*y^^^2))");
  1751 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1752 :
  1753 ####  rls: cancel_p on: (19440 * (x ^^^ 8 * y ^^^ 2) + -490320 * (x ^^^ 6 * y ^^^ 4) +
  1754  108240 * (x ^^^ 4 * y ^^^ 6) +
  1755  -6000 * (x ^^^ 2 * y ^^^ 8)) /
  1756 (2160 * (x ^^^ 8 * y ^^^ 2) + -108240 * (x ^^^ 6 * y ^^^ 4) +
  1757  1362000 * (x ^^^ 4 * y ^^^ 6) +
  1758  -150000 * (x ^^^ 2 * y ^^^ 8))
  1759 *)
  1760