src/HOL/Integ/int_factor_simprocs.ML
author wenzelm
Mon Oct 17 23:10:15 2005 +0200 (2005-10-17)
changeset 17877 67d5ab1cb0d8
parent 16973 b2a894562b8f
child 18328 841261f303a1
permissions -rw-r--r--
Simplifier.inherit_context instead of Simplifier.inherit_bounds;
paulson@10536
     1
(*  Title:      HOL/int_factor_simprocs.ML
paulson@10536
     2
    ID:         $Id$
paulson@10536
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@10536
     4
    Copyright   2000  University of Cambridge
paulson@10536
     5
paulson@14390
     6
Factor cancellation simprocs for the integers (and for fields).
paulson@10536
     7
paulson@14378
     8
This file can't be combined with int_arith1 because it requires IntDiv.thy.
paulson@10536
     9
*)
paulson@10536
    10
paulson@14390
    11
paulson@14390
    12
(*To quote from Provers/Arith/cancel_numeral_factor.ML:
paulson@14390
    13
paulson@14390
    14
Cancels common coefficients in balanced expressions:
paulson@14390
    15
paulson@14390
    16
     u*#m ~~ u'*#m'  ==  #n*u ~~ #n'*u'
paulson@14390
    17
paulson@14390
    18
where ~~ is an appropriate balancing operation (e.g. =, <=, <, div, /)
paulson@14390
    19
and d = gcd(m,m') and n=m/d and n'=m'/d.
paulson@14390
    20
*)
paulson@14390
    21
paulson@14390
    22
val rel_number_of = [eq_number_of_eq, less_number_of_eq_neg, le_number_of_eq];
paulson@14390
    23
paulson@14390
    24
(** Factor cancellation theorems for integer division (div, not /) **)
paulson@10536
    25
paulson@11868
    26
Goal "!!k::int. k~=0 ==> (k*m) div (k*n) = (m div n)";
wenzelm@13462
    27
by (stac zdiv_zmult_zmult1 1);
wenzelm@13462
    28
by Auto_tac;
paulson@10536
    29
qed "int_mult_div_cancel1";
paulson@10536
    30
paulson@10703
    31
(*For ExtractCommonTermFun, cancelling common factors*)
paulson@11868
    32
Goal "(k*m) div (k*n) = (if k = (0::int) then 0 else m div n)";
wenzelm@13462
    33
by (simp_tac (simpset() addsimps [int_mult_div_cancel1]) 1);
paulson@10703
    34
qed "int_mult_div_cancel_disj";
paulson@10703
    35
paulson@14378
    36
paulson@10536
    37
local
paulson@10536
    38
  open Int_Numeral_Simprocs
paulson@10536
    39
in
paulson@10536
    40
paulson@10536
    41
structure CancelNumeralFactorCommon =
paulson@10536
    42
  struct
wenzelm@13462
    43
  val mk_coeff          = mk_coeff
wenzelm@13462
    44
  val dest_coeff        = dest_coeff 1
wenzelm@16973
    45
  val trans_tac         = fn _ => trans_tac
wenzelm@16973
    46
  fun norm_tac ss =
wenzelm@17877
    47
    let val HOL_ss' = Simplifier.inherit_context ss HOL_ss in
wenzelm@16973
    48
      ALLGOALS (simp_tac (HOL_ss' addsimps minus_from_mult_simps @ mult_1s))
wenzelm@16973
    49
      THEN ALLGOALS (simp_tac (HOL_ss' addsimps bin_simps@mult_minus_simps))
wenzelm@16973
    50
      THEN ALLGOALS (simp_tac (HOL_ss' addsimps mult_ac))
wenzelm@16973
    51
    end
wenzelm@16973
    52
  fun numeral_simp_tac ss =
wenzelm@17877
    53
    ALLGOALS (simp_tac (Simplifier.inherit_context ss HOL_ss addsimps rel_number_of @ bin_simps))
wenzelm@16973
    54
  val simplify_meta_eq = 
paulson@14390
    55
	Int_Numeral_Simprocs.simplify_meta_eq
paulson@14390
    56
	     [add_0, add_0_right,
paulson@14390
    57
	      mult_zero_left, mult_zero_right, mult_1, mult_1_right];
paulson@10536
    58
  end
paulson@10536
    59
paulson@14390
    60
(*Version for integer division*)
paulson@10536
    61
structure DivCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
    62
 (open CancelNumeralFactorCommon
wenzelm@13485
    63
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
    64
  val mk_bal   = HOLogic.mk_binop "Divides.op div"
paulson@10536
    65
  val dest_bal = HOLogic.dest_bin "Divides.op div" HOLogic.intT
paulson@10536
    66
  val cancel = int_mult_div_cancel1 RS trans
paulson@10536
    67
  val neg_exchanges = false
paulson@10536
    68
)
paulson@10536
    69
paulson@14390
    70
(*Version for fields*)
paulson@14390
    71
structure FieldDivCancelNumeralFactor = CancelNumeralFactorFun
paulson@14390
    72
 (open CancelNumeralFactorCommon
paulson@14390
    73
  val prove_conv = Bin_Simprocs.prove_conv
paulson@14390
    74
  val mk_bal   = HOLogic.mk_binop "HOL.divide"
paulson@14390
    75
  val dest_bal = HOLogic.dest_bin "HOL.divide" Term.dummyT
paulson@14390
    76
  val cancel = mult_divide_cancel_left RS trans
paulson@14390
    77
  val neg_exchanges = false
paulson@14390
    78
)
paulson@14390
    79
paulson@14390
    80
(*Version for ordered rings: mult_cancel_left requires an ordering*)
paulson@10536
    81
structure EqCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
    82
 (open CancelNumeralFactorCommon
wenzelm@13485
    83
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
    84
  val mk_bal   = HOLogic.mk_eq
paulson@14390
    85
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
paulson@14378
    86
  val cancel = mult_cancel_left RS trans
paulson@10536
    87
  val neg_exchanges = false
paulson@10536
    88
)
paulson@10536
    89
paulson@14390
    90
(*Version for (unordered) fields*)
paulson@14390
    91
structure FieldEqCancelNumeralFactor = CancelNumeralFactorFun
paulson@14390
    92
 (open CancelNumeralFactorCommon
paulson@14390
    93
  val prove_conv = Bin_Simprocs.prove_conv
paulson@14390
    94
  val mk_bal   = HOLogic.mk_eq
paulson@14390
    95
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
paulson@14390
    96
  val cancel = field_mult_cancel_left RS trans
paulson@14390
    97
  val neg_exchanges = false
paulson@14390
    98
)
paulson@14390
    99
paulson@10536
   100
structure LessCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
   101
 (open CancelNumeralFactorCommon
wenzelm@13485
   102
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
   103
  val mk_bal   = HOLogic.mk_binrel "op <"
paulson@14390
   104
  val dest_bal = HOLogic.dest_bin "op <" Term.dummyT
paulson@14378
   105
  val cancel = mult_less_cancel_left RS trans
paulson@10536
   106
  val neg_exchanges = true
paulson@10536
   107
)
paulson@10536
   108
paulson@10536
   109
structure LeCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
   110
 (open CancelNumeralFactorCommon
wenzelm@13485
   111
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
   112
  val mk_bal   = HOLogic.mk_binrel "op <="
paulson@14390
   113
  val dest_bal = HOLogic.dest_bin "op <=" Term.dummyT
paulson@14378
   114
  val cancel = mult_le_cancel_left RS trans
paulson@10536
   115
  val neg_exchanges = true
paulson@10536
   116
)
paulson@10536
   117
paulson@14390
   118
val ring_cancel_numeral_factors =
paulson@11868
   119
  map Bin_Simprocs.prep_simproc
paulson@14390
   120
   [("ring_eq_cancel_numeral_factor",
obua@14738
   121
     ["(l::'a::{ordered_idom,number_ring}) * m = n",
obua@14738
   122
      "(l::'a::{ordered_idom,number_ring}) = m * n"],
paulson@10536
   123
     EqCancelNumeralFactor.proc),
paulson@14390
   124
    ("ring_less_cancel_numeral_factor",
obua@14738
   125
     ["(l::'a::{ordered_idom,number_ring}) * m < n",
obua@14738
   126
      "(l::'a::{ordered_idom,number_ring}) < m * n"],
paulson@10536
   127
     LessCancelNumeralFactor.proc),
paulson@14390
   128
    ("ring_le_cancel_numeral_factor",
obua@14738
   129
     ["(l::'a::{ordered_idom,number_ring}) * m <= n",
obua@14738
   130
      "(l::'a::{ordered_idom,number_ring}) <= m * n"],
paulson@10536
   131
     LeCancelNumeralFactor.proc),
paulson@14390
   132
    ("int_div_cancel_numeral_factors",
paulson@14390
   133
     ["((l::int) * m) div n", "(l::int) div (m * n)"],
paulson@10536
   134
     DivCancelNumeralFactor.proc)];
paulson@10536
   135
paulson@14390
   136
paulson@14390
   137
val field_cancel_numeral_factors =
paulson@14390
   138
  map Bin_Simprocs.prep_simproc
paulson@14390
   139
   [("field_eq_cancel_numeral_factor",
paulson@14390
   140
     ["(l::'a::{field,number_ring}) * m = n",
paulson@14390
   141
      "(l::'a::{field,number_ring}) = m * n"],
paulson@14390
   142
     FieldEqCancelNumeralFactor.proc),
paulson@14390
   143
    ("field_cancel_numeral_factor",
paulson@14426
   144
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
paulson@14426
   145
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)",
paulson@14426
   146
      "((number_of v)::'a::{division_by_zero,field,number_ring}) / (number_of w)"],
paulson@14390
   147
     FieldDivCancelNumeralFactor.proc)]
paulson@14390
   148
paulson@10536
   149
end;
paulson@10536
   150
paulson@14390
   151
Addsimprocs ring_cancel_numeral_factors;
paulson@14390
   152
Addsimprocs field_cancel_numeral_factors;
paulson@10536
   153
paulson@10536
   154
(*examples:
paulson@10536
   155
print_depth 22;
paulson@10536
   156
set timing;
paulson@10536
   157
set trace_simp;
wenzelm@13462
   158
fun test s = (Goal s; by (Simp_tac 1));
paulson@10536
   159
wenzelm@11704
   160
test "9*x = 12 * (y::int)";
wenzelm@11704
   161
test "(9*x) div (12 * (y::int)) = z";
wenzelm@11704
   162
test "9*x < 12 * (y::int)";
wenzelm@11704
   163
test "9*x <= 12 * (y::int)";
paulson@10536
   164
wenzelm@11704
   165
test "-99*x = 132 * (y::int)";
wenzelm@11704
   166
test "(-99*x) div (132 * (y::int)) = z";
wenzelm@11704
   167
test "-99*x < 132 * (y::int)";
wenzelm@11704
   168
test "-99*x <= 132 * (y::int)";
paulson@10536
   169
wenzelm@11704
   170
test "999*x = -396 * (y::int)";
wenzelm@11704
   171
test "(999*x) div (-396 * (y::int)) = z";
wenzelm@11704
   172
test "999*x < -396 * (y::int)";
wenzelm@11704
   173
test "999*x <= -396 * (y::int)";
paulson@10536
   174
wenzelm@11704
   175
test "-99*x = -81 * (y::int)";
wenzelm@11704
   176
test "(-99*x) div (-81 * (y::int)) = z";
wenzelm@11704
   177
test "-99*x <= -81 * (y::int)";
wenzelm@11704
   178
test "-99*x < -81 * (y::int)";
paulson@10536
   179
wenzelm@11704
   180
test "-2 * x = -1 * (y::int)";
wenzelm@11704
   181
test "-2 * x = -(y::int)";
wenzelm@11704
   182
test "(-2 * x) div (-1 * (y::int)) = z";
wenzelm@11704
   183
test "-2 * x < -(y::int)";
wenzelm@11704
   184
test "-2 * x <= -1 * (y::int)";
wenzelm@11704
   185
test "-x < -23 * (y::int)";
wenzelm@11704
   186
test "-x <= -23 * (y::int)";
paulson@10536
   187
*)
paulson@10536
   188
paulson@14390
   189
(*And the same examples for fields such as rat or real:
paulson@14390
   190
test "0 <= (y::rat) * -2";
paulson@14390
   191
test "9*x = 12 * (y::rat)";
paulson@14390
   192
test "(9*x) / (12 * (y::rat)) = z";
paulson@14390
   193
test "9*x < 12 * (y::rat)";
paulson@14390
   194
test "9*x <= 12 * (y::rat)";
paulson@14390
   195
paulson@14390
   196
test "-99*x = 132 * (y::rat)";
paulson@14390
   197
test "(-99*x) / (132 * (y::rat)) = z";
paulson@14390
   198
test "-99*x < 132 * (y::rat)";
paulson@14390
   199
test "-99*x <= 132 * (y::rat)";
paulson@14390
   200
paulson@14390
   201
test "999*x = -396 * (y::rat)";
paulson@14390
   202
test "(999*x) / (-396 * (y::rat)) = z";
paulson@14390
   203
test "999*x < -396 * (y::rat)";
paulson@14390
   204
test "999*x <= -396 * (y::rat)";
paulson@14390
   205
paulson@14390
   206
test  "(- ((2::rat) * x) <= 2 * y)";
paulson@14390
   207
test "-99*x = -81 * (y::rat)";
paulson@14390
   208
test "(-99*x) / (-81 * (y::rat)) = z";
paulson@14390
   209
test "-99*x <= -81 * (y::rat)";
paulson@14390
   210
test "-99*x < -81 * (y::rat)";
paulson@14390
   211
paulson@14390
   212
test "-2 * x = -1 * (y::rat)";
paulson@14390
   213
test "-2 * x = -(y::rat)";
paulson@14390
   214
test "(-2 * x) / (-1 * (y::rat)) = z";
paulson@14390
   215
test "-2 * x < -(y::rat)";
paulson@14390
   216
test "-2 * x <= -1 * (y::rat)";
paulson@14390
   217
test "-x < -23 * (y::rat)";
paulson@14390
   218
test "-x <= -23 * (y::rat)";
paulson@14390
   219
*)
paulson@14390
   220
paulson@10703
   221
paulson@10703
   222
(** Declarations for ExtractCommonTerm **)
paulson@10703
   223
paulson@10703
   224
local
paulson@10703
   225
  open Int_Numeral_Simprocs
paulson@10703
   226
in
paulson@10703
   227
paulson@10703
   228
(*Find first term that matches u*)
wenzelm@13462
   229
fun find_first past u []         = raise TERM("find_first", [])
paulson@10703
   230
  | find_first past u (t::terms) =
wenzelm@13462
   231
        if u aconv t then (rev past @ terms)
paulson@10703
   232
        else find_first (t::past) u terms
wenzelm@13462
   233
        handle TERM _ => find_first (t::past) u terms;
paulson@10703
   234
paulson@15271
   235
(** Final simplification for the CancelFactor simprocs **)
paulson@15271
   236
val simplify_one = 
paulson@15271
   237
    Int_Numeral_Simprocs.simplify_meta_eq  
paulson@15271
   238
       [mult_1_left, mult_1_right, zdiv_1, numeral_1_eq_1];
paulson@15271
   239
wenzelm@16973
   240
fun cancel_simplify_meta_eq cancel_th ss th =
wenzelm@16973
   241
    simplify_one ss (([th, cancel_th]) MRS trans);
paulson@10703
   242
paulson@14426
   243
(*At present, long_mk_prod creates Numeral1, so this requires the axclass
paulson@14426
   244
  number_ring*)
paulson@10703
   245
structure CancelFactorCommon =
paulson@10703
   246
  struct
wenzelm@13462
   247
  val mk_sum            = long_mk_prod
wenzelm@13462
   248
  val dest_sum          = dest_prod
wenzelm@13462
   249
  val mk_coeff          = mk_coeff
wenzelm@13462
   250
  val dest_coeff        = dest_coeff
wenzelm@13462
   251
  val find_first        = find_first []
wenzelm@16973
   252
  val trans_tac         = fn _ => trans_tac
wenzelm@16973
   253
  fun norm_tac ss =
wenzelm@17877
   254
    ALLGOALS (simp_tac (Simplifier.inherit_context ss HOL_ss addsimps mult_1s @ mult_ac))
paulson@10703
   255
  end;
paulson@10703
   256
obua@14738
   257
(*mult_cancel_left requires an ordered comm_ring_1, such as int. The version in
paulson@14387
   258
  rat_arith.ML works for all fields.*)
paulson@10703
   259
structure EqCancelFactor = ExtractCommonTermFun
paulson@10703
   260
 (open CancelFactorCommon
wenzelm@13485
   261
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10703
   262
  val mk_bal   = HOLogic.mk_eq
paulson@10703
   263
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
paulson@14378
   264
  val simplify_meta_eq  = cancel_simplify_meta_eq mult_cancel_left
paulson@10703
   265
);
paulson@10703
   266
paulson@14387
   267
(*int_mult_div_cancel_disj is for integer division (div). The version in
paulson@14387
   268
  rat_arith.ML works for all fields, using real division (/).*)
paulson@10703
   269
structure DivideCancelFactor = ExtractCommonTermFun
paulson@10703
   270
 (open CancelFactorCommon
wenzelm@13485
   271
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10703
   272
  val mk_bal   = HOLogic.mk_binop "Divides.op div"
paulson@10703
   273
  val dest_bal = HOLogic.dest_bin "Divides.op div" HOLogic.intT
paulson@10703
   274
  val simplify_meta_eq  = cancel_simplify_meta_eq int_mult_div_cancel_disj
paulson@10703
   275
);
paulson@10703
   276
wenzelm@13462
   277
val int_cancel_factor =
paulson@11868
   278
  map Bin_Simprocs.prep_simproc
paulson@15271
   279
   [("ring_eq_cancel_factor", ["(l::int) * m = n", "(l::int) = m * n"],
paulson@14390
   280
    EqCancelFactor.proc),
paulson@14390
   281
    ("int_divide_cancel_factor", ["((l::int) * m) div n", "(l::int) div (m*n)"],
paulson@10703
   282
     DivideCancelFactor.proc)];
paulson@10703
   283
paulson@14390
   284
(** Versions for all fields, including unordered ones (type complex).*)
paulson@14390
   285
paulson@14390
   286
structure FieldEqCancelFactor = ExtractCommonTermFun
paulson@14390
   287
 (open CancelFactorCommon
paulson@14390
   288
  val prove_conv = Bin_Simprocs.prove_conv
paulson@14390
   289
  val mk_bal   = HOLogic.mk_eq
paulson@14390
   290
  val dest_bal = HOLogic.dest_bin "op =" Term.dummyT
paulson@14390
   291
  val simplify_meta_eq  = cancel_simplify_meta_eq field_mult_cancel_left
paulson@14390
   292
);
paulson@14390
   293
paulson@14390
   294
structure FieldDivideCancelFactor = ExtractCommonTermFun
paulson@14390
   295
 (open CancelFactorCommon
paulson@14390
   296
  val prove_conv = Bin_Simprocs.prove_conv
paulson@14390
   297
  val mk_bal   = HOLogic.mk_binop "HOL.divide"
paulson@14390
   298
  val dest_bal = HOLogic.dest_bin "HOL.divide" Term.dummyT
paulson@14390
   299
  val simplify_meta_eq  = cancel_simplify_meta_eq mult_divide_cancel_eq_if
paulson@14390
   300
);
paulson@14390
   301
paulson@15271
   302
(*The number_ring class is necessary because the simprocs refer to the
paulson@14426
   303
  binary number 1.  FIXME: probably they could use 1 instead.*)
paulson@14390
   304
val field_cancel_factor =
paulson@14390
   305
  map Bin_Simprocs.prep_simproc
paulson@14390
   306
   [("field_eq_cancel_factor",
paulson@14426
   307
     ["(l::'a::{field,number_ring}) * m = n",
paulson@15271
   308
      "(l::'a::{field,number_ring}) = m * n"],
paulson@14390
   309
     FieldEqCancelFactor.proc),
paulson@14390
   310
    ("field_divide_cancel_factor",
paulson@14426
   311
     ["((l::'a::{division_by_zero,field,number_ring}) * m) / n",
paulson@14426
   312
      "(l::'a::{division_by_zero,field,number_ring}) / (m * n)"],
paulson@14390
   313
     FieldDivideCancelFactor.proc)];
paulson@14390
   314
paulson@10703
   315
end;
paulson@10703
   316
paulson@10703
   317
Addsimprocs int_cancel_factor;
paulson@14390
   318
Addsimprocs field_cancel_factor;
paulson@10703
   319
paulson@10703
   320
paulson@10703
   321
(*examples:
paulson@10703
   322
print_depth 22;
paulson@10703
   323
set timing;
paulson@10703
   324
set trace_simp;
wenzelm@13462
   325
fun test s = (Goal s; by (Asm_simp_tac 1));
paulson@10703
   326
paulson@10703
   327
test "x*k = k*(y::int)";
wenzelm@13462
   328
test "k = k*(y::int)";
paulson@10703
   329
test "a*(b*c) = (b::int)";
paulson@10703
   330
test "a*(b*c) = d*(b::int)*(x*a)";
paulson@10703
   331
paulson@10703
   332
test "(x*k) div (k*(y::int)) = (uu::int)";
wenzelm@13462
   333
test "(k) div (k*(y::int)) = (uu::int)";
paulson@10703
   334
test "(a*(b*c)) div ((b::int)) = (uu::int)";
paulson@10703
   335
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
paulson@10703
   336
*)
paulson@10703
   337
paulson@14390
   338
(*And the same examples for fields such as rat or real:
paulson@14390
   339
print_depth 22;
paulson@14390
   340
set timing;
paulson@14390
   341
set trace_simp;
paulson@14390
   342
fun test s = (Goal s; by (Asm_simp_tac 1));
paulson@14390
   343
paulson@14390
   344
test "x*k = k*(y::rat)";
paulson@14390
   345
test "k = k*(y::rat)";
paulson@14390
   346
test "a*(b*c) = (b::rat)";
paulson@14390
   347
test "a*(b*c) = d*(b::rat)*(x*a)";
paulson@14390
   348
paulson@14390
   349
paulson@14390
   350
test "(x*k) / (k*(y::rat)) = (uu::rat)";
paulson@14390
   351
test "(k) / (k*(y::rat)) = (uu::rat)";
paulson@14390
   352
test "(a*(b*c)) / ((b::rat)) = (uu::rat)";
paulson@14390
   353
test "(a*(b*c)) / (d*(b::rat)*(x*a)) = (uu::rat)";
paulson@14390
   354
paulson@14390
   355
(*FIXME: what do we do about this?*)
paulson@14390
   356
test "a*(b*c)/(y*z) = d*(b::rat)*(x*a)/z";
paulson@14390
   357
*)