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