gpt4 book ai didi

haskell - 模板 Haskell 声明中的局部变量

转载 作者:行者123 更新时间:2023-12-04 12:37:07 25 4
gpt4 key购买 nike

我正在阅读 pozorvlak's baby steps post on Template Haskell为了自己理解它,我遇到了这个部分:

Recall that we were trying to programmatically produce declarations of the form data Fred = Fred. Let's try it with quasiquoting. Because of the restrictions on calling TH code, we'll have to put it in its own module, so let's put the following in Keyword.hs so the compiler can find it:

module Keyword (keyword) where

import Language.Haskell.TH.Syntax
keyword name = [d| data $(name) = $(name) |]

Now compile:

Prelude> :l Keyword.hs
[1 of 1] Compiling Keyword ( Keyword.hs, interpreted )

Keyword.hs:6:24: parse error on input `$('


这给我敲响了警钟,似乎与我最近读到的其他内容相似, the Template Haskell package documentation :

For dynamically bound thing (NameS) we probably want them to in a context-dependent way, so again we don't want the name space. For example:

let v = mkName "T" in [| data $v = $v |]

Here we use the same Name for both type constructor and data constructor



好吧,这几乎是一样的,让我们看看我是否可以让它工作:
 module Example where
import Language.Haskell.TH
let v = mkName "T" in [| data $v = $v |]

试一试:
 % ghc -XTemplateHaskell -c Example.hs

Example.hs:3:25: parse error on input `data'

嗯...哦,也许我需要使用 d声明引用?
 let v = mkName "T" in [d| data $v = $v |]

现在:
 Example.hs:3:31: parse error on input `$v'

发生什么了?使用显式拼接不会改变任何一个错误。我是在断章取义地使用 Template Haskell 文档,还是只是错了?

最佳答案

看起来这是早于 6.12 的 GHC 版本的错误,其中 you can't splice in types .此错误已在 GHC 版本 7.10.1 中修复

关于haskell - 模板 Haskell 声明中的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com