Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Use liftIO</strong></p> <p>You're already very close! Your suggestion</p> <pre><code>newtype Op a = Op {runOp :: ST -&gt; IO (ST, a)} </code></pre> <p>is excellent and the way to go.</p> <p>To be able to execute <code>getLine</code> in an <code>Op</code> context, you need to 'lift' the <code>IO</code> operation into the <code>Op</code> monad. You can do this by writing a function <code>liftIO</code>:</p> <pre><code>liftIO :: IO a -&gt; Op a liftIO io = Op $ \st -&gt; do x &lt;- io return (st, x) </code></pre> <p>You can now write: </p> <pre><code>runOp (do x &lt;- liftIO getLine; ... </code></pre> <p><strong>Use class MonadIO</strong></p> <p>Now the pattern of lifting an IO action into a custom monad is so common that there is a standard type class for it:</p> <pre><code>import Control.Monad.Trans class Monad m =&gt; MonadIO m where liftIO :: IO a -&gt; m a </code></pre> <p>So that your version of <code>liftIO</code> becomes an instance of <code>MonadIO</code> instead:</p> <pre><code>instance MonadIO Op where liftIO = ... </code></pre> <p><strong>Use StateT</strong></p> <p>You've currently written your own version of the state monad, specialised to state <code>ST</code>. Why don't you use the standard state monad? It saves you from having to write your own <code>Monad</code> instance, which is always the same for the state monad.</p> <pre><code>type Op = StateT ST IO </code></pre> <p><code>StateT</code> already has a <code>Monad</code> instance and a <code>MonadIO</code> instance, so you can use those immediately.</p> <p><strong>Monad transformers</strong></p> <p><code>StateT</code> is a so-called <em>monad transformer</em>. You only want <code>IO</code> actions in your <code>Op</code> monad, so I've already specialized it with the <code>IO</code> monad for you (see the definition of <code>type Op</code>). But monad transformers allow you to stack arbitrary monads. This what intoverflow is talking about. You can read more about them <a href="http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html" rel="noreferrer">here</a> and <a href="http://en.wikibooks.org/wiki/Haskell/Monad_transformers" rel="noreferrer">here</a>.</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. 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