Safe Haskell | None |
---|---|
Language | Haskell2010 |
An effect modelling nondeterminism with choice and failure.
Nondeterministic operations are encapsulated by the Alternative
class, where empty
represents failure and <|>
represents choice. This module re-exports the Alternative
interface. If you can't or don't want to use Alternative
, you can use the empty
and <|>
operations (from Control.Effect.Empty and Control.Effect.Choose respectively) directly, as the NonDet
effect is the composition of Choose
and Empty
.
Predefined carriers:
- Control.Carrier.NonDet.Church, which collects all branches' results using an
Alternative
functor. - If
NonDet
is the last effect in a stack, it can be interpreted directly into a[]
.
Since: 0.1.0.0
Synopsis
- type NonDet = Empty :+: Choose
- newtype Choose m k = Choose (Bool -> m k)
- data Empty (m :: * -> *) k = Empty
- oneOf :: (Foldable t, Alternative m) => t a -> m a
- foldMapA :: (Foldable t, Alternative m) => (a -> m b) -> t a -> m b
- class Applicative f => Alternative (f :: Type -> Type) where
- class (HFunctor sig, Monad m) => Algebra sig m | m -> sig
- type Has eff sig m = (Members eff sig, Algebra sig m)
- class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where
- guard :: Alternative f => Bool -> f ()
- optional :: Alternative f => f a -> f (Maybe a)
- run :: Identity a -> a
NonDet effects
Since: 1.0.0.0
Instances
Effect Choose Source # | |
HFunctor Choose Source # | |
Algebra Choose NonEmpty Source # | |
Algebra NonDet [] Source # | |
Defined in Control.Algebra | |
Functor m => Functor (Choose m) Source # | |
Generic1 (Choose m :: Type -> Type) Source # | |
(Algebra sig m, Effect sig) => Algebra (Choose :+: sig) (ChooseC m) Source # | |
(Algebra sig m, Effect sig) => Algebra (NonDet :+: sig) (NonDetC m) Source # | |
(Algebra sig m, Effect sig) => Algebra (Cut :+: (NonDet :+: sig)) (CutC m) Source # | |
(Algebra sig m, Effect sig) => Algebra (Cull :+: (NonDet :+: sig)) (CullC m) Source # | |
type Rep1 (Choose m :: Type -> Type) Source # | |
Defined in Control.Effect.Choose.Internal type Rep1 (Choose m :: Type -> Type) = D1 (MetaData "Choose" "Control.Effect.Choose.Internal" "fused-effects-1.0.0.0-inplace" True) (C1 (MetaCons "Choose" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (((->) Bool :: Type -> Type) :.: Rec1 m))) |
data Empty (m :: * -> *) k Source #
Since: 1.0.0.0
Instances
Effect Empty Source # | |
HFunctor Empty Source # | |
Algebra Empty Maybe Source # | |
Algebra NonDet [] Source # | |
Defined in Control.Algebra | |
Functor (Empty m) Source # | |
Generic1 (Empty m :: Type -> Type) Source # | |
(Algebra sig m, Effect sig) => Algebra (Empty :+: sig) (EmptyC m) Source # | |
(Algebra sig m, Effect sig) => Algebra (NonDet :+: sig) (NonDetC m) Source # | |
(Algebra sig m, Effect sig) => Algebra (Cut :+: (NonDet :+: sig)) (CutC m) Source # | |
(Algebra sig m, Effect sig) => Algebra (Cull :+: (NonDet :+: sig)) (CullC m) Source # | |
type Rep1 (Empty m :: Type -> Type) Source # | |
oneOf :: (Foldable t, Alternative m) => t a -> m a Source #
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 <- oneOf [1..10] b <- oneOf [1..10] c <- oneOf [1..10] guard (a^2 + b^2 == c^2) pure (a, b, c)
Since: 1.0.0.0
foldMapA :: (Foldable t, Alternative m) => (a -> m b) -> t a -> m b Source #
Map a Foldable
collection of values into a nondeterministic computation using the supplied action.
Since: 1.0.0.0
Re-exports
class Applicative f => Alternative (f :: Type -> Type) where #
A monoid on applicative functors.
If defined, some
and many
should be the least solutions
of the equations:
The identity of <|>
(<|>) :: f a -> f a -> f a infixl 3 #
An associative binary operation
One or more.
Zero or more.
Instances
class (HFunctor sig, Monad m) => Algebra sig m | m -> sig Source #
The class of carriers (results) for algebras (effect handlers) over signatures (effects), whose actions are given by the alg
method.
Since: 1.0.0.0
Instances
type Has eff sig m = (Members eff sig, Algebra sig m) Source #
m
is a carrier for sig
containing eff
.
Note that if eff
is a sum, it will be decomposed into multiple Member
constraints. While this technically allows one to combine multiple unrelated effects into a single Has
constraint, doing so has two significant drawbacks:
- Due to a problem with recursive type families, this can lead to significantly slower compiles.
- It defeats
ghc
’s warnings for redundant constraints, and thus can lead to a proliferation of redundant constraints as code is changed.
class (Alternative m, Monad m) => MonadPlus (m :: Type -> Type) where #
Monads that also support choice and failure.
Nothing
The identity of mplus
. It should also satisfy the equations
mzero >>= f = mzero v >> mzero = mzero
The default definition is
mzero = empty
An associative operation. The default definition is
mplus = (<|>
)
Instances
guard :: Alternative f => Bool -> f () #
Conditional failure of Alternative
computations. Defined by
guard True =pure
() guard False =empty
Examples
Common uses of guard
include conditionally signaling an error in
an error monad and conditionally rejecting the current choice in an
Alternative
-based parser.
As an example of signaling an error in the error monad Maybe
,
consider a safe division function safeDiv x y
that returns
Nothing
when the denominator y
is zero and
otherwise. For example:Just
(x `div`
y)
>>> safeDiv 4 0 Nothing >>> safeDiv 4 2 Just 2
A definition of safeDiv
using guards, but not guard
:
safeDiv :: Int -> Int -> Maybe Int safeDiv x y | y /= 0 = Just (x `div` y) | otherwise = Nothing
A definition of safeDiv
using guard
and Monad
do
-notation:
safeDiv :: Int -> Int -> Maybe Int safeDiv x y = do guard (y /= 0) return (x `div` y)
optional :: Alternative f => f a -> f (Maybe a) #
One or none.