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