src/HOL/Tools/numeral_simprocs.ML
author wenzelm
Wed Sep 12 13:56:49 2012 +0200 (2012-09-12)
changeset 49323 6dff6b1f5417
parent 47108 2a1953f0d20d
child 51717 9e7d1c139569
permissions -rw-r--r--
standardized ML aliases;
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
haftmann@44945
    19
  val prep_simproc: theory -> string * string list * (theory -> simpset -> term -> thm option)
haftmann@44945
    20
    -> simproc
haftmann@44945
    21
  val trans_tac: thm option -> tactic
huffman@45284
    22
  val assoc_fold: simpset -> cterm -> thm option
huffman@45284
    23
  val combine_numerals: simpset -> cterm -> thm option
huffman@45284
    24
  val eq_cancel_numerals: simpset -> cterm -> thm option
huffman@45284
    25
  val less_cancel_numerals: simpset -> cterm -> thm option
huffman@45284
    26
  val le_cancel_numerals: simpset -> cterm -> thm option
huffman@45284
    27
  val eq_cancel_factor: simpset -> cterm -> thm option
huffman@45284
    28
  val le_cancel_factor: simpset -> cterm -> thm option
huffman@45284
    29
  val less_cancel_factor: simpset -> cterm -> thm option
huffman@45284
    30
  val div_cancel_factor: simpset -> cterm -> thm option
huffman@45284
    31
  val mod_cancel_factor: simpset -> cterm -> thm option
huffman@45284
    32
  val dvd_cancel_factor: simpset -> cterm -> thm option
huffman@45284
    33
  val divide_cancel_factor: simpset -> cterm -> thm option
huffman@45284
    34
  val eq_cancel_numeral_factor: simpset -> cterm -> thm option
huffman@45284
    35
  val less_cancel_numeral_factor: simpset -> cterm -> thm option
huffman@45284
    36
  val le_cancel_numeral_factor: simpset -> cterm -> thm option
huffman@45284
    37
  val div_cancel_numeral_factor: simpset -> cterm -> thm option
huffman@45284
    38
  val divide_cancel_numeral_factor: simpset -> cterm -> thm option
huffman@45284
    39
  val field_combine_numerals: simpset -> cterm -> thm option
haftmann@31068
    40
  val field_cancel_numeral_factors: simproc list
haftmann@31068
    41
  val num_ss: simpset
haftmann@36751
    42
  val field_comp_conv: 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
huffman@45668
   175
val num_ss = HOL_basic_ss |> Simplifier.set_termless numtermless;
haftmann@31068
   176
huffman@47108
   177
