ファンクタについて。
OCaml のモジュールシステムは強力ですよという話。
関数が値を受け取って値を返すように、
functor は structure を受け取って structure を返す。
値 | 型 | 関数 |
---|---|---|
let x : t = 〜 | type t = 〜 | let f (x : t) : t = 〜 |
structure | signature | functor |
module M : S = struct 〜 end | module type S = sig 〜 end | module F (M : S) : S = struct 〜 end |
このように、値 - 型 - 関数 と同じ関係が、structure - signature - functor 間にあって、
structureは値に、signatureは型に、functor は関数にそれぞれ対応する。
(functorの引数のsignatureは省略できないことに注意)
sig 〜 end はインターフェース、 struct 〜 end はその実装という感じで、それぞれ、〜.mli と 〜.ml の中に書く。
sig type t1 type t2 : type tm val x1 : t1 val x2 : t2 : val xn : tn end struct type t1 = 〜 type t2 = 〜 : type tm = 〜 let x1 = 〜 let x2 = 〜 : let xn = 〜 end