bennofs / th-lift-instances

Lift instances for common haskell data types
http://hackage.haskell.org/package/th-lift-instances
Other
12 stars 11 forks source link

instance Lift TypeRep #27

Open parsonsmatt opened 3 years ago

parsonsmatt commented 3 years ago

I had a thought that this would be useful for some problem I'm trying to solve, but I'm not sure if it's feasible or useful for others. I'm opening this issue to attract commentary and other use-cases before I try and do it myself.

treeowl commented 2 years ago

@parsonsmatt shouldn't this go in template-haskell? Here's my attempt. I think it works, but this stuff is subtle. I imagine those derived instances will have to be replaced by whatever -ddump-splices says.

{-# language TypeApplications, PolyKinds, DataKinds, TemplateHaskellQuotes, GADTs, TypeFamilies
 , DeriveLift, StandaloneDeriving #-}

module LiftTypeRep where
import Type.Reflection
import Type.Reflection.Unsafe
import qualified Language.Haskell.TH.Syntax as TH
import GHC.Types 

deriving instance TH.Lift TyCon
deriving instance TH.Lift Module
deriving instance TH.Lift KindRep
deriving instance TH.Lift RuntimeRep
deriving instance TH.Lift VecCount
deriving instance TH.Lift VecElem
deriving instance TH.Lift Levity
deriving instance TH.Lift TypeLitSort
deriving instance TH.Lift TrName
deriving instance TH.Lift SomeTypeRep -- Needed recursively

instance TH.Lift (TypeRep a) where
  liftTyped (Fun a b) = [|| Fun a b ||]
  liftTyped (App f a) = [|| App f a ||]
  liftTyped (Con' tycon kvars)
    = [|| mkTrCon tycon kvars ||]
parsonsmatt commented 2 years ago

I ended up publishing lift-type, which does something similar (TypeRep -> TH.Type) and solved my original problem. I recall writing something very similar to what you wrote, and it ended up failing on DataKinds (Symbol and '[] are especially tricky). Indeed, someone else pointed out some other subtleties, so you've probably already considered this 😅

Adding that function to template-haskell was punted until GHC could be modified to support it more cleanly: https://gitlab.haskell.org/ghc/ghc/-/issues/20613

treeowl commented 2 years ago

Why would this fail with DataKinds? I don't see the sort of name munging that would lead to that. Am I missing something?

parsonsmatt commented 2 years ago

Oh, I think I was confused - a Q Exp or Q (TExp TypeRep) is different from a TypeRep -> TemplateHaskell.Type.