【发布时间】:2015-01-14 14:39:04
【问题描述】:
我最近在标准 ML 中使用幺半群。签名好写:
signature MONOID =
sig
type monoid
val neutral : monoid
val combine : monoid -> monoid -> monoid
end
简单的幺半群也是如此,例如整数加法幺半群:
structure IntSumMonoid : MONOID =
struct
type monoid = int
val neutral = 0
fun combine a b = a + b
end
但我一直在为更高种类的类型定义一个幺半群,例如list。当然,这不会编译:
structure ListMonoid : MONOID =
struct
type monoid = 'a list
val neutral = []
fun combine a b = a @ b
end
经过一番搜索,我找到了以下基于仿函数的解决方案:
functor ListMonoid (type m) : MONOID =
struct
type monoid = m list
val neutral = []
fun combine a b = a @ b
end
我的问题是是否存在声明通用列表幺半群的替代方法,理想情况下不需要functor 声明。在尝试声明一个列表幺半群时,显然没有必要知道列表的包含类型。
【问题讨论】: