值限制 [英] The value restriction
问题描述
在OCaml中,您不能推广部分应用的咖喱函数(值限制").
In OCaml you can't generalize a partially-applied curried function (the "value restriction").
价值限制的目的是什么?如果不存在,会发生什么不愉快的事情?
What is the purpose of the value restriction? What unpleasant would happen if it did not exist?
推荐答案
没有值限制或其他用于限制泛化的机制,该程序将被类型系统接受:
Without the value restriction or other mechanisms to restrict generalization, this program would be accepted by the type system:
let r = (fun x -> ref x) [];; (* this is the line where the value restriction would trigger *)
> r : 'a list ref
r := [ 1 ];;
let cond = (!r = [ "foo" ]);;
变量r
的类型为'a list ref
,这意味着它的内容可以与[ "foo" ]
进行比较,尽管它包含一个整数列表.
The variable r
would have type 'a list ref
, meaning that its contents could be compared to [ "foo" ]
although it contained a list of integers.
有关更多动机,请参见 Xavier Leroy的博士学位论文(
See Xavier Leroy's PhD thesis for more motivation (ref
is not the only construct one may want to add to pure lambda-calculus that introduces the problem) and a survey of the systems that existed at the time of his thesis (including his).
这篇关于值限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!