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