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.