(*Maps 1 to Numeral1 so that arithmetic isn't complicated by the abstract 1.*)
huffman@47108
   178
val numeral_syms = [@{thm numeral_1_eq_1} RS sym];
haftmann@31068
   179
huffman@47108
   180
(*Simplify 0+n, n+0, Numeral1*n, n*Numeral1, 1*x, x*1, x/1 *)
haftmann@31068
   181
val add_0s =  @{thms add_0s};
haftmann@31068
   182
val mult_1s = @{thms mult_1s mult_1_left mult_1_right divide_1};
haftmann@31068
   183
huffman@45437
   184
(* For post-simplification of the rhs of simproc-generated rules *)
huffman@45437
   185
val post_simps =
huffman@47108
   186
    [@{thm numeral_1_eq_1},
huffman@45437
   187
     @{thm add_0_left}, @{thm add_0_right},
huffman@45437
   188
     @{thm mult_zero_left}, @{thm mult_zero_right},
huffman@45437
   189
     @{thm mult_1_left}, @{thm mult_1_right},
huffman@45437
   190
     @{thm mult_minus1}, @{thm mult_minus1_right}]
huffman@45437
   191
huffman@45437
   192
val field_post_simps =
huffman@45437
   193
    post_simps @ [@{thm divide_zero_left}, @{thm divide_1}]
huffman@45437
   194
                      
haftmann@31068
   195
(*Simplify inverse Numeral1, a/Numeral1*)
haftmann@31068
   196
val inverse_1s = [@{thm inverse_numeral_1}];
haftmann@31068
   197
val divide_1s = [@{thm divide_numeral_1}];
haftmann@31068
   198
haftmann@31068
   199
(*To perform binary arithmetic.  The "left" rewriting handles patterns
haftmann@31068
   200
  created by the Numeral_Simprocs, such as 3 * (5 * x). *)
huffman@47108
   201
val simps =
huffman@47108
   202
    [@{thm numeral_1_eq_1} RS sym] @
huffman@47108
   203
    @{thms add_numeral_left} @
huffman@47108
   204
    @{thms add_neg_numeral_left} @
huffman@47108
   205
    @{thms mult_numeral_left} @
huffman@47108
   206
    @{thms arith_simps} @ @{thms rel_simps};
huffman@47108
   207
    
haftmann@31068
   208
(*Binary arithmetic BUT NOT ADDITION since it may collapse adjacent terms
haftmann@31068
   209
  during re-arrangement*)
haftmann@31068
   210
val non_add_simps =
huffman@47108
   211
  subtract Thm.eq_thm
huffman@47108
   212
    (@{thms add_numeral_left} @
huffman@47108
   213
     @{thms add_neg_numeral_left} @
huffman@47108
   214
     @{thms numeral_plus_numeral} @
huffman@47108
   215
     @{thms add_neg_numeral_simps}) simps;
wenzelm@23164
   216
haftmann@31068
   217
(*To evaluate binary negations of coefficients*)
huffman@47108
   218
val minus_simps = [@{thm minus_zero}, @{thm minus_one}, @{thm minus_numeral}, @{thm minus_neg_numeral}];
haftmann@31068
   219
haftmann@31068
   220
(*To let us treat subtraction as addition*)
haftmann@31068
   221
val diff_simps = [@{thm diff_minus}, @{thm minus_add_distrib}, @{thm minus_minus}];
haftmann@31068
   222
haftmann@31068
   223
(*To let us treat division as multiplication*)
haftmann@31068
   224
val divide_simps = [@{thm divide_inverse}, @{thm inverse_mult_distrib}, @{thm inverse_inverse_eq}];
haftmann@31068
   225
wenzelm@35020
   226
(*push the unary minus down*)
wenzelm@35020
   227
val minus_mult_eq_1_to_2 = @{lemma "- (a::'a::ring) * b = a * - b" by simp};
haftmann@31068
   228
haftmann@31068
   229
(*to extract again any uncancelled minuses*)
haftmann@31068
   230
val minus_from_mult_simps =
haftmann@31068
   231
    [@{thm minus_minus}, @{thm mult_minus_left}, @{thm mult_minus_right}];
haftmann@31068
   232
haftmann@31068
   233
(*combine unary minus with numeric literals, however nested within a product*)
haftmann@31068
   234
val mult_minus_simps =
haftmann@31068
   235
    [@{thm mult_assoc}, @{thm minus_mult_left}, minus_mult_eq_1_to_2];
haftmann@31068
   236
haftmann@31068
   237
val norm_ss1 = num_ss addsimps numeral_syms @ add_0s @ mult_1s @
haftmann@31068
   238
  diff_simps @ minus_simps @ @{thms add_ac}
haftmann@31068
   239
val norm_ss2 = num_ss addsimps non_add_simps @ mult_minus_simps
haftmann@31068
   240
val norm_ss3 = num_ss addsimps minus_from_mult_simps @ @{thms add_ac} @ @{thms mult_ac}
haftmann@31068
   241
haftmann@31068
   242
structure CancelNumeralsCommon =
haftmann@44945
   243
struct
haftmann@44945
   244
  val mk_sum = mk_sum
haftmann@44945
   245
  val dest_sum = dest_sum
haftmann@44945
   246
  val mk_coeff = mk_coeff
haftmann@44945
   247
  val dest_coeff = dest_coeff 1
haftmann@44945
   248
  val find_first_coeff = find_first_coeff []
haftmann@44947
   249
  val trans_tac = trans_tac
haftmann@31068
   250
haftmann@31068
   251
  fun norm_tac ss =
haftmann@31068
   252
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
haftmann@31068
   253
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
haftmann@31068
   254
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
haftmann@31068
   255
huffman@45668
   256
  val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ simps
haftmann@31068
   257
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
huffman@45437
   258
  val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
haftmann@44945
   259
  val prove_conv = Arith_Data.prove_conv
haftmann@44945
   260
end;
haftmann@31068
   261
haftmann@31068
   262
structure EqCancelNumerals = CancelNumeralsFun
haftmann@31068
   263
 (open CancelNumeralsCommon
haftmann@31068
   264
  val mk_bal   = HOLogic.mk_eq
wenzelm@49323
   265
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
haftmann@31068
   266
  val bal_add1 = @{thm eq_add_iff1} RS trans
haftmann@31068
   267
  val bal_add2 = @{thm eq_add_iff2} RS trans
haftmann@31068
   268
);
haftmann@31068
   269
haftmann@31068
   270
structure LessCancelNumerals = CancelNumeralsFun
haftmann@31068
   271
 (open CancelNumeralsCommon
haftmann@35092
   272
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
wenzelm@49323
   273
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
haftmann@31068
   274
  val bal_add1 = @{thm less_add_iff1} RS trans
haftmann@31068
   275
  val bal_add2 = @{thm less_add_iff2} RS trans
haftmann@31068
   276
);
haftmann@31068
   277
haftmann@31068
   278
structure LeCancelNumerals = CancelNumeralsFun
haftmann@31068
   279
 (open CancelNumeralsCommon
haftmann@35092
   280
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
wenzelm@49323
   281
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
haftmann@31068
   282
  val bal_add1 = @{thm le_add_iff1} RS trans
haftmann@31068
   283
  val bal_add2 = @{thm le_add_iff2} RS trans
haftmann@31068
   284
);
haftmann@31068
   285
huffman@45284
   286
fun eq_cancel_numerals ss ct = EqCancelNumerals.proc ss (term_of ct)
huffman@45284
   287
fun less_cancel_numerals ss ct = LessCancelNumerals.proc ss (term_of ct)
huffman@45284
   288
fun le_cancel_numerals ss ct = LeCancelNumerals.proc ss (term_of ct)
haftmann@31068
   289
haftmann@31068
   290
structure CombineNumeralsData =
haftmann@44945
   291
struct
haftmann@44945
   292
  type coeff = int
haftmann@44945
   293
  val iszero = (fn x => x = 0)
haftmann@44945
   294
  val add  = op +
haftmann@44945
   295
  val mk_sum = long_mk_sum    (*to work for e.g. 2*x + 3*x *)
haftmann@44945
   296
  val dest_sum = dest_sum
haftmann@44945
   297
  val mk_coeff = mk_coeff
haftmann@44945
   298
  val dest_coeff = dest_coeff 1
haftmann@44945
   299
  val left_distrib = @{thm combine_common_factor} RS trans
haftmann@44945
   300
  val prove_conv = Arith_Data.prove_conv_nohyps
haftmann@44947
   301
  val trans_tac = trans_tac
haftmann@31068
   302
haftmann@31068
   303
  fun norm_tac ss =
haftmann@31068
   304
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
haftmann@31068
   305
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
haftmann@31068
   306
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
haftmann@31068
   307
huffman@45668
   308
  val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ simps
haftmann@31068
   309
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
huffman@45437
   310
  val simplify_meta_eq = Arith_Data.simplify_meta_eq post_simps
haftmann@44945
   311
end;
haftmann@31068
   312
haftmann@31068
   313
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
haftmann@31068
   314
haftmann@31068
   315
(*Version for fields, where coefficients can be fractions*)
haftmann@31068
   316
structure FieldCombineNumeralsData =
haftmann@44945
   317
struct
haftmann@44945
   318
  type coeff = int * int
haftmann@44945
   319
  val iszero = (fn (p, q) => p = 0)
haftmann@44945
   320
  val add = add_frac
haftmann@44945
   321
  val mk_sum = long_mk_sum
haftmann@44945
   322
  val dest_sum = dest_sum
haftmann@44945
   323
  val mk_coeff = mk_fcoeff
haftmann@44945
   324
  val dest_coeff = dest_fcoeff 1
haftmann@44945
   325
  val left_distrib = @{thm combine_common_factor} RS trans
haftmann@44945
   326
  val prove_conv = Arith_Data.prove_conv_nohyps
haftmann@44947
   327
  val trans_tac = trans_tac
haftmann@31068
   328
haftmann@31068
   329
  val norm_ss1a = norm_ss1 addsimps inverse_1s @ divide_simps
haftmann@31068
   330
  fun norm_tac ss =
haftmann@31068
   331
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1a))
haftmann@31068
   332
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
haftmann@31068
   333
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
haftmann@31068
   334
