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

Stop using fromJust #339

Merged
merged 4 commits into from
Nov 19, 2019
Merged

Stop using fromJust #339

merged 4 commits into from
Nov 19, 2019

Conversation

treeowl
Copy link
Contributor

@treeowl treeowl commented Nov 12, 2019

Get rid of all but one use of fromJust.

Fixes #301

@treeowl
Copy link
Contributor Author

treeowl commented Nov 12, 2019

Here's my best guess about what this stuff is supposed to do. Please review carefully.

@@ -256,8 +259,7 @@ runGenT size seed (GenT m) =
--
evalGen :: Size -> Seed -> Gen a -> Maybe (Tree a)
evalGen size seed =
fmap (fmap Maybe.fromJust) .
Tree.filter Maybe.isJust .
Tree.mapMaybe id .
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a substantial change. Please be especially careful to check that it makes sense!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provided Tree.mapMaybe works then this looks like it would be fine.

* Get rid of all but one use of `fromJust`.
* Expose `mapMaybe`-like functions along with `filter`-like ones.

Fixes hedgehogqa#301
other-modules:
-- This has nothing to do with Hedgehog proper, and it's
-- too trivial to bother exposing.
Hedgehog.Internal.Predicate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the comment, but would like to maintain a policy of always exposing all internal modules

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#339 (comment), most likely we don't even need to introduce a new module.

@jacobstanley
Copy link
Member

I added a stupid unit test just to mess around with it in the repl, looks pretty good to me

@jacobstanley
Copy link
Member

Just have a few style guide things, will make some suggestions

@@ -1270,34 +1271,38 @@ ensure p gen = do
-- forever. If we trigger these limits then the whole generator is discarded.
--
filter :: (MonadGen m, GenBase m ~ Identity) => (a -> Bool) -> m a -> m a
filter p gen0 =
filter p = mapMaybe (fromPred p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filter p = mapMaybe (fromPred p)
filter p =
mapMaybe (fromPred p)

else
try (k + 1)
case p x of
Just _ -> withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Just _ -> withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen
Just _ ->
withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen

try (k + 1)
case p x of
Just _ -> withGenT (mapGenT (Tree.mapMaybeMaybeT p)) gen
Nothing -> try (k + 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Nothing -> try (k + 1)
Nothing ->
try (k + 1)

in
try 0

filterT :: MonadGen m => (a -> Bool) -> m a -> m a
filterT p gen0 =
filterT p = mapMaybeT (fromPred p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filterT p = mapMaybeT (fromPred p)
filterT p =
mapMaybeT (fromPred p)

else
try (k + 1)
case p x of
Just _ -> withGenT (mapGenT (Tree.mapMaybeT p)) gen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Just _ -> withGenT (mapGenT (Tree.mapMaybeT p)) gen
Just _ ->
withGenT (mapGenT (Tree.mapMaybeT p)) gen

try (k + 1)
case p x of
Just _ -> withGenT (mapGenT (Tree.mapMaybeT p)) gen
Nothing -> try (k + 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Nothing -> try (k + 1)
Nothing ->
try (k + 1)

-- | Convert a Boolean predicate to something suitable for use
-- with @mapMaybe@-like functions. The @wizards@ package calls this
-- @ensure@. @protolude@ and @relude@ generalize from 'Maybe' to
-- arbitrary 'Control.Applicative.Alternative' and call it @guarded@.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Extreme nitpick) - need to push those comments on line (8-10) two space deeper, then add an empty -- line, if we want this to be consistent with all the other commented stuff, like so:

-- | Convert a Boolean predicate to something suitable for use
--   with @mapMaybe@-like functions. The @wizards@ package calls this
--   @ensure@. @protolude@ and @relude@ generalize from 'Maybe' to
--   arbitrary 'Control.Applicative.Alternative' and call it @guarded@.
--

(Absolutely not a blocking issue, of course!) 📨

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we even want a dedicated module for this, as we can just declare this in both Gen and Tree (but not expose it). Then, if we need to use it on a third module, we can consider the rule of three and move it to its own module.

It's just that Hedgehog.Internal.Predicate looks like a utility module that we don't really need (yet)...

Comment on lines 1 to 3
module Hedgehog.Internal.Predicate
( fromPred
) where
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't do multiline suggestions but

- module Hedgehog.Internal.Predicate
-   ( fromPred
+ module Hedgehog.Internal.Predicate (
+     fromPred
    ) where

Nothing -> mxs
Just x ->
case p x of
Just x' -> [Tree (Node x' mxs)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Just x' -> [Tree (Node x' mxs)]
Just x' ->
[Tree (Node x' mxs)]

Just x ->
case p x of
Just x' -> [Tree (Node x' mxs)]
Nothing -> mxs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Nothing -> mxs
Nothing ->
mxs


-- | Returns a tree containing only elements that match the predicate.
--
-- When an element does not match the predicate its node is replaced with
-- 'empty'.
--
filterT :: (Monad m, Alternative m) => (a -> Bool) -> TreeT m a -> TreeT m a
filterT p m =
filterT p = mapMaybeT (fromPred p)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filterT p = mapMaybeT (fromPred p)
filterT p =
mapMaybeT (fromPred p)

@jacobstanley
Copy link
Member

Sorry for all the style nits, appreciate the contribution!

@moodmosaic
Copy link
Member

@treeowl would you like me or @jacobstanley to apply those few style nits above? Happy to do so if you're busy. After that we should be good to go and merge this 🚀

@treeowl
Copy link
Contributor Author

treeowl commented Nov 18, 2019 via email

@jacobstanley jacobstanley merged commit d63a71d into hedgehogqa:master Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

filterMaybeT from Hedgehog.Internal.Tree broken
3 participants