Defining of subset of constructors as valid values

Hi all I have the following code : data CmdTiming = RAS Time | CAS Time | PCH Time | ACT Time deriving (Ord,Show,Eq) data Command = RD_CMD {bank :: Int, col :: Int } | WR_CMD {bank :: Int, col :: Int} | ACT_CMD {bank :: Int, row :: Int} | PCH_CMD {bank :: Int} deriving Show I'd like to have something like : RD_CMD {bank :: Int, col :: Int,timing :: RAS Time } to limit possible values to RAS Time . The reason I use constructors is differentiate whether current value is applicable to some function , getting CmdTiming as parameter or not . I am not sure this is valid definition since RAS Time is not type . Regards, Lev

Hi Lev Empty data declarations are often used for doing this (and extending to GADTs if necessary - GADTs are covered in the GHC user manual). Here's a simple revision of your code that doesn't use GADTs - the code is literate Haskell so you should be able to copy-paste it into a file with .lhs as its extension (writing in literate Haskell means I can run the code myself in GHCi as I'm writing it, thus hopefully not posting code that is full of errors). Because each constructor for your original CmdTiming data type carried the same data (just Time) the translation to use EmptyDataDecls was straight forward - if you had different variants for the constructors it would have been more work.
{-# LANGUAGE EmptyDataDecls #-}
module RASTime where
Some dummy Time type to make the compiler happy
type Time = Int
data RAS data CAS data PCH data ACT
data CmdTiming a = CmdTiming a Time deriving (Ord,Show,Eq)
data Command = RD_CMD {bank :: Int, col :: Int, timing :: CmdTiming RAS } | WR_CMD {bank :: Int, col :: Int} | ACT_CMD {bank :: Int, row :: Int} | PCH_CMD {bank :: Int}
Note with EmptyDataDecls now used in the Command type you can't derive Show.
participants (2)
-
Lev Broido
-
Stephen Tetley