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 a oneOf function to improve the ergonomics of NonDet. #201

Merged
merged 6 commits into from
Sep 9, 2019
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
16 changes: 16 additions & 0 deletions src/Control/Effect/NonDet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Control.Effect.NonDet
-- * NonDet carrier
, runNonDet
, NonDetC(..)
, choose
-- * Re-exports
, Alternative(..)
, Carrier
Expand All @@ -19,6 +20,7 @@ import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Data.Monoid
import GHC.Generics (Generic1)

data NonDet m k
Expand All @@ -37,6 +39,20 @@ data NonDet m k
runNonDet :: (Alternative f, Applicative m) => NonDetC m a -> m (f a)
runNonDet (NonDetC m) = m (fmap . (<|>) . pure) (pure empty)

-- | Nondeterministically choose an element from a 'Foldable' collection.
-- This can be used to emulate the style of nondeterminism associated with
-- programming in the list monad:
-- @
-- pythagoreanTriples = do
-- a <- choose [1..10]
-- b <- choose [1..10]
-- c <- choose [1..10]
-- guard (a^2 + b^2 == c^2)
-- pure (a, b, c)
-- @
choose :: (Foldable t, Alternative m) => t a -> m a
choose = getAlt . foldMap (Alt . pure)
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about doing foldMapA instead? I personally find it much more generally useful, and it still enjoys good type inference properties.

Copy link
Collaborator Author

@patrickt patrickt Sep 8, 2019

Choose a reason for hiding this comment

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

No objections at all to exposing foldMapA as well as choose.

Copy link
Contributor

Choose a reason for hiding this comment

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

At that point, given that choose = foldMapA pure, it seems like we don’t gain much from including both. Thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I’m a big fan of providing a single verb for “choose an element of this structure”, for a few reasons: both because I find existing foldMapA pure or asum . fmap pure patterns unreadable, or at best non-obvious, and also because I think this operation is extremely common. (I’ve been practicing some simple logic programs recently, and I needed choose in all of them.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, makes sense 👍

What do you think about renaming it to not conflict with #198’s smart constructor? Perhaps oneOf?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oneOf is fine by me. choose was a little too close to choice from parsers. Will make the change!


-- | A carrier for 'NonDet' effects based on Ralf Hinze’s design described in [Deriving Backtracking Monad Transformers](https://www.cs.ox.ac.uk/ralf.hinze/publications/#P12).
newtype NonDetC m a = NonDetC
{ -- | A higher-order function receiving two parameters: a function to combine each solution with the rest of the solutions, and an action to run when no results are produced.
Expand Down