title: finally tagless parsing and recursive monadic actions
body: <p>i deserialize a data structure from disc in the finally tagless style. i.e.</p>
  
  <pre><code>class SYM repl where
      a :: repl
      include :: FilePath -&gt; repl 
  
  myParser :: SYM r =&gt; Parser r
  </code></pre>
  
  <p>the language i am parsing has include directives.</p>
  
  <p>i am using <code>attoparsec</code> which is not a monad transformer, so i cannot simply supply a type <code>Loader = FilePath -&gt; IO (Maybe Text)</code>.</p>
  
  <p>i can write an interpreter with the following <code>SYM</code> instance, that resolves an include.</p>
  
  <pre><code>instance SYM r =&gt; SYM (Loader -&gt; IO (Either String r)) where
      include path loader = 
          maybe (Left "cannot load") (parseOnly myParser) &lt;$&gt; loader path
  </code></pre>
  
  <p>unfortunately includes in the included file don't get resolved. of course i can resolve them twice to resolve the next layer. but that leads to infinite types if i want to do that for every possible level.</p>
  
  <p>right now i preload all the includes (in a <code>HashMap</code>) and bind them, so i can pass a <code>FilePath -&gt; Maybe Text</code> to the parser and resolve includes there, but that is clearly not optimal. (and include is not part of <code>SYM</code> anymore.)</p>
  
  <p>my question is, how does a finally tagless style deal with that problem?</p>
  
  <p><strong>edit</strong>: i published a complete example on lpaste: <a href="http://lpaste.net/105182" rel="nofollow">http://lpaste.net/105182</a></p>