Normalize instance family type in Haskell template

I use the genifunctors package to instantiate a functor for a type whose definition includes type families.

The first module defines the data type itself:

 {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE DataKinds #-} module Temp where data Record (p :: (*,*)) type family Fst p where Fst (Record '(a,b)) = a type family Snd p where Snd (Record '(a,b)) = b data Bar s = Bar { field_a :: Fst s, field_b :: Snd s } newtype Baz a = Baz { getBaz :: Bar (Record '(Maybe a, [a])) } 

This works as expected:

 λ> import Temp λ> :t Baz $ Bar (Just "a") ["b"] Baz $ Bar (Just "a") ["b"] :: Baz [Char] 

Functor instance is defined in a separate module:

 {-# LANGUAGE TemplateHaskell #-} module Temp2 where import Temp import Data.Generics.Genifunctors instance Functor (Baz a) where fmap = $(genFmap ''Baz) 

And gives this error:

 λ> import Temp1 src/Temp2.hs:9:12-24: Exception when trying to run compile-time code: … unexpected TyCon: FamilyI (ClosedTypeFamilyD Temp.Fst [PlainTV p_1627394000] (Just StarT) [TySynEqn [AppT (ConT Temp.Record) (AppT (AppT (ConT GHC.Tuple.(,)) (VarT a_1627394001)) (VarT b_1627394002))] (VarT a_1627394001)]) [] Code: genFmap ''Baz In the splice: $(genFmap ''Baz) 

This is because genifunctors , like geniplate , can only process types from newtype or data declarations.

This can be fixed by normalizing the type before recursively checking it.

So, is there a way to do this in a Haskell splice pattern? (I.e. in Q monad?)

+7
haskell ghc template-haskell type-families data-kinds
source share

No one has answered this question yet.

See related questions:

278
What is the normal form of a weak head?
255
Getting Associated Synonyms with a Haskell Pattern
242
What's wrong with a Haskell template?
thirteen
GHC Option -ddump-splices - Haskell Template
nine
Haskell-type family apps not rated
7
Is there a way to get binary instances for vinyl record types using Derive and Template Haskell or otherwise
6
Retrieving alerts from Haskell templates
5
Splice type signature in haskell template
2
Haskell: instance definitions for type families
one
How to automatically derive FromJSON using the Haskell, Aeson template families and types

All Articles