How to apply higher order function to an effectful function in Haskell?

前端 未结 2 828
礼貌的吻别
礼貌的吻别 2020-12-16 00:50

I have too functions:

higherOrderPure :: (a -> b) -> c
effectful :: Monad m => (a -> m b)

I\'d like to apply the first function

2条回答
  •  再見小時候
    2020-12-16 01:04

    The type

    Monad m => ((a -> b) -> c) -> (a -> m b) -> m c
    

    is not inhabited, i.e., there is no term t having that type (unless you exploit divergence, e.g. infinite recursion, error, undefined, etc.).

    This means, unfortunately, that it is impossible to implement the operator someOp.

    Proof

    To prove that it is impossible to construct such a t, we proceed by contradiction. Assume t exists with type

    t :: Monad m => ((a -> b) -> c) -> (a -> m b) -> m c
    

    Now, specialize c to (a -> b). We obtain

    t :: Monad m => ((a -> b) -> a -> b) -> (a -> m b) -> m (a -> b)
    

    Hence

    t id :: Monad m => (a -> m b) -> m (a -> b)
    

    Then, specialize the monad m to the continuation monad (* -> r) -> r

    t id :: (a -> (b -> r) -> r) -> ((a -> b) -> r) -> r
    

    Further specialize r to a

    t id :: (a -> (b -> a) -> a) -> ((a -> b) -> a) -> a
    

    So, we obtain

    t id const :: ((a -> b) -> a) -> a
    

    Finally, by the Curry-Howard isomorphism, we deduce that the following is an intuitionistic tautology:

    ((A -> B) -> A) -> A
    

    But the above is the well-known Peirce's law, which is not provable in intuitionistic logic. Hence we obtain a contradiction.

    Conclusion

    The above proves that t can not be implemented in a general way, i.e., working in any monad. In a specific monad this may still be possible.

提交回复
热议问题