src/HOL/Integ/int_factor_simprocs.ML
author paulson
Tue Jul 15 15:20:54 2003 +0200 (2003-07-15)
changeset 14113 7b3513ba0f86
parent 13485 acf39e924091
child 14378 69c4d5997669
permissions -rw-r--r--
Fixing a simproc bug
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@10536
     6
Factor cancellation simprocs for the integers.
paulson@10536
     7
paulson@10536
     8
This file can't be combined with int_arith1,2 because it requires IntDiv.thy.
paulson@10536
     9
*)
paulson@10536
    10
paulson@10536
    11
(** Factor cancellation theorems for "int" **)
paulson@10536
    12
paulson@11868
    13
Goal "!!k::int. (k*m <= k*n) = ((0 < k --> m<=n) & (k < 0 --> n<=m))";
paulson@10536
    14
by (stac zmult_zle_cancel1 1);
wenzelm@13462
    15
by Auto_tac;
paulson@10536
    16
qed "int_mult_le_cancel1";
paulson@10536
    17
paulson@11868
    18
Goal "!!k::int. (k*m < k*n) = ((0 < k & m<n) | (k < 0 & n<m))";
paulson@10536
    19
by (stac zmult_zless_cancel1 1);
wenzelm@13462
    20
by Auto_tac;
paulson@10536
    21
qed "int_mult_less_cancel1";
paulson@10536
    22
paulson@11868
    23
Goal "!!k::int. (k*m = k*n) = (k = 0 | m=n)";
wenzelm@13462
    24
by Auto_tac;
paulson@10536
    25
qed "int_mult_eq_cancel1";
paulson@10536
    26
paulson@11868
    27
Goal "!!k::int. k~=0 ==> (k*m) div (k*n) = (m div n)";
wenzelm@13462
    28
by (stac zdiv_zmult_zmult1 1);
wenzelm@13462
    29
by Auto_tac;
paulson@10536
    30
qed "int_mult_div_cancel1";
paulson@10536
    31
paulson@10703
    32
(*For ExtractCommonTermFun, cancelling common factors*)
paulson@11868
    33
Goal "(k*m) div (k*n) = (if k = (0::int) then 0 else m div n)";
wenzelm@13462
    34
by (simp_tac (simpset() addsimps [int_mult_div_cancel1]) 1);
paulson@10703
    35
qed "int_mult_div_cancel_disj";
paulson@10703
    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
paulson@10536
    45
  val trans_tac         = trans_tac
wenzelm@13462
    46
  val norm_tac =
paulson@12018
    47
     ALLGOALS (simp_tac (HOL_ss addsimps int_minus_from_mult_simps@mult_1s))
paulson@10713
    48
     THEN ALLGOALS (simp_tac (HOL_ss addsimps bin_simps@int_mult_minus_simps))
paulson@14113
    49
     THEN ALLGOALS (simp_tac (HOL_ss addsimps zmult_ac))
wenzelm@13462
    50
  val numeral_simp_tac  = ALLGOALS (simp_tac (HOL_ss addsimps bin_simps))
paulson@10536
    51
  val simplify_meta_eq  = simplify_meta_eq mult_1s
paulson@10536
    52
  end
paulson@10536
    53
paulson@10536
    54
structure DivCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
    55
 (open CancelNumeralFactorCommon
wenzelm@13485
    56
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
    57
  val mk_bal   = HOLogic.mk_binop "Divides.op div"
paulson@10536
    58
  val dest_bal = HOLogic.dest_bin "Divides.op div" HOLogic.intT
paulson@10536
    59
  val cancel = int_mult_div_cancel1 RS trans
paulson@10536
    60
  val neg_exchanges = false
paulson@10536
    61
)
paulson@10536
    62
paulson@10536
    63
structure EqCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
    64
 (open CancelNumeralFactorCommon
wenzelm@13485
    65
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
    66
  val mk_bal   = HOLogic.mk_eq
paulson@10536
    67
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
paulson@10536
    68
  val cancel = int_mult_eq_cancel1 RS trans
paulson@10536
    69
  val neg_exchanges = false
paulson@10536
    70
)
paulson@10536
    71
paulson@10536
    72
structure LessCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
    73
 (open CancelNumeralFactorCommon
wenzelm@13485
    74
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
    75
  val mk_bal   = HOLogic.mk_binrel "op <"
paulson@10536
    76
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.intT
paulson@10536
    77
  val cancel = int_mult_less_cancel1 RS trans
paulson@10536
    78
  val neg_exchanges = true
paulson@10536
    79
)
paulson@10536
    80
