I am trying to define instance Show[MyType] so <br>show (x:xs :: MyType) would return a single string where substrings corresponding to list elements will be separated by "\n".<br>This would allow pretty printing of MyType list in several lines instead of one, as default Show does for lists.<br>
<br>For example:<br><br>data ShipInfo = Ship {<br> name :: String,<br> kind :: String,<br> canons :: Int<br>} deriving Show<br><br>s1 = Ship {name ="HMS Fly", kind = "sloop", canons=16} <br>
s2 = Ship {name ="HMS Surprise", kind = "frigate", canons=42} <br><br>-- Yet when I try to define:<br>instance (Show ShipInfo) => Show [ShipInfo] where<br> show (x:xs) = "<" ++ show x ++ ">" ++ show xs<br>
<br>-- I get this error:<br> Illegal instance declaration for `Show [ShipInfo]'<br> (The instance type must be of form (T a b c)<br> where T is not a synonym, and a,b,c are distinct type variables)<br>
In the instance declaration for `Show [ShipInfo]'<br>Failed, modules loaded: none.<br><br>-- On the other hand this definition:<br>instance (Show a) => Show [a] where<br>
show (x:xs) = "<" ++ show x ++ ">" ++ show xs<br>
<br>-- Gives a different error:<br>
Duplicate instance declarations:<br>
instance (Show a) => Show [a]<br>
-- Defined at C:/wks/haskell-wks/ShowMatrix.hs:37:0<br>
instance (Show a) => Show [a] -- Defined in GHC.Show<br>
Failed, modules loaded: none.<br>
<br>-- Note the last error implicitly tells us that defining Show [a] is possible in principle!<br>-- In fact it already defined in GHC.Show !!!<br><br>-- How to define Show [MyType] ?<br>
<br>Thanks!<br>Dmitri<br>