From 475872efcb9bc8496cedcbf4f978fab44402c046 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 11 Mar 2016 03:43:39 +0100 Subject: Add quickcheck example with functions. --- qcheck.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 qcheck.hs diff --git a/qcheck.hs b/qcheck.hs new file mode 100644 index 0000000..382179b --- /dev/null +++ b/qcheck.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +import Test.QuickCheck +import Test.QuickCheck.Function + +newtype ReversingList a = ReversingList [a] + deriving (Show, Eq, Arbitrary) + +-- This ReversingList is a Functor instance that reverses the elements while it +-- maps over them. +instance Functor ReversingList where + fmap f (ReversingList xs) = ReversingList (reverse (fmap f xs)) + +-- The functor law is that (fmap f . fmap g) == fmap (f . g). +prop_functorLaw (Fun _ f) (Fun _ g) xs = + (fmap f . fmap g) xs == fmap (f . g) xs + + where types = ( xs :: ReversingList Int + , f :: Int -> Int + , g :: Int -> Int ) + +main = quickCheck prop_functorLaw + +{- Output: + +*** Failed! Falsifiable (after 6 tests and 19 shrinks): +{-5->1, _->0} +{0->-5, _->0} +ReversingList [1,0] + +-} -- cgit v1.2.3