Functional programming 不同结构的SML通用类型

Functional programming 不同结构的SML通用类型,functional-programming,sml,functor,signature,ml,Functional Programming,Sml,Functor,Signature,Ml,我正在用标准ML实现集合。目前看起来如下: 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

我正在用标准ML实现集合。目前看起来如下:

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
functor SetMap (structure S1 : SET; structure S2 : SET) =
struct
  fun map f s =
    if S1.isEmpty s then S2.empty else
    let val x = S1.pick s
    in S2.add (f x, map f (S2.remove (x, s)))
    end
end
我希望
map
函数能够获取结构
set
中的任何集合,理想情况下甚至不受
ListSetFn
functor的约束。但是,在顶层,它只能对由单个结构创建的集合进行操作:调用它的结构,例如:

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

有办法吗?我知道它可以在顶层声明,但我想隐藏内部实现,因此使用不透明签名原则上,有两种方法可以执行此类参数化:

  • 将函数包装到它自己的函子中,该函子将另一个结构作为参数

  • 使函数具有多态性,将相关函数作为单个参数或参数记录传递给另一个类型进行操作

  • 假设
    签名包含以下函数:

    val empty : 'a set
    val isEmpty : 'a set -> bool
    val add : 'a * 'a set -> 'a set
    val remove : 'a * 'a set -> 'a set
    val pick : 'a set -> 'a
    
    那么前一种解决方案将如下所示:

    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
    
    functor SetMap (structure S1 : SET; structure S2 : SET) =
    struct
      fun map f s =
        if S1.isEmpty s then S2.empty else
        let val x = S1.pick s
        in S2.add (f x, map f (S2.remove (x, s)))
        end
    end
    
    对于解决方案2,您需要直接传递所有相关功能,例如作为记录:

    fun map {isEmpty, pick, remove} {empty, add} f s =
        let
          fun iter s =
            if isEmpty s then empty else
            let val x = pick s
            in add (f x, iter (remove (x, s)))
            end
        in iter s end
    
    FWIW,使用一流的结构会更好,但SML并没有将它们作为标准特性

    fun map (pack S1 : SET) (pack S2 : SET) f s =
        let
          fun iter s =
            if S1.isEmpty s then S2.empty else
            let val x = S1.pick s
            in S2.add (f x, iter (S2.remove (x, s)))
            end
        in iter s end