src/HOL/Integ/int_factor_simprocs.ML
author paulson
Thu Dec 21 10:11:10 2000 +0100 (2000-12-21)
changeset 10713 69c9fc1d11f2
parent 10703 ba98f00cec4f
child 11701 3d51fbf81c17
permissions -rw-r--r--
simproc bug fix: negative literals and large terms
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@10536
    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);
paulson@10536
    15
by Auto_tac;  
paulson@10536
    16
qed "int_mult_le_cancel1";
paulson@10536
    17
paulson@10536
    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);
paulson@10536
    20
by Auto_tac;  
paulson@10536
    21
qed "int_mult_less_cancel1";
paulson@10536
    22
paulson@10536
    23
Goal "!!k::int. (k*m = k*n) = (k = #0 | m=n)";
paulson@10536
    24
by Auto_tac;  
paulson@10536
    25
qed "int_mult_eq_cancel1";
paulson@10536
    26
paulson@10536
    27
Goal "!!k::int. k~=#0 ==> (k*m) div (k*n) = (m div n)";
paulson@10536
    28
by (stac zdiv_zmult_zmult1 1); 
paulson@10536
    29
by Auto_tac;  
paulson@10536
    30
qed "int_mult_div_cancel1";
paulson@10536
    31
paulson@10703
    32
(*For ExtractCommonTermFun, cancelling common factors*)
paulson@10703
    33
Goal "(k*m) div (k*n) = (if k = (#0::int) then #0 else m div n)";
paulson@10703
    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
paulson@10536
    43
  val mk_coeff		= mk_coeff
paulson@10536
    44
  val dest_coeff	= dest_coeff 1
paulson@10536
    45
  val trans_tac         = trans_tac
paulson@10713
    46
  val norm_tac = 
paulson@10713
    47
     ALLGOALS (simp_tac (HOL_ss addsimps mult_1s))
paulson@10713
    48
     THEN ALLGOALS (simp_tac (HOL_ss addsimps bin_simps@int_mult_minus_simps))
paulson@10713
    49
     THEN ALLGOALS
paulson@10713
    50
            (simp_tac (HOL_ss addsimps int_minus_from_mult_simps@zmult_ac))
paulson@10536
    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@10536
    57
  val prove_conv = 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@10536
    66
  val prove_conv = 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@10536
    75
  val prove_conv = 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@10536
    84
  val prove_conv = 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
paulson@10536
    91
val int_cancel_numeral_factors = 
paulson@10536
    92
  map prep_simproc
paulson@10536
    93
   [("inteq_cancel_numeral_factors",
paulson@10536
    94
     prep_pats ["(l::int) * m = n", "(l::int) = m * n"], 
paulson@10536
    95
     EqCancelNumeralFactor.proc),
paulson@10536
    96
    ("intless_cancel_numeral_factors", 
paulson@10536
    97
     prep_pats ["(l::int) * m < n", "(l::int) < m * n"], 
paulson@10536
    98
     LessCancelNumeralFactor.proc),
paulson@10536
    99
    ("intle_cancel_numeral_factors", 
paulson@10536
   100
     prep_pats ["(l::int) * m <= n", "(l::int) <= m * n"], 
paulson@10536
   101
     LeCancelNumeralFactor.proc),
paulson@10536
   102
    ("intdiv_cancel_numeral_factors", 
paulson@10536
   103
     prep_pats ["((l::int) * m) div n", "(l::int) div (m * n)"], 
paulson@10536
   104
     DivCancelNumeralFactor.proc)];
paulson@10536
   105
paulson@10536
   106
end;
paulson@10536
   107
paulson@10536
   108
Addsimprocs int_cancel_numeral_factors;
paulson@10536
   109
paulson@10536
   110
paulson@10536
   111
(*examples:
paulson@10536
   112
print_depth 22;
paulson@10536
   113
set timing;
paulson@10536
   114
set trace_simp;
paulson@10536
   115
fun test s = (Goal s; by (Simp_tac 1)); 
paulson@10536
   116
paulson@10536
   117
test "#9*x = #12 * (y::int)";
paulson@10536
   118
test "(#9*x) div (#12 * (y::int)) = z";
paulson@10536
   119
test "#9*x < #12 * (y::int)";
paulson@10536
   120
test "#9*x <= #12 * (y::int)";
paulson@10536
   121
paulson@10536
   122
test "#-99*x = #132 * (y::int)";
paulson@10536
   123
test "(#-99*x) div (#132 * (y::int)) = z";
paulson@10536
   124
test "#-99*x < #132 * (y::int)";
paulson@10536
   125
test "#-99*x <= #132 * (y::int)";
paulson@10536
   126
paulson@10536
   127
test "#999*x = #-396 * (y::int)";
paulson@10536
   128
test "(#999*x) div (#-396 * (y::int)) = z";
paulson@10536
   129
test "#999*x < #-396 * (y::int)";
paulson@10536
   130
test "#999*x <= #-396 * (y::int)";
paulson@10536
   131
paulson@10536
   132
test "#-99*x = #-81 * (y::int)";
paulson@10536
   133
test "(#-99*x) div (#-81 * (y::int)) = z";
paulson@10536
   134
test "#-99*x <= #-81 * (y::int)";
paulson@10536
   135
test "#-99*x < #-81 * (y::int)";
paulson@10536
   136
paulson@10536
   137
test "#-2 * x = #-1 * (y::int)";
paulson@10536
   138
test "#-2 * x = -(y::int)";
paulson@10536
   139
test "(#-2 * x) div (#-1 * (y::int)) = z";
paulson@10536
   140
test "#-2 * x < -(y::int)";
paulson@10536
   141
test "#-2 * x <= #-1 * (y::int)";
paulson@10536
   142
test "-x < #-23 * (y::int)";
paulson@10536
   143
test "-x <= #-23 * (y::int)";
paulson@10536
   144
*)
paulson@10536
   145
