- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下 OCaml 代码:
type mytype = My : 'a list * 'a -> mytype
let rec foo : int -> mytype =
fun n -> if n < 0 then My([], 2)
else let My(xs, y) = foo (n - 1)
in My(3::xs, y)
OCaml 解释器在 foo
的最后一行给我一个错误,说:
This expression has type a#1 list but an expression was expected of type int list
Type a#1 is not compatible with type int
我可以通过向 mytype
添加一个类型参数来使该代码正常工作,这样就可以了
type _ mytype = My : 'a list * 'a -> 'a mytype
let rec foo : int -> 'a mytype =
...
但假设我真的不想更改 mytype
的定义。假设我想保留该函数的(通过非工作代码直观地理解)行为,我可以编写 foo
吗?
另外,有人可以解释问题的根源是什么,即为什么初始代码不进行类型检查?
最佳答案
当对 mytype
值进行模式匹配时,无法知道其中的类型。问题是,打字系统的行为非常简单,并且不会尝试知道 mytype 来自哪里,即使它来自递归调用(打字系统只是不那样工作)。
问题是,在这种情况下,您知道 'a
确实是 int
,但您需要向编译器提供证明。
在这种特定情况下,您不需要这样做。您只需要在函数末尾使用 GADT:
let foo n =
let rec aux n =
if n < 0 then ([], 2)
else let (xs, y) = aux (n - 1)
in (3::xs, y)
in
let (xs,y) = aux n in My (xs,y)
值得注意的是,通过该类型定义,您无法使用您知道 mytype
中存在整数值的事实,因此它将非常无法使用。 GADT 应该仅在特定情况下使用,并且您应该准确地知道为什么以及如何使用它们。
编辑:
类型可以被视为附加到每个值的逻辑公式。在大多数情况下,它非常简单,您不必担心,主要是因为类型变量('a
'b
等)是普遍量化并且对于类型的外部始终可见。
type 'a mylist = Cons of 'a * 'a list | Nil
(* should be read as:
for all 'a,
'a mylist is either
* a cons containing the same 'a and 'a list
* nil *)
type mylist = Cons : 'a * mylist -> mylist | Nil : mylist
(* should be read as:
mylist is either
* for some 'a, a 'a and another list
* nil *)
在上面的 GADT 中,您可以看到没有任何内容表明列表中的每个元素都具有相同类型。事实上,如果你得到一个 mylist
,你无法知道里面有什么元素。
所以,您需要证明。这样做的一个好方法是在 gadt 中存储类型证明:
type _ proof =
| Int : int proof
| Float : float proof
| Tuple : 'a proof * 'b proof -> ('a * 'b) proof
(* You can add other constructors depending on
the types you want to store *)
type mytype = My : 'a proof * 'a list * 'a -> mytype
现在有了这个,当打开 mytype
时,您可以匹配证明来证明 'a 的值。编译器会知道它是相同的,因为如果没有与正确类型相对应的证明,它会拒绝创建 mytype。
正如您所看到的,GADT 并不简单,您通常需要在实现之前起草多个草案。大多数时候,您可以避免使用它们(如果您不确定它们是如何工作的,则根本不要使用它们)。
关于types - 存在量化类型参数、递归函数和类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29637591/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!