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

Updates traversable to test the correct laws #61

Merged
merged 5 commits into from
Feb 23, 2022
Merged
Changes from 2 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
32 changes: 19 additions & 13 deletions src/Test/QuickCheck/Classes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ import Data.Functor.Apply (Apply ((<.>)))
import Data.Functor.Alt (Alt ((<!>)))
import Data.Functor.Bind (Bind ((>>-)), apDefault)
import qualified Data.Functor.Bind as B (Bind (join))
import Data.Functor.Compose (Compose (..))
import Data.Functor.Identity (Identity (..))
import Data.List.NonEmpty (NonEmpty(..))
import Data.Semigroup (Semigroup (..))
import Data.Monoid (Endo(..), Dual(..), Sum(..), Product(..))
import Data.Traversable (fmapDefault, foldMapDefault)
import Control.Applicative (Alternative(..))
import Control.Monad (MonadPlus (..), ap, join)
import Control.Arrow (Arrow,ArrowChoice,first,second,left,right,(>>>),arr)
Expand Down Expand Up @@ -717,23 +718,28 @@ arrowChoice = const ("arrow choice laws"
rightMovesP f g = (left f >>> right (arr g))
=-= ((right (arr g)) >>> left f)

traversable :: forall f a b m.
( Traversable f, Monoid m, Show (f a)
, Arbitrary (f a), Arbitrary b, Arbitrary m
, CoArbitrary a
, EqProp (f b), EqProp m) =>
f (a, b, m) -> TestBatch
traversable :: forall t a b c f.
( Traversable t, Applicative f
, Arbitrary (t a), Arbitrary (t b), Arbitrary (f b), Arbitrary (f c)
, CoArbitrary a, CoArbitrary b
, Show (t a), Show (t b)
, EqProp (t b), EqProp (f (f (t c)))) =>
t (f a, b, c) -> TestBatch
traversable = const ( "traversable"
, [ ("fmap", property fmapP)
, ("foldMap", property foldMapP)
sjakobi marked this conversation as resolved.
Show resolved Hide resolved
, [ ("identity", property identityP)
, ("composition", property compositionP)
-- , ("naturality", property $ \(f :: f Int -> g Int) -> naturalityP f)
]
)
where
fmapP :: (a -> b) -> f a -> Property
foldMapP :: (a -> m) -> f a -> Property
identityP :: Property
identityP = (traverse :: (b -> Identity b) -> t b -> Identity (t b)) Identity =-= Identity

compositionP :: (a -> f b) -> (b -> f c) -> Property
compositionP f g = (traverse :: (a -> Compose f f c) -> t a -> Compose f f (t c)) (Compose . fmap g . f) =-= Compose . fmap (traverse g) . traverse f
sjakobi marked this conversation as resolved.
Show resolved Hide resolved

fmapP f x = f `fmap` x =-= f `fmapDefault` x
foldMapP f x = f `foldMap` x =-= f `foldMapDefault` x
--naturalityP :: (forall x. (f x -> g x)) -> (a -> f b) -> Property
--naturalityP t f = t . traverse @t f =-= traverse (t . f)
sjakobi marked this conversation as resolved.
Show resolved Hide resolved

-- | Note that 'foldable' doesn't check the strictness of 'foldl'', `foldr'' and `foldMap''.
--
Expand Down