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