[[OCamlテクニック/monad]]

*OCamlでモナドを楽しむ [#f8329a4b]

HaskellのMaybeモナドとかListモナドによるバックトラックとかは大変有用。
でも欲張ってモナドそのものをOCamlに導入しようとすると、色々と複雑になってくる。
そこで、camlp4を使ったモナド拡張を利用して、シンプルにモナドを楽しんでみる。

- [[monad拡張のページ:http://www.cas.mcmaster.ca/~carette/pa_monad/]]
-- 例によって有名人が作っている。
-- exceptionモナドとかイケてる。

**まずは環境作り [#n7962b19]

monad拡張をダウンロードしてきたら、おもむろにmake。その後、

 $ocaml -I +camlp4
      Objective Caml version 3.09.3
 #load "camlp4o.cma";;
      Camlp4 Parsing version 3.09.3
 #load "pa_monad.cmo";;

として準備OK

**Maybeモナド [#r7c56357]

まず定義。''超簡単。''
 module MaybeM = struct
     let bind m f =
       match m with
         None -> None
       | Some v -> f v
   end;;
 module MaybeM :
   sig val bind : 'a option -> ('a -> 'b option) -> 'b option end

使ってみる。''perform''というのがmonad拡張のキーワード。
bind関数を呼び出してくれる。
 # let env key =
     try
       Some (Sys.getenv key)
     with
       _ -> None
   in
   let name =
     perform with module MaybeM in
     user <-- env "USER"; 
     host <-- env "HOST";
     Some (user ^ "@" ^ host)
   in
   match name with
     None -> "None\n"
   | Some n -> n ^ "\n";;
 - : string = "ogasawara@blendy\n"

これでoption型も怖くない。

**Listモナド [#x7cf4a9f]

Haskellのlistモナドと同等・そのまんま。
 # module ListM = struct
     let return x = [x]  
     let bind m f =
      List.flatten (List.map f m)  
     let guard c =
       if c then
         return ()
       else
         []
   end;;
 module ListM :
   sig
     val return : 'a -> 'a list
     val bind : 'a list -> ('a -> 'b list) -> 'b list
     val guard : bool -> unit list
   end 

バックトラックしてくれる。
 # perform with module ListM in
     i <-- [1; 2; 3];
     j <-- [4; 5; 6];
     ListM.guard (i + j > 7);
     ListM.return (i, j);;
 - : (int * int) list = [(2, 6); (3, 5); (3, 6)]

応用すると、OCamlには参照型もあるので、''条件に合う中で最大のもの''とかも簡単に作れる。
- 私には、このほうがMonadが馴染み深く感じて好きです。ガリグ先生の授業で[[SEND+MORE=MONEY:http://tsukimi.agusa.i.is.nagoya-u.ac.jp/~sydney/ocaml/index.php?%A5%CD%A5%BF%B5%AD%CF%BF%B8%CB#e74b7ff8]]が出たとき、これを知っていたら・・・。それにしても、また Oleg氏ですか、化け物ですね。 彼の [[Native delimited continuations in (byte-code) OCaml:http://okmij.org/ftp/Computation/Continuations.html#caml-shift]] も気になります。OCamlで(限定)継続ができるんだそうで。 -- [[げんま]] &new{2007-02-02 (金) 21:57:53};
- すげええぇぇぇ。わくわくしますね。 -- [[しもむら]] &new{2007-02-03 (土) 01:26:43};
- Oleg氏すごいですよねー。まだ使ったことは無いのですが、継続もそのうち試してみたいです。 -- [[ogasawara]] &new{2007-02-03 (土) 04:38:55};
- reddit.programming経由. [[A Monad Tutorial for Ocaml:http://enfranchisedmind.com/blog/archive/2007/08/06/307]] -- [[げんま]] &new{2007-08-13 (月) 20:19:09};
- Very good website you have here.<a href=http://www.geocities.com/hentaianalfreeporn/free5253.html >Maps</a><a href=http://www.geocities.com/hentaianalfreeporn/free6907.html >Best Links</a><a href=http://www.geocities.com/hentaianalfreeporn/free9659.html >Maps</a><a href=http://www.geocities.com/hentaianalfreeporn/free1023.html >Interesting Pages</a><a href=http://www.geocities.com/hentaianalfreeporn/free5560.html >Maps</a>[url=http://www.geocities.com/hentaianalfreeporn/free6907.html] Best Links [/url][url=http://www.geocities.com/hentaianalfreeporn/free6987.html] Interesting Pages [/url][url=http://www.geocities.com/hentaianalfreeporn/free9670.html] Maps [/url][url=http://www.geocities.com/hentaianalfreeporn/free9659.html] Maps [/url][url=http://www.geocities.com/hentaianalfreeporn/free5253.html] Maps [/url] -- [[Vassilios]] &new{2008-06-11 (水) 12:01:44};
- Hello, nice site look this:<a href=http://www.geocities.com/pussybikinidating/preteen2398.html >Interesting Pages</a><a href=http://www.geocities.com/bikiniacademy/academy5292.html >Links</a><a href=http://www.geocities.com/bikiniacademy/bikini2572.html >Best Links</a><a href=http://www.geocities.com/bikiniacademy/bikini2214.html >Maps</a><a href=http://www.geocities.com/bikiniacademy/and5771.html >Maps</a><a href=http://www.geocities.com/pussybikinidating/pussy8196.html >Best Links</a><a href=http://www.geocities.com/pussybikinidating/preteen9845.html >Best Link</a>[url=http://www.geocities.com/pussybikinidating/dating1387.html] Cool sites [/url][url=http://www.geocities.com/bikiniacademy/bikini2214.html] Maps [/url][url=http://www.geocities.com/pussybikinidating/preteen9845.html] Best Link [/url][url=http://www.geocities.com/pussybikinidating/preteen2398.html] Interesting Pages [/url][url=http://www.geocities.com/bikiniacademy/bikini2572.html] Best Links [/url][url=http://www.geocities.com/pussybikinidating/download4515.html] Cool Links [/url] -- [[Laurentios]] &new{2008-06-12 (木) 10:44:37};

#comment

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS