src/HOL/arith_data.ML
author wenzelm
Thu Nov 27 13:38:06 1997 +0100 (1997-11-27)
changeset 4310 cad4f24be60b
parent 4309 c98f44948d86
child 4332 d4a15e32c024
permissions -rw-r--r--
mk_norm_sum;
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) =
wenzelm@4295
    59
  mk_meta_eq (prove_goalw_cterm [] (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
wenzelm@4295
   126
(* FIXME *)
wenzelm@4295
   127
wenzelm@4295
   128
wenzelm@4295
   129
wenzelm@4295
   130
(** cancel common factor **)
wenzelm@4295
   131
wenzelm@4295
   132
structure Factor =
wenzelm@4295
   133
struct
wenzelm@4310
   134
  val mk_sum = mk_norm_sum;
wenzelm@4295
   135
  val dest_sum = dest_sum;
wenzelm@4295
   136
  val prove_conv = prove_conv;
wenzelm@4295
   137
  val norm_tac = simp_all (add_rules @ mult_rules) THEN simp_all add_ac;
wenzelm@4295
   138
end;
wenzelm@4295
   139
wenzelm@4295
   140
fun mk_cnat n = cterm_of (sign_of Nat.thy) (HOLogic.mk_nat n);
wenzelm@4295
   141
wenzelm@4295
   142
fun gen_multiply_tac rule k =
wenzelm@4295
   143
  if k > 0 then
wenzelm@4295
   144
    rtac (instantiate' [] [None, Some (mk_cnat (k - 1))] (rule RS subst_equals)) 1
wenzelm@4295
   145
  else no_tac;
wenzelm@4295
   146
wenzelm@4295
   147
wenzelm@4295
   148
(* nat eq *)
wenzelm@4295
   149
wenzelm@4295
   150
structure EqCancelFactor = CancelFactorFun
wenzelm@4295
   151
(struct
wenzelm@4295
   152
  open Factor;
wenzelm@4295
   153
  val mk_bal = HOLogic.mk_eq;
wenzelm@4295
   154
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
wenzelm@4295
   155
  val multiply_tac = gen_multiply_tac Suc_mult_cancel1;
wenzelm@4295
   156
end);
wenzelm@4295
   157
wenzelm@4295
   158
wenzelm@4295
   159
(* nat less *)
wenzelm@4295
   160
wenzelm@4295
   161
structure LessCancelFactor = CancelFactorFun
wenzelm@4295
   162
(struct
wenzelm@4295
   163
  open Factor;
wenzelm@4295
   164
  val mk_bal = HOLogic.mk_binrel "op <";
wenzelm@4295
   165
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
wenzelm@4295
   166
  val multiply_tac = gen_multiply_tac Suc_mult_less_cancel1;
wenzelm@4295
   167
end);
wenzelm@4295
   168
wenzelm@4295
   169
wenzelm@4295
   170
(* nat le *)
wenzelm@4295
   171
wenzelm@4295
   172
structure LeCancelFactor = CancelFactorFun
wenzelm@4295
   173
(struct
wenzelm@4295
   174
  open Factor;
wenzelm@4295
   175
  val mk_bal = HOLogic.mk_binrel "op <=";
wenzelm@4295
   176
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
wenzelm@4295
   177
  val multiply_tac = gen_multiply_tac Suc_mult_le_cancel1;
wenzelm@4295
   178
end);
wenzelm@4295
   179
wenzelm@4295
   180
wenzelm@4295
   181
wenzelm@4295
   182
(** prepare nat_cancel simprocs **)
wenzelm@4295
   183
wenzelm@4295
   184
fun prep_pat s = Thm.read_cterm (sign_of Arith.thy) (s, HOLogic.termTVar);
wenzelm@4295
   185
val prep_pats = map prep_pat;
wenzelm@4295
   186
wenzelm@4295
   187
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
wenzelm@4295
   188
wenzelm@4295
   189
val eq_pats = prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"];
wenzelm@4295
   190
val less_pats = prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"];
wenzelm@4295
   191
val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"];
wenzelm@4295
   192
wenzelm@4309
   193
val nat_cancel_sums = map prep_simproc
wenzelm@4309
   194
  [("nateq_cancel_sums", eq_pats, EqCancelSums.proc),
wenzelm@4295
   195
   ("natless_cancel_sums", less_pats, LessCancelSums.proc),
wenzelm@4295
   196
   ("natle_cancel_sums", le_pats, LeCancelSums.proc)];
wenzelm@4295
   197
wenzelm@4309
   198
val nat_cancel_factor = map prep_simproc
wenzelm@4309
   199
  [("nateq_cancel_factor", eq_pats, EqCancelFactor.proc),
wenzelm@4309
   200
   ("natless_cancel_factor", less_pats, LessCancelFactor.proc),
wenzelm@4309
   201
   ("natle_cancel_factor", le_pats, LeCancelFactor.proc)];
wenzelm@4309
   202
wenzelm@4309
   203
val nat_cancel = nat_cancel_factor @ nat_cancel_sums;
wenzelm@4309
   204
wenzelm@4295
   205
wenzelm@4295
   206
end;