src/HOL/Tools/numeral_simprocs.ML
author haftmann
Fri Nov 01 18:51:14 2013 +0100 (2013-11-01)
changeset 54230 b1d955791529
parent 51717 9e7d1c139569
child 54489 03ff4d1e6784
permissions -rw-r--r--
more simplification rules on unary and binary minus
haftmann@31068
     1
(* Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
haftmann@31068
     2
   Copyright   2000  University of Cambridge
wenzelm@23164
     3
haftmann@36751
     4
Simprocs for the (integer) numerals.
wenzelm@23164
     5
*)
wenzelm@23164
     6
wenzelm@23164
     7
(*To quote from Provers/Arith/cancel_numeral_factor.ML:
wenzelm@23164
     8
wenzelm@23164
     9
Cancels common coefficients in balanced expressions:
wenzelm@23164
    10
wenzelm@23164
    11
     u*#m ~~ u'*#m'  ==  #n*u ~~ #n'*u'
wenzelm@23164
    12
wenzelm@23164
    13
where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /)
wenzelm@23164
    14
and d = gcd(m,m') and n=m/d and n'=m'/d.
wenzelm@23164
    15
*)
wenzelm@23164
    16
haftmann@31068
    17
signature NUMERAL_SIMPROCS =
haftmann@31068
    18
sig
wenzelm@51717
    19
  val prep_simproc: theory -> string * string list * (Proof.context -> term -> thm option)
haftmann@44945
    20
    -> simproc
haftmann@44945
    21
  val trans_tac: thm option -> tactic
wenzelm@51717
    22
  val assoc_fold: Proof.context -> cterm -> thm option
wenzelm@51717
    23
  val combine_numerals: Proof.context -> cterm -> thm option
wenzelm@51717
    24
  val eq_cancel_numerals: Proof.context -> cterm -> thm option
wenzelm@51717
    25
  val less_cancel_numerals: Proof.context -> cterm -> thm option
wenzelm@51717
    26
  val le_cancel_numerals: Proof.context -> cterm -> thm option
wenzelm@51717
    27
  val eq_cancel_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    28
  val le_cancel_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    29
  val less_cancel_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    30
  val div_cancel_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    31
  val mod_cancel_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    32
  val dvd_cancel_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    33
  val divide_cancel_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    34
  val eq_cancel_numeral_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    35
  val less_cancel_numeral_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    36
  val le_cancel_numeral_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    37
  val div_cancel_numeral_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    38
  val divide_cancel_numeral_factor: Proof.context -> cterm -> thm option
wenzelm@51717
    39
  val field_combine_numerals: Proof.context -> cterm -> thm option
haftmann@31068
    40
  val field_cancel_numeral_factors: simproc list
haftmann@31068
    41
  val num_ss: simpset
wenzelm@51717
    42
  val field_comp_conv: Proof.context -> conv
haftmann@31068
    43
end;
haftmann@31068
    44
haftmann@31068
    45
structure Numeral_Simprocs : NUMERAL_SIMPROCS =
haftmann@31068
    46
struct
haftmann@31068
    47
haftmann@44945
    48
fun prep_simproc thy (name, pats, proc) =
haftmann@44945
    49
  Simplifier.simproc_global thy name pats proc;
haftmann@44945
    50
haftmann@44945
    51
fun trans_tac NONE  = all_tac
haftmann@44945
    52
  | trans_tac (SOME th) = ALLGOALS (rtac (th RS trans));
haftmann@44945
    53
haftmann@33359
    54
val mk_number = Arith_Data.mk_number;
haftmann@33359
    55
val mk_sum = Arith_Data.mk_sum;
haftmann@33359
    56
val long_mk_sum = Arith_Data.long_mk_sum;
haftmann@33359
    57
val dest_sum = Arith_Data.dest_sum;
haftmann@31068
    58
haftmann@35267
    59
val mk_diff = HOLogic.mk_binop @{const_name Groups.minus};
wenzelm@49323
    60
val dest_diff = HOLogic.dest_bin @{const_name Groups.minus} dummyT;
haftmann@31068
    61
haftmann@35267
    62
val mk_times = HOLogic.mk_binop @{const_name Groups.times};
haftmann@31068
    63
haftmann@35267
    64
fun one_of T = Const(@{const_name Groups.one}, T);
haftmann@31068
    65
haftmann@31068
    66
(* build product with trailing 1 rather than Numeral 1 in order to avoid the
haftmann@31068
    67
   unnecessary restriction to type class number_ring
haftmann@31068
    68
   which is not required for cancellation of common factors in divisions.
huffman@47108
    69
   UPDATE: this reasoning no longer applies (number_ring is gone)
haftmann@31068
    70
*)
haftmann@31068
    71
fun mk_prod T = 
haftmann@31068
    72
  let val one = one_of T
haftmann@31068
    73
  fun mk [] = one
haftmann@31068
    74
    | mk [t] = t
haftmann@31068
    75
    | mk (t :: ts) = if t = one then mk ts else mk_times (t, mk ts)
haftmann@31068
    76
  in mk end;
haftmann@31068
    77
haftmann@31068
    78
(*This version ALWAYS includes a trailing one*)
haftmann@31068
    79
fun long_mk_prod T []        = one_of T
haftmann@31068
    80
  | long_mk_prod T (t :: ts) = mk_times (t, mk_prod T ts);
haftmann@31068
    81
wenzelm@49323
    82
val dest_times = HOLogic.dest_bin @{const_name Groups.times} dummyT;
haftmann@31068
    83
haftmann@31068
    84
fun dest_prod t =
haftmann@31068
    85
      let val (t,u) = dest_times t
haftmann@31068
    86
      in dest_prod t @ dest_prod u end
haftmann@31068
    87
      handle TERM _ => [t];
haftmann@31068
    88
haftmann@33359
    89
fun find_first_numeral past (t::terms) =
haftmann@33359
    90
        ((snd (HOLogic.dest_number t), rev past @ terms)
haftmann@33359
    91
         handle TERM _ => find_first_numeral (t::past) terms)
haftmann@33359
    92
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
haftmann@33359
    93
haftmann@31068
    94
