Data.Vector.Mutable似乎需要PrimMonadST s monad中的IO实例。

类型类定义为-

-- | Class of primitive state-transformer monads
class Monad m => PrimMonad m where
  -- | State token type
  type PrimState m

  -- | Execute a primitive operation
  primitive :: (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a

  -- | Expose the internal structure of the monad
  internal :: m a -> State# (PrimState m) -> (# State# (PrimState m), a #)

它们是这样实现的
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

我根本不了解类型类的任何功能应该做什么,或者实现如何工作。

但是我需要为STT实现它(http://hackage.haskell.org/package/STMonadTrans-0.3.1给出的那个)
STT具有构造函数STT s m a
在天真的尝试中,我尝试将所有ST s替换为STT s m:
instance Monad m => PrimMonad (STT s m) where
  type PrimState (STT s m) = s
  primitive = STT
  internal (STT p m) = p

但是我得到这个错误:
Not in scope: data constructor `STT'

尽管已经在整个程序中多次使用了primitive(尽管我想作为类型构造函数?),但对于internalSTT的定义。

我应该如何正确实现此类型类?

(我最终将使用它作为STT s (Rand g) a)

编辑:我导入Control.Monad.ST.Trans.Internal以获得STT作为数据构造函数,这些是新的错误:(将internal (STT s m)更改为internal (STT s)之后)
Couldn't match kind `*' against `ArgKind'
Kind incompatibility when matching types:
  m0 :: * -> *
  (#,#) (ghc-prim:GHC.Prim.State# (PrimState (STT s m))) :: ArgKind
                                                            -> (#)
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))
               -> (# ghc-prim:GHC.Prim.State# (PrimState (STT s m)), a #)
  Actual type: ghc-prim:GHC.Prim.State# s -> 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
                    -> ghc-prim:GHC.Prim.State# (PrimState (STT s m))
                    -> (# 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))
               -> (# ghc-prim:GHC.Prim.State# (PrimState (STT s m)), a #)
  Actual type: ghc-prim:GHC.Prim.State# s -> m (STTRet s a)
In the expression: p
In an equation for `internal': internal (STT p) = p

最佳答案

原则上,您可能可以为基本类型编写一个实现,但不能为内部编写实现,因为它为您的monad施加了某种结构,只有IO和ST monad的内部实现才能满足该结构。

但是,我的印象是问题出在Data.Vector.Mutable模块上,该模块的要求过于严格。例如,要在单子(monad)m中使用IO vector ,原则上只需要将IO嵌入到m中(即原始方法),反之亦然(即内部方法)。如果正确,则应尝试将PrimMonad类细分为嵌入部分和同构部分,例如,如下所示:

-- | Class of primitive state-transformer monads
class Monad m => PrimMonadEmbed m where
  -- | State token type
  type PrimState m

  -- | Execute a primitive operation
  primitive :: (State# (PrimState m) -> (# State# (PrimState m), a #)) -> m a

class PrimMonadEmbed m => PrimMonad m where
  -- | Expose the internal structure of the monad
  internal :: m a -> State# (PrimState m) -> (# State# (PrimState m), a #)

就向后兼容性而言,这可能是可以的,因为用户代码中不能存在任何自定义实例。降低要求的严格性还将使它们的代码可用于IO monad的转换版本,例如StateT Int IO等。

您可以尝试两件事:
  • 实现PrimMonad的部分实例,该实例仅实现基本方法并内部抛出错误,并查看是否可以使用像
  • 这样的 vector 库
  • 与您使用的 vector 库的作者联系,并询问他们上述建议是否现实...

  • 顺便说一句,下面的原语实现应该可以工作(比较STT的源代码以了解代码中出了什么问题):
    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE UnboxedTuples #-}
    
    module Test where
    
    import Control.Monad.Primitive
    import Control.Monad.ST.Trans
    import Control.Monad.ST.Trans.Internal
    
    instance Monad m => PrimMonad (STT s m) where
      type PrimState (STT s m) = s
      primitive f = STT (\s -> case (f s) of (# s', v #) -> return (STTRet s' v))
      internal _ = error "no implementation of internal for STT"
    

    08-28 06:28