Sam*_*udo 13 ocaml haskell functional-programming caml functor
众所周知,OCaml具有参数多态性,这导致一些限制.Haskell通过其类型类提供了一种特殊的多态性,在很多情况下显然非常方便.众所周知,OCaml的模块和仿函数系统允许创建一种特殊的多态性.看人妖的伟大最近的答案闪耀这里的实例.
我的观点是,在Haskell中可以创建派生多个类型类的类型.例如 :
data Person = Person { firstName :: String
, lastName :: String
, age :: Int
} deriving (Eq, Show, Read)
Run Code Online (Sandbox Code Playgroud)
定义具有多个功能的类型非常方便(允许类型的值Person支持相等性测试,可打印,并且在给定示例中可读).
我的问题如下:我们可以在OCaml中做同样的事吗?通过简单的我的意思是与语言的地面语法和没有太多的计谋.
举一个具体的例子,假设我们有两个OCaml签名
module type Showable = sig
type t
val to_string : t -> string
end
module type Readable = sig
type t
val from_string : string -> t
end
Run Code Online (Sandbox Code Playgroud)
的目的是写入一个算符F由它实现两者模块参数化Showable和Readable.
Dru*_*rup 12
当然,这实际上非常简单,使用模块包含.
module type S = sig
include Showable
include Readable with type t := t (* destructive substitution *)
end
module F ( M : S ) = struct
let fake_id x = M.from_string @@ M.to_string x
end
Run Code Online (Sandbox Code Playgroud)
手册中解释了破坏性替换:http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec234
其余的是常规模块内容(有关完整说明,请参阅手册)
一些算函重型库非常依赖于这种结构子类型.例如,ocamlgraph中的每个仿函数都定义了自己的模块类型参数.以下是Kruskal模块的示例.Kruskal.G仿函数期望一个类型的模块,它实现了一个子签名Sig.G(由大多数图形模块实现).