src/Tools/isac/Knowledge/Rational.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Wed, 27 Mar 2019 19:14:47 +0100
changeset 59531 c7300caa4159
parent 59530 2f33c24381e7
child 59532 0cc7dfa6f430
permissions -rw-r--r--
[-Test_Isac] adapt monom_of_term, add_fraction_p_ to AA
neuper@52105
     1
(* rationals, fractions of multivariate polynomials over the real field
neuper@37906
     2
   author: isac team
neuper@52105
     3
   Copyright (c) isac team 2002, 2013
neuper@37906
     4
   Use is subject to license terms.
neuper@37906
     5
neuper@37906
     6
   depends on Poly (and not on Atools), because 
wneuper@59370
     7
   fractions with _normalised_ polynomials are canceled, added, etc.
neuper@37906
     8
*)
neuper@37906
     9
neuper@48884
    10
theory Rational 
neuper@52075
    11
imports Poly "~~/src/Tools/isac/Knowledge/GCD_Poly_ML"
neuper@48884
    12
begin
neuper@37906
    13
wneuper@59472
    14
section \<open>Constants for evaluation by "Rule.Calc"\<close>
neuper@37906
    15
consts
neuper@37906
    16
jan@42300
    17
  is'_expanded    :: "real => bool" ("_ is'_expanded")     (*RL->Poly.thy*)
jan@42300
    18
  is'_ratpolyexp  :: "real => bool" ("_ is'_ratpolyexp") 
jan@42300
    19
  get_denominator :: "real => real"
jan@42338
    20
  get_numerator   :: "real => real"
neuper@37906
    21
wneuper@59472
    22
ML \<open>
neuper@52105
    23
(*.the expression contains + - * ^ / only ?.*)
neuper@52105
    24
fun is_ratpolyexp (Free _) = true
neuper@52105
    25
  | is_ratpolyexp (Const ("Groups.plus_class.plus",_) $ Free _ $ Free _) = true
neuper@52105
    26
  | is_ratpolyexp (Const ("Groups.minus_class.minus",_) $ Free _ $ Free _) = true
neuper@52105
    27
  | is_ratpolyexp (Const ("Groups.times_class.times",_) $ Free _ $ Free _) = true
neuper@52105
    28
  | is_ratpolyexp (Const ("Atools.pow",_) $ Free _ $ Free _) = true
wneuper@59360
    29
  | is_ratpolyexp (Const ("Rings.divide_class.divide",_) $ Free _ $ Free _) = true
neuper@52105
    30
  | is_ratpolyexp (Const ("Groups.plus_class.plus",_) $ t1 $ t2) = 
neuper@52105
    31
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@52105
    32
  | is_ratpolyexp (Const ("Groups.minus_class.minus",_) $ t1 $ t2) = 
neuper@52105
    33
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@52105
    34
  | is_ratpolyexp (Const ("Groups.times_class.times",_) $ t1 $ t2) = 
neuper@52105
    35
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@52105
    36
  | is_ratpolyexp (Const ("Atools.pow",_) $ t1 $ t2) = 
neuper@52105
    37
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
wneuper@59360
    38
  | is_ratpolyexp (Const ("Rings.divide_class.divide",_) $ t1 $ t2) = 
neuper@52105
    39
               ((is_ratpolyexp t1) andalso (is_ratpolyexp t2))
neuper@52105
    40
  | is_ratpolyexp _ = false;
neuper@37950
    41
neuper@52105
    42
(*("is_ratpolyexp", ("Rational.is'_ratpolyexp", eval_is_ratpolyexp ""))*)
neuper@52105
    43
fun eval_is_ratpolyexp (thmid:string) _ 
neuper@52105
    44
		       (t as (Const("Rational.is'_ratpolyexp", _) $ arg)) thy =
neuper@52105
    45
    if is_ratpolyexp arg
wneuper@59416
    46
    then SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg) "", 
wneuper@59390
    47
	         HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
wneuper@59416
    48
    else SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg) "", 
wneuper@59390
    49
	         HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
neuper@52105
    50
  | eval_is_ratpolyexp _ _ _ _ = NONE; 
neuper@52105
    51
neuper@52105
    52
(*("get_denominator", ("Rational.get_denominator", eval_get_denominator ""))*)
neuper@52105
    53
fun eval_get_denominator (thmid:string) _ 
neuper@52105
    54
		      (t as Const ("Rational.get_denominator", _) $
wneuper@59360
    55
              (Const ("Rings.divide_class.divide", _) $ num $
neuper@52105
    56
                denom)) thy = 
wneuper@59416
    57
      SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy denom) "", 
wneuper@59390
    58
	            HOLogic.Trueprop $ (TermC.mk_equality (t, denom)))
neuper@52105
    59
  | eval_get_denominator _ _ _ _ = NONE; 
neuper@52105
    60
neuper@52105
    61
(*("get_numerator", ("Rational.get_numerator", eval_get_numerator ""))*)
neuper@52105
    62
fun eval_get_numerator (thmid:string) _ 
neuper@52105
    63
      (t as Const ("Rational.get_numerator", _) $
wneuper@59360
    64
          (Const ("Rings.divide_class.divide", _) $num
neuper@52105
    65
            $denom )) thy = 
wneuper@59416
    66
    SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy num) "", 
wneuper@59390
    67
	    HOLogic.Trueprop $ (TermC.mk_equality (t, num)))
neuper@52105
    68
  | eval_get_numerator _ _ _ _ = NONE; 
wneuper@59472
    69
\<close>
neuper@52105
    70
wneuper@59472
    71
section \<open>Theorems for rewriting\<close>
neuper@52105
    72
neuper@52105
    73
axiomatization (* naming due to Isabelle2002, but not contained in Isabelle2002; 
neuper@52105
    74
                  many thms are due to RL and can be removed with updating the equation solver;
neuper@52105
    75
                  TODO: replace by equivalent thms in recent Isabelle201x *) 
neuper@52105
    76
where
neuper@52105
    77
  mult_cross:      "[| b ~= 0; d ~= 0 |] ==> (a / b = c / d) = (a * d = b * c)" and
neuper@52105
    78
  mult_cross1:     "   b ~= 0            ==> (a / b = c    ) = (a     = b * c)" and
neuper@52105
    79
  mult_cross2:     "           d ~= 0    ==> (a     = c / d) = (a * d =     c)" and
neuper@37950
    80
                  
neuper@52105
    81
  add_minus:       "a + b - b = a"(*RL->Poly.thy*) and
neuper@52105
    82
  add_minus1:      "a - b + b = a"(*RL->Poly.thy*) and
neuper@37950
    83
                  
neuper@52105
    84
  rat_mult:        "a / b * (c / d) = a * c / (b * d)"(*?Isa02*)  and
neuper@52105
    85
  rat_mult2:       "a / b *  c      = a * c /  b     "(*?Isa02*) and
t@42211
    86
neuper@52105
    87
  rat_mult_poly_l: "c is_polyexp ==> c * (a / b) = c * a /  b" and
neuper@52105
    88
  rat_mult_poly_r: "c is_polyexp ==> (a / b) * c = a * c /  b" and
neuper@37906
    89
neuper@37906
    90
(*real_times_divide1_eq .. Isa02*) 
neuper@52105
    91
  real_times_divide_1_eq:  "-1 * (c / d) = -1 * c / d " and
neuper@52105
    92
  real_times_divide_num:   "a is_const ==> a * (c / d) = a * c / d " and
t@42211
    93
neuper@52105
    94
  real_mult_div_cancel2:   "k ~= 0 ==> m * k / (n * k) = m / n" and
neuper@37978
    95
(*real_mult_div_cancel1:   "k ~= 0 ==> k * m / (k * n) = m / n"..Isa02*)
neuper@37906
    96
			  
neuper@52105
    97
  real_divide_divide1:     "y ~= 0 ==> (u / v) / (y / z) = (u / v) * (z / y)" and
neuper@52105
    98
  real_divide_divide1_mg:  "y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)" and
neuper@37978
    99
(*real_divide_divide2_eq:  "x / y / z = x / (y * z)"..Isa02*)
neuper@37906
   100
			  
neuper@52105
   101
  rat_power:               "(a / b)^^^n = (a^^^n) / (b^^^n)" and
neuper@37978
   102
neuper@37978
   103
  rat_add:         "[| a is_const; b is_const; c is_const; d is_const |] ==> 
neuper@52105
   104
	           a / c + b / d = (a * d + b * c) / (c * d)" and
neuper@37978
   105
  rat_add_assoc:   "[| a is_const; b is_const; c is_const; d is_const |] ==> 
neuper@52105
   106
	           a / c +(b / d + e) = (a * d + b * c)/(d * c) + e" and
neuper@37978
   107
  rat_add1:        "[| a is_const; b is_const; c is_const |] ==> 
neuper@52105
   108
	           a / c + b / c = (a + b) / c" and
neuper@37978
   109
  rat_add1_assoc:   "[| a is_const; b is_const; c is_const |] ==> 
neuper@52105
   110
	           a / c + (b / c + e) = (a + b) / c + e" and
neuper@37978
   111
  rat_add2:        "[| a is_const; b is_const; c is_const |] ==> 
neuper@52105
   112
	           a / c + b = (a + b * c) / c" and
neuper@37978
   113
  rat_add2_assoc:  "[| a is_const; b is_const; c is_const |] ==> 