paulson@10536
    81
structure LeCancelNumeralFactor = CancelNumeralFactorFun
paulson@10536
    82
 (open CancelNumeralFactorCommon
wenzelm@13485
    83
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10536
    84
  val mk_bal   = HOLogic.mk_binrel "op <="
paulson@10536
    85
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.intT
paulson@10536
    86
  val cancel = int_mult_le_cancel1 RS trans
paulson@10536
    87
  val neg_exchanges = true
paulson@10536
    88
)
paulson@10536
    89
wenzelm@13462
    90
val int_cancel_numeral_factors =
paulson@11868
    91
  map Bin_Simprocs.prep_simproc
wenzelm@13462
    92
   [("inteq_cancel_numeral_factors", ["(l::int) * m = n", "(l::int) = m * n"],
paulson@10536
    93
     EqCancelNumeralFactor.proc),
wenzelm@13462
    94
    ("intless_cancel_numeral_factors", ["(l::int) * m < n", "(l::int) < m * n"],
paulson@10536
    95
     LessCancelNumeralFactor.proc),
wenzelm@13462
    96
    ("intle_cancel_numeral_factors", ["(l::int) * m <= n", "(l::int) <= m * n"],
paulson@10536
    97
     LeCancelNumeralFactor.proc),
wenzelm@13462
    98
    ("intdiv_cancel_numeral_factors", ["((l::int) * m) div n", "(l::int) div (m * n)"],
paulson@10536
    99
     DivCancelNumeralFactor.proc)];
paulson@10536
   100
paulson@10536
   101
end;
paulson@10536
   102
paulson@10536
   103
Addsimprocs int_cancel_numeral_factors;
paulson@10536
   104
paulson@10536
   105
paulson@10536
   106
(*examples:
paulson@10536
   107
print_depth 22;
paulson@10536
   108
set timing;
paulson@10536
   109
set trace_simp;
wenzelm@13462
   110
fun test s = (Goal s; by (Simp_tac 1));
paulson@10536
   111
wenzelm@11704
   112
test "9*x = 12 * (y::int)";
wenzelm@11704
   113
test "(9*x) div (12 * (y::int)) = z";
wenzelm@11704
   114
test "9*x < 12 * (y::int)";
wenzelm@11704
   115
test "9*x <= 12 * (y::int)";
paulson@10536
   116
wenzelm@11704
   117
test "-99*x = 132 * (y::int)";
wenzelm@11704
   118
test "(-99*x) div (132 * (y::int)) = z";
wenzelm@11704
   119
test "-99*x < 132 * (y::int)";
wenzelm@11704
   120
test "-99*x <= 132 * (y::int)";
paulson@10536
   121
wenzelm@11704
   122
test "999*x = -396 * (y::int)";
wenzelm@11704
   123
test "(999*x) div (-396 * (y::int)) = z";
wenzelm@11704
   124
test "999*x < -396 * (y::int)";
wenzelm@11704
   125
test "999*x <= -396 * (y::int)";
paulson@10536
   126
wenzelm@11704
   127
test "-99*x = -81 * (y::int)";
wenzelm@11704
   128
test "(-99*x) div (-81 * (y::int)) = z";
wenzelm@11704
   129
test "-99*x <= -81 * (y::int)";
wenzelm@11704
   130
test "-99*x < -81 * (y::int)";
paulson@10536
   131
wenzelm@11704
   132
test "-2 * x = -1 * (y::int)";
wenzelm@11704
   133
test "-2 * x = -(y::int)";
wenzelm@11704
   134
test "(-2 * x) div (-1 * (y::int)) = z";
wenzelm@11704
   135
test "-2 * x < -(y::int)";
wenzelm@11704
   136
test "-2 * x <= -1 * (y::int)";
wenzelm@11704
   137
test "-x < -23 * (y::int)";
wenzelm@11704
   138
test "-x <= -23 * (y::int)";
paulson@10536
   139
*)
paulson@10536
   140
paulson@10703
   141
paulson@10703
   142
(** Declarations for ExtractCommonTerm **)
paulson@10703
   143
paulson@10703
   144
local
paulson@10703
   145
  open Int_Numeral_Simprocs
paulson@10703
   146
in
paulson@10703
   147
paulson@10703
   148
paulson@10703
   149
(*this version ALWAYS includes a trailing one*)
paulson@10703
   150
