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