neuper@52105
   114
	           a / c + (b + e) = (a + b * c) / c + e" and
neuper@37978
   115
  rat_add3:        "[| a is_const; b is_const; c is_const |] ==> 
neuper@52105
   116
	           a + b / c = (a * c + b) / c" and
neuper@37978
   117
  rat_add3_assoc:   "[| a is_const; b is_const; c is_const |] ==> 
neuper@37950
   118
	           a + (b / c + e) = (a * c + b) / c + e"
neuper@37950
   119
wneuper@59472
   120
section \<open>Cancellation and addition of fractions\<close>
wneuper@59472
   121
subsection \<open>Conversion term <--> poly\<close>
wneuper@59472
   122
subsubsection \<open>Convert a term to the internal representation of a multivariate polynomial\<close>
wneuper@59472
   123
ML \<open>
wneuper@59531
   124
fun monom_of_term vs (c, es) (Const (id, _)) =
wneuper@59531
   125
    (c, list_update es (find_index (curry op = (strip_thy id)) vs) 1)
wneuper@59531
   126
  | monom_of_term  vs (c, es) (Free (id, _)) =
wneuper@59390
   127
    if TermC.is_num' id 
wneuper@59390
   128
    then (id |> TermC.int_of_str_opt |> the |> curry op * c, es) (*several numerals in one monom*)
neuper@52087
   129
    else (c, list_update es (find_index (curry op = id) vs) 1)
neuper@52087
   130
  | monom_of_term  vs (c, es) (Const ("Atools.pow", _) $ Free (id, _) $ Free (e, _)) =
wneuper@59390
   131
    (c, list_update es (find_index (curry op = id) vs) (the (TermC.int_of_str_opt e)))
neuper@52087
   132
  | monom_of_term vs (c, es) (Const ("Groups.times_class.times", _) $ m1 $ m2) =
neuper@52087
   133
    let val (c', es') = monom_of_term vs (c, es) m1
neuper@52087
   134
    in monom_of_term vs (c', es') m2 end
wneuper@59531
   135
  | monom_of_term _ _ t = raise ERROR ("poly malformed 1 with " ^ Rule.term2str t)
neuper@52087
   136
wneuper@59531
   137
fun monoms_of_term vs (t as Const _) =
wneuper@59531
   138
    [monom_of_term  vs (1, replicate (length vs) 0) t]
wneuper@59531
   139
  | monoms_of_term vs (t as Free _) =
neuper@52087
   140
    [monom_of_term  vs (1, replicate (length vs) 0) t]
neuper@52087
   141
  | monoms_of_term vs (t as Const ("Atools.pow", _) $ _ $  _) =
neuper@52087
   142
    [monom_of_term  vs (1, replicate (length vs) 0) t]
neuper@52087
   143
  | monoms_of_term vs (t as Const ("Groups.times_class.times", _) $ _ $  _) =
neuper@52087
   144
    [monom_of_term  vs (1, replicate (length vs) 0) t]
neuper@52087
   145
  | monoms_of_term vs (Const ("Groups.plus_class.plus", _) $ ms1 $ ms2) =
neuper@52087
   146
    (monoms_of_term vs ms1) @ (monoms_of_term vs ms2)
wneuper@59531
   147
  | monoms_of_term _ t = raise ERROR ("poly malformed 2 with " ^ Rule.term2str t)
neuper@52087
   148
neuper@52087
   149
(* convert a term to the internal representation of a multivariate polynomial;
neuper@52087
   150
  the conversion is quite liberal, see test --- fun poly_of_term ---:
neuper@52087
   151
* the order of variables and the parentheses within a monomial are arbitrary
neuper@52087
   152
* the coefficient may be somewhere
neuper@52087
   153
* he order and the parentheses within monomials are arbitrary
neuper@52087
   154
But the term must be completely expand + over * (laws of distributivity are not applicable).
neuper@52087
   155
neuper@52087
   156
The function requires the free variables as strings already given, 
neuper@52087
   157
because the gcd involves 2 polynomials (with the same length for their list of exponents).
neuper@52087
   158
*)
neuper@52087
   159
fun poly_of_term vs (t as Const ("Groups.plus_class.plus", _) $ _ $ _) =
neuper@52101
   160
    (SOME (t |> monoms_of_term vs |> order)
neuper@52087
   161
      handle ERROR _ => NONE)
neuper@52087
   162
  | poly_of_term vs t =
neuper@52101
   163
    (SOME [monom_of_term vs (1, replicate (length vs) 0) t]
neuper@52087
   164
      handle ERROR _ => NONE)
neuper@52087
   165
neuper@52087
   166
fun is_poly t =
neuper@52103
   167
  let 
wneuper@59389
   168
    val vs = t |> TermC.vars |> map TermC.str_of_free_opt (* tolerate Var in simplification *)
neuper@52103
   169
      |> filter is_some |> map the |> sort string_ord
neuper@52087
   170
  in 
neuper@52087
   171
    case poly_of_term vs t of SOME _ => true | NONE => false
neuper@52087
   172
  end
neuper@52105
   173
val is_expanded = is_poly   (* TODO: check names *)
neuper@52105
   174
val is_polynomial = is_poly (* TODO: check names *)
wneuper@59472
   175
\<close>
neuper@52087
   176
wneuper@59472
   177
subsubsection \<open>Convert internal representation of a multivariate polynomial to a term\<close>
wneuper@59472
   178
ML \<open>
neuper@52087
   179
fun term_of_es _ _ _ [] = [] (*assumes same length for vs and es*)
neuper@52087
   180
  | term_of_es baseT expT (_ :: vs) (0 :: es) =
neuper@52087
   181
    [] @ term_of_es baseT expT vs es
neuper@52087
   182
  | term_of_es baseT expT (v :: vs) (1 :: es) =
neuper@52087
   183
    [(Free (v, baseT))] @ term_of_es baseT expT vs es
neuper@52087
   184
  | term_of_es baseT expT (v :: vs) (e :: es) =
neuper@52087
   185
    [Const ("Atools.pow", [baseT, expT] ---> baseT) $ 
wneuper@59389
   186
      (Free (v, baseT)) $  (Free (TermC.isastr_of_int e, expT))]
neuper@52087
   187
    @ term_of_es baseT expT vs es
neuper@52087
   188
neuper@52087
   189
fun term_of_monom baseT expT vs ((c, es): monom) =
neuper@52087
   190
    let val es' = term_of_es baseT expT vs es
neuper@52087
   191
    in 
neuper@52087
   192
      if c = 1 
neuper@52087
   193
      then 
neuper@52087
   194
        if es' = [] (*if es = [0,0,0,...]*)
wneuper@59389
   195
        then Free (TermC.isastr_of_int c, baseT)
neuper@52087
   196
        else foldl (HOLogic.mk_binop "Groups.times_class.times") (hd es', tl es')
wneuper@59389
   197
      else foldl (HOLogic.mk_binop "Groups.times_class.times") (Free (TermC.isastr_of_int c, baseT), es') 
neuper@52087
   198
    end
neuper@52087
   199
neuper@52087
   200
fun term_of_poly baseT expT vs p =
wneuper@59190
   201
  let val monos = map (term_of_monom baseT expT vs) p
neuper@52087
   202
  in foldl (HOLogic.mk_binop "Groups.plus_class.plus") (hd monos, tl monos) end
wneuper@59472
   203
\<close>
neuper@52087
   204
wneuper@59472
   205
subsection \<open>Apply gcd_poly for cancelling and adding fractions as terms\<close>
wneuper@59472
   206
ML \<open>
neuper@52091
   207
fun mk_noteq_0 baseT t = 
neuper@52091
   208
  Const ("HOL.Not", HOLogic.boolT --> HOLogic.boolT) $ 
neuper@52091
   209
    (Const ("HOL.eq", [baseT, baseT] ---> HOLogic.boolT) $ t $ Free ("0", HOLogic.realT))
neuper@52091
   210
neuper@52094
   211
fun mk_asms baseT ts =
wneuper@59389
   212
  let val as' = filter_out TermC.is_num ts (* asm like "2 ~= 0" is needless *)
neuper@52094
   213
  in map (mk_noteq_0 baseT) as' end
wneuper@59472
   214
\<close>
neuper@52094
   215
wneuper@59472
   216
subsubsection \<open>Factor out gcd for cancellation\<close>
wneuper@59472
   217
ML \<open>
neuper@52091
   218
fun check_fraction t =
wneuper@59360
   219
  let val Const ("Rings.divide_class.divide", _) $ numerator $ denominator = t
neuper@52091
   220
  in SOME (numerator, denominator) end
neuper@52091
   221
  handle Bind => NONE
neuper@52091
   222
neuper@52091
   223
(* prepare a term for cancellation by factoring out the gcd
neuper@52091
   224
  assumes: is a fraction with outmost "/"*)
neuper@52091
   225
fun factout_p_ (thy: theory) t =
neuper@52091
   226
  let val opt = check_fraction t
neuper@52091
   227
  in
neuper@52091
   228
    case opt of 
neuper@52091
   229
      NONE => NONE
neuper@52091
   230
    | SOME (numerator, denominator) =>
neuper@52091
   231
      let 
wneuper@59389
   232
        val vs = t |> TermC.vars |> map TermC.str_of_free_opt (* tolerate Var in simplification *)
neuper@52103
   233
          |> filter is_some |> map the |> sort string_ord
neuper@52091
   234
        val baseT = type_of numerator
neuper@52091
   235
        val expT = HOLogic.realT
neuper@52091
   236
      in
neuper@52091
   237
        case (poly_of_term vs numerator, poly_of_term vs denominator) of
neuper@52091
   238
          (SOME a, SOME b) =>
neuper@52091
   239
            let
neuper@52091
   240
              val ((a', b'), c) = gcd_poly a b
neuper@52096
   241
              val es = replicate (length vs) 0 
neuper@52096
   242
            in
neuper@52096
   243
              if c = [(1, es)] orelse c = [(~1, es)]
neuper@52096
   244
              then NONE
neuper@52096
   245
              else 
neuper@52096
   246
                let
neuper@52096
   247
                  val b't = term_of_poly baseT expT vs b'
neuper@52096
   248
                  val ct = term_of_poly baseT expT vs c
neuper@52096
   249
                  val t' = 
wneuper@59360
   250
                    HOLogic.mk_binop "Rings.divide_class.divide" 
neuper@52096
   251
                      (HOLogic.mk_binop "Groups.times_class.times"
wneuper@59190
   252
                        (term_of_poly baseT expT vs a', ct),
neuper@52096
   253
                       HOLogic.mk_binop "Groups.times_class.times" (b't, ct))
neuper@52105
   254
                in SOME (t', mk_asms baseT [b't, ct]) end
neuper@52096
   255
            end
neuper@52091
   256
        | _ => NONE : (term * term list) option
neuper@52091
   257
      end
neuper@52091
   258
  end
wneuper@59472
   259
\<close>
neuper@52091
   260
wneuper@59472
   261
subsubsection \<open>Cancel a fraction\<close>
wneuper@59472
   262
ML \<open>
neuper@52096
   263
(* cancel a term by the gcd ("" denote terms with internal algebraic structure)
neuper@52096
   264
  cancel_p_ :: theory \<Rightarrow> term  \<Rightarrow> (term \<times> term list) option
neuper@52096
   265
  cancel_p_ thy "a / b" = SOME ("a' / b'", ["b' \<noteq> 0"])
neuper@52096
   266
  assumes: a is_polynomial  \<and>  b is_polynomial  \<and>  b \<noteq> 0
neuper@52096
   267
  yields
neuper@52096
   268
    SOME ("a' / b'", ["b' \<noteq> 0"]). gcd_poly a b \<noteq> 1  \<and>  gcd_poly a b \<noteq> -1  \<and>  
neuper@52096
   269
      a' * gcd_poly a b = a  \<and>  b' * gcd_poly a b = b
neuper@52096
   270
    \<or> NONE *)
neuper@52101
   271
fun cancel_p_ (_: theory) t =
neuper@52091
   272
  let val opt = check_fraction t
neuper@52091
   273
  in
neuper@52091
   274
    case opt of 
neuper@52091
   275
      NONE => NONE
neuper@52091
   276
    | SOME (numerator, denominator) =>
neuper@52091
   277
      let 
wneuper@59389
   278
        val vs = t |> TermC.vars |> map TermC.str_of_free_opt (* tolerate Var in simplification *)
neuper@52103
   279
          |> filter is_some |> map the |> sort string_ord
neuper@52091
   280
        val baseT = type_of numerator
neuper@52091
   281
        val expT = HOLogic.realT
neuper@52091
   282
      in
neuper@52091
   283
        case (poly_of_term vs numerator, poly_of_term vs denominator) of
neuper@52091
   284
          (SOME a, SOME b) =>
neuper@52091
   285
            let
neuper@52091
   286
              val ((a', b'), c) = gcd_poly a b
neuper@52096
   287
              val es = replicate (length vs) 0 
neuper@52096
   288
            in
neuper@52096
   289
              if c = [(1, es)] orelse c = [(~1, es)]
neuper@52096
   290
              then NONE
neuper@52096
   291
              else 
neuper@52096
   292
                let
neuper@52096
   293
                  val bt' = term_of_poly baseT expT vs b'
neuper@52096
   294
                  val ct = term_of_poly baseT expT vs c
neuper@52096
   295
                  val t' = 
wneuper@59360
   296
                    HOLogic.mk_binop "Rings.divide_class.divide" 
wneuper@59190
   297
                      (term_of_poly baseT expT vs a', bt')
neuper@52096
   298
                  val asm = mk_asms baseT [bt']
neuper@52096
   299
                in SOME (t', asm) end
neuper@52096
   300
            end
neuper@52091
   301
        | _ => NONE : (term * term list) option
neuper@52091
   302
      end
neuper@52091
   303
  end
wneuper@59472
   304
\<close>
neuper@52091
   305
wneuper@59472
   306
subsubsection \<open>Factor out to a common denominator for addition\<close>
wneuper@59472
   307
ML \<open>
neuper@52101
   308
(* addition of fractions allows (at most) one non-fraction (a monomial) *)
neuper@52101
   309
fun check_frac_sum 
neuper@52091
   310
    (Const ("Groups.plus_class.plus", _) $ 
wneuper@59360
   311
      (Const ("Rings.divide_class.divide", _) $ n1 $ d1) $
wneuper@59360
   312
      (Const ("Rings.divide_class.divide", _) $ n2 $ d2))
neuper@52091
   313
    = SOME ((n1, d1), (n2, d2))
neuper@52101
   314
  | check_frac_sum 
neuper@52091
   315
    (Const ("Groups.plus_class.plus", _) $ 
neuper@52091
   316
      nofrac $ 
wneuper@59360
   317
      (Const ("Rings.divide_class.divide", _) $ n2 $ d2))
neuper@52091
   318
    = SOME ((nofrac, Free ("1", HOLogic.realT)), (n2, d2))
neuper@52101
   319
  | check_frac_sum 
neuper@52091
   320
    (Const ("Groups.plus_class.plus", _) $ 
wneuper@59360
   321
      (Const ("Rings.divide_class.divide", _) $ n1 $ d1) $ 
neuper@52091
   322
      nofrac)
neuper@52091
   323
    = SOME ((n1, d1), (nofrac, Free ("1", HOLogic.realT)))
neuper@52101
   324
  | check_frac_sum _ = NONE  
neuper@52091
   325
neuper@52091
   326
(* prepare a term for addition by providing the least common denominator as a product
neuper@52091
   327
  assumes: is a term with outmost "+" and at least one outmost "/" in respective summands*)
neuper@52101
   328
fun common_nominator_p_ (_: theory) t =
neuper@52101
   329
  let val opt = check_frac_sum t
neuper@52091
   330
  in
neuper@52091
   331
    case opt of 
neuper@52091
   332
      NONE => NONE
neuper@52091
   333
    | SOME ((n1, d1), (n2, d2)) =>
neuper@52103
   334
      let 
wneuper@59389
   335
        val vs = t |> TermC.vars |> map TermC.str_of_free_opt (* tolerate Var in simplification *)
neuper@52103
   336
          |> filter is_some |> map the |> sort string_ord
neuper@52091
   337
      in
neuper@52091
   338
        case (poly_of_term vs d1, poly_of_term vs d2) of
neuper@52091
   339
          (SOME a, SOME b) =>
neuper@52091
   340
            let
neuper@52091
   341
              val ((a', b'), c) = gcd_poly a b
neuper@52101
   342
              val (baseT, expT) = (type_of n1, HOLogic.realT)
wneuper@59190
   343
              val [d1', d2', c'] = map (term_of_poly baseT expT vs) [a', b', c]
neuper@52091
   344
              (*----- minimum of parentheses & nice result, but breaks tests: -------------
neuper@52091
   345
              val denom = HOLogic.mk_binop "Groups.times_class.times" 
neuper@52101
   346
                (HOLogic.mk_binop "Groups.times_class.times" (d1', d2'), c') -------------*)
neuper@52101
   347
              val denom =
neuper@52101
   348
                if c = [(1, replicate (length vs) 0)]
neuper@52101
   349
                then HOLogic.mk_binop "Groups.times_class.times" (d1', d2')
neuper@52101
   350
                else
neuper@52101
   351
                  HOLogic.mk_binop "Groups.times_class.times" (c',
neuper@52101
   352
                  HOLogic.mk_binop "Groups.times_class.times" (d1', d2')) (*--------------*)
neuper@52091
   353
              val t' =
neuper@52091
   354
                HOLogic.mk_binop "Groups.plus_class.plus"
wneuper@59360
   355
                  (HOLogic.mk_binop "Rings.divide_class.divide"
neuper@52091
   356
                    (HOLogic.mk_binop "Groups.times_class.times" (n1, d2'), denom),
wneuper@59360
   357
                  HOLogic.mk_binop "Rings.divide_class.divide" 
neuper@52091
   358
                    (HOLogic.mk_binop "Groups.times_class.times" (n2, d1'), denom))
neuper@52094
   359
              val asm = mk_asms baseT [d1', d2', c']
neuper@52091
   360
            in SOME (t', asm) end
neuper@52091
   361
        | _ => NONE : (term * term list) option
neuper@52091
   362
      end
neuper@52091
   363
  end
neuper@52091
   364
wneuper@59472
   365
\<close>
neuper@52105
   366
wneuper@59472
   367
subsubsection \<open>Addition of at least one fraction within a sum\<close>
wneuper@59472
   368
ML \<open>
neuper@52091
   369
(* add fractions
neuper@52100
   370
  assumes: is a term with outmost "+" and at least one outmost "/" in respective summands
neuper@52100
   371
  NOTE: the case "(_ + _) + _" need not be considered due to iterated addition.*)
neuper@52105
   372
fun add_fraction_p_ (_: theory) t =
neuper@52101
   373
  case check_frac_sum t of 
neuper@52101
   374
    NONE => NONE
neuper@52101
   375
  | SOME ((n1, d1), (n2, d2)) =>
wneuper@59530
   376
    let          (* vvvvvvv                  vvvvvvvvvvv tolerate Free, Const, Var *)
wneuper@59531
   377
      val vs = t |> ids2str |> subtract op = poly_consts |> map strip_thy |> sort string_ord
neuper@52101
   378
    in
neuper@52101
   379
      case (poly_of_term vs n1, poly_of_term vs d1, poly_of_term vs n2, poly_of_term vs d2) of
neuper@52101
   380
        (SOME _, SOME a, SOME _, SOME b) =>
neuper@52101
   381
          let
neuper@52101
   382
            val ((a', b'), c) = gcd_poly a b
neuper@52101
   383
            val (baseT, expT) = (type_of n1, HOLogic.realT)
neuper@52101
   384
            val nomin = term_of_poly baseT expT vs 
neuper@52101
   385
              (((the (poly_of_term vs n1)) %%*%% b') %%+%% ((the (poly_of_term vs n2)) %%*%% a')) 
neuper@52101
   386
            val denom = term_of_poly baseT expT vs ((c %%*%% a') %%*%% b')
wneuper@59360
   387
            val t' = HOLogic.mk_binop "Rings.divide_class.divide" (nomin, denom)
neuper@52101
   388
          in SOME (t', mk_asms baseT [denom]) end
neuper@52101
   389
      | _ => NONE : (term * term list) option
neuper@52101
   390
    end
wneuper@59472
   391
\<close>
neuper@52091
   392
wneuper@59472
   393
section \<open>Embed cancellation and addition into rewriting\<close>
wneuper@59472
   394
ML \<open>val thy = @{theory}\<close>
wneuper@59472
   395
subsection \<open>Rulesets and predicate for embedding\<close>
wneuper@59472
   396
ML \<open>
neuper@52105
   397
(* evaluates conditions in calculate_Rational *)
neuper@52105
   398
val calc_rat_erls =
s1210629013@55444
   399
  prep_rls'
wneuper@59416
   400
    (Rule.Rls {id = "calc_rat_erls", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   401
      erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
neuper@52105
   402
      rules = 
wneuper@59416
   403
        [Rule.Calc ("HOL.eq", eval_equal "#equal_"),
wneuper@59416
   404
        Rule.Calc ("Atools.is'_const", eval_const "#is_const_"),
wneuper@59416
   405
        Rule.Thm ("not_true", TermC.num_str @{thm not_true}),
wneuper@59416
   406
        Rule.Thm ("not_false", TermC.num_str @{thm not_false})], 
wneuper@59416
   407
      scr = Rule.EmptyScr});
neuper@37950
   408
neuper@52105
   409
(* simplifies expressions with numerals;
neuper@52105
   410
   does NOT rearrange the term by AC-rewriting; thus terms with variables 
neuper@52105
   411
   need to have constants to be commuted together respectively           *)
neuper@52105
   412
val calculate_Rational =
wneuper@59416
   413
  prep_rls' (Rule.merge_rls "calculate_Rational"
wneuper@59416
   414
    (Rule.Rls {id = "divide", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   415
      erls = calc_rat_erls, srls = Rule.Erls,
neuper@52105
   416
      calc = [], errpatts = [],
neuper@52105
   417
      rules = 
wneuper@59416
   418
        [Rule.Calc ("Rings.divide_class.divide", eval_cancel "#divide_e"),
neuper@37950
   419
wneuper@59416
   420
        Rule.Thm ("minus_divide_left", TermC.num_str (@{thm minus_divide_left} RS @{thm sym})),
neuper@52105
   421
          (*SYM - ?x / ?y = - (?x / ?y)  may come from subst*)
wneuper@59416
   422
        Rule.Thm ("rat_add", TermC.num_str @{thm rat_add}),
neuper@52105
   423
          (*"[| a is_const; b is_const; c is_const; d is_const |] ==> \
neuper@52105
   424
          \a / c + b / d = (a * d) / (c * d) + (b * c ) / (d * c)"*)
wneuper@59416
   425
        Rule.Thm ("rat_add1", TermC.num_str @{thm rat_add1}),
neuper@52105
   426
          (*"[| a is_const; b is_const; c is_const |] ==> a / c + b / c = (a + b) / c"*)
wneuper@59416
   427
        Rule.Thm ("rat_add2", TermC.num_str @{thm rat_add2}),
neuper@52105
   428
          (*"[| ?a is_const; ?b is_const; ?c is_const |] ==> ?a / ?c + ?b = (?a + ?b * ?c) / ?c"*)
wneuper@59416
   429
        Rule.Thm ("rat_add3", TermC.num_str @{thm rat_add3}),
neuper@52105
   430
          (*"[| a is_const; b is_const; c is_const |] ==> a + b / c = (a * c) / c + b / c"\
neuper@52105
   431
          .... is_const to be omitted here FIXME*)
neuper@52105
   432
        
wneuper@59416
   433
        Rule.Thm ("rat_mult", TermC.num_str @{thm rat_mult}), 
neuper@52105
   434
          (*a / b * (c / d) = a * c / (b * d)*)
wneuper@59416
   435
        Rule.Thm ("times_divide_eq_right", TermC.num_str @{thm times_divide_eq_right}),
neuper@52105
   436
          (*?x * (?y / ?z) = ?x * ?y / ?z*)
wneuper@59416
   437
        Rule.Thm ("times_divide_eq_left", TermC.num_str @{thm times_divide_eq_left}),
neuper@52105
   438
          (*?y / ?z * ?x = ?y * ?x / ?z*)
neuper@52105
   439
        
wneuper@59416
   440
        Rule.Thm ("real_divide_divide1", TermC.num_str @{thm real_divide_divide1}),
neuper@52105
   441
          (*"?y ~= 0 ==> ?u / ?v / (?y / ?z) = ?u / ?v * (?z / ?y)"*)
wneuper@59416
   442
        Rule.Thm ("divide_divide_eq_left", TermC.num_str @{thm divide_divide_eq_left}),
neuper@52105
   443
          (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
neuper@52105
   444
        
wneuper@59416
   445
        Rule.Thm ("rat_power", TermC.num_str @{thm rat_power}),
neuper@52105
   446
          (*"(?a / ?b) ^^^ ?n = ?a ^^^ ?n / ?b ^^^ ?n"*)
neuper@52105
   447
        
wneuper@59416
   448
        Rule.Thm ("mult_cross", TermC.num_str @{thm mult_cross}),
neuper@52105
   449
          (*"[| b ~= 0; d ~= 0 |] ==> (a / b = c / d) = (a * d = b * c)*)
wneuper@59416
   450
        Rule.Thm ("mult_cross1", TermC.num_str @{thm mult_cross1}),
neuper@52105
   451
          (*"   b ~= 0            ==> (a / b = c    ) = (a     = b * c)*)
wneuper@59416
   452
        Rule.Thm ("mult_cross2", TermC.num_str @{thm mult_cross2})
neuper@52105
   453
          (*"           d ~= 0    ==> (a     = c / d) = (a * d =     c)*)], 
wneuper@59416
   454
      scr = Rule.EmptyScr})
neuper@52105
   455
    calculate_Poly);
neuper@37950
   456
neuper@37950
   457
(*("is_expanded", ("Rational.is'_expanded", eval_is_expanded ""))*)
neuper@37950
   458
fun eval_is_expanded (thmid:string) _ 
neuper@37950
   459
		       (t as (Const("Rational.is'_expanded", _) $ arg)) thy = 
neuper@37950
   460
    if is_expanded arg
wneuper@59416
   461
    then SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg) "", 
wneuper@59390
   462
	         HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
wneuper@59416
   463
    else SOME (TermC.mk_thmid thmid (Rule.term_to_string''' thy arg) "", 
wneuper@59390
   464
	         HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
s1210629013@52159
   465
  | eval_is_expanded _ _ _ _ = NONE;
wneuper@59472
   466
\<close>
wneuper@59472
   467
setup \<open>KEStore_Elems.add_calcs
wneuper@59472
   468
  [("is_expanded", ("Rational.is'_expanded", eval_is_expanded ""))]\<close>
wneuper@59472
   469
ML \<open>
neuper@37950
   470
val rational_erls = 
wneuper@59416
   471
  Rule.merge_rls "rational_erls" calculate_Rational 
wneuper@59416
   472
    (Rule.append_rls "is_expanded" Atools_erls 
wneuper@59416
   473
      [Rule.Calc ("Rational.is'_expanded", eval_is_expanded "")]);
wneuper@59472
   474
\<close>
neuper@37950
   475
wneuper@59472
   476
subsection \<open>Embed cancellation into rewriting\<close>
wneuper@59472
   477
ML \<open>
neuper@52105
   478
local (* cancel_p *)
neuper@37950
   479
wneuper@59416
   480
val {rules = rules, rew_ord = (_, ro), ...} = Rule.rep_rls (assoc_rls' @{theory} "rev_rew_p");
neuper@37950
   481
neuper@52105
   482
fun init_state thy eval_rls ro t =
neuper@52105
   483
  let
neuper@52105
   484
    val SOME (t', _) = factout_p_ thy t;
neuper@52105
   485
    val SOME (t'', asm) = cancel_p_ thy t;
wneuper@59263
   486
    val der = Rtools.reverse_deriv thy eval_rls rules ro NONE t';
neuper@52105
   487
    val der = der @ 
wneuper@59416
   488
      [(Rule.Thm ("real_mult_div_cancel2", TermC.num_str @{thm real_mult_div_cancel2}), (t'', asm))]
wneuper@59263
   489
    val rs = (Rtools.distinct_Thm o (map #1)) der
wneuper@59263
   490
  	val rs = filter_out (Rtools.eq_Thms 
neuper@52105
   491
  	  ["sym_real_add_zero_left", "sym_real_mult_0", "sym_real_mult_1"]) rs
neuper@52105
   492
  in (t, t'', [rs(*one in order to ease locate_rule*)], der) end;
neuper@37950
   493
neuper@52105
   494
fun locate_rule thy eval_rls ro [rs] t r =
wneuper@59406
   495
    if member op = ((map (Celem.id_of_thm)) rs) (Celem.id_of_thm r)
neuper@52105
   496
    then 
wneuper@59406
   497
      let val ropt = Rewrite.rewrite_ thy ro eval_rls true (Celem.thm_of_thm r) t;
neuper@52105
   498
      in
neuper@52105
   499
        case ropt of SOME ta => [(r, ta)]
neuper@52105
   500
	      | NONE => (tracing 
wneuper@59416
   501
	          ("### locate_rule:  rewrite " ^ Celem.id_of_thm r ^ " " ^ Rule.term2str t ^ " = NONE"); []) 
neuper@52105
   502
			end
wneuper@59406
   503
    else (tracing ("### locate_rule:  " ^ Celem.id_of_thm r ^ " not mem rrls"); [])
neuper@52105
   504
  | locate_rule _ _ _ _ _ _ = error "locate_rule: doesnt match rev-sets in istate";
neuper@37950
   505
neuper@52105
   506
fun next_rule thy eval_rls ro [rs] t =
neuper@52105
   507
    let
wneuper@59263
   508
      val der = Rtools.make_deriv thy eval_rls rs ro NONE t;
neuper@52105
   509
    in case der of (_, r, _) :: _ => SOME r | _ => NONE end
neuper@52105
   510
  | next_rule _ _ _ _ _ = error ("next_rule: doesnt match rev-sets in istate");
neuper@37950
   511
wneuper@59416
   512
fun attach_form (_: Rule.rule list list) (_: term) (_: term) = 
wneuper@59416
   513
  [(*TODO*)]: ( Rule.rule * (term * term list)) list;
neuper@37950
   514
neuper@37950
   515
in
neuper@37950
   516
neuper@52105
   517
val cancel_p = 
wneuper@59416
   518
  Rule.Rrls {id = "cancel_p", prepat = [],
neuper@52105
   519
	rew_ord=("ord_make_polynomial", ord_make_polynomial false thy),
neuper@52105
   520
	erls = rational_erls, 
neuper@52105
   521
	calc = 
neuper@52105
   522
	  [("PLUS", ("Groups.plus_class.plus", eval_binop "#add_")),
neuper@52105
   523
	  ("TIMES" , ("Groups.times_class.times", eval_binop "#mult_")),
wneuper@59360
   524
	  ("DIVIDE", ("Rings.divide_class.divide", eval_cancel "#divide_e")),
neuper@52105
   525
	  ("POWER", ("Atools.pow", eval_binop "#power_"))],
neuper@52105
   526
    errpatts = [],
neuper@52105
   527
	scr =
wneuper@59416
   528
	  Rule.Rfuns {init_state  = init_state thy Atools_erls ro,
neuper@52105
   529
		normal_form = cancel_p_ thy, 
neuper@52105
   530
		locate_rule = locate_rule thy Atools_erls ro,
neuper@52105
   531
		next_rule   = next_rule thy Atools_erls ro,
neuper@52105
   532
		attach_form = attach_form}}
neuper@52105
   533
end; (* local cancel_p *)
wneuper@59472
   534
\<close>
neuper@37950
   535
wneuper@59472
   536
subsection \<open>Embed addition into rewriting\<close>
wneuper@59472
   537
ML \<open>
neuper@52105
   538
local (* add_fractions_p *)
neuper@37950
   539
wneuper@59416
   540
(*val {rules = rules, rew_ord = (_, ro), ...} = Rule.rep_rls (assoc_rls "make_polynomial");*)
wneuper@59416
   541
val {rules, rew_ord=(_,ro),...} = Rule.rep_rls (assoc_rls' @{theory} "rev_rew_p");
neuper@37950
   542
neuper@52105
   543
fun init_state thy eval_rls ro t =
neuper@52105
   544
  let 
neuper@52105
   545
    val SOME (t',_) = common_nominator_p_ thy t;
neuper@52105
   546
    val SOME (t'', asm) = add_fraction_p_ thy t;
wneuper@59263
   547
    val der = Rtools.reverse_deriv thy eval_rls rules ro NONE t';
neuper@52105
   548
    val der = der @ 
wneuper@59416
   549
      [(Rule.Thm ("real_mult_div_cancel2", TermC.num_str @{thm real_mult_div_cancel2}), (t'',asm))]
wneuper@59263
   550
    val rs = (Rtools.distinct_Thm o (map #1)) der;
wneuper@59263
   551
    val rs = filter_out (Rtools.eq_Thms 
neuper@52105
   552
      ["sym_real_add_zero_left", "sym_real_mult_0", "sym_real_mult_1"]) rs;
neuper@52105
   553
  in (t, t'', [rs(*here only _ONE_*)], der) end;
neuper@37950
   554
neuper@52105
   555
fun locate_rule thy eval_rls ro [rs] t r =
wneuper@59406
   556
    if member op = ((map (Celem.id_of_thm)) rs) (Celem.id_of_thm r)
neuper@52105
   557
    then 
wneuper@59406
   558
      let val ropt = Rewrite.rewrite_ thy ro eval_rls true (Celem.thm_of_thm r) t;
neuper@52105
   559
      in 
neuper@52105
   560
        case ropt of
neuper@52105
   561
          SOME ta => [(r, ta)]
neuper@52105
   562
	      | NONE => 
wneuper@59416
   563
	        (tracing ("### locate_rule:  rewrite " ^ Celem.id_of_thm r ^ " " ^ Rule.term2str t ^ " = NONE");
neuper@52105
   564
	        []) end
wneuper@59406
   565
    else (tracing ("### locate_rule:  " ^ Celem.id_of_thm r ^ " not mem rrls"); [])
neuper@52105
   566
  | locate_rule _ _ _ _ _ _ = error "locate_rule: doesnt match rev-sets in istate";
neuper@37950
   567
neuper@37950
   568
fun next_rule thy eval_rls ro [rs] t =
wneuper@59263
   569
    let val der = Rtools.make_deriv thy eval_rls rs ro NONE t;
neuper@52105
   570
    in 
neuper@52105
   571
      case der of
neuper@52105
   572
	      (_,r,_)::_ => SOME r
neuper@52105
   573
	    | _ => NONE
neuper@37950
   574
    end
neuper@52105
   575
  | next_rule _ _ _ _ _ = error ("next_rule: doesnt match rev-sets in istate");
neuper@37950
   576
wneuper@59389
   577
val pat0 = TermC.parse_patt thy "?r/?s+?u/?v :: real";
wneuper@59389
   578
val pat1 = TermC.parse_patt thy "?r/?s+?u    :: real";
wneuper@59389
   579
val pat2 = TermC.parse_patt thy "?r   +?u/?v :: real";
neuper@48760
   580
val prepat = [([@{term True}], pat0),
neuper@48760
   581
	      ([@{term True}], pat1),
neuper@48760
   582
	      ([@{term True}], pat2)];
neuper@37950
   583
in
neuper@37950
   584
neuper@52105
   585
val add_fractions_p =
wneuper@59416
   586
  Rule.Rrls {id = "add_fractions_p", prepat=prepat,
neuper@52105
   587
    rew_ord = ("ord_make_polynomial", ord_make_polynomial false thy),
neuper@52105
   588
    erls = rational_erls,
neuper@52105
   589
    calc = [("PLUS", ("Groups.plus_class.plus", eval_binop "#add_")),
neuper@52105
   590
      ("TIMES", ("Groups.times_class.times", eval_binop "#mult_")),
wneuper@59360
   591
      ("DIVIDE", ("Rings.divide_class.divide", eval_cancel "#divide_e")),
neuper@52105
   592
      ("POWER", ("Atools.pow", eval_binop "#power_"))],
neuper@52105
   593
    errpatts = [],
wneuper@59416
   594
    scr = Rule.Rfuns {init_state  = init_state thy Atools_erls ro,
neuper@52105
   595
      normal_form = add_fraction_p_ thy,
neuper@52105
   596
      locate_rule = locate_rule thy Atools_erls ro,
neuper@52105
   597
      next_rule   = next_rule thy Atools_erls ro,
neuper@52105
   598
      attach_form = attach_form}}
neuper@52105
   599
end; (*local add_fractions_p *)
wneuper@59472
   600
\<close>
neuper@37950
   601
wneuper@59472
   602
subsection \<open>Cancelling and adding all occurrences in a term /////////////////////////////\<close>
wneuper@59472
   603
ML \<open>
neuper@52105
   604
(*copying cancel_p_rls + add her caused error in interface.sml*)
wneuper@59472
   605
\<close>
neuper@42451
   606
wneuper@59472
   607
section \<open>Rulesets for general simplification\<close>
wneuper@59472
   608
ML \<open>
neuper@37950
   609
(*erls for calculate_Rational; make local with FIXX@ME result:term *term list*)
s1210629013@55444
   610
val powers_erls = prep_rls'(
wneuper@59416
   611
  Rule.Rls {id = "powers_erls", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   612
      erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   613
      rules = [Rule.Calc ("Atools.is'_atom",eval_is_atom "#is_atom_"),
wneuper@59416
   614
	       Rule.Calc ("Atools.is'_even",eval_is_even "#is_even_"),
wneuper@59416
   615
	       Rule.Calc ("Orderings.ord_class.less",eval_equ "#less_"),
wneuper@59416
   616
	       Rule.Thm ("not_false", TermC.num_str @{thm not_false}),
wneuper@59416
   617
	       Rule.Thm ("not_true", TermC.num_str @{thm not_true}),
wneuper@59416
   618
	       Rule.Calc ("Groups.plus_class.plus",eval_binop "#add_")
neuper@37950
   619
	       ],
wneuper@59416
   620
      scr = Rule.EmptyScr
wneuper@59406
   621
      });
neuper@37950
   622
(*.all powers over + distributed; atoms over * collected, other distributed
neuper@37950
   623
   contains absolute minimum of thms for context in norm_Rational .*)
s1210629013@55444
   624
val powers = prep_rls'(
wneuper@59416
   625
  Rule.Rls {id = "powers", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   626
      erls = powers_erls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   627
      rules = [Rule.Thm ("realpow_multI", TermC.num_str @{thm realpow_multI}),
neuper@37950
   628
	       (*"(r * s) ^^^ n = r ^^^ n * s ^^^ n"*)
wneuper@59416
   629
	       Rule.Thm ("realpow_pow",TermC.num_str @{thm realpow_pow}),
neuper@37950
   630
	       (*"(a ^^^ b) ^^^ c = a ^^^ (b * c)"*)
wneuper@59416
   631
	       Rule.Thm ("realpow_oneI",TermC.num_str @{thm realpow_oneI}),
neuper@37950
   632
	       (*"r ^^^ 1 = r"*)
wneuper@59416
   633
	       Rule.Thm ("realpow_minus_even",TermC.num_str @{thm realpow_minus_even}),
neuper@37950
   634
	       (*"n is_even ==> (- r) ^^^ n = r ^^^ n" ?-->discard_minus?*)
wneuper@59416
   635
	       Rule.Thm ("realpow_minus_odd",TermC.num_str @{thm realpow_minus_odd}),
neuper@37950
   636
	       (*"Not (n is_even) ==> (- r) ^^^ n = -1 * r ^^^ n"*)
neuper@37950
   637
	       
neuper@37950
   638
	       (*----- collect atoms over * -----*)
wneuper@59416
   639
	       Rule.Thm ("realpow_two_atom",TermC.num_str @{thm realpow_two_atom}),	
neuper@37950
   640
	       (*"r is_atom ==> r * r = r ^^^ 2"*)
wneuper@59416
   641
	       Rule.Thm ("realpow_plus_1",TermC.num_str @{thm realpow_plus_1}),		
neuper@37950
   642
	       (*"r is_atom ==> r * r ^^^ n = r ^^^ (n + 1)"*)
wneuper@59416
   643
	       Rule.Thm ("realpow_addI_atom",TermC.num_str @{thm realpow_addI_atom}),
neuper@37950
   644
	       (*"r is_atom ==> r ^^^ n * r ^^^ m = r ^^^ (n + m)"*)
neuper@37950
   645
neuper@37950
   646
	       (*----- distribute none-atoms -----*)
wneuper@59416
   647
	       Rule.Thm ("realpow_def_atom",TermC.num_str @{thm realpow_def_atom}),
neuper@37950
   648
	       (*"[| 1 < n; not(r is_atom) |]==>r ^^^ n = r * r ^^^ (n + -1)"*)
wneuper@59416
   649
	       Rule.Thm ("realpow_eq_oneI",TermC.num_str @{thm realpow_eq_oneI}),
neuper@37950
   650
	       (*"1 ^^^ n = 1"*)
wneuper@59416
   651
	       Rule.Calc ("Groups.plus_class.plus",eval_binop "#add_")
neuper@37950
   652
	       ],
wneuper@59416
   653
      scr = Rule.EmptyScr
wneuper@59406
   654
      });
neuper@37950
   655
(*.contains absolute minimum of thms for context in norm_Rational.*)
s1210629013@55444
   656
val rat_mult_divide = prep_rls'(
wneuper@59416
   657
  Rule.Rls {id = "rat_mult_divide", preconds = [], 
wneuper@59416
   658
      rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   659
      erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   660
      rules = [Rule.Thm ("rat_mult",TermC.num_str @{thm rat_mult}),
neuper@37950
   661
	       (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
wneuper@59416
   662
	       Rule.Thm ("times_divide_eq_right",TermC.num_str @{thm times_divide_eq_right}),
neuper@37950
   663
	       (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
neuper@37950
   664
	       otherwise inv.to a / b / c = ...*)
wneuper@59416
   665
	       Rule.Thm ("times_divide_eq_left",TermC.num_str @{thm times_divide_eq_left}),
neuper@37950
   666
	       (*"?a / ?b * ?c = ?a * ?c / ?b" order weights x^^^n too much
neuper@37950
   667
		     and does not commute a / b * c ^^^ 2 !*)
neuper@37950
   668
	       
wneuper@59416
   669
	       Rule.Thm ("divide_divide_eq_right", 
wneuper@59389
   670
                     TermC.num_str @{thm divide_divide_eq_right}),
neuper@37950
   671
	       (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
wneuper@59416
   672
	       Rule.Thm ("divide_divide_eq_left",
wneuper@59389
   673
                     TermC.num_str @{thm divide_divide_eq_left}),
neuper@37950
   674
	       (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
wneuper@59416
   675
	       Rule.Calc ("Rings.divide_class.divide"  ,eval_cancel "#divide_e")
neuper@37950
   676
	       ],
wneuper@59416
   677
      scr = Rule.EmptyScr
wneuper@59406
   678
      });
neuper@37979
   679
neuper@37950
   680
(*.contains absolute minimum of thms for context in norm_Rational.*)
s1210629013@55444
   681
val reduce_0_1_2 = prep_rls'(
wneuper@59416
   682
  Rule.Rls{id = "reduce_0_1_2", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord),
wneuper@59416
   683
      erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   684
      rules = [(*Rule.Thm ("divide_1",TermC.num_str @{thm divide_1}),
neuper@37950
   685
		 "?x / 1 = ?x" unnecess.for normalform*)
wneuper@59416
   686
	       Rule.Thm ("mult_1_left",TermC.num_str @{thm mult_1_left}),                 
neuper@37950
   687
	       (*"1 * z = z"*)
wneuper@59416
   688
	       (*Rule.Thm ("real_mult_minus1",TermC.num_str @{thm real_mult_minus1}),
neuper@37950
   689
	       "-1 * z = - z"*)
wneuper@59416
   690
	       (*Rule.Thm ("real_minus_mult_cancel",TermC.num_str @{thm real_minus_mult_cancel}),
neuper@37950
   691
	       "- ?x * - ?y = ?x * ?y"*)
neuper@37950
   692
wneuper@59416
   693
	       Rule.Thm ("mult_zero_left",TermC.num_str @{thm mult_zero_left}),        
neuper@37950
   694
	       (*"0 * z = 0"*)
wneuper@59416
   695
	       Rule.Thm ("add_0_left",TermC.num_str @{thm add_0_left}),
neuper@37950
   696
	       (*"0 + z = z"*)
wneuper@59416
   697
	       (*Rule.Thm ("right_minus",TermC.num_str @{thm right_minus}),
neuper@37950
   698
	       "?z + - ?z = 0"*)
neuper@37950
   699
wneuper@59416
   700
	       Rule.Thm ("sym_real_mult_2",
wneuper@59389
   701
                     TermC.num_str (@{thm real_mult_2} RS @{thm sym})),	
neuper@37950
   702
	       (*"z1 + z1 = 2 * z1"*)
wneuper@59416
   703
	       Rule.Thm ("real_mult_2_assoc",TermC.num_str @{thm real_mult_2_assoc}),
neuper@37950
   704
	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
neuper@37950
   705
wneuper@59416
   706
	       Rule.Thm ("division_ring_divide_zero",TermC.num_str @{thm division_ring_divide_zero})
neuper@37950
   707
	       (*"0 / ?x = 0"*)
wneuper@59416
   708
	       ], scr = Rule.EmptyScr});
neuper@37950
   709
neuper@37950
   710
(*erls for calculate_Rational; 
neuper@37950
   711
  make local with FIXX@ME result:term *term list WN0609???SKMG*)
s1210629013@55444
   712
val norm_rat_erls = prep_rls'(
wneuper@59416
   713
  Rule.Rls {id = "norm_rat_erls", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   714
      erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   715
      rules = [Rule.Calc ("Atools.is'_const",eval_const "#is_const_")
wneuper@59416
   716
	       ], scr = Rule.EmptyScr});
neuper@37979
   717
neuper@52105
   718
(* consists of rls containing the absolute minimum of thms *)
neuper@37950
   719
(*040209: this version has been used by RL for his equations,
neuper@52105
   720
which is now replaced by MGs version "norm_Rational" below *)
s1210629013@55444
   721
val norm_Rational_min = prep_rls'(
wneuper@59416
   722
  Rule.Rls {id = "norm_Rational_min", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   723
      erls = norm_rat_erls, srls = Rule.Erls, calc = [], errpatts = [],
neuper@37950
   724
      rules = [(*sequence given by operator precedence*)
wneuper@59416
   725
	       Rule.Rls_ discard_minus,
wneuper@59416
   726
	       Rule.Rls_ powers,
wneuper@59416
   727
	       Rule.Rls_ rat_mult_divide,
wneuper@59416
   728
	       Rule.Rls_ expand,
wneuper@59416
   729
	       Rule.Rls_ reduce_0_1_2,
wneuper@59416
   730
	       Rule.Rls_ order_add_mult,
wneuper@59416
   731
	       Rule.Rls_ collect_numerals,
wneuper@59416
   732
	       Rule.Rls_ add_fractions_p,
wneuper@59416
   733
	       Rule.Rls_ cancel_p
neuper@37950
   734
	       ],
wneuper@59416
   735
      scr = Rule.EmptyScr});
neuper@37979
   736
s1210629013@55444
   737
val norm_Rational_parenthesized = prep_rls'(
wneuper@59416
   738
  Rule.Seq {id = "norm_Rational_parenthesized", preconds = []:term list, 
wneuper@59416
   739
       rew_ord = ("dummy_ord", Rule.dummy_ord),
wneuper@59416
   740
      erls = Atools_erls, srls = Rule.Erls,
neuper@42451
   741
      calc = [], errpatts = [],
wneuper@59416
   742
      rules = [Rule.Rls_  norm_Rational_min,
wneuper@59416
   743
	       Rule.Rls_ discard_parentheses
neuper@37950
   744
	       ],
wneuper@59416
   745
      scr = Rule.EmptyScr});      
neuper@37950
   746
neuper@37950
   747
(*WN030318???SK: simplifies all but cancel and common_nominator*)
neuper@37950
   748
val simplify_rational = 
wneuper@59416
   749
    Rule.merge_rls "simplify_rational" expand_binoms
wneuper@59416
   750
    (Rule.append_rls "divide" calculate_Rational
wneuper@59416
   751
		[Rule.Thm ("div_by_1",TermC.num_str @{thm div_by_1}),
neuper@37950
   752
		 (*"?x / 1 = ?x"*)
wneuper@59416
   753
		 Rule.Thm ("rat_mult",TermC.num_str @{thm rat_mult}),
neuper@37950
   754
		 (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
wneuper@59416
   755
		 Rule.Thm ("times_divide_eq_right",TermC.num_str @{thm times_divide_eq_right}),
neuper@37950
   756
		 (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
neuper@37950
   757
		 otherwise inv.to a / b / c = ...*)
wneuper@59416
   758
		 Rule.Thm ("times_divide_eq_left",TermC.num_str @{thm times_divide_eq_left}),
neuper@37950
   759
		 (*"?a / ?b * ?c = ?a * ?c / ?b"*)
wneuper@59416
   760
		 Rule.Thm ("add_minus",TermC.num_str @{thm add_minus}),
neuper@37950
   761
		 (*"?a + ?b - ?b = ?a"*)
wneuper@59416
   762
		 Rule.Thm ("add_minus1",TermC.num_str @{thm add_minus1}),
neuper@37950
   763
		 (*"?a - ?b + ?b = ?a"*)
wneuper@59416
   764
		 Rule.Thm ("divide_minus1",TermC.num_str @{thm divide_minus1})
neuper@37950
   765
		 (*"?x / -1 = - ?x"*)
neuper@37950
   766
		 ]);
wneuper@59472
   767
\<close>
wneuper@59472
   768
ML \<open>
s1210629013@55444
   769
val add_fractions_p_rls = prep_rls'(
wneuper@59416
   770
  Rule.Rls {id = "add_fractions_p_rls", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   771
	  erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   772
	  rules = [Rule.Rls_ add_fractions_p], 
wneuper@59416
   773
	  scr = Rule.EmptyScr});
neuper@37950
   774
wneuper@59416
   775
(* "Rule.Rls" causes repeated application of cancel_p to one and the same term *)
s1210629013@55444
   776
val cancel_p_rls = prep_rls'(
wneuper@59416
   777
  Rule.Rls 
wneuper@59416
   778
    {id = "cancel_p_rls", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   779
    erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   780
    rules = [Rule.Rls_ cancel_p], 
wneuper@59416
   781
	  scr = Rule.EmptyScr});
neuper@52105
   782
neuper@37950
   783
(*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
neuper@37950
   784
    used in initial part norm_Rational_mg, see example DA-M02-main.p.60.*)
s1210629013@55444
   785
val rat_mult_poly = prep_rls'(
wneuper@59416
   786
  Rule.Rls {id = "rat_mult_poly", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   787
	  erls = Rule.append_rls "Rule.e_rls-is_polyexp" Rule.e_rls [Rule.Calc ("Poly.is'_polyexp", eval_is_polyexp "")], 
wneuper@59416
   788
	  srls = Rule.Erls, calc = [], errpatts = [],
neuper@52105
   789
	  rules = 
wneuper@59416
   790
	    [Rule.Thm ("rat_mult_poly_l",TermC.num_str @{thm rat_mult_poly_l}),
neuper@52105
   791
	    (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
wneuper@59416
   792
	    Rule.Thm ("rat_mult_poly_r",TermC.num_str @{thm rat_mult_poly_r})
neuper@52105
   793
	    (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*) ], 
wneuper@59416
   794
	  scr = Rule.EmptyScr});
neuper@37979
   795
neuper@37950
   796
(*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
neuper@37950
   797
    used in looping part norm_Rational_rls, see example DA-M02-main.p.60 
wneuper@59416
   798
    .. WHERE THE LATTER DOES ALWAYS WORK, BECAUSE erls = Rule.e_rls, 
wneuper@59416
   799
    I.E. THE RESPECTIVE ASSUMPTION IS STORED AND Rule.Thm APPLIED; WN051028 
neuper@37950
   800
    ... WN0609???MG.*)
s1210629013@55444
   801
val rat_mult_div_pow = prep_rls'(
wneuper@59416
   802
  Rule.Rls {id = "rat_mult_div_pow", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   803
    erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   804
    rules = [Rule.Thm ("rat_mult", TermC.num_str @{thm rat_mult}),
neuper@52105
   805
      (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
wneuper@59416
   806
      Rule.Thm ("rat_mult_poly_l", TermC.num_str @{thm rat_mult_poly_l}),
neuper@52105
   807
      (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
wneuper@59416
   808
      Rule.Thm ("rat_mult_poly_r", TermC.num_str @{thm rat_mult_poly_r}),
neuper@52105
   809
      (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
neuper@52105
   810
      
wneuper@59416
   811
      Rule.Thm ("real_divide_divide1_mg", TermC.num_str @{thm real_divide_divide1_mg}),
neuper@52105
   812
      (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
wneuper@59416
   813
      Rule.Thm ("divide_divide_eq_right", TermC.num_str @{thm divide_divide_eq_right}),
neuper@52105
   814
      (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
wneuper@59416
   815
      Rule.Thm ("divide_divide_eq_left", TermC.num_str @{thm divide_divide_eq_left}),
neuper@52105
   816
      (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
wneuper@59416
   817
      Rule.Calc ("Rings.divide_class.divide", eval_cancel "#divide_e"),
neuper@52105
   818
      
wneuper@59416
   819
      Rule.Thm ("rat_power", TermC.num_str @{thm rat_power})
neuper@52105
   820
      (*"(?a / ?b) ^^^ ?n = ?a ^^^ ?n / ?b ^^^ ?n"*)
neuper@52105
   821
      ],
wneuper@59416
   822
    scr = Rule.EmptyScr});
neuper@37950
   823
s1210629013@55444
   824
val rat_reduce_1 = prep_rls'(
wneuper@59416
   825
  Rule.Rls {id = "rat_reduce_1", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   826
    erls = Rule.e_rls, srls = Rule.Erls, calc = [], errpatts = [], 
neuper@52105
   827
    rules = 
wneuper@59416
   828
      [Rule.Thm ("div_by_1", TermC.num_str @{thm div_by_1}),
neuper@52105
   829
      (*"?x / 1 = ?x"*)
wneuper@59416
   830
      Rule.Thm ("mult_1_left", TermC.num_str @{thm mult_1_left})           
neuper@52105
   831
      (*"1 * z = z"*)
neuper@52105
   832
      ],
wneuper@59416
   833
    scr = Rule.EmptyScr});
neuper@52105
   834
neuper@52105
   835
(* looping part of norm_Rational *)
s1210629013@55444
   836
val norm_Rational_rls = prep_rls' (
wneuper@59416
   837
  Rule.Rls {id = "norm_Rational_rls", preconds = [], rew_ord = ("dummy_ord",Rule.dummy_ord), 
wneuper@59416
   838
    erls = norm_rat_erls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   839
    rules = [Rule.Rls_ add_fractions_p_rls,
wneuper@59416
   840
      Rule.Rls_ rat_mult_div_pow,
wneuper@59416
   841
      Rule.Rls_ make_rat_poly_with_parentheses,
wneuper@59416
   842
      Rule.Rls_ cancel_p_rls,
wneuper@59416
   843
      Rule.Rls_ rat_reduce_1
neuper@52105
   844
      ],
wneuper@59416
   845
    scr = Rule.EmptyScr});
neuper@52105
   846
s1210629013@55444
   847
val norm_Rational = prep_rls' (
wneuper@59416
   848
  Rule.Seq 
wneuper@59416
   849
    {id = "norm_Rational", preconds = [], rew_ord = ("dummy_ord", Rule.dummy_ord), 
wneuper@59416
   850
    erls = norm_rat_erls, srls = Rule.Erls, calc = [], errpatts = [],
wneuper@59416
   851
    rules = [Rule.Rls_ discard_minus,
wneuper@59416
   852
      Rule.Rls_ rat_mult_poly,             (* removes double fractions like a/b/c *)
wneuper@59416
   853
      Rule.Rls_ make_rat_poly_with_parentheses,
wneuper@59416
   854
      Rule.Rls_ cancel_p_rls,
wneuper@59416
   855
      Rule.Rls_ norm_Rational_rls,         (* the main rls, looping (#) *)
wneuper@59416
   856
      Rule.Rls_ discard_parentheses1       (* mult only *)
neuper@52100
   857
      ],
wneuper@59416
   858
    scr = Rule.EmptyScr});
wneuper@59472
   859
\<close>
neuper@52125
   860
wneuper@59472
   861
setup \<open>KEStore_Elems.add_rlss 
neuper@52125
   862
  [("calculate_Rational", (Context.theory_name @{theory}, calculate_Rational)), 
neuper@52125
   863
  ("calc_rat_erls", (Context.theory_name @{theory}, calc_rat_erls)), 
neuper@52125
   864
  ("rational_erls", (Context.theory_name @{theory}, rational_erls)), 
neuper@52125
   865
  ("cancel_p", (Context.theory_name @{theory}, cancel_p)), 
neuper@52125
   866
  ("add_fractions_p", (Context.theory_name @{theory}, add_fractions_p)),
neuper@52125
   867
 
neuper@52125
   868
  ("add_fractions_p_rls", (Context.theory_name @{theory}, add_fractions_p_rls)), 
neuper@52125
   869
  ("powers_erls", (Context.theory_name @{theory}, powers_erls)), 
neuper@52125
   870
  ("powers", (Context.theory_name @{theory}, powers)), 
neuper@52125
   871
  ("rat_mult_divide", (Context.theory_name @{theory}, rat_mult_divide)), 
neuper@52125
   872
  ("reduce_0_1_2", (Context.theory_name @{theory}, reduce_0_1_2)),
neuper@52125
   873
 
neuper@52125
   874
  ("rat_reduce_1", (Context.theory_name @{theory}, rat_reduce_1)), 
neuper@52125
   875
  ("norm_rat_erls", (Context.theory_name @{theory}, norm_rat_erls)), 
neuper@52125
   876
  ("norm_Rational", (Context.theory_name @{theory}, norm_Rational)), 
neuper@52125
   877
  ("norm_Rational_rls", (Context.theory_name @{theory}, norm_Rational_rls)), 
neuper@55493
   878
  ("norm_Rational_min", (Context.theory_name @{theory}, norm_Rational_min)),
neuper@52125
   879
  ("norm_Rational_parenthesized", (Context.theory_name @{theory}, norm_Rational_parenthesized)),
neuper@52125
   880
 
neuper@52125
   881
  ("rat_mult_poly", (Context.theory_name @{theory}, rat_mult_poly)), 
neuper@52125
   882
  ("rat_mult_div_pow", (Context.theory_name @{theory}, rat_mult_div_pow)), 
wneuper@59472
   883
  ("cancel_p_rls", (Context.theory_name @{theory}, cancel_p_rls))]\<close>
neuper@37950
   884
wneuper@59472
   885
section \<open>A problem for simplification of rationals\<close>
wneuper@59472
   886
setup \<open>KEStore_Elems.add_pbts
wneuper@59406
   887
  [(Specify.prep_pbt thy "pbl_simp_rat" [] Celem.e_pblID
s1210629013@55339
   888
      (["rational","simplification"],
s1210629013@55339
   889
        [("#Given" ,["Term t_t"]),
s1210629013@55339
   890
          ("#Where" ,["t_t is_ratpolyexp"]),
s1210629013@55339
   891
          ("#Find"  ,["normalform n_n"])],
wneuper@59416
   892
        Rule.append_rls "e_rls" Rule.e_rls [(*for preds in where_*)], 
wneuper@59472
   893
        SOME "Simplify t_t", [["simplification","of_rationals"]]))]\<close>
neuper@37950
   894
wneuper@59472
   895
section \<open>A methods for simplification of rationals\<close>
s1210629013@55373
   896
(*WN061025 this methods script is copied from (auto-generated) script
s1210629013@55373
   897
  of norm_Rational in order to ease repair on inform*)
wneuper@59505
   898
(*ok
wneuper@59504
   899
partial_function (tailrec) simplify :: "real \<Rightarrow> real"
wneuper@59504
   900
  where
wneuper@59504
   901
"simplify t_t =
wneuper@59504
   902
  ((Try (Rewrite_Set ''discard_minus'' False) @@
wneuper@59504
   903
    Try (Rewrite_Set ''rat_mult_poly'' False) @@
wneuper@59504
   904
    Try (Rewrite_Set ''make_rat_poly_with_parentheses'' False) @@
wneuper@59504
   905
    Try (Rewrite_Set ''cancel_p_rls'' False) @@
wneuper@59504
   906
    (Repeat
wneuper@59504
   907
     ((Try (Rewrite_Set ''add_fractions_p_rls'' False) @@
wneuper@59504
   908
       Try (Rewrite_Set ''rat_mult_div_pow'' False) @@
wneuper@59504
   909
       Try (Rewrite_Set ''make_rat_poly_with_parentheses'' False) @@
wneuper@59504
   910
       Try (Rewrite_Set ''cancel_p_rls'' False) @@
wneuper@59504
   911
       Try (Rewrite_Set ''rat_reduce_1'' False)))) @@
wneuper@59504
   912
    Try (Rewrite_Set ''discard_parentheses1'' False))
wneuper@59504
   913
   t_t)"
wneuper@59505
   914
*)
wneuper@59472
   915
setup \<open>KEStore_Elems.add_mets
wneuper@59473
   916
    [Specify.prep_met thy "met_simp_rat" [] Celem.e_metID
s1210629013@55373
   917
      (["simplification","of_rationals"],
s1210629013@55373
   918
        [("#Given" ,["Term t_t"]),
s1210629013@55373
   919
          ("#Where" ,["t_t is_ratpolyexp"]),
s1210629013@55373
   920
          ("#Find"  ,["normalform n_n"])],
wneuper@59416
   921
	      {rew_ord'="tless_true", rls' = Rule.e_rls, calc = [], srls = Rule.e_rls, 
wneuper@59416
   922
	        prls = Rule.append_rls "simplification_of_rationals_prls" Rule.e_rls 
wneuper@59416
   923
				    [(*for preds in where_*) Rule.Calc ("Rational.is'_ratpolyexp", eval_is_ratpolyexp "")],
wneuper@59416
   924
				  crls = Rule.e_rls, errpats = [], nrls = norm_Rational_rls},
s1210629013@55373
   925
				  "Script SimplifyScript (t_t::real) =                             " ^
wneuper@59489
   926
          "  ((Try (Rewrite_Set ''discard_minus'' False) @@                   " ^
wneuper@59489
   927
          "    Try (Rewrite_Set ''rat_mult_poly'' False) @@                    " ^
wneuper@59489
   928
          "    Try (Rewrite_Set ''make_rat_poly_with_parentheses'' False) @@   " ^
wneuper@59489
   929
          "    Try (Rewrite_Set ''cancel_p_rls'' False) @@                     " ^
s1210629013@55373
   930
          "    (Repeat                                                     " ^
wneuper@59489
   931
          "     ((Try (Rewrite_Set ''add_fractions_p_rls'' False) @@        " ^
wneuper@59489
   932
          "       Try (Rewrite_Set ''rat_mult_div_pow'' False) @@              " ^
wneuper@59489
   933
          "       Try (Rewrite_Set ''make_rat_poly_with_parentheses'' False) @@" ^
wneuper@59489
   934
          "       Try (Rewrite_Set ''cancel_p_rls'' False) @@                  " ^
wneuper@59489
   935
          "       Try (Rewrite_Set ''rat_reduce_1'' False)))) @@               " ^
wneuper@59489
   936
          "    Try (Rewrite_Set ''discard_parentheses1'' False))               " ^
s1210629013@55373
   937
          "   t_t)")]
wneuper@59472
   938
\<close>
neuper@37979
   939
neuper@52105
   940
end