Scala是否有像ML这样的价值限制,如果没有,那为什么呢? [英] Does Scala have a value restriction like ML, if not then why?
问题描述
这是我对这个问题的看法.任何人都可以确认,否认或详细说明吗?
Here’s my thoughts on the question. Can anyone confirm, deny, or elaborate?
我写:
Scala不会将协变量
List[A]
与分配了List[Int]
的GLB un统一,bcz afaics在对分配的方向很重要.因此,None
必须具有类型Option[⊥]
(即Option[Nothing]
),同上Nil
类型List[Nothing]
,它们不能分别接受来自和Option[Int]
或List[Int]
的赋值.因此,价值限制问题源于无方向的统一,而直到最近的相关研究被认为是全球性的统一还无法确定.
Scala doesn’t unify covariant
List[A]
with a GLB ⊤ assigned toList[Int]
, bcz afaics in subtyping "biunification" the direction of assignment matters. ThusNone
must have typeOption[⊥]
(i.e.Option[Nothing]
), dittoNil
typeList[Nothing]
which can’t accept assignment from anOption[Int]
orList[Int]
respectively. So the value restriction problem originates from directionless unification and global biunification was thought to be undecidable until the recent research linked above.
您可能希望查看上述评论的上下文.
You may wish to view the context of the above comment.
ML的值限制将禁止在(以前被认为是罕见的,但可能更普遍的),否则这样做是合理的(例如,类型安全的)情况,例如特别是对于咖喱函数的部分应用(这在函数式编程中很重要),因为替代性的键入解决方案在函数式和命令式编程之间形成了分层以及破坏模块化抽象类型的封装. Haskell具有类似的双重单态化限制. OCaml在某些情况下可以放宽限制.我详细介绍了.
ML’s value restriction will disallow parametric polymorphism in (formerly thought to be rare but maybe more prevalent) cases where it would otherwise be sound (i.e. type safe) to do so such as especially for partial application of curried functions (which is important in functional programming), because the alternative typing solutions create a stratification between functional and imperative programming as well as break encapsulation of modular abstract types. Haskell has an analogous dual monomorphisation restriction. OCaml has a relaxation of the restriction in some cases. I elaborated about some of these details.
我在上面的引用中表达的原始直觉(可以通过子类型避免价值限制)
my original intuition as expressed in the above quote (that the value restriction may be obviated by subtyping) is incorrect. The answers IMO elucidate the issue(s) well and I’m unable to decide which in the set containing Alexey’s, Andreas’, or mine, should be the selected best answer. IMO they’re all worthy.
推荐答案
比这简单得多.在Scala中,值不能具有多态类型,只有方法可以.例如.如果你写
It's much simpler than that. In Scala values can't have polymorphic types, only methods can. E.g. if you write
val id = x => x
其类型不是[A] A => A
.
如果您采用多态方法,例如
And if you take a polymorphic method e.g.
def id[A](x: A): A = x
并尝试将其分配给一个值
and try to assign it to a value
val id1 = id
再次
编译器将尝试(在这种情况下失败)推断特定的A
而不是创建多态值.
again the compiler will try (and in this case fail) to infer a specific A
instead of creating a polymorphic value.
因此不会出现此问题.
如果您尝试重现示例中的 http://mlton.org/ValueRestriction#_alternatives_to_the_value_restriction 示例Scala,您遇到的问题不是缺少let
:val
恰好对应于它.但是您需要类似
If you try to reproduce the http://mlton.org/ValueRestriction#_alternatives_to_the_value_restriction example in Scala, the problem you run into isn't the lack of let
: val
corresponds to it perfectly well. But you'd need something like
val f[A]: A => A = {
var r: Option[A] = None
{ x => ... }
}
这是非法的.如果您编写def f[A]: A => A = ...
是合法的,但在每次调用时都会创建一个新的r
.用机器学习术语来说,就像
which is illegal. If you write def f[A]: A => A = ...
it's legal but creates a new r
on each call. In ML terms it would be like
val f: unit -> ('a -> 'a) =
fn () =>
let
val r: 'a option ref = ref NONE
in
fn x =>
let
val y = !r
val () = r := SOME x
in
case y of
NONE => x
| SOME y => y
end
end
val _ = f () 13
val _ = f () "foo"
这是价值限制所允许的.
which is allowed by the value restriction.
也就是说,Scala的规则等效于只允许lambda作为ML中的多态值,而不是值限制所允许的一切.
That is, Scala's rules are equivalent to only allowing lambdas as polymorphic values in ML instead of everything value restriction allows.
这篇关于Scala是否有像ML这样的价值限制,如果没有,那为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!