Search code examples
haskelltemplate-haskell

Obtaining TH.Name for '[] without -XTemplateHaskell


Is there a way to obtain (import from base modules or write expression) a value of type Language.Haskell.TH.Name that represents '[] without enabling -XTemplateHaskell?

A good reason to do so is that tools like hlint do not play well with TH and being able to avoid it therefore has a benefit. Then I could put a definition

nilName :: Name
nilName = '[]

in a separate file and import it, but this only makes sense if there is no standard name by which it can be imported or called. Furthermore, nilName cannot be used in pattern matches. Is there such a thing?


Solution

  • import Language.Haskell.TH.Syntax
    
    nilName = mkNameG DataName "ghc-prim" "GHC.Types" "[]"
    

    is an equivalent definition of nilName, even though it is ugly. It can be expanded to a form that admits to pattern matching yielding to

    nilName = Name (OccName "[]") (NameG DataName (PkgName "ghc-prim") (ModName "GHC.Types"))
    

    which is not nicer nor robust. It seems that the best route forward is a combination of the above nilName defined in a separate TH-enabled module together with (== nilName) instead of pattern matching.