<?xml version="1.0" encoding="UTF-8" ?><rdf:RDF 
  xmlns="http://purl.org/rss/1.0/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xml:lang="ja">
  <channel rdf:about="http://w.atwiki.jp/paphi/">
    <title>paphi @wiki</title>
    <link>http://w.atwiki.jp/paphi/</link>
    <atom:link href="https://w.atwiki.jp/paphi/rss10.xml" rel="self" type="application/rss+xml" />
    <atom:link rel="hub" href="https://pubsubhubbub.appspot.com" />
    <description>paphi @wiki</description>

    <dc:language>ja</dc:language>
    <dc:date>2012-12-29T00:33:44+09:00</dc:date>
    <utime>1356708824</utime>

    <items>
      <rdf:Seq>
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/1.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/18.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/17.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/16.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/15.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/2.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/3.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/6.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/7.html" />
                <rdf:li rdf:resource="https://w.atwiki.jp/paphi/pages/10.html" />
              </rdf:Seq>
    </items>
	
		
    
  </channel>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/1.html">
    <title>トップページ</title>
    <link>https://w.atwiki.jp/paphi/pages/1.html</link>
    <description>
      * paphi @wiki

下の顔本が消せるとわかったのでここで続行    </description>
    <dc:date>2012-12-29T00:33:44+09:00</dc:date>
    <utime>1356708824</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/18.html">
    <title>haskell/arrow</title>
    <link>https://w.atwiki.jp/paphi/pages/18.html</link>
    <description>
      [[haskell]] &gt; arrow

* Arrow
Monadの進化形みたいなもの（適当）
 Monad m -- m a   :: *, m :: * -&gt; *
 Arrow a -- a b c :: *, a :: * -&gt; * -&gt; *
ArrowはArrowApplyを満たしてはじめてMonadと等価だそうです．

** ライブラリ
本当はFRPの括りで紹介すべきなんだろうけど．以下の話は全てこれ前提．
[[http://www.haskell.org/haskellwiki/Netwire]]
[[http://hackage.haskell.org/package/netwire]]
2012/12現在4.0.7

* netwire
** Wire型
*** instance
記法として使いそうなのだけ．ArrowApplyは満たしていない．

以下の省略を用いる．本当はdataに制約つけるのは良くないらしい．
 (Monoid e, Monad m) =&gt; type WireP = Wire e m
Categoryっぽく直列につなぐ．
 instance Category WireP where
   id  :: WireP a a
   (.) :: WireP b c -&gt; WireP a b -&gt; WireP a c
Alternativeで並列に．
 instance Functor (WireP a) where
   fmap  :: (b -&gt; c) -&gt; WireP a b -&gt; WireP a c
 instance Functor (WireP a) =&gt; Applicative (WireP a) where
   pure  :: b -&gt; WireP a b
   (&lt;*&gt;) :: WireP a (b -&gt; c) -&gt; WireP a b -&gt; WireP a c
   (&lt;$&gt;) :: (b -&gt; c) -&gt; WireP a b -&gt; WireP a c                 -- (&lt;$&gt;) = fmap
 instance Applicative (WireP a) =&gt; Alternative (WireP a) where -- Monoid e はここ．
   empty :: WireP a b
   (&lt;|&gt;) :: WireP a b -&gt; WireP a b -&gt; WireP a b
   some, many :: WireP a b -&gt; WireP a [b]
timeとかを数字のように扱える．
 instance Num b =&gt; Num (WireP a b) where
   (+), (-), (*)       :: WireP a b -&gt; WireP a b -&gt; WireP a b
   negate, abs, signum :: WireP a b -&gt; WireP a b
   fromInteger         :: Integer -&gt; WireP a b
 instance (Num (WireP a b), Fractional b) =&gt; Fractional (WireP a b) where
   (/)   :: WireP a b -&gt; WireP a b -&gt; WireP a b
   recip :: WireP a b -&gt; WireP a b
   fromRational :: Rational -&gt; WireP a b
 instance (Fractional (WireP a b), Floating b) =&gt; Floating (WireP a b) where
   -- pi, exp, sqrt, log, (**), logBase
   -- sin, tan, cos, asin, atan, acos, sinh, tanh, cosh, asinh, atanh, acosh
OverloadedStrings拡張で使う．
 instance IsString b =&gt; IsString (WireP a b) where
   fromString :: String -&gt; WireP a b
他にRead, Monoid, あとベクトル関係．

* メモ
Arrowの構文
 wire :: WireP Input Output
 wire = proc input -&gt; do
   output &lt;- somewire -&lt; input
   rec
     loop &lt;- delay etc -&lt; loop
   returnA -&lt; output
recも書いてあるけど使いこなせる気がしない

（未検証）変数っぽいのは全て&lt;- -&lt;の外側に置く．こんなんとかダメ
 bad = proc input -&gt; do
   var &lt;- testwire -&lt; ()
   output &lt;- argwire $ var -&lt; input
   ...
ArrowApplyがあれば-&lt;&lt;なんかでできるみたいだけどnetwireは不可．

「Monadに内部状態を加えたもの」だけならMonadFixで多分十分．DoRecとか
Arrowは「計算」そのものを高度に抽象化したものなので（多分）
 Arrow 入力 出力
なんて型になる．
// 正直Arrow使ってみたいだけ，Arrowって言ってみたいだけな気もするが    </description>
    <dc:date>2012-12-26T07:24:57+09:00</dc:date>
    <utime>1356474297</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/17.html">
    <title>haskell/syntax</title>
    <link>https://w.atwiki.jp/paphi/pages/17.html</link>
    <description>
      [[haskell]] &gt; syntax
* Haskellの構文
[[Haskell2010&gt;http://www.haskell.org/onlinereport/haskell2010/]]準拠
工事中

** if, case...of, |
*** if
elseは必須．
 abs n =
   if n &lt; 0
     then -n
     else  n
*** case
これだけインデントを揃える必要がある．多分doと同じ
 sign n = case n `compare` 0 of
   LT -&gt; -1
   GT -&gt;  1
   EQ -&gt;  0
   _  -&gt; error &quot;error&quot;
*** |
otherwise = True
 sign n
   | n &lt; 0 = -1
   | n &gt; 0 =  1
   | otherwise = 0
*** パターンマッチ
分岐っぽいのでついでに
 delta 0 = 1
 delta _ = 0

caseがパターンマッチとかぶってるようだがdataの構成子などで有用．

http://d.hatena.ne.jp/kazu-yamamoto/20110826/1314352340
ここにcaseとガードを混ぜる例があるけど実用はできないと思う．

** where, let...in

** do
[[DoAndIfThenElse&gt;http://hackage.haskell.org/trac/haskell-prime/wiki/DoAndIfThenElse]] hlint(haskell-src-exts)はこの構文に未対応．[[#215&gt;http://trac.haskell.org/haskell-src-exts/ticket/215]]

[[Arrows: A General Interface to Computation&gt;http://www.haskell.org/arrows/]]
[[Haskell/Arrows - Wikibooks&gt;http://ja.wikibooks.org/wiki/Haskell/Arrows]]

** インデント
http://d.hatena.ne.jp/mkotha/20111226/1324909427
全てスペースにしておかないと混乱間違いなし．タブ\tを混ぜるな．

束縛とか型の::は行頭から始める．余分なスペースを入れてはならない．
インデントは基本的に継続行．

* 拡張

* 廃止された構文
[[Changes since Haskell &#039;98&gt;http://www.haskell.org/haskellwiki/Haskell_2010]]
** n+k
Haskell2010で廃止．
教科書によく載ってたりするけど，機械語的に考えると変なので引き算で代用．
 fac  0    =  1
 fac (n+1) = (n+1) * fac n

* リンク
[[The Evolution of a Haskell Programmer&gt;http://www.willamette.edu/~fruehr/haskell/evolution.html]] ネタ    </description>
    <dc:date>2012-09-22T08:23:40+09:00</dc:date>
    <utime>1348269820</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/16.html">
    <title>haskell</title>
    <link>https://w.atwiki.jp/paphi/pages/16.html</link>
    <description>
      * Haskell
世はまさに関数型時代

#ls    </description>
    <dc:date>2012-09-06T03:18:35+09:00</dc:date>
    <utime>1346869115</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/15.html">
    <title>haskell/random</title>
    <link>https://w.atwiki.jp/paphi/pages/15.html</link>
    <description>
      [[haskell]] &gt; random
* Haskellの乱数
乱数は疑似乱数に限定．
&gt; import System.Random
&gt; import Control.Monad.State

** 乱数の定義
乱数はseed値を与えると，値がランダムである無限リストを返す関数であるとひとまず定義する．
乱数の型を1から6までの整数とすると，
&gt; inftyrand seed = [6, 5, 5, 3, ...]
のようになる．ここで，
&gt; inftyrand seed = x:xs -- 先の例では, x = 6, xs = [5, 5, 3, ...]
&gt; inftyrand seed&#039; == xs
となるseed&#039;が存在すると仮定して，乱数を次のように定義することもできる．
#region(seed&#039;の仮定について)
このseed&#039;はまず存在すると考えてよい．
というよりは私の知る乱数は全て Seed -&gt; (a, Seed) という型で計算できる．
例えばC標準の線形randで連想されるアレが一番考えやすい．LCG, 線形合同法というらしい．
http://en.wikipedia.org/wiki/Linear_congruential_generator
簡単に言うと，x = x * c + a (mod m)．この乱数は
&gt; next x = let y = x * c + a `mod` m in (y, y)
と表すことができる．ここでSeedはただの整数値である．

Seedをもう少し複雑にすれば，一気に生成するタイプにも対応できる．多分．(SFMTとか)
Seedの一般化としてRandomGenがある...この辺から表現が曖昧に
複雑なSeed型からどうやって次の値を得るのか，ではなく，
RandomGen型のinstanceであるgが next :: g -&gt; (Int, g) なる関数を持ち，
そこから各種乱数値を生成している...と思う．未確認．
いままで(というかLCGのみ？)値と考えてきたSeedを，次の乱数を生成できる状態と考えることによる一般化である．

seed&#039;が存在するというより，Haskellの乱数は Seed -&gt; (a, Seed) 型の関数を主軸に構成されている，と表現したほうが正しいと思う．
#endregion

** Stateを睨んだ乱数の定義
乱数はseed値を与えると，ランダム値一つと上記のseed&#039;で構成されるタプルを返す関数である．
&gt; next :: Random a =&gt; Seed -&gt; (a, Seed)
Random a は乱数によって生成できる型を表す．
乱数の計算を状態付き計算とみなすことが出来，その型は明らかにStateモナドである．
&gt; newtype State s a = State (s -&gt; (a, s))
&gt; state :: (s -&gt; (a, s)) -&gt; State s a
&gt; runState :: State s a -&gt; s -&gt; (a, s)
&gt; evalState :: State s a -&gt; s -&gt; a
&gt; evalState m s = fst (runState m s)
nextのような関数からstateで値を作って，という形になるかと．
System.Randomにある関数を使って，nextの例を一つ挙げておく．
&gt; next = randomR (1, 6)

まず後の定義から前の定義の乱数が生成しうることを示そう．
State Seed 型がMonadのinstanceであるため，IO型の感覚でdo記法を扱えるし，Monadっぽい関数を使うこともできる．
例えばControl.Monadのsequence関数を使うと，inftyrandは次のように書ける．
ついでにState型の使い方も併記．state被せないとMonadみたく扱えないはず...これで合ってるんだろうか？
&gt; ir_state :: Random a =&gt; State Seed [a]
&gt; ir_state = sequence . repeat $ state next
&gt; inftyrand :: Random a =&gt; Seed -&gt; [a]
&gt; inftyrand = evalState ir_state
無限リストを返す関数となる．Stateに限っては多分遅延評価につき安全．

#region(do記法の例)
do記法で書いてみる．極端な例ですまぬ．
&gt; ir_state = do
&gt;   a1 &lt;- s
&gt;   a2 &lt;- s
&gt;   a3 &lt;- s
&gt;   a4 &lt;- s
&gt;   a5 &lt;- s
&gt;   a6 &lt;- s
&gt;   a7 &lt;- s
&gt;   a8 &lt;- s
&gt;   return [a1+a8, a2+a7, a3+a6, a4+a5]
&gt;  where
&gt;   s = state next
#endregion

** IOとかで使ってみる
例として，この関数をIOにのせることを考える．System.Randomにお誂え向きの関数がある．
&gt; getStdRandom :: (StdGen -&gt; (a, StdGen)) -&gt; IO a
StdGen型は先ほどまでのSeed型と同じ役割を果たす．
&gt; main = do
&gt;   ns &lt;- liftM (take 4) $ getStdRandom $ runState ir_state
&gt;   print ns
ただ値1個だけとかならState経由せずにSystem.Randomの関数を直接使った方がいい．

** System.Random
RandomGenの定義のせるけどこれ32bitと64bitで違ったりしないんだろうか．要調査．
nextは説明済み，genRangeは範囲制限，splitって何だろう．
&gt; class RandomGen g where
&gt;   next     :: g -&gt; (Int, g)
&gt;   genRange :: g -&gt; (Int,Int)
&gt;   split    :: g -&gt; (g, g)

このStdGenは処理系標準の乱数という位置付け．
&gt; data StdGen
&gt; instance RandomGen StdGen

&gt; mkStdGen :: Int -&gt; StdGen
&gt; getStdRandom :: (StdGen -&gt; (a, StdGen)) -&gt; IO a
&gt; getStdGen :: IO StdGen
&gt; setStdGen :: StdGen -&gt; IO ()
&gt; newStdGen :: IO StdGen
処理系にStdGen型の値が1つあって，getStdGenはそれを直接，newStdGenはsplitを使って分岐させるイメージ．
実装見る限りLCGではない．何だろう．

型から推して知るべし．R=range．[a]は無限リスト．
&gt; class Random a where
&gt;   randomR   :: RandomGen g =&gt; (a,a) -&gt; g -&gt; (a,g)
&gt;   random    :: RandomGen g =&gt; g -&gt; (a, g)
&gt;   randomRs  :: RandomGen g =&gt; (a,a) -&gt; g -&gt; [a]
&gt;   randoms   :: RandomGen g =&gt; g -&gt; [a]
&gt;   randomRIO :: (a,a) -&gt; IO a
&gt;   randomIO  :: IO a
IO [a]は無い．多分死ぬから．

** 注釈
State型の定義は本当はStateTを使ってるんだけどスルーしてる．
StateTの中にももう一つMonadがあるんだけどそれはStateT内で完結すると考えて構わない．    </description>
    <dc:date>2012-09-06T03:05:58+09:00</dc:date>
    <utime>1346868358</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/2.html">
    <title>メニュー</title>
    <link>https://w.atwiki.jp/paphi/pages/2.html</link>
    <description>
      **メニュー
-[[トップページ]]

----
** 最新10件
#recent(10)
**リンク
-[[@wiki&gt;&gt;http://atwiki.jp]]
-[[@wikiご利用ガイド&gt;&gt;http://atwiki.jp/guide/]]

&amp;link_editmenu(text=ここを編集)    </description>
    <dc:date>2012-09-01T01:08:18+09:00</dc:date>
    <utime>1346429298</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/3.html">
    <title>右メニュー</title>
    <link>https://w.atwiki.jp/paphi/pages/3.html</link>
    <description>
      **更新履歴
#recent(20)


&amp;link_editmenu2(text=ここを編集)
    </description>
    <dc:date>2012-09-01T00:12:14+09:00</dc:date>
    <utime>1346425934</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/6.html">
    <title>プラグイン/編集履歴</title>
    <link>https://w.atwiki.jp/paphi/pages/6.html</link>
    <description>
      * 更新履歴
@wikiのwikiモードでは
 #recent(数字)
と入力することで、wikiのページ更新履歴を表示することができます。
詳しくはこちらをご覧ください。
＝＞http://atwiki.jp/guide/17_117_ja.html


-----


たとえば、#recent(20)と入力すると以下のように表示されます。


#recent(20)
    </description>
    <dc:date>2012-09-01T00:12:14+09:00</dc:date>
    <utime>1346425934</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/7.html">
    <title>プラグイン/アーカイブ</title>
    <link>https://w.atwiki.jp/paphi/pages/7.html</link>
    <description>
      * アーカイブ
@wikiのwikiモードでは
 #archive_log()
と入力することで、特定のウェブページを保存しておくことができます。
詳しくはこちらをご覧ください。
＝＞http://atwiki.jp/guide/25_171_ja.html


-----


たとえば、#archive_log()と入力すると以下のように表示されます。
保存したいURLとサイト名を入力して&quot;アーカイブログ&quot;をクリックしてみよう


#archive_log()
    </description>
    <dc:date>2012-09-01T00:12:14+09:00</dc:date>
    <utime>1346425934</utime>
  </item>
    <item rdf:about="https://w.atwiki.jp/paphi/pages/10.html">
    <title>プラグイン</title>
    <link>https://w.atwiki.jp/paphi/pages/10.html</link>
    <description>
      @wikiにはいくつかの便利なプラグインがあります。

-----


#ls

-----

これ以外のプラグインについては@wikiガイドをご覧ください
=&gt;http://atwiki.jp/guide/
    </description>
    <dc:date>2012-09-01T00:12:14+09:00</dc:date>
    <utime>1346425934</utime>
  </item>
  </rdf:RDF>
