6

我有一个函数可以检查一个类型是否是另一种类型的子类型:

st :: Monad m => Map String Type  -- ^type environment
   -> Set (Type, Type) -- ^assumed subtypes
   -> (Type, Type) -- ^we are checking if lhs <: rhs      
   -> m (Set (Type, Type))

我想做错误处理。我有以下定义:

instance Monad (Either String) where
  return v = Right v
  fail s = Left s
  (Left s) >>= _ = Left s
  (Right v) >>= f = f v

有时我可以通过将 st 的结果视为 Either 来进行错误处理。例如,以下函数有效,并获取我在 st 中调用“失败”所产生的消息:

isSubType env cs t1 t2 = result where
  result = case st env (S.empty) (t1, t2) of
    Left msg -> Left msg
    Right rel -> Right ()

现在,我在 st 里面,我想递归地调用它。出于某种原因,以下代码嵌套在 st 深处:

  let do_t1 rel t1 = case st env rel (t1, t2) of
        Left msg -> fail $ printf "type %s in the union is not a subtype\
                           \ of the rhs, %s, because: %s" (renderType t1)
                           (renderType t2) (show msg)
        Right rel -> return rel

不输入检查,但给我以下错误:

 No instance for (Monad (Either t))
      arising from a use of `st'
                   at src/TypedJavaScript/Types.hs:386:24-42
    Possible fix: add an instance declaration for (Monad (Either t))

为什么将 st 的结果视为 Either 在 'st' 之外工作而不在内部工作?如何更改我的代码以使其也可以在内部工作?

4

1 回答 1

5

我认为问题是你打电话给show msg你应该使用的地方msg。结果,编译器无法推断出您的意思Either String;它所知道的就是你有满足Either t约束的地方Show t。替换show msgmsg应该修复它。

于 2009-06-03T01:55:31.193 回答