<div dir="ltr"><div>Hi Brent,<br><br></div><div>Thanks for the link to the video. I watched the first half yesterday, and it&#39;s definitely content rich. But the overall design of the library is starting to make sense now. The reason it doesn&#39;t work for Sets (and I assume any similar structure like hashmaps) is because, depending on the implementation of Eq on the item the Set is parametrized on and the field being modified, you could accidentally remove another item - and this would be a BAD THING, since applying a setter on a traversal is basically &quot;fmap++&quot; and fmap doesn&#39;t let you remove things. Is this correct?<br>
<br></div><div>Also, thanks for the traversal example with the list, that&#39;s certainly going to be useful.<br><br></div><div>Cheers,<br><br></div><div>Emm<br>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jul 15, 2013 at 3:16 PM, Brent Yorgey <span dir="ltr">&lt;<a href="mailto:byorgey@seas.upenn.edu" target="_blank">byorgey@seas.upenn.edu</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Emmanuel,<br>
<br>
On Sun, Jul 14, 2013 at 10:47:16PM +0200, Emmanuel Surleau wrote:<br>
&gt; Hello there,<br>
<div class="im">&gt;<br>
&gt; The first (concrete) problem I ran into is how to update the members of a<br>
&gt; set with the result of an IO action. I have managed to do this with a pure<br>
&gt; function (prefixName) but I&#39;m not sure of how to do this with<br>
&gt; promptName.<br>
<br>
</div>Unfortunately you cannot do this using a Set.  The reason is that<br>
modifying the contents of a Set might result in a smaller Set and that<br>
cannot possibly satisfy the lens laws.  However, if we change the Set<br>
of dogs to be a list, we can do this using the (%%~) operator:<br>
<br>
&gt;   ... same as before ...<br>
&gt;<br>
&gt;   data Dogs = Dogs { _dogs :: [Dog] }<br>
&gt;          deriving Show<br>
&gt;   makeLenses &#39;&#39;Dogs<br>
&gt;<br>
<div class="im">&gt;   main :: IO ()<br>
&gt;   main = do<br>
</div>&gt;         ... as before ...<br>
<div class="im">&gt;<br>
&gt;         -- change dog names by prompting the user<br>
</div>&gt;         doggies&#39; &lt;- doggies &amp; (<a href="http://dogs.traverse.name" target="_blank">dogs.traverse.name</a>) %%~ prefixName<br>
&gt;         print doggies&#39;<br>
&gt;<br>
&gt;         return ()<br>
<br>
But astoundingly, if you look at the implementation of (%%~), it<br>
is... (%%~) = id!  So this code also works:<br>
<br>
  doggies&#39; &lt;- (<a href="http://dogs.traverse.name" target="_blank">dogs.traverse.name</a>) prefixName doggies<br>
<br>
That&#39;s right, the magic solution is to just treat the<br>
(<a href="http://dogs.traverse.name" target="_blank">dogs.traverse.name</a>) lens as a *function* and apply it to prefixName!<br>
<br>
To understand why this works we have to look a bit at the<br>
implementation.  I am not surprised that you were baffled by it<br>
because it looks quite magical if you don&#39;t understand where it comes<br>
from.<br>
<div class="im"><br>
&gt; type Lens s t a b = forall f. Functor f =&gt; (a -&gt; f b) -&gt; s -&gt; f t<br>
<br>
</div>Instead of trying to understand why this is the definition, let&#39;s just<br>
see what happens if we take the right-hand side and set f = IO:<br>
<br>
  (a -&gt; IO b) -&gt; s -&gt; IO t<br>
<br>
In the above example, a = b = String, and s = t = Dogs, that is,<br>
(<a href="http://dogs.traverse.name" target="_blank">dogs.traverse.name</a>) has type<br>
<br>
  Lens Dogs Dogs String String<br>
<br>
which expands to<br>
<br>
  (String -&gt; IO String) -&gt; Dogs -&gt; IO Dogs<br>
<br>
(actually this is a lie, it is really just a Traversal and not a Lens,<br>
but it&#39;s the same idea).  So if we apply it to &#39;prefixName :: String<br>
-&gt; IO String&#39; we get a<br>
function of type  Dogs -&gt; IO Dogs! Nifty!<br>
<br>
Now, to answer your specific questions:<br>
<div class="im"><br>
&gt; a) I&#39;m not sure why the explicit forall is needed here (isn&#39;t this<br>
&gt; equivalent to just Functor f =&gt; ...)?<br>
<br>
</div>Yes, it is equivalent; the explicit forall is not strictly necessary.<br>
The forall is there to emphasize that the &#39;f&#39; does not show up on the<br>
left-hand side: something of type  Lens s t a b  is a function which<br>
works for *any* specific Functor f that a user might choose. (E.g., we<br>
specifically chose IO in the example above).  So e.g. a lens cannot do<br>
IO operations, because then it would only work for IO and not for<br>
other Functors.  So a lens may only interact with the f via the fmap<br>
function.  (Similarly a Traversal must work with all Applicatives, and<br>
so on.)<br>
<div class="im"><br>
&gt; b) My understanding is that a lens packs both getter and setters, but I<br>
&gt; don&#39;t know which is supposed to be which here...<br>
<br>
</div>You can think of lenses as generalizations of getters+setters, but<br>
that is NOT how they are implemented!  Nothing in the type (a -&gt; f b)<br>
-&gt; s -&gt; f t  corresponds directly to getters or setters.<br>
<br>
For understanding more about what this type means and why it<br>
corresponds to the idea of lenses, I highly recommend the video of<br>
Edward&#39;s presentation to the NY Haskell user&#39;s group:<br>
<a href="http://youtu.be/cefnmjtAolY?hd=1" target="_blank">http://youtu.be/cefnmjtAolY?hd=1</a> .<br>
<div class="im"><br>
&gt; c) Is there any kind of in-depth guide to Control.Lens somewhere? I have<br>
&gt; found some examples and tutorials but nothing that seemed to do more than<br>
&gt; scratch the surface.<br>
<br>
</div>You can find pretty much everything there is on lens here:<br>
<br>
  <a href="http://lens.github.io/" target="_blank">http://lens.github.io/</a><br>
<br>
-Brent<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>