changeset 9:95fa8bea3364

finish
author Daichi TOMA <toma@cr.ie.u-ryukyu.ac.jp>
date Sat, 06 Jul 2013 15:04:02 +0900
parents 3ee3f33bc368
children bf2da4395b5f
files haskell.html
diffstat 1 files changed, 335 insertions(+), 536 deletions(-) [+]
line wrap: on
line diff
--- a/haskell.html	Sat Jul 06 12:38:28 2013 +0900
+++ b/haskell.html	Sat Jul 06 15:04:02 2013 +0900
@@ -50,42 +50,13 @@
           純粋関数型プログラミング言語です。
         </p>
         <p>
-          純粋とは、一度変数の値を設定すると、変更することは出来ないということです。
+          純粋なので、一度変数の値を設定すると、変更することは出来ません。
         </p>
         <p>
           関数型言語では、引数に関数を作用させていくことで計算を行います。
         </p>
-			</article>
-
-			<article>
-				<h3>
-          なぜ Haskell で Web Serivce を書くのか
-				</h3>
         <p>
-          <ul>
-            <li>RubyやPythonなどのインタプリタ言語と比較して高速です
-            <li>高水準言語で、CやC++、Javaよりも自分の足を撃ち抜きにくいです
-          </ul>
-        </p>
-			</article>
-
-			<article>
-				<h3>
-          命令型プログラミング言語との違い
-				</h3>
-        <p>
-          命令型プログラミング言語では、命令の並びをコンピュータに与えて、それを実行します。
-        </p>
-        <p>
-          関数型プログラミング言語では、何であるかという定義を与えます。
-        </p>
-        <p>
-          例:n番目のフィボナッチ数を求める関数の定義
-<pre>
-fib 0 = 0
-fib 1 = 1
-fib n = fib (n - 1) + fib (n - 2)
-</pre>
+          他にも遅延評価や、強い静的型付けなどの特徴があります。
         </p>
 			</article>
 
@@ -94,70 +65,24 @@
           Haskell の特徴
 				</h3>
         <p>
-          <ul>
-            <li>副作用を持たない
-          </ul>
-        </p>
-        <p>
-          Haskell は副作用を持ちません。
-        </p>
-        <p>
-          つまり、変数を書き換えることはできないということです。
-        </p>
-        <p>
-          関数は同じ引数で呼ばれた場合、同じ値を返すことを保証します。
-        </p>
-        <p>
-          関数の正しさを簡単に推察でき、正しいと分かっている関数同士を容易に組み合わせることができます。
-        </p>
-			</article>
-
-			<article>
-				<h3>
-          Haskell の特徴
-				</h3>
-        <p>
-          <ul>
-            <li>遅延評価
-          </ul>
-        </p>
-        <p>
-          Haskellは、結果が必要になるまで関数を評価しません。
-        </p>
-        <p>
-          遅延評価のため、無限の大きさのデータを扱うことが可能です。
-        </p>
-        <p>
-          例:奇数の無限のリストから最初の3つを入手する
-<pre>
-take 3 [1,3..]
-</pre>
-        </p>
-			</article>
-
-			<article>
-				<h3>
-          Haskell の特徴
-				</h3>
-        <p>
-          <ul>
-            <li>静的型付け
-          </ul>
-        </p>
-        <p>
-          型規則に従ってない式が存在しないことを保証します。
+          強い静的型付けにより型規則に従ってない式が存在しないことを保証します。
         </p>
         <p>
           また型推論を持つため、すべての式に明示的に型を書く必要はありません。
         </p>
         <p>
+          しかしながら、実際にはどのような関数なのか表すために明示的に型を書くほうが良いです。
+        </p>
+        <p>
           コンパイルが通れば概ね思い通りに動くのもHaskellの特徴です。
         </p>
 			</article>
 
+
+
 			<article>
 				<h3>
-          よし、分かった。では Haskell を使う方法を教えてくれ。
+          Haskell の導入
 				</h3>
         <p>
           一番手っ取り早い方法は、Haskell Platformを導入することです。
@@ -172,7 +97,7 @@
 
 			<article>
 				<h3>
-          Haskell Platform をインストールしたら
+          GHCiの起動
 				</h3>
         <p>
           Terminal を開き、
@@ -193,9 +118,9 @@
         </p>
 			</article>
 
-			<article>
+			<article class="smaller">
 				<h3>
-          ghciで遊んでみる
+          GHCiで遊んでみる
 				</h3>
         <p>
 <pre>
