]> gitweb.michael.orlitzky.com - hath.git/blob - src/Bit.hs
Bump the version number to 0.0.4 in hath.cabal.
[hath.git] / src / Bit.hs
1 -- | The Bit module contains the Bit data type, which is essentially a
2 -- renamed Boolean, and some convenience functions.
3 module Bit
4 where
5
6 import Test.QuickCheck (
7 Arbitrary,
8 arbitrary,
9 elements
10 )
11
12
13 data Bit = Zero | One
14 deriving (Enum, Eq)
15
16 instance Show Bit where
17 show Zero = "0"
18 show One = "1"
19
20
21 instance Arbitrary Bit where
22 arbitrary = elements [ Zero, One ]
23
24
25 instance Ord Bit where
26 Zero <= Zero = True
27 Zero <= One = True
28 One <= Zero = False
29 One <= One = True
30
31 instance Bounded Bit where
32 minBound = Zero
33 maxBound = One
34
35
36 -- | Convert a Bit to an Int.
37 bit_to_int :: Bit -> Int
38 bit_to_int Zero = 0
39 bit_to_int One = 1
40
41 -- | If we are passed a '0' or '1', convert it
42 -- appropriately. Otherwise, return Nothing.
43 bit_from_char :: Char -> Maybe Bit
44 bit_from_char '0' = Just Zero
45 bit_from_char '1' = Just One
46 bit_from_char _ = Nothing