Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Control.Applicative.Lift useful for?

Tags:

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?

like image 377
ocharles Avatar asked Dec 24 '12 14:12

ocharles


People also ask

What is applicative in Scala?

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.

What is applicative Haskell?

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.

What is a functor in Haskell?

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.


1 Answers

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.

like image 50
Don Stewart Avatar answered Sep 28 '22 11:09

Don Stewart