diff options
Diffstat (limited to 'Haskell-book/18/Instance/src/Sum.hs')
| -rw-r--r-- | Haskell-book/18/Instance/src/Sum.hs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Haskell-book/18/Instance/src/Sum.hs b/Haskell-book/18/Instance/src/Sum.hs new file mode 100644 index 0000000..d51b211 --- /dev/null +++ b/Haskell-book/18/Instance/src/Sum.hs @@ -0,0 +1,31 @@ +module Sum where + +import Test.QuickCheck +import Test.QuickCheck.Checkers + +data Sum a b = + First a + | Second b + deriving (Eq, Show) + +instance Functor (Sum a) where + fmap f (First x) = First x + fmap f (Second x) = Second $ f x + +instance Applicative (Sum a) where + pure x = Second x + First f <*> _ = First f + Second f <*> x = fmap f x + +instance Monad (Sum a) where + return = pure + (First x) >>= f = First x + (Second x) >>= f = f x + +instance (Arbitrary a, Arbitrary b) => Arbitrary (Sum a b) where + arbitrary = frequency [ (1, fmap First arbitrary) + , (1, fmap Second arbitrary) + ] + +instance (Eq a, Eq b) => EqProp (Sum a b) where + (=-=) = eq |
