Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a limitation, but one with good reason: if it did not work that way what would the expected semantics of </p> <pre><code>runState :: State t s a -&gt; s -&gt; (s,a) runState (State f) s = f s example :: s -&gt; a example = snd $ runState ((State undefined) &gt;&gt; return 1) () </code></pre> <p>well, it could be</p> <pre><code>example = snd $ runState ((State undefined) &gt;&gt;= \_ -&gt; return 1) () = snd $ runState (State $ \s -&gt; case undefined s of (s',_) -&gt; (s',1)) () = snd $ case undefined () of (s',_) -&gt; (s',1) = snd $ case undefined of (s',_) -&gt; (s',1) = snd undefined = undefined </code></pre> <p>or it could be</p> <pre><code>example = snd $ runState ((State undefined) &gt;&gt;= \_ -&gt; return 1) () = snd $ runState (State $ \s -&gt; case undefined s of ~(s',_) -&gt; (s',1)) () = snd $ case undefined () of ~(s',_) -&gt; (s',1) = snd $ (undefined,1) = 1 </code></pre> <p>these are not the same. One option would be to define a function an extra class, like</p> <pre><code>class IsStrictness t where bindState :: State t s a -&gt; (a -&gt; State t s b) -&gt; State t s b </code></pre> <p>and then then define </p> <pre><code>instance IsStrictness t =&gt; Monad (State t s) where return = returnState (&gt;&gt;=) = bindState </code></pre> <p>and instead of defining <code>bindState</code> as part of <code>IsStrictness</code>, you can use a singleton</p> <pre><code>data SingStrictness (t :: Strictness) where SingStrict :: SingStrictness Strict SingLazy :: SingStrictness Lazy class IsStrictness t where singStrictness :: SingStrictness t bindState :: IsStrictness t =&gt; State t s a -&gt; (a -&gt; State t s b) -&gt; State t s b bindState ma' amb' = go singStrictness ma' amb' where go :: SingStrictness t -&gt; State t s a -&gt; (a -&gt; State t s b) -&gt; State t s b go SingStrict ma amb = ... go SingLazy ma amb = ... </code></pre> <p>which can be enhanced even further using the singelton infrastructures from GHC 7.6 instead of the custom class and singleton type. You will end up with</p> <pre><code>instance SingI t =&gt; Monad (State t s) </code></pre> <p>which is really not so scary. Get used to having lots of <code>SingI _</code> in your constraint sets. This is how it is going to work for at least a while, and isn't that ugly.</p> <p>As to why <code>State t [Bool]</code> is not deducible from <code>State t s</code>: the problem is that <code>State t s</code> is in your top level context, which means that <code>s</code> is quantified at the outermost level. You are defining a function which says "for any t and s such that Monad (State t s) I will give you ...". But, this doesn't say "for any t such that Monad (State t [Bool]) I will give you ...". Sadly, these universally quantified constraints are not so easy in Haskell. </p>
 

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