[[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 モジュールを使用するには、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 してしまうと、混乱の元なので注意。 # open Array;; # make ();; This expression has type unit but is here used with type int # また、モジュールの中にモジュールを定義することも出来る。例えば、TODO管理モジュール内部でキュー構造を利用したい場合、下のように定義できる。 # 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, work) 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 の後に大文字から始まるシグネチャー名を付けて宣言する。 # 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 val inc : t -> t val dec : t -> t end # このように宣言したシグネチャーは、モジュール定義の際に適用できる。 # module Counter' : SigCounter = Counter;; module Counter' : SigCounter Counter' モジュールの型が SigCounter になっていることが分かる。 シグネチャーには、情報隠蔽に関する非常に重要な以下の二つの特徴がある。 - 型宣言の抽象化 - 式の隠蔽 最初の特徴は型宣言の詳細を外部に公開しないことを可能にしている。例えば、シグネチャーを指定していない Counter モジュールは Counter.t が実は int 型である事がモジュール外部に公開されているため、 # Counter.inc 2;; - : int = 3 # のような実行が可能である。本来 Counter.t の操作を意図した inc 関数が任意の int を対象に実行可能になってしまっては、せっかくの型チェック機能が台無しである。 一方で、SigCounter を適用した Counter' モジュールでは型 t の詳細は隠蔽されているため、Counter'.t 以外の演算は出来ない。 # Counter'.inc 2;; This expression has type int but is here used with type Counter'.t # もう一つの特徴は、公開する式の限定である。もし値の増加のみ外部に公開するカウンターを作りたい場合、SigIncCounter を次のように定義する。 # 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 関数は外部から参照できないため、どうやっても IncCounterの値を減少させることは出来ない。 **ファンクター [#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 を使用している。 これはこれで何の問題もないのだが、番号を - で区切る必要が出きたり、1000 を法とする必要が出てきた場合、IncCounter モジュールを別のモジュールに書き換えなければならない。 このような困難は、IncCounter モジュールが「順に増えていく何か」という性質以上の実装を持ってしまっているために発生している。 そこで、IncCounter を直接使用するのでは無く、SigIncCounter というシグネチャーを持つモジュールを輸入して Order モジュールを作成すると良い。 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 このようなパラメータ付きのモジュールはファンクターと呼ばれ、他の言語には無い Objective Caml の一つ特徴である。 このファンクターに IncCounter を適用すれば、前述の Order と同じモジュールを定義できる。 # module Order' = OrderMaker(IncCounter);; module Order' : sig type t = OrderMaker(IncCounter).t = { id : IncCounter.t; name : string; } 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; name : string; } val new_order : TCounter.t -> string -> t end # **Exercise [#v6fef488] Exercise ? 相互依存したモジュールは、再帰的なモジュールによって定義で きる。例を挙げよ。 Exercise ? OrderMaker の型を module type によって定義せよ。 Exercise ? ファンクターのパラメータにはファンクターを取ることも出来る。 例を挙げよ。 Exercise ? 通常はシグネチャーによる型の隠蔽は、隠蔽するかしないかのど ちらかになってしまうが、private row によってコンストラクタの一部のみ公 開するといった事が可能になる。例を挙げよ。 - ちょっときれいにしました。 -- [[けいご]] &new{2006-08-04 (金) 02:44:28}; - magic story very thanks <a href=" http://blogs.ign.com/MarkPatroy/2008/05/09/89170/ "><b>ring tones verizon wireless phones</b></a> 671436 -- [[Dkyemscr]] &new{2008-05-11 (日) 14:30:02}; - good material thanks <a href=" http://groups.google.us/group/u-hotsexmovies ">porn star sierra</a> 195137 -- [[Njdkvkpp]] &new{2008-05-11 (日) 15:41:38}; - Cool site goodluck :) <a href=" http://blogs.ign.com/DavidScrotch/2008/05/09/89171/ "><b>stephen colbert ringtones</b></a> 1520 -- [[Upfvplsg]] &new{2008-05-11 (日) 16:09:37}; - very best job <a href=" http://groups.google.us/group/ff-hotsexmovies ">never seen porn</a> gvwfen -- [[Viinkdpi]] &new{2008-05-11 (日) 17:15:57}; - I love this site <a href=" http://blogs.ign.com/DavidScrotch/2008/05/09/89172/ "><b>motorola 120t monophonic ringtones</b></a> 633775 -- [[Jtqoeecv]] &new{2008-05-11 (日) 17:47:35}; - Very interesting tale <a href=" http://groups.google.us/group/XL-hotsexmovies ">xxx z black porn</a> 65420 -- [[Uqdaffjf]] &new{2008-05-11 (日) 18:51:22}; - Excellent work, Nice Design <a href=" http://blogs.ign.com/DavidScrotch/2008/05/09/89173/ "><b>no hassle free ti ringtones</b></a> 8)) -- [[Bjagqmgh]] &new{2008-05-11 (日) 19:26:04}; - very best job <a href=" http://groups.google.us/group/XXL-hotsexmovies ">jap extreme hardcorn porn</a> 3059 -- [[Tyuxrnwu]] &new{2008-05-11 (日) 20:25:22}; - Gloomy tales <a href=" http://blogs.ign.com/DavidScrotch/2008/05/09/89174/ "><b>ringtones yahoo</b></a> 840751 -- [[Yzyemsij]] &new{2008-05-11 (日) 21:01:13}; - Cool site goodluck :) <a href=" http://groups.google.us/group/bb-hotsexmovies ">freeporn directory</a> 3431 -- [[Brgmmoku]] &new{2008-05-11 (日) 21:59:27}; - I'm happy very good site <a href=" http://blogs.ign.com/DavidScrotch/2008/05/09/89175/ "><b>mobile phones full length ringtones</b></a> 0332 -- [[Qvwjptug]] &new{2008-05-11 (日) 22:43:37}; - Thanks funny site <a href=" http://groups.google.us/group/dd-hotsexmovies ">tom chase escort porn</a> %O -- [[Bfrlidne]] &new{2008-05-11 (日) 23:37:51}; - I love this site <a href=" http://blogs.ign.com/SindyClaim/2008/05/09/89176/ "><b>kanye west stronger ringtones</b></a> gnfb -- [[Mzbkonzq]] &new{2008-05-12 (月) 00:24:50}; - I love this site <a href=" http://blogs.ign.com/SindyClaim/2008/05/09/89176/ "><b>kanye west stronger ringtones</b></a> gnfb -- [[Mzbkonzq]] &new{2008-05-12 (月) 00:25:24}; - Wonderfull great site <a href=" http://groups.google.us/group/tt-hotsexmovies ">free tranny porn thumbs</a> elz -- [[Xdbljxtt]] &new{2008-05-12 (月) 01:14:54}; - Thanks funny site <a href=" http://blogs.ign.com/SindyClaim/2008/05/09/89177/ "><b>free ringtones of patriotic songs</b></a> 14548 -- [[Attabdgk]] &new{2008-05-12 (月) 02:00:23}; - Excellent work, Nice Design <a href=" http://groups.google.us/group/ll-hotsexmovies ">golden booty porn</a> ecsp -- [[Xrkvebui]] &new{2008-05-12 (月) 02:44:07}; - Jonny was here <a href=" http://blogs.ign.com/SindyClaim/2008/05/09/89179/ "><b>startrack ringtones</b></a> 5811 -- [[Sawtrqva]] &new{2008-05-12 (月) 03:34:48}; - It's serious <a href=" http://groups.google.us/group/II-hotsexmovies ">free teens gone wild porn</a> =-]]] -- [[Kguekgam]] &new{2008-05-12 (月) 04:14:36}; - very best job <a href=" http://blogs.ign.com/SindyClaim/2008/05/09/89180/ "><b>kyocera phanton ringtones</b></a> cmq -- [[Jsjamamt]] &new{2008-05-12 (月) 05:09:43}; - Hello good day <a href=" http://groups.google.us/group/o-hotsexmovies ">dark skin porno pics</a> smbk -- [[Daapvsdt]] &new{2008-05-12 (月) 05:46:57}; - Good crew it's cool :) <a href=" http://blogs.ign.com/SindyClaim/2008/05/09/89181/ "><b>comedy central ringtones</b></a> yczlpv -- [[Zigxbgde]] &new{2008-05-12 (月) 06:42:08}; - Jonny was here <a href=" http://groups.google.us/group/69-hotsexmovies ">freen animal porn movies</a> aelhsy -- [[Avqskpyy]] &new{2008-05-12 (月) 07:15:39}; - this post is fantastic <a href=" http://blogs.ign.com/MatTaimans/2008/05/09/89182/ "><b>ring tones customized</b></a> erp -- [[Vnkqznlg]] &new{2008-05-12 (月) 08:14:56}; - Excellent work, Nice Design <a href=" http://groups.google.us/group/1000-hotsexmovies ">montana pornstar</a> 00510 -- [[Spgkvjsq]] &new{2008-05-12 (月) 08:45:43}; - I love this site <a href=" http://blogs.ign.com/MatTaimans/2008/05/09/89183/ "><b>crimson tide ring tones</b></a> =-((( -- [[Brhgbaks]] &new{2008-05-12 (月) 09:52:42}; - Thanks funny site <a href=" http://groups.google.us/group/9999-hotsexmovies ">zooporn</a> 8-))) -- [[Vjuxkldv]] &new{2008-05-12 (月) 10:21:52}; - Wonderfull great site <a href=" http://blogs.ign.com/MatTaimans/2008/05/09/89185/ "><b>orotones printing</b></a> %]] -- [[Oruvtgyq]] &new{2008-05-12 (月) 11:34:35}; - Cool site goodluck :) <a href=" http://groups.google.us/group/vip-hotsexmovies ">fatwomanporn</a> 48047 -- [[Zemhkskf]] &new{2008-05-12 (月) 12:01:18}; - Very interesting tale <a href=" http://blogs.ign.com/MatTaimans/2008/05/09/89187/ "><b>brian doerksen free ringtones</b></a> 288 -- [[Yefwffni]] &new{2008-05-12 (月) 13:16:45}; - Very funny pictures <a href=" http://groups.google.us/group/777-hotsexmovies ">free 65 old porn</a> tmumbl -- [[Hpifbszu]] &new{2008-05-12 (月) 13:40:03}; - It's serious <a href=" http://blogs.ign.com/MatTaimans/2008/05/09/89188</b> "><b>tyrese ringtones</b></a> %( -- [[Wtceyoce]] &new{2008-05-12 (月) 14:57:08}; - good material thanks <a href=" http://groups.google.us/group/rr-hotsexmovies ">amature vids of porn</a> 811 -- [[Dtifaesf]] &new{2008-05-12 (月) 15:23:10}; - very best job <a href=" http://blogs.ign.com/GenryWalsmy/2008/05/09/89191/ "><b>free jurassic park ringtones</b></a> =[[[ -- [[Skxhvehh]] &new{2008-05-12 (月) 16:41:49}; - I love this site <a href=" http://groups.google.us/group/10K-hotsexmovies ">pornografic muvies pics</a> 897106 -- [[Xkdqnnhi]] &new{2008-05-12 (月) 17:05:09}; - very best job <a href=" http://blogs.ign.com/GenryWalsmy/2008/05/09/89192/ "><b>verizon txt ring tones free</b></a> 4021 -- [[Rehrqlmy]] &new{2008-05-12 (月) 18:28:22}; - Punk not dead <a href=" http://groups.google.us/group/as-hotsexmovies ">free rape porn movies</a> wvujd -- [[Ychbeuyq]] &new{2008-05-12 (月) 18:52:27}; - Best Site good looking <a href=" http://blogs.ign.com/GenryWalsmy/2008/05/09/89194/ "><b>abnormal ketones</b></a> 1577 -- [[Kdcauoqn]] &new{2008-05-12 (月) 20:13:27}; - <a href=" http://groups.google.us/group/ss-hotsexmovies ">easy gals porn</a> 039 -- [[Txvotcwr]] &new{2008-05-12 (月) 20:38:42}; - Hello good day <a href=" http://blogs.ign.com/GenryWalsmy/2008/05/09/89196/ "><b>mexpys cellular free ringtones</b></a> reicc -- [[Uypvaywk]] &new{2008-05-12 (月) 22:00:12}; - Thanks funny site <a href=" http://groups.google.us/group/100K-hotsexmovies ">latino porno pics</a> 8136 -- [[Ablclnht]] &new{2008-05-12 (月) 22:24:55}; - Very Good Site <a href=" http://blogs.ign.com/GenryWalsmy/2008/05/09/89197/ "><b>free realtones for verizon</b></a> eisf -- [[Eruxdysj]] &new{2008-05-12 (月) 23:47:47}; - Excellent work, Nice Design <a href=" http://groups.google.us/group/k-hotsexmovies ">male porn .</a> drfjwi -- [[Odpqwmfe]] &new{2008-05-13 (火) 00:13:40}; - I love this site <a href=" http://blogs.ign.com/FrenkBently/2008/05/09/89198/ "><b>vzw ringtones</b></a> 8139 -- [[Sshxjmre]] &new{2008-05-13 (火) 01:35:19}; - I love this site <a href=" http://groups.google.us/group/big-hotsexmovies ">sexporn</a> =P -- [[Wiwjwpks]] &new{2008-05-13 (火) 01:57:30}; - Very funny pictures <a href=" http://blogs.ign.com/FrenkBently/2008/05/09/89199/ "><b>copying treo 755 ringtones</b></a> iycflg -- [[Kbymdgfo]] &new{2008-05-13 (火) 03:19:22}; - It's serious <a href=" http://groups.google.us/group/free-hotsexmovies ">porno webcam sites</a> 8(( -- [[Metcmmoh]] &new{2008-05-13 (火) 03:40:37}; - Gloomy tales <a href=" http://blogs.ign.com/FrenkBently/2008/05/09/89200/ "><b>free rigntones</b></a> 985720 -- [[Sysfgbyf]] &new{2008-05-13 (火) 04:58:51}; - Gloomy tales <a href=" http://blogs.ign.com/FrenkBently/2008/05/09/89200/ "><b>free rigntones</b></a> 985720 -- [[Sysfgbyf]] &new{2008-05-13 (火) 04:59:27}; - magic story very thanks <a href=" http://groups.google.us/group/sup-hotsexmovies ">pornstar gallreies</a> =]]] -- [[Lftglzgu]] &new{2008-05-13 (火) 05:21:42}; - great work 10x <a href=" http://groups.google.nf/group/lolital ">14 year old sex fuck lolita</a> -- [[lola]] &new{2008-05-13 (火) 05:36:23}; - cool links thanks <a href=" http://groups.google.nf/group/lolitaq ">pthc lolita preteen</a> -- [[mona]] &new{2008-05-13 (火) 05:37:15}; - cool links thanks <a href=" http://groups.google.nf/group/lolitaq ">pthc lolita preteen</a> -- [[mona]] &new{2008-05-13 (火) 05:37:28}; - hi cool site dude <a href=" http://groups.google.nf/group/lolitar ">free nude lolitas with no tits</a> -- [[jessy]] &new{2008-05-13 (火) 05:37:45}; - It's serious <a href=" http://blogs.ign.com/FrenkBently/2008/05/09/89201/ "><b>ringtones free downloads dangers</b></a> rcdvgy -- [[Scfrbtqw]] &new{2008-05-13 (火) 06:33:18}; - Excellent work, Nice Design <a href=" http://groups.google.us/group/all-hotsexmovies ">pics of pornstar leena</a> mxtt -- [[Ohgigtqz]] &new{2008-05-13 (火) 06:54:18}; - Punk not dead <a href=" http://blogs.ign.com/FrenkBently/2008/05/09/89202/ "><b>free t moble ringtones</b></a> mtvx -- [[Fnywwepd]] &new{2008-05-13 (火) 08:05:10}; - Punk not dead <a href=" http://blogs.ign.com/FrenkBently/2008/05/09/89202/ "><b>free t moble ringtones</b></a> mtvx -- [[Fnywwepd]] &new{2008-05-13 (火) 08:05:47}; - very best job <a href=" http://groups.google.us/group/us-hotsexmovies ">young free porn</a> 7688 -- [[Lpkfuayu]] &new{2008-05-13 (火) 08:25:02}; - interesting post thx <a href=" http://groups.google.nf/group/ztube ">redporntube</a> -- [[tube]] &new{2008-05-13 (火) 09:02:40}; - nice site cool work <a href=" http://groups.google.nf/group/rtube ">x tube porn</a> -- [[rtube]] &new{2008-05-13 (火) 09:03:16}; - hi i love you dude ;) <a href=" http://groups.google.nf/group/zteen ">brdteen</a> -- [[tuts]] &new{2008-05-13 (火) 09:07:55}; #comment