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