Add Lift, Data and Generic instances for Type.Reflection.TypeRep
Here are the proposed instances. I'm sure about the meta information for the Generic
and Data
instances.
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Type.Reflection
import Data.Data hiding (typeRep, TypeRep)
import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Lib
import GHC.Generics hiding (Prefix)
-- Broken, see below:
-- instance Lift (TypeRep a) where
-- liftTyped tr = withTypeable tr [||typeRep||]
--
-- instance Lift SomeTypeRep where
-- liftTyped (SomeTypeRep (tr :: TypeRep a)) =
-- withTypeable tr [||someTypeRep (Proxy @a)||]
instance Typeable a => Data (TypeRep a) where
gunfold _ point _ = point typeRep
toConstr tr = mkConstr (dataTypeOf tr) "typeRep" [] Prefix
dataTypeOf tr = mkDataType "Type.Reflection.TypeRep" [toConstr tr]
instance Typeable a => Generic (TypeRep a) where
type Rep (TypeRep a) =
D1 ('MetaData "TypeRep" "Type.Reflection" "base" 'False)
(C1 ('MetaCons "typeRep" 'PrefixI 'False) U1)
-- I'm not sure if it may be preferable to be lazy here, but I figured
-- it's more correct to be strict (the isomorphism preserves bottoms).
-- For comparison the instance for () is lazy in 'from' and strict in 'to'.
to (M1 (M1 U1)) = typeRep
from !_ = M1 (M1 U1)