lift package:ghc-lib-parser

Turn a value into a Template Haskell expression, suitable for use in a splice.
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.
type LiftedRep = 'BoxedRep 'Lifted
liftCoSubst role lc ty produces a coercion (at role role) that coerces between lc_left(ty) and lc_right(ty), where lc_left is a substitution mapping type variables to the left-hand types of the mapped coercions in lc, and similar for lc_right.
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 an IO operation into CoreM while consuming its SimplCount