src/HOL/Tools/group_cancel.ML
author huffman
Mon, 06 Aug 2012 15:12:18 +0200
changeset 49709 188cbbce880e
parent 49586 d68b74435605
child 55682 b1d955791529
permissions -rw-r--r--
modify group_cancel simprocs so that they can cancel multiple terms at once
huffman@49571
     1
(*  Title:      HOL/Tools/group_cancel.ML
huffman@49571
     2
    Author:     Brian Huffman, TU Munich
huffman@49571
     3
huffman@49571
     4
Simplification procedures for abelian groups:
huffman@49571
     5
- Cancel complementary terms in sums.
huffman@49571
     6
- Cancel like terms on opposite sides of relations.
huffman@49571
     7
*)
huffman@49571
     8
huffman@49571
     9
signature GROUP_CANCEL =
huffman@49571
    10
sig
huffman@49571
    11
  val cancel_diff_conv: conv
huffman@49571
    12
  val cancel_eq_conv: conv
huffman@49571
    13
  val cancel_le_conv: conv
huffman@49571
    14
  val cancel_less_conv: conv
huffman@49571
    15
  val cancel_add_conv: conv
huffman@49571
    16
end
huffman@49571
    17
huffman@49571
    18
structure Group_Cancel: GROUP_CANCEL =
huffman@49571
    19
struct
huffman@49571
    20
huffman@49571
    21
val add1 = @{lemma "(A::'a::comm_monoid_add) == k + a ==> A + b == k + (a + b)"
huffman@49571
    22
      by (simp only: add_ac)}
huffman@49571
    23
val add2 = @{lemma "(B::'a::comm_monoid_add) == k + b ==> a + B == k + (a + b)"
huffman@49571
    24
      by (simp only: add_ac)}
huffman@49571
    25
val sub1 = @{lemma "(A::'a::ab_group_add) == k + a ==> A - b == k + (a - b)"
huffman@49571
    26
      by (simp only: add_diff_eq)}
huffman@49571
    27
val sub2 = @{lemma "(B::'a::ab_group_add) == k + b ==> a - B == - k + (a - b)"
huffman@49571
    28
      by (simp only: diff_minus minus_add add_ac)}
huffman@49571
    29
val neg1 = @{lemma "(A::'a::ab_group_add) == k + a ==> - A == - k + - a"
huffman@49571
    30
      by (simp only: minus_add_distrib)}
huffman@49571
    31
val rule0 = @{lemma "(a::'a::comm_monoid_add) == a + 0"
huffman@49571
    32
      by (simp only: add_0_right)}
huffman@49571
    33
val minus_minus = mk_meta_eq @{thm minus_minus}
huffman@49571
    34
huffman@49571
    35
fun move_to_front path = Conv.every_conv
huffman@49571
    36
    [Conv.rewr_conv (Library.foldl (op RS) (rule0, path)),
huffman@49571
    37
     Conv.arg1_conv (Conv.repeat_conv (Conv.rewr_conv minus_minus))]
huffman@49571
    38
huffman@49571
    39
fun add_atoms pos path (Const (@{const_name Groups.plus}, _) $ x $ y) =
huffman@49571
    40
      add_atoms pos (add1::path) x #> add_atoms pos (add2::path) y
huffman@49571
    41
  | add_atoms pos path (Const (@{const_name Groups.minus}, _) $ x $ y) =
huffman@49571
    42
      add_atoms pos (sub1::path) x #> add_atoms (not pos) (sub2::path) y
huffman@49571
    43
  | add_atoms pos path (Const (@{const_name Groups.uminus}, _) $ x) =
huffman@49571
    44
      add_atoms (not pos) (neg1::path) x
huffman@49571
    45
  | add_atoms _ _ (Const (@{const_name Groups.zero}, _)) = I
huffman@49571
    46
  | add_atoms pos path x = cons ((pos, x), path)
huffman@49571
    47
huffman@49571
    48
fun atoms t = add_atoms true [] t []
huffman@49571
    49
huffman@49571
    50
val coeff_ord = prod_ord bool_ord Term_Ord.term_ord
huffman@49571
    51
huffman@49709
    52
fun find_all_common ord xs ys =
huffman@49571
    53
  let
huffman@49571
    54
    fun find (xs as (x, px)::xs') (ys as (y, py)::ys') =
huffman@49571
    55
        (case ord (x, y) of
huffman@49709
    56
          EQUAL => (px, py) :: find xs' ys'
huffman@49571
    57
        | LESS => find xs' ys
huffman@49571
    58
        | GREATER => find xs ys')
huffman@49709
    59
      | find _ _ = []
huffman@49571
    60
    fun ord' ((x, _), (y, _)) = ord (x, y)
huffman@49571
    61
  in
huffman@49571
    62
    find (sort ord' xs) (sort ord' ys)
huffman@49571
    63
  end
huffman@49571
    64
huffman@49571
    65
fun cancel_conv rule ct =
huffman@49571
    66
  let
huffman@49709
    67
    fun cancel1_conv (lpath, rpath) =
huffman@49709
    68
      let
huffman@49709
    69
        val lconv = move_to_front lpath
huffman@49709
    70
        val rconv = move_to_front rpath
huffman@49709
    71
        val conv1 = Conv.combination_conv (Conv.arg_conv lconv) rconv
huffman@49709
    72
      in
huffman@49709
    73
        conv1 then_conv Conv.rewr_conv rule
huffman@49709
    74
      end
huffman@49571
    75
    val ((_, lhs), rhs) = (apfst dest_comb o dest_comb) (Thm.term_of ct)
huffman@49709
    76
    val common = find_all_common coeff_ord (atoms lhs) (atoms rhs)
huffman@49709
    77
    val conv =
huffman@49709
    78
      if null common then Conv.no_conv
huffman@49709
    79
      else Conv.every_conv (map cancel1_conv common)
huffman@49586
    80
  in conv ct end
huffman@49571
    81
huffman@49571
    82
val cancel_diff_conv = cancel_conv (mk_meta_eq @{thm add_diff_cancel_left})
huffman@49571
    83
val cancel_eq_conv = cancel_conv (mk_meta_eq @{thm add_left_cancel})
huffman@49571
    84
val cancel_le_conv = cancel_conv (mk_meta_eq @{thm add_le_cancel_left})
huffman@49571
    85
val cancel_less_conv = cancel_conv (mk_meta_eq @{thm add_less_cancel_left})
huffman@49571
    86
huffman@49571
    87
val diff_minus_eq_add = mk_meta_eq @{thm diff_minus_eq_add}
huffman@49571
    88
val add_eq_diff_minus = Thm.symmetric diff_minus_eq_add
huffman@49571
    89
val cancel_add_conv = Conv.every_conv
huffman@49571
    90
  [Conv.rewr_conv add_eq_diff_minus,
huffman@49571
    91
   cancel_diff_conv,
huffman@49571
    92
   Conv.rewr_conv diff_minus_eq_add]
huffman@49571
    93
huffman@49571
    94
end