a和l的區別是什么?

[英]What is the difference between 'a and '_l?


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中回答的兩個問題(“錯誤信息:一種類型與自身不兼容”)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/11/22/b632d59d6e48c59038046b81d78e1be0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com