21

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?

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
ocharles
  • 6,172
  • 2
  • 35
  • 46

1 Answers1

19

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.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • 2
    It reminds me of extending a semigroup into a monoid: it extends an applicative-functor-but-without-pure into an applicative functor. And this is obfuscated in both cases because semigroups/applicative-functors-but-without-pure aren't present in the standard library. – dave4420 Dec 24 '12 at 15:32
  • 14
    If you have any applicative homomorphism `fug :: f u -> g u`, then `(f :+: g)` (where `:+:` is pointwise sum) can be made applicative by "keeping left unless you fugging have to go right". Lift is the special case where `f` is the initial applicative, i.e., Identity, from which there is a unique homomorphism to any other. – pigworker Dec 24 '12 at 17:37
  • 1
    It's taken a fair bit of parsing of @pigworker's comment - but after reading http://comonad.com/reader/2012/abstracting-with-applicatives/comment-page-1/#comment-107128 I finally grok this (and have an immediate use for it!). Thanks! – ocharles Dec 27 '12 at 17:33