Skip to content

Commit

Permalink
Unified some signatures and corrected small errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Manuel Gimeno Illa committed May 10, 2018
1 parent 704429b commit eb492fa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
42 changes: 21 additions & 21 deletions introlenses/introlenses.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ <h1 class="title">Notes on &quot;Lenses: compositional data access and manipulat
<p class="author">
Juan Manuel Gimeno Illa
</p>
<p class="date">9 May 2018</p>
<p class="date">9 &amp; 16 May 2018</p>
</div>
<div id="disclaimer" class="slide section level1">
<h1>Disclaimer</h1>
Expand Down Expand Up @@ -159,7 +159,7 @@ <h1>The obvious first attempt</h1>
<ul>
<li><p>Doing view then update is Not Cool</p>
<ul>
<li>You could add a mofify method... but...</li>
<li>You could add a modify method... but...</li>
</ul></li>
</ul>
</div>
Expand Down Expand Up @@ -243,7 +243,7 @@ <h1>How are we going to do the set?</h1>
<span class="kw">instance</span> <span class="dt">Functor</span> <span class="dt">Identity</span> <span class="kw">where</span>
fmap f (<span class="dt">Identity</span> x) <span class="fu">=</span> <span class="dt">Identity</span> (f x)

<span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s)
<span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s
set ln x s
<span class="fu">=</span> runIdentity (ln set_fld s)
<span class="kw">where</span><span class="ot"> set_fld ::</span> a <span class="ot">-&gt;</span> <span class="dt">Identity</span> a
Expand All @@ -257,15 +257,15 @@ <h1>How are we going to do the set?</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell">const<span class="ot"> ::</span> a <span class="ot">-&gt;</span> b <span class="ot">-&gt;</span> a
const x _ <span class="fu">=</span> x

<span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s)
<span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s
set ln x <span class="fu">=</span> runIdentity <span class="fu">.</span> ln (<span class="dt">Identity</span> <span class="fu">.</span> const x)</code></pre></div>
</div>
<div id="and-in-the-same-spirit" class="slide section level1">
<h1>And, in the same spirit</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s)
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s
set ln x <span class="fu">=</span> runIdentity <span class="fu">.</span> ln (<span class="dt">Identity</span> <span class="fu">.</span> const x)

<span class="ot">over ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> a
<span class="ot">over ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s
over ln f <span class="fu">=</span> runIdentity <span class="fu">.</span> ln (<span class="dt">Identity</span> <span class="fu">.</span> f)</code></pre></div>
<p>Which is a lot more efficient than the get/set idea we hold before</p>
</div>
Expand All @@ -276,7 +276,7 @@ <h1>Same again... using a lens to view</h1>
<span class="kw">data</span> <span class="dt">LensR</span> s a <span class="fu">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-&gt;</span> a
,<span class="ot"> setR ::</span> a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s }

<span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> a)
<span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> a
view ln s <span class="fu">=</span> <span class="fu">...</span> <span class="co">-- ln returns a value of type (f s)</span>
<span class="co">-- but we want a value of type a</span>
<span class="co">-- This looks harder !!!</span></code></pre></div>
Expand All @@ -292,7 +292,7 @@ <h1>Same again... using a lens to view</h1>
<span class="kw">instance</span> <span class="dt">Functor</span> (<span class="dt">Const</span> v) <span class="kw">where</span>
fmap _ (<span class="dt">Const</span> x) <span class="fu">=</span> <span class="dt">Const</span> x

<span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> a)
<span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> a
view ln s <span class="fu">=</span> getConst (ln <span class="dt">Const</span> s)</code></pre></div>
<ul>
<li><p>Here Const is, deduced by the type inferencer, a -&gt; Const a a</p></li>
Expand All @@ -304,14 +304,14 @@ <h1>Same again... using a lens to view</h1>
<div id="from-lens-to-lensr" class="slide section level1">
<h1>From Lens to LensR</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="kw">type</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f s)
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f s
<span class="kw">data</span> <span class="dt">LensR</span> s a <span class="fu">=</span> <span class="dt">L</span> {<span class="ot"> viewR ::</span> s <span class="ot">-&gt;</span> a
,<span class="ot"> setR ::</span> a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s }

<span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> a)
<span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> a
view ln <span class="fu">=</span> getComst <span class="fu">.</span> ln <span class="dt">Const</span>

<span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s)
<span class="ot">set ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> s
view ln x <span class="fu">=</span> getId <span class="fu">.</span> ln (<span class="dt">Identity</span> <span class="fu">.</span> const x)

<span class="ot">lensToLensR ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> <span class="dt">LensR</span> s a
Expand All @@ -328,7 +328,7 @@ <h1>Exercise</h1>
<div id="lets-create-a-lens" class="slide section level1">
<h1>Let's create a Lens</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="kw">type</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f s)
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f s

