Note that there are some explanatory texts on larger screens.

plurals
  1. PODefining PrimMonad instance for STT? (ST Transformer)
    primarykey
    data
    text
    <p><code>Data.Vector.Mutable</code> seems to require an instance of <code>PrimMonad</code> in <code>ST s</code> and <code>IO</code> monads.</p> <p>The typeclass is defined as this --</p> <pre><code>-- | Class of primitive state-transformer monads class Monad m =&gt; PrimMonad m where -- | State token type type PrimState m -- | Execute a primitive operation primitive :: (State# (PrimState m) -&gt; (# State# (PrimState m), a #)) -&gt; m a -- | Expose the internal structure of the monad internal :: m a -&gt; State# (PrimState m) -&gt; (# State# (PrimState m), a #) </code></pre> <p>They're implemented like this --</p> <pre><code>instance PrimMonad IO where type PrimState IO = RealWorld primitive = IO internal (IO p) = p instance PrimMonad (ST s) where type PrimState (ST s) = s primitive = ST internal (ST p) = p </code></pre> <p>I don't really understand at all what any of the functions of the typeclass are supposed to do, or how the implementations work.</p> <p>But I need to implement it for STT (the one given by <a href="http://hackage.haskell.org/package/STMonadTrans-0.3.1" rel="nofollow">http://hackage.haskell.org/package/STMonadTrans-0.3.1</a>)</p> <p><code>STT</code> has constructor <code>STT s m a</code></p> <p>In my naive attempt I tried replacing everything <code>ST s</code> with <code>STT s m</code>:</p> <pre><code>instance Monad m =&gt; PrimMonad (STT s m) where type PrimState (STT s m) = s primitive = STT internal (STT p m) = p </code></pre> <p>but I get this error:</p> <pre><code>Not in scope: data constructor `STT' </code></pre> <p>for the definitions of <code>primitive</code> and <code>internal</code>, despite having used <code>STT</code> multiple times throughout the program already (although I guess as a type constructor?).</p> <p>How should I properly implement this typeclass?</p> <p>(I will eventually be using this as <code>STT s (Rand g) a</code>)</p> <hr> <p>EDIT: I imported <code>Control.Monad.ST.Trans.Internal</code> to get <code>STT</code> as a data constructor, and these are the new errors: (after changing <code>internal (STT s m)</code> to <code>internal (STT s)</code>)</p> <pre><code>Couldn't match kind `*' against `ArgKind' Kind incompatibility when matching types: m0 :: * -&gt; * (#,#) (ghc-prim:GHC.Prim.State# (PrimState (STT s m))) :: ArgKind -&gt; (#) In the expression: STT In an equation for `primitive': primitive = STT Couldn't match type `m' with `(#,#) (ghc-prim:GHC.Prim.State# (PrimState (STT s m)))' `m' is a rigid type variable bound by the instance declaration at src/pimc/PIMC.hs:41:16 Expected type: ghc-prim:GHC.Prim.State# (PrimState (STT s m)) -&gt; (# ghc-prim:GHC.Prim.State# (PrimState (STT s m)), a #) Actual type: ghc-prim:GHC.Prim.State# s -&gt; m (STTRet s a) In the expression: p In an equation for `internal': internal (STT p) = p Couldn't match type `a' with `STTRet s a' `a' is a rigid type variable bound by the type signature for internal :: STT s m a -&gt; ghc-prim:GHC.Prim.State# (PrimState (STT s m)) -&gt; (# ghc-prim:GHC.Prim.State# (PrimState (STT s m)), a #) at src/pimc/PIMC.hs:44:3 Expected type: ghc-prim:GHC.Prim.State# (PrimState (STT s m)) -&gt; (# ghc-prim:GHC.Prim.State# (PrimState (STT s m)), a #) Actual type: ghc-prim:GHC.Prim.State# s -&gt; m (STTRet s a) In the expression: p In an equation for `internal': internal (STT p) = p </code></pre>
    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.
 

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