Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've had some success with a rather horrible hand written monad that uses something like </p> <pre><code>newtype M r a = M { runM :: r -&gt; (# Bool, a #) } </code></pre> <p>where I treat the Bool like the Maybe constructor and in the Nothing case put an error in for 'a'. I usually use this when I have more structure (an environment e, state, logs, etc.), so I'm not sure how well it would pay off when it is this simple but the monad looks something like:</p> <pre><code>instance Monad (M r) where return a = M (\_ -&gt; (# True, a #)) M f &gt;&gt;= k = M (\r -&gt; case f r of (# True, a #) -&gt; runM (k a) r (# False, _ #) -&gt; (# False, undefined #)) fail _ = M (\_ -&gt; (# False, undefined #)) </code></pre> <p>This has the benefit that we don't construct any thing on the heap, just the stack.</p> <p>However, you need to be careful to be strict in all the right places. It is easy to accidentally build a thunk in your state which can kill your performance.</p> <p>If you are feeling daring you can smuggle an <code>unsafeCoerce</code>d error through in the 'a' slot on failure as well and extract it at the end, or you can just translate the <code>Bool</code> into a <code>Maybe e</code> but you need to be careful, because you don't want to build up a tower of unsafeCoerces defeating all the work you went through to get this far.</p> <p>The effectiveness of this approach depends on how much the overhead of building and tearing down Maybe frames is vs. the mental and execution time costs of distributing the code for dealing about failure across a lot of different places in the code. Note how >>= has to effectively unwind the Failure case manually.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload