シグネチャはストラクチャのインタフェースです。 シグネチャはストラクチャの要素が外部からアクセス可能か、そしてどのような型としてアクセス可能かを定めます。 ストラクチャの要素を隠したり(ローカル関数定義)、 型に制限をした上で要素を外部からアクセスできるようにしたりするのに使えます。
以下に示すストラクチャは優先度付きキューの 3 つの処理 empty、 insert、extract を規定しますが、補助関数 remove_top は規定しません。
同様に、(具象型としての実際の表現を与えないことで) queue 型を抽象化しています。
#module type PRIOQUEUE = sig type priority = int (* still concrete *) type 'a queue (* now abstract *) val empty : 'a queue val insert : 'a queue -> int -> 'a -> 'a queue val extract : 'a queue -> int * 'a * 'a queue exception Queue_is_empty end;;module type PRIOQUEUE = sig type priority = int type 'a queue val empty : 'a queue val insert : 'a queue -> int -> 'a -> 'a queue val extract : 'a queue -> int * 'a * 'a queue exception Queue_is_empty end
以下の例では上で定義したシグネチャでストラクチャ PrioQueue を制限することで、関数 remove_top にはアクセスできず、優先度付きキューの実際の構造が隠された、新しい PrioQueue のインターフェースができます。
#module AbstractPrioQueue = (PrioQueue : PRIOQUEUE);;module AbstractPrioQueue : PRIOQUEUE#AbstractPrioQueue.remove_top;;Unbound value AbstractPrioQueue.remove_top#AbstractPrioQueue.insert AbstractPrioQueue.empty 1 "hello";;- : string AbstractPrioQueue.queue = <abstr>
こうしたシグネチャによる制限は、ストラクチャの定義と同時に行うことも出来ます。
module PrioQueue = (struct ... end : PRIOQUEUE);;
上記と同様のことを行う別の構文として、以下のような構文も用意されています。
module PrioQueue : PRIOQUEUE = struct ... end;;