title: Identity parser
body: <p>As an exercise¹, I've written a string parser that only uses <a href="https://hackage.haskell.org/package/parsers-0.12.9/docs/Text-Parser-Char.html#v:char" rel="nofollow noreferrer"><code>char</code></a> parsers and <a href="https://hackage.haskell.org/package/trifecta" rel="nofollow noreferrer">Trifecta</a>:</p>
  
  <pre><code>import           Text.Trifecta
  import           Control.Applicative            ( pure )
  
  stringParserWithChar :: String -&gt; Parser Char
  stringParserWithChar stringToParse =
    foldr (\c otherParser -&gt; otherParser &gt;&gt; char c) identityParser
      $ reverse stringToParse
    where identityParser = pure '?' -- ← This works but I think I can do better
  </code></pre>
  
  <p>The parser does its job just fine:</p>
  
  <pre><code>parseString (stringParserWithChar "123") mempty "1234"
  -- Yields: Success '3'
  </code></pre>
  
  <p>Yet, I'm not happy with the specific <code>identityParser</code> to which I applied <code>foldr</code>. It seems hacky to have to choose an arbitrary character for <code>pure</code>.</p>
  
  <p>My first intuition was to use <code>mempty</code> but <a href="https://hackage.haskell.org/package/trifecta-2/docs/Text-Trifecta-Parser.html" rel="nofollow noreferrer"><code>Parser</code></a> is not a monoid. It <em>is</em> an applicative but <code>empty</code> constitutes an unsuccessful parser².</p>
  
  <p>What I'm looking for instead is a parser that works as a neutral element when combined with other parsers. It should successfully do nothing, i.e., not advance the cursor and let the next parser consume the character.</p>
  
  <p>Is there an identity parser as described above in Trifecta or in another library? Or are parsers not meant to be used in a <code>fold</code>?</p>
  
  <hr>
  
  <p>¹ The exercise is from the parser combinators chapter of the book <a href="http://haskellbook.com/" rel="nofollow noreferrer">Haskell Programming from first principles</a>.</p>
  
  <p>² As helpfully <a href="https://stackoverflow.com/questions/57042184/identity-parser/57044340#57044340">pointed out by cole</a>, <code>Parser</code> is an <code>Alternative</code> and thus a monoid. The <code>empty</code> function stems from <code>Alternative</code>, not <code>Parser</code>'s applicative instance.</p>