Skip to content

Instantly share code, notes, and snippets.

@ijt
Created May 20, 2011 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ijt/983630 to your computer and use it in GitHub Desktop.
Save ijt/983630 to your computer and use it in GitHub Desktop.
Example of checking a law for applicative functors using QuickCheck
#!/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