モジュールの定義

モジュール(正確にはストラクチャ)の定義は次のようにして,struct と endo のあいだに各種定義の書く。

 # module Tree =
     struct
       type 'a t = Lf | Br of 'a * 'a t * 'a t
   
       let rec size = function
           Lf -> 0
         | Br (_, left, right) -> 1 + size left + size right
   
       let rec depth = function
           Lf -> 0
         | Br (_, left, right) -> 1 + max (depth left) (depth right)
     end
     ;;

モジュールの中に書けるのは:

  • 変数束縛
  • 関数宣言
  • type による型宣言
  • exception宣言
  • open宣言
  • module定義(入れ子)


さて,対話環境で上のようにモジュール定義をすると次のような応答が返ってくる。

 module Tree :
   sig
     type 'a t = Lf | Br of 'a * 'a t * 'a t
     val size : 'a t -> int
     val depth : 'a t -> int
   end

これはシグネチャといって,いってみればモジュールの型というべきもの。これについてはまた後で書く。