7

对我得到的结果有点困惑。使用以下内容:

GHCi, version 7.4.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :m + Language.Haskell.TH

我得到了这个成功的结果:

Prelude Language.Haskell.TH> runQ [d| data X = X |]
[DataD [] X_0 [] [NormalC X_1 []] []]

但我得到这个错误:

Prelude Language.Haskell.TH> runQ [d| data X = X deriving Show |]

<interactive>:4:30:
    The exact Name `X' is not in scope
      Probable cause: you used a unique name (NameU) in Template Haskell but did not bind it
    In the Template Haskell quotation [d| data X = X deriving (Show) |]
    In the first argument of `runQ', namely
      `[d| data X = X deriving (Show) |]'
    In the expression: runQ [d| data X = X deriving (Show) |]

使用 Template Haskell 时无法派生 Show?

4

1 回答 1

7

看起来这是GHC中的一个错误,已在 7.6.1 版中修复:

$ ~/bin/ghc-7.6.1/bin/ghci
GHCi, version 7.6.1: http://www.haskell.org/ghc/  :? for help
[...]
Prelude> :set -XTemplateHaskell
Prelude> import Language.Haskell.TH
Prelude Language.Haskell.TH> runQ [d| data X = X deriving (Show) |]
[...]
[DataD [] X_0 [] [NormalC X_1 []] [GHC.Show.Show]]
于 2013-04-03T08:33:16.587 回答