lean4-htt/library/init/data/nat/bitwise.lean
2017-05-30 13:05:37 -07:00

52 lines
1.5 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2017 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
prelude
import .lemmas init.meta.well_founded_tactics
universe u
namespace nat
def shiftl :
| m 0 := m
| m (n+1) := 2 * shiftl m n
def shiftr :
| m 0 := m
| m (n+1) := shiftr m n / 2
def bodd (n : ) : bool := n % 2 = 1
def test_bit (m n : ) : bool := bodd (shiftr m n)
def binary_rec {α : Type u} (f : bool → αα) (z : α) : α
| 0 := z
| (n+1) := let n' := shiftr (n+1) 1 in
have n' < (n+1), from (div_lt_iff_lt_mul _ _ dec_trivial).2 $
by note := nat.mul_lt_mul_of_pos_left (dec_trivial : 1 < 2) (succ_pos n);
rwa mul_one at this,
f (bodd (n+1)) n' (binary_rec n')
def size : := binary_rec (λ_ _, succ) 0
def bits : → list bool := binary_rec (λb _ IH, b :: IH) []
def bit (b : bool) : := cond b bit1 bit0
def bitwise (f : bool → bool → bool) : :=
binary_rec
(λa m Ia, binary_rec
(λb n _, bit (f a b) (Ia n))
(bit (f a ff) (Ia 0)))
(binary_rec (λb n Ib, bit (f ff b) Ib) 0)
def lor : := bitwise bor
def land : := bitwise band
def ldiff : := bitwise (λ a b, a && bnot b)
def lxor : := bitwise bxor
end nat