What is the difference between 'a
and '_l
?
a和l的区别是什么?
I was looking at this error, and couldn't comprehend it:
我看着这个错误,无法理解它:
Error: This expression has type ('a -> float polynomial) list but an expression was expected of type float polynomial list derivlist: ('_l → float polynomial) list
2 个解决方案
#1
23
_
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.
在OCaml常见问题中,有两种解释与弱多态性有关:通过部分应用程序获得的函数不够多态和下一个。
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.
当您使用非本地引用(其类型不能被一般化)或定义非语法函数的多态函数时,通常会发生这种情况(它们不是以有趣的x ->开头的)。而是一个函数应用程序。在某些情况下,有一个简单的修复(eta扩展,常见问题解答),有时没有,有时你的程序只是不健全的。
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.
一个简单的例子:让a = ref[]没有得到多态的列表ref类型。否则,您可以将二者作为int列表和bool列表使用,并通过更改引用来混合不同类型的元素。它会得到一个'_a列表ref类型。这意味着类型不是多态性的,但仅仅是未知的。一旦你用一个特定的类型做了某件事,它就会一次又一次地修复“_a”。
# 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).
对于OCaml类型检查中实际实现的价值限制和“放松”价值限制的深入解释,请参见Jacques Garrigue(2004)对价值限制文件的放松。
#2
1
There is a weak type variable in the error message from the compiler, but I am not sure that the weak variable is related to the error (I do not see how the status of the type variable could be the cause of this message).
在编译器的错误消息中有一个弱类型变量,但是我不确定弱变量是否与错误相关(我不知道类型变量的状态如何可能是这个消息的原因)。
Are you sure that you are not defining two types polynomial
? That's the question just above the two pointed out by gashe in his answer in the FAQ ("Error message: a type is not compatible with itself").
你确定你没有定义两种类型的多项式吗?这是gashe在FAQ中回答的两个问题(“错误信息:一种类型与自身不兼容”)。
#1
23
_
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.
在OCaml常见问题中,有两种解释与弱多态性有关:通过部分应用程序获得的函数不够多态和下一个。
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.
当您使用非本地引用(其类型不能被一般化)或定义非语法函数的多态函数时,通常会发生这种情况(它们不是以有趣的x ->开头的)。而是一个函数应用程序。在某些情况下,有一个简单的修复(eta扩展,常见问题解答),有时没有,有时你的程序只是不健全的。
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.
一个简单的例子:让a = ref[]没有得到多态的列表ref类型。否则,您可以将二者作为int列表和bool列表使用,并通过更改引用来混合不同类型的元素。它会得到一个'_a列表ref类型。这意味着类型不是多态性的,但仅仅是未知的。一旦你用一个特定的类型做了某件事,它就会一次又一次地修复“_a”。
# 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).
对于OCaml类型检查中实际实现的价值限制和“放松”价值限制的深入解释,请参见Jacques Garrigue(2004)对价值限制文件的放松。
#2
1
There is a weak type variable in the error message from the compiler, but I am not sure that the weak variable is related to the error (I do not see how the status of the type variable could be the cause of this message).
在编译器的错误消息中有一个弱类型变量,但是我不确定弱变量是否与错误相关(我不知道类型变量的状态如何可能是这个消息的原因)。
Are you sure that you are not defining two types polynomial
? That's the question just above the two pointed out by gashe in his answer in the FAQ ("Error message: a type is not compatible with itself").
你确定你没有定义两种类型的多项式吗?这是gashe在FAQ中回答的两个问题(“错误信息:一种类型与自身不兼容”)。