src/HOL/arith_data.ML
author oheimb
Thu Sep 24 17:17:14 1998 +0200 (1998-09-24)
changeset 5553 ae42b36a50c2
parent 5429 0833486c23ce
child 5591 fbb4e1ac234d
permissions -rw-r--r--
renamed mk_meta_eq to mk_eq
wenzelm@4295
     1
(*  Title:      HOL/arith_data.ML
wenzelm@4295
     2
    ID:         $Id$
wenzelm@4295
     3
    Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
wenzelm@4295
     4
wenzelm@4295
     5
Setup various arithmetic proof procedures.
wenzelm@4295
     6
*)
wenzelm@4295
     7
wenzelm@4295
     8
signature ARITH_DATA =
wenzelm@4295
     9
sig
wenzelm@4309
    10
  val nat_cancel_sums: simproc list
wenzelm@4309
    11
  val nat_cancel_factor: simproc list
wenzelm@4295
    12
  val nat_cancel: simproc list
wenzelm@4295
    13
end;
wenzelm@4295
    14
wenzelm@4309
    15
structure ArithData: ARITH_DATA =
wenzelm@4295
    16
struct
wenzelm@4295
    17
wenzelm@4295
    18
wenzelm@4295
    19
(** abstract syntax of structure nat: 0, Suc, + **)
wenzelm@4295
    20
wenzelm@4310
    21
(* mk_sum, mk_norm_sum *)
wenzelm@4295
    22
wenzelm@4310
    23
val one = HOLogic.mk_nat 1;
wenzelm@4295
    24
val mk_plus = HOLogic.mk_binop "op +";
wenzelm@4295
    25
wenzelm@4295
    26
fun mk_sum [] = HOLogic.zero
wenzelm@4295
    27
  | mk_sum [t] = t
wenzelm@4295
    28
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@4295
    29
wenzelm@4310
    30
(*normal form of sums: Suc (... (Suc (a + (b + ...))))*)
wenzelm@4310
    31
fun mk_norm_sum ts =
wenzelm@4310
    32
  let val (ones, sums) = partition (equal one) ts in
wenzelm@4310
    33
    funpow (length ones) HOLogic.mk_Suc (mk_sum sums)
wenzelm@4310
    34
  end;
wenzelm@4310
    35
wenzelm@4295
    36
wenzelm@4295
    37
(* dest_sum *)
wenzelm@4295
    38
wenzelm@4295
    39
val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT;
wenzelm@4295
    40
wenzelm@4295
    41
fun dest_sum tm =
wenzelm@4295
    42
  if HOLogic.is_zero tm then []
wenzelm@4295
    43
  else
wenzelm@4295
    44
    (case try HOLogic.dest_Suc tm of
wenzelm@4295
    45
      Some t => one :: dest_sum t
wenzelm@4295
    46
    | None =>
wenzelm@4295
    47
        (case try dest_plus tm of
wenzelm@4295
    48
          Some (t, u) => dest_sum t @ dest_sum u
wenzelm@4295
    49
        | None => [tm]));
wenzelm@4295
    50
wenzelm@4295
    51
wenzelm@4295
    52
(** generic proof tools **)
wenzelm@4295
    53
wenzelm@4295
    54
(* prove conversions *)
wenzelm@4295
    55
wenzelm@4295
    56
val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq;
wenzelm@4295
    57
wenzelm@4295
    58
fun prove_conv expand_tac norm_tac sg (t, u) =
oheimb@5553
    59
  mk_meta_eq (prove_goalw_cterm_nocheck [] (cterm_of sg (mk_eqv (t, u)))
wenzelm@4295
    60
    (K [expand_tac, norm_tac]))
wenzelm@4295
    61
  handle ERROR => error ("The error(s) above occurred while trying to prove " ^
wenzelm@4295
    62
    (string_of_cterm (cterm_of sg (mk_eqv (t, u)))));
wenzelm@4295
    63
wenzelm@4295
    64
val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s"
wenzelm@4295
    65
  (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]);
wenzelm@4295
    66
wenzelm@4295
    67
wenzelm@4295
    68
(* rewriting *)
wenzelm@4295
    69
wenzelm@4295
    70
fun simp_all rules = ALLGOALS (simp_tac (HOL_ss addsimps rules));
wenzelm@4295
    71
wenzelm@4295
    72
val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right];
wenzelm@4295
    73
val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right];
wenzelm@4295
    74
wenzelm@4295
    75
wenzelm@4295
    76
wenzelm@4295
    77
(** cancel common summands **)
wenzelm@4295
    78
wenzelm@4295
    79
