Created
May 20, 2011 19:41
-
-
Save ijt/983630 to your computer and use it in GitHub Desktop.
Example of checking a law for applicative functors using QuickCheck
This file contains 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
#!/usr/bin/env runhaskell | |
import Control.Applicative | |
import Test.QuickCheck | |
newtype ZipList2 a = ZipList2 { getZipList :: [a] } | |
instance Applicative ZipList2 where | |
pure x = ZipList2 $ repeat x | |
(ZipList2 gs) <*> (ZipList2 xs) = ZipList2 (zipWith ($) gs xs) | |
instance Functor ZipList2 where | |
fmap f (ZipList2 xs) = ZipList2 (map f xs) | |
main = quickCheck prop_law | |
-- See pages 25 and 26 of the Typeclassopedia. | |
-- http://www.haskell.org/wikiupload/8/85/TMR-Issue13.pdf | |
prop_law :: [Int] -> Bool | |
prop_law x = (fmap g x) == ((pure g) <*> x) | |
where g = id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment