src/HOL/Tools/numeral_simprocs.ML
author haftmann
Thu Oct 02 11:33:06 2014 +0200 (2014-10-02)
changeset 58512 dc4d76dfa8f0
parent 57514 bdc2c6b40bf2
child 59530 2a20354c0877
permissions -rw-r--r--
moved lemmas out of Int.thy which have nothing to do with int
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
nipkow@57136
    40
  val field_divide_cancel_numeral_factor: 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@57512
   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 @
haftmann@57514
   235
    diff_simps @ minus_simps @ @{thms ac_simps})
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@57514
   243
    addsimps minus_from_mult_simps @ @{thms ac_simps} @ @{thms ac_simps 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@57514
   364
  val assoc_ss = simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms ac_simps 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@57514
   384
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms ac_simps 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
nipkow@57136
   449
val field_divide_cancel_numeral_factor =
nipkow@57136
   450
  [prep_simproc @{theory}
nipkow@57136
   451
    ("field_divide_cancel_numeral_factor",
huffman@47108
   452
     ["((l::'a::field_inverse_zero) * m) / n",
huffman@47108
   453
      "(l::'a::field_inverse_zero) / (m * n)",
huffman@47108
   454
      "((numeral v)::'a::field_inverse_zero) / (numeral w)",
haftmann@54489
   455
      "((numeral v)::'a::field_inverse_zero) / (- numeral w)",
haftmann@54489
   456
      "((- numeral v)::'a::field_inverse_zero) / (numeral w)",
haftmann@54489
   457
      "((- numeral v)::'a::field_inverse_zero) / (- numeral w)"],
nipkow@57136
   458
     DivideCancelNumeralFactor.proc)];
nipkow@57136
   459
nipkow@57136
   460
val field_cancel_numeral_factors =
nipkow@57136
   461
  prep_simproc @{theory}
nipkow@57136
   462
    ("field_eq_cancel_numeral_factor",
nipkow@57136
   463
     ["(l::'a::field) * m = n",
nipkow@57136
   464
      "(l::'a::field) = m * n"],
nipkow@57136
   465
     EqCancelNumeralFactor.proc)
nipkow@57136
   466
   :: field_divide_cancel_numeral_factor;
wenzelm@23164
   467
wenzelm@23164
   468
wenzelm@23164
   469
(** Declarations for ExtractCommonTerm **)
wenzelm@23164
   470
wenzelm@23164
   471
(*Find first term that matches u*)
wenzelm@23164
   472
fun find_first_t past u []         = raise TERM ("find_first_t", [])
wenzelm@23164
   473
  | find_first_t past u (t::terms) =
wenzelm@23164
   474
        if u aconv t then (rev past @ terms)
wenzelm@23164
   475
        else find_first_t (t::past) u terms
wenzelm@23164
   476
        handle TERM _ => find_first_t (t::past) u terms;
wenzelm@23164
   477
wenzelm@23164
   478
(** Final simplification for the CancelFactor simprocs **)
haftmann@30518
   479
val simplify_one = Arith_Data.simplify_meta_eq  
nipkow@30031
   480
  [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_by_1}, @{thm numeral_1_eq_1}];
wenzelm@23164
   481
wenzelm@51717
   482
fun cancel_simplify_meta_eq ctxt cancel_th th =
wenzelm@51717
   483
    simplify_one ctxt (([th, cancel_th]) MRS trans);
wenzelm@23164
   484
nipkow@30649
   485
local
haftmann@31067
   486
  val Tp_Eq = Thm.reflexive (Thm.cterm_of @{theory HOL} HOLogic.Trueprop)
nipkow@30649
   487
  fun Eq_True_elim Eq = 
nipkow@30649
   488
    Thm.equal_elim (Thm.combination Tp_Eq (Thm.symmetric Eq)) @{thm TrueI}
nipkow@30649
   489
in
wenzelm@51717
   490
fun sign_conv pos_th neg_th ctxt t =
nipkow@30649
   491
  let val T = fastype_of t;
haftmann@35267
   492
      val zero = Const(@{const_name Groups.zero}, T);
haftmann@35092
   493
      val less = Const(@{const_name Orderings.less}, [T,T] ---> HOLogic.boolT);
nipkow@30649
   494
      val pos = less $ zero $ t and neg = less $ t $ zero
wenzelm@51717
   495
      val thy = Proof_Context.theory_of ctxt
nipkow@30649
   496
      fun prove p =
wenzelm@51717
   497
        SOME (Eq_True_elim (Simplifier.asm_rewrite ctxt (Thm.cterm_of thy p)))
nipkow@30649
   498
        handle THM _ => NONE
nipkow@30649
   499
    in case prove pos of
nipkow@30649
   500
         SOME th => SOME(th RS pos_th)
nipkow@30649
   501
       | NONE => (case prove neg of
nipkow@30649
   502
                    SOME th => SOME(th RS neg_th)
nipkow@30649
   503
                  | NONE => NONE)
nipkow@30649
   504
    end;
nipkow@30649
   505
end
nipkow@30649
   506
wenzelm@23164
   507
structure CancelFactorCommon =
haftmann@44945
   508
struct
haftmann@44945
   509
  val mk_sum = long_mk_prod
haftmann@44945
   510
  val dest_sum = dest_prod
haftmann@44945
   511
  val mk_coeff = mk_coeff
haftmann@44945
   512
  val dest_coeff = dest_coeff
haftmann@44945
   513
  val find_first = find_first_t []
haftmann@44947
   514
  val trans_tac = trans_tac
wenzelm@51717
   515
  val norm_ss =
haftmann@57514
   516
    simpset_of (put_simpset HOL_basic_ss @{context} addsimps mult_1s @ @{thms ac_simps minus_mult_commute})
wenzelm@51717
   517
  fun norm_tac ctxt =
wenzelm@51717
   518
    ALLGOALS (simp_tac (put_simpset norm_ss ctxt))
nipkow@30649
   519
  val simplify_meta_eq  = cancel_simplify_meta_eq 
huffman@45270
   520
  fun mk_eq (a, b) = HOLogic.mk_Trueprop (HOLogic.mk_eq (a, b))
haftmann@44945
   521
end;
wenzelm@23164
   522
wenzelm@23164
   523
(*mult_cancel_left requires a ring with no zero divisors.*)
wenzelm@23164
   524
structure EqCancelFactor = ExtractCommonTermFun
wenzelm@23164
   525
 (open CancelFactorCommon
wenzelm@23164
   526
  val mk_bal   = HOLogic.mk_eq
wenzelm@49323
   527
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} dummyT
wenzelm@31368
   528
  fun simp_conv _ _ = SOME @{thm mult_cancel_left}
nipkow@30649
   529
);
nipkow@30649
   530
nipkow@30649
   531
(*for ordered rings*)
nipkow@30649
   532
structure LeCancelFactor = ExtractCommonTermFun
nipkow@30649
   533
 (open CancelFactorCommon
haftmann@35092
   534
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less_eq}
wenzelm@49323
   535
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} dummyT
nipkow@30649
   536
  val simp_conv = sign_conv
nipkow@30649
   537
    @{thm mult_le_cancel_left_pos} @{thm mult_le_cancel_left_neg}
nipkow@30649
   538
);
nipkow@30649
   539
nipkow@30649
   540
(*for ordered rings*)
nipkow@30649
   541
structure LessCancelFactor = ExtractCommonTermFun
nipkow@30649
   542
 (open CancelFactorCommon
haftmann@35092
   543
  val mk_bal   = HOLogic.mk_binrel @{const_name Orderings.less}
wenzelm@49323
   544
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} dummyT
nipkow@30649
   545
  val simp_conv = sign_conv
nipkow@30649
   546
    @{thm mult_less_cancel_left_pos} @{thm mult_less_cancel_left_neg}
wenzelm@23164
   547
);
wenzelm@23164
   548
haftmann@30931
   549
(*for semirings with division*)
haftmann@30931
   550
structure DivCancelFactor = ExtractCommonTermFun
wenzelm@23164
   551
 (open CancelFactorCommon
wenzelm@23164
   552
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
wenzelm@49323
   553
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} dummyT
wenzelm@31368
   554
  fun simp_conv _ _ = SOME @{thm div_mult_mult1_if}
wenzelm@23164
   555
);
wenzelm@23164
   556
haftmann@30931
   557
structure ModCancelFactor = ExtractCommonTermFun
nipkow@24395
   558
 (open CancelFactorCommon
nipkow@24395
   559
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.mod}
wenzelm@49323
   560
  val dest_bal = HOLogic.dest_bin @{const_name Divides.mod} dummyT
wenzelm@31368
   561
  fun simp_conv _ _ = SOME @{thm mod_mult_mult1}