paulson@10703
   146
paulson@10703
   147
(** Declarations for ExtractCommonTerm **)
paulson@10703
   148
paulson@10703
   149
local
paulson@10703
   150
  open Int_Numeral_Simprocs
paulson@10703
   151
in
paulson@10703
   152
paulson@10703
   153
paulson@10703
   154
(*this version ALWAYS includes a trailing one*)
paulson@10703
   155
fun long_mk_prod []        = one
paulson@10703
   156
  | long_mk_prod (t :: ts) = mk_times (t, mk_prod ts);
paulson@10703
   157
paulson@10703
   158
(*Find first term that matches u*)
paulson@10703
   159
fun find_first past u []         = raise TERM("find_first", []) 
paulson@10703
   160
  | find_first past u (t::terms) =
paulson@10703
   161
	if u aconv t then (rev past @ terms)
paulson@10703
   162
        else find_first (t::past) u terms
paulson@10703
   163
	handle TERM _ => find_first (t::past) u terms;
paulson@10703
   164
paulson@10703
   165
(*Final simplification: cancel + and *  *)
paulson@10703
   166
fun cancel_simplify_meta_eq cancel_th th = 
paulson@10703
   167
    Int_Numeral_Simprocs.simplify_meta_eq 
paulson@10703
   168
        [zmult_1, zmult_1_right] 
paulson@10703
   169
        (([th, cancel_th]) MRS trans);
paulson@10703
   170
paulson@10703
   171
structure CancelFactorCommon =
paulson@10703
   172
  struct
paulson@10703
   173
  val mk_sum    	= long_mk_prod
paulson@10703
   174
  val dest_sum		= dest_prod
paulson@10703
   175
  val mk_coeff		= mk_coeff
paulson@10703
   176
  val dest_coeff	= dest_coeff
paulson@10703
   177
  val find_first	= find_first []
paulson@10703
   178
  val trans_tac         = trans_tac
paulson@10703
   179
  val norm_tac = ALLGOALS (simp_tac (HOL_ss addsimps mult_1s@zmult_ac))
paulson@10703
   180
  end;
paulson@10703
   181
paulson@10703
   182
structure EqCancelFactor = ExtractCommonTermFun
paulson@10703
   183
 (open CancelFactorCommon
paulson@10703
   184
  val prove_conv = prove_conv "int_eq_cancel_factor"
paulson@10703
   185
  val mk_bal   = HOLogic.mk_eq
paulson@10703
   186
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.intT
paulson@10703
   187
  val simplify_meta_eq  = cancel_simplify_meta_eq int_mult_eq_cancel1
paulson@10703
   188
);
paulson@10703
   189
paulson@10703
   190
structure DivideCancelFactor = ExtractCommonTermFun
paulson@10703
   191
 (open CancelFactorCommon
paulson@10703
   192
  val prove_conv = prove_conv "int_divide_cancel_factor"
paulson@10703
   193
  val mk_bal   = HOLogic.mk_binop "Divides.op div"
paulson@10703
   194
  val dest_bal = HOLogic.dest_bin "Divides.op div" HOLogic.intT
paulson@10703
   195
  val simplify_meta_eq  = cancel_simplify_meta_eq int_mult_div_cancel_disj
paulson@10703
   196
);
paulson@10703
   197
paulson@10703
   198
val int_cancel_factor = 
paulson@10703
   199
  map prep_simproc
paulson@10703
   200
   [("int_eq_cancel_factor",
paulson@10703
   201
     prep_pats ["(l::int) * m = n", "(l::int) = m * n"], 
paulson@10703
   202
     EqCancelFactor.proc),
paulson@10703
   203
    ("int_divide_cancel_factor", 
paulson@10703
   204
     prep_pats ["((l::int) * m) div n", "(l::int) div (m * n)"], 
paulson@10703
   205
     DivideCancelFactor.proc)];
paulson@10703
   206
paulson@10703
   207
end;
paulson@10703
   208
paulson@10703
   209
Addsimprocs int_cancel_factor;
paulson@10703
   210
paulson@10703
   211
paulson@10703
   212
(*examples:
paulson@10703
   213
print_depth 22;
paulson@10703
   214
set timing;
paulson@10703
   215
set trace_simp;
paulson@10703
   216
fun test s = (Goal s; by (Asm_simp_tac 1)); 
paulson@10703
   217
paulson@10703
   218
test "x*k = k*(y::int)";
paulson@10703
   219
test "k = k*(y::int)"; 
paulson@10703
   220
test "a*(b*c) = (b::int)";
paulson@10703
   221
test "a*(b*c) = d*(b::int)*(x*a)";
paulson@10703
   222
paulson@10703
   223
test "(x*k) div (k*(y::int)) = (uu::int)";
paulson@10703
   224
test "(k) div (k*(y::int)) = (uu::int)"; 
paulson@10703
   225
test "(a*(b*c)) div ((b::int)) = (uu::int)";
paulson@10703
   226
test "(a*(b*c)) div (d*(b::int)*(x*a)) = (uu::int)";
paulson@10703
   227
*)
paulson@10703
   228