Skip to content

Commit

Permalink
Updates traversable to test the correct laws
Browse files Browse the repository at this point in the history
  • Loading branch information
solomon-b committed Feb 14, 2022
1 parent ee6e265 commit 740a7ed
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/Test/QuickCheck/Classes.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE ScopedTypeVariables, FlexibleContexts, KindSignatures
, Rank2Types, TypeOperators, CPP
, TypeApplications, Rank2Types, TypeOperators, CPP
#-}

----------------------------------------------------------------------
Expand Down 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,27 @@ 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 g.
( 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, g b, c) -> TestBatch
traversable = const ( "traversable"
, [ ("fmap", property fmapP)
, ("foldMap", property foldMapP)
, [ ("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 @t @_ @b Identity =-= Identity

compositionP :: (a -> f b) -> (b -> f c) -> Property
compositionP f g = traverse @t (Compose . fmap g . f) =-= Compose . fmap (traverse g) . traverse f

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)

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

0 comments on commit 740a7ed

Please sign in to comment.