(*DON'T do the obvious simplifications; that would create special cases*)
haftmann@31068
    95
fun mk_coeff (k, t) = mk_times (mk_number (Term.fastype_of t) k, t);
haftmann@31068
    96
haftmann@31068
    97
(*Express t as a product of (possibly) a numeral with other sorted terms*)
haftmann@35267
    98
fun dest_coeff sign (Const (@{const_name Groups.uminus}, _) $ t) = dest_coeff (~sign) t
haftmann@31068
    99
  | dest_coeff sign t =
wenzelm@35408
   100
    let val ts = sort Term_Ord.term_ord (dest_prod t)
haftmann@31068
   101
        val (n, ts') = find_first_numeral [] ts
haftmann@31068
   102
                          handle TERM _ => (1, ts)
haftmann@31068
   103
    in (sign*n, mk_prod (Term.fastype_of t) ts') end;
haftmann@31068
   104
haftmann@31068
   105
(*Find first coefficient-term THAT MATCHES u*)
haftmann@31068
   106
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
haftmann@31068
   107
  | find_first_coeff past u (t::terms) =
haftmann@31068
   108
        let val (n,u') = dest_coeff 1 t
haftmann@31068
   109
        in if u aconv u' then (n, rev past @ terms)
haftmann@31068
   110
                         else find_first_coeff (t::past) u terms
haftmann@31068
   111
        end
haftmann@31068
   112
        handle TERM _ => find_first_coeff (t::past) u terms;
haftmann@31068
   113
haftmann@31068
   114
(*Fractions as pairs of ints. Can't use Rat.rat because the representation
haftmann@31068
   115
  needs to preserve negative values in the denominator.*)
haftmann@31068
   116
fun mk_frac (p, q) = if q = 0 then raise Div else (p, q);
haftmann@31068
   117
haftmann@31068
   118
(*Don't reduce fractions; sums must be proved by rule add_frac_eq.
haftmann@31068
   119
  Fractions are reduced later by the cancel_numeral_factor simproc.*)
haftmann@31068
   120
fun add_frac ((p1, q1), (p2, q2)) = (p1 * q2 + p2 * q1, q1 * q2);
haftmann@31068
   121
huffman@44064
   122
val mk_divide = HOLogic.mk_binop @{const_name Fields.divide};
haftmann@31068
   123
haftmann@31068
   124
(*Build term (p / q) * t*)
haftmann@31068
   125
fun mk_fcoeff ((p, q), t) =
haftmann@31068
   126
  let val T = Term.fastype_of t
haftmann@31068
   127
  in mk_times (mk_divide (mk_number T p, mk_number T q), t) end;
haftmann@31068
   128
haftmann@31068
   129
(*Express t as a product of a fraction with other sorted terms*)
haftmann@35267
   130
fun dest_fcoeff sign (Const (@{const_name Groups.uminus}, _) $ t) = dest_fcoeff (~sign) t
huffman@44064
   131
  | dest_fcoeff sign (Const (@{const_name Fields.divide}, _) $ t $ u) =
haftmann@31068
   132
    let val (p, t') = dest_coeff sign t
haftmann@31068
   133
        val (q, u') = dest_coeff 1 u
haftmann@31068
   134
    in (mk_frac (p, q), mk_divide (t', u')) end
haftmann@31068
   135
  | dest_fcoeff sign t =
haftmann@31068
   136
    let val (p, t') = dest_coeff sign t
haftmann@31068
   137
        val T = Term.fastype_of t
haftmann@31068
   138
    in (mk_frac (p, 1), mk_divide (t', one_of T)) end;
haftmann@31068
   139
haftmann@31068
   140
haftmann@31068
   141
(** New term ordering so that AC-rewriting brings numerals to the front **)
haftmann@31068
   142
haftmann@31068
   143
(*Order integers by absolute value and then by sign. The standard integer
haftmann@31068
   144
  ordering is not well-founded.*)
haftmann@31068
   145
fun num_ord (i,j) =
haftmann@31068
   146
  (case int_ord (abs i, abs j) of
haftmann@31068
   147
    EQUAL => int_ord (Int.sign i, Int.sign j) 
haftmann@31068
   148
  | ord => ord);
haftmann@31068
   149
wenzelm@35408
   150
(*This resembles Term_Ord.term_ord, but it puts binary numerals before other
haftmann@31068
   151
  non-atomic terms.*)
huffman@47108
   152
local open Term
huffman@47108
   153
in
huffman@47108
   154
fun numterm_ord (t, u) =
huffman@47108
   155
    case (try HOLogic.dest_number t, try HOLogic.dest_number u) of
huffman@47108
   156
      (SOME (_, i), SOME (_, j)) => num_ord (i, j)
huffman@47108
   157
    | (SOME _, NONE) => LESS
huffman@47108
   158
    | (NONE, SOME _) => GREATER
huffman@47108
   159
    | _ => (
huffman@47108
   160
      case (t, u) of
huffman@47108
   161
        (Abs (_, T, t), Abs(_, U, u)) =>
huffman@47108
   162
        (prod_ord numterm_ord Term_Ord.typ_ord ((t, T), (u, U)))
huffman@47108
   163
      | _ => (
huffman@47108
   164
        case int_ord (size_of_term t, size_of_term u) of
huffman@47108
   165
          EQUAL =>
haftmann@31068
   166
          let val (f, ts) = strip_comb t and (g, us) = strip_comb u in
huffman@47108
   167
            (prod_ord Term_Ord.hd_ord numterms_ord ((f, ts), (g, us)))
haftmann@31068
   168
          end
huffman@47108
   169
        | ord => ord))
haftmann@31068
   170
and numterms_ord (ts, us) = list_ord numterm_ord (ts, us)
haftmann@31068
   171
end;
haftmann@31068
   172
haftmann@31068
   173
fun numtermless tu = (numterm_ord tu = LESS);
haftmann@31068
   174
wenzelm@51717
   175
val num_ss =
wenzelm@51717
   176
  simpset_of (put_simpset HOL_basic_ss @{context}
wenzelm@51717
   177
    |> Simplifier.set_termless numtermless);
haftmann@31068
   178
huffman@47108
   179
(*Maps 1 to Numeral1 so that arithmetic isn't complicated by the abstract 1.*)
huffman@47108
   180
val numeral_syms = [@{thm numeral_1_eq_1} RS sym];
haftmann@31068
   181
huffman@47108
   182
(*Simplify 0+n, n+0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
haftmann@31068
   183
val add_0s =  @{thms add_0s};
haftmann@31068
   184
val mult_1s = @{thms mult_1s mult_1_left mult_1_right divide_1};
haftmann@31068
   185
huffman@45437
   186
(* For post-simplification of the rhs of simproc-generated rules *)
huffman@45437
   187
val post_simps =
huffman@47108
   188
    [@{thm numeral_1_eq_1},
huffman@45437
   189
     @{thm add_0_left}, @{thm add_0_right},
huffman@45437
   190
     @{thm mult_zero_left}, @{thm mult_zero_right},
huffman@45437
   191
     @{thm mult_1_left}, @{thm mult_1_right},
huffman@45437
   192
     @{thm mult_minus1}, @{thm mult_minus1_right}]
huffman@45437
   193
huffman@45437
   194
val field_post_simps =
huffman@45437
   195
    post_simps @ [@{thm divide_zero_left}, @{thm divide_1}]
huffman@45437
   196
                      
haftmann@31068
   197
(*Simplify inverse Numeral1, a/Numeral1*)
haftmann@31068
   198
val inverse_1s = [@{thm inverse_numeral_1}];
haftmann@31068
   199
val divide_1s = [@{thm divide_numeral_1}];
haftmann@31068
   200
haftmann@31068
   201
(*To perform binary arithmetic.  The "left" rewriting handles patterns
haftmann@31068
   202
  created by the Numeral_Simprocs, such as 3 * (5 * x). *)
huffman@47108
   203
val simps =
huffman@47108
   204
    [@{thm numeral_1_eq_1} RS sym] @
huffman@47108
   205
    @{thms add_numeral_left} @
huffman@47108
   206
    @{thms add_neg_numeral_left} @
huffman@47108
   207
    @{thms mult_numeral_left} @
huffman@47108
   208
    @{thms arith_simps} @ @{thms rel_simps};
huffman@47108
   209
    
haftmann@31068
   210
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
haftmann@31068
   211
  during re-arrangement*)
haftmann@31068
   212
val non_add_simps =
huffman@47108
   213
  subtract Thm.eq_thm
huffman@47108
   214
    (@{thms add_numeral_left} @
huffman@47108
   215
     @{thms add_neg_numeral_left} @
huffman@47108
   216
     @{thms numeral_plus_numeral} @
huffman@47108
   217
     @{thms add_neg_numeral_simps}) simps;
wenzelm@23164
   218
haftmann@31068
   219
(*To evaluate binary negations of coefficients*)
huffman@47108
   220
val minus_simps = [@{thm minus_zero}, @{thm minus_one}, @{thm minus_numeral}, @{thm minus_neg_numeral}];
haftmann@31068
   221
haftmann@31068
   222
(*To let us treat subtraction as addition*)
haftmann@54230
   223
val diff_simps = [@{thm diff_conv_add_uminus}, @{thm minus_add_distrib}, @{thm minus_minus}];
haftmann@31068
   224
haftmann@31068
   225
(*To let us treat division as multiplication*)
haftmann@31068
   226
val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
haftmann@31068
   227
wenzelm@35020
   228
(*push the unary minus down*)
wenzelm@35020
   229
val minus_mult_eq_1_to_2 = @{lemma "- (a::'a::ring) * b = a * - b" by simp};
haftmann@31068
   230
haftmann@31068
   231
(*to extract again any uncancelled minuses*)
haftmann@31068
   232
val minus_from_mult_simps =
haftmann@31068
   233
    [@{thm minus_minus}, @{thm mult_minus_left}, @{thm mult_minus_right}];
haftmann@31068
   234
haftmann@31068
   235
(*combine unary minus with numeric literals, however nested within a product*)
haftmann@31068
   236
val mult_minus_simps =
haftmann@31068
   237
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
haftmann@31068
   238
wenzelm@51717
   239
val norm_ss1 =
wenzelm@51717
   240
  simpset_of (put_simpset num_ss @{context}
wenzelm@51717
   241
    addsimps numeral_syms @ add_0s @ mult_1s @
wenzelm@51717
   242
    diff_simps @ minus_simps @ @{thms add_ac})
wenzelm@51717
   243
wenzelm@51717
   244
val norm_ss2 =
wenzelm@51717
   245
  simpset_of (put_simpset num_ss @{context}
wenzelm@51717
   246
    addsimps non_add_simps @ mult_minus_simps)
wenzelm@51717
   247
wenzelm@51717
   248
val norm_ss3 =
wenzelm@51717
   249
  simpset_of (put_simpset num_ss @{context}
wenzelm@51717
   250
    addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac})
haftmann@31068
   251
haftmann@31068
   252
structure CancelNumeralsCommon =
haftmann@44945
   253
struct
haftmann@44945
   254
  val mk_sum = mk_sum
haftmann@44945
   255
  val dest_sum = dest_sum
haftmann@44945
   256
  val mk_coeff = mk_coeff
haftmann@44945
   257
  val dest_coeff = dest_coeff 1
haftmann@44945
   258
  val find_first_coeff = find_first_coeff []
haftmann@44947
   259
  val trans_tac = trans_tac
haftmann@31068
   260
wenzelm@51717
   261
  fun norm_tac ctxt =
wenzelm@51717
   262
    ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt))
wenzelm@51717
   263
    THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
wenzelm@51717
   264
    THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
haftmann@31068
   265
wenzelm@51717
   266
  val numeral_simp_ss =
wenzelm@51717
   267
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps add_0s @ simps)
wenzelm@51717
   268
  fun numeral_simp_tac ctxt =
wenzelm@51717
   269
    ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
huffman@45437
   270
  val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
haftmann@44945
   271
  val prove_conv = Arith_Data.prove_conv
haftmann@44945
   272
end;
haftmann@31068
   273
haftmann@31068
   274
structure EqCancelNumerals = CancelNumeralsFun
haftmann@31068
   275
 (open CancelNumeralsCommon
haftmann@31068
   276
  val mk_bal   = HOLogic.mk_eq
wenzelm@49323
   277
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
haftmann@31068
   278
  val bal_add1 = @{thm eq_add_iff1} RS trans
haftmann@31068
   279
  val bal_add2 = @{thm eq_add_iff2} RS trans
haftmann@31068
   280
);
haftmann@31068
   281
haftmann@31068
   282
structure LessCancelNumerals = CancelNumeralsFun
haftmann@31068
   283
 (open CancelNumeralsCommon
haftmann@35092
   284
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
wenzelm@49323
   285
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
haftmann@31068
   286
  val bal_add1 = @{thm less_add_iff1} RS trans
haftmann@31068
   287
  val bal_add2 = @{thm less_add_iff2} RS trans
haftmann@31068
   288
);
haftmann@31068
   289
haftmann@31068
   290
structure LeCancelNumerals = CancelNumeralsFun
haftmann@31068
   291
 (open CancelNumeralsCommon
haftmann@35092
   292
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
wenzelm@49323
   293
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
haftmann@31068
   294
  val bal_add1 = @{thm le_add_iff1} RS trans
haftmann@31068
   295
  val bal_add2 = @{thm le_add_iff2} RS trans
haftmann@31068
   296
);
haftmann@31068
   297
wenzelm@51717
   298
fun eq_cancel_numerals ctxt ct = EqCancelNumerals.proc ctxt (term_of ct)
wenzelm@51717
   299
fun less_cancel_numerals ctxt ct = LessCancelNumerals.proc ctxt (term_of ct)
wenzelm@51717
   300
fun le_cancel_numerals ctxt ct = LeCancelNumerals.proc ctxt (term_of ct)
haftmann@31068
   301
haftmann@31068
   302
structure CombineNumeralsData =
haftmann@44945
   303
struct
haftmann@44945
   304
  type coeff = int
haftmann@44945
   305
  val iszero = (fn x => x = 0)
haftmann@44945
   306
  val add  = op +
haftmann@44945
   307
  val mk_sum = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
haftmann@44945
   308
  val dest_sum = dest_sum
haftmann@44945
   309
  val mk_coeff = mk_coeff
haftmann@44945
   310
  val dest_coeff = dest_coeff 1
haftmann@44945
   311
  val left_distrib = @{thm combine_common_factor} RS trans
haftmann@44945
   312
  val prove_conv = Arith_Data.prove_conv_nohyps
haftmann@44947
   313
  val trans_tac = trans_tac
haftmann@31068
   314
wenzelm@51717
   315
  fun norm_tac ctxt =
wenzelm@51717
   316
    ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt))
wenzelm@51717
   317
    THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
wenzelm@51717
   318
    THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
haftmann@31068
   319
wenzelm@51717
   320
  val numeral_simp_ss =
wenzelm@51717
   321
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps add_0s @ simps)
wenzelm@51717
   322
  fun numeral_simp_tac ctxt =
wenzelm@51717
   323
    ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
huffman@45437
   324
  val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
haftmann@44945
   325
end;
haftmann@31068
   326
haftmann@31068
   327
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
haftmann@31068
   328
haftmann@31068
   329
(*Version for fields, where coefficients can be fractions*)
haftmann@31068
   330
structure FieldCombineNumeralsData =
haftmann@44945
   331
struct
haftmann@44945
   332
  type coeff = int * int
haftmann@44945
   333
  val iszero = (fn (p, q) => p = 0)
haftmann@44945
   334
  val add = add_frac
haftmann@44945
   335
  val mk_sum = long_mk_sum
haftmann@44945
   336
  val dest_sum = dest_sum
haftmann@44945
   337
  val mk_coeff = mk_fcoeff
haftmann@44945
   338
  val dest_coeff = dest_fcoeff 1
haftmann@44945
   339
  val left_distrib = @{thm combine_common_factor} RS trans
haftmann@44945
   340
  val prove_conv = Arith_Data.prove_conv_nohyps
haftmann@44947
   341
  val trans_tac = trans_tac
haftmann@31068
   342
wenzelm@51717
   343
  val norm_ss1a =
wenzelm@51717
   344
    simpset_of (put_simpset norm_ss1 @{context} addsimps inverse_1s @ divide_simps)
wenzelm@51717
   345
  fun norm_tac ctxt =
wenzelm@51717
   346
    ALLGOALS (simp_tac (put_simpset norm_ss1a ctxt))
wenzelm@51717
   347
    THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
wenzelm@51717
   348
    THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
haftmann@31068
   349
wenzelm@51717
   350
  val numeral_simp_ss =
wenzelm@51717
   351
    simpset_of (put_simpset HOL_basic_ss @{context}
wenzelm@51717
   352
      addsimps add_0s @ simps @ [@{thm add_frac_eq}, @{thm not_False_eq_True}])
wenzelm@51717
   353
  fun numeral_simp_tac ctxt =
wenzelm@51717
   354
    ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
huffman@45437
   355
  val simplify_meta_eq = Arith_Data.simplify_meta_eq field_post_simps
haftmann@44945
   356
end;
haftmann@31068
   357
haftmann@31068
   358
structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
haftmann@31068
   359
wenzelm@51717
   360
fun combine_numerals ctxt ct = CombineNumerals.proc ctxt (term_of ct)
haftmann@31068
   361
wenzelm@51717
   362
fun field_combine_numerals ctxt ct = FieldCombineNumerals.proc ctxt (term_of ct)
wenzelm@51717
   363
haftmann@31068
   364
haftmann@31068
   365
(** Constant folding for multiplication in semirings **)
haftmann@31068
   366
haftmann@31068
   367
(*We do not need folding for addition: combine_numerals does the same thing*)
haftmann@31068
   368
haftmann@31068
   369
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
haftmann@31068
   370
struct
wenzelm@51717
   371
  val assoc_ss = simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms mult_ac})
haftmann@31068
   372
  val eq_reflection = eq_reflection
boehmes@35983
   373
  val is_numeral = can HOLogic.dest_number
haftmann@31068
   374
end;
haftmann@31068
   375
haftmann@31068
   376
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
haftmann@31068
   377
wenzelm@51717
   378
fun assoc_fold ctxt ct = Semiring_Times_Assoc.proc ctxt (term_of ct)
wenzelm@23164
   379
wenzelm@23164
   380
structure CancelNumeralFactorCommon =
haftmann@44945
   381
struct
haftmann@44945
   382
  val mk_coeff = mk_coeff
haftmann@44945
   383
  val dest_coeff = dest_coeff 1
haftmann@44947
   384
  val trans_tac = trans_tac
wenzelm@23164
   385
wenzelm@51717
   386
  val norm_ss1 =
wenzelm@51717
   387
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps minus_from_mult_simps @ mult_1s)
wenzelm@51717
   388
  val norm_ss2 =
