src/HOL/Tools/nat_numeral_simprocs.ML
author wenzelm
Thu Jul 23 23:12:21 2009 +0200 (2009-07-23)
changeset 32155 e2bf2f73b0c8
parent 32010 cb1a1c94b4cd
child 34974 18b41bba42b5
permissions -rw-r--r--
more @{theory} antiquotations;
haftmann@31068
     1
(* Author: Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@23164
     2
wenzelm@23164
     3
Simprocs for nat numerals.
wenzelm@23164
     4
*)
wenzelm@23164
     5
haftmann@31068
     6
signature NAT_NUMERAL_SIMPROCS =
haftmann@31068
     7
sig
haftmann@31068
     8
  val combine_numerals: simproc
haftmann@31068
     9
  val cancel_numerals: simproc list
haftmann@31068
    10
  val cancel_factors: simproc list
haftmann@31068
    11
  val cancel_numeral_factors: simproc list
haftmann@31068
    12
end;
haftmann@31068
    13
wenzelm@23164
    14
structure Nat_Numeral_Simprocs =
wenzelm@23164
    15
struct
wenzelm@23164
    16
wenzelm@23164
    17
(*Maps n to #n for n = 0, 1, 2*)
haftmann@30496
    18
val numeral_syms = [@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym, @{thm numeral_2_eq_2} RS sym];
wenzelm@23164
    19
val numeral_sym_ss = HOL_ss addsimps numeral_syms;
wenzelm@23164
    20
wenzelm@23164
    21
fun rename_numerals th =
wenzelm@32010
    22
    simplify numeral_sym_ss (Thm.transfer @{theory} th);
wenzelm@23164
    23
wenzelm@23164
    24
(*Utilities*)
wenzelm@23164
    25
wenzelm@23164
    26
fun mk_number n = HOLogic.number_of_const HOLogic.natT $ HOLogic.mk_numeral n;
wenzelm@24630
    27
fun dest_number t = Int.max (0, snd (HOLogic.dest_number t));
wenzelm@23164
    28
wenzelm@23164
    29
fun find_first_numeral past (t::terms) =
wenzelm@23164
    30
        ((dest_number t, t, rev past @ terms)
wenzelm@23164
    31
         handle TERM _ => find_first_numeral (t::past) terms)
wenzelm@23164
    32
  | find_first_numeral past [] = raise TERM("find_first_numeral", []);
wenzelm@23164
    33
wenzelm@23164
    34
val zero = mk_number 0;
wenzelm@23164
    35
val mk_plus = HOLogic.mk_binop @{const_name HOL.plus};
wenzelm@23164
    36
wenzelm@23164
    37
(*Thus mk_sum[t] yields t+0; longer sums don't have a trailing zero*)
wenzelm@23164
    38
fun mk_sum []        = zero
wenzelm@23164
    39
  | mk_sum [t,u]     = mk_plus (t, u)
wenzelm@23164
    40
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@23164
    41
wenzelm@23164
    42
(*this version ALWAYS includes a trailing zero*)
wenzelm@23164
    43
fun long_mk_sum []        = HOLogic.zero
wenzelm@23164
    44
  | long_mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@23164
    45
wenzelm@23164
    46
val dest_plus = HOLogic.dest_bin @{const_name HOL.plus} HOLogic.natT;
wenzelm@23164
    47
wenzelm@23164
    48
wenzelm@23164
    49
(** Other simproc items **)
wenzelm@23164
    50
wenzelm@23164
    51
val bin_simps =
huffman@23471
    52
     [@{thm nat_numeral_0_eq_0} RS sym, @{thm nat_numeral_1_eq_1} RS sym,
huffman@23471
    53
      @{thm add_nat_number_of}, @{thm nat_number_of_add_left}, 
huffman@23471
    54
      @{thm diff_nat_number_of}, @{thm le_number_of_eq_not_less},
huffman@23471
    55
      @{thm mult_nat_number_of}, @{thm nat_number_of_mult_left}, 
huffman@23471
    56
      @{thm less_nat_number_of}, 
huffman@23471
    57
      @{thm Let_number_of}, @{thm nat_number_of}] @
huffman@29039
    58
     @{thms arith_simps} @ @{thms rel_simps} @ @{thms neg_simps};
wenzelm@23164
    59
wenzelm@23164
    60
wenzelm@23164
    61
(*** CancelNumerals simprocs ***)
wenzelm@23164
    62
wenzelm@23164
    63
val one = mk_number 1;
wenzelm@23164
    64
val mk_times = HOLogic.mk_binop @{const_name HOL.times};
wenzelm@23164
    65
wenzelm@23164
    66
fun mk_prod [] = one
wenzelm@23164
    67
  | mk_prod [t] = t
wenzelm@23164
    68
  | mk_prod (t :: ts) = if t = one then mk_prod ts
wenzelm@23164
    69
                        else mk_times (t, mk_prod ts);
wenzelm@23164
    70
wenzelm@23164
    71
val dest_times = HOLogic.dest_bin @{const_name HOL.times} HOLogic.natT;
wenzelm@23164
    72
wenzelm@23164
    73
fun dest_prod t =
wenzelm@23164
    74
      let val (t,u) = dest_times t
wenzelm@23164
    75
      in  dest_prod t @ dest_prod u  end
wenzelm@23164
    76
      handle TERM _ => [t];
wenzelm@23164
    77
wenzelm@23164
    78
(*DON'T do the obvious simplifications; that would create special cases*)
wenzelm@23164
    79
fun mk_coeff (k,t) = mk_times (mk_number k, t);
wenzelm@23164
    80
wenzelm@23164
    81
(*Express t as a product of (possibly) a numeral with other factors, sorted*)
wenzelm@23164
    82
fun dest_coeff t =
wenzelm@29269
    83
    let val ts = sort TermOrd.term_ord (dest_prod t)
wenzelm@23164
    84
        val (n, _, ts') = find_first_numeral [] ts
wenzelm@23164
    85
                          handle TERM _ => (1, one, ts)
wenzelm@23164
    86
    in (n, mk_prod ts') end;
wenzelm@23164
    87
wenzelm@23164
    88
(*Find first coefficient-term THAT MATCHES u*)
wenzelm@23164
    89
fun find_first_coeff past u [] = raise TERM("find_first_coeff", [])
wenzelm@23164
    90
  | find_first_coeff past u (t::terms) =
wenzelm@23164
    91
        let val (n,u') = dest_coeff t
wenzelm@23164
    92
        in  if u aconv u' then (n, rev past @ terms)
wenzelm@23164
    93
                          else find_first_coeff (t::past) u terms
wenzelm@23164
    94
        end
wenzelm@23164
    95
        handle TERM _ => find_first_coeff (t::past) u terms;
wenzelm@23164
    96
wenzelm@23164
    97
wenzelm@23164
    98
(*Split up a sum into the list of its constituent terms, on the way removing any
wenzelm@23164
    99
  Sucs and counting them.*)
wenzelm@23164
   100
fun dest_Suc_sum (Const ("Suc", _) $ t, (k,ts)) = dest_Suc_sum (t, (k+1,ts))
wenzelm@23164
   101
  | dest_Suc_sum (t, (k,ts)) = 
wenzelm@23164
   102
      let val (t1,t2) = dest_plus t
wenzelm@23164
   103
      in  dest_Suc_sum (t1, dest_Suc_sum (t2, (k,ts)))  end
wenzelm@23164
   104
      handle TERM _ => (k, t::ts);
wenzelm@23164
   105
wenzelm@23164
   106
(*Code for testing whether numerals are already used in the goal*)
haftmann@25919
   107
fun is_numeral (Const(@{const_name Int.number_of}, _) $ w) = true
wenzelm@23164
   108
  | is_numeral _ = false;
wenzelm@23164
   109
wenzelm@23164
   110
fun prod_has_numeral t = exists is_numeral (dest_prod t);
wenzelm@23164
   111
wenzelm@23164
   112
(*The Sucs found in the term are converted to a binary numeral. If relaxed is false,
wenzelm@23164
   113
  an exception is raised unless the original expression contains at least one
wenzelm@23164
   114
  numeral in a coefficient position.  This prevents nat_combine_numerals from 
wenzelm@23164
   115
  introducing numerals to goals.*)
wenzelm@23164
   116
fun dest_Sucs_sum relaxed t = 
wenzelm@23164
   117
  let val (k,ts) = dest_Suc_sum (t,(0,[]))
wenzelm@23164
   118
  in
wenzelm@23164
   119
     if relaxed orelse exists prod_has_numeral ts then 
wenzelm@23164
   120
       if k=0 then ts
wenzelm@24630
   121
       else mk_number k :: ts
wenzelm@23164
   122
     else raise TERM("Nat_Numeral_Simprocs.dest_Sucs_sum", [t])
wenzelm@23164
   123
  end;
wenzelm@23164
   124
wenzelm@23164
   125
wenzelm@23164
   126
(*Simplify 1*n and n*1 to n*)
haftmann@23881
   127
val add_0s  = map rename_numerals [@{thm add_0}, @{thm add_0_right}];
wenzelm@23164
   128
val mult_1s = map rename_numerals [@{thm nat_mult_1}, @{thm nat_mult_1_right}];
wenzelm@23164
   129
wenzelm@23164
   130
(*Final simplification: cancel + and *; replace Numeral0 by 0 and Numeral1 by 1*)
wenzelm@23164
   131
wenzelm@23164
   132
(*And these help the simproc return False when appropriate, which helps
wenzelm@23164
   133
  the arith prover.*)
haftmann@23881
   134
val contra_rules = [@{thm add_Suc}, @{thm add_Suc_right}, @{thm Zero_not_Suc},
haftmann@23881
   135
  @{thm Suc_not_Zero}, @{thm le_0_eq}];
wenzelm@23164
   136
wenzelm@23164
   137
val simplify_meta_eq =
haftmann@30518
   138
    Arith_Data.simplify_meta_eq
huffman@23471
   139
        ([@{thm nat_numeral_0_eq_0}, @{thm numeral_1_eq_Suc_0}, @{thm add_0}, @{thm add_0_right},
huffman@23471
   140
          @{thm mult_0}, @{thm mult_0_right}, @{thm mult_1}, @{thm mult_1_right}] @ contra_rules);
wenzelm@23164
   141
wenzelm@23164
   142
wenzelm@23164
   143
(*** Applying CancelNumeralsFun ***)
wenzelm@23164
   144
wenzelm@23164
   145
structure CancelNumeralsCommon =
wenzelm@23164
   146
  struct
wenzelm@23164
   147
  val mk_sum            = (fn T:typ => mk_sum)
wenzelm@23164
   148
  val dest_sum          = dest_Sucs_sum true
wenzelm@23164
   149
  val mk_coeff          = mk_coeff
wenzelm@23164
   150
  val dest_coeff        = dest_coeff
wenzelm@23164
   151
  val find_first_coeff  = find_first_coeff []
wenzelm@31368
   152
  fun trans_tac _       = Arith_Data.trans_tac
wenzelm@23164
   153
haftmann@31068
   154
  val norm_ss1 = Numeral_Simprocs.num_ss addsimps numeral_syms @ add_0s @ mult_1s @
nipkow@31790
   155
    [@{thm Suc_eq_plus1_left}] @ @{thms add_ac}
haftmann@31068
   156
  val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
wenzelm@23164
   157
  fun norm_tac ss = 
wenzelm@23164
   158
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   159
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   160
wenzelm@23164
   161
  val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps;
wenzelm@23164
   162
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss));
wenzelm@23164
   163
  val simplify_meta_eq  = simplify_meta_eq
wenzelm@23164
   164
  end;
wenzelm@23164
   165
wenzelm@23164
   166
wenzelm@23164
   167
structure EqCancelNumerals = CancelNumeralsFun
wenzelm@23164
   168
 (open CancelNumeralsCommon
haftmann@30496
   169
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   170
  val mk_bal   = HOLogic.mk_eq
wenzelm@23164
   171
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT
huffman@23471
   172
  val bal_add1 = @{thm nat_eq_add_iff1} RS trans
huffman@23471
   173
  val bal_add2 = @{thm nat_eq_add_iff2} RS trans
wenzelm@23164
   174
);
wenzelm@23164
   175
wenzelm@23164
   176
structure LessCancelNumerals = CancelNumeralsFun
wenzelm@23164
   177
 (open CancelNumeralsCommon
haftmann@30496
   178
  val prove_conv = Arith_Data.prove_conv
haftmann@23881
   179
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
haftmann@23881
   180
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT
huffman@23471
   181
  val bal_add1 = @{thm nat_less_add_iff1} RS trans
huffman@23471
   182
  val bal_add2 = @{thm nat_less_add_iff2} RS trans
wenzelm@23164
   183
);
wenzelm@23164
   184
wenzelm@23164
   185
structure LeCancelNumerals = CancelNumeralsFun
wenzelm@23164
   186
 (open CancelNumeralsCommon
haftmann@30496
   187
  val prove_conv = Arith_Data.prove_conv
haftmann@23881
   188
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
haftmann@23881
   189
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT
huffman@23471
   190
  val bal_add1 = @{thm nat_le_add_iff1} RS trans
huffman@23471
   191
  val bal_add2 = @{thm nat_le_add_iff2} RS trans
wenzelm@23164
   192
);
wenzelm@23164
   193
wenzelm@23164
   194
structure DiffCancelNumerals = CancelNumeralsFun
wenzelm@23164
   195
 (open CancelNumeralsCommon
haftmann@30496
   196
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   197
  val mk_bal   = HOLogic.mk_binop @{const_name HOL.minus}
wenzelm@23164
   198
  val dest_bal = HOLogic.dest_bin @{const_name HOL.minus} HOLogic.natT
huffman@23471
   199
  val bal_add1 = @{thm nat_diff_add_eq1} RS trans
huffman@23471
   200
  val bal_add2 = @{thm nat_diff_add_eq2} RS trans
wenzelm@23164
   201
);
wenzelm@23164
   202
wenzelm@23164
   203
wenzelm@23164
   204
val cancel_numerals =
wenzelm@32155
   205
  map (Arith_Data.prep_simproc @{theory})
wenzelm@23164
   206
   [("nateq_cancel_numerals",
wenzelm@23164
   207
     ["(l::nat) + m = n", "(l::nat) = m + n",
wenzelm@23164
   208
      "(l::nat) * m = n", "(l::nat) = m * n",
wenzelm@23164
   209
      "Suc m = n", "m = Suc n"],
wenzelm@23164
   210
     K EqCancelNumerals.proc),
wenzelm@23164
   211
    ("natless_cancel_numerals",
wenzelm@23164
   212
     ["(l::nat) + m < n", "(l::nat) < m + n",
wenzelm@23164
   213
      "(l::nat) * m < n", "(l::nat) < m * n",
wenzelm@23164
   214
      "Suc m < n", "m < Suc n"],
wenzelm@23164
   215
     K LessCancelNumerals.proc),
wenzelm@23164
   216
    ("natle_cancel_numerals",
wenzelm@23164
   217
     ["(l::nat) + m <= n", "(l::nat) <= m + n",
wenzelm@23164
   218
      "(l::nat) * m <= n", "(l::nat) <= m * n",
wenzelm@23164
   219
      "Suc m <= n", "m <= Suc n"],
wenzelm@23164
   220
     K LeCancelNumerals.proc),
wenzelm@23164
   221
    ("natdiff_cancel_numerals",
wenzelm@23164
   222
     ["((l::nat) + m) - n", "(l::nat) - (m + n)",
wenzelm@23164
   223
      "(l::nat) * m - n", "(l::nat) - m * n",
wenzelm@23164
   224
      "Suc m - n", "m - Suc n"],
wenzelm@23164
   225
     K DiffCancelNumerals.proc)];
wenzelm@23164
   226
wenzelm@23164
   227
wenzelm@23164
   228
(*** Applying CombineNumeralsFun ***)
wenzelm@23164
   229
wenzelm@23164
   230
structure CombineNumeralsData =
wenzelm@23164
   231
  struct
wenzelm@24630
   232
  type coeff            = int
wenzelm@24630
   233
  val iszero            = (fn x => x = 0)
wenzelm@24630
   234
  val add               = op +
wenzelm@23164
   235
  val mk_sum            = (fn T:typ => long_mk_sum)  (*to work for 2*x + 3*x *)
wenzelm@23164
   236
  val dest_sum          = dest_Sucs_sum false
wenzelm@23164
   237
  val mk_coeff          = mk_coeff
wenzelm@23164
   238
  val dest_coeff        = dest_coeff
huffman@23471
   239
  val left_distrib      = @{thm left_add_mult_distrib} RS trans
haftmann@30496
   240
  val prove_conv        = Arith_Data.prove_conv_nohyps
wenzelm@31368
   241
  fun trans_tac _       = Arith_Data.trans_tac
wenzelm@23164
   242
nipkow@31790
   243
  val norm_ss1 = Numeral_Simprocs.num_ss addsimps numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1}] @ @{thms add_ac}
haftmann@31068
   244
  val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
wenzelm@23164
   245
  fun norm_tac ss =
wenzelm@23164
   246
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   247
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   248
wenzelm@23164
   249
  val numeral_simp_ss = HOL_ss addsimps add_0s @ bin_simps;
wenzelm@23164
   250
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23164
   251
  val simplify_meta_eq  = simplify_meta_eq
wenzelm@23164
   252
  end;
wenzelm@23164
   253
wenzelm@23164
   254
structure CombineNumerals = CombineNumeralsFun(CombineNumeralsData);
wenzelm@23164
   255
wenzelm@23164
   256
val combine_numerals =
wenzelm@32155
   257
  Arith_Data.prep_simproc @{theory}
wenzelm@32155
   258
    ("nat_combine_numerals", ["(i::nat) + j", "Suc (i + j)"], K CombineNumerals.proc);
wenzelm@23164
   259
wenzelm@23164
   260
wenzelm@23164
   261
(*** Applying CancelNumeralFactorFun ***)
wenzelm@23164
   262
wenzelm@23164
   263
structure CancelNumeralFactorCommon =
wenzelm@23164
   264
  struct
wenzelm@23164
   265
  val mk_coeff          = mk_coeff
wenzelm@23164
   266
  val dest_coeff        = dest_coeff
wenzelm@31368
   267
  fun trans_tac _       = Arith_Data.trans_tac
wenzelm@23164
   268
haftmann@31068
   269
  val norm_ss1 = Numeral_Simprocs.num_ss addsimps
nipkow@31790
   270
    numeral_syms @ add_0s @ mult_1s @ [@{thm Suc_eq_plus1_left}] @ @{thms add_ac}
haftmann@31068
   271
  val norm_ss2 = Numeral_Simprocs.num_ss addsimps bin_simps @ @{thms add_ac} @ @{thms mult_ac}
wenzelm@23164
   272
  fun norm_tac ss =
wenzelm@23164
   273
    ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss1))
