added natural transformation explanation

This commit is contained in:
Yann Esposito 2012-12-07 11:58:41 +01:00
parent 4e8436be2f
commit dceac9e1e4
39 changed files with 296 additions and 0 deletions

View file

@ -659,6 +659,69 @@ Haskell types is fractal:</p>
<li>for Haskell functors: <code>F a -&gt; G a</code> are the natural transformations.<br />List to Trees, Tree to List, Tree to Maybe...<br />Rearragement functions only.</li>
</ul></li>
</ul>
</section>
<section class="slide">
<h2 id="natural-transformation-examples">Natural Transformation Examples</h2>
<pre><code class="haskell">data Tree a = Empty | Node a [Tree a]
deriving (Show)
toTree :: [a] -> Tree a
toTree [] = Empty
toTree (x:xs) = Node x [toTree xs]</pre>
</code>
<p><code>toTree</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Tree</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-list-tree.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/list-tree-endofunctor-morphism.png" alt="natural transformation commutative diagram"/>
</figure>
</section>
<section class="slide">
<h2 id="natural-transformation-examples">Natural Transformation Examples</h2>
<pre><code class="haskell">data Tree a = Empty | Node a [Tree a]
deriving (Show)
toList :: Tree a -> [a]
toList Empty = []
toList (Node x l) = [x] ++ concat (map toList l)</pre>
</code>
<p><code>toList</code> is a natural transformation. It is also a morphism from <code>Tree</code> to <code>[]</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-tree-list.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/tree-list-endofunctor-morphism.png" alt="natural transformation commutative diagram"/> <figcaption><code>toList.toTree=id</code> <span class="and">&amp;</span> <code>toTree.toList=id</code>.<br/> Therefore <code>[]</code> <span class="and">&amp;</span> <code>Tree</code> are <span class="yellow">isomorph</span>. </figcaption>
</figure>
</section>
<section class="slide">
<h2 id="another-nat.-trans.-example">Another Nat. Trans. Example</h2>
<pre><code class="haskell">
toMaybe :: [a] -> Maybe a ; mToList :: Maybe a -> [a]
toMaybe [] = Nothing ; mToList Nothing = []
toMaybe (x:xs) = Just x ; mToList Just x = [x]
</pre>
</code>
<p><code>toMaybe</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Maybe</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-list-maybe.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/list-maybe-endofunctor-morphism.png" alt="natural transformation commutative diagram"/> <figcaption>There is <span class="red">no isomorphism</span>.<br/> Hint: <code>Bool</code> lists longer than 1. </figcaption>
</figure>
</section>
<section class="slide">
<h2 id="another-nat.-trans.-example">Another Nat. Trans. Example</h2>
<pre><code class="haskell">
toMaybe :: [a] -> Maybe a
toMaybe [] = Nothing
toMaybe (x:xs) = Just x</pre>
</code>
<p><code>toMaybe</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Maybe</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-maybe-list.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/maybe-list-endofunctor-morphism.png" alt="natural transformation commutative diagram"/> <figcaption>There is no isomorphism.<br/> Hint: List with more than one element. </figcaption>
</figure>
</section>
<section class="slide">
<h2 id="monads">Monads</h2>

View file

@ -0,0 +1,14 @@
<h2 id="natural-transformation-examples">Natural Transformation Examples</h2>
<pre><code class="haskell">data Tree a = Empty | Node a [Tree a]
deriving (Show)
toTree :: [a] -> Tree a
toTree [] = Empty
toTree (x:xs) = Node x [toTree xs]</pre>
</code>
<p><code>toTree</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Tree</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-list-tree.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/list-tree-endofunctor-morphism.png" alt="natural transformation commutative diagram"/>
</figure>

View file

@ -0,0 +1,17 @@
Natural Transformation Examples
-------------------------------
<pre><code class="haskell">data Tree a = Empty | Node a [Tree a]
deriving (Show)
toTree :: [a] -> Tree a
toTree [] = Empty
toTree (x:xs) = Node x [toTree xs]</pre></code>
`toTree` is a natural transformation.
It is also a morphism from `[]` to `Tree` in the Category of \\(\Hask\\) endofunctors.
<img style="float:left;width:50%" src="categories/img/mp/nattrans-list-tree.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/list-tree-endofunctor-morphism.png" alt="natural transformation commutative diagram"/>
</figure>

View file

