{-# LANGUAGE FlexibleInstances, UndecidableInstances, TypeOperators #-} {-# LANGUAGE MultiParamTypeClasses, TypeFamilies, ScopedTypeVariables #-} -- ShowListNO -- a simple example of eliminating overlapping instances. -- See ShowList.hs for the same example with overlapping. module ShowListNO where import Data.List import TTypeable class ShowList a where showl :: [a] -> String -- List of special cases type Special = TYPEOF Bool :/ TYPEOF Char :/ NIL -- The only instance of ShowList, discriminating between the -- general and special cases instance (flag ~ (Member AC_TREPEQ (TYPEOF a) Special), ShowList' flag a) => ShowList a where showl = showl' (undefined::flag) -- A replica of the original class and its instances (see ShowLits.hs), -- with the extra parameter 'flag'. The extra parameter resolves overlapping. class ShowList' flag a where showl' :: flag -> [a] -> String -- General instance instance Show a => ShowList' HFalse a where showl' _ [] = "[]" showl' _ xs = "[" ++ concat (intersperse "," (map show xs)) ++ "]" -- Special instances, for lists of specific types instance ShowList' HTrue Bool where showl' _ xs = concatMap toBit xs where toBit False = "0" toBit True = "1" instance ShowList' HTrue Char where showl' _ = id t1 = showl [10,11::Int] t2 = showl "abc" t3 = showl [True,False,True] {- *ShowList> t1 "[10,11]" *ShowList> t2 "abc" *ShowList> t3 "101" -}