'a和'_l有什么区别? [英] What is the difference between 'a and '_l?

查看:181
本文介绍了'a和'_l有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'a'_l有什么区别?

我正在查看此错误,但无法理解:

Error: This expression has type ('a -> float polynomial) list but an expression was expected of type float polynomial list derivlist: ('_l → float polynomial) list

解决方案

_表示弱多态变量:它处于无法泛化的位置.

OCaml常见问题解答中有两种与弱多态有关的解释:请参见 解决方案

_ denotes a weakly polymorphic variable : it is in a position where it cannot be generalized.

There are two explanations related to weak polymorphism in the OCaml FAQ: see A function obtained through partial application is not polymorphic enough and the next one.

This generally happens when you're using a non-local reference (whose type cannot be generalized), or when defining polymorphic functions that are not syntactically functions (they do not begin with fun x -> .. but rather a function application). In some cases there is an easy fix (eta-expansion, see the FAQ), sometimes there isn't, and sometimes your program was just unsound.

An easy example : let a = ref [] does not get the polymorphic a list ref type. Otherwise you could use both as a int list and a bool list, and mix elements of different types by mutating the reference. It instead get a '_a list ref type. This mean that the type is not polymorphic, but merely unknown. Once you do something with a with a particular type, it fixes '_a once and for all.

# let a = ref [];;
val a : '_a list ref = {contents = []}
# let sum_of_a = List.fold_left (+) 0 !a;;
val sum_of_a : int = 0
# a;;
- : int list ref = {contents = []}

For an in-depth explanation of value restriction and the "relaxed" value restriction actually implemented in the OCaml type checker, see the Relaxing the Value Restriction paper by Jacques Garrigue (2004).

这篇关于'a和'_l有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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