wenzelm@23164
   274
    THEN ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss2))
wenzelm@23164
   275
wenzelm@23164
   276
  val numeral_simp_ss = HOL_ss addsimps bin_simps
wenzelm@23164
   277
  fun numeral_simp_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss numeral_simp_ss))
wenzelm@23164
   278
  val simplify_meta_eq  = simplify_meta_eq
wenzelm@23164
   279
  end
wenzelm@23164
   280
wenzelm@23164
   281
structure DivCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   282
 (open CancelNumeralFactorCommon
haftmann@30496
   283
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   284
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
wenzelm@23164
   285
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
huffman@23471
   286
  val cancel = @{thm nat_mult_div_cancel1} RS trans
wenzelm@23164
   287
  val neg_exchanges = false
wenzelm@23164
   288
)
wenzelm@23164
   289
nipkow@23969
   290
structure DvdCancelNumeralFactor = CancelNumeralFactorFun
nipkow@23969
   291
 (open CancelNumeralFactorCommon
haftmann@30496
   292
  val prove_conv = Arith_Data.prove_conv
haftmann@27651
   293
  val mk_bal   = HOLogic.mk_binrel @{const_name Ring_and_Field.dvd}
haftmann@27651
   294
  val dest_bal = HOLogic.dest_bin @{const_name Ring_and_Field.dvd} HOLogic.natT
nipkow@23969
   295
  val cancel = @{thm nat_mult_dvd_cancel1} RS trans
nipkow@23969
   296
  val neg_exchanges = false
nipkow@23969
   297
)
nipkow@23969
   298
