Is `data PoE a = Empty | Pair a a` a monad?

后端 未结 3 1495
情话喂你
情话喂你 2021-02-13 07:09

This question comes from this answer in example of a functor that is Applicative but not a Monad: It is claimed that the

data PoE a = Empty | Pair a a deriving (         


        
3条回答
  •  我寻月下人不归
    2021-02-13 07:50

    Since you are interested in how to do it systematically, here's how I found a counterexample with quickcheck:

    {-# LANGUAGE DeriveFunctor #-}
    
    import Control.Monad ((>=>))
    import Test.QuickCheck
    
    -- 
    

    Defining an arbitrary instance to generate random PoEs.

    instance (Arbitrary a) => Arbitrary (PoE a) where
        arbitrary = do
          emptyq <- arbitrary
          if emptyq
            then return Empty
            else Pair <$> arbitrary <*> arbitrary
    

    And tests for the monad laws:

    prop_right_id m = (m >>= return) == m
        where
        _types = (m :: PoE Int)
    
    prop_left_id fun x = (return x >>= f) == f x
        where
        _types = fun :: Fun Int (PoE Int)
        f = applyFun fun
    
    prop_assoc fun gun hun x = (f >=> (g >=> h)) x == ((f >=> g) >=> h) x
        where
        _types = (fun :: Fun Int (PoE Int),
                  gun :: Fun Int (PoE Int),
                  hun :: Fun Int (PoE Int),
                  x :: Int)
        f = applyFun fun
        g = applyFun gun
        h = applyFun hun
    

    I don't get any failures for the identity laws, but prop_assoc does generate a counterexample:

    ghci> quickCheck prop_assoc
    *** Failed! Falsifiable (after 7 tests and 36 shrinks):
    {6->Pair 1 (-1), _->Empty}
    {-1->Pair (-3) (-4), 1->Pair (-1) (-2), _->Empty}
    {-3->Empty, _->Pair (-2) (-4)}
    6
    

    Not that it's terribly helpful for understanding why the failure occurs, it does give you a place to start. If we look carefully, we see that we are passing (-3) and (-2) to the third function; (-3) maps to Empty and (-2) maps to a Pair, so we can't defer to the laws of either of the two monads PoE is composed of.

提交回复
热议问题