[Haskell-beginners] Typeclass problems

Stephen Blackheath [to Haskell-Beginners] mutilating.cauliflowers.stephen at blacksapphire.com
Thu Feb 4 17:30:32 EST 2010


Patrick,

I fixed it by adding a type signature to this line:

gen = map (const (1::Int))

The problem is that the literal '1' is of type Num t => t, which is
never tied to a concrete type anywhere.  It has to know the type
definitely before it can decide which type class to use.


Steve

Patrick LeBoutillier wrote:
> Hi all,
> 
> I've written a small type class that is meant to behave a bit like Show:
> 
> class Show a => Echo a where
>   echo :: a -> String
>   echoList :: [a] -> String
>   echoListSep :: a -> String
> 
>   echo = show
>   echoListSep _ = " "
> 
>   echoList [] = ""
>   echoList [x] = echo x
>   echoList (x:xs) = echo x ++ echoListSep x ++ echoList xs
> 
> 
> instance Echo Char where
>   echo c = [c]
>   echoListSep _ = ""
> 
> instance Echo a => Echo [a] where
>   echo = echoList
> 
> instance Echo Int where
> instance Echo Integer where
> instance Echo Double where
> instance Echo Float where
> 
> 
> gen = map (const 1)
> 
> f xs = map echo $ gen xs
> 
> 
> However the code doesn't compile on the last line.
> But it does compile if I replace 'echo' with 'show'.
> What's missing to make my typeclass behave like Show and accept input
> of any type (besides all the missing instances...)?
> I looked at the code for Show but I didn't see anything that looked "magical"...
> 
> 
> Thanks,
> 
> Patrick
> 
> 


More information about the Beginners mailing list