{-# LANGUAGE FlexibleInstances, UndecidableInstances #-} {-# LANGUAGE OverlappingInstances #-} -- ShowList -- a simple example of overlapping instances. -- See ShowListNO.hs for the same example without overlapping. module ShowList where import Data.List class ShowList a where showl :: [a] -> String -- General instance instance Show a => ShowList a where showl [] = "[]" showl xs = "[" ++ concat (intersperse "," (map show xs)) ++ "]" -- Special instances, for lists of specific types instance ShowList Bool where showl xs = concatMap toBit xs where toBit False = "0" toBit True = "1" instance ShowList 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" -}