<span class="kw">data</span> <span class="dt">Person</span> <span class="fu">=</span> <span class="dt">P</span> {<span class="ot"> _name ::</span> <span class="dt">String</span>,<span class="ot"> _salary ::</span> <span class="dt">Int</span> }

Expand Down Expand Up @@ -391,7 +391,7 @@ <h1>Composing and using lenses</h1>
<span class="ot">-&gt;</span> <span class="dt">Lens&#39;</span> s2 a
<span class="ot">-&gt;</span> <span class="dt">Lens&#39;</span> s1 a

<span class="kw">type</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f s)
<span class="kw">type</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f s

<span class="co">-- Lens&#39; s1 s2 -&gt; Lens&#39; s2 a -&gt; Lens&#39; s1 a</span></code></pre></div>
<ul>
Expand All @@ -402,7 +402,7 @@ <h1>Composing and using lenses</h1>
<ul>
<li>then</li>
</ul>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell">ln1 <span class="fu">.</span><span class="ot"> ln2 ::</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s1 <span class="ot">-&gt;</span> f s1)</code></pre></div>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell">ln1 <span class="fu">.</span><span class="ot"> ln2 ::</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s1 <span class="ot">-&gt;</span> f s1</code></pre></div>
<ul>
<li>So lens composition is simply function composition, namely (.)</li>
</ul>
Expand Down Expand Up @@ -628,12 +628,12 @@ <h1>Using Traversals</h1>
</div>
<div id="more-plase...-try-view" class="slide section level1">
<h1>...more plase... try 'view'</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> a)
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="ot">view ::</span> <span class="dt">Lens&#39;</span> s a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> a
view ln s <span class="fu">=</span> getConst (ln <span class="dt">Const</span> s)</code></pre></div>
<ul>
<li>Try replacing Lens with Traversal</li>
</ul>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="ot">view ::</span> <span class="dt">Traversal&#39;</span> s a <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> a)
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="ot">view ::</span> <span class="dt">Traversal&#39;</span> s a <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> a
view ln s <span class="fu">=</span> getConst (ln <span class="dt">Const</span> s)</code></pre></div>
<ul>
<li><p>This absolutely can't work! (Why not?)</p></li>
Expand Down Expand Up @@ -676,9 +676,9 @@ <h1>Non-uniform traversals</h1>
<div id="composing-traversals" class="slide section level1">
<h1>Composing Traversals</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="kw">type</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f s)
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f s
<span class="kw">type</span> <span class="dt">Traversal&#39;</span> s a <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Applicative</span> f
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f s)</code></pre></div>
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f s</code></pre></div>
<div class="sourceCode"><pre class="sourceCode haskell"><code class="sourceCode haskell"><span class="ot">ln1 ::</span> <span class="dt">Lens&#39;</span> s1 s2
<span class="ot">tr1 ::</span> <span class="dt">Traversal&#39;</span> s1 s2
<span class="ot">ln2 ::</span> <span class="dt">Lens&#39;</span> s2 a
Expand All @@ -692,12 +692,12 @@ <h1>Composing Traversals</h1>
<div id="unusually-for-a-library-lenses-are-not-abstract" class="slide section level1">
<h1>Unusually for a library, lenses are not abstract</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="kw">type</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f s)</code></pre></div>
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f s</code></pre></div>
<ul>
<li>...and not:</li>
</ul>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="kw">newtype</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> <span class="dt">L</span> (forall f<span class="fu">.</span> <span class="dt">Functor</span> f
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f s))</code></pre></div>
<span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f a) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f s)</code></pre></div>
<ul>
<li><p>Lenses and traversals would not compose (or would require lots of different functions to do so)</p></li>
<li><p>BUT: the inner workings are more exposed</p></li>
Expand All @@ -711,7 +711,7 @@ <h1>I have been lying throughout</h1>
<div class="sourceCode"><pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="kw">type</span> <span class="dt">Lens&#39;</span> s a <span class="fu">=</span> <span class="dt">Lens</span> s s a a

<span class="kw">type</span> <span class="dt">Lens</span> s t a b
<span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f b) <span class="ot">-&gt;</span> (s <span class="ot">-&gt;</span> f t)</code></pre></div>
<span class="fu">=</span> forall f<span class="fu">.</span> <span class="dt">Functor</span> f <span class="ot">=&gt;</span> (a <span class="ot">-&gt;</span> f b) <span class="ot">-&gt;</span> s <span class="ot">-&gt;</span> f t</code></pre></div>
<p>-- over :: Lens' s a -&gt; (a -&gt; a) -&gt; s -&gt; s &gt; over :: Profunctor p =&gt; Setting p s t a b -&gt; p a b -&gt; s -&gt; t</p>
<ul>
<li><p>Edward is deeply in thrall to abstractionitis</p></li>
Expand Down
Loading

0 comments on commit eb492fa

Please sign in to comment.