huffman@45668
   335
  val numeral_simp_ss = HOL_basic_ss addsimps add_0s @ simps @ [@{thm add_frac_eq}, @{thm not_False_eq_True}]
haftmann@31068
   336
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
huffman@45437
   337
  val simplify_meta_eq = Arith_Data.simplify_meta_eq field_post_simps
haftmann@44945
   338
end;
haftmann@31068
   339
haftmann@31068
   340
structure FieldCombineNumerals = CombineNumeralsFun(FieldCombineNumeralsData);
haftmann@31068
   341
huffman@45284
   342
fun combine_numerals ss ct = CombineNumerals.proc ss (term_of ct)
haftmann@31068
   343
huffman@45284
   344
fun field_combine_numerals ss ct = FieldCombineNumerals.proc ss (term_of ct)
haftmann@31068
   345
haftmann@31068
   346
(** Constant folding for multiplication in semirings **)
haftmann@31068
   347
haftmann@31068
   348
(*We do not need folding for addition: combine_numerals does the same thing*)
haftmann@31068
   349
haftmann@31068
   350
structure Semiring_Times_Assoc_Data : ASSOC_FOLD_DATA =
haftmann@31068
   351
struct
huffman@45668
   352
  val assoc_ss = HOL_basic_ss addsimps @{thms mult_ac}
haftmann@31068
   353
  val eq_reflection = eq_reflection
boehmes@35983
   354
  val is_numeral = can HOLogic.dest_number
haftmann@31068
   355
end;
haftmann@31068
   356
haftmann@31068
   357
structure Semiring_Times_Assoc = Assoc_Fold (Semiring_Times_Assoc_Data);
haftmann@31068
   358
huffman@45284
   359
fun assoc_fold ss ct = Semiring_Times_Assoc.proc ss (term_of ct)
wenzelm@23164
   360
wenzelm@23164
   361
structure CancelNumeralFactorCommon =
haftmann@44945
   362
struct
haftmann@44945
   363
  val mk_coeff = mk_coeff
haftmann@44945
   364
  val dest_coeff = dest_coeff 1
haftmann@44947
   365
  val trans_tac = trans_tac
wenzelm@23164
   366
huffman@44983
   367
  val norm_ss1 = HOL_basic_ss addsimps minus_from_mult_simps @ mult_1s
huffman@44983
   368
  val norm_ss2 = HOL_basic_ss addsimps simps @ mult_minus_simps
huffman@44983
   369
  val norm_ss3 = HOL_basic_ss addsimps @{thms mult_ac}
wenzelm@23164
   370
  fun norm_tac ss =
wenzelm@23164
   371
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   372
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   373
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss3))
wenzelm@23164
   374
huffman@45668
   375
  (* simp_thms are necessary because some of the cancellation rules below
huffman@45668
   376
  (e.g. mult_less_cancel_left) introduce various logical connectives *)
huffman@47108
   377
  val numeral_simp_ss = HOL_basic_ss addsimps simps @ @{thms simp_thms}
wenzelm@23164
   378
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
haftmann@30518
   379
  val simplify_meta_eq = Arith_Data.simplify_meta_eq
huffman@45437
   380
    ([@{thm Nat.add_0}, @{thm Nat.add_0_right}] @ post_simps)
haftmann@44945
   381
  val prove_conv = Arith_Data.prove_conv
haftmann@44945
   382
end
wenzelm@23164
   383
haftmann@30931
   384
(*Version for semiring_div*)
haftmann@30931
   385
structure DivCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   386
 (open CancelNumeralFactorCommon
wenzelm@23164
   387
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
wenzelm@49323
   388
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} dummyT
haftmann@30931
   389
  val cancel = @{thm div_mult_mult1} RS trans
wenzelm@23164
   390
  val neg_exchanges = false
wenzelm@23164
   391
)
wenzelm@23164
   392
wenzelm@23164
   393
(*Version for fields*)
wenzelm@23164
   394
structure DivideCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   395
 (open CancelNumeralFactorCommon
huffman@44064
   396
  val mk_bal   = HOLogic.mk_binop @{const_name Fields.divide}
wenzelm@49323
   397
  val dest_bal = HOLogic.dest_bin @{const_name Fields.divide} dummyT
nipkow@23413
   398
  val cancel = @{thm mult_divide_mult_cancel_left} RS trans
wenzelm@23164
   399
  val neg_exchanges = false
wenzelm@23164
   400
)
wenzelm@23164
   401
wenzelm@23164
   402
structure EqCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   403
 (open CancelNumeralFactorCommon
wenzelm@23164
   404
  val mk_bal   = HOLogic.mk_eq
wenzelm@49323
   405
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
wenzelm@23164
   406
  val cancel = @{thm mult_cancel_left} RS trans
wenzelm@23164
   407
  val neg_exchanges = false
wenzelm@23164
   408
)
wenzelm@23164
   409
