Чтобы помочь мне изучить аппликативные функторы и функторы, я подумал, что было бы неплохо увидеть, как Либо
реализуется с помощью классов типов Функтор
и Аппликативный
. Очевидно, я мог бы просто пойти дальше и прочитать код, но я считаю более полезным попробовать и реализовать что-то самостоятельно, чтобы лучше понять вещи.
К вашему сведению, я пытаюсь реализовать версию 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
Я не совсем уверен, почему это так, может ли кто-нибудь помочь?