structure Sum =
wenzelm@4295
    80
struct
wenzelm@4310
    81
  val mk_sum = mk_norm_sum;
wenzelm@4295
    82
  val dest_sum = dest_sum;
wenzelm@4295
    83
  val prove_conv = prove_conv;
wenzelm@4295
    84
  val norm_tac = simp_all add_rules THEN simp_all add_ac;
wenzelm@4295
    85
end;
wenzelm@4295
    86
wenzelm@4295
    87
fun gen_uncancel_tac rule ct =
wenzelm@4295
    88
  rtac (instantiate' [] [None, Some ct] (rule RS subst_equals)) 1;
wenzelm@4295
    89
wenzelm@4295
    90
wenzelm@4295
    91
(* nat eq *)
wenzelm@4295
    92
wenzelm@4295
    93
structure EqCancelSums = CancelSumsFun
wenzelm@4295
    94
(struct
wenzelm@4295
    95
  open Sum;
wenzelm@4295
    96
  val mk_bal = HOLogic.mk_eq;
wenzelm@4295
    97
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
wenzelm@4295
    98
  val uncancel_tac = gen_uncancel_tac add_left_cancel;
wenzelm@4295
    99
end);
wenzelm@4295
   100
wenzelm@4295
   101
wenzelm@4295
   102
(* nat less *)
wenzelm@4295
   103
wenzelm@4295
   104
structure LessCancelSums = CancelSumsFun
wenzelm@4295
   105
(struct
wenzelm@4295
   106
  open Sum;
wenzelm@4295
   107
  val mk_bal = HOLogic.mk_binrel "op <";
wenzelm@4295
   108
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
wenzelm@4295
   109
  val uncancel_tac = gen_uncancel_tac add_left_cancel_less;
wenzelm@4295
   110
end);
wenzelm@4295
   111
wenzelm@4295
   112
wenzelm@4295
   113
(* nat le *)
wenzelm@4295
   114
wenzelm@4295
   115
structure LeCancelSums = CancelSumsFun
wenzelm@4295
   116
(struct
wenzelm@4295
   117
  open Sum;
wenzelm@4295
   118
  val mk_bal = HOLogic.mk_binrel "op <=";
wenzelm@4295
   119
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
wenzelm@4295
   120
  val uncancel_tac = gen_uncancel_tac add_left_cancel_le;
wenzelm@4295
   121
end);
wenzelm@4295
   122
wenzelm@4295
   123
wenzelm@4295
   124
(* nat diff *)
wenzelm@4295
   125
berghofe@4332
   126
structure DiffCancelSums = CancelSumsFun
berghofe@4332
   127
(struct
berghofe@4332
   128
  open Sum;
berghofe@4332
   129
  val mk_bal = HOLogic.mk_binop "op -";
berghofe@4332
   130
  val dest_bal = HOLogic.dest_bin "op -" HOLogic.natT;
berghofe@4332
   131
  val uncancel_tac = gen_uncancel_tac diff_cancel;
berghofe@4332
   132
end);
wenzelm@4295
   133
wenzelm@4295
   134
wenzelm@4295
   135
wenzelm@4295
   136
(** cancel common factor **)
wenzelm@4295
   137
wenzelm@4295
   138
structure Factor =
wenzelm@4295
   139
struct
wenzelm@4310
   140
  val mk_sum = mk_norm_sum;
wenzelm@4295
   141
  val dest_sum = dest_sum;
wenzelm@4295
   142
  val prove_conv = prove_conv;
wenzelm@4295
   143
  val norm_tac = simp_all (add_rules @ mult_rules) THEN simp_all add_ac;
wenzelm@4295
   144
end;
wenzelm@4295
   145
wenzelm@4295
   146
fun mk_cnat n = cterm_of (sign_of Nat.thy) (HOLogic.mk_nat n);
wenzelm@4295
   147
wenzelm@4295
   148
fun gen_multiply_tac rule k =
wenzelm@4295
   149
  if k > 0 then
wenzelm@4295
   150
    rtac (instantiate' [] [None, Some (mk_cnat (k - 1))] (rule RS subst_equals)) 1
wenzelm@4295
   151
  else no_tac;
wenzelm@4295
   152
wenzelm@4295
   153
wenzelm@4295
   154
(* nat eq *)
wenzelm@4295
   155
wenzelm@4295
   156
structure EqCancelFactor = CancelFactorFun
wenzelm@4295
   157
(struct
wenzelm@4295
   158
  open Factor;
wenzelm@4295
   159
  val mk_bal = HOLogic.mk_eq;
wenzelm@4295
   160
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
wenzelm@4295
   161
  val multiply_tac = gen_multiply_tac Suc_mult_cancel1;
wenzelm@4295
   162
end);
wenzelm@4295
   163
wenzelm@4295
   164
wenzelm@4295
   165
(* nat less *)
wenzelm@4295
   166
wenzelm@4295
   167
structure LessCancelFactor = CancelFactorFun
wenzelm@4295
   168
(struct
wenzelm@4295
   169
  open Factor;
wenzelm@4295
   170
  val mk_bal = HOLogic.mk_binrel "op <";
wenzelm@4295
   171
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
wenzelm@4295
   172
  val multiply_tac = gen_multiply_tac Suc_mult_less_cancel1;
wenzelm@4295
   173
end);
wenzelm@4295
   174
wenzelm@4295
   175
wenzelm@4295
   176
(* nat le *)
wenzelm@4295
   177
wenzelm@4295
   178
structure LeCancelFactor = CancelFactorFun
wenzelm@4295
   179
(struct
wenzelm@4295
   180
  open Factor;
wenzelm@4295
   181
  val mk_bal = HOLogic.mk_binrel "op <=";
wenzelm@4295
   182
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
wenzelm@4295
   183
  val multiply_tac = gen_multiply_tac Suc_mult_le_cancel1;
wenzelm@4295
   184
end);
wenzelm@4295
   185
wenzelm@4295
   186
wenzelm@4295
   187
wenzelm@4295
   188
(** prepare nat_cancel simprocs **)
wenzelm@4295
   189
wenzelm@4295
   190
fun prep_pat s = Thm.read_cterm (sign_of Arith.thy) (s, HOLogic.termTVar);
wenzelm@4295
   191
val prep_pats = map prep_pat;
wenzelm@4295
   192
wenzelm@4295
   193
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
wenzelm@4295
   194
wenzelm@4295
   195
val eq_pats = prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"];
wenzelm@4295
   196
val less_pats = prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"];
wenzelm@4295
   197
val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"];
berghofe@4332
   198
val diff_pats = prep_pats ["((l::nat) + m) - n", "(l::nat) - (m + n)", "Suc m - n", "m - Suc n"];
wenzelm@4295
   199
wenzelm@4309
   200
val nat_cancel_sums = map prep_simproc
wenzelm@4309
   201
  [("nateq_cancel_sums", eq_pats, EqCancelSums.proc),
wenzelm@4295
   202
   ("natless_cancel_sums", less_pats, LessCancelSums.proc),
berghofe@4332
   203
   ("natle_cancel_sums", le_pats, LeCancelSums.proc),
berghofe@4332
   204
   ("natdiff_cancel_sums", diff_pats, DiffCancelSums.proc)];
wenzelm@4295
   205
wenzelm@4309
   206
val nat_cancel_factor = map prep_simproc
wenzelm@4309
   207
  [("nateq_cancel_factor", eq_pats, EqCancelFactor.proc),
wenzelm@4309
   208
   ("natless_cancel_factor", less_pats, LessCancelFactor.proc),
wenzelm@4309
   209
   ("natle_cancel_factor", le_pats, LeCancelFactor.proc)];
wenzelm@4309
   210
wenzelm@4309
   211
val nat_cancel = nat_cancel_factor @ nat_cancel_sums;
wenzelm@4309
   212
wenzelm@4295
   213
wenzelm@4295
   214
end;
wenzelm@4336
   215
wenzelm@4336
   216
wenzelm@4336
   217
open ArithData;
wenzelm@4368
   218
wenzelm@4368
   219
wenzelm@4368
   220
context Arith.thy;
wenzelm@4368
   221
Addsimprocs nat_cancel;
paulson@4675
   222
paulson@4675
   223
paulson@4675
   224
(*This proof requires natdiff_cancel_sums*)
paulson@5429
   225
Goal "m < (n::nat) --> m<l --> (l-n) < (l-m)";
paulson@4675
   226
by (induct_tac "l" 1);
paulson@4675
   227
by (Simp_tac 1);
paulson@4675
   228
by (Clarify_tac 1);
wenzelm@5132
   229
by (etac less_SucE 1);
paulson@5429
   230
by (Clarify_tac 2);
paulson@5429
   231
by (asm_simp_tac (simpset() addsimps [Suc_le_eq]) 2);
paulson@4675
   232
by (asm_simp_tac (simpset() addsimps [diff_Suc_le_Suc_diff RS le_less_trans,
paulson@5418
   233
				      Suc_diff_le]) 1);
paulson@4675
   234
qed_spec_mp "diff_less_mono2";