Haskell中的不安全功能是什么? [英] What is an unsafe function in Haskell?

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

问题描述

我相信一个不安全的函数会说它将返回某种类型的值,但实际上它会抛出异常并结束执行,因此根本不返回任何值,但是我不确定

I believe that an unsafe function is a function that says that it will return a value of some type, but that it can actually throw an exception and end the execution therefore not returning any value at all, but I'm not sure.

还是不安全的函数可以返回除签名中声明的类型以外的其他类型的值的函数?那不是弱类型函数吗?

Or could it be that an unsafe function is a function that can return a value of other type than the declared in the signature? Wouldn't that be a weakly typed function?

还是Haskell中的类型错误的不安全的同义词?

Or are weakly typed and unsafe synonyms in Haskell?

这可能是一个愚蠢的问题,但我找不到直接的答案.

This may be a silly question, but I couldn't find a straightforward answer.

我检查了 readLn 的文档,希望看到对不安全功能的引用,但没有任何内容.

I checked the documentation of readLn hoping to see a reference to unsafe functions, but there wasn't any.

这篇文章不安全的函数,讲述了一些关于破坏类型系统的内容,但并未指定使用哪种方式.是通过异常?是通过返回错误类型的值?

This article, Unsafe functions, says something about breaking the type system, but it doesn't specify in which manner; through exceptions?, through returning values of the wrong type?

那么,Haskell中的不安全函数是什么?

So, what is an unsafe function in Haskell?

推荐答案

在Haskell中有几种不安全"的概念.

There are several notions of "unsafe" in Haskell.

  1. 计算该值将导致I/O.这里的主要嫌疑人是unsafePerformIO.此定义是否应将惰性I/O和unsafeInterleaveIO视为不安全,这存在争议.

  1. Calculating the value leads to I/O. The prime suspect here is unsafePerformIO. It's a bit controversial whether lazy I/O and unsafeInterleaveIO should be considered unsafe by this definition.

某些东西破坏了类型系统.主要嫌疑犯是unsafeCoerce,但是unsafePerformIO也可以.

Something breaks the type system. The prime suspect is unsafeCoerce, but unsafePerformIO can do it too.

某些事情在不破坏类型系统的情况下破坏了内存安全性(感谢卡尔提醒我).主要可疑对象是unsafe数组或向量索引操作以及对外部函数接口的错误使用.

Something breaks memory safety without breaking the type system (thanks to Carl for reminding me). The prime suspects are unsafe array or vector indexing operations and incorrect use of the foreign function interface.

计算结果取决于评估顺序.主要嫌疑人是unsafePerformIO,但unsafeInterleaveST当然也可以.

The result of the calculation depends on the order of evaluation. The prime suspect is unsafePerformIO but unsafeInterleaveST can certainly do it too.

求值可能导致异常或无限循环.这是一种相对温和的不安全措施……除非不是那样.

Evaluation can lead to an exception or infinite loop. This is a relatively mild sort of unsafety ... except when it's not.

某些事情违反了约定(法律"),Haskell程序员依靠它们来推理其代码.是否应将其视为不安全的"尚有争议.示例:将seq应用于函数,以某种方式使用coerce来更改函数相对于其引用实现的适应性,并且如果有人将seq应用于以前的部分应用程序,而现在又将其应用,则会引起问题可能是最底层的(在某些情况下,出于良好的性能原因),编写违反函子,可应用性,monad,可遍历等法律的类实例.期望参数满足先决条件但不检查其是否满足条件(例如,将升序列表快速转换为集合或映射的函数).

Something breaks the conventions ("laws") Haskell programmers rely on to reason about their code. Whether this should be considered "unsafe" is subject to debate. Examples: applying seq to a function, using coerce in such a manner as to change the arity of a function relative to its reference implementation and cause a problem if someone applies seq to what was previously a partial application and now can possibly be bottom (there are good performance reasons to do this in some cases), writing class instances that break functor, applicative, monad, traversable, etc., laws. Expecting arguments to satisfy pre-conditions but not checking that they do (e.g., functions that quickly turn ascending lists into sets or maps).

Safe Haskell

为帮助程序员控制其中一些不安全的形式,Safe Haskell系统根据模块的导入和使用的语言扩展将模块分为安全或不安全.我尚未研究细节,但GarethR 表示那个

我认为您的概念1至3将被Safe Haskell视为不安全.值得一读,因为安全Haskell"作者已经对安全性进行了深入思考.

I think your notion 1 through 3 would be considered unsafe by Safe Haskell. It may be worth reading up on it because the Safe Haskell authors have clearly thought deeply on safety.

和ØrjanJohansen 表示

and Ørjan Johansen indicates that

Safe Haskell还不允许符合第6点的某些要求,例如可以绕过模块导出边界的扩展(模板Haskell,广义新类型派生)或更改导入代码的行为(规则,重叠实例).

Safe Haskell also disallows some things that fit in point 6, such as extensions that can circumvent module export boundaries (Template Haskell, generalized newtype deriving) or change the behavior of imported code (rules, overlapping instances).

程序员可以标记模块Safe表示他们希望GHC检查它是否安全,Unsafe表示它不安全,或者Trustworthy表示作者声称相信其模块.尽管API的实现使用了不安全的功能,但仍可以安全使用.

A programmer can mark a module Safe to indicate that they want GHC to check that it is safe, Unsafe to indicate that it is not safe, or Trustworthy to indicate that the author claims to believe that its API is safe to use despite the fact that its implementation uses unsafe features.

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

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