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