title: Sneaking lenses and CPS past the value restriction
body: <p>I'm encoding <a href="https://gist.github.com/tel/08d2a94de21f483cbb20" rel="noreferrer">a form of van Laarhoven lenses in OCaml</a>, but am having difficulty due to the value restriction.</p>
  <p>The relevant code is as follows:</p>
  <pre><code>module Optic : sig
    type (-'s, +'t, +'a, -'b) t
    val lens : ('s -&gt; 'a) -&gt; ('s -&gt; 'b -&gt; 't) -&gt; ('s, 't, 'a, 'b) t
    val _1 : ('a * 'x, 'b * 'x, 'a, 'b) t
  end = struct
    type (-'s, +'t, +'a, -'b) t = 
      { op : 'r . ('a -&gt; ('b -&gt; 'r) -&gt; 'r) -&gt; ('s -&gt; ('t -&gt; 'r) -&gt; 'r) }
  
    let lens get set =
      let op cont this read = cont (get this) (fun b -&gt; read (set this b))
      in { op }
  
    let _1 = let build (_, b) a = (a, b) in lens fst build
  end
  </code></pre>
  <p>Here I am representing a lens as a higher order type, a transformer of CPS-transformed functions <code>('a -&gt; 'b) -&gt; ('s -&gt; 't)</code> (as was suggested <a href="http://lpaste.net/128137" rel="noreferrer">here</a> and discussed <a href="http://www.reddit.com/r/haskell/comments/2zsmg5/my_lens_idea_asking_for/cpm2r6p" rel="noreferrer">here</a>). The functions <code>lens</code>, <code>fst</code>, and <code>build</code> all have fully generalized types but their composition <code>lens fst build</code> does not.</p>
  <pre><code>Error: Signature mismatch:
         ...
         Values do not match:
           val _1 : ('_a * '_b, '_c * '_b, '_a, '_c) t
         is not included in
           val _1 : ('a * 'x, 'b * 'x, 'a, 'b) t
  </code></pre>
  <p>As shown in the gist, it's perfectly possible to write <code>_1</code></p>
  <pre><code>let _1 = { op = fun cont (a, x) read -&gt; cont a (fun b -&gt; read (b, x)) }
  </code></pre>
  <p>but having to manually construct these lenses each time is tedious and it would be nice to build them using higher order functions like <code>lens</code>.</p>
  <p>Is there any way around the value restriction here?</p>