test/Tools/isac/Knowledge/rational-2.sml
author wneuper <walther.neuper@jku.at>
Sat, 17 Jul 2021 14:05:28 +0200
changeset 60329 0c10aeff57d7
parent 60327 464109593df0
child 60330 e5e9a6c45597
permissions -rw-r--r--
replace "-*" by "- *" for numerals "*" 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 ("HOL.False", _), []) = 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;
   382 val SOME (t', _) = Rewrite.rewrite_set_ thy true add_fractions_p t;
   383 Rewrite.trace_on := 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;
   425 val SOME (t', _) = Rewrite.rewrite_set_ thy true add_fractions_p t;
   426 Rewrite.trace_on := 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 ("Partial_Fractions.AA", _), Const ("Partial_Fractions.BB", _)] =>
   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 ("Free ("AA", "real") *)
   467 
   468 val SOME (t', _) = rewrite_set_ thy true cancel_p t;
   469 case t' of
   470   Const ("Rings.divide_class.divide", _) $ Const ("Partial_Fractions.AA", _) $
   471     Const ("Groups.one_class.one", _) => ()
   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 ("Partial_Fractions.AA", _)] => ()
   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;
   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:=true;*)
   884 val t = TermC.str2term "Not (6*x is_atom)";
   885 val SOME (t',_) = rewrite_set_ thy false powers_erls t; UnparseC.term t';
   886 "HOL.True";
   887 val t = TermC.str2term "1 < 2";
   888 val SOME (t',_) = rewrite_set_ thy false powers_erls t; UnparseC.term t';
   889 "HOL.True";
   890 
   891 val t = TermC.str2term "(6*x) \<up> 2";
   892 val SOME (t',_) = rewrite_ thy dummy_ord powers_erls false 
   893 			   (ThmC.numerals_to_Free @{thm realpow_def_atom}) t;
   894 if UnparseC.term t' = "6 * x * (6 * x) \<up> (2 + - 1)" then ()
   895 else error "rational.sml powers_erls (6*x) \<up> 2";
   896 
   897 val t = TermC.str2term "- 1 * (- 2 * (5 / 2 * (13 * x / 2)))";
   898 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; UnparseC.term t';
   899 if UnparseC.term t' = "65 * x / 2" then () else error "rational.sml 4";
   900 
   901 val t = TermC.str2term "1 - ((13*x)/2 - 5/2) \<up> 2";
   902 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; UnparseC.term t';
   903 if UnparseC.term t' = "(- 21 + 130 * x + - 169 * x \<up> 2) / 4" then () 
   904 else error "rational.sml 5";
   905 
   906 (*SRAM Schalk I, p.92 Nr. 609a*)
   907 val t = TermC.str2term "2*(3 - x/5)/3 - 4*(1 - x/3) - x/3 - 2*(x/2 - 1/4)/27 +5/54";
   908 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; UnparseC.term t';
   909 if UnparseC.term t' = "(- 255 + 112 * x) / 135" then () 
   910 else error "rational.sml 6";
   911 
   912 (*SRAM Schalk I, p.92 Nr. 610c*)
   913 val t = TermC.str2term "((x- 1)/(x+1) + 1) / ((x- 1)/(x+1) - (x+1)/(x- 1)) - 2";
   914 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; UnparseC.term t';
   915 if UnparseC.term t' = "(3 + x) / - 2" then () else error "rational.sml 7";
   916 
   917 (*SRAM Schalk I, p.92 Nr. 476a*)
   918 val t = TermC.str2term "(x \<up> 2/(1 - x \<up> 2) + 1)/(x/(1 - x) + 1) * (1 + x)";
   919 (*. a/b : c/d translated to a/b * d/c .*)
   920 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; UnparseC.term t';
   921 if UnparseC.term t' = "1" then () else error "rational.sml 8";
   922 
   923 (*Schalk I, p.92 Nr. 472a*)
   924 val t = TermC.str2term "((8*x \<up> 2 - 32*y \<up> 2)/(2*x + 4*y))/((4*x - 8*y)/(x + y))";
   925 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; UnparseC.term t';
   926 if UnparseC.term t' = "x + y" then () else error "rational.sml p.92 Nr. 472a";
   927 
   928 (*Schalk I, p.70 Nr. 480b: SEE rational.sml --- nonterminating rls norm_Rational ---*)
   929 
   930 (*WN130910 add_fractions_p exception Div raised + history:
   931 ### WN.2.6.03 from rlang.sml 56a 
   932 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)";
   933 val NONE = rewrite_set_ thy false add_fractions_p t;
   934 
   935 THE ERROR ALREADY OCCURS IN THIS PART:
   936 val t = TermC.str2term "(a + b * x) / (a + - 1 * (b * x)) + (- 1 * a + b * x) / (a + b * x)";
   937 val NONE = add_fraction_p_ thy t;
   938 
   939 SEE Test_Some.thy: section {* add_fractions_p downto exception Div raised ===
   940 *)
   941 
   942 "-------- rational numerals --------------------------------------------------";
   943 "-------- rational numerals --------------------------------------------------";
   944 "-------- rational numerals --------------------------------------------------";
   945 (*SRA Schalk I, p.40 Nr. 164b *)
   946 val t = TermC.str2term "(47/6 - 76/9 + 13/4)/(35/12)";
   947 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   948 if UnparseC.term t = "19 / 21" then ()
   949 else error "rational.sml: diff.behav. in norm_Rational_mg 1";
   950 
   951 (*SRA Schalk I, p.40 Nr. 166a *)
   952 val t = TermC.str2term "((5/4)/(4+22/7) + 37/20)*(110/3 - 110/9 * 23/11)";
   953 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   954 if UnparseC.term t = "45 / 2" then ()
   955 else error "rational.sml: diff.behav. in norm_Rational_mg 2";
   956 
   957 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   958 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   959 "-------- examples cancellation from: Mathematik 1 Schalk --------------------";
   960 (* e190c Stefan K.*)
   961 val t = TermC.str2term "((1 + 9*a \<up> 2) * (1 + 3*a)) / ((3*a + 9*a \<up> 2) * (1 + 3*a))";
   962 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   963 if UnparseC.term t = "(1 + 9 * a \<up> 2) / (3 * a + 9 * a \<up> 2)"
   964 then () else error "rational.sml: diff.behav. in norm_Rational_mg 3";
   965 
   966 (* e192b Stefan K.*)
   967 val t = TermC.str2term "(x \<up> 2 * (7*x + (- 1)*y))  /  (y \<up> 2 * (7*x + (- 1)*y))";
   968 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   969 if UnparseC.term t = "x \<up> 2 / y \<up> 2"
   970 then () else error "rational.sml: diff.behav. in norm_Rational_mg 4";
   971 
   972 (*SRC Schalk I, p.66 Nr. 379c *)
   973 val t = TermC.str2term "(a - b)/(b - a)";
   974 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   975 if UnparseC.term t = "- 1"
   976 then () else error "rational.sml: diff.behav. in norm_Rational_mg 5";
   977 
   978 (*SRC Schalk I, p.66 Nr. 380b *)
   979 val t = TermC.str2term "15*(3*x + 3) * (4*x + 9)  /  (12*(2*x + 7) * (5*x + 5))";
   980 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   981 if UnparseC.term t = "(27 + 12 * x) / (28 + 8 * x)"
   982 then () else error "rational.sml: diff.behav. in norm_Rational_mg 6";
   983 
   984 (* e190c Stefan K.*)
   985 val t = TermC.str2term "((1 + 9*a \<up> 2) * (1 + 3*a))  /  ((3*a + 9*a \<up> 2) * (1 + 3 * a))";
   986 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   987 if UnparseC.term t =  "(1 + 9 * a \<up> 2) / (3 * a + 9 * a \<up> 2)"
   988 then () else error "rational.sml: diff.behav. in norm_Rational_mg 3";
   989 
   990 (* e192b Stefan K.*)
   991 val t = TermC.str2term "(x \<up> 2 * (7*x + (- 1)*y))  /  (y \<up> 2 * (7*x + (- 1)*y))";
   992 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   993 if UnparseC.term t = "x \<up> 2 / y \<up> 2"
   994 then () else error "rational.sml: diff.behav. in norm_Rational_mg 4";
   995 
   996 (*SRC Schalk I, p.66 Nr. 379c *)
   997 val t = TermC.str2term "(a - b) / (b - a)";
   998 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
   999 if UnparseC.term t = "- 1"
  1000 then () else error "rational.sml: diff.behav. in norm_Rational_mg 5";
  1001 
  1002 (*SRC Schalk I, p.66 Nr. 380b *)
  1003 val t = TermC.str2term "15*(3*x + 3) * (4*x + 9)  /  (12*(2*x + 7) * (5*x + 5))";
  1004 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1005 if UnparseC.term t = "(27 + 12 * x) / (28 + 8 * x)"
  1006 then () else error "rational.sml: diff.behav. in norm_Rational_mg 6";
  1007 
  1008 (* extreme example from somewhere *)
  1009 val t = TermC.str2term 
  1010     ("(a \<up> 4 * x  +  - 1*a \<up> 4 * y  +  4*a \<up> 3 * b * x  +  -4*a \<up> 3 * b * y  + " ^
  1011       "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  + " ^
  1012       "b \<up> 4 * x  +  - 1*b \<up> 4 * y) " ^
  1013   " / (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 + " ^
  1014       "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 + " ^
  1015       "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)")
  1016 val SOME (t, _) = rewrite_set_ thy false cancel_p t;
  1017 if UnparseC.term t = "(a \<up> 2 + 2 * a * b + b \<up> 2) / (x \<up> 2 + - 2 * x * y + y \<up> 2)"
  1018 then () else error "with Isabelle2002: NONE -- now SOME changed";
  1019 
  1020 (*Schalk I, p.66 Nr. 381a *)
  1021 (* ATTENTION: here the rls is very slow. In Isabelle2002 this required 2 min *)
  1022 val t = TermC.str2term "18*(a + b) \<up> 3 * (a - b) \<up> 2 / (72*(a - b) \<up> 3 * (a + b) \<up> 2)";
  1023 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1024 if UnparseC.term t = "(a + b) / (4 * a + - 4 * b)"
  1025 then () else error "rational.sml: diff.behav. in norm_Rational_mg 8";
  1026 
  1027 (*SRC Schalk I, p.66 Nr. 381b *)
  1028 val t = TermC.str2term "(4*x \<up> 2 - 20*x + 25) / (2*x - 5) \<up> 3";
  1029 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1030 if UnparseC.term t = "1 / (- 5 + 2 * x)"
  1031 then () else error "rational.sml: diff.behav. in norm_Rational_mg 9";
  1032 
  1033 (*SRC Schalk I, p.66 Nr. 381c *)
  1034 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)";
  1035 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1036 if UnparseC.term t = "(1 + 9 * a \<up> 2) / (3 * a + 9 * a \<up> 2)"
  1037 then () else error "rational.sml: diff.behav. in norm_Rational_mg 10";
  1038 
  1039 (*SRC Schalk I, p.66 Nr. 383a *)
  1040 val t = TermC.str2term "(5*a \<up> 2 - 5*a*b) / (a - b) \<up> 2";
  1041 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1042 if UnparseC.term t = "- 5 * a / (- 1 * a + b)"
  1043 then () else error "rational.sml: diff.behav. in norm_Rational_mg 11";
  1044 
  1045 "----- NOT TERMINATING ?: worked before 0707xx";
  1046 val t = TermC.str2term "(a \<up> 2 - 1)*(b + 1) / ((b \<up> 2 - 1)*(a+1))";
  1047 (* WN130911 "exception Div raised" by 
  1048   cancel_p_ thy (TermC.str2term ("(- 1 + - 1 * b + a \<up> 2 + a \<up> 2 * b) /" ^
  1049                            "(- 1 + - 1 * a + b \<up> 2 + a * b \<up> 2)"))
  1050 
  1051 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1052 if UnparseC.term t = "(1 + - 1 * a) / (1 + - 1 * b)" then ()
  1053 else error "rational.sml MG tests 3e";
  1054 *)
  1055 
  1056 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1057 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1058 "-------- examples common denominator from: Mathematik 1 Schalk --------------";
  1059 (*SRA Schalk I, p.67 Nr. 403a *)
  1060 val t = TermC.str2term "4/x - 3/y - 1";
  1061 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1062 if UnparseC.term t = "(- 3 * x + 4 * y + - 1 * x * y) / (x * y)"
  1063 then () else error "rational.sml: diff.behav. in norm_Rational_mg 12";
  1064 
  1065 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)";
  1066 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1067 if UnparseC.term t = "4 / c"
  1068 then () else error "rational.sml: diff.behav. in norm_Rational_mg 13";
  1069 
  1070 (*SRA Schalk I, p.67 Nr. 410b *)
  1071 val t = TermC.str2term "1/(x+1) + 1/(x+2) - 2/(x+3)";
  1072 (* WN130911 non-termination due to non-termination of
  1073   cancel_p_ thy (TermC.str2term "(5 + 3 * x) / (6 + 11 * x + 6 * x \<up> 2 + x \<up> 3)")
  1074 
  1075 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1076 if UnparseC.term t = "(5 + 3 * x) / (6 + 11 * x + 6 * x \<up> 2 + x \<up> 3)"
  1077 then () else error "rational.sml: diff.behav. in norm_Rational_mg 14";
  1078 *)
  1079 
  1080 (*SRA Schalk I, p.67 Nr. 413b *)
  1081 val t = TermC.str2term "(1 + x)/(1 - x)  -  (1 - x)/(1 + x)  +  2*x/(1 - x \<up> 2)";
  1082 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1083 if UnparseC.term t = "6 * x / (1 + - 1 * x \<up> 2)"
  1084 then () else error "rational.sml: diff.behav. in norm_Rational_mg 15";
  1085 
  1086 (*SRA Schalk I, p.68 Nr. 414a *)
  1087 val t = TermC.str2term "(x + 2)/(x - 1)  +  (x - 3)/(x - 2)  -  (x + 1)/((x - 1)*(x - 2))";
  1088 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1089 if UnparseC.term t ="(- 2 + - 5 * x + 2 * x \<up> 2) / (2 + - 3 * x + x \<up> 2)"
  1090 then () else error "rational.sml: diff.behav. in norm_Rational_mg 16";
  1091 
  1092 (*SRA Schalk I, p.68 Nr. 428b *)
  1093 val t = TermC.str2term 
  1094   "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";
  1095 (* WN130911 non-termination due to non-termination of
  1096   cancel_p_ thy (TermC.str2term "(4 + -4 * b \<up> 2) / (a \<up> 4 + - 2 * (a \<up> 2 * b \<up> 2) + b \<up> 4)")
  1097 
  1098 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1099 if UnparseC.term t = "4 / (a \<up> 4 + - 2 * a \<up> 2 * b \<up> 2 + b \<up> 4)"
  1100 then () else error "rational.sml: diff.behav. in norm_Rational_mg 18";
  1101 *)
  1102 
  1103 (*SRA Schalk I, p.68 Nr. 430b *)
  1104 val t = TermC.str2term 
  1105   "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";
  1106 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1107 if UnparseC.term t = "a + 3 * b"
  1108 then () else error "rational.sml: diff.behav. in norm_Rational_mg 19";
  1109 
  1110 (*SRA Schalk I, p.68 Nr. 432 *)
  1111 val t = TermC.str2term 
  1112   ("(a \<up> 2 + a*b) / (a \<up> 2 - b \<up> 2)  -  (b \<up> 2 - a*b) / (b \<up> 2 - a \<up> 2)  +  " ^
  1113   "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)  -  " ^
  1114   "2*b \<up> 2 / (a \<up> 2 - b \<up> 2)");
  1115 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1116 if UnparseC.term t = (*"0" ..isabisac15 | Isabelle2017..*)  "0 / (a \<up> 2 + - 1 * b \<up> 2)"
  1117 then () else error "rational.sml: diff.behav. in norm_Rational_mg 20";
  1118 
  1119 (* some example *)
  1120 val t = TermC.str2term "3*a / (a*b)  +  x/y";
  1121 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1122 if UnparseC.term t = "(3 * y + b * x) / (b * y)"
  1123 then () else error "rational.sml: diff.behav. in norm_Rational_mg 21";
  1124 
  1125 
  1126 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1127 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1128 "-------- examples multiply and cancel from: Mathematik 1 Schalk -------------";
  1129 (*------- SRM Schalk I, p.68 Nr. 436a *)
  1130 val t = TermC.str2term "3*(x+y) / (15*(x - y))  *   25*(x - y) \<up> 2 / (18*(x + y) \<up> 2)";
  1131 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1132 if UnparseC.term t = "(- 5 * x + 5 * y) / (- 18 * x + - 18 * y)"
  1133 then () else error "rational.sml: diff.behav. in norm_Rational_mg 22";
  1134 
  1135 (*------- SRM.test Schalk I, p.68 Nr. 436b *)
  1136 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";
  1137 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1138 if UnparseC.term t = "5 * a / (a + - 1 * b)"
  1139 then () else error "rational.sml: diff.behav. in norm_Rational_mg 23";
  1140 
  1141 (*------- Schalk I, p.68 Nr. 437a *)
  1142 val t = TermC.str2term "(3*a - 4*b) / (4*c+3*e)  *  (3*a+4*b)/(9*a \<up> 2 - 16*b \<up> 2)";
  1143 (* raises an exception for unclear reasons:
  1144 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1145 :
  1146 ###  rls: cancel_p on: (9 * a \<up> 2 + - 16 * b \<up> 2) / (4 * c + 3 * e) /
  1147 (9 * a \<up> 2 + - 16 * b \<up> 2) 
  1148 exception Div raised
  1149 
  1150 BUT
  1151 val t = TermC.str2term 
  1152   ("(9 * a \<up> 2 + - 16 * b \<up> 2) / (4 * c + 3 * e) /" ^
  1153   "(9 * a \<up> 2 + - 16 * b \<up> 2)");
  1154 NONE = cancel_p_ thy t;
  1155 
  1156 if UnparseC.term t = "1 / (4 * c + 3 * e)" then ()
  1157 else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1158 *)
  1159 
  1160 "----- S.K. corrected non-termination 060904";
  1161 val t = TermC.str2term "(3*a - 4*b) * (3*a+4*b)/((4*c+3*e)*(9*a \<up> 2 - 16*b \<up> 2))";
  1162 val SOME (t, _) = rewrite_set_ thy false make_polynomial t;
  1163 if UnparseC.term t = 
  1164   "(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)"
  1165 then () else error "rational.sml: S.K.8..corrected 060904-6";
  1166 
  1167 "----- S.K. corrected non-termination of cancel_p_";
  1168 val t'' = TermC.str2term ("(9 * a \<up> 2 + - 16 * b \<up> 2) /" ^
  1169   "(36 * a \<up> 2 * c + (27 * a \<up> 2 * e + (-64 * b \<up> 2 * c + -48 * b \<up> 2 * e)))");
  1170 (* /--- DOES NOT TERMINATE AT TRANSITION isabisac15 --> Isabelle2017 --------------------------\
  1171 val SOME (t',_) = rewrite_set_ thy false cancel_p t'';
  1172 if UnparseC.term t' = "1 / (4 * c + 3 * e)"
  1173 then () else error "rational.sml: diff.behav. in cancel_p S.K.8";
  1174    \--- DOES NOT TERMINATE AT TRANSITION isabisac15 --> Isabelle2017 --------------------------/*)
  1175 
  1176 (*------- Schalk I, p.68 Nr. 437b*)
  1177 val t = TermC.str2term "(a + b)/(x \<up> 2 - y \<up> 2) * ((x - y) \<up> 2/(a \<up> 2 - b \<up> 2))";
  1178 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1179 :
  1180 ####  rls: cancel_p on: (a * x \<up> 2 + - 2 * (a * (x * y)) + a * y \<up> 2 + b * x \<up> 2 +
  1181  - 2 * (b * (x * y)) +
  1182  b * y \<up> 2) /
  1183 (a \<up> 2 * x \<up> 2 + - 1 * (a \<up> 2 * y \<up> 2) + - 1 * (b \<up> 2 * x \<up> 2) +
  1184  b \<up> 2 * y \<up> 2) 
  1185 exception Div raised
  1186 *)
  1187 
  1188 (*------- SRM Schalk I, p.68 Nr. 438a *)
  1189 val t = TermC.str2term "x*y / (x*y - y \<up> 2)  *  (x \<up> 2 - x*y)";
  1190 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1191 if UnparseC.term t = "x \<up> 2"
  1192 then () else error "rational.sml: diff.behav. in norm_Rational_mg 24";
  1193 
  1194 (*------- SRM Schalk I, p.68 Nr. 439b *)
  1195 val t = TermC.str2term "(4*x \<up> 2 + 4*x + 1)  *  ((x \<up> 2 - 2*x \<up> 3) / (4*x \<up> 2 + 2*x))";
  1196 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1197 if UnparseC.term t = "(x + - 4 * x \<up> 3) / 2"
  1198 then () else error "rational.sml: diff.behav. in norm_Rational_mg 25";
  1199 
  1200 (*------- SRM Schalk I, p.68 Nr. 440a *)
  1201 val t = TermC.str2term "(x \<up> 2 - 2*x) / (x \<up> 2 - 3*x)  *  (x - 3) \<up> 2 / (x \<up> 2 - 4)";
  1202 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1203 if UnparseC.term t = "(- 3 + x) / (2 + x)"
  1204 then () else error "rational.sml: diff.behav. in norm_Rational_mg 26";
  1205 
  1206 "----- Schalk I, p.68 Nr. 440b SK11 works since 0707xx";
  1207 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)";
  1208 (* WN130911 non-termination for unclear reasons:
  1209 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1210 
  1211 ... ENDS WITH THIS TRACE:
  1212 :
  1213 ###  rls: cancel_p on: (-9 * (a \<up> 3 * b) + -9 * (a \<up> 2 * b \<up> 2) + a \<up> 5 * b +
  1214  a \<up> 4 * b \<up> 2) /
  1215 (a \<up> 3 * b + - 1 * (a * b \<up> 3)) /
  1216 (3 + a)
  1217 BUT THIS IS CORRECTLY RECOGNISED 
  1218 val t = TermC.str2term 
  1219   ("(-9 * (a \<up> 3 * b) + -9 * (a \<up> 2 * b \<up> 2) + a \<up> 5 * b + a \<up> 4 * b \<up> 2)  /" ^
  1220   "(a \<up> 3 * b + - 1 * (a * b \<up> 3))  /  (3 + (a::real))");
  1221 AS
  1222 NONE = cancel_p_ thy t;
  1223 
  1224 if UnparseC.term t = "(-3 * a + a \<up> 2) / (a + - 1 * b)" then ()
  1225 else error "rational.sml: diff.behav. in norm_Rational 27";
  1226 *)
  1227 
  1228 "----- SK12 works since 0707xx";
  1229 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))";
  1230 (* WN130911 non-termination due to non-termination of
  1231   cancel_p_ thy (TermC.str2term "(4 + -4 * b \<up> 2) / (a \<up> 4 + - 2 * (a \<up> 2 * b \<up> 2) + b \<up> 4)")
  1232 
  1233 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1234 if UnparseC.term t' = "(-3 * a + a \<up> 2) / (a + - 1 * b)" then ()
  1235 else error "rational.sml: diff.behav. in norm_Rational 28";
  1236 *)
  1237 
  1238 "-------- examples common denominator and multiplication from: Schalk --------";
  1239 "-------- examples common denominator and multiplication from: Schalk --------";
  1240 "-------- examples common denominator and multiplication from: Schalk --------";
  1241 (*------- SRAM Schalk I, p.69 Nr. 441b *)
  1242 val t = TermC.str2term "(4*a/3 + 3*b \<up> 2/a \<up> 3 + b/(4*a))*(4*b/(3*a))";
  1243 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1244 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)"
  1245 then () else error "rational.sml: diff.behav. in norm_Rational_mg 28";
  1246 
  1247 (*------- SRAM Schalk I, p.69 Nr. 442b *)
  1248 val t = TermC.str2term ("(15*a \<up> 2/x \<up> 3 - 5*b \<up> 4/x \<up> 2 + 25*c \<up> 2/x) * " ^
  1249   "(x \<up> 3/(5*a*b \<up> 3*c \<up> 3)) + 1/c \<up> 3 * (b*x/a - 3*a/b \<up> 3)");
  1250 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1251 if UnparseC.term t = "5 * x \<up> 2 / (a * b \<up> 3 * c)"
  1252 then () else error "rational.sml: diff.behav. in norm_Rational_mg 29";
  1253 
  1254 (*------- SRAM Schalk I, p.69 Nr. 443b *)
  1255 val t = TermC.str2term "(a/2 + b/3) * (b/3 - a/2)";
  1256 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1257 if UnparseC.term t = "(- 9 * a \<up> 2 + 4 * b \<up> 2) / 36"
  1258 then () else error "rational.sml: diff.behav. in norm_Rational_mg 30";
  1259 
  1260 (*------- SRAM Schalk I, p.69 Nr. 445b *)
  1261 val t = TermC.str2term "(a \<up> 2/9 + 2*a/(3*b) + 4/b \<up> 2)*(a/3 - 2/b) + 8/b \<up> 3";
  1262 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1263 if UnparseC.term t = "a \<up> 3 / 27"
  1264 then () else error "rational.sml: diff.behav. in norm_Rational_mg 31";
  1265 
  1266 (*------- SRAM Schalk I, p.69 Nr. 446b *)
  1267 val t = TermC.str2term "(x/(5*x + 4*y) - y/(5*x - 4*y) + 1)*(25*x \<up> 2 - 16*y \<up> 2)";
  1268 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1269 if UnparseC.term t = (*"30 * x \<up> 2 + -9 * x * y + - 20 * y \<up> 2" ..isabisac15 | Isabelle2017..*)
  1270                   "(- 30 * x \<up> 2 + 9 * x * y + 20 * y \<up> 2) / - 1"
  1271 then () else error "rational.sml: diff.behav. in norm_Rational_mg 32";
  1272 
  1273 (*------- SRAM Schalk I, p.69 Nr. 449a *)(*Achtung: rechnet ca 8 Sekunden*)
  1274 val t = TermC.str2term 
  1275 "(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)";
  1276 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1277 if UnparseC.term t = "(- 81 * x \<up> 4 + 16 * x \<up> 8 * y \<up> 4) / (81 * y \<up> 8)"
  1278 then () else error "rational.sml: diff.behav. in norm_Rational_mg 33";
  1279 
  1280 (*------- SRAM Schalk I, p.69 Nr. 450a *)
  1281 val t = TermC.str2term 
  1282 "(4*x/(3*y)+2*y/(3*x)) \<up> 2 - (2*y/(3*x) - 2*x/y)*(2*y/(3*x)+2*x/y)";
  1283 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1284 if UnparseC.term t = "(52 * x \<up> 2 + 16 * y \<up> 2) / (9 * y \<up> 2)"
  1285 then () else error "rational.sml: diff.behav. in norm_Rational_mg 34";
  1286 
  1287 (*------- SRAM Schalk I, p.69 Nr. 442b --- abgewandelt*)
  1288 val t = TermC.str2term 
  1289   ("(15*a \<up> 4/(a*x \<up> 3)  -  5*a*((b \<up> 4 - 5*c \<up> 2*x) / x \<up> 2))  *  " ^
  1290   "(x \<up> 3/(5*a*b \<up> 3*c \<up> 3))   +   a/c \<up> 3 * (x*(b/a) - 3*b*(a/b \<up> 4))");
  1291 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1292 if UnparseC.term t = "5 * x \<up> 2 / (b \<up> 3 * c)"
  1293 then () else error "rational.sml: diff.behav. in norm_Rational_mg 53";
  1294 
  1295 
  1296 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1297 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1298 "-------- examples double fractions from: Mathematik 1 Schalk ----------------";
  1299 "----- SRD Schalk I, p.69 Nr. 454b";
  1300 val t = TermC.str2term "((2 - x)/(2*a)) / (2*a/(x - 2))";
  1301 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1302 if UnparseC.term t = "(- 4 + 4 * x + - 1 * x \<up> 2) / (4 * a \<up> 2)"
  1303 then () else error "rational.sml: diff.behav. in norm_Rational_mg 35";
  1304 
  1305 "----- SRD Schalk I, p.69 Nr. 455a";
  1306 val t = TermC.str2term "(a \<up> 2 + 1)/(a \<up> 2 - 1) / ((a+1)/(a - 1))";
  1307 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1308 if UnparseC.term t = "(1 + a \<up> 2) / (1 + 2 * a + a \<up> 2)" then ()
  1309 else error "rational.sml: diff.behav. in norm_Rational_mg 36";
  1310 
  1311 "----- Schalk I, p.69 Nr. 455b";
  1312 val t = TermC.str2term "(x \<up> 2 - 4)/(y \<up> 2 - 9)/((2+x)/(3 - y))";
  1313 (* WN130911 non-termination due to non-termination of
  1314   cancel_p_ thy (TermC.str2term ("(- 12 + 4 * y + 3 * x \<up> 2 + - 1 * (x \<up> 2 * y)) /" ^
  1315                            "(- 18 + -9 * x + 2 * y \<up> 2 + x * y \<up> 2)"))
  1316 
  1317 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1318 if UnparseC.term t = "(2 + - 1 * x) / (3 + y)" then ()
  1319 else error "rational.sml: diff.behav. in norm_Rational_mg 37";
  1320 *)
  1321 
  1322 "----- SK060904- 1a non-termination of cancel_p_ ?: worked before 0707xx";
  1323 val t = TermC.str2term "(x \<up> 2 - 4)*(3 - y) / ((y \<up> 2 - 9)*(2+x))";
  1324 (* WN130911 non-termination due to non-termination of
  1325   cancel_p_ thy (TermC.str2term ("(- 12 + 4 * y + 3 * x \<up> 2 + - 1 * (x \<up> 2 * y)) /" ^
  1326                            "(- 18 + -9 * x + 2 * y \<up> 2 + x * y \<up> 2)"))
  1327 
  1328 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1329 if UnparseC.term t = "(2 + - 1 * x) / (3 + y)" then ()
  1330 else error "rational.sml: diff.behav. in norm_Rational_mg 37b";
  1331 *)
  1332 
  1333 "----- ?: worked before 0707xx";
  1334 val t = TermC.str2term "(3 + - 1 * y) / (-9 + y \<up> 2)";
  1335 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1336 if UnparseC.term t = "- 1 / (3 + y)"
  1337 then () else error "rational.sml: - 1 / (3 + y) norm_Rational";
  1338 
  1339 "----- SRD Schalk I, p.69 Nr. 456b";
  1340 val t = TermC.str2term "(b \<up> 3 - b \<up> 2) / (b \<up> 2+b) / (b \<up> 2 - 1)";
  1341 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1342 if UnparseC.term t = "b / (1 + 2 * b + b \<up> 2)"
  1343 then () else error "rational.sml: diff.behav. in norm_Rational_mg 38";
  1344 
  1345 "----- SRD Schalk I, p.69 Nr. 457b";
  1346 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))";
  1347 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1348 if UnparseC.term t = "8 * a \<up> 2 + - 6 * a * b + - 12 * a \<up> 2 * b + 9 * a * b \<up> 2"
  1349 then () else error "rational.sml: diff.behav. in norm_Rational_mg 39";
  1350 
  1351 "----- Schalk I, p.69 Nr. 458b works since 0707";
  1352 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)))";
  1353 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1354 :
  1355 ###  rls: cancel_p on: (- 1 * a \<up> 2 + 2 * (a \<up> 2 * x)) / (a * x + - 1 * (b * x)) /
  1356 ((- 1 * b \<up> 2 + 2 * (b \<up> 2 * x)) / (a * x + - 1 * (b * x))) 
  1357 exception Div raised
  1358 
  1359 BUT
  1360 val t = TermC.str2term 
  1361   ("(- 1 * a \<up> 2 + 2 * (a \<up> 2 * x)) / (a * x + - 1 * (b * x)) /" ^
  1362   "((- 1 * b \<up> 2 + 2 * (b \<up> 2 * x)) / (a * x + - 1 * (b * x)))");
  1363 NONE = cancel_p_ thy t;
  1364 
  1365 if UnparseC.term t = "a \<up> 2 / b \<up> 2" then ()
  1366 else error "rational.sml: diff.behav. in norm_Rational_mg 39b";
  1367 *)
  1368 
  1369 "----- SRD Schalk I, p.69 Nr. 459b";
  1370 val t = TermC.str2term "(a \<up> 2 - b \<up> 2)/(a*b) / (4*(a+b) \<up> 2/a)";
  1371 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1372 if UnparseC.term t = "(a + - 1 * b) / (4 * a * b + 4 * b \<up> 2)" then ()
  1373 else error "rational.sml: diff.behav. in norm_Rational_mg 41";
  1374 
  1375 "----- Schalk I, p.69 Nr. 460b nonterm.SK";
  1376 val t = TermC.str2term "(9*(x \<up> 2 - 8*x + 16) / (4*(y \<up> 2 - 2*y + 1))) / ((3*x - 12) / (16*y - 16))";
  1377 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1378 exception Div raised
  1379 
  1380 BUT
  1381 val t = TermC.str2term 
  1382   ("(144 + -72 * x + 9 * x \<up> 2) / (4 + -8 * y + 4 * y \<up> 2) /" ^
  1383   "((- 12 + 3 * x) / (- 16 + 16 * y))");
  1384 NONE = cancel_p_ thy t;
  1385 
  1386 if UnparseC.term t = !!!!!!!!!!!!!!!!!!!!!!!!!
  1387 then () else error "rational.sml: diff.behav. in norm_Rational_mg 42";
  1388 *)
  1389 
  1390 "----- some variant of the above; was non-terminating before";
  1391 val t = TermC.str2term "9*(x \<up> 2 - 8*x+16)*(16*y - 16)/(4*(y \<up> 2 - 2*y+1)*(3*x - 12))";
  1392 val SOME (t , _) = rewrite_set_ thy false norm_Rational t;
  1393 if UnparseC.term t = "(48 + - 12 * x) / (1 + - 1 * y)"
  1394 then () else error "some variant of the above; was non-terminating before";
  1395 
  1396 "----- SRD Schalk I, p.70 Nr. 472a";
  1397 val t = TermC.str2term ("((8*x \<up> 2 - 32*y \<up> 2) / (2*x + 4*y))  /  ((4*x - 8*y) / (x + y))");
  1398 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1399 if UnparseC.term t = "x + y"
  1400 then () else error "rational.sml: diff.behav. in norm_Rational_mg 43";
  1401 
  1402 "----- Schalk I, p.70 Nr. 478b ----- Rechenzeit: 5 sec";
  1403 val t = TermC.str2term ("(a - (a*b + b \<up> 2)/(a+b))/(b+(a - b)/(1+(a+b)/(a - b))) / " ^
  1404 		 "((a - a \<up> 2/(a+b))/(a+(a*b)/(a - b)))");
  1405 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1406 if UnparseC.term t = "(2 * a \<up> 3 + 2 * a \<up> 2 * b) / (a \<up> 2 * b + b \<up> 3)"
  1407 then () else error "rational.sml: diff.behav. in norm_Rational_mg 51";
  1408 
  1409 (*SRD Schalk I, p.69 Nr. 461a *)
  1410 val t = TermC.str2term "(2/(x+3) + 2/(x - 3)) / (8*x/(x \<up> 2 - 9))";
  1411 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1412 if UnparseC.term t = "1 / 2"
  1413 then () else error "rational.sml: diff.behav. in norm_Rational_mg 44";
  1414 
  1415 (*SRD Schalk I, p.69 Nr. 464b *)
  1416 val t = TermC.str2term "(a - a/(a - 2)) / (a + a/(a - 2))";
  1417 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1418 if UnparseC.term t = "(- 3 + a) / (- 1 + a)"
  1419 then () else error "rational.sml: diff.behav. in norm_Rational_mg 45";
  1420 
  1421 (*SRD Schalk I, p.69 Nr. 465b *)
  1422 val t = TermC.str2term "((x+3*y)/9 + (4*y \<up> 2 - 9*z \<up> 2)/(16*x))   /   (x/9 + y/6 + z/4)";
  1423 (* WN130911 non-termination due to non-termination of
  1424   cancel_p_ thy (TermC.str2term 
  1425     ("("(576 * x \<up> 2 + 1728 * (x * y) + 1296 * y \<up> 2 + - 2916 * z \<up> 2) /" ^
  1426       "(576 * x \<up> 2 + 864 * (x * y) + 1296 * (x * z))"))
  1427 
  1428 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1429 if UnparseC.term t = "(4 * x + 6 * y + -9 * z) / (4 * x)"
  1430 then () else error "rational.sml: diff.behav. in norm_Rational_mg 46";
  1431 *)
  1432 
  1433 (*SRD Schalk I, p.69 Nr. 466b *)
  1434 val t = TermC.str2term "((1 - 7*(x - 2)/(x \<up> 2 - 4)) / (6/(x+2))) / (3/(x+5)+30/(x \<up> 2 - 25))";
  1435 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1436 if UnparseC.term t = "(25 + - 10 * x + x \<up> 2) / 18"
  1437 then () else error "rational.sml: diff.behav. in norm_Rational_mg 47";
  1438 
  1439 (*SRD Schalk I, p.70 Nr. 469 *)
  1440 val t = TermC.str2term ("3*b \<up> 2 / (4*a \<up> 2 - 8*a*b + 4*b \<up> 2) / " ^
  1441   "(a / (a \<up> 2*b - b \<up> 3)  +  (a - b) / (4*a*b \<up> 2 + 4*b \<up> 3)  -  1 / (4*b \<up> 2))");
  1442 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1443 if UnparseC.term t = "- 3 * b \<up> 3 / (- 2 * a + 2 * b)"
  1444 then () else error "rational.sml: diff.behav. in norm_Rational_mg 48";
  1445 
  1446 (*//----------------------------------TOODOO (*Rfuns revsets \<longrightarrow> broken*)
  1447 "-------- me Schalk I No.186 -------------------------------------------------";
  1448 "-------- me Schalk I No.186 -------------------------------------------------";
  1449 "-------- me Schalk I No.186 -------------------------------------------------";
  1450 val fmz = ["Term ((14 * x * y) / ( x * y ))", "normalform N"];
  1451 val (dI',pI',mI') =
  1452   ("Rational",["rational", "simplification"],
  1453    ["simplification", "of_rationals"]);
  1454 val p = e_pos'; val c = []; 
  1455 val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
  1456 val (p,_,f,nxt,_,pt) = me nxt p c pt;
  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; f2str f;
  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;(*++ for explicit script*)
  1466 val (p,_,f,nxt,_,pt) = me nxt p c pt; f2str f;(*++ for explicit script*)
  1467 case (f2str f, nxt) of
  1468     ("14", ("End_Proof'", _)) => ()
  1469   | _ => error "rational.sml diff.behav. in me Schalk I No.186";
  1470   \\----------------------------------TOODOO (*Rfuns revsets \<longrightarrow> broken*)*)
  1471 
  1472 (*//----------------------------------TOODOO (*Rfuns revsets \<longrightarrow> broken*)
  1473 "-------- interSteps ..Simp_Rat_Double_No- 1.xml ------------------------------";
  1474 "-------- interSteps ..Simp_Rat_Double_No- 1.xml ------------------------------";
  1475 "-------- interSteps ..Simp_Rat_Double_No- 1.xml ------------------------------";
  1476 reset_states ();
  1477 CalcTree [(["Term (((2 - x)/(2*a)) / (2*a/(x - 2)))", "normalform N"], 
  1478   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1479 Iterator 1;
  1480 moveActiveRoot 1;
  1481 autoCalculate 1 CompleteCalc;
  1482 val ((pt, p), _) = get_calc 1; 
  1483 (*
  1484 Test_Tool.show_pt pt;
  1485 [
  1486 (([], Frm), Simplify ((2 - x) / (2 * a) / (2 * a / (x - 2)))),
  1487 (([1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1488 (([1], Res), (2 + - 1 * x) / (2 * a) / (2 * a / (x + - 1 * 2))),
  1489 (([2], Res), (2 + - 1 * x) / (2 * a) / (2 * a / (- 2 + x))),
  1490 (([3], Res), (2 + - 1 * x) * (- 2 + x) / (2 * a * (2 * a))),
  1491 (([4], Res), (-4 + 4 * x + - 1 * x \<up> 2) / (4 * a \<up> 2)),
  1492 (([], Res), (-4 + 4 * x + - 1 * x \<up> 2) / (4 * a \<up> 2))] 
  1493 *)
  1494 interSteps 1 ([1], Res);
  1495 val ((pt, p), _) = get_calc 1; 
  1496 (*Test_Tool.show_pt pt;
  1497 [
  1498 (([], Frm), Simplify ((2 - x) / (2 * a) / (2 * a / (x - 2)))),
  1499 (([1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1500 (([1,1], Frm), (2 - x) / (2 * a) / (2 * a / (x - 2))),
  1501 (([1,1], Res), (2 - x) / (2 * a) / (2 * a / (x + - 1 * 2))),
  1502 (([1,2], Res), (2 + - 1 * x) / (2 * a) / (2 * a / (x + - 1 * 2))),
  1503 (([1], Res), (2 + - 1 * x) / (2 * a) / (2 * a / (x + - 1 * 2))),
  1504 (([2], Res), (2 + - 1 * x) / (2 * a) / (2 * a / (- 2 + x))),
  1505 (([3], Res), (2 + - 1 * x) * (- 2 + x) / (2 * a * (2 * a))),
  1506 (([4], Res), (-4 + 4 * x + - 1 * x \<up> 2) / (4 * a \<up> 2)),
  1507 (([], Res), (-4 + 4 * x + - 1 * x \<up> 2) / (4 * a \<up> 2))] 
  1508 *)
  1509 val (t, asm) = get_obj g_result pt [1, 1];
  1510 if UnparseC.term t = "(2 - x) / (2 * a) / (2 * a / (x + - 1 * 2))" andalso UnparseC.terms asm = "[]"
  1511 then () else error "2nd interSteps ..Simp_Rat_Double_No- 1 changed on [1, 1]";
  1512 val (t, asm) = get_obj g_result pt [1, 2];
  1513 if UnparseC.term t = "(2 + - 1 * x) / (2 * a) / (2 * a / (x + - 1 * 2))" andalso UnparseC.terms asm = "[]"
  1514 then () else error "3rd interSteps ..Simp_Rat_Double_No- 1 changed on [1, 2]";
  1515   \\----------------------------------TOODOO (*Rfuns revsets \<longrightarrow> broken*)*)
  1516 
  1517 
  1518 (*//----------------------------------TOODOO (*Rfuns revsets \<longrightarrow> broken*)
  1519 "-------- interSteps ..Simp_Rat_Cancel_No- 1.xml ------------------------------";
  1520 "-------- interSteps ..Simp_Rat_Cancel_No- 1.xml ------------------------------";
  1521 "-------- interSteps ..Simp_Rat_Cancel_No- 1.xml ------------------------------";
  1522 reset_states ();
  1523 CalcTree [(["Term ((a^2 + - 1*b^2) / (a^2 + - 2*a*b + b^2))", "normalform N"], 
  1524   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1525 Iterator 1;
  1526 moveActiveRoot 1;
  1527 autoCalculate 1 CompleteCalc;
  1528 val ((pt, p), _) = get_calc 1;
  1529 (*Test_Tool.show_pt pt;
  1530 [
  1531 (([], Frm), Simplify ((a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * a * b + b \<up> 2))),
  1532 (([1], Frm), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * a * b + b \<up> 2)),
  1533 (([1], Res), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * (a * b) + b \<up> 2)),
  1534 (([2], Res), (a + b) / (a + - 1 * b)),
  1535 (([], Res), (a + b) / (a + - 1 * b))] 
  1536 *)
  1537 interSteps 1 ([2], Res);
  1538 val ((pt, p), _) = get_calc 1;
  1539 (*Test_Tool.show_pt pt;
  1540 [
  1541 (([], Frm), Simplify ((a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * a * b + b \<up> 2))),
  1542 (([1], Frm), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * a * b + b \<up> 2)),
  1543 (([1], Res), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * (a * b) + b \<up> 2)),
  1544 (([2,1], Frm), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * (a * b) + b \<up> 2)),
  1545 (([2,1], Res), (a + b) / (a + - 1 * b)),
  1546 (([2], Res), (a + b) / (a + - 1 * b)),
  1547 (([], Res), (a + b) / (a + - 1 * b))] 
  1548 *)
  1549 interSteps 1 ([2,1],Res);
  1550 val ((pt, p), _) = get_calc 1; 
  1551 (*Test_Tool.show_pt pt;
  1552 [
  1553 (([], Frm), Simplify ((a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * a * b + b \<up> 2))),
  1554 (([1], Frm), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * a * b + b \<up> 2)),
  1555 (([1], Res), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * (a * b) + b \<up> 2)),
  1556 (([2,1], Frm), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * (a * b) + b \<up> 2)),
  1557 (([2,1,1], Frm), (a \<up> 2 + - 1 * b \<up> 2) / (a \<up> 2 + - 2 * (a * b) + b \<up> 2)),
  1558 (([2,1,1], Res), (a \<up> 2 + - 1 * (a * b) + a * b + - 1 * b \<up> 2) /
  1559 (a \<up> 2 + - 2 * (a * b) + 1 * b \<up> 2)),
  1560 (([2,1,2], Res), (a \<up> 2 + - 1 * (a * b) + a * b + - 1 * b \<up> 2) /
  1561 (a \<up> 2 + - 2 * (a * b) + - 1 \<up> 2 * b \<up> 2)),
  1562 (([2,1,3], Res), (a \<up> 2 + - 1 * (a * b) + a * b + - 1 * b \<up> 2) /
  1563 (a \<up> 2 + - 2 * (a * b) + (- 1 * b) \<up> 2)),
  1564 (([2,1,4], Res), (a * a + - 1 * (a * b) + a * b + - 1 * b \<up> 2) /
  1565 (a \<up> 2 + - 2 * (a * b) + (- 1 * b) \<up> 2)),
  1566 (([2,1,5], Res), (a * a + - 1 * (a * b) + a * b + - 1 * (b * b)) /
  1567 (a \<up> 2 + - 2 * (a * b) + (- 1 * b) \<up> 2)),
  1568 (([2,1,6], Res), (a * a + - 1 * (a * b) + a * b + - 1 * (b * b)) /
  1569 (a \<up> 2 + - 1 * (2 * (a * b)) + (- 1 * b) \<up> 2)),
  1570 (([2,1,7], Res), (a * a + a * (- 1 * b) + (b * a + b * (- 1 * b))) /
  1571 (a \<up> 2 + 2 * (a * (- 1 * b)) + (- 1 * b) \<up> 2)),
  1572 (([2,1,8], Res), (a * a + a * (- 1 * b) + (b * a + b * (- 1 * b))) /
  1573 (a \<up> 2 + 2 * a * (- 1 * b) + (- 1 * b) \<up> 2)),
  1574 (([2,1,9], Res), (a * (a + - 1 * b) + (b * a + b * (- 1 * b))) /
  1575 (a \<up> 2 + 2 * a * (- 1 * b) + (- 1 * b) \<up> 2)),
  1576 (([2,1,10], Res), (a * (a + - 1 * b) + b * (a + - 1 * b)) /
  1577 (a \<up> 2 + 2 * a * (- 1 * b) + (- 1 * b) \<up> 2)),
  1578 (([2,1,11], Res), (a + b) * (a + - 1 * b) / (a \<up> 2 + 2 * a * (- 1 * b) + (- 1 * b) \<up> 2)),
  1579 (([2,1,12], Res), (a + b) * (a + - 1 * b) / ((a + - 1 * b) * (a + - 1 * b))),
  1580 (([2,1,13], Res), (a + b) / (a + - 1 * b)),
  1581 (([2,1], Res), (a + b) / (a + - 1 * b)),
  1582 (([2], Res), (a + b) / (a + - 1 * b)),
  1583 (([], Res), (a + b) / (a + - 1 * b))] 
  1584 *)
  1585 val newnds = children (get_nd pt [2,1]) (*see "fun detailrls"*);
  1586 if length newnds = 13 then () else error "rational.sml: interSteps cancel_p rev_rew_p";
  1587 
  1588 val p = ([2,1,9],Res);
  1589 getTactic 1 p;
  1590 val (_, tac, _) = ME_Misc.pt_extract (pt, p);
  1591 case tac of SOME (Rewrite ("sym_distrib_left", _)) => ()
  1592 | _ => error "rational.sml: getTactic, sym_real_plus_binom_times1";
  1593   \\----------------------------------TOODOO (*Rfuns revsets \<longrightarrow> broken*)*)
  1594 
  1595 
  1596 "-------- investigate rulesets for cancel_p ----------------------------------";
  1597 "-------- investigate rulesets for cancel_p ----------------------------------";
  1598 "-------- investigate rulesets for cancel_p ----------------------------------";
  1599 val thy = @{theory "Rational"};
  1600 val t = TermC.str2term "(a \<up> 2 + - 1*b \<up> 2) / (a \<up> 2 + - 2*a*b + b \<up> 2)";
  1601 val tt = TermC.str2term "(1 * a + 1 * b) * (1 * a + - 1 * b)"(*numerator only*);
  1602 
  1603 "----- with rewrite_set_";
  1604 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1605 if UnparseC.term tt'= "a \<up> 2 + - 1 * b \<up> 2" then () else error "rls chancel_p 1";
  1606 val tt = TermC.str2term "((1 * a + - 1 * b) * (1 * a + - 1 * b))"(*denominator only*);
  1607 val SOME (tt',asm) = rewrite_set_ thy false make_polynomial tt;
  1608 if UnparseC.term tt' = "a \<up> 2 + - 2 * a * b + b \<up> 2" then () else error "rls chancel_p 2";
  1609 
  1610 "----- with Derive.do_one; WN1130912 not investigated further, will be discontinued";
  1611 val SOME (tt, _) = factout_p_ thy t; 
  1612 if UnparseC.term tt = "(a + b) * (a + - 1 * b) / ((a + - 1 * b) * (a + - 1 * b))"
  1613 then () else error "rls chancel_p 3";
  1614 
  1615 "--- with simpler ruleset";
  1616 val {rules, rew_ord= (_, ro), ...} = Rule_Set.rep (assoc_rls "rev_rew_p");
  1617 val der = Derive.do_one thy Atools_erls rules ro NONE tt;
  1618 if length der = 12 then () else error "WN1130912 rls chancel_p 4";
  1619 (*default_print_depth 99;*) writeln (Derive.deriv2str der); (*default_print_depth 3;*)
  1620 
  1621 (*default_print_depth 99;*) map (UnparseC.term o #1) der; (*default_print_depth 3;*)
  1622 "...,(- 1 * b \<up> 2 + a \<up> 2) / (- 2 * (a * b) + a \<up> 2 + (- 1 * b) \<up> 2) ]";
  1623 (*default_print_depth 99;*) map (Rule.to_string o #2) der; (*default_print_depth 3;*)
  1624 (*default_print_depth 99;*) map (UnparseC.term o #1 o #3) der; (*default_print_depth 3;*)
  1625 
  1626 val der = Derive.do_one thy Atools_erls rules ro NONE 
  1627 	(TermC.str2term "(1 * a + 1 * b) * (1 * a + - 1 * b)");
  1628 (*default_print_depth 99;*) writeln (Derive.deriv2str der); (*default_print_depth 3;*)
  1629 
  1630 val {rules, rew_ord=(_,ro),...} = Rule_Set.rep (assoc_rls "rev_rew_p");
  1631 val der = Derive.do_one thy Atools_erls rules ro NONE 
  1632 	(TermC.str2term "(1 * a + - 1 * b) * (1 * a + - 1 * b)");
  1633 (*default_print_depth 99;*) writeln (Derive.deriv2str der); (*default_print_depth 3;*)
  1634 (*default_print_depth 99;*) map (UnparseC.term o #1) der; (*default_print_depth 3;*)
  1635 (*WN060829 ...postponed*)
  1636 
  1637 
  1638 "-------- fun eval_get_denominator -------------------------------------------";
  1639 "-------- fun eval_get_denominator -------------------------------------------";
  1640 "-------- fun eval_get_denominator -------------------------------------------";
  1641 val thy = @{theory Isac_Knowledge};
  1642 val t = Thm.term_of (the (TermC.parse thy "get_denominator ((a +x)/b)"));
  1643 val SOME (_, t') = eval_get_denominator "" 0 t thy;
  1644 if UnparseC.term t' = "get_denominator ((a + x) / b) = b"
  1645 then () else error "get_denominator ((a + x) / b) = b"
  1646 
  1647 
  1648 "-------- several errpats in complicated term --------------------------------";
  1649 "-------- several errpats in complicated term --------------------------------";
  1650 "-------- several errpats in complicated term --------------------------------";
  1651 (*WN12xxxx TODO: instead of Gabriella's example here (27.Jul.12) find a simpler one
  1652   WN130912: kept this test, although not clear what for*)
  1653 reset_states ();
  1654 CalcTree [(["Term ((5*b + 25)/(a^2 - b^2) * (a - b)/(5*b))", "normalform N"], 
  1655   ("Rational", ["rational", "simplification"], ["simplification", "of_rationals"]))];
  1656 Iterator 1;
  1657 moveActiveRoot 1;
  1658 autoCalculate 1 CompleteCalc;
  1659 val ((pt, p), _) = get_calc 1;
  1660 (*Test_Tool.show_pt pt;
  1661 [
  1662 (([], Frm), Simplify ((5 * b + 25) / (a \<up> 2 - b \<up> 2) * (a - b) / (5 * b))),
  1663 (([1], Frm), (5 * b + 25) / (a \<up> 2 - b \<up> 2) * (a - b) / (5 * b)),
  1664 (([1], Res), (5 * b + 25) / (a \<up> 2 + - 1 * b \<up> 2) * (a + - 1 * b) / (5 * b)),
  1665 (([2], Res), (5 * b + 25) * (a + - 1 * b) / (a \<up> 2 + - 1 * b \<up> 2) / (5 * b)),
  1666 (([3], Res), (25 * a + - 25 * b + 5 * (a * b) + -5 * b \<up> 2) / (a \<up> 2 + - 1 * b \<up> 2) /
  1667 (5 * b)),
  1668 (([4], Res), (25 + 5 * b) / (a + b) / (5 * b)),
  1669 (([5], Res), (25 + 5 * b) / ((a + b) * (5 * b))),
  1670 (([6], Res), (25 + 5 * b) / (5 * (a * b) + 5 * b \<up> 2)),
  1671 (([7], Res), (5 + b) / (a * b + b \<up> 2)),
  1672 (([], Res), (5 + b) / (a * b + b \<up> 2))] *)
  1673 
  1674 
  1675 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1676 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1677 "-------- WN1309xx non-terminating rls norm_Rational -------------------------";
  1678 (*------- Schalk I, p.70 Nr. 480b; a/b : c/d translated to a/b * d/c*)
  1679 val t = TermC.str2term 
  1680   ("((12*x*y / (9*x \<up> 2 - y \<up> 2))  /  (1 / (3*x - y) \<up> 2 - 1 / (3*x + y) \<up> 2))  *  " ^
  1681 	"((1/(x - 5*y) \<up> 2  -  1/(x + 5*y) \<up> 2)  /  (20*x*y / (x \<up> 2 - 25*y \<up> 2)))");
  1682 
  1683 (*1st factor separately simplified *)
  1684 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))";
  1685 val SOME (t', _) = rewrite_set_ thy false norm_Rational t; 
  1686 if UnparseC.term t' = "(- 9 * x \<up> 2 + y \<up> 2) / - 1" then () else error "Nr. 480b lhs changed";
  1687 (*2nd factor separately simplified *)
  1688 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)))";
  1689 val SOME (t',_) = rewrite_set_ thy false norm_Rational t; UnparseC.term t';
  1690 if UnparseC.term t' = "- 1 / (- 1 * x \<up> 2 + 25 * y \<up> 2)" then () else error "Nr. 480b rhs changed";
  1691 
  1692 "-------- Schalk I, p.70 Nr. 477a: terms are exploding ?!?";
  1693 val t = TermC.str2term ("b*y/(b - 2*y)/((b \<up> 2 - y \<up> 2)/(b+2*y))  /" ^
  1694 		 "(b \<up> 2*y + b*y \<up> 2) * (a+x) \<up> 2  /  ((b \<up> 2 - 4*y \<up> 2) * (a+2*x) \<up> 2)");
  1695 (*val SOME (t',_) = rewrite_set_ thy false norm_Rational t;
  1696 :
  1697 ###  rls: cancel_p on: (a \<up> 2 * (b * y) + 2 * (a * (b * (x * y))) + b * (x \<up> 2 * y)) /
  1698 (b + - 2 * y) /
  1699 ((b \<up> 2 + - 1 * y \<up> 2) / (b + 2 * y)) /
  1700 (b \<up> 2 * y + b * y \<up> 2) /
  1701 (a \<up> 2 * b \<up> 2 + -4 * (a \<up> 2 * y \<up> 2) + 4 * (a * (b \<up> 2 * x)) +
  1702  - 16 * (a * (x * y \<up> 2)) +
  1703  4 * (b \<up> 2 * x \<up> 2) +
  1704  - 16 * (x \<up> 2 * y \<up> 2)) 
  1705 exception Div raised
  1706 
  1707 BUT
  1708 val t = TermC.str2term 
  1709   ("(a \<up> 2 * (b * y) + 2 * (a * (b * (x * y))) + b * (x \<up> 2 * y)) /" ^
  1710   "(b + - 2 * y) /" ^
  1711   "((b \<up> 2 + - 1 * y \<up> 2) / (b + 2 * y)) /" ^
  1712   "(b \<up> 2 * y + b * y \<up> 2) /" ^
  1713   "(a \<up> 2 * b \<up> 2 + -4 * (a \<up> 2 * y \<up> 2) + 4 * (a * (b \<up> 2 * x)) +" ^
  1714   "- 16 * (a * (x * y \<up> 2)) +" ^
  1715   "4 * (b \<up> 2 * x \<up> 2) +" ^
  1716   "- 16 * (x \<up> 2 * y \<up> 2))");
  1717 NONE = cancel_p_ thy t;
  1718 *)
  1719 
  1720 (*------- Schalk I, p.70 Nr. 476b in 2003 this worked using 10 sec. *)
  1721 val t = TermC.str2term 
  1722   ("((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)    / " ^
  1723    "((a \<up> 2 + b \<up> 2) \<up> 2  /  (a + b) \<up> 2)");
  1724 (* Rewrite.trace_on := true;
  1725 rewrite_set_ thy false norm_Rational t;
  1726 :
  1727 ####  rls: cancel_p on: (2 * (a \<up> 7 * b) + 4 * (a \<up> 6 * b \<up> 2) + 6 * (a \<up> 5 * b \<up> 3) +
  1728  8 * (a \<up> 4 * b \<up> 4) +
  1729  6 * (a \<up> 3 * b \<up> 5) +
  1730  4 * (a \<up> 2 * b \<up> 6) +
  1731  2 * (a * b \<up> 7)) /
  1732 (2 * (a \<up> 9 * b) + 4 * (a \<up> 8 * b \<up> 2) +
  1733  2 * (2 * (a \<up> 7 * b \<up> 3)) +
  1734  4 * (a \<up> 6 * b \<up> 4) +
  1735  -4 * (a \<up> 4 * b \<up> 6) +
  1736  -4 * (a \<up> 3 * b \<up> 7) +
  1737  -4 * (a \<up> 2 * b \<up> 8) +
  1738  - 2 * (a * b \<up> 9))
  1739 
  1740 if UnparseC.term t = "1 / (a \<up> 2 + - 1 * b \<up> 2)" then ()
  1741 else error "rational.sml: diff.behav. in norm_Rational_mg 49";
  1742 *)
  1743 
  1744 "-------- Schalk I, p.70 Nr. 480a: terms are exploding ?!?";
  1745 val t = TermC.str2term ("(1/x + 1/y + 1/z)  /  (1/x - 1/y - 1/z)  /  " ^
  1746   "(2*x \<up> 2 / (x \<up> 2 - z \<up> 2) / (x / (x + z)  +  x / (x - z)))");
  1747 (* Rewrite.trace_on := true;
  1748 rewrite_set_ thy false norm_Rational t;
  1749 :
  1750 ####  rls: cancel_p on: (2 * (x \<up> 6 * (y \<up> 2 * z)) + 2 * (x \<up> 6 * (y * z \<up> 2)) +
  1751  2 * (x \<up> 5 * (y \<up> 2 * z \<up> 2)) +
  1752  - 2 * (x \<up> 4 * (y \<up> 2 * z \<up> 3)) +
  1753  - 2 * (x \<up> 4 * (y * z \<up> 4)) +
  1754  - 2 * (x \<up> 3 * (y \<up> 2 * z \<up> 4))) /
  1755 (- 2 * (x \<up> 6 * (y \<up> 2 * z)) + - 2 * (x \<up> 6 * (y * z \<up> 2)) +
  1756  2 * (x \<up> 5 * (y \<up> 2 * z \<up> 2)) +
  1757  2 * (x \<up> 4 * (y \<up> 2 * z \<up> 3)) +
  1758  2 * (x \<up> 4 * (y * z \<up> 4)) +
  1759  - 2 * (x \<up> 3 * (y \<up> 2 * z \<up> 4)))
  1760 *)
  1761 
  1762 "-------- Schalk I, p.60 Nr. 215d: terms are exploding, internal loop does not terminate";
  1763 val t = TermC.str2term "(a-b) \<up> 3 * (x+y) \<up> 4 / ((x+y) \<up> 2 * (a-b) \<up> 5)";
  1764 (* Kein Wunder, denn Z???ler und Nenner extra als Polynom dargestellt ergibt:
  1765 
  1766 val t = TermC.str2term "(a-b) \<up> 3 * (x+y) \<up> 4";
  1767 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1768 UnparseC.term t;
  1769 "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";
  1770 val t = TermC.str2term "((x+y) \<up> 2 * (a-b) \<up> 5)";
  1771 val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1772 UnparseC.term t;
  1773 "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";
  1774 
  1775 anscheinend macht dem Rechner das Krzen diese Bruches keinen Spass mehr ...*)
  1776 
  1777 "-------- Schalk I, p.70 Nr. 480b: terms are exploding, Rewrite.trace_on stops at";
  1778 val t = TermC.str2term ("((12*x*y/(9*x \<up> 2 - y \<up> 2))/" ^
  1779 		 "(1/(3*x - y) \<up> 2 - 1/(3*x + y) \<up> 2)) *" ^
  1780 		 "(1/(x - 5*y) \<up> 2 - 1/(x + 5*y) \<up> 2)/" ^
  1781 		 "(20*x*y/(x \<up> 2 - 25*y \<up> 2))");
  1782 (*val SOME (t, _) = rewrite_set_ thy false norm_Rational t;
  1783 :
  1784 ####  rls: cancel_p on: (19440 * (x \<up> 8 * y \<up> 2) + -490320 * (x \<up> 6 * y \<up> 4) +
  1785  108240 * (x \<up> 4 * y \<up> 6) +
  1786  -6000 * (x \<up> 2 * y \<up> 8)) /
  1787 (2160 * (x \<up> 8 * y \<up> 2) + - 108240 * (x \<up> 6 * y \<up> 4) +
  1788  1362000 * (x \<up> 4 * y \<up> 6) +
  1789  - 150000 * (x \<up> 2 * y \<up> 8))
  1790 *)
  1791