トップ
新規
単語検索
ヘルプ
OCamlテクニック/module
をテンプレートにして作成
開始行:
[[OCamlテクニック/module]]
*モジュールシステムとオブジェクト指向機能 [#effea7af]
Objective Caml にはプログラムを構造化する仕組みが2種類用...
モジュールシステムとオブジェクト指向機能である。それぞれ...
方を背景としているので、それらを理解し、適材適所を心がけ...
*モジュールシステム [#g99bf7ce]
**モジュール [#ib108db2]
モジュールは、module キーワードの後に大文字から始まるモジ...
# module Counter = struct
type t = int
let make () = 0
let inc c = c + 1
let dec c = c - 1
end;;
module Counter :
sig
type t = int
val make : unit -> int
val inc : int -> int
val dec : int -> int
end
#
最初の例では、型 Counter.t とその型の値を生成・操作する関...
この例のように、モジュールは型宣言や複数の式を一つのまと...
Counter モジュールを使用するには、Counter の後に . を付け...
# let c = Counter.make ();;
val c : int = 0
# let next = Counter.inc c;;
val next : int = 1
#
モジュールは open する事により、そのモジュール名を指定す...
# open Counter;;
# make ();;
- : int = 0
#
ただし、異なるモジュールに同じ関数名がある場合、その両方...
# open Array;;
# make ();;
This expression has type unit but is here used with type...
#
また、モジュールの中にモジュールを定義することも出来る。...
# module Todo = struct
module Queue = struct
type 'a t = (int * 'a) list
let make () = []
let add q v = v :: q
end
type 'a t = 'a Queue.t
let add todo priori work = Queue.add todo (priori, wo...
end;;
module Todo :
sig
module Queue :
sig
type 'a t = (int * 'a) list
val make : unit -> 'a list
val add : 'a list -> 'a -> 'a list
end
type 'a t = 'a Queue.t
val add : ('a * 'b) list -> 'a -> 'b -> ('a * 'b) list
end
#
*シグネチャー [#e70cad11]
最初の Counter モジュールを定義した時、
module Counter :
sig
type t = int
val make : unit -> int
val inc : int -> int
val dec : int -> int
end
という応答があった。これはモジュールのシグネチャー(モジュ...
# module type SigCounter = sig
type t
val make : unit -> t
val inc : t -> t
val dec : t -> t
end;;
module type SigCounter = sig type t val make : unit -> t...
#
このように宣言したシグネチャーは、モジュール定義の際に適...
# module Counter' : SigCounter = Counter;;
module Counter' : SigCounter
Counter' モジュールの型が SigCounter になっていることが分...
シグネチャーには、情報隠蔽に関する非常に重要な以下の二つ...
- 型宣言の抽象化
- 式の隠蔽
最初の特徴は型宣言の詳細を外部に公開しないことを可能にし...
# Counter.inc 2;;
- : int = 3
#
のような実行が可能である。本来 Counter.t の操作を意図した...
一方で、SigCounter を適用した Counter' モジュールでは型 t...
# Counter'.inc 2;;
This expression has type int but is here used with type ...
#
もう一つの特徴は、公開する式の限定である。もし値の増加の...
# module type SigIncCounter = sig
type t
val make : unit -> t
val inc : t -> t
end;;
module type SigIncCounter =
sig type t val make : unit -> t val inc : t -> t end
# module IncCounter : SigIncCounter = Counter;;
module IncCounter : SigIncCounter
# let id = IncCounter.make ();;
val id : IncCounter.t = <abstr>
# IncCounter.dec id;;
Unbound value IncCounter.dec
#
Counter.dec 関数は外部から参照できないため、どうやっても ...
**ファンクター [#ud2e61d6]
Counter モジュールを使って受注を生成するモジュールを作っ...
module Order = struct
type t = { id : IncCounter.t; name : string }
let new_order last_order_id name =
{ id = IncCounter.inc last_order_id; name = name }
end;;
module Order :
sig
type t = { id : IncCounter.t; name : string; }
val new_order : IncCounter.t -> string -> t
end
#
Order モジュールは受注番号と商品名を持っており、受注番号...
これはこれで何の問題もないのだが、番号を - で区切る必要が...
このような困難は、IncCounter モジュールが「順に増えていく...
そこで、IncCounter を直接使用するのでは無く、SigIncCounte...
module OrderMaker (C : SigIncCounter) = struct
type t = { id : C.t; name : string }
let new_order last_order_id name =
{ id = C.inc last_order_id; name = name }
end;;
module OrderMaker :
functor (C : SigIncCounter) ->
sig
type t = { id : C.t; name : string; }
val new_order : C.t -> string -> t
end
このようなパラメータ付きのモジュールはファンクターと呼ば...
このファンクターに IncCounter を適用すれば、前述の Order ...
# module Order' = OrderMaker(IncCounter);;
module Order' :
sig
type t = OrderMaker(IncCounter).t = { id : IncCounte...
val new_order : IncCounter.t -> string -> t
end
#
また、もし 1000 を法とする受注番号を生成したいなら、以下...
# module TCounter : SigIncCounter = struct
type t = int
let make () = 0
let inc c = (c + 1) mod 1000
end;;
module TCounter : SigIncCounter
# module TOrder = OrderMaker(TCounter);;
module TOrder :
sig
type t = OrderMaker(TCounter).t = { id : TCounter.t;...
val new_order : TCounter.t -> string -> t
end
#
**Exercise [#v6fef488]
Exercise ? 相互依存したモジュールは、再帰的なモジュールに...
きる。例を挙げよ。
Exercise ? OrderMaker の型を module type によって定義せよ。
Exercise ? ファンクターのパラメータにはファンクターを取る...
例を挙げよ。
Exercise ? 通常はシグネチャーによる型の隠蔽は、隠蔽するか...
ちらかになってしまうが、private row によってコンストラク...
開するといった事が可能になる。例を挙げよ。
- ちょっときれいにしました。 -- [[けいご]] &new{2006-08-0...
終了行:
[[OCamlテクニック/module]]
*モジュールシステムとオブジェクト指向機能 [#effea7af]
Objective Caml にはプログラムを構造化する仕組みが2種類用...
モジュールシステムとオブジェクト指向機能である。それぞれ...
方を背景としているので、それらを理解し、適材適所を心がけ...
*モジュールシステム [#g99bf7ce]
**モジュール [#ib108db2]
モジュールは、module キーワードの後に大文字から始まるモジ...
# module Counter = struct
type t = int
let make () = 0
let inc c = c + 1
let dec c = c - 1
end;;
module Counter :
sig
type t = int
val make : unit -> int
val inc : int -> int
val dec : int -> int
end
#
最初の例では、型 Counter.t とその型の値を生成・操作する関...
この例のように、モジュールは型宣言や複数の式を一つのまと...
Counter モジュールを使用するには、Counter の後に . を付け...
# let c = Counter.make ();;
val c : int = 0
# let next = Counter.inc c;;
val next : int = 1
#
モジュールは open する事により、そのモジュール名を指定す...
# open Counter;;
# make ();;
- : int = 0
#
ただし、異なるモジュールに同じ関数名がある場合、その両方...
# open Array;;
# make ();;
This expression has type unit but is here used with type...
#
また、モジュールの中にモジュールを定義することも出来る。...
# module Todo = struct
module Queue = struct
type 'a t = (int * 'a) list
let make () = []
let add q v = v :: q
end
type 'a t = 'a Queue.t
let add todo priori work = Queue.add todo (priori, wo...
end;;
module Todo :
sig
module Queue :
sig
type 'a t = (int * 'a) list
val make : unit -> 'a list
val add : 'a list -> 'a -> 'a list
end
type 'a t = 'a Queue.t
val add : ('a * 'b) list -> 'a -> 'b -> ('a * 'b) list
end
#
*シグネチャー [#e70cad11]
最初の Counter モジュールを定義した時、
module Counter :
sig
type t = int
val make : unit -> int
val inc : int -> int
val dec : int -> int
end
という応答があった。これはモジュールのシグネチャー(モジュ...
# module type SigCounter = sig
type t
val make : unit -> t
val inc : t -> t
val dec : t -> t
end;;
module type SigCounter = sig type t val make : unit -> t...
#
このように宣言したシグネチャーは、モジュール定義の際に適...
# module Counter' : SigCounter = Counter;;
module Counter' : SigCounter
Counter' モジュールの型が SigCounter になっていることが分...
シグネチャーには、情報隠蔽に関する非常に重要な以下の二つ...
- 型宣言の抽象化
- 式の隠蔽
最初の特徴は型宣言の詳細を外部に公開しないことを可能にし...
# Counter.inc 2;;
- : int = 3
#
のような実行が可能である。本来 Counter.t の操作を意図した...
一方で、SigCounter を適用した Counter' モジュールでは型 t...
# Counter'.inc 2;;
This expression has type int but is here used with type ...
#
もう一つの特徴は、公開する式の限定である。もし値の増加の...
# module type SigIncCounter = sig
type t
val make : unit -> t
val inc : t -> t
end;;
module type SigIncCounter =
sig type t val make : unit -> t val inc : t -> t end
# module IncCounter : SigIncCounter = Counter;;
module IncCounter : SigIncCounter
# let id = IncCounter.make ();;
val id : IncCounter.t = <abstr>
# IncCounter.dec id;;
Unbound value IncCounter.dec
#
Counter.dec 関数は外部から参照できないため、どうやっても ...
**ファンクター [#ud2e61d6]
Counter モジュールを使って受注を生成するモジュールを作っ...
module Order = struct
type t = { id : IncCounter.t; name : string }
let new_order last_order_id name =
{ id = IncCounter.inc last_order_id; name = name }
end;;
module Order :
sig
type t = { id : IncCounter.t; name : string; }
val new_order : IncCounter.t -> string -> t
end
#
Order モジュールは受注番号と商品名を持っており、受注番号...
これはこれで何の問題もないのだが、番号を - で区切る必要が...
このような困難は、IncCounter モジュールが「順に増えていく...
そこで、IncCounter を直接使用するのでは無く、SigIncCounte...
module OrderMaker (C : SigIncCounter) = struct
type t = { id : C.t; name : string }
let new_order last_order_id name =
{ id = C.inc last_order_id; name = name }
end;;
module OrderMaker :
functor (C : SigIncCounter) ->
sig
type t = { id : C.t; name : string; }
val new_order : C.t -> string -> t
end
このようなパラメータ付きのモジュールはファンクターと呼ば...
このファンクターに IncCounter を適用すれば、前述の Order ...
# module Order' = OrderMaker(IncCounter);;
module Order' :
sig
type t = OrderMaker(IncCounter).t = { id : IncCounte...
val new_order : IncCounter.t -> string -> t
end
#
また、もし 1000 を法とする受注番号を生成したいなら、以下...
# module TCounter : SigIncCounter = struct
type t = int
let make () = 0
let inc c = (c + 1) mod 1000
end;;
module TCounter : SigIncCounter
# module TOrder = OrderMaker(TCounter);;
module TOrder :
sig
type t = OrderMaker(TCounter).t = { id : TCounter.t;...
val new_order : TCounter.t -> string -> t
end
#
**Exercise [#v6fef488]
Exercise ? 相互依存したモジュールは、再帰的なモジュールに...
きる。例を挙げよ。
Exercise ? OrderMaker の型を module type によって定義せよ。
Exercise ? ファンクターのパラメータにはファンクターを取る...
例を挙げよ。
Exercise ? 通常はシグネチャーによる型の隠蔽は、隠蔽するか...
ちらかになってしまうが、private row によってコンストラク...
開するといった事が可能になる。例を挙げよ。
- ちょっときれいにしました。 -- [[けいご]] &new{2006-08-0...
ページ名: