Cla*_*diu 6 error-handling monads haskell type-inference
我有一个函数检查类型是否是另一种类型的子类型:
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))
Run Code Online (Sandbox Code Playgroud)
我想做错误处理.我有以下定义:
instance Monad (Either String) where
return v = Right v
fail s = Left s
(Left s) >>= _ = Left s
(Right v) >>= f = f v
Run Code Online (Sandbox Code Playgroud)
有时我可以通过将st的结果视为Either来进行错误处理.例如,以下函数有效,并获取在st中调用"fail"导致的消息:
isSubType env cs t1 t2 = result where
result = case st env (S.empty) (t1, t2) of
Left msg -> Left msg
Right rel -> Right ()
Run Code Online (Sandbox Code Playgroud)
现在,我在里面,我想以递归方式调用它.出于某种原因,下面的代码深入嵌入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
Run Code Online (Sandbox Code Playgroud)
不键入检查,但给我以下错误:
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))
Run Code Online (Sandbox Code Playgroud)
为什么将st的结果视为'st'之外的工作而不是在'st'之外?如何更改我的代码以使其在内部工作?
我认为问题在于你打电话给show msg你应该使用的地方msg.因此,编译器无法推断出您的意思Either String; 它只知道你有满足Either t约束的地方Show t.更换show msg用msg应该修复它.
| 归档时间: |
|
| 查看次数: |
3791 次 |
| 最近记录: |