]> gitweb.michael.orlitzky.com - hath.git/blob - src/Bit.hs
3799516c04c4514dd1a9241d0ea25ba753789438
[hath.git] / src / Bit.hs
1 module Bit where
2
3 import Test.QuickCheck
4
5
6 data Bit = None | Zero | One
7 deriving (Eq)
8
9 instance Show Bit where
10 show None = "None"
11 show Zero = "0"
12 show One = "1"
13
14
15 instance Arbitrary Bit where
16 arbitrary = elements [ Zero, One ]
17 coarbitrary _ = variant 0
18
19
20 bit_to_int :: Bit -> Int
21 bit_to_int None = -1
22 bit_to_int Zero = 0
23 bit_to_int One = 1
24
25 -- If we are passed a '0' or '1', convert it appropriately. Otherwise,
26 -- default to None.
27 bit_from_char :: Char -> Bit
28 bit_from_char '0' = Zero
29 bit_from_char '1' = One
30 bit_from_char _ = None