wenzelm@23164
   410
structure LessCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   411
 (open CancelNumeralFactorCommon
haftmann@35092
   412
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
wenzelm@49323
   413
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
wenzelm@23164
   414
  val cancel = @{thm mult_less_cancel_left} RS trans
wenzelm@23164
   415
  val neg_exchanges = true
wenzelm@23164
   416
)
wenzelm@23164
   417
wenzelm@23164
   418
structure LeCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   419
 (open CancelNumeralFactorCommon
haftmann@35092
   420
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
wenzelm@49323
   421
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
wenzelm@23164
   422
  val cancel = @{thm mult_le_cancel_left} RS trans
wenzelm@23164
   423
  val neg_exchanges = true
wenzelm@23164
   424
)
wenzelm@23164
   425
huffman@45284
   426
fun eq_cancel_numeral_factor ss ct = EqCancelNumeralFactor.proc ss (term_of ct)
huffman@45284
   427
fun less_cancel_numeral_factor ss ct = LessCancelNumeralFactor.proc ss (term_of ct)
huffman@45284
   428
fun le_cancel_numeral_factor ss ct = LeCancelNumeralFactor.proc ss (term_of ct)
huffman@45284
   429
fun div_cancel_numeral_factor ss ct = DivCancelNumeralFactor.proc ss (term_of ct)
huffman@45284
   430
fun divide_cancel_numeral_factor ss ct = DivideCancelNumeralFactor.proc ss (term_of ct)
wenzelm@23164
   431
wenzelm@23164
   432
val field_cancel_numeral_factors =
haftmann@44945
   433
  map (prep_simproc @{theory})
wenzelm@23164
   434
   [("field_eq_cancel_numeral_factor",
huffman@47108
   435
     ["(l::'a::field) * m = n",
huffman@47108
   436
      "(l::'a::field) = m * n"],
wenzelm@23164
   437
     K EqCancelNumeralFactor.proc),
wenzelm@23164
   438
    ("field_cancel_numeral_factor",
huffman@47108
   439
     ["((l::'a::field_inverse_zero) * m) / n",
huffman@47108
   440
      "(l::'a::field_inverse_zero) / (m * n)",
huffman@47108
   441
      "((numeral v)::'a::field_inverse_zero) / (numeral w)",
huffman@47108
   442
      "((numeral v)::'a::field_inverse_zero) / (neg_numeral w)",
huffman@47108
   443
      "((neg_numeral v)::'a::field_inverse_zero) / (numeral w)",
huffman@47108
   444
      "((neg_numeral v)::'a::field_inverse_zero) / (neg_numeral w)"],
wenzelm@23164
   445
     K DivideCancelNumeralFactor.proc)]
wenzelm@23164
   446
wenzelm@23164
   447
wenzelm@23164
   448
(** Declarations for ExtractCommonTerm **)
wenzelm@23164
   449
wenzelm@23164
   450
(*Find first term that matches u*)
wenzelm@23164
   451
fun find_first_t past u []         = raise TERM ("find_first_t", [])
wenzelm@23164
   452
  | find_first_t past u (t::terms) =
wenzelm@23164
   453
        if u aconv t then (rev past @ terms)
wenzelm@23164
   454
        else find_first_t (t::past) u terms
wenzelm@23164
   455
        handle TERM _ => find_first_t (t::past) u terms;
wenzelm@23164
   456
wenzelm@23164
   457
(** Final simplification for the CancelFactor simprocs **)
haftmann@30518
   458
val simplify_one = Arith_Data.simplify_meta_eq  
nipkow@30031
   459
  [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_1_eq_1}];
wenzelm@23164
   460
nipkow@30649
   461
fun cancel_simplify_meta_eq ss cancel_th th =
wenzelm@23164
   462
    simplify_one ss (([th, cancel_th]) MRS trans);
wenzelm@23164
   463
nipkow@30649
   464
local
haftmann@31067
   465
  val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory HOL} HOLogic.Trueprop)
nipkow@30649
   466
  fun Eq_True_elim Eq = 
nipkow@30649
   467
    Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
nipkow@30649
   468
in
nipkow@30649
   469
fun sign_conv pos_th neg_th ss t =
nipkow@30649
   470
  let val T = fastype_of t;
haftmann@35267
   471
      val zero = Const(@{const_name Groups.zero}, T);
haftmann@35092
   472
      val less = Const(@{const_name Orderings.less}, [T,T] ---> HOLogic.boolT);
nipkow@30649
   473
      val pos = less $ zero $ t and neg = less $ t $ zero
huffman@46240
   474
      val thy = Proof_Context.theory_of (Simplifier.the_context ss)
nipkow@30649
   475
      fun prove p =
huffman@46240
   476
        SOME (Eq_True_elim (Simplifier.asm_rewrite ss (Thm.cterm_of thy p)))
nipkow@30649
   477
        handle THM _ => NONE
nipkow@30649
   478
    in case prove pos of
nipkow@30649
   479
         SOME th => SOME(th RS pos_th)
nipkow@30649
   480
       | NONE => (case prove neg of
nipkow@30649
   481
                    SOME th => SOME(th RS neg_th)
nipkow@30649
   482
                  | NONE => NONE)
nipkow@30649
   483
    end;
nipkow@30649
   484
end
nipkow@30649
   485
wenzelm@23164
   486
structure CancelFactorCommon =
haftmann@44945
   487
struct
haftmann@44945
   488
  val mk_sum = long_mk_prod
haftmann@44945
   489
  val dest_sum = dest_prod
haftmann@44945
   490
  val mk_coeff = mk_coeff
haftmann@44945
   491
  val dest_coeff = dest_coeff
haftmann@44945
   492
  val find_first = find_first_t []