wenzelm@23164
   299
structure EqCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   300
 (open CancelNumeralFactorCommon
haftmann@30496
   301
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   302
  val mk_bal   = HOLogic.mk_eq
wenzelm@23164
   303
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT
huffman@23471
   304
  val cancel = @{thm nat_mult_eq_cancel1} RS trans
wenzelm@23164
   305
  val neg_exchanges = false
wenzelm@23164
   306
)
wenzelm@23164
   307
wenzelm@23164
   308
structure LessCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   309
 (open CancelNumeralFactorCommon
haftmann@30496
   310
  val prove_conv = Arith_Data.prove_conv
haftmann@23881
   311
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
haftmann@23881
   312
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT
huffman@23471
   313
  val cancel = @{thm nat_mult_less_cancel1} RS trans
wenzelm@23164
   314
  val neg_exchanges = true
wenzelm@23164
   315
)
wenzelm@23164
   316
wenzelm@23164
   317
structure LeCancelNumeralFactor = CancelNumeralFactorFun
wenzelm@23164
   318
 (open CancelNumeralFactorCommon
haftmann@30496
   319
  val prove_conv = Arith_Data.prove_conv
haftmann@23881
   320
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
haftmann@23881
   321
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT
huffman@23471
   322
  val cancel = @{thm nat_mult_le_cancel1} RS trans
wenzelm@23164
   323
  val neg_exchanges = true
wenzelm@23164
   324
)
wenzelm@23164
   325