nipkow@24395
   562
);
nipkow@24395
   563
haftmann@30931
   564
(*for idoms*)
haftmann@30931
   565
structure DvdCancelFactor = ExtractCommonTermFun
nipkow@23969
   566
 (open CancelFactorCommon
haftmann@35050
   567
  val mk_bal   = HOLogic.mk_binrel @{const_name Rings.dvd}
wenzelm@49323
   568
  val dest_bal = HOLogic.dest_bin @{const_name Rings.dvd} dummyT
wenzelm@31368
   569
  fun simp_conv _ _ = SOME @{thm dvd_mult_cancel_left}
nipkow@23969
   570
);
nipkow@23969
   571
wenzelm@23164
   572
(*Version for all fields, including unordered ones (type complex).*)
wenzelm@23164
   573
structure DivideCancelFactor = ExtractCommonTermFun
wenzelm@23164
   574
 (open CancelFactorCommon
huffman@44064
   575
  val mk_bal   = HOLogic.mk_binop @{const_name Fields.divide}
wenzelm@49323
   576
  val dest_bal = HOLogic.dest_bin @{const_name Fields.divide} dummyT
wenzelm@31368
   577
  fun simp_conv _ _ = SOME @{thm mult_divide_mult_cancel_left_if}
wenzelm@23164
   578
);
wenzelm@23164
   579
wenzelm@51717
   580
fun eq_cancel_factor ctxt ct = EqCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   581
fun le_cancel_factor ctxt ct = LeCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   582
fun less_cancel_factor ctxt ct = LessCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   583
fun div_cancel_factor ctxt ct = DivCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   584
fun mod_cancel_factor ctxt ct = ModCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   585
fun dvd_cancel_factor ctxt ct = DvdCancelFactor.proc ctxt (term_of ct)
wenzelm@51717
   586
fun divide_cancel_factor ctxt ct = DivideCancelFactor.proc ctxt (term_of ct)
wenzelm@23164
   587
haftmann@36751
   588
local
haftmann@36751
   589
 val zr = @{cpat "0"}
haftmann@36751
   590
 val zT = ctyp_of_term zr
haftmann@38864
   591
 val geq = @{cpat HOL.eq}
haftmann@36751
   592
 val eqT = Thm.dest_ctyp (ctyp_of_term geq) |> hd
haftmann@36751
   593
 val add_frac_eq = mk_meta_eq @{thm "add_frac_eq"}
haftmann@36751
   594
 val add_frac_num = mk_meta_eq @{thm "add_frac_num"}
haftmann@36751
   595
 val add_num_frac = mk_meta_eq @{thm "add_num_frac"}
haftmann@36751
   596
wenzelm@51717
   597
 fun prove_nz ctxt T t =
haftmann@36751
   598
    let
wenzelm@36945
   599
      val z = Thm.instantiate_cterm ([(zT,T)],[]) zr
wenzelm@36945
   600
      val eq = Thm.instantiate_cterm ([(eqT,T)],[]) geq
wenzelm@51717
   601
      val th = Simplifier.rewrite (ctxt addsimps @{thms simp_thms})
wenzelm@46497
   602
           (Thm.apply @{cterm "Trueprop"} (Thm.apply @{cterm "Not"}
wenzelm@46497
   603
                  (Thm.apply (Thm.apply eq t) z)))
wenzelm@36945
   604
    in Thm.equal_elim (Thm.symmetric th) TrueI
haftmann@36751
   605
    end
haftmann@36751
   606
wenzelm@51717
   607
 fun proc phi ctxt ct =
haftmann@36751
   608
  let
haftmann@36751
   609
    val ((x,y),(w,z)) =
haftmann@36751
   610
         (Thm.dest_binop #> (fn (a,b) => (Thm.dest_binop a, Thm.dest_binop b))) ct
haftmann@36751
   611
    val _ = map (HOLogic.dest_number o term_of) [x,y,z,w]
haftmann@36751
   612
    val T = ctyp_of_term x
wenzelm@51717
   613
    val [y_nz, z_nz] = map (prove_nz ctxt T) [y, z]
haftmann@36751
   614
    val th = instantiate' [SOME T] (map SOME [y,z,x,w]) add_frac_eq
wenzelm@36945
   615
  in SOME (Thm.implies_elim (Thm.implies_elim th y_nz) z_nz)
haftmann@36751
   616
  end
haftmann@36751
   617
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36751
   618
wenzelm@51717
   619
 fun proc2 phi ctxt ct =
haftmann@36751
   620
  let
haftmann@36751
   621
    val (l,r) = Thm.dest_binop ct
haftmann@36751
   622
    val T = ctyp_of_term l
haftmann@36751
   623
  in (case (term_of l, term_of r) of
huffman@44064
   624
      (Const(@{const_name Fields.divide},_)$_$_, _) =>
haftmann@36751
   625
        let val (x,y) = Thm.dest_binop l val z = r
haftmann@36751
   626
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
wenzelm@51717
   627
            val ynz = prove_nz ctxt T y
wenzelm@36945
   628
        in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,x,z]) add_frac_num) ynz)
