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内:
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的结果作为外擦出火花'st'但不在里面?我怎样才能改变我的代码,使其在内部也起作用?
您似乎正在重新实施[Control.Monad.Error](http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-Error.html)? – ephemient 2009-06-03 02:16:25