@ -0,0 +1,14 @@
<h2 id="natural-transformation-examples">Natural Transformation Examples</h2>
<pre><code class="haskell">data Tree a = Empty | Node a [Tree a]
deriving (Show)
toList :: Tree a -> [a]
toList Empty = []
toList (Node x l) = [x] ++ concat (map toList l)</pre>
</code>
<p><code>toList</code> is a natural transformation. It is also a morphism from <code>Tree</code> to <code>[]</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-tree-list.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/tree-list-endofunctor-morphism.png" alt="natural transformation commutative diagram"/> <figcaption><code>toList.toTree=id</code> &amp; <code>toTree.toList=id</code>.<br/> Therefore <code>[]</code> &amp; <code>Tree</code> are <span class="yellow">isomorph</span>. </figcaption>
</figure>

View file

@ -0,0 +1,20 @@
Natural Transformation Examples
-------------------------------
<pre><code class="haskell">data Tree a = Empty | Node a [Tree a]
deriving (Show)
toList :: Tree a -> [a]
toList Empty = []
toList (Node x l) = [x] ++ concat (map toList l)</pre></code>
`toList` is a natural transformation.
It is also a morphism from `Tree` to `[]` in the Category of \\(\Hask\\) endofunctors.
<img style="float:left;width:50%" src="categories/img/mp/nattrans-tree-list.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/tree-list-endofunctor-morphism.png" alt="natural transformation commutative diagram"/>
<figcaption><code>toList.toTree=id</code> &amp; <code>toTree.toList=id</code>.<br/>
Therefore <code>[]</code> &amp; <code>Tree</code> are <span class="yellow">isomorph</span>.
</figcaption>
</figure>

View file

@ -0,0 +1,14 @@
<h2 id="another-nat.-trans.-example">Another Nat. Trans. Example</h2>
<pre><code class="haskell">
toMaybe :: [a] -> Maybe a ; mToList :: Maybe a -> [a]
toMaybe [] = Nothing ; mToList Nothing = []
toMaybe (x:xs) = Just x ; mToList Just x = [x]
</pre>
</code>
<p><code>toMaybe</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Maybe</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-list-maybe.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/list-maybe-endofunctor-morphism.png" alt="natural transformation commutative diagram"/> <figcaption>There is <span class="red">no isomorphism</span>.<br/> Hint: <code>Bool</code> lists longer than 1. </figcaption>
</figure>

View file

@ -0,0 +1,20 @@
Another Nat. Trans. Example
---------------------------
<pre><code class="haskell">
toMaybe :: [a] -> Maybe a ; mToList :: Maybe a -> [a]
toMaybe [] = Nothing ; mToList Nothing = []
toMaybe (x:xs) = Just x ; mToList Just x = [x]
</pre></code>
`toMaybe` is a natural transformation.
It is also a morphism from `[]` to `Maybe` in the Category of \\(\Hask\\) endofunctors.
<img style="float:left;width:50%" src="categories/img/mp/nattrans-list-maybe.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/list-maybe-endofunctor-morphism.png" alt="natural transformation commutative diagram"/>
<figcaption>There is <span class="red">no isomorphism</span>.<br/>
Hint: <code>Bool</code> lists longer than 1.
</figcaption>
</figure>

View file

@ -0,0 +1,13 @@
<h2 id="another-nat.-trans.-example">Another Nat. Trans. Example</h2>
<pre><code class="haskell">
toMaybe :: [a] -> Maybe a
toMaybe [] = Nothing
toMaybe (x:xs) = Just x</pre>
</code>
<p><code>toMaybe</code> is a natural transformation. It is also a morphism from <code>[]</code> to <code>Maybe</code> in the Category of \(\) endofunctors.</p>
<img style="float:left;width:50%" src="categories/img/mp/nattrans-maybe-list.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/maybe-list-endofunctor-morphism.png" alt="natural transformation commutative diagram"/> <figcaption>There is no isomorphism.<br/> Hint: List with more than one element. </figcaption>
</figure>

View file

@ -0,0 +1,19 @@
Another Nat. Trans. Example
---------------------------
<pre><code class="haskell">
toMaybe :: [a] -> Maybe a
toMaybe [] = Nothing
toMaybe (x:xs) = Just x</pre></code>
`toMaybe` is a natural transformation.
It is also a morphism from `[]` to `Maybe` in the Category of \\(\Hask\\) endofunctors.
<img style="float:left;width:50%" src="categories/img/mp/nattrans-maybe-list.png" alt="natural transformation commutative diagram"/>
<figure style="float:right;width:40%">
<img src="categories/img/mp/maybe-list-endofunctor-morphism.png" alt="natural transformation commutative diagram"/>
<figcaption>There is no isomorphism.<br/>
Hint: List with more than one element.
</figcaption>
</figure>

View file

@ -0,0 +1,8 @@
pair tree,list;
resize(.8cm);
list=origin;
tree=list shifted (gu,0);
drawState(list,"\mathtt{[]}");
drawState(tree,"\mathtt{Maybe}");
drawEdgeAngle(list,tree,"\mathtt{toMaybe}",30);
drawEdgeAngle(tree,list,"\mathtt{mToList}",30);

