| 1 |
{-# LANGUAGE CPP #-} |
| 2 |
{-# LANGUAGE FlexibleInstances #-} |
| 3 |
{-# LANGUAGE MagicHash #-} |
| 4 |
{-# LANGUAGE TemplateHaskellQuotes #-} |
| 5 |
{-# LANGUAGE TypeSynonymInstances #-} |
| 6 |
|
| 7 |
{-# OPTIONS_GHC -Wno-orphans #-} |
| 8 |
|
| 9 |
module Language.Haskell.TH.Lift |
| 10 |
( deriveLift |
| 11 |
, deriveLiftMany |
| 12 |
, deriveLift' |
| 13 |
, deriveLiftMany' |
| 14 |
, makeLift |
| 15 |
, makeLift' |
| 16 |
, Lift(..) |
| 17 |
) where |
| 18 |
|
| 19 |
import Control.Monad ((<=<), zipWithM) |
| 20 |
|
| 21 |
import Data.Char (ord) |
| 22 |
import Data.Maybe (catMaybes) |
| 23 |
|
| 24 |
import GHC.Base (unpackCString#) |
| 25 |
import GHC.Exts (Char(..), Double(..), Float(..), Int(..), Word(..)) |
| 26 |
import GHC.Prim (Addr#, Char#, Double#, Float#, Int#, Word#) |
| 27 |
|
| 28 |
import Language.Haskell.TH |
| 29 |
import Language.Haskell.TH.Datatype as Datatype |
| 30 |
import qualified Language.Haskell.TH.Lib as Lib (starK) |
| 31 |
import Language.Haskell.TH.Lift.Internal |
| 32 |
import Language.Haskell.TH.Syntax |
| 33 |
|
| 34 |
-- | Derive a 'Lift' instance for the given datatype. |
| 35 |
-- |
| 36 |
-- Note that 'deriveLift' uses a very simple technique for inferring the |
| 37 |
-- instance context: it simply takes all visible type variables from the data |
| 38 |
-- type declaration and adds a 'Lift' constraint for each one. For instance, |
| 39 |
-- in the following example: |
| 40 |
-- |
| 41 |
-- @ |
| 42 |
-- data Foo a b = ... |
| 43 |
-- $(deriveLift ''Foo) |
| 44 |
-- @ |
| 45 |
-- |
| 46 |
-- The following instance would be generated: |
| 47 |
-- |
| 48 |
-- @ |
| 49 |
-- instance (Lift a, Lift b) => Lift (Foo a b) where ... |
| 50 |
-- @ |
| 51 |
-- |
| 52 |
-- This will not work in all situations, however. For instance, there could |
| 53 |
-- conceivably be type variables that are not of the appropriate kind. For |
| 54 |
-- these other situations, the 'makeLift' function can provide a more |
| 55 |
-- fine-grained approach that allows specifying the instance context precisely. |
| 56 |
deriveLift :: Name -> Q [Dec] |
| 57 |
deriveLift name = do |
| 58 |
roles <- reifyDatatypeRoles name |
| 59 |
info <- reifyDatatype name |
| 60 |
fmap (:[]) $ deriveLiftOne roles info |
| 61 |
|
| 62 |
-- | Derive 'Lift' instances for many datatypes. |
| 63 |
deriveLiftMany :: [Name] -> Q [Dec] |
| 64 |
deriveLiftMany names = do |
| 65 |
roles <- mapM reifyDatatypeRoles names |
| 66 |
infos <- mapM reifyDatatype names |
| 67 |
mapM (uncurry deriveLiftOne) $ zip roles infos |
| 68 |
|
| 69 |
-- | Obtain 'Info' values through a custom reification function. This is useful |
| 70 |
-- when generating instances for datatypes that have not yet been declared. |
| 71 |
deriveLift' :: [Role] -> Info -> Q [Dec] |
| 72 |
deriveLift' roles = fmap (:[]) . deriveLiftOne roles <=< normalizeInfo |
| 73 |
|
| 74 |
deriveLiftMany' :: [([Role], Info)] -> Q [Dec] |
| 75 |
deriveLiftMany' = mapM (\(rs, i) -> deriveLiftOne rs =<< normalizeInfo i) |
| 76 |
|
| 77 |
-- | Generates a lambda expresson which behaves like 'lift' (without requiring |
| 78 |
-- a 'Lift' instance). Example: |
| 79 |
-- |
| 80 |
-- @ |
| 81 |
-- newtype Fix f = In { out :: f (Fix f) } |
| 82 |
-- |
| 83 |
-- instance Lift (f (Fix f)) => Lift (Fix f) where |
| 84 |
-- lift = $(makeLift ''Fix) |
| 85 |
-- @ |
| 86 |
-- |
| 87 |
-- This can be useful when 'deriveLift' is not clever enough to infer the |
| 88 |
-- correct instance context, such as in the example above. |
| 89 |
makeLift :: Name -> Q Exp |
| 90 |
makeLift = makeLiftInternal <=< reifyDatatype |
| 91 |
|
| 92 |
-- | Like 'makeLift', but using a custom reification function. |
| 93 |
makeLift' :: Info -> Q Exp |
| 94 |
makeLift' = makeLiftInternal <=< normalizeInfo |
| 95 |
|
| 96 |
makeLiftInternal :: DatatypeInfo -> Q Exp |
| 97 |
makeLiftInternal i = withInfo i $ \_ n _ cons -> makeLiftOne n cons |
| 98 |
|
| 99 |
deriveLiftOne :: [Role] -> DatatypeInfo -> Q Dec |
| 100 |
deriveLiftOne roles i = withInfo i liftInstance |
| 101 |
where |
| 102 |
liftInstance dcx n tys cons = do |
| 103 |
-- roles <- reifyDatatypeRoles n |
| 104 |
-- Compute the set of phantom variables. |
| 105 |
let phtys = catMaybes $ |
| 106 |
zipWith (\t role -> if role == PhantomR then Just t else Nothing) |
| 107 |
tys |
| 108 |
roles |
| 109 |
_x <- newName "x" |
| 110 |
instanceD (ctxt dcx phtys tys) |
| 111 |
(conT ''Lift `appT` typ n tys) |
| 112 |
[ funD 'lift [clause [] (normalB (makeLiftOne n cons)) []] |
| 113 |
#if MIN_VERSION_template_haskell(2,16,0) |
| 114 |
, let rhs = varE 'unsafeSpliceCoerce `appE` |
| 115 |
(varE 'lift `appE` varE _x) in |
| 116 |
funD 'liftTyped [clause [varP _x] (normalB rhs) []] |
| 117 |
#endif |
| 118 |
] |
| 119 |
typ n = foldl appT (conT n) . map unKind |
| 120 |
-- Only consider *-kinded type variables for now. Furthermore, filter out |
| 121 |
-- type variables that are obviously phantom. |
| 122 |
ctxt dcx phtys = |
| 123 |
fmap (dcx ++) . cxt . concatMap liftPred . filter (`notElem` phtys) |
| 124 |
liftPred ty = |
| 125 |
case ty of |
| 126 |
SigT t k |
| 127 |
| k == Lib.starK -> mkLift t |
| 128 |
| otherwise -> [] |
| 129 |
_ -> mkLift ty |
| 130 |
mkLift ty = [conT ''Lift `appT` (return ty)] |
| 131 |
unKind (SigT t k) |
| 132 |
| k == Lib.starK = return t |
| 133 |
unKind t = return t |
| 134 |
|
| 135 |
makeLiftOne :: Name -> [ConstructorInfo] -> Q Exp |
| 136 |
makeLiftOne n cons = do |
| 137 |
e <- newName "e" |
| 138 |
lam1E (varP e) $ caseE (varE e) $ consMatches n cons |
| 139 |
|
| 140 |
consMatches :: Name -> [ConstructorInfo] -> [Q Match] |
| 141 |
consMatches n [] = [match wildP (normalB e) []] |
| 142 |
where |
| 143 |
e = varE 'errorQuoteExp `appE` |
| 144 |
(stringE $ "Can't lift value of empty datatype " ++ nameBase n) |
| 145 |
consMatches _ cons = concatMap doCons cons |
| 146 |
|
| 147 |
doCons :: ConstructorInfo -> [Q Match] |
| 148 |
doCons (ConstructorInfo { constructorName = c |
| 149 |
, constructorFields = ts |
| 150 |
, constructorVariant = variant |
| 151 |
}) = (:[]) $ do |
| 152 |
ns <- zipWithM (\_ i -> newName ('x':show (i :: Int))) ts [0..] |
| 153 |
let con = [| conE c |] |
| 154 |
case (variant, ns, ts) of |
| 155 |
(InfixConstructor, [x0, x1], [t0, t1]) -> |
| 156 |
let e = varE 'infixApp `appE` liftVar x0 t0 `appE` con `appE` liftVar x1 t1 |
| 157 |
in match (infixP (varP x0) c (varP x1)) (normalB e) [] |
| 158 |
(_, _, _) -> |
| 159 |
let e = foldl (\e1 e2 -> varE 'appE `appE` e1 `appE` e2) con $ zipWith liftVar ns ts |
| 160 |
in match (conP c (map varP ns)) (normalB e) [] |
| 161 |
|
| 162 |
-- Reify the roles of a data type. Note that the argument Name may correspond |
| 163 |
-- to that of a data family instance constructor, so we need to go through |
| 164 |
-- reifyDatatype to determine what the parent data family Name is. |
| 165 |
reifyDatatypeRoles :: Name -> Q [Role] |
| 166 |
reifyDatatypeRoles n = do |
| 167 |
DatatypeInfo { datatypeName = dn } <- reifyDatatype n |
| 168 |
qReifyRoles dn |
| 169 |
|
| 170 |
liftVar :: Name -> Type -> Q Exp |
| 171 |
liftVar varName (ConT tyName) |
| 172 |
| tyName == ''Addr# = apps |
| 173 |
[ varE 'litE, varE 'stringPrimL |
| 174 |
, varE 'map `appE` |
| 175 |
infixApp (varE 'fromIntegral) (varE '(.)) (varE 'ord) |
| 176 |
, varE 'unpackCString# ] |
| 177 |
| tyName == ''Char# = apps [ varE 'litE, varE 'charPrimL, conE 'C# ] |
| 178 |
| tyName == ''Double# = apps [ varE 'litE, varE 'doublePrimL, varE 'toRational, conE 'D# ] |
| 179 |
| tyName == ''Float# = apps [ varE 'litE, varE 'floatPrimL, varE 'toRational, conE 'F# ] |
| 180 |
| tyName == ''Int# = apps [ varE 'litE, varE 'intPrimL, varE 'toInteger, conE 'I# ] |
| 181 |
| tyName == ''Word# = apps [ varE 'litE, varE 'wordPrimL, varE 'toInteger, conE 'W# ] |
| 182 |
|
| 183 |
where |
| 184 |
apps = foldr appE var |
| 185 |
|
| 186 |
var :: Q Exp |
| 187 |
var = varE varName |
| 188 |
|
| 189 |
liftVar varName _ = varE 'lift `appE` varE varName |
| 190 |
|
| 191 |
withInfo :: DatatypeInfo |
| 192 |
-> (Cxt -> Name -> [Type] -> [ConstructorInfo] -> Q a) |
| 193 |
-> Q a |
| 194 |
withInfo i f = case i of |
| 195 |
DatatypeInfo { datatypeContext = dcx |
| 196 |
, datatypeName = n |
| 197 |
, datatypeInstTypes = vs |
| 198 |
, datatypeCons = cons |
| 199 |
, datatypeVariant = variant |
| 200 |
} -> do |
| 201 |
case variant of |
| 202 |
Datatype.TypeData -> typeDataError n |
| 203 |
_ -> return () |
| 204 |
f dcx n vs cons |
| 205 |
|
| 206 |
-- | We cannot define implementations for @lift@ at the term level for |
| 207 |
-- @type data@ declarations, which only exist at the type level. |
| 208 |
typeDataError :: Name -> Q a |
| 209 |
typeDataError dataName = fail |
| 210 |
. showString "Cannot derive instance for ‘" |
| 211 |
. showString (nameBase dataName) |
| 212 |
. showString "‘, which is a ‘type data‘ declaration" |
| 213 |
$ "" |
| 214 |
|
| 215 |
#if !MIN_VERSION_template_haskell(2,23,0) |
| 216 |
instance Lift Name where |
| 217 |
lift (Name occName nameFlavour) = [| Name occName nameFlavour |] |
| 218 |
# if MIN_VERSION_template_haskell(2,16,0) |
| 219 |
liftTyped = unsafeSpliceCoerce . lift |
| 220 |
# endif |
| 221 |
|
| 222 |
instance Lift OccName where |
| 223 |
lift n = [| mkOccName |] `appE` lift (occString n) |
| 224 |
# if MIN_VERSION_template_haskell(2,16,0) |
| 225 |
liftTyped = unsafeSpliceCoerce . lift |
| 226 |
# endif |
| 227 |
|
| 228 |
instance Lift PkgName where |
| 229 |
lift n = [| mkPkgName |] `appE` lift (pkgString n) |
| 230 |
# if MIN_VERSION_template_haskell(2,16,0) |
| 231 |
liftTyped = unsafeSpliceCoerce . lift |
| 232 |
# endif |
| 233 |
|
| 234 |
instance Lift ModName where |
| 235 |
lift n = [| mkModName |] `appE` lift (modString n) |
| 236 |
# if MIN_VERSION_template_haskell(2,16,0) |
| 237 |
liftTyped = unsafeSpliceCoerce . lift |
| 238 |
# endif |
| 239 |
|
| 240 |
instance Lift NameFlavour where |
| 241 |
lift NameS = [| NameS |] |
| 242 |
lift (NameQ modnam) = [| NameQ modnam |] |
| 243 |
lift (NameU i) = [| NameU i |] |
| 244 |
lift (NameL i) = [| NameL i |] |
| 245 |
lift (NameG nameSpace' pkgName modnam) |
| 246 |
= [| NameG nameSpace' pkgName modnam |] |
| 247 |
# if MIN_VERSION_template_haskell(2,16,0) |
| 248 |
liftTyped = unsafeSpliceCoerce . lift |
| 249 |
# endif |
| 250 |
|
| 251 |
instance Lift NameSpace where |
| 252 |
lift VarName = [| VarName |] |
| 253 |
lift DataName = [| DataName |] |
| 254 |
lift TcClsName = [| TcClsName |] |
| 255 |
# if MIN_VERSION_template_haskell(2,21,0) |
| 256 |
lift (FldName parent) = [| FldName parent |] |
| 257 |
# endif |
| 258 |
# if MIN_VERSION_template_haskell(2,16,0) |
| 259 |
liftTyped = unsafeSpliceCoerce . lift |
| 260 |
# endif |
| 261 |
#endif |