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