Advertisement
Guest User

Identity Functor

a guest
Jan 25th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE ViewPatterns #-}
  2.  
  3. module FuncInstances where
  4.  
  5. import Test.QuickCheck
  6. import Test.QuickCheck.Function
  7.  
  8. functorIdentity :: (Functor f, Eq (f a)) => f a -> Bool
  9. functorIdentity f = fmap id f == f
  10.  
  11. functorCompose :: (Eq (f c), Functor f) => (a -> b) -> (b -> c) -> f a -> Bool
  12. functorCompose f g x = (fmap g (fmap f x)) == (fmap (g . f) x)
  13.  
  14. functorCompose' :: (Eq (f c), Functor f) =>
  15.                     f a
  16.                  -> Fun a b
  17.                  -> Fun b c
  18.                  -> Bool
  19. functorCompose' x (Fun _ f) (Fun _ g) =
  20.   (fmap (g . f) x) == (fmap g . fmap f $ x)
  21.  
  22. newtype Identity a = Identity a
  23.   deriving (Show, Eq)
  24.  
  25. instance Functor Identity where
  26.   fmap f (Identity a) = Identity (f a)
  27.  
  28. instance Arbitrary a => Arbitrary (Identity a) where
  29.   arbitrary = do
  30.     x <- arbitrary
  31.     return $ Identity x
  32.  
  33. type IdentityId = Identity Int -> Bool
  34. type IdentityComp = (Int -> Int) -> (Int -> Int) -> Identity Int -> Bool
  35.  
  36. type IdentityIntToInt = Fun (Identity Int) (Identity Int)
  37. type IdentityIntFC = [Identity Int] -> IdentityIntToInt -> IdentityIntToInt -> Bool
  38.  
  39. main :: IO ()
  40. main = do
  41.   quickCheck (functorIdentity :: IdentityId)
  42.   quickCheck (functorCompose' :: IdentityIntFC)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement