Created
January 8, 2017 19:57
-
-
Save ygrenzinger/77b89e828a6c2bf7b6bc9e4e4a79a49a to your computer and use it in GitHub Desktop.
Functor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Test.QuickCheck | |
import Test.QuickCheck.Function | |
prop_revapp :: [Int] -> [Int] -> Bool | |
prop_revapp xs ys = reverse (ys++xs) == reverse xs ++ reverse ys | |
data Two a b = Two a b deriving (Eq, Show) | |
data Or a b = First a | Second b deriving (Eq, Show) | |
instance Functor (Two a) where | |
fmap f (Two a b) = Two a (f b) | |
functorTwoIdentity :: (Functor f, Eq (f a)) => f a -> Bool | |
functorTwoIdentity f = fmap id f == f | |
functorTwoCompose :: (Eq (f c), Functor f) => f a -> Fun a b -> Fun b c -> Bool | |
functorTwoCompose x (Fun _ f) (Fun _ g) = fmap (g . f) x == (fmap g . fmap f $ x) | |
type IntToInt = Fun Int Int | |
type IntFC = [Int] -> IntToInt -> IntToInt -> Bool | |
instance Functor (Or a) where | |
fmap f (First a) = First a | |
fmap f (Second b) = Second (f b) | |
data Wrap f a = Wrap (f a) deriving (Eq, Show) | |
instance Functor (Wrap f) where | |
fmap f (Wrap fa) = Wrap (f fa) | |
instance Functor (Wrap f) where | |
fmap f (Wrap fa) = Wrap (fmap f fa) | |
instance Functor f => Functor (Wrap f) where | |
fmap f (Wrap fa) = Wrap (fmap f fa) | |
main :: IO () | |
main = do | |
quickCheck $ \x -> functorTwoIdentity (x :: [Int]) | |
quickCheck (functorTwoIdentity :: [Int] -> Bool) | |
quickCheck (functorTwoCompose :: IntFC) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment