src/Tools/isac/Knowledge/Rational.thy
author wenzelm
Sun, 20 Jun 2021 16:26:18 +0200
changeset 60306 51ec2e101e9f
parent 60303 815b0dc8b589
child 60309 70a1d102660d
permissions -rw-r--r--
Isar command 'problem' as combination of KEStore_Elems.add_pbts + Problem.prep_import, without change of semantics;
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 
wneuper@59553
    11
imports Poly GCD_Poly_ML
neuper@48884
    12
begin
neuper@37906
    13
walther@59878
    14
section \<open>Constants for evaluation by "Rule.Eval"\<close>
neuper@37906
    15
consts
neuper@37906
    16
walther@60278
    17
  is_expanded    :: "real => bool" ("_ is'_expanded")     (*RL->Poly.thy*)
walther@60278
    18
  is_ratpolyexp  :: "real => bool" ("_ is'_ratpolyexp") 
jan@42300
    19
  get_denominator :: "real => real"
walther@60017
    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
walther@60275
    28
  | is_ratpolyexp (Const ("Transcendental.powr",_) $ 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))
walther@60275
    36
  | is_ratpolyexp (Const ("Transcendental.powr",_) $ 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
walther@60278
    42
(*("is_ratpolyexp", ("Rational.is_ratpolyexp", eval_is_ratpolyexp ""))*)
neuper@52105
    43
fun eval_is_ratpolyexp (thmid:string) _ 
walther@60278
    44
		       (t as (Const("Rational.is_ratpolyexp", _) $ arg)) thy =
neuper@52105
    45
    if is_ratpolyexp arg
walther@59870
    46
    then SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "", 
wneuper@59390
    47
	         HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
walther@59870
    48
    else SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy 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", _) $
walther@59603
    55
              (Const ("Rings.divide_class.divide", _) $ _(*num*) $
neuper@52105
    56
                denom)) thy = 
walther@59870
    57
      SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy 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 = 
walther@59870
    66
    SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy 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
			  
walther@60242
   101
  rat_power:               "(a / b) \<up> n = (a \<up> n) / (b \<up> 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@59532
   124
fun monom_of_term vs (c, es) (t as Const _) =
wneuper@59532
   125
    (c, list_update es (find_index (curry op = t) vs) 1)
wneuper@59532
   126
  | monom_of_term  vs (c, es) (t as Free (id, _)) =
wneuper@59390
   127
    if TermC.is_num' id 
walther@59875
   128
    then (id |> TermC.int_opt_of_string |> the |> curry op * c, es) (*several numerals in one monom*)
wneuper@59532
   129
    else (c, list_update es (find_index (curry op = t) vs) 1)
walther@60275
   130
  | monom_of_term  vs (c, es) (Const ("Transcendental.powr", _) $ (t as Free _) $ Free (e, _)) =
