src/HOL/Tools/nat_arith.ML
author huffman
Fri Jul 27 17:57:31 2012 +0200 (2012-07-27)
changeset 48559 686cc7c47589
parent 48372 868dc809c8a2
child 48560 e0875d956a6b
permissions -rw-r--r--
give Nat_Arith simprocs proper name bindings by using simproc_setup
haftmann@30496
     1
(* Author: Markus Wenzel, Stefan Berghofer, and Tobias Nipkow
wenzelm@9436
     2
haftmann@30496
     3
Basic arithmetic for natural numbers.
wenzelm@9436
     4
*)
wenzelm@9436
     5
haftmann@30496
     6
signature NAT_ARITH =
haftmann@26101
     7
sig
haftmann@26101
     8
  val mk_sum: term list -> term
haftmann@26101
     9
  val mk_norm_sum: term list -> term
haftmann@26101
    10
  val dest_sum: term -> term list
huffman@48559
    11
  val nateq_cancel_sums: simpset -> cterm -> thm option
huffman@48559
    12
  val natless_cancel_sums: simpset -> cterm -> thm option
huffman@48559
    13
  val natle_cancel_sums: simpset -> cterm -> thm option
huffman@48559
    14
  val natdiff_cancel_sums: simpset -> cterm -> thm option
haftmann@26101
    15
end;
wenzelm@9436
    16
haftmann@30496
    17
structure Nat_Arith: NAT_ARITH =
wenzelm@9436
    18
struct
wenzelm@9436
    19
wenzelm@9436
    20
(** abstract syntax of structure nat: 0, Suc, + **)
wenzelm@9436
    21
haftmann@35267
    22
val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
haftmann@35267
    23
val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
haftmann@26101
    24
wenzelm@9436
    25
fun mk_sum [] = HOLogic.zero
wenzelm@9436
    26
  | mk_sum [t] = t
wenzelm@9436
    27
  | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
wenzelm@9436
    28
wenzelm@9436
    29
(*normal form of sums: Suc (... (Suc (a + (b + ...))))*)
wenzelm@9436
    30
fun mk_norm_sum ts =
haftmann@21621
    31
  let val (ones, sums) = List.partition (equal HOLogic.Suc_zero) ts in
wenzelm@9436
    32
    funpow (length ones) HOLogic.mk_Suc (mk_sum sums)
wenzelm@9436
    33
  end;
wenzelm@9436
    34
wenzelm@9436
    35
fun dest_sum tm =
wenzelm@9436
    36
  if HOLogic.is_zero tm then []
wenzelm@9436
    37
  else
wenzelm@9436
    38
    (case try HOLogic.dest_Suc tm of
haftmann@21621
    39
      SOME t => HOLogic.Suc_zero :: dest_sum t
skalberg@15531
    40
    | NONE =>
wenzelm@9436
    41
        (case try dest_plus tm of
skalberg@15531
    42
          SOME (t, u) => dest_sum t @ dest_sum u
skalberg@15531
    43
        | NONE => [tm]));
wenzelm@9436
    44
webertj@20217
    45
wenzelm@9436
    46
(** cancel common summands **)
wenzelm@9436
    47
haftmann@30496
    48
structure CommonCancelSums =
wenzelm@9436
    49
struct
wenzelm@9436
    50
  val mk_sum = mk_norm_sum;
wenzelm@9436
    51
  val dest_sum = dest_sum;
huffman@48372
    52
  val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
haftmann@35047
    53
  val norm_tac1 = Arith_Data.simp_all_tac [@{thm add_Suc}, @{thm add_Suc_right},
haftmann@35064
    54
    @{thm Nat.add_0}, @{thm Nat.add_0_right}];
haftmann@30496
    55
  val norm_tac2 = Arith_Data.simp_all_tac @{thms add_ac};
wenzelm@18328
    56
  fun norm_tac ss = norm_tac1 ss THEN norm_tac2 ss;
wenzelm@9436
    57
end;
wenzelm@9436
    58
wenzelm@9436
    59
structure EqCancelSums = CancelSumsFun
wenzelm@9436
    60
(struct
haftmann@30496
    61
  open CommonCancelSums;
wenzelm@9436
    62
  val mk_bal = HOLogic.mk_eq;
haftmann@38864
    63
  val dest_bal = HOLogic.dest_bin @{const_name HOL.eq} HOLogic.natT;
huffman@48372
    64
  val cancel_rule = mk_meta_eq @{thm nat_add_left_cancel};
wenzelm@9436
    65
end);
wenzelm@9436
    66
wenzelm@9436
    67
structure LessCancelSums = CancelSumsFun
wenzelm@9436
    68
(struct
haftmann@30496
    69
  open CommonCancelSums;
haftmann@35092
    70
  val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less};
haftmann@35092
    71
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less} HOLogic.natT;
huffman@48372
    72
  val cancel_rule = mk_meta_eq @{thm nat_add_left_cancel_less};
wenzelm@9436
    73
end);
wenzelm@9436
    74
wenzelm@9436
    75
structure LeCancelSums = CancelSumsFun
wenzelm@9436
    76
(struct
haftmann@30496
    77
  open CommonCancelSums;
haftmann@35092
    78
  val mk_bal = HOLogic.mk_binrel @{const_name Orderings.less_eq};
haftmann@35092
    79
  val dest_bal = HOLogic.dest_bin @{const_name Orderings.less_eq} HOLogic.natT;
huffman@48372
    80
  val cancel_rule = mk_meta_eq @{thm nat_add_left_cancel_le};
wenzelm@9436
    81
end);
wenzelm@9436
    82
wenzelm@9436
    83
structure DiffCancelSums = CancelSumsFun
wenzelm@9436
    84
(struct
haftmann@30496
    85
  open CommonCancelSums;
haftmann@35267
    86
  val mk_bal = HOLogic.mk_binop @{const_name Groups.minus};
haftmann@35267
    87
  val dest_bal = HOLogic.dest_bin @{const_name Groups.minus} HOLogic.natT;
huffman@48372
    88
  val cancel_rule = mk_meta_eq @{thm diff_cancel};
wenzelm@9436
    89
end);
wenzelm@9436
    90
huffman@48559
    91
fun nateq_cancel_sums ss = EqCancelSums.proc ss o Thm.term_of
huffman@48559
    92
fun natless_cancel_sums ss = LessCancelSums.proc ss o Thm.term_of
huffman@48559
    93
fun natle_cancel_sums ss = LeCancelSums.proc ss o Thm.term_of
huffman@48559
    94
fun natdiff_cancel_sums ss = DiffCancelSums.proc ss o Thm.term_of
wenzelm@24076
    95
wenzelm@24095
    96
end;