src/ZF/int_arith.ML
author wenzelm
Tue Sep 18 16:08:00 2007 +0200 (2007-09-18)
changeset 24630 351a308ab58d
parent 23146 0bc590051d95
child 24893 b8ef7afe3a6b
permissions -rw-r--r--
simplified type int (eliminated IntInf.int, integer);
wenzelm@23146
     1
(*  Title:      ZF/int_arith.ML
wenzelm@23146
     2
    ID:         $Id$
wenzelm@23146
     3
    Author:     Larry Paulson
wenzelm@23146
     4
    Copyright   2000  University of Cambridge
wenzelm@23146
     5
wenzelm@23146
     6
Simprocs for linear arithmetic.
wenzelm@23146
     7
*)
wenzelm@23146
     8
wenzelm@23146
     9
wenzelm@23146
    10
(** To simplify inequalities involving integer negation and literals,
wenzelm@23146
    11
    such as -x = #3
wenzelm@23146
    12
**)
wenzelm@23146
    13
wenzelm@23146
    14
Addsimps [inst "y" "integ_of(?w)" zminus_equation,
wenzelm@23146
    15
          inst "x" "integ_of(?w)" equation_zminus];
wenzelm@23146
    16
wenzelm@23146
    17
AddIffs [inst "y" "integ_of(?w)" zminus_zless,
wenzelm@23146
    18
         inst "x" "integ_of(?w)" zless_zminus];
wenzelm@23146
    19
wenzelm@23146
    20
AddIffs [inst "y" "integ_of(?w)" zminus_zle,
wenzelm@23146
    21
         inst "x" "integ_of(?w)" zle_zminus];
wenzelm@23146
    22
wenzelm@23146
    23
Addsimps [inst "s" "integ_of(?w)" (thm "Let_def")];
wenzelm@23146
    24
wenzelm@23146
    25
(*** Simprocs for numeric literals ***)
wenzelm@23146
    26
wenzelm@23146
    27
(** Combining of literal coefficients in sums of products **)
wenzelm@23146
    28
wenzelm@23146
    29
Goal "(x $< y) <-> (x$-y $< #0)";
wenzelm@23146
    30
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@23146
    31
qed "zless_iff_zdiff_zless_0";
wenzelm@23146
    32
wenzelm@23146
    33
Goal "[| x: int; y: int |] ==> (x = y) <-> (x$-y = #0)";
wenzelm@23146
    34
