本文介绍了类型推断会干扰参照透明性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell语言在引用透明性方面提供了什么确切的承诺/保证?至少Haskell报告中没有提及这一概念.

What is the precise promise/guarantee the Haskell language provides with respect to referential transparency? At least the Haskell report does not mention this notion.

考虑表达式

(7^7^7`mod`5`mod`2)

我想知道此表达式是否为1.为了安全起见,我将执行两次:

And I want to know whether or not this expression is 1. For my safety, I will do perform this twice:

( (7^7^7`mod`5`mod`2)==1, [False,True]!!(7^7^7`mod`5`mod`2) )

现在为(True,False)提供GHCi 7.4.1.

which now gives (True,False) with GHCi 7.4.1.

很明显,此表达式现在是参照不透明的.如何判断程序是否受到此类行为的影响?我可以用::淹没程序,但这并不能使其变得很可读.我错过了其他类的Haskell程序吗?那是在完全注释的和未注释的之间?

Evidently, this expression is now referentially opaque. How can I tell whether or not a program is subject to such behavior? I can inundate the program with :: all over but that does not make it very readable. Is there any other class of Haskell programs in between that I miss? That is between a fully annotated and an unannotated one?

(除了唯一的相关问题我在那儿找到一定是其他东西

(Apart from the only somewhat related question I found on SO there must be something else on this)

推荐答案

问题是超载,的确确实违反了参照透明性.您不知道在Haskell中类似(+)的功能是什么.取决于类型.

The problem is overloading, which does indeed sort of violate referential transparency. You have no idea what something like (+) does in Haskell; it depends on the type.

在Haskell程序中不受限制的数字类型时,编译器将使用默认类型来选择一些合适的类型.这是为了方便起见,通常不会引起任何意外.但是在这种情况下,确实引起了意外.在ghc中,您可以使用-fwarn-type-defaults来查看编译器何时使用默认值为您选择类型.您还可以将default ()行添加到模块中,以停止所有默认设置.

When a numeric type is unconstrained in a Haskell program the compiler uses type defaulting to pick some suitable type. This is for convenience, and usually doesn't lead to any surprises. But in this case it did lead to a surprise. In ghc you can use -fwarn-type-defaults to see when the compiler has used defaulting to pick a type for you. You can also add the line default () to your module to stop all defaulting.

这篇关于类型推断会干扰参照透明性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:06