wenzelm@23164
   326
val cancel_numeral_factors =
wenzelm@32155
   327
  map (Arith_Data.prep_simproc @{theory})
wenzelm@23164
   328
   [("nateq_cancel_numeral_factors",
wenzelm@23164
   329
     ["(l::nat) * m = n", "(l::nat) = m * n"],
wenzelm@23164
   330
     K EqCancelNumeralFactor.proc),
wenzelm@23164
   331
    ("natless_cancel_numeral_factors",
wenzelm@23164
   332
     ["(l::nat) * m < n", "(l::nat) < m * n"],
wenzelm@23164
   333
     K LessCancelNumeralFactor.proc),
wenzelm@23164
   334
    ("natle_cancel_numeral_factors",
wenzelm@23164
   335
     ["(l::nat) * m <= n", "(l::nat) <= m * n"],
wenzelm@23164
   336
     K LeCancelNumeralFactor.proc),
wenzelm@23164
   337
    ("natdiv_cancel_numeral_factors",
wenzelm@23164
   338
     ["((l::nat) * m) div n", "(l::nat) div (m * n)"],
nipkow@23969
   339
     K DivCancelNumeralFactor.proc),
nipkow@23969
   340
    ("natdvd_cancel_numeral_factors",
nipkow@23969
   341
     ["((l::nat) * m) dvd n", "(l::nat) dvd (m * n)"],
nipkow@23969
   342
     K DvdCancelNumeralFactor.proc)];
