src/HOL/Tools/numeral_simprocs.ML
author haftmann
Mon, 06 Jul 2009 14:19:13 +0200
changeset 31949 3f933687fae9
parent 31368 763f4b0fd579
child 32155 e2bf2f73b0c8
permissions -rw-r--r--
moved Inductive.myinv to Fun.inv; tuned
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
     1
(* Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
     2
   Copyright   2000  University of Cambridge
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     3
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
     4
Simprocs for the integer numerals.
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     5
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     6
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     7
(*To quote from Provers/Arith/cancel_numeral_factor.ML:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     8
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
     9
Cancels common coefficients in balanced expressions:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    10
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    11
     u*#m ~~ u'*#m'  ==  #n*u ~~ #n'*u'
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    12
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    13
where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    14
and d = gcd(m,m') and n=m/d and n'=m'/d.
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    15
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
    16
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    17
signature NUMERAL_SIMPROCS =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    18
sig
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    19
  val mk_sum: typ -> term list -> term
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    20
  val dest_sum: term -> term list
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    21
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    22
  val assoc_fold_simproc: simproc
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    23
  val combine_numerals: simproc
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    24
  val cancel_numerals: simproc list
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    25
  val cancel_factors: simproc list
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    26
  val cancel_numeral_factors: simproc list
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    27
  val field_combine_numerals: simproc
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    28
  val field_cancel_numeral_factors: simproc list
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    29
  val num_ss: simpset
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    30
end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    31
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    32
structure Numeral_Simprocs : NUMERAL_SIMPROCS =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    33
struct
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    34
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    35
fun mk_number T n = HOLogic.number_of_const T $ HOLogic.mk_numeral n;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    36
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    37
fun find_first_numeral past (t::terms) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    38
        ((snd (HOLogic.dest_number t), rev past @ terms)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    39
         handle TERM _ => find_first_numeral (t::past) terms)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    40
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    41
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    42
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    43
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    44
fun mk_minus t = 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    45
  let val T = Term.fastype_of t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    46
  in Const (@{const_name HOL.uminus}, T --> T) $ t end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    47
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    48
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    49
fun mk_sum T []        = mk_number T 0
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    50
  | mk_sum T [t,u]     = mk_plus (t, u)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    51
  | mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    52
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    53
(*this version ALWAYS includes a trailing zero*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    54
fun long_mk_sum T []        = mk_number T 0
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    55
  | long_mk_sum T (t :: ts) = mk_plus (t, mk_sum T ts);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    56
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    57
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} Term.dummyT;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    58
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    59
(*decompose additions AND subtractions as a sum*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    60
fun dest_summing (pos, Const (@{const_name HOL.plus}, _) $ t $ u, ts) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    61
        dest_summing (pos, t, dest_summing (pos, u, ts))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    62
  | dest_summing (pos, Const (@{const_name HOL.minus}, _) $ t $ u, ts) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    63
        dest_summing (pos, t, dest_summing (not pos, u, ts))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    64
  | dest_summing (pos, t, ts) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    65
        if pos then t::ts else mk_minus t :: ts;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    66
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    67
fun dest_sum t = dest_summing (true, t, []);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    68
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    69
val mk_diff = HOLogic.mk_binop @{const_name HOL.minus};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    70
val dest_diff = HOLogic.dest_bin @{const_name HOL.minus} Term.dummyT;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    71
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    72
val mk_times = HOLogic.mk_binop @{const_name HOL.times};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    73
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    74
fun one_of T = Const(@{const_name HOL.one},T);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    75
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    76
(* build product with trailing 1 rather than Numeral 1 in order to avoid the
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    77
   unnecessary restriction to type class number_ring
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    78
   which is not required for cancellation of common factors in divisions.
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    79
*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    80
fun mk_prod T = 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    81
  let val one = one_of T
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    82
  fun mk [] = one
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    83
    | mk [t] = t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    84
    | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    85
  in mk end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    86
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    87
(*This version ALWAYS includes a trailing one*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    88
fun long_mk_prod T []        = one_of T
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    89
  | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    90
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    91
val dest_times = HOLogic.dest_bin @{const_name HOL.times} Term.dummyT;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    92
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    93
fun dest_prod t =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    94
      let val (t,u) = dest_times t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    95
      in dest_prod t @ dest_prod u end
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    96
      handle TERM _ => [t];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    97
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    98
(*DON'T do the obvious simplifications; that would create special cases*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
    99
fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   100
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   101
(*Express t as a product of (possibly) a numeral with other sorted terms*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   102
fun dest_coeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_coeff (~sign) t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   103
  | dest_coeff sign t =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   104
    let val ts = sort TermOrd.term_ord (dest_prod t)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   105
        val (n, ts') = find_first_numeral [] ts
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   106
                          handle TERM _ => (1, ts)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   107
    in (sign*n, mk_prod (Term.fastype_of t) ts') end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   108
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   109
(*Find first coefficient-term THAT MATCHES u*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   110
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   111
  | find_first_coeff past u (t::terms) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   112
        let val (n,u') = dest_coeff 1 t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   113
        in if u aconv u' then (n, rev past @ terms)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   114
                         else find_first_coeff (t::past) u terms
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   115
        end
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   116
        handle TERM _ => find_first_coeff (t::past) u terms;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   117
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   118
(*Fractions as pairs of ints. Can't use Rat.rat because the representation
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   119
  needs to preserve negative values in the denominator.*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   120
fun mk_frac (p, q) = if q = 0 then raise Div else (p, q);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   121
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   122
(*Don't reduce fractions; sums must be proved by rule add_frac_eq.
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   123
  Fractions are reduced later by the cancel_numeral_factor simproc.*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   124
fun add_frac ((p1, q1), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   125
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   126
val mk_divide = HOLogic.mk_binop @{const_name HOL.divide};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   127
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   128
(*Build term (p / q) * t*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   129
fun mk_fcoeff ((p, q), t) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   130
  let val T = Term.fastype_of t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   131
  in mk_times (mk_divide (mk_number T p, mk_number T q), t) end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   132
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   133
(*Express t as a product of a fraction with other sorted terms*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   134
fun dest_fcoeff sign (Const (@{const_name HOL.uminus}, _) $ t) = dest_fcoeff (~sign) t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   135
  | dest_fcoeff sign (Const (@{const_name HOL.divide}, _) $ t $ u) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   136
    let val (p, t') = dest_coeff sign t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   137
        val (q, u') = dest_coeff 1 u
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   138
    in (mk_frac (p, q), mk_divide (t', u')) end
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   139
  | dest_fcoeff sign t =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   140
    let val (p, t') = dest_coeff sign t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   141
        val T = Term.fastype_of t
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   142
    in (mk_frac (p, 1), mk_divide (t', one_of T)) end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   143
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   144
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   145
(** New term ordering so that AC-rewriting brings numerals to the front **)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   146
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   147
(*Order integers by absolute value and then by sign. The standard integer
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   148
  ordering is not well-founded.*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   149
fun num_ord (i,j) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   150
  (case int_ord (abs i, abs j) of
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   151
    EQUAL => int_ord (Int.sign i, Int.sign j) 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   152
  | ord => ord);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   153
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   154
(*This resembles TermOrd.term_ord, but it puts binary numerals before other
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   155
  non-atomic terms.*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   156
local open Term 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   157
in 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   158
fun numterm_ord (Abs (_, T, t), Abs(_, U, u)) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   159
      (case numterm_ord (t, u) of EQUAL => TermOrd.typ_ord (T, U) | ord => ord)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   160
  | numterm_ord
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   161
     (Const(@{const_name Int.number_of}, _) $ v, Const(@{const_name Int.number_of}, _) $ w) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   162
     num_ord (HOLogic.dest_numeral v, HOLogic.dest_numeral w)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   163
  | numterm_ord (Const(@{const_name Int.number_of}, _) $ _, _) = LESS
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   164
  | numterm_ord (_, Const(@{const_name Int.number_of}, _) $ _) = GREATER
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   165
  | numterm_ord (t, u) =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   166
      (case int_ord (size_of_term t, size_of_term u) of
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   167
        EQUAL =>
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   168
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   169
            (case TermOrd.hd_ord (f, g) of EQUAL => numterms_ord (ts, us) | ord => ord)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   170
          end
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   171
      | ord => ord)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   172
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   173
end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   174
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   175
fun numtermless tu = (numterm_ord tu = LESS);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   176
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   177
val num_ss = HOL_ss settermless numtermless;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   178
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   179
(*Maps 0 to Numeral0 and 1 to Numeral1 so that arithmetic isn't complicated by the abstract 0 and 1.*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   180
val numeral_syms = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   181
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   182
(*Simplify Numeral0+n, n+Numeral0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   183
val add_0s =  @{thms add_0s};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   184
val mult_1s = @{thms mult_1s mult_1_left mult_1_right divide_1};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   185
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   186
(*Simplify inverse Numeral1, a/Numeral1*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   187
val inverse_1s = [@{thm inverse_numeral_1}];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   188
val divide_1s = [@{thm divide_numeral_1}];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   189
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   190
(*To perform binary arithmetic.  The "left" rewriting handles patterns
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   191
  created by the Numeral_Simprocs, such as 3 * (5 * x). *)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   192
val simps = [@{thm numeral_0_eq_0} RS sym, @{thm numeral_1_eq_1} RS sym,
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   193
                 @{thm add_number_of_left}, @{thm mult_number_of_left}] @
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   194
                @{thms arith_simps} @ @{thms rel_simps};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   195
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   196
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   197
  during re-arrangement*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   198
val non_add_simps =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   199
  subtract Thm.eq_thm [@{thm add_number_of_left}, @{thm number_of_add} RS sym] simps;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   200
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   201
(*To evaluate binary negations of coefficients*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   202
val minus_simps = [@{thm numeral_m1_eq_minus_1} RS sym, @{thm number_of_minus} RS sym] @
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   203
                   @{thms minus_bin_simps} @ @{thms pred_bin_simps};
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   204
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   205
(*To let us treat subtraction as addition*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   206
val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   207
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   208
(*To let us treat division as multiplication*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   209
val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   210
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   211
(*push the unary minus down: - x * y = x * - y *)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   212
val minus_mult_eq_1_to_2 =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   213
    [@{thm mult_minus_left}, @{thm minus_mult_right}] MRS trans |> standard;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   214
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   215
(*to extract again any uncancelled minuses*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   216
val minus_from_mult_simps =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   217
    [@{thm minus_minus}, @{thm mult_minus_left}, @{thm mult_minus_right}];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   218
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   219
(*combine unary minus with numeric literals, however nested within a product*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   220
val mult_minus_simps =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   221
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   222
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   223
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   224
  diff_simps @ minus_simps @ @{thms add_ac}
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   225
val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   226
val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   227
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   228
structure CancelNumeralsCommon =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   229
  struct
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   230
  val mk_sum            = mk_sum
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   231
  val dest_sum          = dest_sum
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   232
  val mk_coeff          = mk_coeff
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   233
  val dest_coeff        = dest_coeff 1
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   234
  val find_first_coeff  = find_first_coeff []
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   235
  fun trans_tac _       = Arith_Data.trans_tac
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   236
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   237
  fun norm_tac ss =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   238
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   239
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   240
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   241
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   242
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   243
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   244
  val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   245
  end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   246
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   247
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   248
structure EqCancelNumerals = CancelNumeralsFun
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   249
 (open CancelNumeralsCommon
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   250
  val prove_conv = Arith_Data.prove_conv
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   251
  val mk_bal   = HOLogic.mk_eq
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   252
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   253
  val bal_add1 = @{thm eq_add_iff1} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   254
  val bal_add2 = @{thm eq_add_iff2} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   255
);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   256
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   257
structure LessCancelNumerals = CancelNumeralsFun
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   258
 (open CancelNumeralsCommon
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   259
  val prove_conv = Arith_Data.prove_conv
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   260
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   261
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} Term.dummyT
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   262
  val bal_add1 = @{thm less_add_iff1} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   263
  val bal_add2 = @{thm less_add_iff2} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   264
);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   265
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   266
structure LeCancelNumerals = CancelNumeralsFun
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   267
 (open CancelNumeralsCommon
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   268
  val prove_conv = Arith_Data.prove_conv
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   269
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   270
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} Term.dummyT
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   271
  val bal_add1 = @{thm le_add_iff1} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   272
  val bal_add2 = @{thm le_add_iff2} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   273
);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   274
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   275
val cancel_numerals =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   276
  map Arith_Data.prep_simproc
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   277
   [("inteq_cancel_numerals",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   278
     ["(l::'a::number_ring) + m = n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   279
      "(l::'a::number_ring) = m + n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   280
      "(l::'a::number_ring) - m = n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   281
      "(l::'a::number_ring) = m - n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   282
      "(l::'a::number_ring) * m = n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   283
      "(l::'a::number_ring) = m * n"],
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   284
     K EqCancelNumerals.proc),
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   285
    ("intless_cancel_numerals",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   286
     ["(l::'a::{ordered_idom,number_ring}) + m < n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   287
      "(l::'a::{ordered_idom,number_ring}) < m + n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   288
      "(l::'a::{ordered_idom,number_ring}) - m < n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   289
      "(l::'a::{ordered_idom,number_ring}) < m - n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   290
      "(l::'a::{ordered_idom,number_ring}) * m < n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   291
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   292
     K LessCancelNumerals.proc),
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   293
    ("intle_cancel_numerals",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   294
     ["(l::'a::{ordered_idom,number_ring}) + m <= n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   295
      "(l::'a::{ordered_idom,number_ring}) <= m + n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   296
      "(l::'a::{ordered_idom,number_ring}) - m <= n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   297
      "(l::'a::{ordered_idom,number_ring}) <= m - n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   298
      "(l::'a::{ordered_idom,number_ring}) * m <= n",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   299
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   300
     K LeCancelNumerals.proc)];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   301
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   302
structure CombineNumeralsData =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   303
  struct
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   304
  type coeff            = int
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   305
  val iszero            = (fn x => x = 0)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   306
  val add               = op +
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   307
  val mk_sum            = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   308
  val dest_sum          = dest_sum
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   309
  val mk_coeff          = mk_coeff
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   310
  val dest_coeff        = dest_coeff 1
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   311
  val left_distrib      = @{thm combine_common_factor} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   312
  val prove_conv        = Arith_Data.prove_conv_nohyps
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   313
  fun trans_tac _       = Arith_Data.trans_tac
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   314
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   315
  fun norm_tac ss =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   316
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   317
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   318
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   319
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   320
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   321
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   322
  val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   323
  end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   324
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   325
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   326
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   327
(*Version for fields, where coefficients can be fractions*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   328
structure FieldCombineNumeralsData =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   329
  struct
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   330
  type coeff            = int * int
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   331
  val iszero            = (fn (p, q) => p = 0)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   332
  val add               = add_frac
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   333
  val mk_sum            = long_mk_sum
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   334
  val dest_sum          = dest_sum
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   335
  val mk_coeff          = mk_fcoeff
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   336
  val dest_coeff        = dest_fcoeff 1
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   337
  val left_distrib      = @{thm combine_common_factor} RS trans
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   338
  val prove_conv        = Arith_Data.prove_conv_nohyps
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   339
  fun trans_tac _       = Arith_Data.trans_tac
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   340
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   341
  val norm_ss1a = norm_ss1 addsimps inverse_1s @ divide_simps
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   342
  fun norm_tac ss =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   343
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1a))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   344
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   345
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   346
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   347
  val numeral_simp_ss = HOL_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}]
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   348
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   349
  val simplify_meta_eq = Arith_Data.simplify_meta_eq (add_0s @ mult_1s @ divide_1s)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   350
  end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   351
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   352
structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   353
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   354
val combine_numerals =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   355
  Arith_Data.prep_simproc
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   356
    ("int_combine_numerals", 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   357
     ["(i::'a::number_ring) + j", "(i::'a::number_ring) - j"], 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   358
     K CombineNumerals.proc);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   359
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   360
val field_combine_numerals =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   361
  Arith_Data.prep_simproc
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   362
    ("field_combine_numerals", 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   363
     ["(i::'a::{number_ring,field,division_by_zero}) + j",
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   364
      "(i::'a::{number_ring,field,division_by_zero}) - j"], 
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   365
     K FieldCombineNumerals.proc);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   366
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   367
(** Constant folding for multiplication in semirings **)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   368
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   369
(*We do not need folding for addition: combine_numerals does the same thing*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   370
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   371
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   372
struct
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   373
  val assoc_ss = HOL_ss addsimps @{thms mult_ac}
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   374
  val eq_reflection = eq_reflection
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   375
  fun is_numeral (Const(@{const_name Int.number_of}, _) $ _) = true
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   376
    | is_numeral _ = false;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   377
end;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   378
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   379
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   380
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   381
val assoc_fold_simproc =
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   382
  Arith_Data.prep_simproc
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   383
   ("semiring_assoc_fold", ["(a::'a::comm_semiring_1_cancel) * b"],
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   384
    K Semiring_Times_Assoc.proc);
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   385
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   386
structure CancelNumeralFactorCommon =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   387
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   388
  val mk_coeff          = mk_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   389
  val dest_coeff        = dest_coeff 1
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   390
  fun trans_tac _       = Arith_Data.trans_tac
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   391
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   392
  val norm_ss1 = HOL_ss addsimps minus_from_mult_simps @ mult_1s
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   393
  val norm_ss2 = HOL_ss addsimps simps @ mult_minus_simps
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23413
diff changeset
   394
  val norm_ss3 = HOL_ss addsimps @{thms mult_ac}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   395
  fun norm_tac ss =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   396
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   397
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   398
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   399
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   400
  val numeral_simp_ss = HOL_ss addsimps
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   401
    [@{thm eq_number_of_eq}, @{thm less_number_of}, @{thm le_number_of}] @ simps
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   402
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
30518
07b45c1aa788 moved some generic nonsense to arith_data.ML
haftmann
parents: 30496
diff changeset
   403
  val simplify_meta_eq = Arith_Data.simplify_meta_eq
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   404
    [@{thm add_0}, @{thm add_0_right}, @{thm mult_zero_left},
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 25481
diff changeset
   405
      @{thm mult_zero_right}, @{thm mult_Bit1}, @{thm mult_1_right}];
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   406
  end
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   407
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   408
(*Version for semiring_div*)
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   409
structure DivCancelNumeralFactor = CancelNumeralFactorFun
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   410
 (open CancelNumeralFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   411
  val prove_conv = Arith_Data.prove_conv
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   412
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   413
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} Term.dummyT
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   414
  val cancel = @{thm div_mult_mult1} RS trans
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   415
  val neg_exchanges = false
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   416
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   417
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   418
(*Version for fields*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   419
structure DivideCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   420
 (open CancelNumeralFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   421
  val prove_conv = Arith_Data.prove_conv
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   422
  val mk_bal   = HOLogic.mk_binop @{const_name HOL.divide}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   423
  val dest_bal = HOLogic.dest_bin @{const_name HOL.divide} Term.dummyT
23413
5caa2710dd5b tuned laws for cancellation in divisions for fields.
nipkow
parents: 23401
diff changeset
   424
  val cancel = @{thm mult_divide_mult_cancel_left} RS trans
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   425
  val neg_exchanges = false
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   426
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   427
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   428
structure EqCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   429
 (open CancelNumeralFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   430
  val prove_conv = Arith_Data.prove_conv
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   431
  val mk_bal   = HOLogic.mk_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   432
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   433
  val cancel = @{thm mult_cancel_left} RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   434
  val neg_exchanges = false
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   435
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   436
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   437
structure LessCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   438
 (open CancelNumeralFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   439
  val prove_conv = Arith_Data.prove_conv
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23413
diff changeset
   440
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23413
diff changeset
   441
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} Term.dummyT
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   442
  val cancel = @{thm mult_less_cancel_left} RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   443
  val neg_exchanges = true
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   444
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   445
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   446
structure LeCancelNumeralFactor = CancelNumeralFactorFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   447
 (open CancelNumeralFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   448
  val prove_conv = Arith_Data.prove_conv
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23413
diff changeset
   449
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23413
diff changeset
   450
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} Term.dummyT
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   451
  val cancel = @{thm mult_le_cancel_left} RS trans
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   452
  val neg_exchanges = true
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   453
)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   454
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   455
val cancel_numeral_factors =
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   456
  map Arith_Data.prep_simproc
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   457
   [("ring_eq_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   458
     ["(l::'a::{idom,number_ring}) * m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   459
      "(l::'a::{idom,number_ring}) = m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   460
     K EqCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   461
    ("ring_less_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   462
     ["(l::'a::{ordered_idom,number_ring}) * m < n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   463
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   464
     K LessCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   465
    ("ring_le_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   466
     ["(l::'a::{ordered_idom,number_ring}) * m <= n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   467
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   468
     K LeCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   469
    ("int_div_cancel_numeral_factors",
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   470
     ["((l::'a::{semiring_div,number_ring}) * m) div n",
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   471
      "(l::'a::{semiring_div,number_ring}) div (m * n)"],
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   472
     K DivCancelNumeralFactor.proc),
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   473
    ("divide_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   474
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   475
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   476
      "((number_of v)::'a::{division_by_zero,field,number_ring}) / (number_of w)"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   477
     K DivideCancelNumeralFactor.proc)];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   478
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   479
val field_cancel_numeral_factors =
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   480
  map Arith_Data.prep_simproc
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   481
   [("field_eq_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   482
     ["(l::'a::{field,number_ring}) * m = n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   483
      "(l::'a::{field,number_ring}) = m * n"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   484
     K EqCancelNumeralFactor.proc),
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   485
    ("field_cancel_numeral_factor",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   486
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   487
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)",
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   488
      "((number_of v)::'a::{division_by_zero,field,number_ring}) / (number_of w)"],
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   489
     K DivideCancelNumeralFactor.proc)]
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   490
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   491
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   492
(** Declarations for ExtractCommonTerm **)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   493
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   494
(*Find first term that matches u*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   495
fun find_first_t past u []         = raise TERM ("find_first_t", [])
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   496
  | find_first_t past u (t::terms) =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   497
        if u aconv t then (rev past @ terms)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   498
        else find_first_t (t::past) u terms
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   499
        handle TERM _ => find_first_t (t::past) u terms;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   500
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   501
(** Final simplification for the CancelFactor simprocs **)
30518
07b45c1aa788 moved some generic nonsense to arith_data.ML
haftmann
parents: 30496
diff changeset
   502
val simplify_one = Arith_Data.simplify_meta_eq  
30031
bd786c37af84 Removed redundant lemmas
nipkow
parents: 29981
diff changeset
   503
  [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_1_eq_1}];
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   504
30649
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   505
fun cancel_simplify_meta_eq ss cancel_th th =
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   506
    simplify_one ss (([th, cancel_th]) MRS trans);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   507
30649
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   508
local
31067
fd7ec31f850c generalized simproc for mod
haftmann
parents: 30931
diff changeset
   509
  val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory HOL} HOLogic.Trueprop)
30649
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   510
  fun Eq_True_elim Eq = 
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   511
    Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   512
in
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   513
fun sign_conv pos_th neg_th ss t =
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   514
  let val T = fastype_of t;
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   515
      val zero = Const(@{const_name HOL.zero}, T);
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   516
      val less = Const(@{const_name HOL.less}, [T,T] ---> HOLogic.boolT);
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   517
      val pos = less $ zero $ t and neg = less $ t $ zero
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   518
      fun prove p =
31101
26c7bb764a38 qualified names for Lin_Arith tactics and simprocs
haftmann
parents: 31068
diff changeset
   519
        Option.map Eq_True_elim (Lin_Arith.simproc ss p)
30649
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   520
        handle THM _ => NONE
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   521
    in case prove pos of
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   522
         SOME th => SOME(th RS pos_th)
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   523
       | NONE => (case prove neg of
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   524
                    SOME th => SOME(th RS neg_th)
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   525
                  | NONE => NONE)
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   526
    end;
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   527
end
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   528
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   529
structure CancelFactorCommon =
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   530
  struct
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   531
  val mk_sum            = long_mk_prod
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   532
  val dest_sum          = dest_prod
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   533
  val mk_coeff          = mk_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   534
  val dest_coeff        = dest_coeff
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   535
  val find_first        = find_first_t []
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   536
  fun trans_tac _       = Arith_Data.trans_tac
23881
851c74f1bb69 moved class ord from Orderings.thy to HOL.thy
haftmann
parents: 23413
diff changeset
   537
  val norm_ss = HOL_ss addsimps mult_1s @ @{thms mult_ac}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   538
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
30649
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   539
  val simplify_meta_eq  = cancel_simplify_meta_eq 
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   540
  end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   541
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   542
(*mult_cancel_left requires a ring with no zero divisors.*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   543
structure EqCancelFactor = ExtractCommonTermFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   544
 (open CancelFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   545
  val prove_conv = Arith_Data.prove_conv
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   546
  val mk_bal   = HOLogic.mk_eq
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   547
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   548
  fun simp_conv _ _ = SOME @{thm mult_cancel_left}
30649
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   549
);
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   550
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   551
(*for ordered rings*)
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   552
structure LeCancelFactor = ExtractCommonTermFun
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   553
 (open CancelFactorCommon
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   554
  val prove_conv = Arith_Data.prove_conv
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   555
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   556
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} Term.dummyT
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   557
  val simp_conv = sign_conv
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   558
    @{thm mult_le_cancel_left_pos} @{thm mult_le_cancel_left_neg}
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   559
);
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   560
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   561
(*for ordered rings*)
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   562
structure LessCancelFactor = ExtractCommonTermFun
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   563
 (open CancelFactorCommon
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   564
  val prove_conv = Arith_Data.prove_conv
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   565
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   566
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} Term.dummyT
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   567
  val simp_conv = sign_conv
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   568
    @{thm mult_less_cancel_left_pos} @{thm mult_less_cancel_left_neg}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   569
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   570
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   571
(*for semirings with division*)
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   572
structure DivCancelFactor = ExtractCommonTermFun
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   573
 (open CancelFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   574
  val prove_conv = Arith_Data.prove_conv
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   575
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   576
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} Term.dummyT
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   577
  fun simp_conv _ _ = SOME @{thm div_mult_mult1_if}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   578
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   579
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   580
structure ModCancelFactor = ExtractCommonTermFun
24395
8df021f79e0b Added mod cancellation simproc
nipkow
parents: 23969
diff changeset
   581
 (open CancelFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   582
  val prove_conv = Arith_Data.prove_conv
24395
8df021f79e0b Added mod cancellation simproc
nipkow
parents: 23969
diff changeset
   583
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.mod}
31067
fd7ec31f850c generalized simproc for mod
haftmann
parents: 30931
diff changeset
   584
  val dest_bal = HOLogic.dest_bin @{const_name Divides.mod} Term.dummyT
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   585
  fun simp_conv _ _ = SOME @{thm mod_mult_mult1}
24395
8df021f79e0b Added mod cancellation simproc
nipkow
parents: 23969
diff changeset
   586
);
8df021f79e0b Added mod cancellation simproc
nipkow
parents: 23969
diff changeset
   587
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   588
(*for idoms*)
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   589
structure DvdCancelFactor = ExtractCommonTermFun
23969
ef782bbf2d09 Added cancel simprocs for dvd on nat and int
nipkow
parents: 23881
diff changeset
   590
 (open CancelFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   591
  val prove_conv = Arith_Data.prove_conv
27651
16a26996c30e moved op dvd to theory Ring_and_Field; generalized a couple of lemmas
haftmann
parents: 26086
diff changeset
   592
  val mk_bal   = HOLogic.mk_binrel @{const_name Ring_and_Field.dvd}
29981
7d0ed261b712 generalize int_dvd_cancel_factor simproc to idom class
huffman
parents: 29038
diff changeset
   593
  val dest_bal = HOLogic.dest_bin @{const_name Ring_and_Field.dvd} Term.dummyT
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   594
  fun simp_conv _ _ = SOME @{thm dvd_mult_cancel_left}
23969
ef782bbf2d09 Added cancel simprocs for dvd on nat and int
nipkow
parents: 23881
diff changeset
   595
);
ef782bbf2d09 Added cancel simprocs for dvd on nat and int
nipkow
parents: 23881
diff changeset
   596
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   597
(*Version for all fields, including unordered ones (type complex).*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   598
structure DivideCancelFactor = ExtractCommonTermFun
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   599
 (open CancelFactorCommon
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   600
  val prove_conv = Arith_Data.prove_conv
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   601
  val mk_bal   = HOLogic.mk_binop @{const_name HOL.divide}
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   602
  val dest_bal = HOLogic.dest_bin @{const_name HOL.divide} Term.dummyT
31368
763f4b0fd579 made SML/NJ happy;
wenzelm
parents: 31101
diff changeset
   603
  fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   604
);
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   605
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   606
val cancel_factors =
30496
7cdcc9dd95cb vague cleanup in arith proof tools setup: deleted dead code, more proper structures, clearer arrangement
haftmann
parents: 30242
diff changeset
   607
  map Arith_Data.prep_simproc
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   608
   [("ring_eq_cancel_factor",
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   609
     ["(l::'a::idom) * m = n",
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   610
      "(l::'a::idom) = m * n"],
30649
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   611
     K EqCancelFactor.proc),
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   612
    ("ordered_ring_le_cancel_factor",
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   613
     ["(l::'a::ordered_ring) * m <= n",
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   614
      "(l::'a::ordered_ring) <= m * n"],
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   615
     K LeCancelFactor.proc),
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   616
    ("ordered_ring_less_cancel_factor",
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   617
     ["(l::'a::ordered_ring) * m < n",
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   618
      "(l::'a::ordered_ring) < m * n"],
57753e0ec1d4 1. New cancellation simprocs for common factors in inequations
nipkow
parents: 30518
diff changeset
   619
     K LessCancelFactor.proc),
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   620
    ("int_div_cancel_factor",
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   621
     ["((l::'a::semiring_div) * m) div n", "(l::'a::semiring_div) div (m * n)"],
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   622
     K DivCancelFactor.proc),
24395
8df021f79e0b Added mod cancellation simproc
nipkow
parents: 23969
diff changeset
   623
    ("int_mod_cancel_factor",
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   624
     ["((l::'a::semiring_div) * m) mod n", "(l::'a::semiring_div) mod (m * n)"],
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   625
     K ModCancelFactor.proc),
29981
7d0ed261b712 generalize int_dvd_cancel_factor simproc to idom class
huffman
parents: 29038
diff changeset
   626
    ("dvd_cancel_factor",
7d0ed261b712 generalize int_dvd_cancel_factor simproc to idom class
huffman
parents: 29038
diff changeset
   627
     ["((l::'a::idom) * m) dvd n", "(l::'a::idom) dvd (m * n)"],
30931
86ca651da03e generalized some simprocs from int to semiring_div
haftmann
parents: 30685
diff changeset
   628
     K DvdCancelFactor.proc),
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   629
    ("divide_cancel_factor",
23400
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23398
diff changeset
   630
     ["((l::'a::{division_by_zero,field}) * m) / n",
a64b39e5809b The simprocs "divide_cancel_factor" and "ring_eq_cancel_factor" no
nipkow
parents: 23398
diff changeset
   631
      "(l::'a::{division_by_zero,field}) / (m * n)"],
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   632
     K DivideCancelFactor.proc)];
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   633
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   634
end;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   635
31068
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   636
Addsimprocs Numeral_Simprocs.cancel_numerals;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   637
Addsimprocs [Numeral_Simprocs.combine_numerals];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   638
Addsimprocs [Numeral_Simprocs.field_combine_numerals];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   639
Addsimprocs [Numeral_Simprocs.assoc_fold_simproc];
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   640
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   641
(*examples:
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   642
print_depth 22;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   643
set timing;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   644
set trace_simp;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   645
fun test s = (Goal s, by (Simp_tac 1));
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   646
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   647
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   648
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   649
test "2*u = (u::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   650
test "(i + j + 12 + (k::int)) - 15 = y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   651
test "(i + j + 12 + (k::int)) - 5 = y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   652
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   653
test "y - b < (b::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   654
test "y - (3*b + c) < (b::int) - 2*c";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   655
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   656
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   657
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   658
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   659
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   660
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   661
test "(i + j + 12 + (k::int)) = u + 15 + y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   662
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   663
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   664
test "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = 2*y' + 3*z' + 6*w' + 2*y' + 3*z' + u + (vv::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   665
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   666
test "a + -(b+c) + b = (d::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   667
test "a + -(b+c) - b = (d::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   668
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   669
(*negative numerals*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   670
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   671
test "(i + j + -3 + (k::int)) < u + 5 + y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   672
test "(i + j + 3 + (k::int)) < u + -6 + y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   673
test "(i + j + -12 + (k::int)) - 15 = y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   674
test "(i + j + 12 + (k::int)) - -15 = y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   675
test "(i + j + -12 + (k::int)) - -15 = y";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   676
*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   677
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   678
Addsimprocs Numeral_Simprocs.cancel_numeral_factors;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   679
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   680
(*examples:
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   681
print_depth 22;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   682
set timing;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   683
set trace_simp;
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   684
fun test s = (Goal s; by (Simp_tac 1));
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   685
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   686
test "9*x = 12 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   687
test "(9*x) div (12 * (y::int)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   688
test "9*x < 12 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   689
test "9*x <= 12 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   690
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   691
test "-99*x = 132 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   692
test "(-99*x) div (132 * (y::int)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   693
test "-99*x < 132 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   694
test "-99*x <= 132 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   695
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   696
test "999*x = -396 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   697
test "(999*x) div (-396 * (y::int)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   698
test "999*x < -396 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   699
test "999*x <= -396 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   700
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   701
test "-99*x = -81 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   702
test "(-99*x) div (-81 * (y::int)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   703
test "-99*x <= -81 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   704
test "-99*x < -81 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   705
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   706
test "-2 * x = -1 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   707
test "-2 * x = -(y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   708
test "(-2 * x) div (-1 * (y::int)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   709
test "-2 * x < -(y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   710
test "-2 * x <= -1 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   711
test "-x < -23 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   712
test "-x <= -23 * (y::int)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   713
*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   714
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   715
(*And the same examples for fields such as rat or real:
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   716
test "0 <= (y::rat) * -2";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   717
test "9*x = 12 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   718
test "(9*x) / (12 * (y::rat)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   719
test "9*x < 12 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   720
test "9*x <= 12 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   721
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   722
test "-99*x = 132 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   723
test "(-99*x) / (132 * (y::rat)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   724
test "-99*x < 132 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   725
test "-99*x <= 132 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   726
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   727
test "999*x = -396 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   728
test "(999*x) / (-396 * (y::rat)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   729
test "999*x < -396 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   730
test "999*x <= -396 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   731
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   732
test  "(- ((2::rat) * x) <= 2 * y)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   733
test "-99*x = -81 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   734
test "(-99*x) / (-81 * (y::rat)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   735
test "-99*x <= -81 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   736
test "-99*x < -81 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   737
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   738
test "-2 * x = -1 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   739
test "-2 * x = -(y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   740
test "(-2 * x) / (-1 * (y::rat)) = z";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   741
test "-2 * x < -(y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   742
test "-2 * x <= -1 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   743
test "-x < -23 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   744
test "-x <= -23 * (y::rat)";
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   745
*)
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   746
f591144b0f17 modules numeral_simprocs, nat_numeral_simprocs; proper structures for numeral simprocs
haftmann
parents: 31067
diff changeset
   747
Addsimprocs Numeral_Simprocs.cancel_factors;
23164
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   748
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   749
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   750
(*examples:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   751
print_depth 22;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   752
set timing;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   753
set trace_simp;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   754
fun test s = (Goal s; by (Asm_simp_tac 1));
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   755
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   756
test "x*k = k*(y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   757
test "k = k*(y::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   758
test "a*(b*c) = (b::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   759
test "a*(b*c) = d*(b::int)*(x*a)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   760
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   761
test "(x*k) div (k*(y::int)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   762
test "(k) div (k*(y::int)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   763
test "(a*(b*c)) div ((b::int)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   764
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   765
*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   766
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   767
(*And the same examples for fields such as rat or real:
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   768
print_depth 22;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   769
set timing;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   770
set trace_simp;
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   771
fun test s = (Goal s; by (Asm_simp_tac 1));
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   772
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   773
test "x*k = k*(y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   774
test "k = k*(y::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   775
test "a*(b*c) = (b::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   776
test "a*(b*c) = d*(b::rat)*(x*a)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   777
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   778
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   779
test "(x*k) / (k*(y::rat)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   780
test "(k) / (k*(y::rat)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   781
test "(a*(b*c)) / ((b::rat)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   782
test "(a*(b*c)) / (d*(b::rat)*(x*a)) = (uu::rat)";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   783
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   784
(*FIXME: what do we do about this?*)
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   785
test "a*(b*c)/(y*z) = d*(b::rat)*(x*a)/z";
69e55066dbca moved Integ files to canonical place;
wenzelm
parents:
diff changeset
   786
*)