How to extract value from monadic action

前端 未结 8 2321

Is there a built-in function with signature :: (Monad m) => m a -> a ?

Hoogle tells that there is no such function.

Can you explain why?<

8条回答
  •  一向
    一向 (楼主)
    2020-11-27 05:39

    There is probably a better answer than this, but one way to see why you cannot have a type (Monad m) => m a -> a is to consider a null monad:

    data Null a = Null
    
    instance Monad Null where
        return a = Null
        ma >>= f = Null
    

    Now (Monad m) => m a -> a means Null a -> a, ie getting something out of nothing. You can't do that.

提交回复
热议问题