title: Error detection and reporting using Maybe
body: <p>I am writing a propositional logic parser in Haskell. I am doing the parsing by hand for now as a learning exercise. Eventually I will tackle Parsec. In the mean time, I am trying to wrap my head around Monads. In particular, I am using <code>Maybe</code> to report errors from my <code>parse</code> function. My current trouble is with part of a helper function:</p>
  
  <pre><code>parse' :: String -&gt; (Maybe Wff, String)
  parse' ('[':rest) = (x, if null rest''
                          then ""
                          else tail rest'')
          where (a, rest') = parse' rest
                (b, rest'') = parse' (if null rest'
                                      then ""
                                      else tail rest')
                x = if null rest'
                       || null rest''
                       || head rest' /= '|'
                       || head rest'' /= ']'
                    then Nothing
                    else Or &lt;$&gt; a &lt;*&gt; b
  </code></pre>
  
  <p>(For reference, the full <code>parse</code> function can be found <a href="https://github.com/codeguru42/theorem/blob/2efca7ec42752e64ac60377f0e815d99bd695e14/Wff.hs" rel="nofollow">here</a>.)</p>
  
  <p>This code parses a proposition of the form <code>[ A | B ]</code> where <code>A</code> and <code>B</code> are any arbitrary propositions. As you can see, I am using applicative style on the last line to propagate the <code>Nothing</code> result if a previous recursive call results in a <code>Nothing</code>. This allowed me to take out <code>a == Nothing</code> and <code>b == Nothing</code> from the <code>if</code> condition. How can I use the <code>Applicative</code> or <code>Monad</code> instance of <code>Maybe</code> to colapse the rest of this <code>if</code>?</p>