我正在构建一个通用容器,用于成对类型见证和值得见证的类型。这个我想用于几种不同的类型,这给了我错误,因为类型都被命名为相同。
所以我试图在这样的仿函数的结果中重命名类型:
module type Witness = sig type 'a key type 'a value end
module type Witnessed = sig
type 'a key
type 'a value
type t
type ('a, 'b) conv = {
key : 'c . 'c key -> 'a;
value : 'c . 'c value -> 'b;
}
val box : 'a key -> 'a value -> t
val conv : ('a, 'b) conv -> t -> ('a * 'b)
end
module MAKE(W : Witness) : Witnessed with type 'a key = 'a W.key
and type 'a value = 'a W.value = struct
include W
type t = Box : 'a key * 'a value -> t
let box k v = Box (k, v)
type ('a, 'b) conv = {
key : 'c . 'c key -> 'a;
value : 'c . 'c value -> 'b;
}
let conv conv (Box (k, v)) = (conv.key k, conv.value v)
end
type _ token
type _ attrib
module W = struct
type 'a key = 'a token
type 'a value = 'a attrib
end
module Boxed = struct
module T = MAKE(W)
type lexeme = T.t
type ('a, 'b) lexeme_conv = ('a, 'b) T.conv
include (T : module type of T with type 'a key := 'a token
and type 'a value := 'a attrib
and type t := lexeme
and type ('a, 'b) conv := ('a, 'b) lexeme_conv)
end
和ocaml说:
File "foo.ml", line 49, characters 38-80:
Error: This variant or record definition does not match that of type
('a, 'b) lexeme_conv
Their kinds differ.
conv和lexeme_conv的类型有何不同?
答案 0 :(得分:4)
您可以通过以下方式替换lexeme_conv的定义来解决问题:
type ('a, 'b) lexeme_conv = ('a, 'b) T.conv = {
key : 'c . 'c T.key -> 'a;
value : 'c . 'c T.value -> 'b;
}
这是因为类型别名和类型定义在破坏性替换方面的行为方式不同。例如,使用lexeme_conv作为别名,签名将更改(因为记录定义将停止公开),这是禁止的。