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