wenzelm@23164
   343
wenzelm@23164
   344
wenzelm@23164
   345
wenzelm@23164
   346
(*** Applying ExtractCommonTermFun ***)
wenzelm@23164
   347
wenzelm@23164
   348
(*this version ALWAYS includes a trailing one*)
wenzelm@23164
   349
fun long_mk_prod []        = one
wenzelm@23164
   350
  | long_mk_prod (t :: ts) = mk_times (t, mk_prod ts);
wenzelm@23164
   351
wenzelm@23164
   352
(*Find first term that matches u*)
wenzelm@23164
   353
fun find_first_t past u []         = raise TERM("find_first_t", [])
wenzelm@23164
   354
  | find_first_t past u (t::terms) =
wenzelm@23164
   355
        if u aconv t then (rev past @ terms)
wenzelm@23164
   356
        else find_first_t (t::past) u terms
wenzelm@23164
   357
        handle TERM _ => find_first_t (t::past) u terms;
wenzelm@23164
   358
wenzelm@23164
   359
(** Final simplification for the CancelFactor simprocs **)
haftmann@30518
   360
val simplify_one = Arith_Data.simplify_meta_eq  
wenzelm@23164
   361
  [@{thm mult_1_left}, @{thm mult_1_right}, @{thm div_1}, @{thm numeral_1_eq_Suc_0}];