Binary file not shown.

After

Width:  |  Height:  |  Size: 392 KiB

View file

@ -0,0 +1,7 @@
pair tree,list;
resize(.8cm);
list=origin;
tree=list shifted (gu,0);
drawState(list,"\mathtt{[]}");
drawState(tree,"\mathtt{Tree}");
drawEdgeAngle(list,tree,"\mathtt{toTree}",30);

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

View file

@ -0,0 +1,28 @@
z0=(0,0);
z1=(1.5gu,0);
z2=(0,-gu);
z3=(1.5gu,-gu);
path ac,ab;
label(btex $\mathtt{[a]}$ etex,z0);
label(btex $\mathtt{[b]}$ etex,z1);
label(btex $\mathtt{Maybe\ a}$ etex,z2 shifted (-u,0));
label(btex $\mathtt{Maybe\ b}$ etex,z3 shifted (u,0));
drawEdge(z0,z1,"\mathtt{fmap_{[]}\ f}");
drawEdge(z2,z3,"\mathtt{fmap_{Maybe}\ f}");
ab:=edge(z0,z2);
drawarrow ab;
label.lft(btex $\mathtt{toMaybe}$ etex,midpoint(ab));
ac:=edge(z1,z3);
drawarrow ac;
label.rt(btex $\mathtt{toMaybe}$ etex,midpoint(ac));
path abb,acb;
abb:=edge(z2 shifted (.2u,0),z0 shifted (.2u,0));
acb:=edge(z3 shifted (-.2u,0),z1 shifted (-.2u,0));
drawarrow abb;
drawarrow acb;
label.rt(btex $\mathtt{mToList}$ etex,midpoint(abb));
label.lft(btex $\mathtt{mToList}$ etex,midpoint(acb));

Binary file not shown.

After

Width:  |  Height:  |  Size: 419 KiB

View file

@ -0,0 +1,21 @@
z0=(0,0);
z1=(1.5gu,0);
z2=(0,-gu);
z3=(1.5gu,-gu);
path ac,ab;
label(btex $\mathtt{[a]}$ etex,z0);
label(btex $\mathtt{[b]}$ etex,z1);
label(btex $\mathtt{Tree\ a}$ etex,z2 shifted (-u,0));
label(btex $\mathtt{Tree\ b}$ etex,z3 shifted (u,0));
drawEdge(z0,z1,"\mathtt{fmap_{[]}\ f}");
drawEdge(z2,z3,"\mathtt{fmap_{Tree}\ f}");
ab:=edge(z0,z2);
drawarrow ab;
label.lft(btex $\mathtt{toTree}$ etex,midpoint(ab));
ac:=edge(z1,z3);
drawarrow ac;
label.rt(btex $\mathtt{toTree}$ etex,midpoint(ac));

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 KiB

View file

@ -0,0 +1,29 @@
z0=(0,0);
z1=(1.5gu,0);
z2=(0,-gu);
z3=(1.5gu,-gu);
path ac,ab;
label(btex $\mathtt{[a]}$ etex,z0);
label(btex $\mathtt{[b]}$ etex,z1);
label(btex $\mathtt{Tree\ a}$ etex,z2 shifted (-u,0));
label(btex $\mathtt{Tree\ b}$ etex,z3 shifted (u,0));
drawEdge(z0,z1,"\mathtt{fmap_{[]}\ f}");
drawEdge(z2,z3,"\mathtt{fmap_{Tree}\ f}");
ab:=edge(z0,z2);
drawarrow ab;
label.lft(btex $\mathtt{toTree}$ etex,midpoint(ab));
ac:=edge(z1,z3);
drawarrow ac;
label.rt(btex $\mathtt{toTree}$ etex,midpoint(ac));
drawoptions(withcolor yellow);
path abb,acb;
abb:=edge(z2 shifted (.2u,0),z0 shifted (.2u,0));
acb:=edge(z3 shifted (-.2u,0),z1 shifted (-.2u,0));
drawarrow abb;
drawarrow acb;
label.rt(btex $\mathtt{toList}$ etex,midpoint(abb));
label.lft(btex $\mathtt{toList}$ etex,midpoint(acb));

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 KiB

View file

@ -0,0 +1,9 @@
pair tree,list;
resize(.8cm);
list=origin;
tree=list shifted (gu,0);
drawState(list,"\mathtt{[]}");
drawState(tree,"\mathtt{Tree}");
drawEdgeAngle(list,tree,"\mathtt{toTree}",30);
drawoptions(withcolor yellow);
drawEdgeAngle(tree,list,"\mathtt{toList}",30);

Binary file not shown.

After

Width:  |  Height:  |  Size: 352 KiB