假设我有许多模块,它们都使用一种模块类型进行参数化,并且彼此之间也具有依赖关系:
module type AT = sig
end
module B(A: AT) = struct
module Hash = struct
type t = int
let equal b1 b2 = b1 = b2
let hash b = b
end
end
module C(A: AT) = struct
module B = B(A)
module Hashtbl = Hashtbl.Make(B.Hash)
let make () = Hashtbl.create 16
end
module D(A: AT) = struct
module B = B(A)
module Hashtbl = Hashtbl.Make(B.Hash)
let use ht =
Hashtbl.clear ht
end
module E(A: AT) = struct
module C = C(A)
module D = D(A)
let f () =
D.use (C.make ())
end
在这里,一切都参数化了AT
. Then, C
and D
是独立的,并且E
依赖于取决于C
and D
。这段代码无法编译,因为编译器不相信里面E
, C.Hashtbl
and D.Hashtbl
是相同的模块:
File "xxxx.ml", line xx, characters xx-xx:
Error: This expression has type 'a C.Hashtbl.t = 'a Hashtbl.Make(C.B.Hash).t
but an expression was expected of type
'b D.Hashtbl.t = 'b Hashtbl.Make(D.B.Hash).t
有没有一种快速方法可以让 ocaml 相信两个哈希集模块是相同的?
类型检查器是正确的,这两个Hashtbl
模块不相同,不应混合在一起:例如考虑稍微修改一下B
module:
module B(A: AT) = struct
module Hash = struct
let y = Random.int 10
type t = int
let equal b1 b2 = b1 = b2
let hash b = b + y
end
end
然后两个实例C.B
and D.B
函子应用的F(A)
不要共用同一种盐。因此,混合源自以下的哈希表C.B.Hash
and D.B.Hash
将是一个导致完全不稳定的行为的错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)