title: How to work around inability to use lenses with existential types?
body: <p>I am using Edward Kmett's <strong>lens</strong> library for the first time, and finding it rather nice, but I ran into a snag...</p>
  
  <p>The question at [1] explains that existential quantifiers disrupt makeLenses.  I really rather want to use an existential with lenses in some fashion.</p>
  
  <p>As background, I have the class:</p>
  
  <pre><code>class (TextShow file, Eq file, Ord file, Typeable file) =&gt; File file where
    fromAnyFile :: AnyFile -&gt; Maybe file
    fileType :: Simple Lens file FileType
    path :: Simple Lens file Text.Text
    provenance :: Simple Lens file Provenance
  </code></pre>
  
  <p>For the actual question, I want to have the type:</p>
  
  <pre><code>data AnyFile = forall file . File file =&gt; AnyFile { _anyFileAnyFile :: File }
  </code></pre>
  
  <p>And I want to be able to write something along the lines of:</p>
  
  <pre><code>instance File AnyFile where
    fromAnyFile (AnyFile file) = cast file
    fileType (AnyFile file) = fileType . anyFile
    path (AnyFile file) = path . anyFile
    provenance (AnyFile file) = provenance . anyFile
  </code></pre>
  
  <p>This doesn't work, for the reason explained in [1].  If I ask GHC for debugging information by compiling with <code>-ddump-splices</code>, I get:</p>
  
  <pre><code>Haskell/Main.hs:1:1: Splicing declarations
      makeLenses ''AnyFile ======&gt; Haskell/Main.hs:59:1-20
  </code></pre>
  
  <p>The splice itself is blank, which indicates to me that no declarations are produced by it.  This part I expect and understand now that I have read [1].</p>
  
  <p>What I would like to know is how I can do this - what might I do to work around the problem?  What might I do to avoid swimming upstream on this?  I would like to be able to access any part of my structures through a path of composed lenses, but because I have fields in other types with types such as <code>Set AnyFile</code>, I cannot do so unless I can access the content of <code>AnyFile</code> with a lens.</p>
  
  <p>[1] <a href="https://stackoverflow.com/questions/17256091/existential-quantifier-silently-disrupts-template-haskell-makelenses-why">Existential quantifier silently disrupts Template Haskell (makeLenses). Why?</a></p>