module type SET =
sig
type 'a set
val emptyset : 'a set
val union : 'a set -> 'a set -> 'a set
end
module Set : SET =
struct
type 'a set = 'a -> bool
let emptyset : 'a set = fun _ -> false
let union (s1 : 'a set) (s2 : 'a set) : 'a set =
fun x -> s1 x || s2 x
end
我如何在其中调用 union 函数,有人能给我举个例子吗?因为我刚刚在 OCaml 语言的学习过程中,无法理解模块内部的函数调用。
你的设计有问题。类型
Set.t
是 abstract,因为它受到 SET
签名的约束。您可以合并两个空集,但永远无法创建任何其他集。
实际的函数调用非常简单。您只需指定您正在从
Set
模块访问值。
Set union Set.emptyset Set.emptyset
或者:
Set.(union emptyset emptyset)