1
我实现套标ML。目前,它看起来像这样:SML普通型的不同结构
signature SET = sig
type t
type 'a set
...
val map : ('a -> t) -> 'a set -> t set
end
functor ListSetFn (EQ : sig type t val equal : t * t -> bool end)
:> SET where type t = EQ.t = struct
type t = EQ.t
type 'a set = 'a list
...
fun map f = fromList o (List.map f)
end
我想map
功能能够采取任何一组在结构SET
,理想方式甚至不限制于那些从ListSetFn
仿函数。然而,在顶层它只能由单一结构创建集进行操作:一个是从所谓的,如:
functor EqListSetFn(eqtype t) :> SET where type t = t = struct
structure T = ListSetFn(struct type t = t val equal = op= end)
open T
end
structure IntSet = EqListSetFn(type t = int)
IntSet.map : ('a -> IntSet.t) -> 'a IntSet.set -> IntSet.t IntSet.set
虽然我真的很喜欢它是像
IntSet.map : ('a -> IntSet.t) -> 'a ArbitrarySet.set -> IntSet.t IntSet.set
有没有办法做到这一点?我知道它可以在顶级声明,但我想隐藏的内部实现,因此使用不透明签名(S)