简体   繁体   English

弗雷格如何概括数字文字?

[英]How does Frege generalize number literals?

It appears that Frege can evaluate 似乎弗雷格可以评估

1/2

to return the Double value 0.5. 返回Double值0.5。

The literal 1 is of type Int . 文字1Int类型。 It seems to be promoted to Double , which is a type in the Real class and thus knows the / operator. 它似乎被提升为Double ,它是Real类中的一个类型,因此知道/运算符。 How does this happen? 这是怎么发生的? Is it using the Haskell approach of silently replacing the literal 1 with fromInt 1 or is something else going on? 它是否使用Haskell方法静默地用fromInt 1替换文字fromInt 1或者是其他的东西? How is Double chosen as the instance of Real to use here? 如何选择Double作为Real的实例在这里使用? Is there a list of "default" instance types like in Haskell? 是否有像Haskell一样的“默认”实例类型列表?

Simple decimal literals without type indicator (ie one of the letters lndf) do not automatically have type Int in Frege. 没有类型指示符的简单小数文字(即字母lndf之一)在Frege中不会自动具有Int类型。

They will get assigned the type you probably wanted, and the literal will get adapted accordingly. 他们将被分配您可能想要的类型,文字将相应地进行调整。 This is why they are called DWIM (do what I mean) literals. 这就是为什么他们被称为DWIM(做我的意思)文字。

This is mentioned in the "Differences to Haskell" document. 这在“与Haskell的差异”文档中提到。 And it should be also in the language reference manual, of course. 当然,它也应该在语言参考手册中。 If it isn't there yet, it must be because the author is extremely lazy. 如果它还没有,那一定是因为作者非常懒惰。

In brief, DWIM works like this: when the type checker sees the literal the first time, it just assigns a type variable for it, plus a Num constraint for that type variable. 简而言之,DWIM的工作原理如下:当类型检查器第一次看到文字时,它只为它分配一个类型变量,加上该类型变量的Num约束。 Later, in a second pass, it finds all DWIM literals. 稍后,在第二遍中,它会找到所有DWIM文字。 And now, the type variable is in one of the following states : 现在,类型变量处于以下状态之一:

  1. unified with some type, like Long, Double or the like. 与某种类型统一,如Long,Double等。 The literal will be typed accordingly. 字面值将相应输入。
  2. Unified with some other type: this is an error. 与其他类型统一:这是一个错误。
  3. Unified with a type variable from a type signature: the literal gets replaced with an application of fromInt to the literal typed as Int. 与类型签名中的类型变量统一:文本被fromInt的应用程序替换为以Int类型输入的文字。
  4. Not unified at all: if the type variable is constrained by (a subtype of) Real the type is Double otherwise Int. 根本不统一:如果类型变量受(子类型)Real的约束,则类型为Double,否则为Int。

In your example, the literals are typed Double because of the Real constraint of the division operator. 在您的示例中,由于除法运算符的Real约束,因此文字是Double类型。

A similar approach is taken for literals with decimal point, however, they start out with a Real constraint, and can thus end up as Float or Double or application of fromDouble. 对于具有小数点的文字采用类似的方法,但是,它们以Real约束开始,因此最终可以作为Float或Double或fromDouble的应用程序。

Note that except for case 3 there is no type conversion or type cast at runtime. 请注意,除了案例3之外,在运行时没有类型转换或类型转换。

The reason literals are not generally overloaded like in Haskell is to avoid unnecessary class constraints, because their implementation tends to result in expensive code. 文本通常不像Haskell那样重载的原因是为了避免不必要的类约束,因为它们的实现往往会导致代码昂贵。 Also, since we don't have monomorphism restriction, you could have polymorphic constants accidentally. 此外,由于我们没有单态限制,您可能会意外地具有多态常量。 (Think fibs = 1:1:...) (认为​​fibs = 1:1:......)

With DWIM literals, you get monomorphic constants and functions unless you require otherwise with a type annotation. 使用DWIM文字,除非您需要使用类型注释,否则您将获得单态常量和函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM