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