src/HOL/arith_data.ML
author wenzelm
Wed Nov 26 16:44:25 1997 +0100 (1997-11-26)
changeset 4295 81132a38996a
child 4309 c98f44948d86
permissions -rw-r--r--
Setup various arithmetic proof procedures.
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@4295
    10
  val nat_cancel: simproc list
wenzelm@4295
    11
end;
wenzelm@4295
    12
wenzelm@4295
    13
structure ArithData (* FIXME :ARITH_DATA *) =
wenzelm@4295
    14
struct
wenzelm@4295
    15
wenzelm@4295
    16
wenzelm@4295
    17
(** abstract syntax of structure nat: 0, Suc, + **)
wenzelm@4295
    18
wenzelm@4295
    19
(* mk_sum *)
wenzelm@4295
    20
wenzelm@4295
    21
val mk_plus = HOLogic.mk_binop "op +";
wenzelm@4295
    22
wenzelm@4295
    23
fun mk_sum [] = HOLogic.zero
wenzelm@4295
    24
  | mk_sum [t] = t
wenzelm@4295
    25
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@4295
    26
wenzelm@4295
    27
wenzelm@4295
    28
(* dest_sum *)
wenzelm@4295
    29
wenzelm@4295
    30
val dest_plus = HOLogic.dest_bin "op +" HOLogic.natT;
wenzelm@4295
    31
val one = HOLogic.mk_nat 1;
wenzelm@4295
    32
wenzelm@4295
    33
fun dest_sum tm =
wenzelm@4295
    34
  if HOLogic.is_zero tm then []
wenzelm@4295
    35
  else
wenzelm@4295
    36
    (case try HOLogic.dest_Suc tm of
wenzelm@4295
    37
      Some t => one :: dest_sum t
wenzelm@4295
    38
    | None =>
wenzelm@4295
    39
        (case try dest_plus tm of
wenzelm@4295
    40
          Some (t, u) => dest_sum t @ dest_sum u
wenzelm@4295
    41
        | None => [tm]));
wenzelm@4295
    42
wenzelm@4295
    43
wenzelm@4295
    44
(** generic proof tools **)
wenzelm@4295
    45
wenzelm@4295
    46
(* prove conversions *)
wenzelm@4295
    47
wenzelm@4295
    48
val mk_eqv = HOLogic.mk_Trueprop o HOLogic.mk_eq;
wenzelm@4295
    49
wenzelm@4295
    50
fun prove_conv expand_tac norm_tac sg (t, u) =
wenzelm@4295
    51
  mk_meta_eq (prove_goalw_cterm [] (cterm_of sg (mk_eqv (t, u)))
wenzelm@4295
    52
    (K [expand_tac, norm_tac]))
wenzelm@4295
    53
  handle ERROR => error ("The error(s) above occurred while trying to prove " ^
wenzelm@4295
    54
    (string_of_cterm (cterm_of sg (mk_eqv (t, u)))));
wenzelm@4295
    55
wenzelm@4295
    56