by (asm_simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@23146
    35
qed "eq_iff_zdiff_eq_0";
wenzelm@23146
    36
wenzelm@23146
    37
Goal "(x $<= y) <-> (x$-y $<= #0)";
wenzelm@23146
    38
by (asm_simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@23146
    39
qed "zle_iff_zdiff_zle_0";
wenzelm@23146
    40
wenzelm@23146
    41
wenzelm@23146
    42
(** For combine_numerals **)
wenzelm@23146
    43
wenzelm@23146
    44
Goal "i$*u $+ (j$*u $+ k) = (i$+j)$*u $+ k";
wenzelm@23146
    45
by (simp_tac (simpset() addsimps [zadd_zmult_distrib]@zadd_ac) 1);
wenzelm@23146
    46
qed "left_zadd_zmult_distrib";
wenzelm@23146
    47
wenzelm@23146
    48
wenzelm@23146
    49
(** For cancel_numerals **)
wenzelm@23146
    50
wenzelm@23146
    51
val rel_iff_rel_0_rls = map (inst "y" "?u$+?v")
wenzelm@23146
    52
                          [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0,
wenzelm@23146
    53
                           zle_iff_zdiff_zle_0] @
wenzelm@23146
    54
                        map (inst "y" "n")
wenzelm@23146
    55
                          [zless_iff_zdiff_zless_0, eq_iff_zdiff_eq_0,
wenzelm@23146
    56
                           zle_iff_zdiff_zle_0];
wenzelm@23146
    57
wenzelm@23146
    58
Goal "(i$*u $+ m = j$*u $+ n) <-> ((i$-j)$*u $+ m = intify(n))";
wenzelm@23146
    59
by (simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
wenzelm@23146
    60
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@23146
    61
by (simp_tac (simpset() addsimps zadd_ac) 1);
wenzelm@23146
    62
qed "eq_add_iff1";
wenzelm@23146
    63
wenzelm@23146
    64
Goal "(i$*u $+ m = j$*u $+ n) <-> (intify(m) = (j$-i)$*u $+ n)";
wenzelm@23146
    65
by (simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
wenzelm@23146
    66
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@23146
    67
by (simp_tac (simpset() addsimps zadd_ac) 1);
wenzelm@23146
    68
qed "eq_add_iff2";
wenzelm@23146
    69
wenzelm@23146
    70
Goal "(i$*u $+ m $< j$*u $+ n) <-> ((i$-j)$*u $+ m $< n)";
wenzelm@23146
    71
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
wenzelm@23146
    72
                                     zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@23146
    73
qed "less_add_iff1";
wenzelm@23146
    74
wenzelm@23146
    75
Goal "(i$*u $+ m $< j$*u $+ n) <-> (m $< (j$-i)$*u $+ n)";
wenzelm@23146
    76
by (asm_simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]@
wenzelm@23146
    77
                                     zadd_ac@rel_iff_rel_0_rls) 1);
wenzelm@23146
    78
qed "less_add_iff2";
wenzelm@23146
    79
wenzelm@23146
    80
Goal "(i$*u $+ m $<= j$*u $+ n) <-> ((i$-j)$*u $+ m $<= n)";
wenzelm@23146
    81
by (simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
wenzelm@23146
    82
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@23146
    83
by (simp_tac (simpset() addsimps zadd_ac) 1);
wenzelm@23146
    84
qed "le_add_iff1";
wenzelm@23146
    85
wenzelm@23146
    86
Goal "(i$*u $+ m $<= j$*u $+ n) <-> (m $<= (j$-i)$*u $+ n)";
wenzelm@23146
    87
by (simp_tac (simpset() addsimps [zdiff_def, zadd_zmult_distrib]) 1);
wenzelm@23146
    88
by (simp_tac (simpset() addsimps zcompare_rls) 1);
wenzelm@23146
    89
by (simp_tac (simpset() addsimps zadd_ac) 1);
wenzelm@23146
    90
qed "le_add_iff2";
wenzelm@23146
    91
wenzelm@23146
    92
wenzelm@23146
    93
structure Int_Numeral_Simprocs =
wenzelm@23146
    94
struct
wenzelm@23146
    95
wenzelm@23146
    96
(*Utilities*)
wenzelm@23146
    97
wenzelm@23146
    98
val integ_of_const = Const ("Bin.integ_of", iT --> iT);
wenzelm@23146
    99
wenzelm@23146
   100
fun mk_numeral n = integ_of_const $ NumeralSyntax.mk_bin n;
wenzelm@23146
   101
wenzelm@23146
   102
(*Decodes a binary INTEGER*)
wenzelm@23146
   103
fun dest_numeral (Const("Bin.integ_of", _) $ w) =
wenzelm@23146
   104
     (NumeralSyntax.dest_bin w
wenzelm@23146
   105
      handle Match => raise TERM("Int_Numeral_Simprocs.dest_numeral:1", [w]))
wenzelm@23146
   106
  | dest_numeral t =  raise TERM("Int_Numeral_Simprocs.dest_numeral:2", [t]);
wenzelm@23146
   107
wenzelm@23146
   108
fun find_first_numeral past (t::terms) =
wenzelm@23146
   109
        ((dest_numeral t, rev past @ terms)
wenzelm@23146
   110
         handle TERM _ => find_first_numeral (t::past) terms)
wenzelm@23146
   111
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
wenzelm@23146
   112
wenzelm@23146
   113
val zero = mk_numeral 0;
wenzelm@23146
   114
val mk_plus = FOLogic.mk_binop "Int.zadd";
wenzelm@23146
   115
wenzelm@23146
   116
val iT = Ind_Syntax.iT;
wenzelm@23146
   117
wenzelm@23146
   118
val zminus_const = Const ("Int.zminus", iT --> iT);
wenzelm@23146
   119
wenzelm@23146
   120
(*Thus mk_sum[t] yields t+#0; longer sums don't have a trailing zero*)
wenzelm@23146
   121
fun mk_sum []        = zero
wenzelm@23146
   122
  | mk_sum [t,u]     = mk_plus (t, u)
wenzelm@23146
   123
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@23146
   124
wenzelm@23146
   125
(*this version ALWAYS includes a trailing zero*)
wenzelm@23146
   126
fun long_mk_sum []        = zero
wenzelm@23146
   127
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@23146
   128
wenzelm@23146
   129
val dest_plus = FOLogic.dest_bin "Int.zadd" iT;
wenzelm@23146
   130
wenzelm@23146
   131
(*decompose additions AND subtractions as a sum*)
wenzelm@23146
   132
fun dest_summing (pos, Const ("Int.zadd", _) $ t $ u, ts) =
wenzelm@23146
   133
        dest_summing (pos, t, dest_summing (pos, u, ts))
wenzelm@23146
   134
  | dest_summing (pos, Const ("Int.zdiff", _) $ t $ u, ts) =
wenzelm@23146
   135
        dest_summing (pos, t, dest_summing (not pos, u, ts))
wenzelm@23146
   136
  | dest_summing (pos, t, ts) =
wenzelm@23146
   137
        if pos then t::ts else zminus_const$t :: ts;
wenzelm@23146
   138
wenzelm@23146
   139
fun dest_sum t = dest_summing (true, t, []);
wenzelm@23146
   140
wenzelm@23146
   141
val mk_diff = FOLogic.mk_binop "Int.zdiff";
wenzelm@23146
   142
val dest_diff = FOLogic.dest_bin "Int.zdiff" iT;
wenzelm@23146
   143
wenzelm@23146
   144
val one = mk_numeral 1;
wenzelm@23146
   145
val mk_times = FOLogic.mk_binop "Int.zmult";
wenzelm@23146
   146
wenzelm@23146
   147
fun mk_prod [] = one
wenzelm@23146
   148
  | mk_prod [t] = t
wenzelm@23146
   149
  | mk_prod (t :: ts) = if t = one then mk_prod ts
wenzelm@23146
   150
                        else mk_times (t, mk_prod ts);
wenzelm@23146
   151
wenzelm@23146
   152
val dest_times = FOLogic.dest_bin "Int.zmult" iT;
wenzelm@23146
   153
wenzelm@23146
   154
fun dest_prod t =
wenzelm@23146
   155
      let val (t,u) = dest_times t
wenzelm@23146
   156
      in  dest_prod t @ dest_prod u  end
wenzelm@23146
   157
      handle TERM _ => [t];
wenzelm@23146
   158
wenzelm@23146
   159
(*DON'T do the obvious simplifications; that would create special cases*)
wenzelm@23146
   160
fun mk_coeff (k, t) = mk_times (mk_numeral k, t);
wenzelm@23146
   161
wenzelm@23146
   162
(*Express t as a product of (possibly) a numeral with other sorted terms*)
wenzelm@23146
   163
fun dest_coeff sign (Const ("Int.zminus", _) $ t) = dest_coeff (~sign) t
wenzelm@23146
   164
  | dest_coeff sign t =
wenzelm@23146
   165
    let val ts = sort Term.term_ord (dest_prod t)
wenzelm@23146
   166
        val (n, ts') = find_first_numeral [] ts
wenzelm@23146
   167
                          handle TERM _ => (1, ts)
wenzelm@23146
   168
    in (sign*n, mk_prod ts') end;
wenzelm@23146
   169
wenzelm@23146
   170
(*Find first coefficient-term THAT MATCHES u*)
wenzelm@23146
   171
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
wenzelm@23146
   172
  | find_first_coeff past u (t::terms) =
wenzelm@23146
   173
        let val (n,u') = dest_coeff 1 t
wenzelm@23146
   174
        in  if u aconv u' then (n, rev past @ terms)
wenzelm@23146
   175
                          else find_first_coeff (t::past) u terms
wenzelm@23146
   176
        end
wenzelm@23146
   177
        handle TERM _ => find_first_coeff (t::past) u terms;
wenzelm@23146
   178
wenzelm@23146
   179
wenzelm@23146
   180
(*Simplify #1*n and n*#1 to n*)
wenzelm@23146
   181
val add_0s = [zadd_0_intify, zadd_0_right_intify];
wenzelm@23146
   182
wenzelm@23146
   183
val mult_1s = [zmult_1_intify, zmult_1_right_intify,
wenzelm@23146
   184
               zmult_minus1, zmult_minus1_right];
wenzelm@23146
   185
wenzelm@23146
   186
val tc_rules = [integ_of_type, intify_in_int,
wenzelm@23146
   187
                int_of_type, zadd_type, zdiff_type, zmult_type] @ 
wenzelm@23146
   188
               thms "bin.intros";
wenzelm@23146
   189
val intifys = [intify_ident, zadd_intify1, zadd_intify2,
wenzelm@23146
   190
               zdiff_intify1, zdiff_intify2, zmult_intify1, zmult_intify2,
wenzelm@23146
   191
               zless_intify1, zless_intify2, zle_intify1, zle_intify2];
wenzelm@23146
   192
wenzelm@23146
   193
(*To perform binary arithmetic*)
wenzelm@23146
   194
val bin_simps = [add_integ_of_left] @ bin_arith_simps @ bin_rel_simps;
wenzelm@23146
   195
wenzelm@23146
   196
(*To evaluate binary negations of coefficients*)
wenzelm@23146
   197
val zminus_simps = NCons_simps @
wenzelm@23146
   198
                   [integ_of_minus RS sym,
wenzelm@23146
   199
                    bin_minus_1, bin_minus_0, bin_minus_Pls, bin_minus_Min,
wenzelm@23146
   200
                    bin_pred_1, bin_pred_0, bin_pred_Pls, bin_pred_Min];
wenzelm@23146
   201
wenzelm@23146
   202
(*To let us treat subtraction as addition*)
wenzelm@23146
   203
val diff_simps = [zdiff_def, zminus_zadd_distrib, zminus_zminus];
wenzelm@23146
   204
wenzelm@23146
   205
(*push the unary minus down: - x * y = x * - y *)
wenzelm@23146
   206
val int_minus_mult_eq_1_to_2 =
wenzelm@23146
   207
    [zmult_zminus, zmult_zminus_right RS sym] MRS trans |> standard;
wenzelm@23146
   208
wenzelm@23146
   209
(*to extract again any uncancelled minuses*)
wenzelm@23146
   210
val int_minus_from_mult_simps =
wenzelm@23146
   211
    [zminus_zminus, zmult_zminus, zmult_zminus_right];
wenzelm@23146
   212
wenzelm@23146
   213
(*combine unary minus with numeric literals, however nested within a product*)
wenzelm@23146
   214
val int_mult_minus_simps =
wenzelm@23146
   215
    [zmult_assoc, zmult_zminus RS sym, int_minus_mult_eq_1_to_2];
wenzelm@23146
   216
wenzelm@23146
   217
fun prep_simproc (name, pats, proc) =
wenzelm@23146
   218
  Simplifier.simproc (the_context ()) name pats proc;
wenzelm@23146
   219
wenzelm@23146
   220
structure CancelNumeralsCommon =
wenzelm@23146
   221
  struct
wenzelm@23146
   222
  val mk_sum            = (fn T:typ => mk_sum)
wenzelm@23146
   223
  val dest_sum          = dest_sum
wenzelm@23146
   224
  val mk_coeff          = mk_coeff
wenzelm@23146
   225
  val dest_coeff        = dest_coeff 1
wenzelm@23146
   226
  val find_first_coeff  = find_first_coeff []
wenzelm@23146
   227
  fun trans_tac _       = ArithData.gen_trans_tac iff_trans
wenzelm@23146
   228
wenzelm@23146
   229
  val norm_ss1 = ZF_ss addsimps add_0s @ mult_1s @ diff_simps @ zminus_simps @ zadd_ac
wenzelm@23146
   230
  val norm_ss2 = ZF_ss addsimps bin_simps @ int_mult_minus_simps @ intifys
wenzelm@23146
   231
  val norm_ss3 = ZF_ss addsimps int_minus_from_mult_simps @ zadd_ac @ zmult_ac @ tc_rules @ intifys
wenzelm@23146
   232
  fun norm_tac ss =
wenzelm@23146
   233
    ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23146
   234
    THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23146
   235
    THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss3))
wenzelm@23146
   236
wenzelm@23146
   237
  val numeral_simp_ss = ZF_ss addsimps add_0s @ bin_simps @ tc_rules @ intifys
wenzelm@23146
   238
  fun numeral_simp_tac ss =
wenzelm@23146
   239
    ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23146
   240
    THEN ALLGOALS (SIMPSET' (fn simpset => asm_simp_tac (Simplifier.inherit_context ss simpset)))
wenzelm@23146
   241
  val simplify_meta_eq  = ArithData.simplify_meta_eq (add_0s @ mult_1s)
wenzelm@23146
   242
  end;
wenzelm@23146
   243
wenzelm@23146
   244
wenzelm@23146
   245
structure EqCancelNumerals = CancelNumeralsFun
wenzelm@23146
   246
 (open CancelNumeralsCommon
wenzelm@23146
   247
  val prove_conv = ArithData.prove_conv "inteq_cancel_numerals"
wenzelm@23146
   248
  val mk_bal   = FOLogic.mk_eq
wenzelm@23146
   249
  val dest_bal = FOLogic.dest_eq
wenzelm@23146
   250
  val bal_add1 = eq_add_iff1 RS iff_trans
wenzelm@23146
   251
  val bal_add2 = eq_add_iff2 RS iff_trans
wenzelm@23146
   252
);
wenzelm@23146
   253
wenzelm@23146
   254
structure LessCancelNumerals = CancelNumeralsFun
wenzelm@23146
   255
 (open CancelNumeralsCommon
wenzelm@23146
   256
  val prove_conv = ArithData.prove_conv "intless_cancel_numerals"
wenzelm@23146
   257
  val mk_bal   = FOLogic.mk_binrel "Int.zless"
wenzelm@23146
   258
  val dest_bal = FOLogic.dest_bin "Int.zless" iT
wenzelm@23146
   259
  val bal_add1 = less_add_iff1 RS iff_trans
wenzelm@23146
   260
  val bal_add2 = less_add_iff2 RS iff_trans
wenzelm@23146
   261
);
wenzelm@23146
   262
wenzelm@23146
   263
structure LeCancelNumerals = CancelNumeralsFun
wenzelm@23146
   264
 (open CancelNumeralsCommon
wenzelm@23146
   265
  val prove_conv = ArithData.prove_conv "intle_cancel_numerals"
wenzelm@23146
   266
  val mk_bal   = FOLogic.mk_binrel "Int.zle"
wenzelm@23146
   267
  val dest_bal = FOLogic.dest_bin "Int.zle" iT
wenzelm@23146
   268
  val bal_add1 = le_add_iff1 RS iff_trans
wenzelm@23146
   269
  val bal_add2 = le_add_iff2 RS iff_trans
wenzelm@23146
   270
);
wenzelm@23146
   271
wenzelm@23146
   272
val cancel_numerals =
wenzelm@23146
   273
  map prep_simproc
wenzelm@23146
   274
   [("inteq_cancel_numerals",
wenzelm@23146
   275
     ["l $+ m = n", "l = m $+ n",
wenzelm@23146
   276
      "l $- m = n", "l = m $- n",
wenzelm@23146
   277
      "l $* m = n", "l = m $* n"],
wenzelm@23146
   278
     K EqCancelNumerals.proc),
wenzelm@23146
   279
    ("intless_cancel_numerals",
wenzelm@23146
   280
     ["l $+ m $< n", "l $< m $+ n",
wenzelm@23146
   281
      "l $- m $< n", "l $< m $- n",
wenzelm@23146
   282
      "l $* m $< n", "l $< m $* n"],
wenzelm@23146
   283
     K LessCancelNumerals.proc),
wenzelm@23146
   284
    ("intle_cancel_numerals",
wenzelm@23146
   285
     ["l $+ m $<= n", "l $<= m $+ n",
wenzelm@23146
   286
      "l $- m $<= n", "l $<= m $- n",
wenzelm@23146
   287
      "l $* m $<= n", "l $<= m $* n"],
wenzelm@23146
   288
     K LeCancelNumerals.proc)];
wenzelm@23146
   289
wenzelm@23146
   290
wenzelm@23146
   291
(*version without the hyps argument*)
wenzelm@23146
   292
fun prove_conv_nohyps name tacs sg = ArithData.prove_conv name tacs sg [];
wenzelm@23146
   293
wenzelm@23146
   294
structure CombineNumeralsData =
wenzelm@23146
   295
  struct
wenzelm@24630
   296
  type coeff            = int
wenzelm@24630
   297
  val iszero            = (fn x => x = 0)
wenzelm@24630
   298
  val add               = op + 
wenzelm@23146
   299
  val mk_sum            = (fn T:typ => long_mk_sum) (*to work for #2*x $+ #3*x *)
wenzelm@23146
   300
  val dest_sum          = dest_sum
wenzelm@23146
   301
  val mk_coeff          = mk_coeff
wenzelm@23146
   302
  val dest_coeff        = dest_coeff 1
wenzelm@23146
   303
  val left_distrib      = left_zadd_zmult_distrib RS trans
wenzelm@23146
   304
  val prove_conv        = prove_conv_nohyps "int_combine_numerals"
wenzelm@23146
   305
  fun trans_tac _       = ArithData.gen_trans_tac trans
wenzelm@23146
   306
wenzelm@23146
   307
  val norm_ss1 = ZF_ss addsimps add_0s @ mult_1s @ diff_simps @ zminus_simps @ zadd_ac @ intifys
wenzelm@23146
   308
  val norm_ss2 = ZF_ss addsimps bin_simps @ int_mult_minus_simps @ intifys
wenzelm@23146
   309
  val norm_ss3 = ZF_ss addsimps int_minus_from_mult_simps @ zadd_ac @ zmult_ac @ tc_rules @ intifys
wenzelm@23146
   310
  fun norm_tac ss =
wenzelm@23146
   311
    ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23146
   312
    THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23146
   313
    THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss3))
wenzelm@23146
   314
wenzelm@23146
   315
  val numeral_simp_ss = ZF_ss addsimps add_0s @ bin_simps @ tc_rules @ intifys
wenzelm@23146
   316
  fun numeral_simp_tac ss =
wenzelm@23146
   317
    ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23146
   318
  val simplify_meta_eq  = ArithData.simplify_meta_eq (add_0s @ mult_1s)
wenzelm@23146
   319
  end;
wenzelm@23146
   320
wenzelm@23146
   321
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
wenzelm@23146
   322
wenzelm@23146
   323
val combine_numerals =
wenzelm@23146
   324
  prep_simproc ("int_combine_numerals", ["i $+ j", "i $- j"], K CombineNumerals.proc);
wenzelm@23146
   325
wenzelm@23146
   326
wenzelm@23146
   327
wenzelm@23146
   328
(** Constant folding for integer multiplication **)
wenzelm@23146
   329
wenzelm@23146
   330
(*The trick is to regard products as sums, e.g. #3 $* x $* #4 as
wenzelm@23146
   331
  the "sum" of #3, x, #4; the literals are then multiplied*)
wenzelm@23146
   332
wenzelm@23146
   333
wenzelm@23146
   334
structure CombineNumeralsProdData =
wenzelm@23146
   335
  struct
wenzelm@24630
   336
  type coeff            = int
wenzelm@24630
   337
  val iszero            = (fn x => x = 0)
wenzelm@24630
   338
  val add               = op *
wenzelm@23146
   339
  val mk_sum            = (fn T:typ => mk_prod)
wenzelm@23146
   340
  val dest_sum          = dest_prod
wenzelm@23146
   341
  fun mk_coeff(k,t) = if t=one then mk_numeral k
wenzelm@23146
   342
                      else raise TERM("mk_coeff", [])
wenzelm@23146
   343
  fun dest_coeff t = (dest_numeral t, one)  (*We ONLY want pure numerals.*)
wenzelm@23146
   344
  val left_distrib      = zmult_assoc RS sym RS trans
wenzelm@23146
   345
  val prove_conv        = prove_conv_nohyps "int_combine_numerals_prod"
wenzelm@23146
   346
  fun trans_tac _       = ArithData.gen_trans_tac trans
wenzelm@23146
   347
wenzelm@23146
   348
wenzelm@23146
   349
wenzelm@23146
   350
val norm_ss1 = ZF_ss addsimps mult_1s @ diff_simps @ zminus_simps
wenzelm@23146
   351
  val norm_ss2 = ZF_ss addsimps [zmult_zminus_right RS sym] @
wenzelm@23146
   352
    bin_simps @ zmult_ac @ tc_rules @ intifys
wenzelm@23146
   353
  fun norm_tac ss =
wenzelm@23146
   354
    ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23146
   355
    THEN ALLGOALS (asm_simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23146
   356
wenzelm@23146
   357
  val numeral_simp_ss = ZF_ss addsimps bin_simps @ tc_rules @ intifys
wenzelm@23146
   358
  fun numeral_simp_tac ss =
wenzelm@23146
   359
    ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23146
   360
  val simplify_meta_eq  = ArithData.simplify_meta_eq (mult_1s);
wenzelm@23146
   361
  end;
wenzelm@23146
   362
wenzelm@23146
   363
wenzelm@23146
   364
structure CombineNumeralsProd = CombineNumeralsFun(CombineNumeralsProdData);
wenzelm@23146
   365
wenzelm@23146
   366
val combine_numerals_prod =
wenzelm@23146
   367
  prep_simproc ("int_combine_numerals_prod", ["i $* j"], K CombineNumeralsProd.proc);
wenzelm@23146
   368
wenzelm@23146
   369
end;
wenzelm@23146
   370
wenzelm@23146
   371
wenzelm@23146
   372
Addsimprocs Int_Numeral_Simprocs.cancel_numerals;
wenzelm@23146
   373
Addsimprocs [Int_Numeral_Simprocs.combine_numerals,
wenzelm@23146
   374
             Int_Numeral_Simprocs.combine_numerals_prod];
wenzelm@23146
   375
wenzelm@23146
   376
wenzelm@23146
   377
(*examples:*)
wenzelm@23146
   378
(*
wenzelm@23146
   379
print_depth 22;
wenzelm@23146
   380
set timing;
wenzelm@23146
   381
set trace_simp;
wenzelm@23146
   382
fun test s = (Goal s; by (Asm_simp_tac 1));
wenzelm@23146
   383
val sg = #sign (rep_thm (topthm()));
wenzelm@23146
   384
val t = FOLogic.dest_Trueprop (Logic.strip_assums_concl(getgoal 1));
wenzelm@23146
   385
val (t,_) = FOLogic.dest_eq t;
wenzelm@23146
   386
wenzelm@23146
   387
(*combine_numerals_prod (products of separate literals) *)
wenzelm@23146
   388
test "#5 $* x $* #3 = y";
wenzelm@23146
   389
wenzelm@23146
   390
test "y2 $+ ?x42 = y $+ y2";
wenzelm@23146
   391
wenzelm@23146
   392
test "oo : int ==> l $+ (l $+ #2) $+ oo = oo";
wenzelm@23146
   393
wenzelm@23146
   394
test "#9$*x $+ y = x$*#23 $+ z";
wenzelm@23146
   395
test "y $+ x = x $+ z";
wenzelm@23146
   396
wenzelm@23146
   397
test "x : int ==> x $+ y $+ z = x $+ z";
wenzelm@23146
   398
test "x : int ==> y $+ (z $+ x) = z $+ x";
wenzelm@23146
   399
test "z : int ==> x $+ y $+ z = (z $+ y) $+ (x $+ w)";
wenzelm@23146
   400
test "z : int ==> x$*y $+ z = (z $+ y) $+ (y$*x $+ w)";
wenzelm@23146
   401
wenzelm@23146
   402
test "#-3 $* x $+ y $<= x $* #2 $+ z";
wenzelm@23146
   403
test "y $+ x $<= x $+ z";
wenzelm@23146
   404
test "x $+ y $+ z $<= x $+ z";
wenzelm@23146
   405
wenzelm@23146
   406
test "y $+ (z $+ x) $< z $+ x";
wenzelm@23146
   407
test "x $+ y $+ z $< (z $+ y) $+ (x $+ w)";
wenzelm@23146
   408
test "x$*y $+ z $< (z $+ y) $+ (y$*x $+ w)";
wenzelm@23146
   409
wenzelm@23146
   410
test "l $+ #2 $+ #2 $+ #2 $+ (l $+ #2) $+ (oo $+ #2) = uu";
wenzelm@23146
   411
test "u : int ==> #2 $* u = u";
wenzelm@23146
   412
test "(i $+ j $+ #12 $+ k) $- #15 = y";
wenzelm@23146
   413
test "(i $+ j $+ #12 $+ k) $- #5 = y";
wenzelm@23146
   414
wenzelm@23146
   415
test "y $- b $< b";
wenzelm@23146
   416
test "y $- (#3 $* b $+ c) $< b $- #2 $* c";
wenzelm@23146
   417
wenzelm@23146
   418
test "(#2 $* x $- (u $* v) $+ y) $- v $* #3 $* u = w";
wenzelm@23146
   419
test "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u $* #4 = w";
wenzelm@23146
   420
test "(#2 $* x $* u $* v $+ (u $* v) $* #4 $+ y) $- v $* u = w";
wenzelm@23146
   421
test "u $* v $- (x $* u $* v $+ (u $* v) $* #4 $+ y) = w";
wenzelm@23146
   422
wenzelm@23146
   423
test "(i $+ j $+ #12 $+ k) = u $+ #15 $+ y";
wenzelm@23146
   424
test "(i $+ j $* #2 $+ #12 $+ k) = j $+ #5 $+ y";
wenzelm@23146
   425
wenzelm@23146
   426
test "#2 $* y $+ #3 $* z $+ #6 $* w $+ #2 $* y $+ #3 $* z $+ #2 $* u = #2 $* y' $+ #3 $* z' $+ #6 $* w' $+ #2 $* y' $+ #3 $* z' $+ u $+ vv";
wenzelm@23146
   427
wenzelm@23146
   428
test "a $+ $-(b$+c) $+ b = d";
wenzelm@23146
   429
test "a $+ $-(b$+c) $- b = d";
wenzelm@23146
   430
wenzelm@23146
   431
(*negative numerals*)
wenzelm@23146
   432
test "(i $+ j $+ #-2 $+ k) $- (u $+ #5 $+ y) = zz";
wenzelm@23146
   433
test "(i $+ j $+ #-3 $+ k) $< u $+ #5 $+ y";
wenzelm@23146
   434
test "(i $+ j $+ #3 $+ k) $< u $+ #-6 $+ y";
wenzelm@23146
   435
test "(i $+ j $+ #-12 $+ k) $- #15 = y";
wenzelm@23146
   436
test "(i $+ j $+ #12 $+ k) $- #-15 = y";
wenzelm@23146
   437
test "(i $+ j $+ #-12 $+ k) $- #-15 = y";
wenzelm@23146
   438
wenzelm@23146
   439
(*Multiplying separated numerals*)
wenzelm@23146
   440
Goal "#6 $* ($# x $* #2) =  uu";
wenzelm@23146
   441
Goal "#4 $* ($# x $* $# x) $* (#2 $* $# x) =  uu";
wenzelm@23146
   442
*)
wenzelm@23146
   443