haftmann@44947
   493
  val trans_tac = trans_tac
huffman@45668
   494
  val norm_ss = HOL_basic_ss addsimps mult_1s @ @{thms mult_ac}
wenzelm@23164
   495
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
nipkow@30649
   496
  val simplify_meta_eq  = cancel_simplify_meta_eq 
huffman@45270
   497
  fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
haftmann@44945
   498
end;
wenzelm@23164
   499
wenzelm@23164
   500
(*mult_cancel_left requires a ring with no zero divisors.*)
wenzelm@23164
   501
structure EqCancelFactor = ExtractCommonTermFun
wenzelm@23164
   502
 (open CancelFactorCommon
wenzelm@23164
   503
  val mk_bal   = HOLogic.mk_eq
wenzelm@49323
   504
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
wenzelm@31368
   505
  fun simp_conv _ _ = SOME @{thm mult_cancel_left}
nipkow@30649
   506
);
nipkow@30649
   507
nipkow@30649
   508
(*for ordered rings*)
nipkow@30649
   509
structure LeCancelFactor = ExtractCommonTermFun
nipkow@30649
   510
 (open CancelFactorCommon
haftmann@35092
   511
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
wenzelm@49323
   512
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
nipkow@30649
   513
  val simp_conv = sign_conv
nipkow@30649
   514
    @{thm mult_le_cancel_left_pos} @{thm mult_le_cancel_left_neg}
nipkow@30649
   515
);
nipkow@30649
   516
nipkow@30649
   517
(*for ordered rings*)
nipkow@30649
   518
structure LessCancelFactor = ExtractCommonTermFun
nipkow@30649
   519
 (open CancelFactorCommon
haftmann@35092
   520
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
wenzelm@49323
   521
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
nipkow@30649
   522
  val simp_conv = sign_conv
nipkow@30649
   523
    @{thm mult_less_cancel_left_pos} @{thm mult_less_cancel_left_neg}
wenzelm@23164
   524
);
wenzelm@23164
   525
haftmann@30931
   526
(*for semirings with division*)
haftmann@30931
   527
structure DivCancelFactor = ExtractCommonTermFun
wenzelm@23164
   528
 (open CancelFactorCommon
wenzelm@23164
   529
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
wenzelm@49323
   530
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} dummyT
wenzelm@31368
   531
  fun simp_conv _ _ = SOME @{thm div_mult_mult1_if}
wenzelm@23164
   532
);
wenzelm@23164
   533
haftmann@30931
   534
structure ModCancelFactor = ExtractCommonTermFun
nipkow@24395
   535
 (open CancelFactorCommon
nipkow@24395
   536
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.mod}
wenzelm@49323
   537
  val dest_bal = HOLogic.dest_bin @{const_name Divides.mod} dummyT
wenzelm@31368
   538
  fun simp_conv _ _ = SOME @{thm mod_mult_mult1}
nipkow@24395
   539
);
nipkow@24395
   540
haftmann@30931
   541
(*for idoms*)
haftmann@30931
   542
structure DvdCancelFactor = ExtractCommonTermFun
nipkow@23969
   543
 (open CancelFactorCommon
haftmann@35050
   544
  val mk_bal   = HOLogic.mk_binrel @{const_name Rings.dvd}
wenzelm@49323
   545
  val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} dummyT
wenzelm@31368
   546
  fun simp_conv _ _ = SOME @{thm dvd_mult_cancel_left}
nipkow@23969
   547
);
nipkow@23969
   548
wenzelm@23164
   549
(*Version for all fields, including unordered ones (type complex).*)
wenzelm@23164
   550
structure DivideCancelFactor = ExtractCommonTermFun
wenzelm@23164
   551
 (open CancelFactorCommon
huffman@44064
   552
  val mk_bal   = HOLogic.mk_binop @{const_name Fields.divide}
wenzelm@49323
   553
  val dest_bal = HOLogic.dest_bin @{const_name Fields.divide} dummyT
wenzelm@31368
   554
  fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
wenzelm@23164
   555
);
wenzelm@23164
   556
huffman@45284
   557
fun eq_cancel_factor ss ct = EqCancelFactor.proc ss (term_of ct)
huffman@45284
   558
fun le_cancel_factor ss ct = LeCancelFactor.proc ss (term_of ct)
huffman@45284
   559
fun less_cancel_factor ss ct = LessCancelFactor.proc ss (term_of ct)
huffman@45284
   560
fun div_cancel_factor ss ct = DivCancelFactor.proc ss (term_of ct)
huffman@45284
   561
fun mod_cancel_factor ss ct = ModCancelFactor.proc ss (term_of ct)
huffman@45284
   562
fun dvd_cancel_factor ss ct = DvdCancelFactor.proc ss (term_of ct)
huffman@45284
   563
fun divide_cancel_factor ss ct = DivideCancelFactor.proc ss (term_of ct)
wenzelm@23164
   564
haftmann@36751
   565
local
haftmann@36751
   566
 val zr = @{cpat "0"}
haftmann@36751
   567
 val zT = ctyp_of_term zr
haftmann@38864
   568
 val geq = @{cpat HOL.eq}
haftmann@36751
   569
 val eqT = Thm.dest_ctyp (ctyp_of_term geq) |> hd
haftmann@36751
   570
 val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
haftmann@36751
   571
 val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
haftmann@36751
   572
 val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
haftmann@36751
   573
haftmann@36751
   574
 fun prove_nz ss T t =
haftmann@36751
   575
    let
wenzelm@36945
   576
      val z = Thm.instantiate_cterm ([(zT,T)],[]) zr
wenzelm@36945
   577
      val eq = Thm.instantiate_cterm ([(eqT,T)],[]) geq
haftmann@36751
   578
      val th = Simplifier.rewrite (ss addsimps @{thms simp_thms})
