[Haskell-beginners] help with error

Tyler Hayes tgh at pdx.edu
Sat Feb 20 23:04:46 EST 2010


Thanks that helps a lot!

- Tyler

On Sun, 2010-02-21 at 04:36 +0100, Daniel Fischer wrote:
> Am Sonntag 21 Februar 2010 04:07:50 schrieb Tyler Hayes:
> > I'm getting an error and I do not know why...
> >
> > This is what I have:
> >
> >
> > class Finite a where
> >   elements' :: [a]
> >
> > instance (Finite a, Show a, Show b) => Show (a -> b) where
> >    show f = concat (map show [ a | a <- elements'::[a] ])
> >
> >
> > and this is what ghci gives me:
> >
> >
> >     Could not deduce (Finite a1) from the context ()
> >       arising from a use of `elements'' at Hw06.lhs:119:42-50
> >     Possible fix:
> >       add (Finite a1) to the context of an expression type signature
> >     In the expression: elements' :: [a]
> >     In a stmt of a list comprehension: a <- elements' :: [a]
> >     In the second argument of `map', namely
> >         `[a | a <- elements' :: [a]]'
> >
> >
> > What's going on here?
> 
> All type variables are implicitly universally quantified. Thus the type 
> variable a in [a | a <- elements' :: [a] ] is *not* the type variable a 
> from the signature, it's a fresh type variable (and promises something you 
> can't keep, that elements' belongs to *every* list type).
> 
> In GHC, you can bring the type variables from the signature into scope by 
> enabling ScopedTypeVariables and explicitly quantifying the type variables:
> 
> {-# LANGUAGE ScopedTypeVariables #-}
> -- or -XScopedTypeVariables on the command line
> 
> class Finite a where
>     elements' :: [a]
> 
> instance forall a b. (Finite a, Show a, Show b) => Show (a -> b) where
>     show f = concatMap show (elements' :: [a])
> 
> But that isn't really a good show instance, it involves neither b nor f, 
> perhaps you'd rather want
> 
> instance (Finite a, Show a, Show b) => Show (a -> b) where
>     show f = show [ (x, f x) | x <- elements' ]
> 
> and then you don't need ScopedTypeVariables and explicit forall.
> 
> >
> > Thanks for the help!
> > - Tyler
> 
> 



More information about the Beginners mailing list