Note that there are some explanatory texts on larger screens.

plurals
  1. POParameterized Maybe monad
    primarykey
    data
    text
    <p>I'm making a mess with Monad, ReaderT, ... to perform a "simple?" behavior.</p> <p>I want to intersperse a test function into <code>Maybe</code> transformation (<code>Maybe</code> or another personalized monad).</p> <p>Exactly, I want avoid <code>t</code> calls, creating some kind of monad (monad, I think) on</p> <pre><code>doCalculus :: (Int -&gt; Bool) -&gt; Int -&gt; Maybe Int doCalculus f a = do b &lt;- t $ a + 1 c &lt;- t $ 2 * b d &lt;- t $ a + b + c return d where t = if f n then Just n else Nothing </code></pre> <p>example</p> <pre><code>test :: Int -&gt; Bool test n = not (n `elem` [3, 7, 9]) *Main&gt; doCalculus test 2 Nothing *Main&gt; doCalculus test 3 Just 15 *Main&gt; </code></pre> <p>I'm trying to do some monad like <code>ReaderT</code> to perform some like</p> <pre><code>runMaybeTest doCalculus test </code></pre> <p>to use as</p> <pre><code>doCalculus :: Int -&gt; Maybe' Int doCalculus a = do b &lt;- a + 1 c &lt;- 2 * b d &lt;- a + b + c return d perform = runMaybe' doCalculus test </code></pre> <p>but I can't.</p> <p>(Of course, <code>Int</code> type would be generic into monad)</p> <p>Thank you for any tip!</p> <p><strong>=== UPDATE 1 ===</strong></p> <p>I can do it! :) ... but isn't practical (I think) :(</p> <p>I had adapted a fantastic <a href="http://www.randomhacks.net/articles/2007/03/15/data-set-monad-haskell-macros" rel="nofollow">Eric Kidd post</a></p> <pre><code>import Prelude hiding (Just, Nothing, return, (&gt;&gt;=)) class Tester a where test :: a -&gt; Bool test _ = True data MMaybe a = Nothing | Just a deriving (Eq, Show) class Monad1 m a where return :: a -&gt; m a fail :: String -&gt; m a class (Monad1 m a, Monad1 m b) =&gt; Monad2 m a b where (&gt;&gt;=) :: m a -&gt; (a -&gt; m b) -&gt; m b instance (Tester a) =&gt; Monad1 MMaybe a where return = Just fail _ = Nothing instance (Tester a, Tester b) =&gt; Monad2 MMaybe a b where Nothing &gt;&gt;= _ = Nothing (Just x) &gt;&gt;= f = if test x then f x else Nothing instance Tester Int where test n = not $ n `mod` 2 == 0 &amp;&amp; n `mod` 3 == 0 test1 :: Int -&gt; MMaybe Int test1 n = return n &gt;&gt;= \a -&gt; return (a + 3) &gt;&gt;= \b -&gt; return (a + b) test2 = map test1 [1..20] </code></pre> <p>possible (important) problems are:</p> <ol> <li>is a usable monad?</li> <li>where is do notation?</li> <li>only works defining test function into a unique type (new testing functions require new types)</li> </ol> <p>but I can wrap test function into pseudo-monad... (it's something)</p>
    singulars
    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.
 

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