wenzelm@51717
   389
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps simps @ mult_minus_simps)
wenzelm@51717
   390
  val norm_ss3 =
wenzelm@51717
   391
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms mult_ac})
wenzelm@51717
   392
  fun norm_tac ctxt =
wenzelm@51717
   393
    ALLGOALS (simp_tac (put_simpset norm_ss1 ctxt))
wenzelm@51717
   394
    THEN ALLGOALS (simp_tac (put_simpset norm_ss2 ctxt))
wenzelm@51717
   395
    THEN ALLGOALS (simp_tac (put_simpset norm_ss3 ctxt))
wenzelm@23164
   396
huffman@45668
   397
  (* simp_thms are necessary because some of the cancellation rules below
huffman@45668
   398
  (e.g. mult_less_cancel_left) introduce various logical connectives *)
wenzelm@51717
   399
  val numeral_simp_ss =
wenzelm@51717
   400
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps simps @ @{thms simp_thms})
wenzelm@51717
   401
  fun numeral_simp_tac ctxt =
wenzelm@51717
   402
    ALLGOALS (simp_tac (put_simpset numeral_simp_ss ctxt))
haftmann@30518
   403
  val simplify_meta_eq = Arith_Data.simplify_meta_eq
huffman@45437
   404
    ([@{thm Nat.add_0}, @{thm Nat.add_0_right}] @ post_simps)
