【发布时间】:2013-05-24 15:36:31
【问题描述】:
module MapHelpers (Ord : Map.OrderedType) = struct
include Map.Make (Ord)
let add_all a b = fold add a b
end
有效但看似等效
module MapHelpers (Ord : Map.OrderedType) = struct
include Map.Make (Ord)
let add_all = fold add
end
编译失败
File "Foo.ml", line 2, characters 18-104:
Error: The type of this module,
functor (Ord : Map.OrderedType) ->
sig
...
val add_all : '_a t -> '_a t -> '_a t
end,
contains type variables that cannot be generalized
Command exited with code 2.
并添加显式类型注释
: 'a . 'a t -> 'a t -> 'a t
导致编译提前失败
Error: This definition has type 'a t -> 'a t -> 'a t
which is less general than 'a0. 'a0 t -> 'a0 t -> 'a0 t
为什么添加显式形式 a b 会改变这两个模块的类型?
【问题讨论】:
-
您可能想阅读stackoverflow.com/questions/4242677/…。 Caml曾经实现“值限制”,现在实现“宽松的值限制”,我认为:math.nagoya-u.ac.jp/~garrigue/papers/morepoly-long.pdf
-
@PascalCuoq,我看到“或者在定义不是语法函数的多态函数时”
标签: ocaml monomorphism