有没有办法限制仿函数的参数签名,以便参数可以为结构提供未指定的相等类型?
Is there a way of constraining a functor's parameter signature so the parameter can supply unspecified equality types to a structure?
如果我尝试编写一个参数化模块,在参数提供的未指定类型上调用 =
,SML/NJ 会引发类型错误。例如,如果我有签名
signature SIG =
sig
type t
end
并尝试使用签名 SIG
在模块 S
上参数化模块 F
functor F (S : SIG) =
struct
fun f (x:S.t) (y:S.t) = (x = y)
end
我会触发以下编译错误:
Error: operator and operand don't agree [equality type required]
operator domain: ''Z * ''Z
operand: S.t * S.t
in expression:
x = y
如何指定 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 )
似乎必须有更好的方法,尽管我也可能误解了关于函子如何工作的一些基本和重要的东西。
你只需要指定t
是一个eqtype
:
signature SIG =
sig
eqtype t
end
如果我尝试编写一个参数化模块,在参数提供的未指定类型上调用 =
,SML/NJ 会引发类型错误。例如,如果我有签名
signature SIG =
sig
type t
end
并尝试使用签名 SIG
S
上参数化模块 F
functor F (S : SIG) =
struct
fun f (x:S.t) (y:S.t) = (x = y)
end
我会触发以下编译错误:
Error: operator and operand don't agree [equality type required]
operator domain: ''Z * ''Z
operand: S.t * S.t
in expression:
x = y
如何指定 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 )
似乎必须有更好的方法,尽管我也可能误解了关于函子如何工作的一些基本和重要的东西。
你只需要指定t
是一个eqtype
:
signature SIG =
sig
eqtype t
end