added natural transformation explanation
This commit is contained in:
parent
4e8436be2f
commit
dceac9e1e4
39 changed files with 296 additions and 0 deletions
|
@ -659,6 +659,69 @@ Haskell types is fractal:</p>
|
|||
<li>for Haskell functors: <code>F a -> 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">&</span> <code>toTree.toList=id</code>.<br/> Therefore <code>[]</code> <span class="and">&</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>
|
||||
|
|
14
categories/30_How/170_Natural_Transformation_Examples.html
Normal file
14
categories/30_How/170_Natural_Transformation_Examples.html
Normal 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>
|
||||
|
||||
|
17
categories/30_How/170_Natural_Transformation_Examples.md
Normal file
17
categories/30_How/170_Natural_Transformation_Examples.md
Normal 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>
|
14
categories/30_How/180_Natural_Transformation_Examples.html
Normal file
14
categories/30_How/180_Natural_Transformation_Examples.html
Normal 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> & <code>toTree.toList=id</code>.<br/> Therefore <code>[]</code> & <code>Tree</code> are <span class="yellow">isomorph</span>. </figcaption>
|
||||
</figure>
|
||||
|
||||
|
20
categories/30_How/180_Natural_Transformation_Examples.md
Normal file
20
categories/30_How/180_Natural_Transformation_Examples.md
Normal 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> & <code>toTree.toList=id</code>.<br/>
|
||||
Therefore <code>[]</code> & <code>Tree</code> are <span class="yellow">isomorph</span>.
|
||||
</figcaption>
|
||||
</figure>
|
14
categories/30_How/190_Another_Nat_Trans_Example.html
Normal file
14
categories/30_How/190_Another_Nat_Trans_Example.html
Normal 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>
|
||||
|
||||
|
20
categories/30_How/190_Another_Nat_Trans_Example.md
Normal file
20
categories/30_How/190_Another_Nat_Trans_Example.md
Normal 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>
|
13
categories/30_How/200_Another_Nat_Trans_Example.html
Normal file
13
categories/30_How/200_Another_Nat_Trans_Example.html
Normal 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>
|
||||
|
||||
|
19
categories/30_How/200_Another_Nat_Trans_Example.md
Normal file
19
categories/30_How/200_Another_Nat_Trans_Example.md
Normal 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>
|
8
categories/img/mp/list-maybe-endofunctor-morphism.mp
Normal file
8
categories/img/mp/list-maybe-endofunctor-morphism.mp
Normal 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);
|
BIN
categories/img/mp/list-maybe-endofunctor-morphism.png
Normal file
BIN
categories/img/mp/list-maybe-endofunctor-morphism.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 392 KiB |
7
categories/img/mp/list-tree-endofunctor-morphism.mp
Normal file
7
categories/img/mp/list-tree-endofunctor-morphism.mp
Normal 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);
|
BIN
categories/img/mp/list-tree-endofunctor-morphism.png
Normal file
BIN
categories/img/mp/list-tree-endofunctor-morphism.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 256 KiB |
28
categories/img/mp/nattrans-list-maybe.mp
Normal file
28
categories/img/mp/nattrans-list-maybe.mp
Normal 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));
|
BIN
categories/img/mp/nattrans-list-maybe.png
Normal file
BIN
categories/img/mp/nattrans-list-maybe.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 419 KiB |
21
categories/img/mp/nattrans-list-tree.mp
Normal file
21
categories/img/mp/nattrans-list-tree.mp
Normal 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));
|
||||
|
BIN
categories/img/mp/nattrans-list-tree.png
Normal file
BIN
categories/img/mp/nattrans-list-tree.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 300 KiB |
29
categories/img/mp/nattrans-tree-list.mp
Normal file
29
categories/img/mp/nattrans-tree-list.mp
Normal 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));
|
BIN
categories/img/mp/nattrans-tree-list.png
Normal file
BIN
categories/img/mp/nattrans-tree-list.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 352 KiB |
9
categories/img/mp/tree-list-endofunctor-morphism.mp
Normal file
9
categories/img/mp/tree-list-endofunctor-morphism.mp
Normal 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);
|
BIN
categories/img/mp/tree-list-endofunctor-morphism.png
Normal file
BIN
categories/img/mp/tree-list-endofunctor-morphism.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 352 KiB |
Loading…
Reference in a new issue