haskell - few more monad - Error on the wall

in the module called "Control.Monad.Error" , it has defined a type called Error, adn that type hass been made an instance of the Monad (Either e), and the definition of that is

 

instance (Error e) => Monad (Either e) where  
    return x = Right x   
    Right x >>= f = f x  
    Left err >>= f = Left err  
    fail msg = Left (strMsg msg)  

 

before we going to the details of the instance definition, we will first check the types of the Either type.

ghci> :t Right 4  
Right 4 :: (Num t) => Either a t  
ghci> :t Left "out of cheese error"  
Left "out of cheese error" :: Either [Char] b  

 

so the left type is often denoted as the error type and the right is dnoted as the result type. and there is no strong correlation between the left side and the right side.(we might be able to see the implication very soon).. 

 

the return function just will return a result wrapped in the Either type. (this is straightforward) the right x when feeded to the f (a -> Monad b) will apply the function on the result contained in it, otherwise, if the value feed is a Left value (an error condition), then Left error is returned disregarding the function being applied. and there is a fail method, and it delegate the work to the strMsg function, what that does is to take an error in the form os string and return such a value (we can check the result type of strMsg as follow) .

 

ghci> :t strMsg  
strMsg :: (Error a) => String -> a  
ghci> strMsg "boom!" :: String  
"boom!"  

 

But since we usually use String to describe the error when using Either, we don't have to worry about this too much!, when a pattern match fails in do notation, a Left value is used to signify this failure. 

 

Below is an example of the usage. 

ghci> Left "boom" >>= \x -> return (x+1)  
Left "boom"  
ghci> Right 100 >>= \x -> Left "no way!"  
Left "no way!"  

 

but, as we said that the type parameter of Either is irrelevant, so that there is some implication on its use, here is the example. 

ghci> Right 3 >>= \x -> return (x + 100)  
  
<interactive>:1:0:  
    Ambiguous type variable `a' in the constraints:  
      `Error a' arising from a use of `it' at <interactive>:1:0-33  
      `Show a' arising from a use of `print' at <interactive>:1:0-33  
    Probable fix: add a type signature that fixes these type variable(s)  

 

so, you have to explicitly add the type signature. 

 

ghci> Right 3 >>= \x -> return (x + 100) :: Either String Int  
Right 103  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值