Note that there are some explanatory texts on larger screens.

plurals
  1. POType error while trying to implement the (>>=) function in order to create a custom monad transformer
    primarykey
    data
    text
    <p>I'm trying to create a monad transformer for a future project, but unfortunately, my implementation of the Monad typeclasse's (>>=) function doesn't work.</p> <p>First of all, here is the underlying monad's implementation : </p> <pre><code>newtype Runtime a = R { unR :: State EInfo a } deriving (Monad) </code></pre> <p>Here, the implementation of the Monad typeclasse is done automatically by GHC (using the <code>GeneralizedNewtypeDeriving</code> language pragma). The monad transformer is defined as so : </p> <pre><code>newtype RuntimeT m a = RuntimeT { runRuntimeT :: m (Runtime a) } </code></pre> <p>The problem comes from the way I instanciate the (>>=) function of the Monad typeclasse :</p> <pre><code>instance (Monad m) =&gt; Monad (RuntimeT m) where return a = RuntimeT $ (return . return) a x &gt;&gt;= f = runRuntimeT x &gt;&gt;= id &gt;&gt;= f </code></pre> <p>The way I see it, the first <code>&gt;&gt;=</code> runs in the underlying <code>m</code> monad. Thus, <code>runRuntimeT x &gt;&gt;=</code> returns a value of type <code>Runtime a</code> (right ?). Then, the following code, <code>id &gt;&gt;=</code>, should return a value of type <code>a</code>. This value is the passed on to the function f of type <code>f :: (Monad m) =&gt; a -&gt; RuntimeT m b</code>. </p> <p>And here comes the type problem : the <code>f</code> function's type doesn't match the type required by the (>>=) function. Jow can I make this coherent ? I can see why this doesn't work, but I can't manage to turn it into something functionnal.</p> <p>Edit : The error message : </p> <pre><code>Core.hs:34:4: Occurs check: cannot construct the infinite type: m = RuntimeT m When generalising the type(s) for `&gt;&gt;=' In the instance declaration for `Monad (RuntimeT m)' Failed, modules loaded: none. </code></pre> <p>Thank you for you help, and do not hesitate to correct any flaws in my message,<br> Charlie P.</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