Instances for BaudRate from System.Posix.Terminal

Heka Treep zena.treep at gmail.com
Mon May 2 17:01:17 CEST 2011


Well, with this things become too complicated :) When we deriving
instances for Eq, Ord or Show typeclasses for BaudRate it has the
right semantic. But to ensure the correct semantic for the Enum
typeclass we need to do all manually:

1. succ means "take a greater baud rate", when pred means "take a
lesser baud rate". succ B115200 = _|_ in terms of "there is no greater
baud rate" (just like head [] = _|_ means "there is no heads anymore")
and pred B0 = _|_ in terms of "there is no lesser baud rate".

2. toEnum / fromEnum is usefull for converting baud rates to/from
integers (e.g. for give it to foreign code).

3. enumFrom, enumFromThen, enumFromTo, enumFromThenTo serve to create
a range of available baud rates.

4. Infinite lists of baud rates is do not make sense of course,
because there is no such hardware :)

Finally, instances can be defined like this (but I don't write
enumFromThenTo and enumFromThen methods):

data BaudRate
  = B0
  | B50
  | B75
  | B110
  | B134
  | B150
  | B200
  | B300
  | B600
  | B1200
  | B1800
  | B2400
  | B4800
  | B9600
  | B19200
  | B38400
  | B57600
  | B115200
  deriving ( Show, Eq, Ord )

instance Enum BaudRate where

  fromEnum b = case b of
    B0      -> 0
    B50     -> 50
    B75     -> 75
    B110    -> 110
    B134    -> 134
    B150    -> 150
    B200    -> 200
    B300    -> 300
    B600    -> 600
    B1200   -> 1200
    B1800   -> 1800
    B2400   -> 2400
    B4800   -> 4800
    B9600   -> 9600
    B19200  -> 19200
    B38400  -> 38400
    B57600  -> 57600
    B115200 -> 115200

  toEnum i = case i of
    0       -> B0
    50      -> B50
    75      -> B75
    110     -> B110
    134     -> B134
    150     -> B150
    200     -> B200
    300     -> B300
    600     -> B600
    1200    -> B1200
    1800    -> B1800
    2400    -> B2400
    4800    -> B4800
    9600    -> B9600
    19200   -> B19200
    38400   -> B38400
    57600   -> B57600
    115200  -> B115200
    _       -> error $ "unsupported baudrate " ++ show i

  succ B115200 = error "There is no greater baud rate."
  succ b       = head $ tail $ dropWhile (/= b) baudRates

  pred B0      = error "There is no lesser baud rate."
  pred b       = last $ takeWhile (/= b) baudRates

  enumFromTo b b' | b > b'    = []
                  | b == b'   = [b]
                  | otherwise = b : enumFromTo (succ b) b'

  enumFrom b = enumFromTo b B115200

  enumFromThenTo = undefined
  enumFromThen   = undefined

baudRates :: [BaudRate]
baudRates =
  [ B0
  , B50
  , B75
  , B110
  , B134
  , B150
  , B200
  , B300
  , B600
  , B1200
  , B1800
  , B2400
  , B4800
  , B9600
  , B19200
  , B38400
  , B57600
  , B115200
  ]

2011/4/26, Stephan Friedrichs <deduktionstheorem at googlemail.com>:
> On 26/04/11 10:19, Heka Treep wrote:
>> Hi, the c-libraries use BaudRate as integers, so how about to add Enum
>> instance for BaudRate? It allow users to use fromEnum, toEnum methods.
>>
>> Like this:
>>
>> module System.Posix.Terminal (
>> ...
>>
>> data BaudRate
>>   = B0
>>   ...
>>   | B115200
>>   deriving ( Eq, Ord )
>>
>> instance Enum BaudRate where
>>   fromEnum b = case b of
>>     B0      -> 0
>>     B50     -> 50
>>     B75     -> 75
>> [...]
>>   toEnum i = case i of
>>     0       -> B0
>>     50      -> B50
>>     75      -> B75
>> [...]
>
> This way, the default implementations of
>  - succ, pred
>  - enumFrom, enumFromTo, enumFromThen, enumFromThenTo
> will fail, because, e.g. succ B0 ~> toEnum (0 + 1) ~> error ...
> Do you want succ B0 to be B50? Does [B0,B75..] make sense?
>
> //Stephan
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>



More information about the Libraries mailing list