haftmann@44945
   405
  val prove_conv = Arith_Data.prove_conv
haftmann@44945
   406
end
wenzelm@23164
   407
haftmann@30931
   408
(*Version for semiring_div*)
haftmann@30931
   409
structure DivCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   410
 (open CancelNumeralFactorCommon
wenzelm@23164
   411
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
wenzelm@49323
   412
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} dummyT
haftmann@30931
   413
  val cancel = @{thm div_mult_mult1} RS trans
wenzelm@23164
   414
  val neg_exchanges = false
wenzelm@23164
   415
)
wenzelm@23164
   416
wenzelm@23164
   417
(*Version for fields*)
wenzelm@23164
   418
structure DivideCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   419
 (open CancelNumeralFactorCommon
huffman@44064
   420
  val mk_bal   = HOLogic.mk_binop @{const_name Fields.divide}
wenzelm@49323
   421
  val dest_bal = HOLogic.dest_bin @{const_name Fields.divide} dummyT
nipkow@23413
   422
  val cancel = @{thm mult_divide_mult_cancel_left} RS trans
wenzelm@23164
   423
  val neg_exchanges = false
wenzelm@23164
   424
)
wenzelm@23164
   425
wenzelm@23164
   426
structure EqCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   427
 (open CancelNumeralFactorCommon
wenzelm@23164
   428
  val mk_bal   = HOLogic.mk_eq
wenzelm@49323
   429
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
wenzelm@23164
   430
  val cancel = @{thm mult_cancel_left} RS trans
wenzelm@23164
   431
  val neg_exchanges = false
wenzelm@23164
   432
)
wenzelm@23164
   433
wenzelm@23164
   434
structure LessCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   435
 (open CancelNumeralFactorCommon
haftmann@35092
   436
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
wenzelm@49323
   437
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
wenzelm@23164
   438
  val cancel = @{thm mult_less_cancel_left} RS trans
wenzelm@23164
   439
  val neg_exchanges = true
wenzelm@23164
   440
)
wenzelm@23164
   441
