Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To do error handling, you don't necessary need the <code>Either(T)</code> monad. You can be perfectly fine by staying in <code>Applicative</code> via composition. Example (for fun using <code>AccValidation</code> which accumulates all errors):</p> <pre><code>import Control.Applicative import Control.Monad.Error import Data.Validation import Data.Bifoldable import Data.Functor.Compose -- Replicating OP example with a Dummy monad (Get is made Applicative in newer libs) data Dummy a = D a deriving Show instance Monad Dummy where return = D (D x) &gt;&gt;= f = f x instance Functor Dummy where fmap f (D x) = D (f x) getM1 :: ErrorT String Dummy Int getM1 = lift (D 1) -- Can do with Applicatives + (Acc)Validation too instance Applicative Dummy where pure = return (&lt;*&gt;) = ap getA :: Compose Dummy (AccValidation String) Int getA = Compose $ D (success 1) getE :: Compose Dummy (AccValidation String) Int getE = Compose $ D (failure "bad") -- Applicative composition can work either way getA2 :: Compose (AccValidation String) Dummy Int getA2 = Compose $ success (D 1) getE2 :: Compose (AccValidation String) Dummy Int getE2 = Compose $ failure "bad" main = do runMonadic $ (+) &lt;$&gt; getM1 &lt;*&gt; getM1 -- D "2" -- runApplicative $ (+) &lt;$&gt; getA &lt;*&gt; getA -- D "2" runApplicative $ (+) &lt;$&gt; getE &lt;*&gt; getA -- D "bad" runApplicative $ (+) &lt;$&gt; getE &lt;*&gt; getE -- D "badbad" -- runOtherApp $ (+) &lt;$&gt; getA2 &lt;*&gt; getA2 -- "D 2" runOtherApp $ (+) &lt;$&gt; getE2 &lt;*&gt; getE2 -- "badbad" where runMonadic = print . fmap (either id show) . runErrorT runApplicative = print . fmap (validate id show) . getCompose runOtherApp = print . validate id show . getCompose -- some helper mimicking @either@ of @Either@ validate :: (e -&gt; c) -&gt; (a -&gt; c) -&gt; AccValidation e a -&gt; c validate f g = bifoldl (const f) (const g) undefined </code></pre>
 

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