lift package:ghc-internal

Turn a value into a Template Haskell expression, suitable for use in a splice.
Lift a precedence-insensitive ReadP to a ReadPrec.
This module gives the definition of the Lift class. This is an internal module. Please import Language.Haskell.TH or Language.Haskell.TH.Syntax instead!
A Lift instance can have any of its values turned into a Template Haskell expression. This is needed when a value used within a Template Haskell quotation is bound outside the Oxford brackets ([| ... |] or [|| ... ||]) but not at the top level. As an example:
add1 :: Int -> Code Q Int
add1 x = [|| x + 1 ||]
Template Haskell has no way of knowing what value x will take on at splice-time, so it requires the type of x to be an instance of Lift. A Lift instance must satisfy $(lift x) ≡ x and $$(liftTyped x) ≡ x for all x, where $(...) and $$(...) are Template Haskell splices. It is additionally expected that lift x ≡ unTypeCode (liftTyped x). Lift instances can be derived automatically by use of the -XDeriveLift GHC language extension:
{-# LANGUAGE DeriveLift #-}
module Foo where

import Language.Haskell.TH.Syntax

data Bar a = Bar1 a (Bar a) | Bar2 String
deriving Lift
Representation-polymorphic since template-haskell-2.16.0.0.
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 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]
Lift a ternary function to actions.
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 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
liftData is a variant of lift in the Lift type class which works for any type with a Data instance.
Turn a value into a Template Haskell typed expression, suitable for use in a typed splice.
Lift a monadic action producing code into the typed Code representation
The runtime representation of lifted types.
The runtime representation of unlifted types.
The kind of boxed, unlifted values, for example Array# or a user-defined unlifted data type, using -XUnliftedDataTypes.