wenzelm@46497
   579
           (Thm.apply @{cterm "Trueprop"} (Thm.apply @{cterm "Not"}
wenzelm@46497
   580
                  (Thm.apply (Thm.apply eq t) z)))
wenzelm@36945
   581
    in Thm.equal_elim (Thm.symmetric th) TrueI
haftmann@36751
   582
    end
haftmann@36751
   583
haftmann@36751
   584
 fun proc phi ss ct =
haftmann@36751
   585
  let
haftmann@36751
   586
    val ((x,y),(w,z)) =
haftmann@36751
   587
         (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct
haftmann@36751
   588
    val _ = map (HOLogic.dest_number o term_of) [x,y,z,w]
haftmann@36751
   589
    val T = ctyp_of_term x
haftmann@36751
   590
    val [y_nz, z_nz] = map (prove_nz ss T) [y, z]
haftmann@36751
   591
    val th = instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq
wenzelm@36945
   592
  in SOME (Thm.implies_elim (Thm.implies_elim th y_nz) z_nz)
haftmann@36751
   593
  end
haftmann@36751
   594
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36751
   595
haftmann@36751
   596
 fun proc2 phi ss ct =
haftmann@36751
   597
  let
haftmann@36751
   598
    val (l,r) = Thm.dest_binop ct
haftmann@36751
   599
    val T = ctyp_of_term l
haftmann@36751
   600
  in (case (term_of l, term_of r) of
huffman@44064
   601
      (Const(@{const_name Fields.divide},_)$_$_, _) =>
haftmann@36751
   602
        let val (x,y) = Thm.dest_binop l val z = r
haftmann@36751
   603
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
haftmann@36751
   604
            val ynz = prove_nz ss T y
wenzelm@36945
   605
        in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz)
haftmann@36751
   606
        end
huffman@44064
   607
     | (_, Const (@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   608
        let val (x,y) = Thm.dest_binop r val z = l
haftmann@36751
   609
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
haftmann@36751
   610
            val ynz = prove_nz ss T y
wenzelm@36945
   611
        in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz)
haftmann@36751
   612
        end
haftmann@36751
   613
     | _ => NONE)
haftmann@36751
   614
  end
haftmann@36751
   615
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36751
   616
huffman@44064
   617
 fun is_number (Const(@{const_name Fields.divide},_)$a$b) = is_number a andalso is_number b
haftmann@36751
   618
   | is_number t = can HOLogic.dest_number t
haftmann@36751
   619
haftmann@36751
   620
 val is_number = is_number o term_of
haftmann@36751
   621
haftmann@36751
   622
 fun proc3 phi ss ct =
haftmann@36751
   623
  (case term_of ct of
huffman@44064
   624
    Const(@{const_name Orderings.less},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   625
      let
haftmann@36751
   626
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   627
        val _ = map is_number [a,b,c]
haftmann@36751
   628
        val T = ctyp_of_term c
haftmann@36751
   629
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_less_eq"}
haftmann@36751
   630
      in SOME (mk_meta_eq th) end
huffman@44064
   631
  | Const(@{const_name Orderings.less_eq},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   632
      let
haftmann@36751
   633
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   634
        val _ = map is_number [a,b,c]
haftmann@36751
   635
        val T = ctyp_of_term c
haftmann@36751
   636
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_le_eq"}
haftmann@36751
   637
      in SOME (mk_meta_eq th) end
huffman@44064
   638
  | Const(@{const_name HOL.eq},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   639
      let
haftmann@36751
   640
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   641
        val _ = map is_number [a,b,c]
haftmann@36751
   642
        val T = ctyp_of_term c
haftmann@36751
   643
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_eq_eq"}
haftmann@36751
   644
      in SOME (mk_meta_eq th) end
huffman@44064
   645
  | Const(@{const_name Orderings.less},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   646
    let
haftmann@36751
   647
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   648
        val _ = map is_number [a,b,c]
haftmann@36751
   649
        val T = ctyp_of_term c
haftmann@36751
   650
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "less_divide_eq"}
haftmann@36751
   651
      in SOME (mk_meta_eq th) end
huffman@44064
   652
  | Const(@{const_name Orderings.less_eq},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   653
    let
haftmann@36751
   654
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   655
        val _ = map is_number [a,b,c]
haftmann@36751
   656
        val T = ctyp_of_term c
haftmann@36751
   657
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "le_divide_eq"}
haftmann@36751
   658
      in SOME (mk_meta_eq th) end
huffman@44064
   659
  | Const(@{const_name HOL.eq},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   660
    let
haftmann@36751
   661
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   662
        val _ = map is_number [a,b,c]
haftmann@36751
   663
        val T = ctyp_of_term c
haftmann@36751
   664
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "eq_divide_eq"}
haftmann@36751
   665
      in SOME (mk_meta_eq th) end
haftmann@36751
   666
  | _ => NONE)
haftmann@36751
   667
  handle TERM _ => NONE | CTERM _ => NONE | THM _ => NONE
haftmann@36751
   668
haftmann@36751
   669
val add_frac_frac_simproc =
haftmann@36751
   670
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + (?w::?'a::field)/?z"}],
haftmann@36751
   671
                     name = "add_frac_frac_simproc",
haftmann@36751
   672
                     proc = proc, identifier = []}
haftmann@36751
   673
haftmann@36751
   674
val add_frac_num_simproc =
haftmann@36751
   675
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + ?z"}, @{cpat "?z + (?x::?'a::field)/?y"}],
haftmann@36751
   676
                     name = "add_frac_num_simproc",
haftmann@36751
   677
                     proc = proc2, identifier = []}
haftmann@36751
   678
haftmann@36751
   679
val ord_frac_simproc =
haftmann@36751
   680
  make_simproc
haftmann@36751
   681
    {lhss = [@{cpat "(?a::(?'a::{field, ord}))/?b < ?c"},
haftmann@36751
   682
             @{cpat "(?a::(?'a::{field, ord}))/?b <= ?c"},
haftmann@36751
   683
             @{cpat "?c < (?a::(?'a::{field, ord}))/?b"},
haftmann@36751
   684
             @{cpat "?c <= (?a::(?'a::{field, ord}))/?b"},
haftmann@36751
   685
             @{cpat "?c = ((?a::(?'a::{field, ord}))/?b)"},
haftmann@36751
   686
             @{cpat "((?a::(?'a::{field, ord}))/ ?b) = ?c"}],
haftmann@36751
   687
             name = "ord_frac_simproc", proc = proc3, identifier = []}
haftmann@36751
   688
haftmann@36751
   689
val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"},
haftmann@36751
   690
           @{thm "divide_Numeral1"},
huffman@47108
   691
           @{thm "divide_zero"}, @{thm divide_zero_left},
haftmann@36751
   692
           @{thm "divide_divide_eq_left"}, 
haftmann@36751
   693
           @{thm "times_divide_eq_left"}, @{thm "times_divide_eq_right"},
haftmann@36751
   694
           @{thm "times_divide_times_eq"},
haftmann@36751
   695
           @{thm "divide_divide_eq_right"},
haftmann@37887
   696
           @{thm "diff_minus"}, @{thm "minus_divide_left"},
huffman@47108
   697
           @{thm "add_divide_distrib"} RS sym,
haftmann@36751
   698
           @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
haftmann@36751
   699
           Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult_commute}))))   
