src/HOL/Tools/boolean_algebra_cancel.ML
author wenzelm
Tue, 05 Dec 2023 20:56:51 +0100
changeset 79134 5f0bbed1c606
parent 70490 c42a0a0a9a8d
child 80722 b7d051e25d9d
permissions -rw-r--r--
misc tuning and clarification; eliminate clones (see also 3ae09d27ee7a);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62058
1cfd5d604937 updated headers;
wenzelm
parents: 61629
diff changeset
     1
(*  Title:      HOL/Tools/boolean_algebra_cancel.ML
61629
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     2
    Author:     Andreas Lochbihler, ETH Zurich
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     3
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     4
Simplification procedures for boolean algebras:
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     5
- Cancel complementary terms sup and inf.
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     6
*)
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     7
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     8
signature BOOLEAN_ALGEBRA_CANCEL =
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
     9
sig
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    10
  val cancel_sup_conv: conv
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    11
  val cancel_inf_conv: conv
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    12
end
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    13
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    14
structure Boolean_Algebra_Cancel: BOOLEAN_ALGEBRA_CANCEL =
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    15
struct
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    16
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    17
fun move_to_front rule path = Conv.rewr_conv (Library.foldl (op RS) (rule, path))
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    18
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 62391
diff changeset
    19
fun add_atoms sup pos path (t as Const (\<^const_name>\<open>Lattices.sup\<close>, _) $ x $ y) =
61629
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    20
    if sup then
70490
c42a0a0a9a8d prefer named lemmas -- more compact proofterms;
wenzelm
parents: 67149
diff changeset
    21
      add_atoms sup pos (@{thm boolean_algebra_cancel.sup1}::path) x #>
c42a0a0a9a8d prefer named lemmas -- more compact proofterms;
wenzelm
parents: 67149
diff changeset
    22
      add_atoms sup pos (@{thm boolean_algebra_cancel.sup2}::path) y
61629
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    23
    else cons ((pos, t), path)
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 62391
diff changeset
    24
  | add_atoms sup pos path (t as Const (\<^const_name>\<open>Lattices.inf\<close>, _) $ x $ y) =
61629
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    25
    if not sup then
70490
c42a0a0a9a8d prefer named lemmas -- more compact proofterms;
wenzelm
parents: 67149
diff changeset
    26
      add_atoms sup pos (@{thm boolean_algebra_cancel.inf1}::path) x #>
c42a0a0a9a8d prefer named lemmas -- more compact proofterms;
wenzelm
parents: 67149
diff changeset
    27
      add_atoms sup pos (@{thm boolean_algebra_cancel.inf2}::path) y
61629
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    28
    else cons ((pos, t), path)
67149
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 62391
diff changeset
    29
  | add_atoms _ _ _ (Const (\<^const_name>\<open>Orderings.bot\<close>, _)) = I
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 62391
diff changeset
    30
  | add_atoms _ _ _ (Const (\<^const_name>\<open>Orderings.top\<close>, _)) = I
e61557884799 prefer control symbol antiquotations;
wenzelm
parents: 62391
diff changeset
    31
  | add_atoms _ pos path (Const (\<^const_name>\<open>Groups.uminus\<close>, _) $ x) = cons ((not pos, x), path)
61629
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    32
  | add_atoms _ pos path x = cons ((pos, x), path);
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    33
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    34
fun atoms sup pos t = add_atoms sup pos [] t []
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    35
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    36
val coeff_ord = prod_ord bool_ord Term_Ord.term_ord
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    37
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    38
fun find_common ord xs ys =
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    39
  let
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    40
    fun find (xs as (x, px)::xs') (ys as (y, py)::ys') =
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    41
        (case ord (x, y) of
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    42
          EQUAL => SOME (fst x, px, py)
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    43
        | LESS => find xs' ys
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    44
        | GREATER => find xs ys')
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    45
      | find _ _ = NONE
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    46
    fun ord' ((x, _), (y, _)) = ord (x, y)
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    47
  in
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    48
    find (sort ord' xs) (sort ord' ys)
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    49
  end
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    50
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    51
fun cancel_conv sup rule ct =
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    52
  let
70490
c42a0a0a9a8d prefer named lemmas -- more compact proofterms;
wenzelm
parents: 67149
diff changeset
    53
    val rule0 =
c42a0a0a9a8d prefer named lemmas -- more compact proofterms;
wenzelm
parents: 67149
diff changeset
    54
      if sup then @{thm boolean_algebra_cancel.sup0} else @{thm boolean_algebra_cancel.inf0}
61629
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    55
    fun cancel1_conv (pos, lpath, rpath) =
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    56
      let
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    57
        val lconv = move_to_front rule0 lpath
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    58
        val rconv = move_to_front rule0 rpath
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    59
        val conv1 = Conv.combination_conv (Conv.arg_conv lconv) rconv
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    60
      in
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    61
        conv1 then_conv Conv.rewr_conv (rule pos)
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    62
      end
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    63
    val ((_, lhs), rhs) = (apfst dest_comb o dest_comb) (Thm.term_of ct)
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    64
    val common = find_common coeff_ord (atoms sup true lhs) (atoms sup false rhs)
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    65
    val conv =
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    66
      case common of NONE => Conv.no_conv
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    67
      | SOME x => cancel1_conv x
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    68
  in conv ct end
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    69
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    70
val cancel_sup_conv = cancel_conv true (fn pos => if pos then mk_meta_eq @{thm sup_cancel_left1} else mk_meta_eq @{thm sup_cancel_left2})
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    71
val cancel_inf_conv = cancel_conv false (fn pos => if pos then mk_meta_eq @{thm inf_cancel_left1} else mk_meta_eq @{thm inf_cancel_left2})
90f54d9e63f2 cancel complementary terms as arguments to sup/inf in boolean algebras
Andreas Lochbihler
parents:
diff changeset
    72
62391
1658fc9b2618 more canonical names
nipkow
parents: 62058
diff changeset
    73
end