X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FBit.hs;h=e3d90e420e2bffbbe97accd2ad08246731aacde4;hb=942b8ef3bc5830ca0defa62342d55550aea59934;hp=7c1840823b041774774f7d49519ff190f6f52033;hpb=5825a18ea1267c1f769a054c650b2761de7f291c;p=hath.git diff --git a/src/Bit.hs b/src/Bit.hs index 7c18408..e3d90e4 100644 --- a/src/Bit.hs +++ b/src/Bit.hs @@ -1,16 +1,46 @@ -module Bit where +-- | The Bit module contains the Bit data type, which is essentially a +-- renamed Boolean, and some convenience functions. +module Bit +where -data Bit = None | Zero | One - deriving (Eq, Show) +import Test.QuickCheck ( + Arbitrary, + arbitrary, + elements + ) + +data Bit = Zero | One + deriving (Enum, Eq) + +instance Show Bit where + show Zero = "0" + show One = "1" + + +instance Arbitrary Bit where + arbitrary = elements [ Zero, One ] + + +instance Ord Bit where + Zero <= Zero = True + Zero <= One = True + One <= Zero = False + One <= One = True + +instance Bounded Bit where + minBound = Zero + maxBound = One + + +-- | Convert a Bit to an Int. bit_to_int :: Bit -> Int -bit_to_int None = -1 bit_to_int Zero = 0 bit_to_int One = 1 --- If we are passed a '0' or '1', convert it appropriately. Otherwise, --- default to None. -bit_from_char :: Char -> Bit -bit_from_char '0' = Zero -bit_from_char '1' = One -bit_from_char _ = None +-- | If we are passed a '0' or '1', convert it +-- appropriately. Otherwise, return Nothing. +bit_from_char :: Char -> Maybe Bit +bit_from_char '0' = Just Zero +bit_from_char '1' = Just One +bit_from_char _ = Nothing