Haskell多态函数使用左右

时间:2018-04-28 09:28:41

标签: haskell either polymorphic-functions

我是Haskell的新手。我有类型:

type Variable = String
type Value = Float
type EvalError = [Variable]
type EvalResult = Either EvalError Value

我想创建一个函数,我将使用一个函数在2 EvalResult个类型上使用它,并相应地获得EvalResult。 如果我得到2个值类型,我想在它们上使用函数(例如sum / sub),如果我得到EvalError,我想返回EvalError。

我做了什么:

evalResultOp :: (a -> b -> c) -> EvalResult a -> EvalResult b -> EvalResult c
evalResultOp f (Left a) (Left b) = Left (a ++ b)
evalResultOp f (Left a) (Right b) = Left a
evalResultOp f (Right a) (Left b) = Left b
evalResultOp f (Right a) (Right b) = Right (f a b)

错误:

hs3.hs:46:34: error:
    • Expecting one fewer arguments to ‘EvalResult’
      Expected kind ‘* -> *’, but ‘EvalResult’ has kind ‘*’
    • In the type signature:
        evalResultOp :: (a -> b -> c)
                        -> EvalResult a -> EvalResult b -> EvalResult c
   |
46 | evalResultOp :: (a -> b -> c) -> EvalResult a -> EvalResult b -> EvalResult c    |                                  ^^^^^^^^^^^^

    hs3.hs:46:50: error:
        • Expecting one fewer arguments to ‘EvalResult’
          Expected kind ‘* -> *’, but ‘EvalResult’ has kind ‘*’
        • In the type signature:
            evalResultOp :: (a -> b -> c)
                            -> EvalResult a -> EvalResult b -> EvalResult c
       |
    46 | evalResultOp :: (a -> b -> c) -> EvalResult a -> EvalResult b -> EvalResult c    |                                                  ^^^^^^^^^^^^

    hs3.hs:46:66: error:
        • Expecting one fewer arguments to ‘EvalResult’
          Expected kind ‘* -> *’, but ‘EvalResult’ has kind ‘*’
        • In the type signature:
            evalResultOp :: (a -> b -> c)
                            -> EvalResult a -> EvalResult b -> EvalResult c
       |
    46 | evalResultOp :: (a -> b -> c) -> EvalResult a -> EvalResult b -> EvalResult c    | 

1 个答案:

答案 0 :(得分:3)

问题是您将EvalResult类型定义为:

type EvalResult = Either EvalError Value

因为Either是一个类型构造函数,它有两种类型,这意味着你现在已经构造了一个类型(没有任何类型参数)。如果你编写一个类型为签名(a -> b -> c) -> EvalResult a -> EvalResult b -> EvalResult c的函数,那么Haskell最终将必须构造类型EvalResult a ~ Either EvalError Value a,并且由于Either只需要两个类型参数,这没有任何意义。

我的猜测是你要定义

type EvalResult a = Either EvalError a

或更短:

type EvalResult = Either EvalError

现在EvakResult就像一个类型构造函数,它可以接受一个类型参数,然后该函数确实适用于类型。

我们可以通过写:

使实现更紧凑
import Control.Monad(liftM2)

evalResultOp :: (a -> b -> c) -> EvalResult a -> EvalResult b -> EvalResult c
evalResultOp f (Left a) (Left b) = Left (a ++ b)
evalResultOp f x y = liftM2 f x y

liftM2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c是一个适用于monadic类型m的函数。 Either a是monadic类型,定义为:

instance Monad (Either a) where
    return = Right
    (>>=) (Right x) f = f x
    (>>=) (Left l) _ = Left l

liftM2实现为:

liftM2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
liftM2 f xm ym = do
    x <- mx
    y <- my
    return (f x y)

这是语法糖:

liftM2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
liftM2 f xm ym = mx >>= (\x -> my >>= \y -> return (f x y))

所以基本上我们通过检查mx >>= (...)是否为mx来评估Right,如果它是Left,我们会返回Left的内容。由于我们已经用两个Left来处理案例,因此不再可能出现这种情况,因此我们知道第二个EvalResultRight,在这种情况下我们会返回第一个Left {1}}。如果mxRight x,我们现在会检查my >>= (..)并检查my的状态。如果myLeft,我们会再次返回Left,否则,我们会返回return (f x y),因为return来自Either a monad,实际上是Right,因此我们将f x y的内容包装在Right数据构造函数中。

相关问题