Skip to content

Instantly share code, notes, and snippets.

@kurtmilam
Last active May 25, 2019 21:10
Show Gist options
  • Save kurtmilam/7c83fecacb4b1834e3c628eeddd6db3c to your computer and use it in GitHub Desktop.
Save kurtmilam/7c83fecacb4b1834e3c628eeddd6db3c to your computer and use it in GitHub Desktop.
Minimal example of using `purescript-quickcheck-laws` to verify a type class instance on a custom type
module Pair where
import Effect
import Prelude
import Data.Eq (class Eq1)
import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
import Test.QuickCheck.Laws (checkLaws)
import Test.QuickCheck.Laws.Data.Functor (checkFunctor)
data Pair a = Pair a a
derive instance eq1Pair :: Eq1 Pair
derive instance eqPair :: Eq a => Eq (Pair a)
instance arbPair :: Arbitrary a => Arbitrary (Pair a) where
arbitrary = Pair <$> arbitrary <*> arbitrary
instance functorPair :: Functor Pair where
map f (Pair x y) = Pair (f x) (f y)
prxPair :: Proxy2 Pair
prxPair = Proxy2
main :: Effect Unit
main = do checkLaws "Pair" (checkFunctor prxPair)
{-
Or, in the repl (if you don't want to define `main`):
> import Prelude
> import Test.QuickCheck.Laws
> import Test.QuickCheck.Laws.Data.Functor
> import Pair
> checkLaws "Pair" (checkFunctor prxPair)
Checking laws of Pair instances...
Checking 'Identity' law for Functor
1000/1000 test(s) passed.
Checking 'Composition' law for Functor
1000/1000 test(s) passed.
unit
> -- Also works (without checkLaws):
> checkFunctor prxPair
Checking 'Identity' law for Functor
1000/1000 test(s) passed.
Checking 'Composition' law for Functor
1000/1000 test(s) passed.
unit
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment