为什么 Safe Haskell 不支持 Template Haskell?
Safe Haskell 的文档指出:
[...] 不幸的是,Haskell 模板可用于破坏模块边界,因此可用于访问此构造函数。 [...] 使用 -XSafe 标志来编译 Danger 模块限制了可用于安全子集的 Haskell 功能。这包括禁止 unsafePerfromIO、Template Haskell、[...]
用作将 AST 转换为另一个 AST 的宏系统,如果不可能简单地将 TH 限制为 Haskell 的安全子集,并将生成的 AST 限制为此子集?
The documentation for Safe Haskell states:
[...] Unfortunately Template Haskell can be used to subvert module boundaries and so could be used gain access to this constructor. [...] The use of the -XSafe flag to compile the Danger module restricts the features of Haskell that can be used to a safe subset. This includes disallowing unsafePerfromIO, Template Haskell,[...]
Used as a macro system that translates an AST to another AST, should it not be possible to simply restrict TH to the safe subset of Haskell, and also restrict the resulting AST to this subset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您链接的页面上再往下一点:
对副作用的担忧来自这样一个事实:TH 允许您使用
runIO
。这将使任何安全的希望都破灭。打破模块边界意味着使用 TH,您可以访问数据构造函数,即使模块没有导出它们。
请参阅 此存储库,了解 Safe Haskell 中允许的不安全事物的许多示例,包括 < a href="https://github.com/dterei/SafeHaskellExamples/tree/master/thReify" rel="noreferrer">打破模块边界。
如果禁用这些功能,Haskell 模板可能会变得安全,但这需要对 TH 进行重大更改。
A bit further down on the page you linked:
The concern about side effects comes from the fact that TH allows you to run arbitrary
IO
computations at compile time usingrunIO
. This would throw any hope of safety right out the window.Breaking module boundaries means that using TH you can for example access data constructors even though a module did not export them.
See this repository for many examples of things that would be unsafe to allow in Safe Haskell, including an example of breaking module boundaries.
It might be possible that Template Haskell could be made safe if these features were disabled, however it would require significant changes to TH.