[Haskell-beginners] question on typeclasses and applicatives
daniel.is.fischer at web.de
Thu Sep 2 17:50:39 EDT 2010
On Thursday 02 September 2010 23:10:29, Alec Benzer wrote:
> Ah, ok, so the reason what I trying didn't work is because I used an
> actual type instead of a type variable?
Basically yes. There's a small additional problem because String is a type
synonym (and type synonyms are forbidden in H98 instance declarations).
Apparently, FlexibleInstances allows them in type variable positions, but
if you want to put a type synonym in the type constructor position, you
type STI = StateT Int
instance Foo (STI  a) where
you need FlexibleInstances ( is not a type variable) and
> I got confused because of the
> emphasis you put on * distinct *.
Sorry for that. I wanted to prevent "Why can't I have instance Foo (Bar a
a) where ... ?".
> And so, if I want to make Maps applicative functors without dealing
> with FlexibleInstances, I'd have to do something like this?
> import Control.Applicative
> import qualified Data.Map as M
> import Data.Monoid
> instance (Monoid k, Ord k) => Applicative (M.Map k) where
> pure x = M.fromList [(mempty,x)]
> fs <*> xs = M.fromList [(k1 `mappend` k2,v1 v2) | (k1,v1) <-
> M.assocs fs, (k2,v2) <- M.assocs xs]
> (sacrificing some functionality, since spaces won't get intercalated
> between keys if i use strings)
Yes, but why avoid FlexibleInstances?
More information about the Beginners