Skip to content

Draft: Give a better implementation for (*>) #546

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

Open
wants to merge 5 commits 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Version 1.6.1 (2025-05-19)

* Improve Applicative's `(*>)` ([#546][546], [@L0neGamer][L0neGamer])

## Version 1.6 (2024-08-27)

* Add callstacks to generators that can error ([#538][538], [@ChickenProp][ChickenProp])
Expand Down
9 changes: 6 additions & 3 deletions hedgehog/src/Hedgehog/Internal/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ instance Functor m => Functor (GenT m) where
--
-- implementation: parallel shrinking
--
-- | This Applicative instance is not lawful with regards to the Monad instance.
-- This is because using applicative allows us to do parallel shrinking, but
-- Monad does not allow that.
instance Monad m => Applicative (GenT m) where
pure =
fromTreeMaybeT . pure
Expand All @@ -511,6 +514,9 @@ instance Monad m => Applicative (GenT m) where
runGenT size sf f `mzip`
runGenT size sm m

(*>) a b = a >>= const b
(<*) a b = const a =<< b

--
-- implementation: satisfies law (ap = <*>)
--
Expand All @@ -525,9 +531,6 @@ instance Monad m => Applicative (GenT m) where
-- runGenT size sm m

instance Monad m => Monad (GenT m) where
return =
pure

(>>=) m k =
GenT $ \size seed ->
case Seed.split seed of
Expand Down
10 changes: 1 addition & 9 deletions hedgehog/src/Hedgehog/Internal/Property.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ newtype TestT m a =
} deriving (
Functor
, Applicative
, Monad
, MonadIO
, MonadBase b
, MonadThrow
Expand Down Expand Up @@ -690,15 +691,6 @@ newtype Coverage a =
------------------------------------------------------------------------
-- TestT

instance Monad m => Monad (TestT m) where
return =
pure

(>>=) m k =
TestT $
unTest m >>=
unTest . k

instance Monad m => MonadFail (TestT m) where
fail err =
TestT . ExceptT . pure . Left $ Failure Nothing err Nothing
Expand Down
24 changes: 9 additions & 15 deletions hedgehog/src/Hedgehog/Internal/Tree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-} -- MonadBase
{-# LANGUAGE DeriveFunctor #-}
#if __GLASGOW_HASKELL__ < 802
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
#endif
Expand Down Expand Up @@ -105,6 +106,9 @@ newtype TreeT m a =
TreeT {
runTreeT :: m (NodeT m a)
}
deriving
( Functor
)

instance MonadBaseControl b m => MonadBaseControl b (TreeT m) where
type StM (TreeT m) a = StM m (NodeT m a)
Expand Down Expand Up @@ -134,7 +138,11 @@ data NodeT m a =

-- | The children of this 'NodeT'.
, nodeChildren :: [TreeT m a]
} deriving (Eq)
}
deriving
( Eq
, Functor
)

-- | Extracts the 'Node' from a 'Tree'.
--
Expand Down Expand Up @@ -417,14 +425,6 @@ instance (Eq1 m, Eq a) => Eq (TreeT m a) where
TreeT m0 == TreeT m1 =
liftEq (==) m0 m1

instance Functor m => Functor (NodeT m) where
fmap f (NodeT x xs) =
NodeT (f x) (fmap (fmap f) xs)

instance Functor m => Functor (TreeT m) where
fmap f =
TreeT . fmap (fmap f) . runTreeT

instance Applicative m => Applicative (NodeT m) where
pure x =
NodeT x []
Expand All @@ -440,19 +440,13 @@ instance Applicative m => Applicative (TreeT m) where
liftA2 (<*>) mab ma

instance Monad m => Monad (NodeT m) where
return =
pure

(>>=) (NodeT x xs) k =
case k x of
NodeT y ys ->
NodeT y $
fmap (TreeT . fmap (>>= k) . runTreeT) xs ++ ys

instance Monad m => Monad (TreeT m) where
return =
pure

(>>=) m k =
TreeT $ do
NodeT x xs <- runTreeT m
Expand Down
Loading