wenzelm@23164
   362
nipkow@30649
   363
fun cancel_simplify_meta_eq ss cancel_th th =
wenzelm@23164
   364
    simplify_one ss (([th, cancel_th]) MRS trans);
wenzelm@23164
   365
wenzelm@23164
   366
structure CancelFactorCommon =
wenzelm@23164
   367
  struct
wenzelm@23164
   368
  val mk_sum            = (fn T:typ => long_mk_prod)
wenzelm@23164
   369
  val dest_sum          = dest_prod
wenzelm@23164
   370
  val mk_coeff          = mk_coeff
wenzelm@23164
   371
  val dest_coeff        = dest_coeff
wenzelm@23164
   372
  val find_first        = find_first_t []
wenzelm@31368
   373
  fun trans_tac _       = Arith_Data.trans_tac
haftmann@23881
   374
  val norm_ss = HOL_ss addsimps mult_1s @ @{thms mult_ac}
wenzelm@23164
   375
  fun norm_tac ss = ALLGOALS (simp_tac (Simplifier.inherit_context ss norm_ss))
nipkow@30649
   376
  val simplify_meta_eq  = cancel_simplify_meta_eq
wenzelm@23164
   377
  end;
wenzelm@23164
   378
wenzelm@23164
   379
structure EqCancelFactor = ExtractCommonTermFun
wenzelm@23164
   380
 (open CancelFactorCommon
haftmann@30496
   381
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   382
  val mk_bal   = HOLogic.mk_eq
wenzelm@23164
   383
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT
wenzelm@31368
   384
  fun simp_conv _ _ = SOME @{thm nat_mult_eq_cancel_disj}
wenzelm@23164
   385
);
wenzelm@23164
   386
wenzelm@23164
   387
structure LessCancelFactor = ExtractCommonTermFun
wenzelm@23164
   388
 (open CancelFactorCommon
haftmann@30496
   389
  val prove_conv = Arith_Data.prove_conv
haftmann@23881
   390
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less}
haftmann@23881
   391
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less} HOLogic.natT
wenzelm@31368
   392
  fun simp_conv _ _ = SOME @{thm nat_mult_less_cancel_disj}
wenzelm@23164
   393
);
wenzelm@23164
   394
wenzelm@23164
   395
structure LeCancelFactor = ExtractCommonTermFun
wenzelm@23164
   396
 (open CancelFactorCommon
haftmann@30496
   397
  val prove_conv = Arith_Data.prove_conv
haftmann@23881
   398
  val mk_bal   = HOLogic.mk_binrel @{const_name HOL.less_eq}
haftmann@23881
   399
  val dest_bal = HOLogic.dest_bin @{const_name HOL.less_eq} HOLogic.natT
wenzelm@31368
   400
  fun simp_conv _ _ = SOME @{thm nat_mult_le_cancel_disj}
wenzelm@23164
   401
);
wenzelm@23164
   402
wenzelm@23164
   403
structure DivideCancelFactor = ExtractCommonTermFun
wenzelm@23164
   404
 (open CancelFactorCommon
haftmann@30496
   405
  val prove_conv = Arith_Data.prove_conv
wenzelm@23164
   406
  val mk_bal   = HOLogic.mk_binop @{const_name Divides.div}
wenzelm@23164
   407
  val dest_bal = HOLogic.dest_bin @{const_name Divides.div} HOLogic.natT
wenzelm@31368
   408
  fun simp_conv _ _ = SOME @{thm nat_mult_div_cancel_disj}
wenzelm@23164
   409
);
wenzelm@23164
   410
nipkow@23969
   411
structure DvdCancelFactor = ExtractCommonTermFun
nipkow@23969
   412
 (open CancelFactorCommon
haftmann@30496
   413
  val prove_conv = Arith_Data.prove_conv
haftmann@27651
   414
  val mk_bal   = HOLogic.mk_binrel @{const_name Ring_and_Field.dvd}
haftmann@27651
   415
  val dest_bal = HOLogic.dest_bin @{const_name Ring_and_Field.dvd} HOLogic.natT
wenzelm@31368
   416
  fun simp_conv _ _ = SOME @{thm nat_mult_dvd_cancel_disj}
nipkow@23969
   417
);
nipkow@23969
   418