wenzelm@23164
   442
structure LeCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   443
 (open CancelNumeralFactorCommon
haftmann@35092
   444
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
wenzelm@49323
   445
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
wenzelm@23164
   446
  val cancel = @{thm mult_le_cancel_left} RS trans
wenzelm@23164
   447
  val neg_exchanges = true
wenzelm@23164
   448
)
wenzelm@23164
   449
wenzelm@51717
   450
fun eq_cancel_numeral_factor ctxt ct = EqCancelNumeralFactor.proc ctxt (term_of ct)
wenzelm@51717
   451
fun less_cancel_numeral_factor ctxt ct = LessCancelNumeralFactor.proc ctxt (term_of ct)
wenzelm@51717
   452
fun le_cancel_numeral_factor ctxt ct = LeCancelNumeralFactor.proc ctxt (term_of ct)
wenzelm@51717
   453
fun div_cancel_numeral_factor ctxt ct = DivCancelNumeralFactor.proc ctxt (term_of ct)
wenzelm@51717
   454
fun divide_cancel_numeral_factor ctxt ct = DivideCancelNumeralFactor.proc ctxt (term_of ct)
wenzelm@23164
   455
wenzelm@23164
   456
val field_cancel_numeral_factors =
haftmann@44945
   457
  map (prep_simproc @{theory})
wenzelm@23164
   458
   [("field_eq_cancel_numeral_factor",
huffman@47108
   459
     ["(l::'a::field) * m = n",
huffman@47108
   460
      "(l::'a::field) = m * n"],
wenzelm@51717
   461
     EqCancelNumeralFactor.proc),
wenzelm@23164
   462
    ("field_cancel_numeral_factor",
huffman@47108
   463
     ["((l::'a::field_inverse_zero) * m) / n",
huffman@47108
   464
      "(l::'a::field_inverse_zero) / (m * n)",
huffman@47108
   465
      "((numeral v)::'a::field_inverse_zero) / (numeral w)",
huffman@47108
   466
      "((numeral v)::'a::field_inverse_zero) / (neg_numeral w)",
huffman@47108
   467
      "((neg_numeral v)::'a::field_inverse_zero) / (numeral w)",
huffman@47108
   468
      "((neg_numeral v)::'a::field_inverse_zero) / (neg_numeral w)"],
wenzelm@51717
   469
     DivideCancelNumeralFactor.proc)]
wenzelm@23164
   470
wenzelm@23164
   471
wenzelm@23164
   472
(** Declarations for ExtractCommonTerm **)
wenzelm@23164
   473
wenzelm@23164
   474
(*Find first term that matches u*)
wenzelm@23164
   475
fun find_first_t past u []         = raise TERM ("find_first_t", [])
wenzelm@23164
   476
  | find_first_t past u (t::terms) =
wenzelm@23164
   477
        if u aconv t then (rev past @ terms)
wenzelm@23164
   478
        else find_first_t (t::past) u terms
wenzelm@23164
   479
        handle TERM _ => find_first_t (t::past) u terms;
wenzelm@23164
   480
wenzelm@23164
   481
(** Final simplification for the CancelFactor simprocs **)
haftmann@30518
   482
val simplify_one = Arith_Data.simplify_meta_eq  
nipkow@30031
   483
  [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_1_eq_1}];
wenzelm@23164
   484
wenzelm@51717
   485
fun cancel_simplify_meta_eq ctxt cancel_th th =
wenzelm@51717
   486
    simplify_one ctxt (([th, cancel_th]) MRS trans);
wenzelm@23164
   487
nipkow@30649
   488
local
haftmann@31067
   489
  val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory HOL} HOLogic.Trueprop)
nipkow@30649
   490
  fun Eq_True_elim Eq = 
nipkow@30649
   491
    Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
nipkow@30649
   492
in
wenzelm@51717
   493
fun sign_conv pos_th neg_th ctxt t =
nipkow@30649
   494
  let val T = fastype_of t;
haftmann@35267
   495
      val zero = Const(@{const_name Groups.zero}, T);
haftmann@35092
   496
      val less = Const(@{const_name Orderings.less}, [T,T] ---> HOLogic.boolT);
nipkow@30649
   497
      val pos = less $ zero $ t and neg = less $ t $ zero
wenzelm@51717
   498
      val thy = Proof_Context.theory_of ctxt
nipkow@30649
   499
      fun prove p =
wenzelm@51717
   500
        SOME (Eq_True_elim (Simplifier.asm_rewrite ctxt (Thm.cterm_of thy p)))
nipkow@30649
   501
        handle THM _ => NONE
nipkow@30649
   502
    in case prove pos of
nipkow@30649
   503
         SOME th => SOME(th RS pos_th)
nipkow@30649
   504
       | NONE => (case prove neg of
nipkow@30649
   505
                    SOME th => SOME(th RS neg_th)
nipkow@30649
   506
                  | NONE => NONE)
nipkow@30649
   507
    end;
nipkow@30649
   508
end
nipkow@30649
   509
wenzelm@23164
   510
structure CancelFactorCommon =
haftmann@44945
   511
struct
haftmann@44945
   512
  val mk_sum = long_mk_prod
haftmann@44945
   513
  val dest_sum = dest_prod
haftmann@44945
   514
  val mk_coeff = mk_coeff
haftmann@44945
   515
  val dest_coeff = dest_coeff
haftmann@44945
   516
  val find_first = find_first_t []
haftmann@44947
   517
  val trans_tac = trans_tac
wenzelm@51717
   518
  val norm_ss =
wenzelm@51717
   519
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps mult_1s @ @{thms mult_ac})
wenzelm@51717
   520
  fun norm_tac ctxt =
wenzelm@51717
   521
    ALLGOALS (simp_tac (put_simpset norm_ss ctxt))
nipkow@30649
   522
  val simplify_meta_eq  = cancel_simplify_meta_eq 
huffman@45270
   523
  fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
haftmann@44945
   524
end;
wenzelm@23164
   525
wenzelm@23164
   526
(*mult_cancel_left requires a ring with no zero divisors.*)
wenzelm@23164
   527
structure EqCancelFactor = ExtractCommonTermFun
wenzelm@23164
   528
 (open CancelFactorCommon
wenzelm@23164
   529
  val mk_bal   = HOLogic.mk_eq
wenzelm@49323
   530
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
wenzelm@31368
   531
  fun simp_conv _ _ = SOME @{thm mult_cancel_left}
nipkow@30649
   532
);
nipkow@30649
   533
nipkow@30649
   534
(*for ordered rings*)
nipkow@30649
   535
structure LeCancelFactor = ExtractCommonTermFun
nipkow@30649
   536
 (open CancelFactorCommon
haftmann@35092
   537
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
wenzelm@49323
   538
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
nipkow@30649
   539
  val simp_conv = sign_conv
nipkow@30649
   540
    @{thm mult_le_cancel_left_pos} @{thm mult_le_cancel_left_neg}
nipkow@30649
   541
);
nipkow@30649
   542
nipkow@30649
   543
