To help me learn Applicative Functors and Functors I thought it would be good fun to see how Either
is implemented with the typeclasses Functor
and Applicative
. Obviously I could just go ahead and read the code but I find it more useful to try and implement things myself to get a better understanding of things.
FYI I'm trying to implement the Haskell version of the results of this presentation http://applicative-errors-scala.googlecode.com/svn/artifacts/0.6/chunk-html/index.html
Anyway, this is what I have so far
data Validation a b = Success a | Failure b deriving (Show, Eq)
instance Functor (Validation a) where
fmap f (Failure x) = Failure x
fmap f (Success x) = Success (f x)
But whenever I try to run this with ghci
I just get the following error message: -
[1 of 1] Compiling Main ( t.hs, interpreted )
t.hs:5:35:
Couldn't match type `b' with `a1'
`b' is a rigid type variable bound by
the type signature for
fmap :: (a1 -> b) -> Validation a a1 -> Validation a b
at t.hs:4:5
`a1' is a rigid type variable bound by
the type signature for
fmap :: (a1 -> b) -> Validation a a1 -> Validation a b
at t.hs:4:5
Expected type: a
Actual type: b
In the return type of a call of `f'
In the first argument of `Success', namely `(f x)'
In the expression: Success (f x)
t.hs:5:37:
Couldn't match type `a' with `a1'
`a' is a rigid type variable bound by
the instance declaration at t.hs:3:30
`a1' is a rigid type variable bound by
the type signature for
fmap :: (a1 -> b) -> Validation a a1 -> Validation a b
at t.hs:4:5
In the first argument of `f', namely `x'
In the first argument of `Success', namely `(f x)'
In the expression: Success
I'm not really sure why this is, can anyone help?