Having some problems understanding how/if it's possible to make a function that operates on all different instances of a data family declaration.
Like so, I would want to write a Show instance for XList a. But it is not possible to use a general type a and map it to Char and () as in this case.
Something like
instance Show (XList a) where
show XNil = "hej"
show (XListUnit whatever) = show whatever
will not work. I read somewhere (forgot where, sorry) that to do this, you have to use GADTs. Is this true, and if so why?
The only thing I got working was the following, using different functions for the different XList instances. which is hyper ugly:
{-# LANGUAGE TypeFamilies #-}
data family XList a
data instance XList Char = XCons !Char !(XList Char) | XNil
data instance XList () = XListUnit !Int
format' :: XList Char -> String
format' XNil = "nil"
format' (XCons elem rest) = [elem] ++ ", " ++ format' rest
format'' :: XList () -> String
format'' (XListUnit x) = concat ["()" | _ <- [1..x]]