0

I have to work with the following Template Haskell code:

{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE TemplateHaskell #-}

module myModule where

import Language.Haskell.TH

newtype C a = C (TExpQ a)
unC :: C a -> TExpQ a
unC (C x) = x

However when I load the file I get an error:

Not in scope: type constructor or class ‘TExpQ’

Perhaps you meant one of these:

  ‘TExp’ (imported from Language.Haskell.TH),

  ‘ExpQ’ (imported from Language.Haskell.TH)

I looked in the docs and TExpQ is in the library that should be loaded http://hackage.haskell.org/package/template-haskell-2.16.0.0/docs/Language-Haskell-TH-Lib.html

Is the library not loading? Is that why the type is not in scope?

pmichaels
  • 139
  • 6
  • 2
    Consider writing explicitly _what_ you import from a module. `import Language.Haskell.TH (TExpQ)` would have given a clearer error message. – leftaroundabout Dec 11 '20 at 16:55

1 Answers1

1

The abbreviations are defined in the Language.Haskell.TH.Lib module of the template-haskell package, so you should use:

{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE TemplateHaskell #-}

module MyModule where

import Language.Haskell.TH
import Language.Haskell.TH.Lib

newtype C a = C (TExpQ a)

unC :: C a -> TExpQ a
unC (C x) = x

A module starts with an Uppercase, so module MyModule, not module myModule.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I see. I thought Language.Haskell.TH would have also imported the Lib. Thanks – pmichaels Dec 11 '20 at 16:57
  • 1
    @pmichaels: no, there is no "hierarchy" that importing `A` will import `A.B`, that would also result in a lot of trouble if one would `import Data`, since then there will be a lot of modules that sometimes export types with the same name. Some modules indeed re-export other modules. – Willem Van Onsem Dec 11 '20 at 16:58