val subst_equals = prove_goal HOL.thy "[| t = s; u = t |] ==> u = s"
wenzelm@4295
    57
  (fn prems => [cut_facts_tac prems 1, SIMPSET' asm_simp_tac 1]);
wenzelm@4295
    58
wenzelm@4295
    59
wenzelm@4295
    60
(* rewriting *)
wenzelm@4295
    61
wenzelm@4295
    62
fun simp_all rules = ALLGOALS (simp_tac (HOL_ss addsimps rules));
wenzelm@4295
    63
wenzelm@4295
    64
val add_rules = [add_Suc, add_Suc_right, add_0, add_0_right];
wenzelm@4295
    65
val mult_rules = [mult_Suc, mult_Suc_right, mult_0, mult_0_right];
wenzelm@4295
    66
wenzelm@4295
    67
wenzelm@4295
    68
wenzelm@4295
    69
(** cancel common summands **)
wenzelm@4295
    70
wenzelm@4295
    71
structure Sum =
wenzelm@4295
    72
struct
wenzelm@4295
    73
  val mk_sum = mk_sum;
wenzelm@4295
    74
  val dest_sum = dest_sum;
wenzelm@4295
    75
  val prove_conv = prove_conv;
wenzelm@4295
    76
  val norm_tac = simp_all add_rules THEN simp_all add_ac;
wenzelm@4295
    77
end;
wenzelm@4295
    78
wenzelm@4295
    79
fun gen_uncancel_tac rule ct =
wenzelm@4295
    80
  rtac (instantiate' [] [None, Some ct] (rule RS subst_equals)) 1;
wenzelm@4295
    81
wenzelm@4295
    82
wenzelm@4295
    83
(* nat eq *)
wenzelm@4295
    84
wenzelm@4295
    85
structure EqCancelSums = CancelSumsFun
wenzelm@4295
    86
(struct
wenzelm@4295
    87
  open Sum;
wenzelm@4295
    88
  val mk_bal = HOLogic.mk_eq;
wenzelm@4295
    89
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
wenzelm@4295
    90
  val uncancel_tac = gen_uncancel_tac add_left_cancel;
wenzelm@4295
    91
end);
wenzelm@4295
    92
wenzelm@4295
    93
wenzelm@4295
    94
(* nat less *)
wenzelm@4295
    95
wenzelm@4295
    96
structure LessCancelSums = CancelSumsFun
wenzelm@4295
    97
(struct
wenzelm@4295
    98
  open Sum;
wenzelm@4295
    99
  val mk_bal = HOLogic.mk_binrel "op <";
wenzelm@4295
   100
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
wenzelm@4295
   101
  val uncancel_tac = gen_uncancel_tac add_left_cancel_less;
wenzelm@4295
   102
end);
wenzelm@4295
   103
wenzelm@4295
   104
wenzelm@4295
   105
(* nat le *)
wenzelm@4295
   106
wenzelm@4295
   107
structure LeCancelSums = CancelSumsFun
wenzelm@4295
   108
(struct
wenzelm@4295
   109
  open Sum;
wenzelm@4295
   110
  val mk_bal = HOLogic.mk_binrel "op <=";
wenzelm@4295
   111
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
wenzelm@4295
   112
  val uncancel_tac = gen_uncancel_tac add_left_cancel_le;
wenzelm@4295
   113
end);
wenzelm@4295
   114
wenzelm@4295
   115
wenzelm@4295
   116
(* nat diff *)
wenzelm@4295
   117
wenzelm@4295
   118
(* FIXME *)
wenzelm@4295
   119
wenzelm@4295
   120
wenzelm@4295
   121
wenzelm@4295
   122
(** cancel common factor **)
wenzelm@4295
   123
wenzelm@4295
   124
structure Factor =
wenzelm@4295
   125
struct
wenzelm@4295
   126
  val mk_sum = mk_sum;
wenzelm@4295
   127
  val dest_sum = dest_sum;
wenzelm@4295
   128
  val prove_conv = prove_conv;
wenzelm@4295
   129
  val norm_tac = simp_all (add_rules @ mult_rules) THEN simp_all add_ac;
wenzelm@4295
   130
end;
wenzelm@4295
   131
wenzelm@4295
   132
fun mk_cnat n = cterm_of (sign_of Nat.thy) (HOLogic.mk_nat n);
wenzelm@4295
   133
wenzelm@4295
   134
fun gen_multiply_tac rule k =
wenzelm@4295
   135
  if k > 0 then
wenzelm@4295
   136
    rtac (instantiate' [] [None, Some (mk_cnat (k - 1))] (rule RS subst_equals)) 1
wenzelm@4295
   137
  else no_tac;
wenzelm@4295
   138
wenzelm@4295
   139
wenzelm@4295
   140
(* nat eq *)
wenzelm@4295
   141
wenzelm@4295
   142
structure EqCancelFactor = CancelFactorFun
wenzelm@4295
   143
(struct
wenzelm@4295
   144
  open Factor;
wenzelm@4295
   145
  val mk_bal = HOLogic.mk_eq;
wenzelm@4295
   146
  val dest_bal = HOLogic.dest_bin "op =" HOLogic.natT;
wenzelm@4295
   147
  val multiply_tac = gen_multiply_tac Suc_mult_cancel1;
wenzelm@4295
   148
end);
wenzelm@4295
   149
wenzelm@4295
   150
wenzelm@4295
   151
(* nat less *)
wenzelm@4295
   152
wenzelm@4295
   153
structure LessCancelFactor = CancelFactorFun
wenzelm@4295
   154
(struct
wenzelm@4295
   155
  open Factor;
wenzelm@4295
   156
  val mk_bal = HOLogic.mk_binrel "op <";
wenzelm@4295
   157
  val dest_bal = HOLogic.dest_bin "op <" HOLogic.natT;
wenzelm@4295
   158
  val multiply_tac = gen_multiply_tac Suc_mult_less_cancel1;
wenzelm@4295
   159
end);
wenzelm@4295
   160
wenzelm@4295
   161
wenzelm@4295
   162
(* nat le *)
wenzelm@4295
   163
wenzelm@4295
   164
structure LeCancelFactor = CancelFactorFun
wenzelm@4295
   165
(struct
wenzelm@4295
   166
  open Factor;
wenzelm@4295
   167
  val mk_bal = HOLogic.mk_binrel "op <=";
wenzelm@4295
   168
  val dest_bal = HOLogic.dest_bin "op <=" HOLogic.natT;
wenzelm@4295
   169
  val multiply_tac = gen_multiply_tac Suc_mult_le_cancel1;
wenzelm@4295
   170
end);
wenzelm@4295
   171
wenzelm@4295
   172
wenzelm@4295
   173
wenzelm@4295
   174
(** prepare nat_cancel simprocs **)
wenzelm@4295
   175
wenzelm@4295
   176
fun prep_pat s = Thm.read_cterm (sign_of Arith.thy) (s, HOLogic.termTVar);
wenzelm@4295
   177
val prep_pats = map prep_pat;
wenzelm@4295
   178
wenzelm@4295
   179
fun prep_simproc (name, pats, proc) = Simplifier.mk_simproc name pats proc;
wenzelm@4295
   180
wenzelm@4295
   181
val eq_pats = prep_pats ["(l::nat) + m = n", "(l::nat) = m + n", "Suc m = n", "m = Suc n"];
wenzelm@4295
   182
val less_pats = prep_pats ["(l::nat) + m < n", "(l::nat) < m + n", "Suc m < n", "m < Suc n"];
wenzelm@4295
   183
val le_pats = prep_pats ["(l::nat) + m <= n", "(l::nat) <= m + n", "Suc m <= n", "m <= Suc n"];
wenzelm@4295
   184
wenzelm@4295
   185
val nat_cancel = map prep_simproc
wenzelm@4295
   186
  [("nateq_cancel_factor", eq_pats, EqCancelFactor.proc),
wenzelm@4295
   187
   ("natless_cancel_factor", less_pats, LessCancelFactor.proc),
wenzelm@4295
   188
   ("natle_cancel_factor", le_pats, LeCancelFactor.proc),
wenzelm@4295
   189
   ("nateq_cancel_sums", eq_pats, EqCancelSums.proc),
wenzelm@4295
   190
   ("natless_cancel_sums", less_pats, LessCancelSums.proc),
wenzelm@4295
   191
   ("natle_cancel_sums", le_pats, LeCancelSums.proc)];
wenzelm@4295
   192
wenzelm@4295
   193
wenzelm@4295
   194
end;