lean4-htt/library/init/data/int/bitwise.lean
2017-07-26 11:52:10 +01:00

73 lines
2.1 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 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Mario Carneiro
-/
prelude
import init.data.int.basic init.data.nat.bitwise
universe u
namespace int
def div2 :
| (of_nat n) := n.div2
| -[1+ n] := -[1+ n.div2]
def bodd : → bool
| (of_nat n) := n.bodd
| -[1+ n] := bnot (n.bodd)
def bit (b : bool) : := cond b bit1 bit0
def test_bit : → bool
| (m : ) n := nat.test_bit m n
| -[1+ m] n := bnot (nat.test_bit m n)
def nat_bitwise (f : bool → bool → bool) (m n : ) : :=
cond (f ff ff) -[1+ nat.bitwise (λx y, bnot (f x y)) m n] (nat.bitwise f m n)
def bitwise (f : bool → bool → bool) :
| (m : ) (n : ) := nat_bitwise f m n
| (m : ) -[1+ n] := nat_bitwise (λ x y, f x (bnot y)) m n
| -[1+ m] (n : ) := nat_bitwise (λ x y, f (bnot x) y) m n
| -[1+ m] -[1+ n] := nat_bitwise (λ x y, f (bnot x) (bnot y)) m n
def lnot :
| (m : ) := -[1+ m]
| -[1+ m] := m
def lor :
| (m : ) (n : ) := nat.lor m n
| (m : ) -[1+ n] := -[1+ nat.ldiff n m]
| -[1+ m] (n : ) := -[1+ nat.ldiff m n]
| -[1+ m] -[1+ n] := -[1+ nat.land m n]
def land :
| (m : ) (n : ) := nat.land m n
| (m : ) -[1+ n] := nat.ldiff m n
| -[1+ m] (n : ) := nat.ldiff n m
| -[1+ m] -[1+ n] := -[1+ nat.lor m n]
def ldiff :
| (m : ) (n : ) := nat.ldiff m n
| (m : ) -[1+ n] := nat.land m n
| -[1+ m] (n : ) := -[1+ nat.lor m n]
| -[1+ m] -[1+ n] := nat.ldiff n m
def lxor :
| (m : ) (n : ) := nat.lxor m n
| (m : ) -[1+ n] := -[1+ nat.lxor m n]
| -[1+ m] (n : ) := -[1+ nat.lxor m n]
| -[1+ m] -[1+ n] := nat.lxor m n
def shiftl :
| (m : ) (n : ) := nat.shiftl m n
| (m : ) -[1+ n] := nat.shiftr m (nat.succ n)
| -[1+ m] (n : ) := -[1+ nat.shiftl' tt m n]
| -[1+ m] -[1+ n] := -[1+ nat.shiftr m (nat.succ n)]
def shiftr (m n : ) : := shiftl m (-n)
end int