- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Data.Vector.Mutable
似乎需要 ST
和 IO
monad 中的 PrimMonad
实例。
类型类定义如下 --
-- | 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
和internal
的定义,尽管已经在整个程序中多次使用了STT
(尽管我猜作为类型构造函数) ?)。
我应该如何正确实现这个类型类?
(我最终将使用它作为STT s (Rand g) a
)
编辑:我导入了 Control.Monad.ST.Trans.Internal
以获取 STT
作为数据构造函数,这些是新错误:(更改 内部(STT s m)
到内部(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 等。
您可以尝试两件事:
顺便说一句,以下原语实现应该可以工作(比较 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"
关于haskell - 为 STT 定义 PrimMonad 实例? (意法半导体变压器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17522198/
这个问题显然与讨论的问题here相关。和 here 。不幸的是,我的要求与这些问题略有不同,并且给出的答案不适用于我。我也不太明白为什么 runST 在这些情况下无法进行类型检查,这没有帮助。 我的问
有什么方法可以隐藏在android中使用语音转文本时显示的对话框吗?我在某处读到这是不可能的,但肯定有一种方法至少可以显示它然后立即隐藏它? 也许有某种方法可以对某些东西进行子类化并改变它的外观?在我
我正在使用 Cloud Functions 从存储桶中的 getUserMedia() 转换音频/mp4 使用 ffmpeg 转换为 audio/x-flac 格式,以便能够使用 Google STT
文字转语音 我一直在尝试在 windows 和 Linux 环境中运行 pyttsx... Linux 环境: import pyttsx engine = pyttsx.init() python
Data.Vector.Mutable 似乎需要 ST 和 IO monad 中的 PrimMonad 实例。 类型类定义如下 -- -- | Class of primitive state-tra
无法解析 MediaType AUDIO_WEBM。我错过了什么吗?我收到以下错误。 [ERROR] /C:/Users/IBM_ADMIN/workspace/ListenApp/src/main/
我之前让这段代码在我的设备上运行良好,但现在它根本无法运行。设备是三星 SM-G900P(Android 6.0.2,API23)。 我必须自己构建语音转文本库、核心库和文本转语音库,因为我的应用程序
我当前正在尝试将麦克风流发送到 Watson STT 服务,但由于某种原因,Watson 服务未接收该流(我猜测),因此我收到错误“错误:30 秒内未检测到语音”。 请注意,我已将 .wav 文件传输
我在沙盒中安装来自 Hackage 的最新 ghc-mod (5.2.1.1) 时遇到此错误: [15 of 38] Compiling Language.Haskell.GhcMod.CabalCo
我正在尝试构建一个采用流式音频输入(例如:麦克风中的线路)并使用 IBM Bluemix (Watson) 进行语音转文本的应用。 我简要修改了找到的示例 Java 代码 here .此示例发送的是
我是一名优秀的程序员,十分优秀!