[Haskell] parsing and printing user defined types

Stephan Zaubzer s.zaubzer at gmx.at
Sat Sep 11 08:43:53 EDT 2004


Hi Guys!

I have defined the following type:
data Entry a = EmptyEntry | MakeEntry a a

i have defined show and read as follows:
---------------------------------------------
instance (Show a) => Show (Entry a)
     where showsPrec _ x  = showEntry x

showEntry :: (Show a) => Entry a -> String -> String
showEntry EmptyEntry      = shows "Empty"
showEntry (MakeEntry a b) =  shows a . (':':) . (' ':) . shows b

instance (Read a) => Read (Entry a)
     where readsPrec _ s = readEntry s

readEntry :: (Read a) => ReadS (Entry a)
readEntry s = [(EmptyEntry, x) | ("Empty", x) <- lex s]
	      ++
	      [(MakeEntry a b, w) | (a, u) <- reads s,
	                            (": ", v) <- lex u,
	                            (b, w) <- reads v]
------------------------------------------------

when I use show or read in any function, i get the following 
errormessage from hugs:

ERROR "./main.hgs":14 - Cannot justify constraints in explicitly typed 
binding
*** Expression    : getandparse
*** Type          : String -> Entry a
*** Given context : ()
*** Constraints   : Read a

printing an instance of Entry directly ( print (MakeEntry "Hello" 
"World") ) works. But using print in a function that takes a string, 
doesn't work...

Does anybody have an idea how to solve the problem?
thanks
Stephan


More information about the Haskell mailing list