Applicative instance for Either String

Why does the following give me an error unless I enable FlexibleInstances? instance Applicative (Either String) where pure x = Right x Right g <*> Right x = Right (g x) Right _ <*> Left s = Left s Left s <*> _ = Left s

Oh I get it. I should use a type variable 's' instead of String.
instance Applicative (Either s) where pure x = Right x Right g <*> Right x = Right (g x) Right _ <*> Left s = Left s Left s <*> _ = Left s
I guess there is already a functor instance of Either s? Interesting how functors and applicative functors can be thought of as containers, but one thing that makes them flexible is that they can contain zero, one, or many pieces of data (depending on the functor), and they can contain something of a different sort entirely (like Left). Is it possible to make an applicative functor that contains auxiliary data unrelated to the "main" data. I think not, because then there's no way to define pure. Michael Mossey wrote:
Why does the following give me an error unless I enable FlexibleInstances?
instance Applicative (Either String) where pure x = Right x Right g <*> Right x = Right (g x) Right _ <*> Left s = Left s Left s <*> _ = Left s _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Const has an Applicative instance that doesn't touch the "subject" parameter i.e. the rightmost element of the type. -- Const - drops /b/ newtype Const a b = Const a deriving (Eq,Show) instance Functor (Const a) where fmap f (Const a) = Const a The applicative instance obliges the "data" i.e. the first parameter to be an instance of Monoid: instance Monoid a => Applicative (Const a) where pure b = Const mzero Const f <*> Const v = Const (f `mappend` v)
participants (2)
-
Michael Mossey
-
Stephen Tetley