(*for ordered rings*)
nipkow@30649
   544
structure LessCancelFactor = ExtractCommonTermFun
nipkow@30649
   545
 (open CancelFactorCommon
haftmann@35092
   546
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
wenzelm@49323
   547
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
nipkow@30649
   548
  val simp_conv = sign_conv
nipkow@30649
   549
    @{thm mult_less_cancel_left_pos} @{thm mult_less_cancel_left_neg}
wenzelm@23164
   550
);
wenzelm@23164
   551
haftmann@30931
   552
(*for semirings with division*)
haftmann@30931
   553
structure DivCancelFactor = ExtractCommonTermFun
wenzelm@23164
   554
 (open CancelFactorCommon
wenzelm@23164
   555
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
wenzelm@49323
   556
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} dummyT
wenzelm@31368
   557
  fun simp_conv _ _ = SOME @{thm div_mult_mult1_if}
wenzelm@23164
   558
);
wenzelm@23164
   559
haftmann@30931
   560
structure ModCancelFactor = ExtractCommonTermFun
nipkow@24395
   561
 (open CancelFactorCommon
nipkow@24395
   562
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.mod}
wenzelm@49323
   563
  val dest_bal = HOLogic.dest_bin @{const_name Divides.mod} dummyT
wenzelm@31368
   564
  fun simp_conv _ _ = SOME @{thm mod_mult_mult1}
nipkow@24395
   565
);
nipkow@24395
   566
haftmann@30931
   567
(*for idoms*)
haftmann@30931
   568
structure DvdCancelFactor = ExtractCommonTermFun
nipkow@23969
   569
 (open CancelFactorCommon
haftmann@35050
   570
  val mk_bal   = HOLogic.mk_binrel @{const_name Rings.dvd}
wenzelm@49323
   571
  val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} dummyT
wenzelm@31368
   572
  fun simp_conv _ _ = SOME @{thm dvd_mult_cancel_left}
nipkow@23969
   573
);
nipkow@23969
   574
wenzelm@23164
   575
(*Version for all fields, including unordered ones (type complex).*)
wenzelm@23164
   576
structure DivideCancelFactor = ExtractCommonTermFun
wenzelm@23164
   577
 (open CancelFactorCommon
huffman@44064
   578
  val mk_bal   = HOLogic.mk_binop @{const_name Fields.divide}
wenzelm@49323
   579
  val dest_bal = HOLogic.dest_bin @{const_name Fields.divide} dummyT
wenzelm@31368
   580
  fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
wenzelm@23164
   581
);
wenzelm@23164
   582
wenzelm@51717
   583
fun eq_cancel_factor ctxt ct = EqCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   584
fun le_cancel_factor ctxt ct = LeCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   585
fun less_cancel_factor ctxt ct = LessCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   586
fun div_cancel_factor ctxt ct = DivCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   587
fun mod_cancel_factor ctxt ct = ModCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   588
fun dvd_cancel_factor ctxt ct = DvdCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   589
fun divide_cancel_factor ctxt ct = DivideCancelFactor.proc ctxt (term_of ct)
wenzelm@23164
   590
haftmann@36751
   591
local
haftmann@36751
   592
 val zr = @{cpat "0"}
haftmann@36751
   593
 val zT = ctyp_of_term zr
haftmann@38864
   594
 val geq = @{cpat HOL.eq}
haftmann@36751
   595
 val eqT = Thm.dest_ctyp (ctyp_of_term geq) |> hd
haftmann@36751
   596
 val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
haftmann@36751
   597
 val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
haftmann@36751
   598
 val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
haftmann@36751
   599
wenzelm@51717
   600
 fun prove_nz ctxt T t =
haftmann@36751
   601
    let
wenzelm@36945
   602
      val z = Thm.instantiate_cterm ([(zT,T)],[]) zr
wenzelm@36945
   603
      val eq = Thm.instantiate_cterm ([(eqT,T)],[]) geq
wenzelm@51717
   604
      val th = Simplifier.rewrite (ctxt addsimps @{thms simp_thms})
wenzelm@46497
   605
           (Thm.apply @{cterm "Trueprop"} (Thm.apply @{cterm "Not"}
wenzelm@46497
   606
                  (Thm.apply (Thm.apply eq t) z)))
wenzelm@36945
   607
    in Thm.equal_elim (Thm.symmetric th) TrueI
haftmann@36751
   608
    end
haftmann@36751
   609
wenzelm@51717
   610
 fun proc phi ctxt ct =
haftmann@36751
   611
  let
haftmann@36751
   612
    val ((x,y),(w,z)) =
