Чтобы помочь мне научиться аппликативным функторам и функторам, я подумал, что было бы неплохо увидеть, как Either
реализуется с помощью типов Functor
и Applicative
. Очевидно, я мог бы просто пойти дальше и прочитать код, но я считаю более полезным попытаться сам реализовать вещи, чтобы лучше понять вещи.
FYI Я пытаюсь реализовать версию результатов этой презентации Haskell http://applicative-errors-scala.googlecode.com/svn/artifacts/0.6/chunk-html/index.html
Во всяком случае, это то, что я до сих пор
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)
Но всякий раз, когда я пытаюсь запустить это с помощью ghci
, я получаю следующее сообщение об ошибке: -
[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
Я не уверен, почему это так, может ли кто-нибудь помочь?