Skip to content

TH 'Lift' instance for 'NonEmpty'

I was using deriving Lift on a data type and the DeriveLift extension:

import Data.List.NonEmpty
import Language.Haskell.TH

data T = T (NonEmpty String) Int deriving Lift

and I noticed I couldn't get an automatic instance because NonEmpty doesn't have a Lift instance. I'm wondering if an instance can be added to the template-haskell package (or elsewhere if that isn't the right place? I'm assuming it is because NonEmpty is in base now)

Since NonEmpty has a Data instance, I suppose the following would be enough?

instance Data a => Lift (NonEmpty a)

And without using Data it could be:

nonemptyConName :: Name
nonemptyConName = mkNameG DataName "base" "Data.List.NonEmpty" ":|"

instance Lift a => Lift (NonEmpty a) where
    lift (x :| xs) = do
        x' <- lift x
        xs' <- traverse lift xs
        return $ ConE nonemptyConName `AppE` x' `AppE` xs'
Edited by fr33domlover
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information