Let’s consider a data type with many constructors:
data T = Alpha Int | Beta Int | Gamma Int Int | Delta Int
I want to write a function to check if two values are produced with the same constructor:
sameK (Alpha _) (Alpha _) = True
sameK (Beta _) (Beta _) = True
sameK (Gamma _ _) (Gamma _ _) = True
sameK _ _ = False
Maintaining sameK is not much fun, it cannot be checked for correctness easily. For example, when new constructors are added to T, it’s easy to forget to update sameK. I omitted one line to give an example:
-- it’s easy to forget:
-- sameK (Delta _) (Delta _) = True
The question is how to avoid boilerplate in sameK? Or how to make sure it checks for all T constructors?
The workaround I found is to use separate data types for each of the constructors, deriving Data.Typeable, and declaring a common type class, but I don’t like this solution, because it is much less readable and otherwise just a simple algebraic type works for me:
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Typeable
class Tlike t where
value :: t -> t
value = id
data Alpha = Alpha Int deriving Typeable
data Beta = Beta Int deriving Typeable
data Gamma = Gamma Int Int deriving Typeable
data Delta = Delta Int deriving Typeable
instance Tlike Alpha
instance Tlike Beta
instance Tlike Gamma
instance Tlike Delta
sameK :: (Tlike t, Typeable t, Tlike t', Typeable t') => t -> t' -> Bool
sameK a b = typeOf a == typeOf b
Look at the Data.Data module, the
toConstrfunction in particular. Along with{-# LANGUAGE DeriveDataTypeable #-}that will get you a 1-line solution which works for any type which is an instance ofData.Data. You don’t need to figure out all of SYB!If, for some reason (stuck with Hugs?), that is not an option, then here is a very ugly and very slow hack. It works only if your datatype is
Showable (e.g. by usingderiving (Show)– which means no function types inside, for example).constrTgets the string representation of the outermost constructor of aTvalue by showing it, chopping it up into words and then getting the first. I give an explicit type signature so you’re not tempted to use it on other types (and to evade the monomorphism restriction).Some notable disadvantages:
data T2 = Eta Int | T2 :^: T2)show, such as many library types.That said, it is Haskell 98… but that’s about the only nice thing I can say about it!