title: How to signal failure in trifecta parser
body: <p>As an experiment with trifecta I've written the following simple function:</p>
  
  <pre><code>filterParser :: (a -&gt; Bool) -&gt; Parser a -&gt; Parser a
  filterParser cond p = do
    a &lt;- p
    if cond a 
      then return a 
      else unexpected "condition failed!"
  </code></pre>
  
  <p>The idea was to be able to add a condition to a parser. For example (assuming predicate <code>prime</code> already exists), you would write:
  <code>filterParser prime integer</code> to create a parser that only accepts prime numbers.</p>
  
  <hr>
  
  <p>With single parses it seems OK:</p>
  
  <pre><code>&gt; parseString (filterParser (&gt; 'm') letter) mempty "z"
  &gt; Success 'z
  
  &gt; parseString (filterParser (&gt; 'm') letter) mempty "a"
  &gt; Failure (interactive):1:2: error: unexpected
  &gt; condition failed!
  </code></pre>
  
  <hr>
  
  <p>But with 'many' it doesn't work - compare:</p>
  
  <pre><code>&gt; parseString (many $ filterParser (&gt; 'm') letter) mempty "zzz2"
  &gt; Success "zzz"
  
  &gt; parseString (many $ filterParser (&gt; 'm') letter) mempty "zzza"
  &gt; Failure (interactive):1:5: error: unexpected
  &gt; condition failed!
  </code></pre>
  
  <p>I was hoping the last example would also return <code>Success "zzz"</code>. The call to <code>unexpected</code> seems to de-rail the entire parse, which isn't what I wanted.</p>