@@ -208,14 +133,6 @@
 ghci&gt; True &amp;&amp; False
 False
 </pre>
-        </p>
-			</article>
-
-			<article>
-				<h3>
-          ghciで遊んでみる
-				</h3>
-        <p>
         gchi内で関数を定義する際はletが必要
 <pre>
 ghci&gt; let doubleMe x = x + x
@@ -232,6 +149,17 @@
 
 			<article>
 				<h3>
+          なぜ Haskell で Web Serivce を書くのか
+				</h3>
+        <p>
+          <ul>
+            <li>RubyやPythonなどのインタプリタ言語と比較して高速です
+            <li>高水準言語で、CやC++、Javaよりも自分の足を撃ち抜きにくいです
+          </ul>
+        </p>
+			</article>
+			<article>
+				<h3>
           Warp
 				</h3>
         <p>
@@ -258,32 +186,100 @@
           cabal を使えば簡単に入れられます。<br>
           cabal とは Haskell の Package 管理システムです。
         </p>
+        <p>
+          インストールされているPackage、場所などは以下のコマンドで確かめられます。
+        </p>
+<pre>
+$ ghc-pkg list
+</pre>
+			</article>
+
+			<article>
+				<h3>
+          HaskellによるWeb Service
+				</h3>
+        <p>
+          今日の例題を見ていきます。<br>
+          <a href="https://gist.github.com/amothic/5938617">https://gist.github.com/amothic/5938617</a>
+        </p>
+        <p>
+          この例題は、URLによって出力する結果を変更するWeb Serviceです。
+          また、/welcome/worldへアクセスした場合、インクリメントされるcounterが表示されます。
+        </p>
+			</article>
+			<article class="smaller">
+				<h3>
+          HaskellによるWeb Service
+				</h3>
+<pre style="height:450px; overflow:scroll;">
+{-# LANGUAGE OverloadedStrings #-}
+import Network.Wai
+import Network.HTTP.Types (status200, status404)
+import Network.Wai.Handler.Warp (run)
+import Control.Monad.Trans (lift)
+import Data.IORef (newIORef, atomicModifyIORef)
+import Data.ByteString.Lazy.UTF8 (fromString)
+
+application counter request = function counter
+  where
+    function = routes $ pathInfo request
+
+routes path = findRoute path routeSetting
+
+findRoute path [] = notFound
+findRoute path ((p,f):xs)
+    | path == p = f
+    | otherwise = findRoute path xs
+
+routeSetting = [([],                 index),
+                (["hello"],          hello),
+                (["welcome","world"],world)]
+
+notFound _ = return $
+    responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found"
+
+index _ = return $
+    responseLBS status200 [("Content-type", "text/html")] $ "index page"
+
+hello _ = return $
+    responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom"
+
+world counter = do
+    count &lt;- lift $ incCount counter
+    return $ responseLBS status200 [("Content-type", "text/html")] $
+      fromString $ show count
+
+incCount counter = atomicModifyIORef counter (\c -&gt; (c+1, c))
+
+main = do
+  counter &lt;- newIORef 0
+  run 3000 $ application counter
+</pre>
 			</article>
 
 			<article class="smaller">
 				<h3>
-          簡単なプログラム
+          import
 				</h3>
-        <p>
-          <a href="https://gist.github.com/amothic/5938383">hello.hs</a>
-        </p>
 <pre>
 {-# LANGUAGE OverloadedStrings #-}
 import Network.Wai
-import Network.HTTP.Types (status200)
+import Network.HTTP.Types (status200, status404)
 import Network.Wai.Handler.Warp (run)
-
-application _ = return $
-  responseLBS status200 [("Content-Type", "text/plain")] "Hello World"
-
-main = run 3000 application
+import Control.Monad.Trans (lift)
+import Data.IORef (newIORef, atomicModifyIORef)
+import Data.ByteString.Lazy.UTF8 (fromString)
+$ ghci
 </pre>
         <p>
-          このソースコードを読み解いていきます。
+          使用するmoduleをimportしています。
         </p>
         <p>
-          Haskell は型を見れば多くのことが分かる言語です。<br>
-          ということで、関数の型を見ていきます!
+          括弧内に関数名を指定することでその関数のみをimportできます。
+        </p>
+        <p>
+          プログラムの最初に書かれている OverloadedStrings という言語拡張は、ダブルクオートで囲んだ文字列を、ByteString リテラルとして扱ってくれます。
+          ByteStringは、Stringと比較して効率よく文字列を扱います。
         </p>
 			</article>
 
@@ -292,18 +288,14 @@
           関数の型を確認する
 				</h3>
         <p>
-          まず、対話モードを開きます。
+          Haskell は型を見れば多くのことが分かる言語です。<br>
+          ということで、関数の型を見ていきます!
+        </p>
+        <p>
+          今回作成した例題をロードして、対話モードを開きます。
         </p>
 <pre>
-$ ghci
-</pre>
-        <p>
-          Wai 及び Warp の module を import します。<br>
-          Wai というのは、Web Application Interface の略で、Web サーバと Web アプリケーション間の共通のプロトコルを取り扱います。
-        </p>
-<pre>
-ghci&gt; :module +Network.Wai
-ghci&gt; :module +Network.Wai.Handler.Warp
+$ ghci example.hs
 </pre>
         <p>
           型を教えて貰うには、:t コマンドに続けて式を入力します。
@@ -313,11 +305,13 @@
 run :: Port -&gt; Application -&gt; IO ()
 </pre>
 			</article>
-
 			<article class="smaller">
 				<h3>
           run
 				</h3>
+        <p>
+          まず、main内にあるrunから見ていきます。
+        </p>
 <pre>
 ghci&gt; :t run
 run :: Port -&gt; Application -&gt; IO ()
@@ -326,12 +320,35 @@
           run は、Port と Application を受け取って、IO () を返す関数だということが分かります。
         </p>
         <p>
-          IO () は IO モナドを表しています。
-          I/O といった副作用を持つ処理を行う時に利用します。
+          Haskellのすべての関数は、実は引数を1つだけ取ることになっています。( カリー化関数 )
+        </p>
+        <p>
+          複数取るように見えますが、実際には関数が1つの引数で呼び出されると、その次の引数を受け取る関数を返します。
         </p>
         <p>
-          IO モナドは中身に直接触ることのできない抽象データ型です。
-          外から触ることを禁止することで、参照透過性を保っています。
+          これにより、関数を本来より少ない引数で呼び出したときに部分適用された関数が得られます。
+        </p>
+			</article>
+
+			<article class="smaller">
+				<h3>
+          IO ()
+				</h3>
+<pre>
+ghci&gt; :t run
+run :: Port -&gt; Application -&gt; IO ()
+</pre>
+        <p>
+          IO () は IO モナドを表しています。
+        <p>
+          Haskell では、副作用を持つ処理は基本的に許されていません。
+        </p>
+        <p>
+          そのため、IO モナドという限られた範囲でのみ行えるようになっています。
+        </p>
+        <p>
+          IOモナドは外から触ることのできない抽象データ型です。
+          外から触ることを禁止することで参照透過性を保っています。
         </p>
         <p>
           Port は、Int の別名です。<br>
@@ -352,31 +369,23 @@
           Request を受け取って Response を返す関数を表しています。
         </p>
         <p>
+          Portと同じようにtypeで定義されています。
+        </p>
+        <p>
+          これはRequest -&gt; ResourceT IO Response に別名 Application をつけているという意味です。
+        </p>
+        <p>
           Response は 2つのモナドに包まれています。
         </p>
         <p>
           ResourceT は、IOのリソースの解放を安全に行うためのものです。
         </p>
-        <p>
-          Haskell では、関数が副作用を持つことは許されませんが、IO モナドによって IO 操作を処理系に押し付けています。
-        </p>
 			</article>
 
 			<article class="smaller">
 				<h3>
-          runとは結局なんぞや
+          run
 				</h3>
-<pre>
-{-# LANGUAGE OverloadedStrings #-}
-import Network.Wai
-import Network.HTTP.Types (status200)
-import Network.Wai.Handler.Warp (run)
-
-application _ = return $
-  responseLBS status200 [("Content-Type", "text/plain")] "Hello World"
-
-main = run 3000 application
-</pre>
         <p>
           型情報から以下のことが分かります。
         </p>
@@ -388,25 +397,31 @@
           実際の動作としては、この関数 run は受け取った Port 3000番で、Application を実行します。
         </p>
         <p>
-          次に、Application の実装を見ていきます。
+          次に、Request を受け取って Response を返す関数である application の実装を見ていきます。
         </p>
 			</article>
 
 			<article class="smaller">
 				<h3>
-          Hello Worldと出力する簡単なお仕事
+          application
 				</h3>
+        <p>
+        applicationでは、requestによって呼び出す関数を振り分けます。
+        where は関数内で使う変数を定義するもので、今回 function には呼び出す関数が入ります。
+        </p>
 <pre>
-application _ = return $
-  responseLBS status200 [("Content-Type", "text/plain")] "Hello World"
+application counter request = function counter
+  where
+    function = routes $ pathInfo request
 </pre>
         <p>
-          まず引数で渡される Request は _ (Underscore) となっているので、使用していません。
-        </p>
-        <p>
-          return の後ろに付いている $ は、関数適用演算子といって括弧の数を減らすのに役たちます。
+          routes の後ろに付いている $ は、関数適用演算子といって括弧の数を減らすのに役たちます。
           普通の関数適用は非常に優先順位が高いのですが、$ は最も低い優先順位を持ちます。
         </p>
+<pre>
+($) :: (a -&gt; b) -&gt; a -&gt; b
+f $ x = f x
+</pre>
         <p>
           下記の2つのコードは同じ結果になります。
         </p>
@@ -417,110 +432,16 @@
 			</article>
 
 			<article class="smaller">
-        <h3>
-          responseLBS とは?
-        </h3>
-<pre>
-ghci&gt; :t responseLBS
-responseLBS
-  :: Status
-     -&gt; ResponseHeaders
-     -&gt; Data.ByteString.Lazy.Internal.ByteString
-     -&gt; Response
-</pre>
+				<h3>
+          request
+				</h3>
         <p>
-          Statusと、ResponseHeaders、ByteStringを受け取り、Responseを返します。
-        </p>
-        <p>
-          簡単に説明すると、文字列からResponseを構築するためのコンストラクターです。
+          request には、clientが送る様々な情報が含まれています。<br>
+          その中には pathInfo という、どこの path へアクセスしてきたかの情報があります。
+          この情報をroutes関数に渡すことで呼び出す関数を振り分けています。
         </p>
         <p>
-          ByteStringは、Stringと比較して効率よく文字列を扱います。
-          プログラムの最初に書かれている OverloadedStrings という言語拡張は、ダブルクオートで囲んだ文字列を、ByteString リテラルとして扱ってくれます。
-        </p>
-<pre>
-{-# LANGUAGE OverloadedStrings #-}
-</pre>
-			</article>
-
-			<article class="smaller">
-        <h3>
-          Hello Worldと出力するプログラム
-        </h3>
-        <p>
-          <a href="https://gist.github.com/amothic/5938383">hello.hs</a>
-        </p>
-<pre>
-{-# LANGUAGE OverloadedStrings #-}
-import Network.Wai
-import Network.HTTP.Types (status200)
-import Network.Wai.Handler.Warp (run)
-
-application _ = return $
-  responseLBS status200 [("Content-Type", "text/plain")] "Hello World"
-
-main = run 3000 application
-</pre>
-			</article>
-
-			<article>
-        <h3>
-          おおよその内容は掴めた、では実行してみようではないか。
-        </h3>
-        <p>
-          実行方法は2つあります。
-        </p>
-        <p>
-          <ul>
-            <li>コンパイルせずに実行
-          </ul>
-<pre>
-$ runghc Hello.hs
-</pre>
-        </p>
-        <p>
-          <ul>
-            <li>コンパイルして実行
-          </ul>
-<pre>
-$ ghc --make Hello.hs
-$ ./Hello
-</pre>
-        </p>
-			</article>
-			<article>
-        <h3>
-          サイトにアクセスしてみる
-        </h3>
-        <p>
-          <a href="http://localhost:3000/">http://localhost:3000/</a>
-        </p>
-        <p>
-          Hello Worldと表示されれば成功です。
-        </p>
-			</article>
-
-			<article>
-        <h3>
-          Routing
-        </h3>
-        <p>
-          次に単純な Routing を行うサイトを実装してみたいと思います。
-        </p>
-        <p>
-          http://localhost:3000/ の後の URL の Path によって 出力する結果を変更してみます。
-        </p>
-        <p>
-          Application が受け取る Request には、clientが送る様々な情報が含まれています。<br>
-          その中には pathInfo という、どこの path へアクセスしてきたかの情報があります。
-        </p>
-			</article>
-
-			<article>
-        <h3>
-          Request に含まれる情報
-        </h3>
-        <p>
+          以下はRequestに含まれる情報です。
           <ul>
             <li>requestMethod :: Method
             <li>httpVersion :: HttpVersion
@@ -535,72 +456,24 @@
             <li>queryString :: Query
           </ul>
         </p>
-
 			</article>
 
 			<article class="smaller">
-        <h3>
-          アクセスしてきたURLで、出力結果を変えるプログラム
-        </h3>
+				<h3>
+          request
+				</h3>
         <p>
-          <a href="https://gist.github.com/amothic/5933808">routes.hs</a>
-<pre>
-application request = return $
-    routes $ pathInfo request
-
-routes path = findRoute path routeSetting
-
-findRoute path [] = notFound
-findRoute path ((p,f):xs)
-    | path == p = f
-    | otherwise = findRoute path xs
-routeSetting = [([],                 index),
-                (["hello"],          hello),
-                (["welcome","world"],world)]
-notFound =
-    responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found"
-index =
-    responseLBS status200 [("Content-type", "text/html")] $ "index page"
-hello =
-    responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom"
-world =
-    responseLBS status200 [("Content-type", "text/html")] $ "Welcome to Underground"
-
-main = run 3000 application
-</pre>
-        </p>
-			</article>
-
-			<article>
-        <h3>
-          pathInfoの情報をどう入手するか
-        </h3>
-        <p>
-          実は超簡単です。
-        </p>
-<pre>
-pathInfo request
-</pre>
-        <p>
-          pathInfo という関数に Request を渡すだけ!
+          requestは、data キーワードを使って定義されています。
+          Haskell では、data キーワードを使って自作のデータ型を作ることができます。
         </p>
         <p>
-          Haskell では、レコード構文というデータ型のフィールドにアクセスするためのコードを自動生成する仕組みがあります。
-          レコード構文で、データ型を定義するとデータ型が作られるのと同時に、フィールド名でフィールドを取得する関数たちが自動で作られます。
+          pathInfoという関数は、requestからpathに関する情報を抜き出しますが、これはレコード構文というものを利用しています。
         </p>
         <p>
-<pre>
-ghci&gt; :t pathInfo
-pathInfo :: Request -&gt; [Data.Text.Internal.Text]
-</pre>
+          以下にレコード構文の例を表示します。
+          レコード構文を使うと簡単にアクセサ関数を定義できます。
         </p>
-			</article>
-
-			<article class="smaller">
-        <h3>
-          レコード構文
-        </h3>
-<pre>
+<pre style="height:300px; overflow:scroll;">
 -- レコード構文を使わない場合
 data Person = Person String String Int Float String String
     deriving (Show)
@@ -611,12 +484,8 @@
 lastName (Person _ lastname _ _ _ _) = lastname
 age :: Person -&gt; Int
 age (Person _ _ age _ _ _) = age
-height :: Person -&gt; Float
-height (Person _ _ _ height _ _) = height
-phoneNumber :: Person -&gt; String
-phoneNumber (Person _ _ _ _ number _) = number
-flavor :: Person -&gt; String
-flavor (Person _ _ _ _ _ flavor) = flavor
+
+( 省略 )
 
 -- レコード構文
 data Person = Person { firstName :: String
@@ -626,140 +495,94 @@
                      , phoneNumber :: String
                      , flavor :: String } deriving (Show)
 </pre>
-        </p>
 			</article>
-
-			<article>
-        <h3>
-          pathによって返す関数を変えてみよう
-        </h3>
+			<article class="smaller">
+				<h3>
+          routes
+				</h3>
         <p>
-          まず、application で request を取り routes という関数に path を渡すように変更します。
-        </p>
-<pre>
-application request = return $
-    routes $ pathInfo request
-</pre>
-        <p>
-          routes は、path を受け取って response を返す関数です。
+          routes 関数では、routeSettingを付け足して、findRoute関数を呼び出しています。
         </p>
 <pre>
 routes path = findRoute path routeSetting
 </pre>
         <p>
-          routes では、routeSetting という List から path が一致するもの探します。
+          routeSetting は、path と関数を記載した List になっています。
+        </p>
+<pre>
+routeSetting = [([],                 index),
+                (["hello"],          hello),
+                (["welcome","world"],world)]
+</pre>
+        <p>
+          この情報を使って、返す関数を決めます。
         </p>
 			</article>
-
-			<article>
-        <h3>
-          pathによって返す関数を変えてみよう
-        </h3>
-        <p>
-          findRoute では、再帰的に List を探索しています。<br>
-          一致するものがなければ、notFound という関数を返します。<br>
-          一致するものがあれば、routeSetting に記載された関数を返します。
+			<article class="smaller">
+				<h3>
+          findRoute
+				</h3>
 <pre>
 findRoute path [] = notFound
 findRoute path ((p,f):xs)
     | path == p = f
     | otherwise = findRoute path xs
-
-routeSetting = [([],                 index),
-                (["hello"],          hello),
-                (["welcome","world"],world)]
 </pre>
+        <p>
+          findRoute は再帰的にListを探索しています。
+        </p>
+        <p>
+          一致するものがなければ、notFound という関数を返します。<br>
+          一致するものがあれば、routeSetting に記載された関数を返します。
+        </p>
+        <p>
+          findRoute が二行にわたって書かれているのはパターンマッチを利用しているためです。
+        </p>
+        <p>
+          findRouteを呼ぶと、パターンが上から下の順で試されます。
+          渡された値が指定されたパターンと一致すると、対応する関数の本体が使われ、残りのパターンは無視されます。
+        </p>
 			</article>
-
 			<article class="smaller">
-        <h3>
-          response関数を定義しよう
-        </h3>
+				<h3>
+          Response を返す関数の実装
+				</h3>
         <p>
-          いくつか定義してみます。
+          notFoundは、404 - File Not Found と表示する関数になります。
+          indexは、index page と表示する関数になります。
         </p>
 <pre>
-notFound =
+notFound _ = return $
     responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found"
 
-index =
+index _ = return $
     responseLBS status200 [("Content-type", "text/html")] $ "index page"
-
-hello =
-    responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom"
-
-world =
-    responseLBS status200 [("Content-type", "text/html")] $ "Welcome to Underground"
 </pre>
-			</article>
-
-			<article class="smaller">
-        <h3>
-          完成!
-        </h3>
         <p>
-          <a href="https://gist.github.com/amothic/5933808">routes.hs</a>
+        responseLBS とは?
+        </p>
 <pre>
-application request = return $
-    routes $ pathInfo request
-
-routes path = findRoute path routeSetting
-
-findRoute path [] = notFound
-findRoute path ((p,f):xs)
-    | path == p = f
-    | otherwise = findRoute path xs
-routeSetting = [([],                 index),
-                (["hello"],          hello),
-                (["welcome","world"],world)]
-notFound =
-    responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found"
-index =
-    responseLBS status200 [("Content-type", "text/html")] $ "index page"
-hello =
-    responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom"
-world =
-    responseLBS status200 [("Content-type", "text/html")] $ "Welcome to Underground"
-
-main = run 3000 application
+ghci&gt; :t responseLBS
+responseLBS
+  :: Status
+     -&gt; ResponseHeaders
+     -&gt; Data.ByteString.Lazy.Internal.ByteString
+     -&gt; Response
 </pre>
+        <p>
+          Statusと、ResponseHeaders、ByteStringを受け取り、Responseを返します。
+        </p>
+        <p>
+          簡単に説明すると、文字列からResponseを構築するためのコンストラクターです。
         </p>
 			</article>
 
 			<article>
-        <h3>
-          実際に動かしてみる!
-        </h3>
-        <p>
-          シンプルなシステムですが、実際に動くのか確かめてみます。
-        </p>
-        <p>
-          "index page" と表示されるはず<br>
-          <a href="http://localhost:3000/">http://localhost:3000/</a>
-        </p>
-        <p>
-          "hello, my name is Tom" と表示されるはず<br>
-          <a href="http://localhost:3000/hello">http://localhost:3000/hello</a>
-        </p>
+				<h3>
+          Counter の実装
+				</h3>
         <p>
-          "welcome to Underground" と表示されるはず<br>
-          <a href="http://localhost:3000/welcome/world">http://localhost:3000/welcome/world</a>
-        </p>
-        <p>
-          一致するpathがないので、"404 - File Not Found" と表示されるはず<br>
-          <a href="http://localhost:3000/hogehoge">http://localhost:3000/hogehoge</a>
-        </p>
-			</article>
-
-			<article>
-        <h3>
-          Counter
-        </h3>
-        <p>
-          最後に、Counter の実装を行なってみたいと思います。
-        </p>
-        <p>
-          アクセスするたびに、Count がインクリメントされていくようなサイトを作成します。
+          アクセスするたびに、Count がインクリメントされていくようなページを作ります。
         </p>
         <p>
           ここでは、Thread-safe な State である Data.IORef を用います。
@@ -768,26 +591,6 @@
 
 			<article>
         <h3>
-          変数の更新はできなかったんじゃ?
-        </h3>
-        <p>
-          Haskell では、変数の更新のような副作用を持つ処理は基本的に許されていません。
-        </p>
-        <p>
-          そのため、IO モナドという限られた範囲でのみ行えるようになっています。
-        </p>
-        <p>
-          IOモナドは中身に直接触ることのできない抽象データ型です。
-          状態を外から触ることを禁止することで参照透過性を保っています。
-        </p>
-        <p>
-          また、Haskell は遅延評価ですが、初期化などIOでは実行順序が重要になってきます。
-          モナドのbindを利用して、計算の実行順序を保証します。
-        </p>
-			</article>
-
-			<article>
-        <h3>
           Data.IORefの使い方
         </h3>
         <p>
@@ -806,27 +609,6 @@
         <h3>
           Data.IORefの使い方
         </h3>
-<pre>
--- IORef Int という型のデータを作製する
-counter &lt;- newIORef 0
-
--- データの更新を atomic に行う
--- atomicModifyIORef には、更新したい IORef a 型の変数と、
--- IORef が持つ値を受け取って 
--- ( 更新後の値, 戻り値にしたい値 ) というタプルを返す関数を渡す
-incCount:: IORef a -&gt; IO a
-incCount counter = atomicModifyIORef counter (\c -&gt; (c+1, c))
-
--- 現在のデータの値を受け取る
-currentNum &lt;- readIORef counter
-</pre>
-        </p>
-			</article>
-
-			<article>
-        <h3>
-          では、このIORefを使ってCounterを作りましょう!
-        </h3>
         <p>
 <pre>
 -- IORef Int という型のデータを作製する
@@ -844,129 +626,146 @@
 </pre>
         </p>
 			</article>
-
 			<article class="smaller">
         <h3>
-          プログラム全容
+          Data.IORef を使った Counter の実装
         </h3>
         <p>
-          <a href="https://gist.github.com/amothic/5937576">counter.hs</a>
+          まず、main内でcouterを初期化します。
+        </p>
+<pre>
+counter &lt;- newIORef 0
+</pre>
+        <p>
+          world 関数で、counterを利用します。
+        </p>
 <pre>
+world counter = do
+    count &lt;- lift $ incCount counter
+    return $ responseLBS status200 [("Content-type", "text/html")] $
+        fromString $ show count
+
+incCount counter = atomicModifyIORef counter (\c -&gt; (c+1, c))
+</pre>
+        <p>
+          incCountでは、atomicModifyIORefを使って、counterをインクリメントしています。
+        </p>
+        <p>
+          また、incCountを、ResourceTのモナド内に持ち込むためliftを行なっています。
+        </p>
+        </article>
+
+			<article class="smaller">
+				<h3>
+          Haskell による Web Service
+				</h3>
+<pre style="height:450px; overflow:scroll;">
 {-# LANGUAGE OverloadedStrings #-}
 import Network.Wai
-import Network.HTTP.Types (status200)
+import Network.HTTP.Types (status200, status404)
 import Network.Wai.Handler.Warp (run)
-import Control.Monad.Trans (liftIO, lift)
+import Control.Monad.Trans (lift)
 import Data.IORef (newIORef, atomicModifyIORef)
 import Data.ByteString.Lazy.UTF8 (fromString)
 
-application counter request = do
-  count &lt;- lift $ incCount counter
-  return $ responseLBS status200 [("Content-type", "text/html")] $
+application counter request = function counter
+  where
+    function = routes $ pathInfo request
+
+routes path = findRoute path routeSetting
+
+findRoute path [] = notFound
+findRoute path ((p,f):xs)
+    | path == p = f
+    | otherwise = findRoute path xs
+
+routeSetting = [([],                 index),
+                (["hello"],          hello),
+                (["welcome","world"],world)]
+
+notFound _ = return $
+    responseLBS status404 [("Content-type", "text/html")] $ "404 - File Not Found"
+
+index _ = return $
+    responseLBS status200 [("Content-type", "text/html")] $ "index page"
+
+hello _ = return $
+    responseLBS status200 [("Content-type", "text/html")] $ "hello, my name is Tom"
+
+world counter = do
+    count &lt;- lift $ incCount counter
+    return $ responseLBS status200 [("Content-type", "text/html")] $
       fromString $ show count
 
 incCount counter = atomicModifyIORef counter (\c -&gt; (c+1, c))
 
-main = do 
+main = do
   counter &lt;- newIORef 0
   run 3000 $ application counter
 </pre>
+			</article>
+
+			<article>
+        <h3>
+          実行方法
+        </h3>
+        <p>
+          実行方法は2つあります。
+        </p>
+        <p>
+          <ul>
+            <li>コンパイルせずに実行
+          </ul>
+<pre>
+$ runghc example.hs
+</pre>
+        </p>
+        <p>
+          <ul>
+            <li>コンパイルして実行
+          </ul>
+<pre>
+$ ghc --make example.hs
+$ ./example
+</pre>
         </p>
 			</article>
 
 			<article>
         <h3>
-          main
+          動作確認
         </h3>
+
         <p>
-          mainでは、counterの初期化を行なっています。
+          シンプルなシステムですが、実際に動くのか確かめてみます。
         </p>
         <p>
-          それをrunとdo構文で糊付けしています。
-        </p>
-<pre>
-main = do 
-  counter &lt;- newIORef 0
-  run 3000 $ application counter
-</pre>
-      </article>
-
-			<article>
-        <h3>
-          application
-        </h3>
-        <p>
-          applicationでは、引数をひとつ増やして、初期化されたIORefを受け取れるようにしています。
-        </p>
-        <p>
-          また、incCountを、ResourceTのモナド内に持ち込むためliftを行なっています。
+          "index page" と表示されるはず<br>
+          <a href="http://localhost:3000/">http://localhost:3000/</a>
         </p>
         <p>
-          incCountでは、atomicModifyIORefを使って、counterをインクリメントしています。
+          "hello, my name is Tom" と表示されるはず<br>
+          <a href="http://localhost:3000/hello">http://localhost:3000/hello</a>
         </p>
-<pre>
-application counter request = do
-  count &lt;- lift $ incCount counter
-  return $ responseLBS status200 [("Content-type", "text/html")] $
-      fromString $ show count
-
-incCount counter = atomicModifyIORef counter (\c -&gt; (c+1, c))
-</pre>
-      </article>
-
-			<article class="smaller">
-        <h3>
-          完成!
-        </h3>
         <p>
-          <a href="https://gist.github.com/amothic/5937576">counter.hs</a>
-<pre>
-{-# LANGUAGE OverloadedStrings #-}
-import Network.Wai
-import Network.HTTP.Types (status200)
-import Network.Wai.Handler.Warp (run)
-import Control.Monad.Trans (liftIO, lift)
-import Data.IORef (newIORef, atomicModifyIORef)
-import Data.ByteString.Lazy.UTF8 (fromString)
-
-application counter request = do
-  count &lt;- lift $ incCount counter
-  return $ responseLBS status200 [("Content-type", "text/html")] $
-      fromString $ show count
-
-incCount counter = atomicModifyIORef counter (\c -&gt; (c+1, c))
-
-main = do 
-  counter &lt;- newIORef 0
-  run 3000 $ application counter
-</pre>
+          インクリメントされる counter が表示されるはず<br>
+          <a href="http://localhost:3000/welcome/world">http://localhost:3000/welcome/world</a>
+        </p>
+        <p>
+          一致するpathがないので、"404 - File Not Found" と表示されるはず<br>
+          <a href="http://localhost:3000/hogehoge">http://localhost:3000/hogehoge</a>
         </p>
 			</article>
 
 			<article>
         <h3>
-          実行
+          Haskell プログラミング
         </h3>
         <p>
-          <a href="http://localhost:3000/">http://localhost:3000/</a>
-        </p>
-        <p>
-          アクセスするたびに表示される数字がインクリメントされていくはずです。
+          ここまで書いてきて、Haskellのプログラムはだいぶ短く書けることに気がつくと思います。
         </p>
         <p>
-          Chromeだと、毎回faviconにアクセスされるせいで、countが+2されていきます…
-        </p>
-      </article>
-
-			<article>
-        <h3>
-          そして気づくことなど
-        </h3>
-        <p>
-          あれ、Haskellのプログラムって短くね?
-        </p>
-        <p>
-          そうです、圧倒的な記述力も特徴なんです。
+          圧倒的な記述力も特徴のひとつです。
         </p>
         <p>
           速くて安全なHaskellで、あなたもWeb Serviceを作って見ませんか?