title: Haskell lenses: how to make view play nicely with traverse?
body: <p>I am trying to learn about lenses by implementing it in Haskell. I have implemented the <code>view</code> combinator as follows:</p>
  
  <pre><code>{-# LANGUAGE RankNTypes #-}
  
  import Control.Applicative
  import Data.Traversable
  
  type Lens s a = Functor f =&gt; (a -&gt; f a) -&gt; s -&gt; f s
  
  view :: Lens s a -&gt; s -&gt; a
  view lens = getConst . lens Const
  </code></pre>
  
  <p>However when I try to use it in conjunction with <code>traverse</code> I get the following error message:</p>
  
  <pre><code>Prelude&gt; :load Lens.hs
  [1 of 1] Compiling Main             ( Lens.hs, interpreted )
  Ok, modules loaded: Main.
  *Main&gt; :t view traverse
  
  &lt;interactive&gt;:1:6:
      Could not deduce (Applicative f) arising from a use of ‘traverse’
      from the context (Traversable t)
        bound by the inferred type of it :: Traversable t =&gt; t a -&gt; a
        at Top level
      or from (Functor f)
        bound by a type expected by the context:
                   Functor f =&gt; (a -&gt; f a) -&gt; t a -&gt; f (t a)
        at &lt;interactive&gt;:1:1-13
      Possible fix:
        add (Applicative f) to the context of
          a type expected by the context:
            Functor f =&gt; (a -&gt; f a) -&gt; t a -&gt; f (t a)
          or the inferred type of it :: Traversable t =&gt; t a -&gt; a
      In the first argument of ‘view’, namely ‘traverse’
      In the expression: view traverse
  </code></pre>
  
  <p>Unfortunately, I don't understand this error message. Please explain what it means and how I may fix it.</p>