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