I wrote about transformers
in a recent blog post, and someone asked "what do people use Control.Applicative.Lift for?" I wasn't able to answer this, so I echo the question to StackOverflow - what is Control.Applicative.Lift
used for?
I see one example use of it in the package, but I don't seem to be entirely able to parse what it does. Does anyone know any other examples in the wild?
Applicative is a generalization of Monad , allowing expression of effectful computations in a pure functional way. Applicative is generally preferred to Monad when the structure of a computation is fixed a priori. That makes it possible to perform certain kinds of static analysis on applicative values.
In Haskell, an applicative is a parametrized type that we think of as being a container for data of that type plus two methods pure and <*> . Consider a parametrized type f a . The pure method for an applicative of type f has type. pure :: a -> f a. and can be thought of as bringing values into the applicative.
Functor in Haskell is a kind of functional representation of different Types which can be mapped over. It is a high level concept of implementing polymorphism. According to Haskell developers, all the Types such as List, Map, Tree, etc. are the instance of the Haskell Functor.
Lift is a relatively new contribution:
data Lift f a = Pure a | Other (f a)
That is, given a functor f
, you can get a new functor by composing f
with a pure value.
The package itself gives an example:
-- | An applicative functor that collects a monoid (e.g. lists) of errors.
-- A sequence of computations fails if any of its components do, but
-- unlike monads made with 'ErrorT' from "Control.Monad.Trans.Error",
-- these computations continue after an error, collecting all the errors.
type Errors e = Lift (Constant e)
-- | Report an error.
failure :: Monoid e => e -> Errors e a
failure e = Other (Constant e)
I don't know of any in-the-wild uses of this, however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With