haftmann@36751
   629
        end
huffman@44064
   630
     | (_, Const (@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   631
        let val (x,y) = Thm.dest_binop r val z = l
haftmann@36751
   632
            val _ = map (HOLogic.dest_number o term_of) [x,y,z]
wenzelm@51717
   633
            val ynz = prove_nz ctxt T y
wenzelm@36945
   634
        in SOME (Thm.implies_elim (instantiate' [SOME T] (map SOME [y,z,x]) add_num_frac) ynz)
haftmann@36751
   635
        end
haftmann@36751
   636
     | _ => NONE)
haftmann@36751
   637
  end
haftmann@36751
   638
  handle CTERM _ => NONE | TERM _ => NONE | THM _ => NONE
haftmann@36751
   639
huffman@44064
   640
 fun is_number (Const(@{const_name Fields.divide},_)$a$b) = is_number a andalso is_number b
haftmann@36751
   641
   | is_number t = can HOLogic.dest_number t
haftmann@36751
   642
haftmann@36751
   643
 val is_number = is_number o term_of
haftmann@36751
   644
wenzelm@51717
   645
 fun proc3 phi ctxt ct =
haftmann@36751
   646
  (case term_of ct of
huffman@44064
   647
    Const(@{const_name Orderings.less},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   648
      let
haftmann@36751
   649
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   650
        val _ = map is_number [a,b,c]
haftmann@36751
   651
        val T = ctyp_of_term c
haftmann@36751
   652
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_less_eq"}
haftmann@36751
   653
      in SOME (mk_meta_eq th) end
huffman@44064
   654
  | Const(@{const_name Orderings.less_eq},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   655
      let
haftmann@36751
   656
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   657
        val _ = map is_number [a,b,c]
haftmann@36751
   658
        val T = ctyp_of_term c
haftmann@36751
   659
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_le_eq"}
haftmann@36751
   660
      in SOME (mk_meta_eq th) end
huffman@44064
   661
  | Const(@{const_name HOL.eq},_)$(Const(@{const_name Fields.divide},_)$_$_)$_ =>
haftmann@36751
   662
      let
haftmann@36751
   663
        val ((a,b),c) = Thm.dest_binop ct |>> Thm.dest_binop
haftmann@36751
   664
        val _ = map is_number [a,b,c]
haftmann@36751
   665
        val T = ctyp_of_term c
haftmann@36751
   666
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "divide_eq_eq"}
haftmann@36751
   667
      in SOME (mk_meta_eq th) end
huffman@44064
   668
  | Const(@{const_name Orderings.less},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   669
    let
haftmann@36751
   670
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   671
        val _ = map is_number [a,b,c]
haftmann@36751
   672
        val T = ctyp_of_term c
haftmann@36751
   673
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "less_divide_eq"}
haftmann@36751
   674
      in SOME (mk_meta_eq th) end
huffman@44064
   675
  | Const(@{const_name Orderings.less_eq},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   676
    let
haftmann@36751
   677
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   678
        val _ = map is_number [a,b,c]
haftmann@36751
   679
        val T = ctyp_of_term c
haftmann@36751
   680
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "le_divide_eq"}
haftmann@36751
   681
      in SOME (mk_meta_eq th) end
huffman@44064
   682
  | Const(@{const_name HOL.eq},_)$_$(Const(@{const_name Fields.divide},_)$_$_) =>
haftmann@36751
   683
    let
haftmann@36751
   684
      val (a,(b,c)) = Thm.dest_binop ct ||> Thm.dest_binop
haftmann@36751
   685
        val _ = map is_number [a,b,c]
haftmann@36751
   686
        val T = ctyp_of_term c
haftmann@36751
   687
        val th = instantiate' [SOME T] (map SOME [a,b,c]) @{thm "eq_divide_eq"}
haftmann@36751
   688
      in SOME (mk_meta_eq th) end
haftmann@36751
   689
  | _ => NONE)
haftmann@36751
   690
  handle TERM _ => NONE | CTERM _ => NONE | THM _ => NONE
haftmann@36751
   691
haftmann@36751
   692
val add_frac_frac_simproc =
haftmann@36751
   693
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + (?w::?'a::field)/?z"}],
haftmann@36751
   694
                     name = "add_frac_frac_simproc",
haftmann@36751
   695
                     proc = proc, identifier = []}
haftmann@36751
   696
haftmann@36751
   697
val add_frac_num_simproc =
haftmann@36751
   698
       make_simproc {lhss = [@{cpat "(?x::?'a::field)/?y + ?z"}, @{cpat "?z + (?x::?'a::field)/?y"}],
haftmann@36751
   699
                     name = "add_frac_num_simproc",
haftmann@36751
   700
                     proc = proc2, identifier = []}
haftmann@36751
   701
haftmann@36751
   702
val ord_frac_simproc =
haftmann@36751
   703
  make_simproc
haftmann@36751
   704
    {lhss = [@{cpat "(?a::(?'a::{field, ord}))/?b < ?c"},
haftmann@36751
   705
             @{cpat "(?a::(?'a::{field, ord}))/?b <= ?c"},
haftmann@36751
   706
             @{cpat "?c < (?a::(?'a::{field, ord}))/?b"},
haftmann@36751
   707
             @{cpat "?c <= (?a::(?'a::{field, ord}))/?b"},
haftmann@36751
   708
             @{cpat "?c = ((?a::(?'a::{field, ord}))/?b)"},
haftmann@36751
   709
             @{cpat "((?a::(?'a::{field, ord}))/ ?b) = ?c"}],
haftmann@36751
   710
             name = "ord_frac_simproc", proc = proc3, identifier = []}
haftmann@36751
   711
haftmann@36751
   712
val ths = [@{thm "mult_numeral_1"}, @{thm "mult_numeral_1_right"},
haftmann@58512
   713
           @{thm "divide_numeral_1"},
huffman@47108
   714
           @{thm "divide_zero"}, @{thm divide_zero_left},
haftmann@36751
   715
           @{thm "divide_divide_eq_left"}, 
haftmann@36751
   716
           @{thm "times_divide_eq_left"}, @{thm "times_divide_eq_right"},
haftmann@36751
   717
           @{thm "times_divide_times_eq"},
haftmann@36751
   718
           @{thm "divide_divide_eq_right"},
haftmann@54230
   719
           @{thm diff_conv_add_uminus}, @{thm "minus_divide_left"},
huffman@47108
   720
           @{thm "add_divide_distrib"} RS sym,
haftmann@36751
   721
           @{thm field_divide_inverse} RS sym, @{thm inverse_divide}, 
haftmann@57512
   722
           Conv.fconv_rule (Conv.arg_conv (Conv.arg1_conv (Conv.rewr_conv (mk_meta_eq @{thm mult.commute}))))   
haftmann@36751
   723
           (@{thm field_divide_inverse} RS sym)]
haftmann@36751
   724
wenzelm@51717
   725
val field_comp_ss =
wenzelm@51717
   726
  simpset_of
wenzelm@51717
   727
    (put_simpset HOL_basic_ss @{context}
wenzelm@51717
   728
      addsimps @{thms "semiring_norm"}
wenzelm@45620
   729
      addsimps ths addsimps @{thms simp_thms}
wenzelm@45620
   730
      addsimprocs field_cancel_numeral_factors
wenzelm@45620
   731
      addsimprocs [add_frac_frac_simproc, add_frac_num_simproc, ord_frac_simproc]
wenzelm@45620
   732
      |> Simplifier.add_cong @{thm "if_weak_cong"})
wenzelm@51717
   733
wenzelm@51717
   734
in
wenzelm@51717
   735
wenzelm@51717
   736
fun field_comp_conv ctxt =
wenzelm@51717
   737
  Simplifier.rewrite (put_simpset field_comp_ss ctxt)
wenzelm@45620
   738
  then_conv
wenzelm@51717
   739
  Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps [@{thm numeral_1_eq_1}])
haftmann@36751
   740
haftmann@36751
   741
end
haftmann@36751
   742
wenzelm@23164
   743
end;
wenzelm@23164
   744