值限制 [英] The value restriction

查看:85
本文介绍了值限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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的博士学位论文(并不是唯一可能要添加到引入问题的纯lambda演算中的构造),并且不是对撰写本文时(包括他的)存在的系统进行的调查.

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屋!

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