haftmann@36751
   700
           (@{thm field_divide_inverse} RS sym)]
haftmann@36751
   701
haftmann@36751
   702
in
haftmann@36751
   703
wenzelm@45620
   704
val field_comp_conv =
wenzelm@45620
   705
  Simplifier.rewrite
wenzelm@45620
   706
    (HOL_basic_ss addsimps @{thms "semiring_norm"}
wenzelm@45620
   707
      addsimps ths addsimps @{thms simp_thms}
wenzelm@45620
   708
      addsimprocs field_cancel_numeral_factors
wenzelm@45620
   709
      addsimprocs [add_frac_frac_simproc, add_frac_num_simproc, ord_frac_simproc]
wenzelm@45620
   710
      |> Simplifier.add_cong @{thm "if_weak_cong"})
wenzelm@45620
   711
  then_conv
huffman@47108
   712
  Simplifier.rewrite (HOL_basic_ss addsimps [@{thm numeral_1_eq_1}])
haftmann@36751
   713
haftmann@36751
   714
end
haftmann@36751
   715
wenzelm@23164
   716
end;
wenzelm@23164
   717
haftmann@31068
   718
(*examples:
haftmann@31068
   719
print_depth 22;
haftmann@31068
   720
set timing;
wenzelm@40878
   721
set simp_trace;
haftmann@31068
   722
fun test s = (Goal s, by (Simp_tac 1));
haftmann@31068
   723
haftmann@31068
   724
test "l + 2 + 2 + 2 + (l + 2) + (oo + 2) = (uu::int)";
haftmann@31068
   725
haftmann@31068
   726
test "2*u = (u::int)";
haftmann@31068
   727
test "(i + j + 12 + (k::int)) - 15 = y";
haftmann@31068
   728
test "(i + j + 12 + (k::int)) - 5 = y";
haftmann@31068
   729
haftmann@31068
   730
test "y - b < (b::int)";
haftmann@31068
   731
test "y - (3*b + c) < (b::int) - 2*c";
haftmann@31068
   732
haftmann@31068
   733
test "(2*x - (u*v) + y) - v*3*u = (w::int)";
haftmann@31068
   734
test "(2*x*u*v + (u*v)*4 + y) - v*u*4 = (w::int)";
haftmann@31068
   735
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::int)";
haftmann@31068
   736
test "u*v - (x*u*v + (u*v)*4 + y) = (w::int)";
haftmann@31068
   737
haftmann@31068
   738
test "(i + j + 12 + (k::int)) = u + 15 + y";
haftmann@31068
   739
test "(i + j*2 + 12 + (k::int)) = j + 5 + y";
haftmann@31068
   740
haftmann@31068
   741
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
   742
haftmann@31068
   743
test "a + -(b+c) + b = (d::int)";
haftmann@31068
   744
test "a + -(b+c) - b = (d::int)";
haftmann@31068
   745
haftmann@31068
   746
(*negative numerals*)
haftmann@31068
   747
test "(i + j + -2 + (k::int)) - (u + 5 + y) = zz";
haftmann@31068
   748
test "(i + j + -3 + (k::int)) < u + 5 + y";
haftmann@31068
   749
test "(i + j + 3 + (k::int)) < u + -6 + y";
haftmann@31068
   750
test "(i + j + -12 + (k::int)) - 15 = y";
haftmann@31068
   751
test "(i + j + 12 + (k::int)) - -15 = y";
haftmann@31068
   752
test "(i + j + -12 + (k::int)) - -15 = y";
haftmann@31068
   753
*)
haftmann@31068
   754
haftmann@31068
   755
