[Haskell-beginners] question on typeclasses and applicatives

Alec Benzer alecbenzer at gmail.com
Thu Sep 2 18:17:22 EDT 2010


> Technically, the version with the intercalated spaces didn't satisfy
> the Applicative laws anyway.  For example, if <*> inserts a space it
> is not the case that
>
>  pure f <*> x = f <$> x
>
> since there would be an extra space introduced on the left-hand side.

That could be fixed by replacing k1 ++ " " ++ k2 with k1 ++ (if null
k1 then "" else " ") ++ k2, or something though, couldn't it?


> Sure.  Although there's really no reason to avoid FlexibleInstances.

> Yes, but why avoid FlexibleInstances?

I guess I'm still sort of confused or perturbed with why it's disabled
by default. If the compiler has the ability to do it and there are no
problems with doing it, why not just allow it without requiring you to
pass a flag to the compiler?

On Thu, Sep 2, 2010 at 5:50 PM, Daniel Fischer <daniel.is.fischer at web.de> wrote:
> 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
> need TypeSynonymInstances.
>
> So for
>
> import Control.Monad.State
>
> type STI = StateT Int
>
> instance Foo (STI [] a) where
>
> you need FlexibleInstances ([] is not a type variable) and
> TypeSynonymInstances.
>
>> 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 mailing list