有没有办法约束仿函数的参数签名,以便参数可以为结构提供未指定的相等类型?

Sho*_*hon 6 types module equality sml smlnj

如果我尝试编写一个调用参数=提供的未指定类型的参数化模块,SML/NJ会抛出一个类型错误.例如,如果我有签名

signature SIG =
sig
  type t
end
Run Code Online (Sandbox Code Playgroud)

并尝试使用签名在模块F上参数化模块SSIG

functor F (S : SIG) =
struct
  fun f (x:S.t) (y:S.t) = (x = y)
end
Run Code Online (Sandbox Code Playgroud)

我将触发以下编译错误:

Error: operator and operand don't agree [equality type required]
  operator domain: ''Z * ''Z
  operand:         S.t * S.t
  in expression:
    x = y
Run Code Online (Sandbox Code Playgroud)

如何指定S.t应该是相等类型?

到目前为止,我能够找到的唯一解决方法是在参数化仿函数的结构中提供相等函数,即:

signature SIG' =
sig
  type t
  val eq : (''a * ''a) -> bool
end

functor F' (S' : SIG') =
struct
  fun f' x y = S'.eq (x, y)
end


structure A = F'( struct
                    type t = int
                    val eq = (op =)
                  end )
Run Code Online (Sandbox Code Playgroud)

似乎必须有一个更好的方法,虽然我也可能误解了一些基本的和重要的关于仿函数的工作方式.

Ion*_*tan 6

你只需要指定teqtype:

signature SIG =
sig
  eqtype t
end
Run Code Online (Sandbox Code Playgroud)