HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerebase-1.21.2Haskell2010

Rebase.Data.Functor.Bind

  • 2 types
  • 3 classes
  • 10 values
  • Packagerebase-1.21.2
  • Exports15
  • LanguageHaskell2010
  • LicenceMIT
  • SourceClass.hs
classclass Functor f => Apply (f :: Type -> Type) where
#

A strong lax semi-monoidal endofunctor. This is equivalent to an Applicative without pure.

Laws:

(.) <$> u <.> v <.> w = u <.> (v <.> w)
x <.> (f <$> y) = (. f) <$> x <.> y
f <$> (x <.> y) = (f .) <$> x <.> y

The laws imply that .> and <. really ignore their left and right results, respectively, and really return their right and left results, respectively. Specifically,

(mf <$> m) .> (nf <$> n) = nf <$> (m .> n)
(mf <$> m) <. (nf <$> n) = mf <$> (m <. n)

Methods

  • (<.>) :: f (a -> b) -> f a -> f binfixl 4
  • (.>) :: f a -> f b -> f binfixl 4
     a .> b = const id <$> a <.> b
  • (<.) :: f a -> f b -> f ainfixl 4
     a <. b = const <$> a <.> b
  • liftF2 :: (a -> b -> c) -> f a -> f b -> f c

    Lift a binary function into a comonad with zipping

Instances96Apply, …
classclass Functor (f :: Type -> Type) where
#

A type f is a Functor if it provides a function fmap which, given any types a and b lets you apply any function from (a -> b) to turn an f a into an f b, preserving the structure of f. Furthermore f needs to adhere to the following:

Identity

fmap id == id

Composition

fmap (f . g) == fmap f . fmap g

Note, that the second law follows from the free theorem of the type fmap and the first law, so you need only check that the former condition holds. See these articles by School of Haskell or David Luposchainsky for an explanation.

Methods

  • fmap :: (a -> b) -> f a -> f b

    fmap is used to apply a function of type (a -> b) to a value of type f a, where f is a functor, to produce a value of type f b. Note that for any type constructor with more than one parameter (e.g., Either), only the last type parameter can be modified with fmap (e.g., b in `Either a b`).

    Some type constructors with two parameters or more have a Data.Bifunctor instance that allows both the last and the penultimate parameters to be mapped over.

    Examples

    Convert from a Maybe Int to a Maybe String using show:

    Example2 expressions
    fmap show NothingNothingfmap show (Just 3)Just "3"

    Convert from an Either Int Int to an Either Int String using show:

    Example2 expressions
    fmap show (Left 17)Left 17fmap show (Right 17)Right "17"

    Double each element of a list:

    Example1 expression
    fmap (*2) [1,2,3][2,4,6]

    Apply even to the second element of a pair:

    Example1 expression
    fmap even (2,2)(2,True)

    It may seem surprising that the function is only applied to the last element of the tuple compared to the list example above which applies it to every element in the list. To understand, remember that tuples are type constructors with multiple type parameters: a tuple of 3 elements (a,b,c) can also be written (,,) a b c and its Functor instance is defined for Functor ((,,) a b) (i.e., only the third parameter is free to be mapped over with fmap).

    It explains why fmap can be used with tuples containing values of different types as in the following example:

    Example1 expression
    fmap even ("hello", 1.0, 4)("hello",1.0,True)
  • (<$) :: a -> f b -> f ainfixl 4

    Replace all locations in the input with the same value. The default definition is fmap . const, but this may be overridden with a more efficient version.

    Examples

    Perform a computation with Maybe and replace the result with a constant value if it is Just:

    Example2 expressions
    'a' <$ Just 2Just 'a''a' <$ NothingNothing
Instances241Functor, …
classclass Apply m => Bind (m :: Type -> Type) where
#

A Monad sans return.

Minimal definition: Either join or >>-

If defining both, then the following laws (the default definitions) must hold:

join = (>>- id)
m >>- f = join (fmap f m)

Laws:

induced definition of <.>: f <.> x = f >>- (<$> x)

Finally, there are two associativity conditions:

associativity of (>>-):    (m >>- f) >>- g == m >>- (\x -> f x >>- g)
associativity of join:     join . join = join . fmap join

These can both be seen as special cases of the constraint that

associativity of (->-): (f ->- g) ->- h = f ->- (g ->- h)

Methods

  • (>>-) :: m a -> (a -> m b) -> m binfixl 1
  • join :: m (m a) -> m a
