为什么不安全的Haskell支持模板Haskell? [英] Why doesn't Safe Haskell support Template Haskell?

查看:102
本文介绍了为什么不安全的Haskell支持模板Haskell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Safe Haskell 的文件:


[...]不幸的是,模板Haskell可以用来破坏模块边界,所以可以用来访问这个构造函数。 [...]使用-XSafe标志编译Danger模块会限制可用于安全子集的Haskell功能。这包括不允许unsafePerfromIO,模板Haskell,[...]

用作一个将AST转换为另一个AST的宏系统,应该它不可能简单地将TH限制在Haskell的安全子集中​​,并且还将产生的AST限制在这个子集中?

解决方案

在你链接的页面上进一步缩小:


TemplateHaskell - 特别危险,因为它甚至可以在编译时引起副作用,并且可以用于访问抽象数据类型。使用TH打破模块边界是非常容易的。


对副作用的担忧来自于TH允许您运行任意 IO 编译时的计算使用 runIO 。这会给安全带来任何希望。



打破模块边界意味着使用TH你可以访问数据构造函数,即使模块没有导出它们。



请参阅此资料库,以查看许多事例那么在安全Haskell中将是不安全的,包括打破模块边界的示例。 。

如果禁用了这些功能,模板Haskell可能会变得安全,但是它需要对TH进行重大更改。


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?

解决方案

A bit further down on the page you linked:

TemplateHaskell — Is particularly dangerous, as it can cause side effects even at compilation time and can be used to access abstract data types. It is very easy to break module boundaries with TH.

The concern about side effects comes from the fact that TH allows you to run arbitrary IO computations at compile time using runIO. 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.

这篇关于为什么不安全的Haskell支持模板Haskell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