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