fun long_mk_prod []        = one
paulson@10703
   151
  | long_mk_prod (t :: ts) = mk_times (t, mk_prod ts);
paulson@10703
   152
paulson@10703
   153
(*Find first term that matches u*)
wenzelm@13462
   154
fun find_first past u []         = raise TERM("find_first", [])
paulson@10703
   155
  | find_first past u (t::terms) =
wenzelm@13462
   156
        if u aconv t then (rev past @ terms)
paulson@10703
   157
        else find_first (t::past) u terms
wenzelm@13462
   158
        handle TERM _ => find_first (t::past) u terms;
paulson@10703
   159
paulson@10703
   160
(*Final simplification: cancel + and *  *)
wenzelm@13462
   161
fun cancel_simplify_meta_eq cancel_th th =
wenzelm@13462
   162
    Int_Numeral_Simprocs.simplify_meta_eq
wenzelm@13462
   163
        [zmult_1, zmult_1_right]
paulson@10703
   164
        (([th, cancel_th]) MRS trans);
paulson@10703
   165
paulson@10703
   166
structure CancelFactorCommon =
paulson@10703
   167
  struct
wenzelm@13462
   168
  val mk_sum            = long_mk_prod
wenzelm@13462
   169
  val dest_sum          = dest_prod
wenzelm@13462
   170
  val mk_coeff          = mk_coeff
wenzelm@13462
   171
  val dest_coeff        = dest_coeff
wenzelm@13462
   172
  val find_first        = find_first []
paulson@10703
   173
  val trans_tac         = trans_tac
paulson@10703
   174
  val norm_tac = ALLGOALS (simp_tac (HOL_ss addsimps mult_1s@zmult_ac))
paulson@10703
   175
  end;
paulson@10703
   176
paulson@10703
   177
structure EqCancelFactor = ExtractCommonTermFun
paulson@10703
   178
 (open CancelFactorCommon
wenzelm@13485
   179
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10703
   180
  val mk_bal   = HOLogic.mk_eq
paulson@10703
   181
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
paulson@10703
   182
  val simplify_meta_eq  = cancel_simplify_meta_eq int_mult_eq_cancel1
paulson@10703
   183
);
paulson@10703
   184
paulson@10703
   185
structure DivideCancelFactor = ExtractCommonTermFun
paulson@10703
   186
 (open CancelFactorCommon
wenzelm@13485
   187
  val prove_conv = Bin_Simprocs.prove_conv
paulson@10703
   188
  val mk_bal   = HOLogic.mk_binop "Divides.op div"
paulson@10703
   189
  val dest_bal = HOLogic.dest_bin "Divides.op div" HOLogic.intT
paulson@10703
   190
  val simplify_meta_eq  = cancel_simplify_meta_eq int_mult_div_cancel_disj
paulson@10703
   191
);
paulson@10703
   192
wenzelm@13462
   193
val int_cancel_factor =
paulson@11868
   194
  map Bin_Simprocs.prep_simproc
wenzelm@13462
   195
   [("int_eq_cancel_factor", ["(l::int) * m = n", "(l::int) = m * n"], EqCancelFactor.proc),
wenzelm@13462
   196
    ("int_divide_cancel_factor", ["((l::int) * m) div n", "(l::int) div (m * n)"],
paulson@10703
   197
     DivideCancelFactor.proc)];
paulson@10703
   198
paulson@10703
   199
end;
paulson@10703
   200
paulson@10703
   201
Addsimprocs int_cancel_factor;
paulson@10703
   202
paulson@10703
   203
paulson@10703
   204
(*examples:
paulson@10703
   205
print_depth 22;
paulson@10703
   206
set timing;
paulson@10703
   207
set trace_simp;
wenzelm@13462
   208
fun test s = (Goal s; by (Asm_simp_tac 1));
paulson@10703
   209
paulson@10703
   210
test "x*k = k*(y::int)";
wenzelm@13462
   211
test "k = k*(y::int)";
paulson@10703
   212
test "a*(b*c) = (b::int)";
paulson@10703
   213
test "a*(b*c) = d*(b::int)*(x*a)";
paulson@10703
   214
paulson@10703
   215
test "(x*k) div (k*(y::int)) = (uu::int)";
wenzelm@13462
   216
test "(k) div (k*(y::int)) = (uu::int)";
paulson@10703
   217
test "(a*(b*c)) div ((b::int)) = (uu::int)";
paulson@10703
   218
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
paulson@10703
   219
*)
paulson@10703
   220