为什么部分应用程序有价值限制? [英] Why does a partial application have value restriction?

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

问题描述

我可以理解allowing mutable是值限制和弱多态性的原因.基本上,函数内部的可变ref可能会更改所涉及的类型并影响函数的未来使用.因此,在类型不匹配的情况下,可能不会引入真正的多态性.

I can understand that allowing mutable is the reason for value restriction and weakly polymorphism. Basically a mutable ref inside a function may change the type involved and affect the future use of the function. So real polymorphism may not be introduced in case of type mismatch.

例如

# let remember =
    let cache = ref None in
    (fun x ->
       match !cache with
       | Some y -> y
       | None -> cache := Some x; x)
  ;;
val remember : '_a -> '_a = <fun>

请记住,缓存最初是'a option,但是一旦它第一次被调用为let () = remember 1,缓存就变成了int option,因此类型变得有限.价值限制解决了这个潜在的问题.

In remember, cache originally was 'a option, but once it gets called first time let () = remember 1, cache turns to be int option, thus the type becomes limited. Value restriction solves this potential problem.

我仍然不了解部分应用程序的价值限制.

What I still don't understand is the value restriction on partial application.

例如,

let identity x = x
val identity: 'a -> 'a = <fun>

let map_rep = List.map identity
val map_rep: '_a list -> '_a list = <fun>

在上面的函数中,我看不到任何引用或可变的地方,为什么仍然应用值限制?

in the functions above, I don't see any ref or mutable place, why still value restriction is applied?

推荐答案

以下是一篇很好的文章,描述了OCaml当前对值限制的处理方式:

Here is a good paper that describes OCaml's current handling of the value restriction:

行军,放宽价值限制

它很好地概括了问题及其历史.

It has a good capsule summary of the problem and its history.

以下是其中一些观察值,以了解它们的价值.我不是专家,只是业余观察者:

Here are some observations, for what they're worth. I'm not an expert, just an amateur observer:

  1. 价值限制"一词中价值"的含义具有很高的技术性,与特定语言操纵的价值没有直接关系.这是一个句法术语;也就是说,您只需查看程序的符号即可识别值,而无需了解类型.

  1. The meaning of "value" in the term "value restriction" is highly technical, and isn't directly related to the values manipulated by a particular language. It's a syntactic term; i.e., you can recognize values by just looking at the symbols of the program, without knowing anything about types.

毫不费力地制作示例,其中值限制过于严格.即,在值限制禁止的情况下可以泛化类型的地方.但是,试图做得更好(允许更多概括)的规则导致规则太难为普通人(例如我自己)记住和遵循.

It's not hard at all to produce examples where the value restriction is too restrictive. I.e., where it would be safe to generalize a type when the value restriction forbids it. But attempts to do a better job (to allow more generalization) resulted in rules that were too difficult to remember and follow for mere mortals (such as myself).

准确地概括何时可以安全进行的障碍不是单独的编译(IMHO),而是停顿问题.也就是说,即使您看到了所有程序文本,从理论上讲也是不可能的.

The impediment to generalizing exactly when it would be safe to do so is not separate compilation (IMHO) but the halting problem. I.e., it's not possible in theory even if you see all the program text.

这篇关于为什么部分应用程序有价值限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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