wenzelm@23164
   419
val cancel_factor =
wenzelm@32155
   420
  map (Arith_Data.prep_simproc @{theory})
wenzelm@23164
   421
   [("nat_eq_cancel_factor",
wenzelm@23164
   422
     ["(l::nat) * m = n", "(l::nat) = m * n"],
wenzelm@23164
   423
     K EqCancelFactor.proc),
wenzelm@23164
   424
    ("nat_less_cancel_factor",
wenzelm@23164
   425
     ["(l::nat) * m < n", "(l::nat) < m * n"],
wenzelm@23164
   426
     K LessCancelFactor.proc),
wenzelm@23164
   427
    ("nat_le_cancel_factor",
wenzelm@23164
   428
     ["(l::nat) * m <= n", "(l::nat) <= m * n"],
wenzelm@23164
   429
     K LeCancelFactor.proc),
wenzelm@23164
   430
    ("nat_divide_cancel_factor",
wenzelm@23164
   431
     ["((l::nat) * m) div n", "(l::nat) div (m * n)"],
nipkow@23969
   432
     K DivideCancelFactor.proc),
nipkow@23969
   433
    ("nat_dvd_cancel_factor",
nipkow@23969
   434
     ["((l::nat) * m) dvd n", "(l::nat) dvd (m * n)"],
nipkow@23969
   435
     K DvdCancelFactor.proc)];
wenzelm@23164
   436
wenzelm@23164
   437
end;
wenzelm@23164
   438
wenzelm@23164
   439
wenzelm@23164
   440
Addsimprocs Nat_Numeral_Simprocs.cancel_numerals;
wenzelm@23164
   441
Addsimprocs [Nat_Numeral_Simprocs.combine_numerals];
wenzelm@23164
   442
Addsimprocs Nat_Numeral_Simprocs.cancel_numeral_factors;
wenzelm@23164
   443
Addsimprocs Nat_Numeral_Simprocs.cancel_factor;
wenzelm@23164
   444
wenzelm@23164
   445
wenzelm@23164
   446
(*examples:
wenzelm@23164
   447
print_depth 22;
wenzelm@23164
   448
set timing;
wenzelm@23164
   449
set trace_simp;
wenzelm@23164
   450
fun test s = (Goal s; by (Simp_tac 1));
wenzelm@23164
   451
wenzelm@23164
   452
(*cancel_numerals*)
wenzelm@23164
   453
test "l +( 2) + (2) + 2 + (l + 2) + (oo  + 2) = (uu::nat)";
wenzelm@23164
   454
test "(2*length xs < 2*length xs + j)";
wenzelm@23164
   455
test "(2*length xs < length xs * 2 + j)";
wenzelm@23164
   456
test "2*u = (u::nat)";
wenzelm@23164
   457
test "2*u = Suc (u)";
wenzelm@23164
   458
test "(i + j + 12 + (k::nat)) - 15 = y";
wenzelm@23164
   459
test "(i + j + 12 + (k::nat)) - 5 = y";
wenzelm@23164
   460
test "Suc u - 2 = y";
wenzelm@23164
   461
test "Suc (Suc (Suc u)) - 2 = y";
wenzelm@23164
   462
test "(i + j + 2 + (k::nat)) - 1 = y";
wenzelm@23164
   463
test "(i + j + 1 + (k::nat)) - 2 = y";
wenzelm@23164
   464
wenzelm@23164
   465
test "(2*x + (u*v) + y) - v*3*u = (w::nat)";
wenzelm@23164
   466
test "(2*x*u*v + 5 + (u*v)*4 + y) - v*u*4 = (w::nat)";
wenzelm@23164
   467
test "(2*x*u*v + (u*v)*4 + y) - v*u = (w::nat)";
wenzelm@23164
   468
test "Suc (Suc (2*x*u*v + u*4 + y)) - u = w";
wenzelm@23164
   469
test "Suc ((u*v)*4) - v*3*u = w";
wenzelm@23164
   470
test "Suc (Suc ((u*v)*3)) - v*3*u = w";
wenzelm@23164
   471
wenzelm@23164
   472
test "(i + j + 12 + (k::nat)) = u + 15 + y";
wenzelm@23164
   473
test "(i + j + 32 + (k::nat)) - (u + 15 + y) = zz";
wenzelm@23164
   474
test "(i + j + 12 + (k::nat)) = u + 5 + y";
wenzelm@23164
   475
(*Suc*)
wenzelm@23164
   476
test "(i + j + 12 + k) = Suc (u + y)";
wenzelm@23164
   477
test "Suc (Suc (Suc (Suc (Suc (u + y))))) <= ((i + j) + 41 + k)";
wenzelm@23164
   478
test "(i + j + 5 + k) < Suc (Suc (Suc (Suc (Suc (u + y)))))";
wenzelm@23164
   479
test "Suc (Suc (Suc (Suc (Suc (u + y))))) - 5 = v";
wenzelm@23164
   480
test "(i + j + 5 + k) = Suc (Suc (Suc (Suc (Suc (Suc (Suc (u + y)))))))";
wenzelm@23164
   481
test "2*y + 3*z + 2*u = Suc (u)";
wenzelm@23164
   482
test "2*y + 3*z + 6*w + 2*y + 3*z + 2*u = Suc (u)";
wenzelm@23164
   483
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::nat)";
wenzelm@23164
   484
