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