Mercurial > hg > Members > atton > generated_seminar_slides
changeset 42:ce4917c50d29
auto-Update generated slides by script
author | Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 02 Sep 2014 18:03:20 +0900 |
parents | 3773cf2f688d |
children | 14da4e3ec832 |
files | slides/20140902/slide.html slides/20140902/slide.md |
diffstat | 2 files changed, 155 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/slides/20140902/slide.html Tue Sep 02 17:46:15 2014 +0900 +++ b/slides/20140902/slide.html Tue Sep 02 18:03:20 2014 +0900 @@ -43,7 +43,7 @@ <!-- === begin markdown block === generated by markdown 1.1.1 on Ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0] - on 2014-09-02 17:46:14 +0900 with Markdown engine kramdown (1.3.3) + on 2014-09-02 18:03:19 +0900 with Markdown engine kramdown (1.3.3) using options {} --> @@ -101,6 +101,10 @@ data Similar a a = Similar a (a -> a) a </code></pre> +<ul> + <li>型変数をどう扱うかで悩み中です</li> +</ul> + </section> @@ -138,7 +142,27 @@ </code></pre> <ul> - <li>こんな感じなんだろうけれど Monad の return が定義できない + <li>型変数が2つだと Functor の定義は楽</li> +</ul> + + + + </section> +</div></div> + +<div class="slide" id="7"><div> + <section> + <header> + <h1 id="similar-monad-as-functor-1">Similar Monad as Functor</h1> + </header> + <!-- _S9SLIDE_ --> + +<pre><code>instance Monad (Similar a) where + return x = Similar x id x -- NG + s >>= f = mu (eqmap f s) +</code></pre> +<ul> + <li>型変数が2つだと Monad の return が定義できない <ul> <li>(Similar a) の a を固定していないと Monad の instance にできない</li> <li>return :: a1 -> Similar a a1</li> @@ -152,17 +176,17 @@ </section> </div></div> -<div class="slide" id="7"><div> +<div class="slide" id="8"><div> <section> <header> - <h1 id="sample--similar-monad-as-functor">Sample : Similar Monad as Functor</h1> + <h1 id="sample--similar-monad-as-functor-revision-9">Sample : Similar Monad as Functor (revision 9)</h1> </header> <!-- _S9SLIDE_ --> <pre><code>similar :: (Show b, Eq b) => (a -> b) -> (a -> b) -> a -> b similar f g x = same $ Similar x g (f x) --- samples (revision 9) +-- samples ok = map (similar twicePlus double) [1..10] notOk = map (similar twicePlus plusTwo) [1..10] okSpecific = map (similar twicePlus plusTwo) [2] @@ -173,10 +197,81 @@ </section> </div></div> -<div class="slide" id="8"><div> +<div class="slide" id="9"><div> + <section> + <header> + <h1 id="similar-monad-as-monad">Similar Monad as Monad</h1> + </header> + <!-- _S9SLIDE_ --> + +<pre><code>data Similar a b = Similar a (a -> b) b + +instance EqMonad Similar where + return x = Similar x id x + s >>= f = mu (eqmap f s) +</code></pre> + +<ul> + <li>型変数が1つなら Monad の定義はできる</li> + <li>通常の Functor だと (a -> b) の b に constraint をかけられない</li> +</ul> + + + + </section> +</div></div> + +<div class="slide" id="10"><div> <section> <header> - <h1 id="section-2">謎</h1> + <h1 id="similar-monad-as-monad-1">Similar Monad as Monad</h1> + </header> + <!-- _S9SLIDE_ --> + + +<pre><code>class EqFunctor f where + eqmap :: (Eq a, Eq b) => (a -> b) -> f a -> f b + +instance EqFunctor Similar where + eqmap f s = Similar fs id fs + where fs = f $ same s +</code></pre> + +<ul> + <li>(a -> b) の b が Eq 保証された Functor Class を使えばどうにか</li> +</ul> + + + + </section> +</div></div> + +<div class="slide" id="11"><div> + <section> + <header> + <h1 id="sample--similar-monad-as-monad-revision-11">Sample : Similar Monad as Monad (revision 11)</h1> + </header> + <!-- _S9SLIDE_ --> + +<pre><code>same $ return 100 >>= (\x -> Similar x twicePlus $ double x) +200 + +same $ return 2 >>= (\x -> Similar x plusTwo $ double x) +4 + +same $ return 100 >>= (\x -> Similar x plusTwo $ double x) +*** Exception: Prelude.undefined +</code></pre> + + + + </section> +</div></div> + +<div class="slide" id="12"><div> + <section> + <header> + <h1 id="problem">Problem</h1> </header> <!-- _S9SLIDE_ -->
--- a/slides/20140902/slide.md Tue Sep 02 17:46:15 2014 +0900 +++ b/slides/20140902/slide.md Tue Sep 02 18:03:20 2014 +0900 @@ -30,6 +30,8 @@ data Similar a a = Similar a (a -> a) a ``` +* 型変数をどう扱うかで悩み中です + # Usage : Similar Monad ``` initObj = Similar 100 id 100 @@ -47,23 +49,70 @@ fmap g (Similar a f b) = Similar a (g . f) $ g b ``` -* こんな感じなんだろうけれど Monad の return が定義できない +* 型変数が2つだと Functor の定義は楽 + +# Similar Monad as Functor +``` +instance Monad (Similar a) where + return x = Similar x id x -- NG + s >>= f = mu (eqmap f s) +``` +* 型変数が2つだと Monad の return が定義できない * (Similar a) の a を固定していないと Monad の instance にできない * return :: a1 -> Similar a a1 * を想定されているが、 id なので Similar a a でおかしい -# Sample : Similar Monad as Functor +# Sample : Similar Monad as Functor (revision 9) ``` similar :: (Show b, Eq b) => (a -> b) -> (a -> b) -> a -> b similar f g x = same $ Similar x g (f x) --- samples (revision 9) +-- samples ok = map (similar twicePlus double) [1..10] notOk = map (similar twicePlus plusTwo) [1..10] okSpecific = map (similar twicePlus plusTwo) [2] ``` -# 謎 + +# Similar Monad as Monad +``` +data Similar a b = Similar a (a -> b) b + +instance EqMonad Similar where + return x = Similar x id x + s >>= f = mu (eqmap f s) +``` + +* 型変数が1つなら Monad の定義はできる +* 通常の Functor だと (a -> b) の b に constraint をかけられない + + +# Similar Monad as Monad + +``` +class EqFunctor f where + eqmap :: (Eq a, Eq b) => (a -> b) -> f a -> f b + +instance EqFunctor Similar where + eqmap f s = Similar fs id fs + where fs = f $ same s +``` + +* (a -> b) の b が Eq 保証された Functor Class を使えばどうにか + +# Sample : Similar Monad as Monad (revision 11) +``` +same $ return 100 >>= (\x -> Similar x twicePlus $ double x) +200 + +same $ return 2 >>= (\x -> Similar x plusTwo $ double x) +4 + +same $ return 100 >>= (\x -> Similar x plusTwo $ double x) +*** Exception: Prelude.undefined +``` + +# Problem * type variable が 1つだと * Functor で移す先が Eq である保証が無くて怒られる * type variable が 2つだと