gpt4 book ai didi

haskell - 使用 TemplateHaskell 列出命名空间中的所有名称

转载 作者:行者123 更新时间:2023-12-03 05:23:05 25 4
gpt4 key购买 nike

我想要一个 TemplateHaskell 函数 variablesInScope::Q [Name],它返回范围内所有变量的 Name 列表。 TemplateHaskell 显然可以使用此信息来实现诸如 reify::Name -> Q InfolookupValueName::String -> Q (Maybe Name) 之类的函数。

我想要的功能是否存在于某个地方而我只是忽略了它?或者它可以以某种方式轻松构建吗?

最佳答案

不幸的是,您无法单独使用 TH 来完成此操作。尝试使用 haskell-src-meta 将 Haskell 模块解析为 TH AST

不过,它需要 Q monad 的 IO 功能来加载模块。

请引用https://ghc.haskell.org/trac/ghc/ticket/9699#ticket查看当前的粗略规范

(1) Extend ​ModuleInfo (obtained from ​reifyModule) to ModuleInfo [Module] [Name], where [Module] is still the import list and [Name] contains the module's list of exported names.

(2) Add thisModule :: Q Module producing the current ​Module.

(3) Add topLevelNames :: Q [Name] producing a list of top-level names (both exported and non-exported) bound in the current module that would be visible to reify.

(4) Add nestedNames :: Q [Name] (in need of a better name) producing a list of the non-top-level (nested) names visible to reify in this context.

(5) Add parentNames :: Q [Name] (also in need of a better name) producing a list of the names immediately associated with the current splicing context, if available. For example, foo, bar :: $(typeSplice) would see [foo, bar], foo = $(exprSplice) would see [foo], and $(topLevelDecSplice) would see [].

(6) Optional Add isTopLevel :: Name -> Q Bool to detect whether a name is bound at the top level (of the current module?). Something like this could alternately be accomplished by searching through topLevelNames.

关于haskell - 使用 TemplateHaskell 列出命名空间中的所有名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26394199/

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