lift package:base

Lift a precedence-insensitive ReadP to a ReadPrec.
Lift a binary function to actions. Some functors support an implementation of liftA2 that is more efficient than the default one. In particular, if fmap is an expensive operation, it is likely better to use liftA2 than to fmap over the structure and then use <*>. This became a typeclass method in 4.10.0.0. Prior to that, it was a function defined in terms of <*> and fmap.

Example

>>> liftA2 (,) (Just 3) (Just 5)
Just (3,5)
>>> liftA2 (+) [1, 2, 3] [4, 5, 6]
[5,6,7,6,7,8,7,8,9]
Promote a function to a monad. This is equivalent to fmap but specialised to Monads.
Promote a function to a monad, scanning the monadic arguments from left to right.

Examples

>>> liftM2 (+) [0,1] [0,2]
[0,2,1,3]
>>> liftM2 (+) (Just 1) Nothing
Nothing
>>> liftM2 (+) (+ 3) (* 2) 5
18
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Promote a function to a monad, scanning the monadic arguments from left to right (cf. liftM2).
Lift a function to actions. Equivalent to Functor's fmap but implemented using only Applicative's methods: liftA f a = pure f <*> a As such this function may be used to implement a Functor instance from an Applicative one.

Examples

Using the Applicative instance for Lists:
>>> liftA (+1) [1, 2]
[2,3]
Or the Applicative instance for Maybe
>>> liftA (+1) (Just 3)
Just 4
Lift a ternary function to actions.
Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

Example

import Control.Monad.Trans.State -- from the "transformers" library

printState :: Show s => StateT s IO ()
printState = do
state <- get
liftIO $ print state
Had we omitted liftIO, we would have ended up with this error:
• Couldn't match type ‘IO’ with ‘StateT s IO’
Expected type: StateT s IO ()
Actual type: IO ()
The important part here is the mismatch between StateT s IO () and IO (). Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:
> evalStateT printState "hello"
"hello"

> evalStateT printState 3
3
Lift a compare function through the type constructor. The function will usually be applied to a comparison function, but the more general type ensures that the implementation uses it to compare elements of the first container with elements of the second.
Lift compare functions through the type constructor. The function will usually be applied to comparison functions, but the more general type ensures that the implementation uses them to compare elements of the first container with elements of the second.
Lift an equality test through the type constructor. The function will usually be applied to an equality function, but the more general type ensures that the implementation uses it to compare elements of the first container with elements of the second.
Lift equality tests through the type constructor. The function will usually be applied to equality functions, but the more general type ensures that the implementation uses them to compare elements of the first container with elements of the second.
readList function for an application of the type constructor based on readsPrec and readList functions for the argument type. The default implementation using standard list syntax is correct for most types.
readList function for an application of the type constructor based on readsPrec and readList functions for the argument types. The default implementation using standard list syntax is correct for most types.
A possible replacement definition for the liftReadList2 method. This is only needed for Read2 instances where liftReadListPrec2 isn't defined as liftReadListPrec2Default.
A possible replacement definition for the liftReadList method. This is only needed for Read1 instances where liftReadListPrec isn't defined as liftReadListPrecDefault.
readListPrec function for an application of the type constructor based on readPrec and readListPrec functions for the argument type. The default definition uses liftReadList. Instances that define liftReadPrec should also define liftReadListPrec as liftReadListPrecDefault.
readListPrec function for an application of the type constructor based on readPrec and readListPrec functions for the argument types. The default definition uses liftReadList2. Instances that define liftReadPrec2 should also define liftReadListPrec2 as liftReadListPrec2Default.
A possible replacement definition for the liftReadListPrec2 method, defined using liftReadPrec2.
A possible replacement definition for the liftReadListPrec method, defined using liftReadPrec.
readPrec function for an application of the type constructor based on readPrec and readListPrec functions for the argument type.
readPrec function for an application of the type constructor based on readPrec and readListPrec functions for the argument types.
readsPrec function for an application of the type constructor based on readsPrec and readList functions for the argument type.