haftmann@36751
   613
         (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct
haftmann@36751
   614
    val _ = map (HOLogic.dest_number o term_of) [x,y,z,w]
haftmann@36751
   615
    val T = ctyp_of_term x
wenzelm@51717
   616
    val [y_nz, z_nz] = map (prove_nz ctxt T) [y, z]
haftmann@36751
   617
    val th = instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq
wenzelm@36945
   618
  in SOME (Thm.implies_elim (Thm.implies_elim th y_nz) z_nz)
haftmann@36751
   619
  end
haftmann@36751
   620
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36751
   621
wenzelm@51717
   622
 fun proc2 phi ctxt ct =
haftmann@36751
   623
  let
haftmann@36751
   624
    val (l,r) = Thm.dest_binop ct
haftmann@36751
   625
    val T = ctyp_of_term l
haftmann@36751
   626
  in (case (term_of l, term_of r) of
huffman@44064
   627
      (Const(@{const_name Fields.divide},_)$_$_, _) =>
haftmann@36751
   628
        let val (x,y) = Thm.dest_binop l val z = r
haftmann@36751
   629
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
wenzelm@51717
   630
            val ynz = prove_nz ctxt T y
wenzelm@36945
   631
        in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz)
haftmann@36751
   632
        end
huffman@44064
   633
     | (_, Const (@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   634
        let val (x,y) = Thm.dest_binop r val z = l
haftmann@36751
   635
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
wenzelm@51717
   636
            val ynz = prove_nz ctxt T y
wenzelm@36945
   637
        in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz)
haftmann@36751
   638
        end
haftmann@36751
   639
     | _ => NONE)
haftmann@36751
   640
  end
haftmann@36751
   641
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36751
   642
huffman@44064
   643
 fun is_number (Const(@{const_name Fields.divide},_)$a$b) = is_number a andalso is_number b
haftmann@36751
   644
   | is_number t = can HOLogic.dest_number t
haftmann@36751
   645
haftmann@36751
   646
 val is_number = is_number o term_of
haftmann@36751
   647
wenzelm@51717
   648
 fun proc3 phi ctxt ct =
haftmann@36751
   649
  (case term_of ct of
huffman@44064
   650
    Const(@{const_name Orderings.less},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   651
      let
haftmann@36751
   652
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   653
        val _ = map is_number [a,b,c]
haftmann@36751
   654
        val T = ctyp_of_term c
haftmann@36751
   655
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_less_eq"}
haftmann@36751
   656
      in SOME (mk_meta_eq th) end
huffman@44064
   657
  | Const(@{const_name Orderings.less_eq},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   658
      let
haftmann@36751
   659
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   660
        val _ = map is_number [a,b,c]
haftmann@36751
   661
        val T = ctyp_of_term c
haftmann@36751
   662
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_le_eq"}
haftmann@36751
   663
      in SOME (mk_meta_eq th) end
huffman@44064
   664
  | Const(@{const_name HOL.eq},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   665
      let
haftmann@36751
   666
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   667
        val _ = map is_number [a,b,c]
haftmann@36751
   668
        val T = ctyp_of_term c
haftmann@36751
   669
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_eq_eq"}
haftmann@36751
   670
      in SOME (mk_meta_eq th) end
huffman@44064
   671
  | Const(@{const_name Orderings.less},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   672
    let
haftmann@36751
   673
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   674
        val _ = map is_number [a,b,c]
haftmann@36751
   675
        val T = ctyp_of_term c
haftmann@36751
   676
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "less_divide_eq"}
haftmann@36751
   677
      in SOME (mk_meta_eq th) end
huffman@44064
   678
  | Const(@{const_name Orderings.less_eq},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   679
    let
haftmann@36751
   680
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   681
        val _ = map is_number [a,b,c]
haftmann@36751
   682
        val T = ctyp_of_term c
haftmann@36751
   683
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "le_divide_eq"}
haftmann@36751
   684
      in SOME (mk_meta_eq th) end
huffman@44064
   685
  | Const(@{const_name HOL.eq},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   686
    let
haftmann@36751
   687
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   688
        val _ = map is_number [a,b,c]
haftmann@36751
   689
        val T = ctyp_of_term c
haftmann@36751
   690
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "eq_divide_eq"}
haftmann@36751
   691
      in SOME (mk_meta_eq th) end
haftmann@36751
   692
  | _ => NONE)
haftmann@36751
   693
  handle TERM _ => NONE | CTERM _ => NONE | THM _ => NONE
haftmann@36751
   694
haftmann@36751
   695
val add_frac_frac_simproc =
haftmann@36751
   696
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + (?w::?'a::field)/?z"}],
haftmann@36751
   697
                     name = "add_frac_frac_simproc",
haftmann@36751
   698
                     proc = proc, identifier = []}
haftmann@36751
   699
haftmann@36751
   700
val add_frac_num_simproc =
haftmann@36751
   701
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + ?z"}, @{cpat "?z + (?x::?'a::field)/?y"}],
haftmann@36751
   702
                     name = "add_frac_num_simproc",
haftmann@36751
   703
                     proc = proc2, identifier = []}
haftmann@36751
   704
haftmann@36751
   705
val ord_frac_simproc =
haftmann@36751
   706
  make_simproc
haftmann@36751
   707
    {lhss = [@{cpat "(?a::(?'a::{field, ord}))/?b < ?c"},
haftmann@36751
   708
             @{cpat "(?a::(?'a::{field, ord}))/?b <= ?c"},
haftmann@36751
   709
             @{cpat "?c < (?a::(?'a::{field, ord}))/?b"},
haftmann@36751
   710
             @{cpat "?c <= (?a::(?'a::{field, ord}))/?b"},
haftmann@36751
   711
             @{cpat "?c = ((?a::(?'a::{field, ord}))/?b)"},
haftmann@36751
   712
             @{cpat "((?a::(?'a::{field, ord}))/ ?b) = ?c"}],
haftmann@36751
   713
             name = "ord_frac_simproc", proc = proc3, identifier = []}
haftmann@36751
   714
haftmann@36751
   715
val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"},
haftmann@36751
   716
           @{thm "divide_Numeral1"},
huffman@47108
   717
           @{thm "divide_zero"}, @{thm divide_zero_left},
haftmann@36751
   718
           @{thm "divide_divide_eq_left"}, 
haftmann@36751
   719
           @{thm "times_divide_eq_left"}, @{thm "times_divide_eq_right"},
haftmann@36751
   720
           @{thm "times_divide_times_eq"},
haftmann@36751
   721
           @{thm "divide_divide_eq_right"},
haftmann@54230
   722
           @{thm diff_conv_add_uminus}, @{thm "minus_divide_left"},
huffman@47108
   723
           @{thm "add_divide_distrib"} RS sym,
haftmann@36751
   724
           @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
haftmann@36751
   725
           Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult_commute}))))   
haftmann@36751
   726
           (@{thm field_divide_inverse} RS sym)]
haftmann@36751
   727
wenzelm@51717
   728
val field_comp_ss =
wenzelm@51717
   729
  simpset_of
wenzelm@51717
   730
    (put_simpset HOL_basic_ss @{context}
wenzelm@51717
   731
      addsimps @{thms "semiring_norm"}
wenzelm@45620
   732
      addsimps ths addsimps @{thms simp_thms}
wenzelm@45620
   733
      addsimprocs field_cancel_numeral_factors
wenzelm@45620
   734
      addsimprocs [add_frac_frac_simproc, add_frac_num_simproc, ord_frac_simproc]
wenzelm@45620
   735
      |> Simplifier.add_cong @{thm "if_weak_cong"})
wenzelm@51717
   736
wenzelm@51717
   737
in
wenzelm@51717
   738
wenzelm@51717
   739
fun field_comp_conv ctxt =
wenzelm@51717
   740
  Simplifier.rewrite (put_simpset field_comp_ss ctxt)
wenzelm@45620
   741
  then_conv
wenzelm@51717
   742
  Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps [@{thm numeral_1_eq_1}])
haftmann@36751
   743
haftmann@36751
   744
end
haftmann@36751
   745
wenzelm@23164
   746
end;
wenzelm@23164
   747
haftmann@31068
   748
(*examples:
haftmann@31068
   749
print_depth 22;
haftmann@31068
   750
set timing;
wenzelm@40878
   751
set simp_trace;
haftmann@31068
   752
fun test s = (Goal s, by (Simp_tac 1));
haftmann@31068
   753
haftmann@31068
   754
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
haftmann@31068
   755
haftmann@31068
   756
test "2*u = (u::int)";
haftmann@31068
   757
test "(i + j + 12 + (k::int)) - 15 = y";
haftmann@31068
   758
test "(i + j + 12 + (k::int)) - 5 = y";
haftmann@31068
   759
haftmann@31068
   760
test "y - b < (b::int)";
haftmann@31068
   761
test "y - (3*b + c) < (b::int) - 2*c";
haftmann@31068
   762
haftmann@31068
   763
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
haftmann@31068
   764
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
haftmann@31068
   765
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
haftmann@31068
   766
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
haftmann@31068
   767
haftmann@31068
   768
test "(i + j + 12 + (k::int)) = u + 15 + y";
haftmann@31068
   769
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
haftmann@31068
   770
haftmann@31068
   771
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)";
haftmann@31068
   772
haftmann@31068
   773
test "a + -(b+c) + b = (d::int)";
haftmann@31068
   774
test "a + -(b+c) - b = (d::int)";
haftmann@31068
   775
haftmann@31068
   776
(*negative numerals*)
haftmann@31068
   777
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
haftmann@31068
   778
test "(i + j + -3 + (k::int)) < u + 5 + y";
haftmann@31068
   779