test "6 + 2*y + 3*z + 4*u = Suc (vv + 2*u + z)";
wenzelm@23164
   485
test "(2*n*m) < (3*(m*n)) + (u::nat)";
wenzelm@23164
   486
wenzelm@23164
   487
test "(Suc (Suc (Suc (Suc (Suc (Suc (case length (f c) of 0 => 0 | Suc k => k)))))) <= Suc 0)";
wenzelm@23164
   488
 
wenzelm@23164
   489
test "Suc (Suc (Suc (Suc (Suc (Suc (length l1 + length l2)))))) <= length l1";
wenzelm@23164
   490
wenzelm@23164
   491
test "( (Suc (Suc (Suc (Suc (Suc (length (compT P E A ST mxr e) + length l3)))))) <= length (compT P E A ST mxr e))";
wenzelm@23164
   492
wenzelm@23164
   493
test "( (Suc (Suc (Suc (Suc (Suc (length (compT P E A ST mxr e) + length (compT P E (A Un \<A> e) ST mxr c))))))) <= length (compT P E A ST mxr e))";
wenzelm@23164
   494
wenzelm@23164
   495
wenzelm@23164
   496
(*negative numerals: FAIL*)
wenzelm@23164
   497
test "(i + j + -23 + (k::nat)) < u + 15 + y";
wenzelm@23164
   498
test "(i + j + 3 + (k::nat)) < u + -15 + y";
wenzelm@23164
   499
test "(i + j + -12 + (k::nat)) - 15 = y";
wenzelm@23164
   500
test "(i + j + 12 + (k::nat)) - -15 = y";
wenzelm@23164
   501
test "(i + j + -12 + (k::nat)) - -15 = y";
wenzelm@23164
   502
wenzelm@23164
   503
(*combine_numerals*)
wenzelm@23164
   504
test "k + 3*k = (u::nat)";
wenzelm@23164
   505
test "Suc (i + 3) = u";
wenzelm@23164
   506
test "Suc (i + j + 3 + k) = u";
wenzelm@23164
   507
test "k + j + 3*k + j = (u::nat)";
wenzelm@23164
   508
test "Suc (j*i + i + k + 5 + 3*k + i*j*4) = (u::nat)";
wenzelm@23164
   509
test "(2*n*m) + (3*(m*n)) = (u::nat)";
wenzelm@23164
   510
(*negative numerals: FAIL*)
wenzelm@23164
   511
test "Suc (i + j + -3 + k) = u";
wenzelm@23164
   512
wenzelm@23164
   513
(*cancel_numeral_factors*)
wenzelm@23164
   514
test "9*x = 12 * (y::nat)";
wenzelm@23164
   515
test "(9*x) div (12 * (y::nat)) = z";
wenzelm@23164
   516
test "9*x < 12 * (y::nat)";
wenzelm@23164
   517
test "9*x <= 12 * (y::nat)";
wenzelm@23164
   518
wenzelm@23164
   519
(*cancel_factor*)
wenzelm@23164
   520
test "x*k = k*(y::nat)";
wenzelm@23164
   521
test "k = k*(y::nat)";
wenzelm@23164
   522
test "a*(b*c) = (b::nat)";
wenzelm@23164
   523
test "a*(b*c) = d*(b::nat)*(x*a)";
wenzelm@23164
   524
wenzelm@23164
   525
test "x*k < k*(y::nat)";
wenzelm@23164
   526
test "k < k*(y::nat)";
wenzelm@23164
   527
test "a*(b*c) < (b::nat)";
wenzelm@23164
   528
test "a*(b*c) < d*(b::nat)*(x*a)";
wenzelm@23164
   529
wenzelm@23164
   530
test "x*k <= k*(y::nat)";
wenzelm@23164
   531
test "k <= k*(y::nat)";
wenzelm@23164
   532
test "a*(b*c) <= (b::nat)";
wenzelm@23164
   533
test "a*(b*c) <= d*(b::nat)*(x*a)";
wenzelm@23164
   534
wenzelm@23164
   535
test "(x*k) div (k*(y::nat)) = (uu::nat)";
wenzelm@23164
   536
test "(k) div (k*(y::nat)) = (uu::nat)";
wenzelm@23164
   537
test "(a*(b*c)) div ((b::nat)) = (uu::nat)";
wenzelm@23164
   538
test "(a*(b*c)) div (d*(b::nat)*(x*a)) = (uu::nat)";
wenzelm@23164
   539
*)