src/HOL/Tools/nat_arith.ML
author huffman
Fri Jul 27 19:27:21 2012 +0200 (2012-07-27)
changeset 48561 12aa0cb2b447
parent 48560 e0875d956a6b
child 48571 d68b74435605
permissions -rw-r--r--
move ML functions from nat_arith.ML to Divides.thy, which is the only place they are used
haftmann@30496
     1
(* Author: Markus Wenzel, Stefan Berghofer, and Tobias Nipkow
huffman@48560
     2
   Author: Brian Huffman
wenzelm@9436
     3
haftmann@30496
     4
Basic arithmetic for natural numbers.
wenzelm@9436
     5
*)
wenzelm@9436
     6
haftmann@30496
     7
signature NAT_ARITH =
haftmann@26101
     8
sig
huffman@48560
     9
  val cancel_diff_conv: conv
huffman@48560
    10
  val cancel_eq_conv: conv
huffman@48560
    11
  val cancel_le_conv: conv
huffman@48560
    12
  val cancel_less_conv: conv
haftmann@26101
    13
end;
wenzelm@9436
    14
haftmann@30496
    15
structure Nat_Arith: NAT_ARITH =
wenzelm@9436
    16
struct
wenzelm@9436
    17
huffman@48560
    18
val add1 = @{lemma "(A::'a::comm_monoid_add) == k + a ==> A + b == k + (a + b)"
huffman@48560
    19
      by (simp only: add_ac)}
huffman@48560
    20
val add2 = @{lemma "(B::'a::comm_monoid_add) == k + b ==> a + B == k + (a + b)"
huffman@48560
    21
      by (simp only: add_ac)}
huffman@48560
    22
val suc1 = @{lemma "A == k + a ==> Suc A == k + Suc a"
huffman@48560
    23
      by (simp only: add_Suc_right)}
huffman@48560
    24
val rule0 = @{lemma "(a::'a::comm_monoid_add) == a + 0"
huffman@48560
    25
      by (simp only: add_0_right)}
webertj@20217
    26
huffman@48560
    27
val norm_rules = map mk_meta_eq @{thms add_0_left add_0_right}
wenzelm@9436
    28
huffman@48560
    29
fun move_to_front path = Conv.every_conv
huffman@48560
    30
    [Conv.rewr_conv (Library.foldl (op RS) (rule0, path)),
huffman@48560
    31
     Conv.arg_conv (Raw_Simplifier.rewrite false norm_rules)]
wenzelm@9436
    32
huffman@48560
    33
fun add_atoms path (Const (@{const_name Groups.plus}, _) $ x $ y) =
huffman@48560
    34
      add_atoms (add1::path) x #> add_atoms (add2::path) y
huffman@48560
    35
  | add_atoms path (Const (@{const_name Nat.Suc}, _) $ x) =
huffman@48560
    36
      add_atoms (suc1::path) x
huffman@48560
    37
  | add_atoms _ (Const (@{const_name Groups.zero}, _)) = I
huffman@48560
    38
  | add_atoms path x = cons (x, path)
huffman@48560
    39
huffman@48560
    40
fun atoms t = add_atoms [] t []
huffman@48560
    41
huffman@48560
    42
exception Cancel
wenzelm@9436
    43
huffman@48560
    44
fun find_common ord xs ys =
huffman@48560
    45
  let
huffman@48560
    46
    fun find (xs as (x, px)::xs') (ys as (y, py)::ys') =
huffman@48560
    47
        (case ord (x, y) of
huffman@48560
    48
          EQUAL => (px, py)
huffman@48560
    49
        | LESS => find xs' ys
huffman@48560
    50
        | GREATER => find xs ys')
huffman@48560
    51
      | find _ _ = raise Cancel
huffman@48560
    52
    fun ord' ((x, _), (y, _)) = ord (x, y)
huffman@48560
    53
  in
huffman@48560
    54
    find (sort ord' xs) (sort ord' ys)
huffman@48560
    55
  end
wenzelm@9436
    56
huffman@48560
    57
fun cancel_conv rule ct =
huffman@48560
    58
  let
huffman@48560
    59
    val ((_, lhs), rhs) = (apfst dest_comb o dest_comb) (Thm.term_of ct)
huffman@48560
    60
    val (lpath, rpath) = find_common Term_Ord.term_ord (atoms lhs) (atoms rhs)
huffman@48560
    61
    val lconv = move_to_front lpath
huffman@48560
    62
    val rconv = move_to_front rpath
huffman@48560
    63
    val conv1 = Conv.combination_conv (Conv.arg_conv lconv) rconv
huffman@48560
    64
    val conv = conv1 then_conv Conv.rewr_conv rule
huffman@48560
    65
  in conv ct handle Cancel => raise CTERM ("no_conversion", []) end
wenzelm@9436
    66
huffman@48560
    67
val cancel_diff_conv = cancel_conv (mk_meta_eq @{thm diff_cancel})
huffman@48560
    68
val cancel_eq_conv = cancel_conv (mk_meta_eq @{thm add_left_cancel})
huffman@48560
    69
val cancel_le_conv = cancel_conv (mk_meta_eq @{thm add_le_cancel_left})
huffman@48560
    70
val cancel_less_conv = cancel_conv (mk_meta_eq @{thm add_less_cancel_left})
wenzelm@24076
    71
wenzelm@24095
    72
end;