walther@59875
   131
    (c, list_update es (find_index (curry op = t) vs) (the (TermC.int_opt_of_string 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
walther@59868
   135
  | monom_of_term _ _ t = raise ERROR ("poly malformed 1 with " ^ UnparseC.term 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]
walther@60275
   141
  | monoms_of_term vs (t as Const ("Transcendental.powr", _) $ _ $  _) =
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)
walther@59868
   147
  | monoms_of_term _ t = raise ERROR ("poly malformed 2 with " ^ UnparseC.term 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 =
wneuper@59532
   167
  let
wneuper@59532
   168
    val vs = TermC.vars_of t
neuper@52087
   169
  in 
neuper@52087
   170
    case poly_of_term vs t of SOME _ => true | NONE => false
neuper@52087
   171
  end
neuper@52105
   172
val is_expanded = is_poly   (* TODO: check names *)
neuper@52105
   173
val is_polynomial = is_poly (* TODO: check names *)
wneuper@59472
   174
\<close>
neuper@52087
   175
wneuper@59472
   176
subsubsection \<open>Convert internal representation of a multivariate polynomial to a term\<close>
wneuper@59472
   177
ML \<open>
neuper@52087
   178
fun term_of_es _ _ _ [] = [] (*assumes same length for vs and es*)
wneuper@59532
   179
  | term_of_es baseT expT (_ :: vs) (0 :: es) = [] @ term_of_es baseT expT vs es
wneuper@59532
   180
  | term_of_es baseT expT (v :: vs) (1 :: es) = v :: term_of_es baseT expT vs es
neuper@52087
   181
  | term_of_es baseT expT (v :: vs) (e :: es) =
walther@60275
   182
    Const ("Transcendental.powr", [baseT, expT] ---> baseT) $ v $  (Free (TermC.isastr_of_int e, expT))
wneuper@59532
   183
    :: term_of_es baseT expT vs es
wneuper@59532
   184
  | term_of_es _ _ _ _ = raise ERROR "term_of_es: length vs <> length es"
neuper@52087
   185
neuper@52087
   186
fun term_of_monom baseT expT vs ((c, es): monom) =
neuper@52087
   187
    let val es' = term_of_es baseT expT vs es
neuper@52087
   188
    in 
neuper@52087
   189
      if c = 1 
neuper@52087
   190
      then 
neuper@52087
   191
        if es' = [] (*if es = [0,0,0,...]*)
wneuper@59389
   192
        then Free (TermC.isastr_of_int c, baseT)
neuper@52087
   193
        else foldl (HOLogic.mk_binop "Groups.times_class.times") (hd es', tl es')
wneuper@59389
   194
      else foldl (HOLogic.mk_binop "Groups.times_class.times") (Free (TermC.isastr_of_int c, baseT), es') 
neuper@52087
   195
    end
neuper@52087
   196
neuper@52087
   197
fun term_of_poly baseT expT vs p =
wneuper@59190
   198
  let val monos = map (term_of_monom baseT expT vs) p
neuper@52087
   199
  in foldl (HOLogic.mk_binop "Groups.plus_class.plus") (hd monos, tl monos) end
wneuper@59472
   200
\<close>
neuper@52087
   201
wneuper@59472
   202
subsection \<open>Apply gcd_poly for cancelling and adding fractions as terms\<close>
wneuper@59472
   203
ML \<open>
neuper@52091
   204
fun mk_noteq_0 baseT t = 
neuper@52091
   205
  Const ("HOL.Not", HOLogic.boolT --> HOLogic.boolT) $ 
neuper@52091
   206
    (Const ("HOL.eq", [baseT, baseT] ---> HOLogic.boolT) $ t $ Free ("0", HOLogic.realT))
neuper@52091
   207
neuper@52094
   208
fun mk_asms baseT ts =
wneuper@59389
   209
  let val as' = filter_out TermC.is_num ts (* asm like "2 ~= 0" is needless *)
neuper@52094
   210
  in map (mk_noteq_0 baseT) as' end
wneuper@59472
   211
\<close>
neuper@52094
   212
wneuper@59472
   213
subsubsection \<open>Factor out gcd for cancellation\<close>
wneuper@59472
   214
ML \<open>
neuper@52091
   215
fun check_fraction t =
walther@60269
   216
  case t of
walther@60269
   217
    Const ("Rings.divide_class.divide", _) $ numerator $ denominator
walther@60269
   218
      => SOME (numerator, denominator)
walther@60269
   219
  | _ => NONE
neuper@52091
   220
neuper@52091
   221
(* prepare a term for cancellation by factoring out the gcd
neuper@52091
   222
  assumes: is a fraction with outmost "/"*)
neuper@52091
   223
fun factout_p_ (thy: theory) t =
neuper@52091
   224
  let val opt = check_fraction t
neuper@52091
   225
  in
neuper@52091
   226
    case opt of 
neuper@52091
   227
      NONE => NONE
neuper@52091
   228
    | SOME (numerator, denominator) =>
wneuper@59532
   229
      let
wneuper@59532
   230
        val vs = TermC.vars_of t
neuper@52091
   231
        val baseT = type_of numerator
neuper@52091
   232
        val expT = HOLogic.realT
neuper@52091
   233
      in
neuper@52091
   234
        case (poly_of_term vs numerator, poly_of_term vs denominator) of
neuper@52091
   235
          (SOME a, SOME b) =>
neuper@52091
   236
            let
neuper@52091
   237
              val ((a', b'), c) = gcd_poly a b
neuper@52096
   238
              val es = replicate (length vs) 0 
neuper@52096
   239
            in
neuper@52096
   240
              if c = [(1, es)] orelse c = [(~1, es)]
neuper@52096
   241
              then NONE
neuper@52096
   242
              else 
neuper@52096
   243
                let
neuper@52096
   244
                  val b't = term_of_poly baseT expT vs b'
neuper@52096
   245
                  val ct = term_of_poly baseT expT vs c
neuper@52096
   246
                  val t' = 
wneuper@59360
   247
                    HOLogic.mk_binop "Rings.divide_class.divide" 
neuper@52096
   248
                      (HOLogic.mk_binop "Groups.times_class.times"
wneuper@59190
   249
                        (term_of_poly baseT expT vs a', ct),
neuper@52096
   250
                       HOLogic.mk_binop "Groups.times_class.times" (b't, ct))
neuper@52105
   251
                in SOME (t', mk_asms baseT [b't, ct]) end
neuper@52096
   252
            end
neuper@52091
   253
        | _ => NONE : (term * term list) option
neuper@52091
   254
      end
neuper@52091
   255
  end
wneuper@59472
   256
\<close>
neuper@52091
   257
wneuper@59472
   258
subsubsection \<open>Cancel a fraction\<close>
wneuper@59472
   259
ML \<open>
neuper@52096
   260
(* cancel a term by the gcd ("" denote terms with internal algebraic structure)
neuper@52096
   261
  cancel_p_ :: theory \<Rightarrow> term  \<Rightarrow> (term \<times> term list) option
neuper@52096
   262
  cancel_p_ thy "a / b" = SOME ("a' / b'", ["b' \<noteq> 0"])
neuper@52096
   263
  assumes: a is_polynomial  \<and>  b is_polynomial  \<and>  b \<noteq> 0
neuper@52096
   264
  yields
neuper@52096
   265
    SOME ("a' / b'", ["b' \<noteq> 0"]). gcd_poly a b \<noteq> 1  \<and>  gcd_poly a b \<noteq> -1  \<and>  
neuper@52096
   266
      a' * gcd_poly a b = a  \<and>  b' * gcd_poly a b = b
neuper@52096
   267
    \<or> NONE *)
neuper@52101
   268
fun cancel_p_ (_: theory) t =
neuper@52091
   269
  let val opt = check_fraction t
neuper@52091
   270
  in
neuper@52091
   271
    case opt of 
neuper@52091
   272
      NONE => NONE
neuper@52091
   273
    | SOME (numerator, denominator) =>
wneuper@59532
   274
      let
wneuper@59532
   275
        val vs = TermC.vars_of t
neuper@52091
   276
        val baseT = type_of numerator
neuper@52091
   277
        val expT = HOLogic.realT
neuper@52091
   278
      in
neuper@52091
   279
        case (poly_of_term vs numerator, poly_of_term vs denominator) of
neuper@52091
   280
          (SOME a, SOME b) =>
neuper@52091
   281
            let
neuper@52091
   282
              val ((a', b'), c) = gcd_poly a b
neuper@52096
   283
              val es = replicate (length vs) 0 
neuper@52096
   284
            in
neuper@52096
   285
              if c = [(1, es)] orelse c = [(~1, es)]
neuper@52096
   286
              then NONE
neuper@52096
   287
              else 
neuper@52096
   288
                let
neuper@52096
   289
                  val bt' = term_of_poly baseT expT vs b'
neuper@52096
   290
                  val ct = term_of_poly baseT expT vs c
neuper@52096
   291
                  val t' = 
wneuper@59360
   292
                    HOLogic.mk_binop "Rings.divide_class.divide" 
wneuper@59190
   293
                      (term_of_poly baseT expT vs a', bt')
neuper@52096
   294
                  val asm = mk_asms baseT [bt']
neuper@52096
   295
                in SOME (t', asm) end
neuper@52096
   296
            end
neuper@52091
   297
        | _ => NONE : (term * term list) option
neuper@52091
   298
      end
neuper@52091
   299
  end
wneuper@59472
   300
\<close>
neuper@52091
   301
wneuper@59472
   302
subsubsection \<open>Factor out to a common denominator for addition\<close>
wneuper@59472
   303
ML \<open>
neuper@52101
   304
(* addition of fractions allows (at most) one non-fraction (a monomial) *)
neuper@52101
   305
fun check_frac_sum 
neuper@52091
   306
    (Const ("Groups.plus_class.plus", _) $ 
wneuper@59360
   307
      (Const ("Rings.divide_class.divide", _) $ n1 $ d1) $
wneuper@59360
   308
      (Const ("Rings.divide_class.divide", _) $ n2 $ d2))
neuper@52091
   309
    = SOME ((n1, d1), (n2, d2))
neuper@52101
   310
  | check_frac_sum 
neuper@52091
   311
    (Const ("Groups.plus_class.plus", _) $ 
neuper@52091
   312
      nofrac $ 
wneuper@59360
   313
      (Const ("Rings.divide_class.divide", _) $ n2 $ d2))
neuper@52091
   314
    = SOME ((nofrac, Free ("1", HOLogic.realT)), (n2, d2))
neuper@52101
   315
  | check_frac_sum 
neuper@52091
   316
    (Const ("Groups.plus_class.plus", _) $ 
wneuper@59360
   317
      (Const ("Rings.divide_class.divide", _) $ n1 $ d1) $ 
neuper@52091
   318
      nofrac)
neuper@52091
   319
    = SOME ((n1, d1), (nofrac, Free ("1", HOLogic.realT)))
neuper@52101
   320
  | check_frac_sum _ = NONE  
neuper@52091
   321
neuper@52091
   322
(* prepare a term for addition by providing the least common denominator as a product
neuper@52091
   323
  assumes: is a term with outmost "+" and at least one outmost "/" in respective summands*)
neuper@52101
   324
fun common_nominator_p_ (_: theory) t =
neuper@52101
   325
  let val opt = check_frac_sum t
neuper@52091
   326
  in
neuper@52091
   327
    case opt of 
neuper@52091
   328
      NONE => NONE
neuper@52091
   329
    | SOME ((n1, d1), (n2, d2)) =>
wneuper@59532
   330
      let
wneuper@59532
   331
        val vs = TermC.vars_of t
neuper@52091
   332
      in
neuper@52091
   333
        case (poly_of_term vs d1, poly_of_term vs d2) of
neuper@52091
   334
          (SOME a, SOME b) =>
neuper@52091
   335
            let
neuper@52091
   336
              val ((a', b'), c) = gcd_poly a b
neuper@52101
   337
              val (baseT, expT) = (type_of n1, HOLogic.realT)
wneuper@59190
   338
              val [d1', d2', c'] = map (term_of_poly baseT expT vs) [a', b', c]
neuper@52091
   339
              (*----- minimum of parentheses & nice result, but breaks tests: -------------
neuper@52091
   340
              val denom = HOLogic.mk_binop "Groups.times_class.times" 
neuper@52101
   341
                (HOLogic.mk_binop "Groups.times_class.times" (d1', d2'), c') -------------*)
neuper@52101
   342
              val denom =
neuper@52101
   343
                if c = [(1, replicate (length vs) 0)]
neuper@52101
   344
                then HOLogic.mk_binop "Groups.times_class.times" (d1', d2')
neuper@52101
   345
                else
neuper@52101
   346
                  HOLogic.mk_binop "Groups.times_class.times" (c',
neuper@52101
   347
                  HOLogic.mk_binop "Groups.times_class.times" (d1', d2')) (*--------------*)
neuper@52091
   348
              val t' =
neuper@52091
   349
                HOLogic.mk_binop "Groups.plus_class.plus"
wneuper@59360
   350
                  (HOLogic.mk_binop "Rings.divide_class.divide"
neuper@52091
   351
                    (HOLogic.mk_binop "Groups.times_class.times" (n1, d2'), denom),
wneuper@59360
   352
                  HOLogic.mk_binop "Rings.divide_class.divide" 
neuper@52091
   353
                    (HOLogic.mk_binop "Groups.times_class.times" (n2, d1'), denom))
neuper@52094
   354
              val asm = mk_asms baseT [d1', d2', c']
neuper@52091
   355
            in SOME (t', asm) end
neuper@52091
   356
        | _ => NONE : (term * term list) option
neuper@52091
   357
      end
neuper@52091
   358
  end
wneuper@59472
   359
\<close>
neuper@52105
   360
wneuper@59472
   361
subsubsection \<open>Addition of at least one fraction within a sum\<close>
wneuper@59472
   362
ML \<open>
neuper@52091
   363
(* add fractions
neuper@52100
   364
  assumes: is a term with outmost "+" and at least one outmost "/" in respective summands
neuper@52100
   365
  NOTE: the case "(_ + _) + _" need not be considered due to iterated addition.*)
neuper@52105
   366
fun add_fraction_p_ (_: theory) t =
neuper@52101
   367
  case check_frac_sum t of 
neuper@52101
   368
    NONE => NONE
neuper@52101
   369
  | SOME ((n1, d1), (n2, d2)) =>
wneuper@59532
   370
    let
wneuper@59532
   371
      val vs = TermC.vars_of t
neuper@52101
   372
    in
neuper@52101
   373
      case (poly_of_term vs n1, poly_of_term vs d1, poly_of_term vs n2, poly_of_term vs d2) of
neuper@52101
   374
        (SOME _, SOME a, SOME _, SOME b) =>
neuper@52101
   375
          let
neuper@52101
   376
            val ((a', b'), c) = gcd_poly a b
neuper@52101
   377
            val (baseT, expT) = (type_of n1, HOLogic.realT)
neuper@52101
   378
            val nomin = term_of_poly baseT expT vs 
neuper@52101
   379
              (((the (poly_of_term vs n1)) %%*%% b') %%+%% ((the (poly_of_term vs n2)) %%*%% a')) 
neuper@52101
   380
            val denom = term_of_poly baseT expT vs ((c %%*%% a') %%*%% b')
wneuper@59360
   381
            val t' = HOLogic.mk_binop "Rings.divide_class.divide" (nomin, denom)
neuper@52101
   382
          in SOME (t', mk_asms baseT [denom]) end
neuper@52101
   383
      | _ => NONE : (term * term list) option
neuper@52101
   384
    end
wneuper@59472
   385
\<close>
neuper@52091
   386
wneuper@59472
   387
section \<open>Embed cancellation and addition into rewriting\<close>
wenzelm@60291
   388
wneuper@59472
   389
subsection \<open>Rulesets and predicate for embedding\<close>
wneuper@59472
   390
ML \<open>
neuper@52105
   391
(* evaluates conditions in calculate_Rational *)
neuper@52105
   392
val calc_rat_erls =
s1210629013@55444
   393
  prep_rls'
walther@59857
   394
    (Rule_Def.Repeat {id = "calc_rat_erls", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
walther@59852
   395
      erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
neuper@52105
   396
      rules = 
wenzelm@60294
   397
        [\<^rule_eval>\<open>HOL.eq\<close> (Prog_Expr.eval_equal "#equal_"),
wenzelm@60294
   398
        \<^rule_eval>\<open>Prog_Expr.is_const\<close> (Prog_Expr.eval_const "#is_const_"),
wenzelm@60297
   399
        \<^rule_thm>\<open>not_true\<close>,
wenzelm@60297
   400
        \<^rule_thm>\<open>not_false\<close>], 
walther@59878
   401
      scr = Rule.Empty_Prog});
neuper@37950
   402
neuper@52105
   403
(* simplifies expressions with numerals;
neuper@52105
   404
   does NOT rearrange the term by AC-rewriting; thus terms with variables 
neuper@52105
   405
   need to have constants to be commuted together respectively           *)
neuper@52105
   406
val calculate_Rational =
walther@59852
   407
  prep_rls' (Rule_Set.merge "calculate_Rational"
walther@59857
   408
    (Rule_Def.Repeat {id = "divide", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
walther@59851
   409
      erls = calc_rat_erls, srls = Rule_Set.Empty,
neuper@52105
   410
      calc = [], errpatts = [],
neuper@52105
   411
      rules = 
wenzelm@60294
   412
        [\<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e"),
neuper@37950
   413
walther@59871
   414
        Rule.Thm ("minus_divide_left", ThmC.numerals_to_Free (@{thm minus_divide_left} RS @{thm sym})),
neuper@52105
   415
          (*SYM - ?x / ?y = - (?x / ?y)  may come from subst*)
wenzelm@60297
   416
        \<^rule_thm>\<open>rat_add\<close>,
neuper@52105
   417
          (*"[| a is_const; b is_const; c is_const; d is_const |] ==> \
neuper@52105
   418
          \a / c + b / d = (a * d) / (c * d) + (b * c ) / (d * c)"*)
wenzelm@60297
   419
        \<^rule_thm>\<open>rat_add1\<close>,
neuper@52105
   420
          (*"[| a is_const; b is_const; c is_const |] ==> a / c + b / c = (a + b) / c"*)
wenzelm@60297
   421
        \<^rule_thm>\<open>rat_add2\<close>,
neuper@52105
   422
          (*"[| ?a is_const; ?b is_const; ?c is_const |] ==> ?a / ?c + ?b = (?a + ?b * ?c) / ?c"*)
wenzelm@60297
   423
        \<^rule_thm>\<open>rat_add3\<close>,
neuper@52105
   424
          (*"[| a is_const; b is_const; c is_const |] ==> a + b / c = (a * c) / c + b / c"\
neuper@52105
   425
          .... is_const to be omitted here FIXME*)
neuper@52105
   426
        
wenzelm@60297
   427
        \<^rule_thm>\<open>rat_mult\<close>, 
neuper@52105
   428
          (*a / b * (c / d) = a * c / (b * d)*)
wenzelm@60297
   429
        \<^rule_thm>\<open>times_divide_eq_right\<close>,
neuper@52105
   430
          (*?x * (?y / ?z) = ?x * ?y / ?z*)
wenzelm@60297
   431
        \<^rule_thm>\<open>times_divide_eq_left\<close>,
neuper@52105
   432
          (*?y / ?z * ?x = ?y * ?x / ?z*)
neuper@52105
   433
        
wenzelm@60297
   434
        \<^rule_thm>\<open>real_divide_divide1\<close>,
neuper@52105
   435
          (*"?y ~= 0 ==> ?u / ?v / (?y / ?z) = ?u / ?v * (?z / ?y)"*)
wenzelm@60297
   436
        \<^rule_thm>\<open>divide_divide_eq_left\<close>,
neuper@52105
   437
          (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
neuper@52105
   438
        
wenzelm@60297
   439
        \<^rule_thm>\<open>rat_power\<close>,
walther@60260
   440
          (*"(?a / ?b)  \<up>  ?n = ?a  \<up>  ?n / ?b  \<up>  ?n"*)
neuper@52105
   441
        
wenzelm@60297
   442
        \<^rule_thm>\<open>mult_cross\<close>,
neuper@52105
   443
          (*"[| b ~= 0; d ~= 0 |] ==> (a / b = c / d) = (a * d = b * c)*)
wenzelm@60297
   444
        \<^rule_thm>\<open>mult_cross1\<close>,
neuper@52105
   445
          (*"   b ~= 0            ==> (a / b = c    ) = (a     = b * c)*)
wenzelm@60297
   446
        \<^rule_thm>\<open>mult_cross2\<close>
neuper@52105
   447
          (*"           d ~= 0    ==> (a     = c / d) = (a * d =     c)*)], 
walther@59878
   448
      scr = Rule.Empty_Prog})
neuper@52105
   449
    calculate_Poly);
neuper@37950
   450
walther@60278
   451
(*("is_expanded", ("Rational.is_expanded", eval_is_expanded ""))*)
neuper@37950
   452
fun eval_is_expanded (thmid:string) _ 
walther@60278
   453
		       (t as (Const("Rational.is_expanded", _) $ arg)) thy = 
neuper@37950
   454
    if is_expanded arg
walther@59870
   455
    then SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "", 
wneuper@59390
   456
	         HOLogic.Trueprop $ (TermC.mk_equality (t, @{term True})))
walther@59870
   457
    else SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy arg) "", 
wneuper@59390
   458
	         HOLogic.Trueprop $ (TermC.mk_equality (t, @{term False})))
s1210629013@52159
   459
  | eval_is_expanded _ _ _ _ = NONE;
wneuper@59472
   460
\<close>
wneuper@59472
   461
setup \<open>KEStore_Elems.add_calcs
walther@60278
   462
  [("is_expanded", ("Rational.is_expanded", eval_is_expanded ""))]\<close>
wneuper@59472
   463
ML \<open>
neuper@37950
   464
val rational_erls = 
walther@59852
   465
  Rule_Set.merge "rational_erls" calculate_Rational 
walther@59852
   466
    (Rule_Set.append_rules "is_expanded" Atools_erls 
wenzelm@60294
   467
      [\<^rule_eval>\<open>is_expanded\<close> (eval_is_expanded "")]);
wneuper@59472
   468
\<close>
neuper@37950
   469
wneuper@59472
   470
subsection \<open>Embed cancellation into rewriting\<close>
wneuper@59472
   471
ML \<open>
walther@59603
   472
(**)local (* cancel_p *)
neuper@37950
   473
walther@59852
   474
val {rules = rules, rew_ord = (_, ro), ...} = Rule_Set.rep (assoc_rls' @{theory} "rev_rew_p");
neuper@37950
   475
neuper@52105
   476
fun init_state thy eval_rls ro t =
neuper@52105
   477
  let
neuper@52105
   478
    val SOME (t', _) = factout_p_ thy t;
neuper@52105
   479
    val SOME (t'', asm) = cancel_p_ thy t;
walther@59907
   480
    val der = Derive.steps_reverse thy eval_rls rules ro NONE t';
neuper@52105
   481
    val der = der @ 
wenzelm@60297
   482
      [(\<^rule_thm>\<open>real_mult_div_cancel2\<close>, (t'', asm))]
walther@60017
   483
    val rs = (Rule.distinct' o (map #1)) der
walther@60017
   484
  	val rs = filter_out (ThmC.member'
neuper@52105
   485
  	  ["sym_real_add_zero_left", "sym_real_mult_0", "sym_real_mult_1"]) rs
neuper@52105
   486
  in (t, t'', [rs(*one in order to ease locate_rule*)], der) end;
neuper@37950
   487
neuper@52105
   488
fun locate_rule thy eval_rls ro [rs] t r =
walther@59876
   489
    if member op = ((map (Rule.thm_id)) rs) (Rule.thm_id r)
neuper@52105
   490
    then 
walther@59876
   491
      let val ropt = Rewrite.rewrite_ thy ro eval_rls true (Rule.thm r) t;
neuper@52105
   492
      in
neuper@52105
   493
        case ropt of SOME ta => [(r, ta)]
walther@59733
   494
	      | NONE => ((*tracing 
walther@59876
   495
	          ("### locate_rule:  rewrite " ^ Rule.thm_id r ^ " " ^ UnparseC.term t ^ " = NONE");*) []) 
neuper@52105
   496
			end
walther@59876
   497
    else ((*tracing ("### locate_rule:  " ^ Rule.thm_id r ^ " not mem rrls");*) [])
walther@59962
   498
  | locate_rule _ _ _ _ _ _ = raise ERROR "locate_rule: doesnt match rev-sets in istate";
neuper@37950
   499
neuper@52105
   500
fun next_rule thy eval_rls ro [rs] t =
neuper@52105
   501
    let
walther@59907
   502
      val der = Derive.do_one thy eval_rls rs ro NONE t;
neuper@52105
   503
    in case der of (_, r, _) :: _ => SOME r | _ => NONE end
walther@59962
   504
  | next_rule _ _ _ _ _ = raise ERROR ("next_rule: doesnt match rev-sets in istate");
neuper@37950
   505
wneuper@59416
   506
fun attach_form (_: Rule.rule list list) (_: term) (_: term) = 
wneuper@59416
   507
  [(*TODO*)]: ( Rule.rule * (term * term list)) list;
neuper@37950
   508
walther@59861
   509
(**)in(**)
neuper@37950
   510
neuper@52105
   511
val cancel_p = 
walther@59850
   512
  Rule_Set.Rrls {id = "cancel_p", prepat = [],
wenzelm@60291
   513
	rew_ord=("ord_make_polynomial", ord_make_polynomial false \<^theory>),
neuper@52105
   514
	erls = rational_erls, 
neuper@52105
   515
	calc = 
walther@59603
   516
	  [("PLUS", ("Groups.plus_class.plus", (**)eval_binop "#add_")),
walther@59603
   517
	  ("TIMES" , ("Groups.times_class.times", (**)eval_binop "#mult_")),
walther@59603
   518
	  ("DIVIDE", ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e")),
walther@60275
   519
	  ("POWER", ("Transcendental.powr", (**)eval_binop "#power_"))],
neuper@52105
   520
    errpatts = [],
neuper@52105
   521
	scr =
wenzelm@60291
   522
	  Rule.Rfuns {init_state  = init_state \<^theory> Atools_erls ro,
wenzelm@60291
   523
		normal_form = cancel_p_ \<^theory>, 
wenzelm@60291
   524
		locate_rule = locate_rule \<^theory> Atools_erls ro,
wenzelm@60291
   525
		next_rule   = next_rule \<^theory> Atools_erls ro,
neuper@52105
   526
		attach_form = attach_form}}
walther@59861
   527
(**)end(* local cancel_p *)
wneuper@59472
   528
\<close>
neuper@37950
   529
wneuper@59472
   530
subsection \<open>Embed addition into rewriting\<close>
wneuper@59472
   531
ML \<open>
walther@59861
   532
(**)local (* add_fractions_p *)
neuper@37950
   533
walther@59852
   534
(*val {rules = rules, rew_ord = (_, ro), ...} = Rule_Set.rep (assoc_rls "make_polynomial");*)
walther@59852
   535
val {rules, rew_ord=(_,ro),...} = Rule_Set.rep (assoc_rls' @{theory} "rev_rew_p");
neuper@37950
   536
neuper@52105
   537
fun init_state thy eval_rls ro t =
neuper@52105
   538
  let 
neuper@52105
   539
    val SOME (t',_) = common_nominator_p_ thy t;
neuper@52105
   540
    val SOME (t'', asm) = add_fraction_p_ thy t;
walther@59907
   541
    val der = Derive.steps_reverse thy eval_rls rules ro NONE t';
neuper@52105
   542
    val der = der @ 
wenzelm@60297
   543
      [(\<^rule_thm>\<open>real_mult_div_cancel2\<close>, (t'',asm))]
walther@60017
   544
    val rs = (Rule.distinct' o (map #1)) der;
walther@60017
   545
    val rs = filter_out (ThmC.member'
neuper@52105
   546
      ["sym_real_add_zero_left", "sym_real_mult_0", "sym_real_mult_1"]) rs;
neuper@52105
   547
  in (t, t'', [rs(*here only _ONE_*)], der) end;
neuper@37950
   548
neuper@52105
   549
fun locate_rule thy eval_rls ro [rs] t r =
walther@59876
   550
    if member op = ((map (Rule.thm_id)) rs) (Rule.thm_id r)
neuper@52105
   551
    then 
walther@59876
   552
      let val ropt = Rewrite.rewrite_ thy ro eval_rls true (Rule.thm r) t;
neuper@52105
   553
      in 
neuper@52105
   554
        case ropt of
neuper@52105
   555
          SOME ta => [(r, ta)]
neuper@52105
   556
	      | NONE => 
walther@59876
   557
	        ((*tracing ("### locate_rule:  rewrite " ^ Rule.thm_id r ^ " " ^ UnparseC.term t ^ " = NONE");*)
neuper@52105
   558
	        []) end
walther@59876
   559
    else ((*tracing ("### locate_rule:  " ^ Rule.thm_id r ^ " not mem rrls");*) [])
walther@59962
   560
  | locate_rule _ _ _ _ _ _ = raise ERROR "locate_rule: doesnt match rev-sets in istate";
neuper@37950
   561
neuper@37950
   562
fun next_rule thy eval_rls ro [rs] t =
walther@59907
   563
    let val der = Derive.do_one thy eval_rls rs ro NONE t;
neuper@52105
   564
    in 
neuper@52105
   565
      case der of
neuper@52105
   566
	      (_,r,_)::_ => SOME r
neuper@52105
   567
	    | _ => NONE
neuper@37950
   568
    end
walther@59962
   569
  | next_rule _ _ _ _ _ = raise ERROR ("next_rule: doesnt match rev-sets in istate");
neuper@37950
   570
wenzelm@60291
   571
val pat0 = TermC.parse_patt \<^theory> "?r/?s+?u/?v :: real";
wenzelm@60291
   572
val pat1 = TermC.parse_patt \<^theory> "?r/?s+?u    :: real";
wenzelm@60291
   573
val pat2 = TermC.parse_patt \<^theory> "?r   +?u/?v :: real";
neuper@48760
   574
val prepat = [([@{term True}], pat0),
neuper@48760
   575
	      ([@{term True}], pat1),
neuper@48760
   576
	      ([@{term True}], pat2)];
walther@59861
   577
(**)in(**)
neuper@37950
   578
neuper@52105
   579
val add_fractions_p =
walther@59850
   580
  Rule_Set.Rrls {id = "add_fractions_p", prepat=prepat,
wenzelm@60291
   581
    rew_ord = ("ord_make_polynomial", ord_make_polynomial false \<^theory>),
neuper@52105
   582
    erls = rational_erls,
walther@59603
   583
    calc = [("PLUS", ("Groups.plus_class.plus", (**)eval_binop "#add_")),
walther@59603
   584
      ("TIMES", ("Groups.times_class.times", (**)eval_binop "#mult_")),
walther@59603
   585
      ("DIVIDE", ("Rings.divide_class.divide", Prog_Expr.eval_cancel "#divide_e")),
walther@60275
   586
      ("POWER", ("Transcendental.powr", (**)eval_binop "#power_"))],
neuper@52105
   587
    errpatts = [],
wenzelm@60291
   588
    scr = Rule.Rfuns {init_state  = init_state \<^theory> Atools_erls ro,
wenzelm@60291
   589
      normal_form = add_fraction_p_ \<^theory>,
wenzelm@60291
   590
      locate_rule = locate_rule \<^theory> Atools_erls ro,
wenzelm@60291
   591
      next_rule   = next_rule \<^theory> Atools_erls ro,
neuper@52105
   592
      attach_form = attach_form}}
walther@59861
   593
(**)end(*local add_fractions_p *)
wneuper@59472
   594
\<close>
neuper@37950
   595
wneuper@59472
   596
subsection \<open>Cancelling and adding all occurrences in a term /////////////////////////////\<close>
wneuper@59472
   597
ML \<open>
neuper@52105
   598
(*copying cancel_p_rls + add her caused error in interface.sml*)
wneuper@59472
   599
\<close>
neuper@42451
   600
wneuper@59472
   601
section \<open>Rulesets for general simplification\<close>
wneuper@59472
   602
ML \<open>
neuper@37950
   603
(*erls for calculate_Rational; make local with FIXX@ME result:term *term list*)
s1210629013@55444
   604
val powers_erls = prep_rls'(
walther@59857
   605
  Rule_Def.Repeat {id = "powers_erls", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
walther@59852
   606
      erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
wenzelm@60294
   607
      rules = [\<^rule_eval>\<open>Prog_Expr.is_atom\<close> (Prog_Expr.eval_is_atom "#is_atom_"),
wenzelm@60294
   608
	       \<^rule_eval>\<open>Prog_Expr.is_even\<close> (Prog_Expr.eval_is_even "#is_even_"),
wenzelm@60294
   609
	       \<^rule_eval>\<open>less\<close> (Prog_Expr.eval_equ "#less_"),
wenzelm@60297
   610
	       \<^rule_thm>\<open>not_false\<close>,
wenzelm@60297
   611
	       \<^rule_thm>\<open>not_true\<close>,
wenzelm@60294
   612
	       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
neuper@37950
   613
	       ],
walther@59878
   614
      scr = Rule.Empty_Prog
wneuper@59406
   615
      });
neuper@37950
   616
(*.all powers over + distributed; atoms over * collected, other distributed
neuper@37950
   617
   contains absolute minimum of thms for context in norm_Rational .*)
s1210629013@55444
   618
val powers = prep_rls'(
walther@59857
   619
  Rule_Def.Repeat {id = "powers", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
walther@59851
   620
      erls = powers_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
wenzelm@60297
   621
      rules = [\<^rule_thm>\<open>realpow_multI\<close>,
walther@60260
   622
	       (*"(r * s)  \<up>  n = r  \<up>  n * s  \<up>  n"*)
wenzelm@60297
   623
	       \<^rule_thm>\<open>realpow_pow\<close>,
walther@60260
   624
	       (*"(a  \<up>  b)  \<up>  c = a  \<up>  (b * c)"*)
wenzelm@60297
   625
	       \<^rule_thm>\<open>realpow_oneI\<close>,
walther@60260
   626
	       (*"r  \<up>  1 = r"*)
wenzelm@60297
   627
	       \<^rule_thm>\<open>realpow_minus_even\<close>,
walther@60260
   628
	       (*"n is_even ==> (- r)  \<up>  n = r  \<up>  n" ?-->discard_minus?*)
wenzelm@60297
   629
	       \<^rule_thm>\<open>realpow_minus_odd\<close>,
walther@60260
   630
	       (*"Not (n is_even) ==> (- r)  \<up>  n = -1 * r  \<up>  n"*)
neuper@37950
   631
	       
neuper@37950
   632
	       (*----- collect atoms over * -----*)
wenzelm@60297
   633
	       \<^rule_thm>\<open>realpow_two_atom\<close>,	
walther@60260
   634
	       (*"r is_atom ==> r * r = r  \<up>  2"*)
wenzelm@60297
   635
	       \<^rule_thm>\<open>realpow_plus_1\<close>,		
walther@60260
   636
	       (*"r is_atom ==> r * r  \<up>  n = r  \<up>  (n + 1)"*)
wenzelm@60297
   637
	       \<^rule_thm>\<open>realpow_addI_atom\<close>,
walther@60260
   638
	       (*"r is_atom ==> r  \<up>  n * r  \<up>  m = r  \<up>  (n + m)"*)
neuper@37950
   639
neuper@37950
   640
	       (*----- distribute none-atoms -----*)
wenzelm@60297
   641
	       \<^rule_thm>\<open>realpow_def_atom\<close>,
walther@60276
   642
	       (*"[| 1 < n; ~ (r is_atom) |]==>r  \<up>  n = r * r  \<up>  (n + -1)"*)
wenzelm@60297
   643
	       \<^rule_thm>\<open>realpow_eq_oneI\<close>,
walther@60260
   644
	       (*"1  \<up>  n = 1"*)
wenzelm@60294
   645
	       \<^rule_eval>\<open>plus\<close> (**)(eval_binop "#add_")
neuper@37950
   646
	       ],
walther@59878
   647
      scr = Rule.Empty_Prog
wneuper@59406
   648
      });
neuper@37950
   649
(*.contains absolute minimum of thms for context in norm_Rational.*)
s1210629013@55444
   650
val rat_mult_divide = prep_rls'(
walther@59851
   651
  Rule_Def.Repeat {id = "rat_mult_divide", preconds = [], 
walther@59857
   652
      rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
walther@59852
   653
      erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
wenzelm@60297
   654
      rules = [\<^rule_thm>\<open>rat_mult\<close>,
neuper@37950
   655
	       (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
wenzelm@60297
   656
	       \<^rule_thm>\<open>times_divide_eq_right\<close>,
neuper@37950
   657
	       (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
neuper@37950
   658
	       otherwise inv.to a / b / c = ...*)
wenzelm@60297
   659
	       \<^rule_thm>\<open>times_divide_eq_left\<close>,
walther@60260
   660
	       (*"?a / ?b * ?c = ?a * ?c / ?b" order weights x \<up> n too much
walther@60260
   661
		     and does not commute a / b * c  \<up>  2 !*)
neuper@37950
   662
	       
wenzelm@60298
   663
	       \<^rule_thm>\<open>divide_divide_eq_right\<close>,
neuper@37950
   664
	       (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
wenzelm@60298
   665
	       \<^rule_thm>\<open>divide_divide_eq_left\<close>,
neuper@37950
   666
	       (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
wenzelm@60294
   667
	       \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e")
neuper@37950
   668
	       ],
walther@59878
   669
      scr = Rule.Empty_Prog
wneuper@59406
   670
      });
neuper@37979
   671
neuper@37950
   672
(*.contains absolute minimum of thms for context in norm_Rational.*)
s1210629013@55444
   673
val reduce_0_1_2 = prep_rls'(
walther@59857
   674
  Rule_Def.Repeat{id = "reduce_0_1_2", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
walther@59852
   675
      erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
wenzelm@60297
   676
      rules = [(*\<^rule_thm>\<open>divide_1\<close>,
neuper@37950
   677
		 "?x / 1 = ?x" unnecess.for normalform*)
wenzelm@60297
   678
	       \<^rule_thm>\<open>mult_1_left\<close>,                 
neuper@37950
   679
	       (*"1 * z = z"*)
wenzelm@60297
   680
	       (*\<^rule_thm>\<open>real_mult_minus1\<close>,
neuper@37950
   681
	       "-1 * z = - z"*)
wenzelm@60297
   682
	       (*\<^rule_thm>\<open>real_minus_mult_cancel\<close>,
neuper@37950
   683
	       "- ?x * - ?y = ?x * ?y"*)
neuper@37950
   684
wenzelm@60297
   685
	       \<^rule_thm>\<open>mult_zero_left\<close>,        
neuper@37950
   686
	       (*"0 * z = 0"*)
wenzelm@60297
   687
	       \<^rule_thm>\<open>add_0_left\<close>,
neuper@37950
   688
	       (*"0 + z = z"*)
wenzelm@60297
   689
	       (*\<^rule_thm>\<open>right_minus\<close>,
neuper@37950
   690
	       "?z + - ?z = 0"*)
neuper@37950
   691
wenzelm@60296
   692
	       \<^rule_thm_sym>\<open>real_mult_2\<close>,	
neuper@37950
   693
	       (*"z1 + z1 = 2 * z1"*)
wenzelm@60297
   694
	       \<^rule_thm>\<open>real_mult_2_assoc\<close>,
neuper@37950
   695
	       (*"z1 + (z1 + k) = 2 * z1 + k"*)
neuper@37950
   696
wenzelm@60297
   697
	       \<^rule_thm>\<open>division_ring_divide_zero\<close>
neuper@37950
   698
	       (*"0 / ?x = 0"*)
walther@59878
   699
	       ], scr = Rule.Empty_Prog});
neuper@37950
   700
neuper@37950
   701
(*erls for calculate_Rational; 
neuper@37950
   702
  make local with FIXX@ME result:term *term list WN0609???SKMG*)
s1210629013@55444
   703
val norm_rat_erls = prep_rls'(
walther@59857
   704
  Rule_Def.Repeat {id = "norm_rat_erls", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
walther@59852
   705
      erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
wenzelm@60294
   706
      rules = [\<^rule_eval>\<open>Prog_Expr.is_const\<close> (Prog_Expr.eval_const "#is_const_")
walther@59878
   707
	       ], scr = Rule.Empty_Prog});
neuper@37979
   708
neuper@52105
   709
(* consists of rls containing the absolute minimum of thms *)
neuper@37950
   710
(*040209: this version has been used by RL for his equations,
neuper@52105
   711
which is now replaced by MGs version "norm_Rational" below *)
s1210629013@55444
   712
val norm_Rational_min = prep_rls'(
walther@59857
   713
  Rule_Def.Repeat {id = "norm_Rational_min", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
walther@59851
   714
      erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
neuper@37950
   715
      rules = [(*sequence given by operator precedence*)
wneuper@59416
   716
	       Rule.Rls_ discard_minus,
wneuper@59416
   717
	       Rule.Rls_ powers,
wneuper@59416
   718
	       Rule.Rls_ rat_mult_divide,
wneuper@59416
   719
	       Rule.Rls_ expand,
wneuper@59416
   720
	       Rule.Rls_ reduce_0_1_2,
wneuper@59416
   721
	       Rule.Rls_ order_add_mult,
wneuper@59416
   722
	       Rule.Rls_ collect_numerals,
wneuper@59416
   723
	       Rule.Rls_ add_fractions_p,
wneuper@59416
   724
	       Rule.Rls_ cancel_p
neuper@37950
   725
	       ],
walther@59878
   726
      scr = Rule.Empty_Prog});
neuper@37979
   727
s1210629013@55444
   728
val norm_Rational_parenthesized = prep_rls'(
walther@59878
   729
  Rule_Set.Sequence {id = "norm_Rational_parenthesized", preconds = []:term list, 
walther@59857
   730
       rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord),
walther@59851
   731
      erls = Atools_erls, srls = Rule_Set.Empty,
neuper@42451
   732
      calc = [], errpatts = [],
wneuper@59416
   733
      rules = [Rule.Rls_  norm_Rational_min,
wneuper@59416
   734
	       Rule.Rls_ discard_parentheses
neuper@37950
   735
	       ],
walther@59878
   736
      scr = Rule.Empty_Prog});      
neuper@37950
   737
neuper@37950
   738
(*WN030318???SK: simplifies all but cancel and common_nominator*)
neuper@37950
   739
val simplify_rational = 
walther@59852
   740
    Rule_Set.merge "simplify_rational" expand_binoms
walther@59852
   741
    (Rule_Set.append_rules "divide" calculate_Rational
wenzelm@60297
   742
		[\<^rule_thm>\<open>div_by_1\<close>,
neuper@37950
   743
		 (*"?x / 1 = ?x"*)
wenzelm@60297
   744
		 \<^rule_thm>\<open>rat_mult\<close>,
neuper@37950
   745
		 (*(1)"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
wenzelm@60297
   746
		 \<^rule_thm>\<open>times_divide_eq_right\<close>,
neuper@37950
   747
		 (*(2)"?a * (?c / ?d) = ?a * ?c / ?d" must be [2],
neuper@37950
   748
		 otherwise inv.to a / b / c = ...*)
wenzelm@60297
   749
		 \<^rule_thm>\<open>times_divide_eq_left\<close>,
neuper@37950
   750
		 (*"?a / ?b * ?c = ?a * ?c / ?b"*)
wenzelm@60297
   751
		 \<^rule_thm>\<open>add_minus\<close>,
neuper@37950
   752
		 (*"?a + ?b - ?b = ?a"*)
wenzelm@60297
   753
		 \<^rule_thm>\<open>add_minus1\<close>,
neuper@37950
   754
		 (*"?a - ?b + ?b = ?a"*)
wenzelm@60297
   755
		 \<^rule_thm>\<open>divide_minus1\<close>
neuper@37950
   756
		 (*"?x / -1 = - ?x"*)
neuper@37950
   757
		 ]);
wneuper@59472
   758
\<close>
wneuper@59472
   759
ML \<open>
s1210629013@55444
   760
val add_fractions_p_rls = prep_rls'(
walther@59857
   761
  Rule_Def.Repeat {id = "add_fractions_p_rls", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
walther@59852
   762
	  erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
wneuper@59416
   763
	  rules = [Rule.Rls_ add_fractions_p], 
walther@59878
   764
	  scr = Rule.Empty_Prog});
neuper@37950
   765
walther@59851
   766
(* "Rule_Def.Repeat" causes repeated application of cancel_p to one and the same term *)
s1210629013@55444
   767
val cancel_p_rls = prep_rls'(
walther@59851
   768
  Rule_Def.Repeat 
walther@59857
   769
    {id = "cancel_p_rls", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
walther@59852
   770
    erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
wneuper@59416
   771
    rules = [Rule.Rls_ cancel_p], 
walther@59878
   772
	  scr = Rule.Empty_Prog});
neuper@52105
   773
neuper@37950
   774
(*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
neuper@37950
   775
    used in initial part norm_Rational_mg, see example DA-M02-main.p.60.*)
s1210629013@55444
   776
val rat_mult_poly = prep_rls'(
walther@59857
   777
  Rule_Def.Repeat {id = "rat_mult_poly", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
wenzelm@60294
   778
	  erls = Rule_Set.append_rules "Rule_Set.empty-is_polyexp" Rule_Set.empty
wenzelm@60294
   779
      [\<^rule_eval>\<open>is_polyexp\<close> (eval_is_polyexp "")],
walther@59851
   780
	  srls = Rule_Set.Empty, calc = [], errpatts = [],
neuper@52105
   781
	  rules = 
wenzelm@60297
   782
	    [\<^rule_thm>\<open>rat_mult_poly_l\<close>,
neuper@52105
   783
	    (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
wenzelm@60297
   784
	    \<^rule_thm>\<open>rat_mult_poly_r\<close>
neuper@52105
   785
	    (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*) ], 
walther@59878
   786
	  scr = Rule.Empty_Prog});
neuper@37979
   787
neuper@37950
   788
(*. makes 'normal' fractions; 'is_polyexp' inhibits double fractions;
neuper@37950
   789
    used in looping part norm_Rational_rls, see example DA-M02-main.p.60 
walther@59852
   790
    .. WHERE THE LATTER DOES ALWAYS WORK, BECAUSE erls = Rule_Set.empty, 
wneuper@59416
   791
    I.E. THE RESPECTIVE ASSUMPTION IS STORED AND Rule.Thm APPLIED; WN051028 
neuper@37950
   792
    ... WN0609???MG.*)
s1210629013@55444
   793
val rat_mult_div_pow = prep_rls'(
walther@59857
   794
  Rule_Def.Repeat {id = "rat_mult_div_pow", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
walther@59852
   795
    erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
wenzelm@60297
   796
    rules = [\<^rule_thm>\<open>rat_mult\<close>,
neuper@52105
   797
      (*"?a / ?b * (?c / ?d) = ?a * ?c / (?b * ?d)"*)
wenzelm@60297
   798
      \<^rule_thm>\<open>rat_mult_poly_l\<close>,
neuper@52105
   799
      (*"?c is_polyexp ==> ?c * (?a / ?b) = ?c * ?a / ?b"*)
wenzelm@60297
   800
      \<^rule_thm>\<open>rat_mult_poly_r\<close>,
neuper@52105
   801
      (*"?c is_polyexp ==> ?a / ?b * ?c = ?a * ?c / ?b"*)
neuper@52105
   802
      
wenzelm@60297
   803
      \<^rule_thm>\<open>real_divide_divide1_mg\<close>,
neuper@52105
   804
      (*"y ~= 0 ==> (u / v) / (y / z) = (u * z) / (y * v)"*)
wenzelm@60297
   805
      \<^rule_thm>\<open>divide_divide_eq_right\<close>,
neuper@52105
   806
      (*"?x / (?y / ?z) = ?x * ?z / ?y"*)
wenzelm@60297
   807
      \<^rule_thm>\<open>divide_divide_eq_left\<close>,
neuper@52105
   808
      (*"?x / ?y / ?z = ?x / (?y * ?z)"*)
wenzelm@60294
   809
      \<^rule_eval>\<open>divide\<close> (Prog_Expr.eval_cancel "#divide_e"),
neuper@52105
   810
      
wenzelm@60297
   811
      \<^rule_thm>\<open>rat_power\<close>
walther@60260
   812
      (*"(?a / ?b)  \<up>  ?n = ?a  \<up>  ?n / ?b  \<up>  ?n"*)
neuper@52105
   813
      ],
walther@59878
   814
    scr = Rule.Empty_Prog});
neuper@37950
   815
s1210629013@55444
   816
val rat_reduce_1 = prep_rls'(
walther@59857
   817
  Rule_Def.Repeat {id = "rat_reduce_1", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
walther@59852
   818
    erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [], 
neuper@52105
   819
    rules = 
wenzelm@60297
   820
      [\<^rule_thm>\<open>div_by_1\<close>,
neuper@52105
   821
      (*"?x / 1 = ?x"*)
wenzelm@60297
   822
      \<^rule_thm>\<open>mult_1_left\<close>           
neuper@52105
   823
      (*"1 * z = z"*)
neuper@52105
   824
      ],
walther@59878
   825
    scr = Rule.Empty_Prog});
neuper@52105
   826
neuper@52105
   827
(* looping part of norm_Rational *)
s1210629013@55444
   828
val norm_Rational_rls = prep_rls' (
walther@59857
   829
  Rule_Def.Repeat {id = "norm_Rational_rls", preconds = [], rew_ord = ("dummy_ord",Rewrite_Ord.dummy_ord), 
walther@59851
   830
    erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
wneuper@59416
   831
    rules = [Rule.Rls_ add_fractions_p_rls,
wneuper@59416
   832
      Rule.Rls_ rat_mult_div_pow,
wneuper@59416
   833
      Rule.Rls_ make_rat_poly_with_parentheses,
wneuper@59416
   834
      Rule.Rls_ cancel_p_rls,
wneuper@59416
   835
      Rule.Rls_ rat_reduce_1
neuper@52105
   836
      ],
walther@59878
   837
    scr = Rule.Empty_Prog});
neuper@52105
   838
s1210629013@55444
   839
val norm_Rational = prep_rls' (
walther@59878
   840
  Rule_Set.Sequence 
walther@59857
   841
    {id = "norm_Rational", preconds = [], rew_ord = ("dummy_ord", Rewrite_Ord.dummy_ord), 
walther@59851
   842
    erls = norm_rat_erls, srls = Rule_Set.Empty, calc = [], errpatts = [],
wneuper@59416
   843
    rules = [Rule.Rls_ discard_minus,
wneuper@59416
   844
      Rule.Rls_ rat_mult_poly,             (* removes double fractions like a/b/c *)
wneuper@59416
   845
      Rule.Rls_ make_rat_poly_with_parentheses,
wneuper@59416
   846
      Rule.Rls_ cancel_p_rls,
wneuper@59416
   847
      Rule.Rls_ norm_Rational_rls,         (* the main rls, looping (#) *)
wneuper@59416
   848
      Rule.Rls_ discard_parentheses1       (* mult only *)
neuper@52100
   849
      ],
walther@59878
   850
    scr = Rule.Empty_Prog});
wneuper@59472
   851
\<close>
neuper@52125
   852
wenzelm@60289
   853
rule_set_knowledge
wenzelm@60286
   854
  calculate_Rational = calculate_Rational and
wenzelm@60286
   855
  calc_rat_erls = calc_rat_erls and
wenzelm@60286
   856
  rational_erls = rational_erls and
wenzelm@60286
   857
  cancel_p = cancel_p and
wenzelm@60286
   858
  add_fractions_p = add_fractions_p and
wenzelm@60286
   859
wenzelm@60286
   860
  add_fractions_p_rls = add_fractions_p_rls and
wenzelm@60286
   861
  powers_erls = powers_erls and
wenzelm@60286
   862
  powers = powers and
wenzelm@60286
   863
  rat_mult_divide = rat_mult_divide and
wenzelm@60286
   864
  reduce_0_1_2 = reduce_0_1_2 and
wenzelm@60286
   865
wenzelm@60286
   866
  rat_reduce_1 = rat_reduce_1 and
wenzelm@60286
   867
  norm_rat_erls = norm_rat_erls and
wenzelm@60286
   868
  norm_Rational = norm_Rational and
wenzelm@60286
   869
  norm_Rational_rls = norm_Rational_rls and
wenzelm@60286
   870
  norm_Rational_min = norm_Rational_min and
wenzelm@60286
   871
  norm_Rational_parenthesized = norm_Rational_parenthesized and
wenzelm@60286
   872
wenzelm@60286
   873
  rat_mult_poly = rat_mult_poly and
wenzelm@60286
   874
  rat_mult_div_pow = rat_mult_div_pow and
wenzelm@60286
   875
  cancel_p_rls = cancel_p_rls
neuper@37950
   876
wneuper@59472
   877
section \<open>A problem for simplification of rationals\<close>
wenzelm@60306
   878
wenzelm@60306
   879
problem pbl_simp_rat : "rational/simplification" =
wenzelm@60306
   880
  \<open>Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)]\<close>
wenzelm@60306
   881
  Method: "simplification/of_rationals"
wenzelm@60306
   882
  CAS: "Simplify t_t"
wenzelm@60306
   883
  Given: "Term t_t"
wenzelm@60306
   884
  Where: "t_t is_ratpolyexp"
wenzelm@60306
   885
  Find: "normalform n_n"
neuper@37950
   886
wneuper@59472
   887
section \<open>A methods for simplification of rationals\<close>
s1210629013@55373
   888
(*WN061025 this methods script is copied from (auto-generated) script
s1210629013@55373
   889
  of norm_Rational in order to ease repair on inform*)
wneuper@59545
   890
wneuper@59504
   891
partial_function (tailrec) simplify :: "real \<Rightarrow> real"
wneuper@59504
   892
  where
walther@59716
   893
"simplify term = (
walther@59637
   894
  (Try (Rewrite_Set ''discard_minus'') #>
walther@59637
   895
   Try (Rewrite_Set ''rat_mult_poly'') #>
walther@59637
   896
   Try (Rewrite_Set ''make_rat_poly_with_parentheses'') #>
walther@59637
   897
   Try (Rewrite_Set ''cancel_p_rls'') #>
walther@59635
   898
   (Repeat (
walther@59637
   899
     Try (Rewrite_Set ''add_fractions_p_rls'') #>
walther@59637
   900
     Try (Rewrite_Set ''rat_mult_div_pow'') #>
walther@59637
   901
     Try (Rewrite_Set ''make_rat_poly_with_parentheses'') #>
walther@59637
   902
     Try (Rewrite_Set ''cancel_p_rls'') #>
walther@59637
   903
     Try (Rewrite_Set ''rat_reduce_1''))) #>
walther@59635
   904
   Try (Rewrite_Set ''discard_parentheses1''))
walther@59716
   905
   term)"
wenzelm@60303
   906
wenzelm@60303
   907
wenzelm@60303
   908
method met_simp_rat : "simplification/of_rationals" =
wenzelm@60303
   909
  \<open>{rew_ord'="tless_true", rls' = Rule_Set.empty, calc = [], srls = Rule_Set.empty, 
wenzelm@60303
   910
    prls = Rule_Set.append_rules "simplification_of_rationals_prls" Rule_Set.empty 
wenzelm@60303
   911
      [(*for preds in where_*) \<^rule_eval>\<open>is_ratpolyexp\<close> (eval_is_ratpolyexp "")],
wenzelm@60303
   912
    crls = Rule_Set.empty, errpats = [], nrls = norm_Rational_rls}\<close>
wenzelm@60303
   913
  Program: simplify.simps
wenzelm@60303
   914
  Given: "Term t_t"
wenzelm@60303
   915
  Where: "t_t is_ratpolyexp"
wenzelm@60303
   916
  Find: "normalform n_n"
wenzelm@60303
   917
ML \<open>
walther@60278
   918
\<close> ML \<open>
wneuper@59472
   919
\<close>
neuper@52105
   920
end