Instances63Bind, …
  • Bind ComplexDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind FirstDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind LastDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind MaxDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind MinDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind IntMapDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    An IntMap is not a Monad, but it is an instance of Bind

  • Bind SeqDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind TreeDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind NonEmptyDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind IdentityDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind FirstDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind LastDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind DownDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind DualDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind ProductDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind SumDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind Par1Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind MaybeDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind IODefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind QDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind VectorDefined in vector-instances-3.4.2 · Data.Vector.Instances · orphan
  • Bind []Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Representable f => Bind (Co f)Defined in adjunctions-4.4.3 · Data.Functor.Rep
  • Functor f => Bind (Free f)Defined in free-5.2 · Control.Monad.Free
  • Monad m => Bind (WrappedMonad m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Monad m => Bind (IterT m)Defined in free-5.2 · Control.Monad.Trans.Iter
  • Semigroup m => Bind (Tuple2 m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    A (,) m is not a Monad unless its m is a Monoid, but it is an instance of Bind

  • Ord k => Bind (Map k)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    A 'Map k' is not a Monad, but it is an instance of Bind

  • Apply f => Bind (Free f)Defined in free-5.2 · Control.Monad.Free.Ap
  • Bind ProxyDefined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind U1Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind V1Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    A V1 is not a Monad, but it is an instance of Bind

  • Bind (F f)Defined in free-5.2 · Control.Monad.Free.Church
  • Bind (Either a)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (Coyoneda m)Defined in kan-extensions-5.2.7 · Data.Functor.Coyoneda
  • Bind m => Bind (Yoneda m)Defined in kan-extensions-5.2.7 · Data.Functor.Yoneda
  • (Functor m, Monad m) => Bind (MaybeT m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • (Hashable k, Eq k) => Bind (HashMap k)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    A 'HashMap k' is not a Monad, but it is an instance of Bind

  • Bind (FT f m)Defined in free-5.2 · Control.Monad.Trans.Free.Church
  • Bind (Tagged a)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind f => Bind (Alt f)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (Rec1 m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (IdentityT m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (ReaderT e m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (StateT s m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (StateT s m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (WriterT w m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • (Representable f, Bind m) => Bind (ReaderT f m)Defined in adjunctions-4.4.3 · Control.Monad.Representable.Reader
  • (Representable g, Bind m) => Bind (StateT g m)Defined in adjunctions-4.4.3 · Control.Monad.Representable.State
  • (Functor f, Monad m) => Bind (FreeT f m)Defined in free-5.2 · Control.Monad.Trans.Free
  • (Functor m, Monad m) => Bind (ExceptT e m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • (Apply f, Apply m, Monad m) => Bind (FreeT f m)Defined in free-5.2 · Control.Monad.Trans.Free.Ap
  • (Bind m, Semigroup w) => Bind (WriterT w m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    A WriterT w m is not a Monad unless its w is a Monoid, but it is an instance of Bind

  • (Bind m, Semigroup w) => Bind (WriterT w m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    A WriterT w m is not a Monad unless its w is a Monoid, but it is an instance of Bind

  • Bind (ContT r m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind ((->) m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Extend w => Bind (CoT w m)Defined in kan-extensions-5.2.7 · Control.Monad.Co
  • (Bind f, Bind g) => Bind (Product f g)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • (Bind f, Bind g) => Bind (f :*: g)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind f => Bind (M1 i c f)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • Bind m => Bind (RWST r w s m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class
  • (Bind m, Semigroup w) => Bind (RWST r w s m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    An RWST r w s m is not a Monad unless its w is a Monoid, but it is an instance of Bind

  • (Bind m, Semigroup w) => Bind (RWST r w s m)Defined in semigroupoids-6.0.1 · Data.Functor.Bind.Class

    An RWST r w s m is not a Monad unless its w is a Monoid, but it is an instance of Bind

value(-<<) :: Bind m => (a -> m b) -> m a -> m b
#
value($>) :: Functor f => f a -> b -> f b
#

Flipped version of <$.

Examples

Replace the contents of a Maybe Int with a constant String:

Example1 expression
Nothing $> "foo"Nothing
Example1 expression
Just 90210 $> "foo"Just "foo"

Replace the contents of an Either Int Int with a constant String, resulting in an Either Int String:

Example1 expression
Left 8675309 $> "foo"Left 8675309
Example1 expression
Right 8675309 $> "foo"Right "foo"

Replace each element of a list with a constant String:

Example1 expression
[1,2,3] $> "foo"["foo","foo","foo"]

Replace the second element of a pair with a constant String:

Example1 expression
(1,2) $> "foo"(1,"foo")
value(<$>) :: Functor f => (a -> b) -> f a -> f b
#

An infix synonym for fmap.

The name of this operator is an allusion to Prelude.$. Note the similarities between their types:

 ($)  ::              (a -> b) ->   a ->   b
(<$>) :: Functor f => (a -> b) -> f a -> f b

Whereas Prelude.$ is function application, <$> is function application lifted over a Functor.

Examples

Convert from a Maybe Int to a Maybe String using show:

Example1 expression
show <$> NothingNothing
Example1 expression
show <$> Just 3Just "3"

Convert from an Either Int Int to an Either Int String using show:

Example1 expression
show <$> Left 17Left 17
Example1 expression
show <$> Right 17Right "17"

Double each element of a list:

Example1 expression
(*2) <$> [1,2,3][2,4,6]

Apply even to the second element of a pair:

Example1 expression
even <$> (2,2)(2,True)
newtypenewtype WrappedApplicative (f :: Type -> Type) a
#

Wrap an Applicative to be used as a member of Apply

Instances8Functor, Applicative, Alternative, Alt, Apply, Plus, …
newtypenewtype MaybeApply (f :: Type -> Type) a
#

Transform an Apply into an Applicative by adding a unit.

Constructors

Instances7Functor, Applicative, Comonad, Apply, Extend, Copointed, …
value(<..>) :: Apply w => w a -> w (a -> b) -> w b
#

A variant of <.> with the arguments reversed.

valueliftF3 :: Apply w => (a -> b -> c -> d) -> w a -> w b -> w c -> w d
#

Lift a ternary function into a comonad with zipping

value(-<-) :: Bind m => (b -> m c) -> (a -> m b) -> a -> m c
#
value(->-) :: Bind m => (a -> m b) -> (b -> m c) -> a -> m c
#
valuegbind :: (Generic1 m, Bind (Rep1 m)) => m a -> (a -> m b) -> m b
#

Generic (>>-). Caveats:

  1. Will not compile if m is a sum type.

  2. Will not compile if m contains fields that do not mention its type variable.

  3. Will not compile if m contains fields where the type variable appears underneath the composition of type constructors (e.g., f (g a)).

  4. May do redundant work, due to the nature of the Bind instance for (:*:)