(*examples:
haftmann@31068
   756
print_depth 22;
haftmann@31068
   757
set timing;
wenzelm@40878
   758
set simp_trace;
haftmann@31068
   759
fun test s = (Goal s; by (Simp_tac 1));
haftmann@31068
   760
haftmann@31068
   761
test "9*x = 12 * (y::int)";
haftmann@31068
   762
test "(9*x) div (12 * (y::int)) = z";
haftmann@31068
   763
test "9*x < 12 * (y::int)";
haftmann@31068
   764
test "9*x <= 12 * (y::int)";
haftmann@31068
   765
haftmann@31068
   766
test "-99*x = 132 * (y::int)";
haftmann@31068
   767
test "(-99*x) div (132 * (y::int)) = z";
haftmann@31068
   768
test "-99*x < 132 * (y::int)";
haftmann@31068
   769
test "-99*x <= 132 * (y::int)";
haftmann@31068
   770
haftmann@31068
   771
test "999*x = -396 * (y::int)";
haftmann@31068
   772
test "(999*x) div (-396 * (y::int)) = z";
haftmann@31068
   773
test "999*x < -396 * (y::int)";
haftmann@31068
   774
test "999*x <= -396 * (y::int)";
haftmann@31068
   775
haftmann@31068
   776
test "-99*x = -81 * (y::int)";
haftmann@31068
   777
test "(-99*x) div (-81 * (y::int)) = z";
haftmann@31068
   778
test "-99*x <= -81 * (y::int)";
haftmann@31068
   779
test "-99*x < -81 * (y::int)";
haftmann@31068
   780
haftmann@31068
   781
test "-2 * x = -1 * (y::int)";
haftmann@31068
   782
test "-2 * x = -(y::int)";
haftmann@31068
   783
test "(-2 * x) div (-1 * (y::int)) = z";
haftmann@31068
   784
test "-2 * x < -(y::int)";
haftmann@31068
   785
test "-2 * x <= -1 * (y::int)";
haftmann@31068
   786
test "-x < -23 * (y::int)";
haftmann@31068
   787
test "-x <= -23 * (y::int)";
haftmann@31068
   788
*)
haftmann@31068
   789
haftmann@31068
   790
(*And the same examples for fields such as rat or real:
haftmann@31068
   791
test "0 <= (y::rat) * -2";
haftmann@31068
   792
test "9*x = 12 * (y::rat)";
haftmann@31068
   793
test "(9*x) / (12 * (y::rat)) = z";
haftmann@31068
   794
test "9*x < 12 * (y::rat)";
haftmann@31068
   795
test "9*x <= 12 * (y::rat)";
haftmann@31068
   796
haftmann@31068
   797
test "-99*x = 132 * (y::rat)";
haftmann@31068
   798
test "(-99*x) / (132 * (y::rat)) = z";
haftmann@31068
   799
test "-99*x < 132 * (y::rat)";
haftmann@31068
   800
test "-99*x <= 132 * (y::rat)";
haftmann@31068
   801
haftmann@31068
   802
test "999*x = -396 * (y::rat)";
haftmann@31068
   803
test "(999*x) / (-396 * (y::rat)) = z";
haftmann@31068
   804
test "999*x < -396 * (y::rat)";
haftmann@31068
   805
test "999*x <= -396 * (y::rat)";
haftmann@31068
   806
haftmann@31068
   807
test  "(- ((2::rat) * x) <= 2 * y)";
haftmann@31068
   808
test "-99*x = -81 * (y::rat)";
haftmann@31068
   809
test "(-99*x) / (-81 * (y::rat)) = z";
haftmann@31068
   810
test "-99*x <= -81 * (y::rat)";
haftmann@31068
   811
test "-99*x < -81 * (y::rat)";
haftmann@31068
   812
haftmann@31068
   813
test "-2 * x = -1 * (y::rat)";
haftmann@31068
   814
test "-2 * x = -(y::rat)";
haftmann@31068
   815
test "(-2 * x) / (-1 * (y::rat)) = z";
haftmann@31068
   816
test "-2 * x < -(y::rat)";
haftmann@31068
   817
test "-2 * x <= -1 * (y::rat)";
haftmann@31068
   818
test "-x < -23 * (y::rat)";
haftmann@31068
   819
test "-x <= -23 * (y::rat)";
haftmann@31068
   820
*)
haftmann@31068
   821
wenzelm@23164
   822
(*examples:
wenzelm@23164
   823
print_depth 22;
wenzelm@23164
   824
set timing;
wenzelm@40878
   825
set simp_trace;
wenzelm@23164
   826
fun test s = (Goal s; by (Asm_simp_tac 1));
wenzelm@23164
   827
wenzelm@23164
   828
test "x*k = k*(y::int)";
wenzelm@23164
   829
test "k = k*(y::int)";
wenzelm@23164
   830
test "a*(b*c) = (b::int)";
wenzelm@23164
   831
test "a*(b*c) = d*(b::int)*(x*a)";
wenzelm@23164
   832
wenzelm@23164
   833
test "(x*k) div (k*(y::int)) = (uu::int)";
wenzelm@23164
   834
test "(k) div (k*(y::int)) = (uu::int)";
wenzelm@23164
   835
test "(a*(b*c)) div ((b::int)) = (uu::int)";
wenzelm@23164
   836
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
wenzelm@23164
   837
*)
wenzelm@23164
   838
wenzelm@23164
   839
(*And the same examples for fields such as rat or real:
wenzelm@23164
   840
print_depth 22;
wenzelm@23164
   841
set timing;
wenzelm@40878
   842
set simp_trace;
wenzelm@23164
   843
fun test s = (Goal s; by (Asm_simp_tac 1));
wenzelm@23164
   844
wenzelm@23164
   845
test "x*k = k*(y::rat)";
wenzelm@23164
   846
test "k = k*(y::rat)";
wenzelm@23164
   847
test "a*(b*c) = (b::rat)";
wenzelm@23164
   848
test "a*(b*c) = d*(b::rat)*(x*a)";
wenzelm@23164
   849
wenzelm@23164
   850
wenzelm@23164
   851
test "(x*k) / (k*(y::rat)) = (uu::rat)";
wenzelm@23164
   852
test "(k) / (k*(y::rat)) = (uu::rat)";
wenzelm@23164
   853
test "(a*(b*c)) / ((b::rat)) = (uu::rat)";
wenzelm@23164
   854
test "(a*(b*c)) / (d*(b::rat)*(x*a)) = (uu::rat)";
wenzelm@23164
   855
wenzelm@23164
   856
(*FIXME: what do we do about this?*)
wenzelm@23164
   857
test "a*(b*c)/(y*z) = d*(b::rat)*(x*a)/z";
wenzelm@23164
   858
*)