我正在尝试构建一个简单的 monad 计算器,但由于某种原因它无法编译。

{-# LANGUAGE GADTs #-}

module Main where

data Result a = Value a | Undefined deriving (Show, Eq)

data Calculator x a where
  Bind :: Calculator x a -> (a -> Calculator x b) -> Calculator x b
  Return :: a -> Calculator x a
  Add :: a -> a -> Calculator x a
  Div :: a -> a -> Calculator x a

instance Monad (Calculator x) where
  return = Return
  (>>=) = Bind

calculate :: Calculator (Result Integer) Integer
calculate = do
  value <- addr 1 2
  divr 1 value

addr :: a -> a -> Calculator (Result a) a
addr a b = Add a b

divr :: a -> a -> Calculator (Result a) a
divr a b = Div a b

eval :: (Integral a, Num a, Eq a) => Calculator (Result a) a -> Result a
eval (Add a b) = Value (a + b)
eval (Div a b)
  | b == 0    = Undefined
  | otherwise = Value $ a `div` b

eval (Bind m f) =
  case eval m of
    Value v -> eval $ f v
    _       -> undefined -- for now

我得到的错误是这个
Main.hs:35:13:
    Could not deduce (a1 ~ a)
    from the context (Integral a, Num a, Eq a)
      bound by the type signature for
                 eval :: (Integral a, Num a, Eq a) =>
                         Calculator (Result a) a -> Result a
      at Main.hs:28:9-72
      ‘a1’ is a rigid type variable bound by
           a pattern with constructor
             Bind :: forall x b a.
                     Calculator x a -> (a -> Calculator x b) -> Calculator x b,
           in an equation for ‘eval’
           at Main.hs:34:7
      ‘a’ is a rigid type variable bound by
          the type signature for
            eval :: (Integral a, Num a, Eq a) =>
                    Calculator (Result a) a -> Result a
          at Main.hs:28:9
    Expected type: Calculator (Result a) a
      Actual type: Calculator (Result a) a1
    Relevant bindings include
      f :: a1 -> Calculator (Result a) a (bound at Main.hs:34:14)
      m :: Calculator (Result a) a1 (bound at Main.hs:34:12)
      eval :: Calculator (Result a) a -> Result a (bound at Main.hs:29:1)
    In the first argument of ‘eval’, namely ‘m’
    In the expression: eval m

错误发生在倒数第三行。有谁知道为什么?

我正在使用 ghci 7.8.4 。我错过了什么?

最佳答案

首先,需要将您的约束推送到 Calculator 构造函数中。要了解原因,让我们稍微重命名 Bind 签名中的类型变量:

data Calculator x a where
  Bind :: Calculator x b -> (b -> Calculator x a) -> Calculator x a

eval :: (Integral a, Num a, Eq a) => ...
eval (Bind m f) = ...

在这里, m 的类型为 Calculator x b ,并且对 a 的约束(即 (Integral a, Num a, Eq a) )不会对 b 施加任何限制,因此您没有希望在 eval 上递归调用 m

这个很容易解决:
data Calculator x a where
  Bind :: Calculator x a -> (a -> Calculator x b) -> Calculator x b
  Return :: a -> Calculator x a
  Add :: (Num a) => a -> a -> Calculator x a
  Div :: (Num a, Eq a, Integral a) => a -> a -> Calculator x a

然后 eval 不再需要对 a 的约束,因为它将由相关构造函数上的模式匹配提供。

另一个问题源于 xCalculator 类型参数。在
Bind :: Calculator x b -> (b -> Calculator x a) -> Calculator x a

请注意,x 的选择保持不变。因此,当您在 Bind m f :: Calculator (Result a) a 上进行模式匹配时,您会得到
m :: Calculator (Result a) b
f :: b -> Calculator (Result a) a

但是, eval 的类型只能实例化为 Calculator (Result a) a -> Result aCalculator (Result b) b -> Result b ,所以我们不能递归地应用到 m 上。

解决方案是在仿函数而不是类型上参数化 Calculator ,因为这样我们就可以输入 eval :: Calculator Result a -> Result a :
{-# LANGUAGE GADTs, KindSignatures #-}

module Main where

data Result a = Value a | Undefined deriving (Show, Eq)

data Calculator (f :: * -> *) a where
  Bind :: Calculator f a -> (a -> Calculator f b) -> Calculator f b
  Return :: a -> Calculator f a
  Add :: (Num a) => a -> a -> Calculator f a
  Div :: (Num a, Eq a, Integral a) => a -> a -> Calculator f a

instance Monad (Calculator f) where
  return = Return
  (>>=) = Bind

calculate :: Calculator Result Integer
calculate = do
  value <- addr 1 2
  divr 1 value

addr :: (Num a) => a -> a -> Calculator Result a
addr a b = Add a b

divr :: (Num a, Eq a, Integral a) => a -> a -> Calculator Result a
divr a b = Div a b

eval :: Calculator Result a -> Result a
eval (Add a b) = Value (a + b)
eval (Div a b)
  | b == 0    = Undefined
  | otherwise = Value $ a `div` b

eval (Bind m f) =
  case eval m of
    Value v -> eval $ f v
    _       -> undefined -- for now

关于haskell - Monad 错误(无法推断),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28800975/

10-16 05:38