Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add monadIOLaws #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions quickcheck-classes.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ library
Test.QuickCheck.Classes.Json
Test.QuickCheck.Classes.Monad
Test.QuickCheck.Classes.MonadFail
Test.QuickCheck.Classes.MonadIO
Test.QuickCheck.Classes.MonadPlus
Test.QuickCheck.Classes.MonadZip
Test.QuickCheck.Classes.Monoid
Expand Down
2 changes: 2 additions & 0 deletions src/Test/QuickCheck/Classes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ module Test.QuickCheck.Classes
, foldableLaws
, functorLaws
, monadLaws
, monadIOLaws
, monadPlusLaws
, monadZipLaws
#if HAVE_SEMIGROUPOIDS
Expand Down Expand Up @@ -120,6 +121,7 @@ import Test.QuickCheck.Classes.Applicative
import Test.QuickCheck.Classes.Foldable
import Test.QuickCheck.Classes.Functor
import Test.QuickCheck.Classes.Monad
import Test.QuickCheck.Classes.MonadIO
import Test.QuickCheck.Classes.MonadPlus
import Test.QuickCheck.Classes.MonadZip
#if HAVE_SEMIGROUPOIDS
Expand Down
85 changes: 85 additions & 0 deletions src/Test/QuickCheck/Classes/MonadIO.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}

#if HAVE_QUANTIFIED_CONSTRAINTS
{-# LANGUAGE QuantifiedConstraints #-}
#endif

{-# OPTIONS_GHC -Wall #-}

module Test.QuickCheck.Classes.MonadIO
(
#if HAVE_UNARY_LAWS
monadIOLaws
#endif
) where

import System.IO.Unsafe (unsafePerformIO)
import Control.Applicative
import Test.QuickCheck hiding ((.&.))
import Control.Monad.IO.Class (MonadIO(..))
#if HAVE_UNARY_LAWS
import Test.QuickCheck.Arbitrary (Arbitrary1(..))
import Data.Functor.Classes (Eq1,Show1)
#endif
import Test.QuickCheck.Property (Property)

import Test.QuickCheck.Classes.Common
#if HAVE_UNARY_LAWS
import Test.QuickCheck.Classes.Compat (eq1)
#endif

#if HAVE_UNARY_LAWS

-- | Tests the following 'MonadIO' properties:
--
-- [/Return/]
-- @'liftIO' '.' 'return' ≡ 'return'@
-- [/LiftIO Transforms/]
-- @'liftIO' (m '>>=' f) ≡ 'liftIO' m '>>=' ('liftIO' '.' f)@
monadIOLaws ::
#if HAVE_QUANTIFIED_CONSTRAINTS
(MonadIO f, Applicative f, forall a. Eq a => Eq (f a), forall a. Show a => Show (f a), forall a. Arbitrary a => Arbitrary (f a))
#else
(MonadIO f, Applicative f, Eq1 f, Show1 f, Arbitrary1 f)
#endif
=> proxy f -> Laws
monadIOLaws p = Laws "Monad"
[ ("Return", monadIOReturn p)
, ("LiftIO Transform", monadIOTransform p)
]

type MonadIOProp proxy f =
( MonadIO f
#if HAVE_QUANTIFIED_CONSTRAINTS
, forall x. Eq x => Eq (f x)
, forall x. Show x => Show (f x)
, forall x. Arbitrary x => Arbitrary (f x)
#else
, Eq1 f
, Show1 f
, Arbitrary1 f
#endif
) => proxy f -> Property

monadIOReturn :: forall proxy f. MonadIOProp proxy f
monadIOReturn _ = property $ \(x :: Integer) -> liftIO (pure x) == (pure x :: f Integer)

monadIOTransform :: forall proxy f. MonadIOProp proxy f
monadIOTransform _ = property $ \(m' :: ShowIO Integer) (f' :: LinearEquation) ->
let m = getShowIO m'
f = pure . runLinearEquation f'
x = liftIO (m >>= f) :: f Integer
y = liftIO m >>= (liftIO . f) :: f Integer
in x == y

newtype ShowIO a = ShowIO { getShowIO :: IO a }

instance Show a => Show (ShowIO a) where
show = unsafePerformIO . fmap (\x -> "IO <val: " ++ show x ++ ">") . getShowIO

instance Arbitrary a => Arbitrary (ShowIO a) where
arbitrary = fmap (ShowIO . pure) arbitrary

#endif