Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 359261e

Browse files
committed
feat(data/nat): commutativity of bitwise operations (#3956)
1 parent 6b556cf commit 359261e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/data/nat/basic.lean

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,18 @@ by { cases n, cases h, apply succ_pos, }
935935
def bit_cases {C : ℕ → Sort u} (H : Π b n, C (bit b n)) (n : ℕ) : C n :=
936936
eq.rec_on n.bit_decomp (H (bodd n) (div2 n))
937937

938+
/-- If `f` is a commutative operation on bools such that `f ff ff = ff`, then `bitwise f` is also
939+
commutative. -/
940+
lemma bitwise_comm {f : bool → bool → bool} (hf : ∀ b b', f b b' = f b' b)
941+
(hf' : f ff ff = ff) (n m : ℕ) : bitwise f n m = bitwise f m n :=
942+
suffices bitwise f = function.swap (bitwise f), by conv_lhs { rw this },
943+
calc bitwise f = bitwise (function.swap f) : congr_arg _ $ funext $ λ _, funext $ hf _
944+
... = function.swap (bitwise f) : bitwise_swap hf'
945+
946+
lemma lor_comm (n m : ℕ) : lor n m = lor m n := bitwise_comm bool.bor_comm rfl n m
947+
lemma land_comm (n m : ℕ) : land n m = land m n := bitwise_comm bool.band_comm rfl n m
948+
lemma lxor_comm (n m : ℕ) : lxor n m = lxor m n := bitwise_comm bool.bxor_comm rfl n m
949+
938950
/- partial subtraction -/
939951

940952
/-- Partial predecessor operation. Returns `ppred n = some m`

0 commit comments

Comments
 (0)