test "(i + j + 3 + (k::int)) < u + -6 + y";
haftmann@31068
   780
test "(i + j + -12 + (k::int)) - 15 = y";
haftmann@31068
   781
test "(i + j + 12 + (k::int)) - -15 = y";
haftmann@31068
   782
test "(i + j + -12 + (k::int)) - -15 = y";
haftmann@31068
   783
*)
haftmann@31068
   784
haftmann@31068
   785
(*examples:
haftmann@31068
   786
print_depth 22;
haftmann@31068
   787
set timing;
wenzelm@40878
   788
set simp_trace;
haftmann@31068
   789
fun test s = (Goal s; by (Simp_tac 1));
haftmann@31068
   790
haftmann@31068
   791
test "9*x = 12 * (y::int)";
haftmann@31068
   792
test "(9*x) div (12 * (y::int)) = z";
haftmann@31068
   793
test "9*x < 12 * (y::int)";
haftmann@31068
   794
test "9*x <= 12 * (y::int)";
haftmann@31068
   795
haftmann@31068
   796
test "-99*x = 132 * (y::int)";
haftmann@31068
   797
test "(-99*x) div (132 * (y::int)) = z";
haftmann@31068
   798
test "-99*x < 132 * (y::int)";
haftmann@31068
   799
test "-99*x <= 132 * (y::int)";
haftmann@31068
   800
haftmann@31068
   801
test "999*x = -396 * (y::int)";
haftmann@31068
   802
test "(999*x) div (-396 * (y::int)) = z";
haftmann@31068
   803
test "999*x < -396 * (y::int)";
haftmann@31068
   804
test "999*x <= -396 * (y::int)";
haftmann@31068
   805
haftmann@31068
   806
test "-99*x = -81 * (y::int)";
haftmann@31068
   807
test "(-99*x) div (-81 * (y::int)) = z";
haftmann@31068
   808
test "-99*x <= -81 * (y::int)";
haftmann@31068
   809
test "-99*x < -81 * (y::int)";
haftmann@31068
   810
haftmann@31068
   811
test "-2 * x = -1 * (y::int)";
haftmann@31068
   812
test "-2 * x = -(y::int)";
haftmann@31068
   813
test "(-2 * x) div (-1 * (y::int)) = z";
haftmann@31068
   814
test "-2 * x < -(y::int)";
haftmann@31068
   815
test "-2 * x <= -1 * (y::int)";
haftmann@31068
   816
test "-x < -23 * (y::int)";
haftmann@31068
   817
test "-x <= -23 * (y::int)";
haftmann@31068
   818
*)
haftmann@31068
   819
haftmann@31068
   820
(*And the same examples for fields such as rat or real:
haftmann@31068
   821
test "0 <= (y::rat) * -2";
haftmann@31068
   822
test "9*x = 12 * (y::rat)";
haftmann@31068
   823
test "(9*x) / (12 * (y::rat)) = z";
haftmann@31068
   824
test "9*x < 12 * (y::rat)";
haftmann@31068
   825
test "9*x <= 12 * (y::rat)";
haftmann@31068
   826
haftmann@31068
   827
test "-99*x = 132 * (y::rat)";
haftmann@31068
   828
test "(-99*x) / (132 * (y::rat)) = z";
haftmann@31068
   829
test "-99*x < 132 * (y::rat)";
haftmann@31068
   830
test "-99*x <= 132 * (y::rat)";
haftmann@31068
   831
haftmann@31068
   832
test "999*x = -396 * (y::rat)";
haftmann@31068
   833
test "(999*x) / (-396 * (y::rat)) = z";
haftmann@31068
   834
test "999*x < -396 * (y::rat)";
haftmann@31068
   835
test "999*x <= -396 * (y::rat)";
haftmann@31068
   836
haftmann@31068
   837
test  "(- ((2::rat) * x) <= 2 * y)";
haftmann@31068
   838
test "-99*x = -81 * (y::rat)";
haftmann@31068
   839
test "(-99*x) / (-81 * (y::rat)) = z";
haftmann@31068
   840
test "-99*x <= -81 * (y::rat)";
haftmann@31068
   841
test "-99*x < -81 * (y::rat)";
haftmann@31068
   842
haftmann@31068
   843
test "-2 * x = -1 * (y::rat)";
haftmann@31068
   844
test "-2 * x = -(y::rat)";
haftmann@31068
   845
test "(-2 * x) / (-1 * (y::rat)) = z";
haftmann@31068
   846
test "-2 * x < -(y::rat)";
haftmann@31068
   847
test "-2 * x <= -1 * (y::rat)";
haftmann@31068
   848
test "-x < -23 * (y::rat)";
haftmann@31068
   849
test "-x <= -23 * (y::rat)";
haftmann@31068
   850
*)
haftmann@31068
   851
wenzelm@23164
   852
(*examples:
wenzelm@23164
   853
print_depth 22;
wenzelm@23164
   854
set timing;
wenzelm@40878
   855
set simp_trace;
wenzelm@23164
   856
fun test s = (Goal s; by (Asm_simp_tac 1));
wenzelm@23164
   857
wenzelm@23164
   858
test "x*k = k*(y::int)";
wenzelm@23164
   859
test "k = k*(y::int)";
wenzelm@23164
   860
test "a*(b*c) = (b::int)";
wenzelm@23164
   861
test "a*(b*c) = d*(b::int)*(x*a)";
wenzelm@23164
   862
wenzelm@23164
   863
test "(x*k) div (k*(y::int)) = (uu::int)";
wenzelm@23164
   864
test "(k) div (k*(y::int)) = (uu::int)";
wenzelm@23164
   865
test "(a*(b*c)) div ((b::int)) = (uu::int)";
wenzelm@23164
   866
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
wenzelm@23164
   867
*)
wenzelm@23164
   868
wenzelm@23164
   869
(*And the same examples for fields such as rat or real:
wenzelm@23164
   870
print_depth 22;
wenzelm@23164
   871
set timing;
wenzelm@40878
   872
set simp_trace;
wenzelm@23164
   873
fun test s = (Goal s; by (Asm_simp_tac 1));
wenzelm@23164
   874
wenzelm@23164
   875
test "x*k = k*(y::rat)";
wenzelm@23164
   876
test "k = k*(y::rat)";
wenzelm@23164
   877
test "a*(b*c) = (b::rat)";
wenzelm@23164
   878
test "a*(b*c) = d*(b::rat)*(x*a)";
wenzelm@23164
   879
wenzelm@23164
   880
wenzelm@23164
   881
test "(x*k) / (k*(y::rat)) = (uu::rat)";
wenzelm@23164
   882
test "(k) / (k*(y::rat)) = (uu::rat)";
wenzelm@23164
   883
test "(a*(b*c)) / ((b::rat)) = (uu::rat)";
wenzelm@23164
   884
test "(a*(b*c)) / (d*(b::rat)*(x*a)) = (uu::rat)";
wenzelm@23164
   885
wenzelm@23164
   886
(*FIXME: what do we do about this?*)
wenzelm@23164
   887
test "a*(b*c)/(y*z) = d*(b::rat)*(x*a)/z";
wenzelm@23164
   888
*)