我刚刚开始使用netwire,我在基本知识方面遇到了困难。
下面的代码对我来说很好:
main :: IO ()
main = testWire clockSession_ (for 3 . yeah)
yeah :: Monad m => Wire s () m a String
yeah = pure "yes"但这并不意味着:
main :: IO ()
main = testWire clockSession_ forYeah
forYeah :: (Show b, Show e) => Wire s e Identity a b
forYeah = for 3 . yeah失败时出错:
Could not deduce (b ~ [Char])
from the context (Show b, Show e)
bound by the type signature for
forYeah :: (Show b, Show e) => Wire s e Identity a b
at /home/fiendfan1/workspace/Haskell/Haskell-OpenGL/src/Main.hs:12:12-54
`b' is a rigid type variable bound by
the type signature for
forYeah :: (Show b, Show e) => Wire s e Identity a b
at /home/fiendfan1/workspace/Haskell/Haskell-OpenGL/src/Main.hs:12:12
Expected type: Wire s e Identity a b
Actual type: Wire s () Identity a String
In the second argument of `(.)', namely `yeah'
In the expression: for 3 . yeah
In an equation for `forYeah': forYeah = for 3 . yeah所以我把它改成:
forYeah :: Show e => Wire s e Identity a String这给了我一个错误:
Could not deduce (e ~ ())
from the context (Show e)
bound by the type signature for
forYeah :: Show e => Wire s e Identity a String
at /home/fiendfan1/workspace/Haskell/Haskell-OpenGL/src/Main.hs:12:12-49
`e' is a rigid type variable bound by
the type signature for
forYeah :: Show e => Wire s e Identity a String
at /home/fiendfan1/workspace/Haskell/Haskell-OpenGL/src/Main.hs:12:12
Expected type: Wire s e Identity a String
Actual type: Wire s () Identity a String
In the second argument of `(.)', namely `yeah'
In the expression: for 3 . yeah
In an equation for `forYeah': forYeah = for 3 . yeah改为:
forYeah :: Wire s () Identity a String给出以下错误:
No instance for (HasTime Integer s) arising from a use of `for'
Possible fix: add an instance declaration for (HasTime Integer s)
In the first argument of `(.)', namely `for 3'
In the expression: for 3 . yeah
In an equation for `forYeah': forYeah = for 3 . yeah有人能解释为什么会发生这种情况,以及我如何修复我的第二个代码示例吗?
发布于 2013-12-02 21:32:19
编辑:这里有一个完整的、编译的、运行的解决方案来解决这个问题:
module Main (
main
) where
import Prelude hiding ((.), id)
import qualified Prelude as Prelude
import Control.Wire
import Control.Wire.Interval
main :: IO ()
main = testWire clockSession_ (withoutErrors forYeah)
yeah :: Monad m => Wire s e m a String
yeah = pure "yes"
forYeah :: (Num t, HasTime t s, Monoid e, Monad m) => Wire s e m a String
forYeah = for 3 . yeah
-- This just is an easy way to specify to use () as the type for errors in testWire
withoutErrors :: Wire s () m a b -> Wire s () m a b
withoutErrors = Prelude.id这是最初的答案,讨论了为什么我们应该更改yeah的类型,然后对forYeah的类型进行必要的更改
将yeah的类型更改为Monad m => Wire s e m a String。Monad m => (Wire s e m a)有一个Applicative实例,所以pure应该存在,而不指定yeah类型中Wire的第二个类型参数是()。
注意:我不使用netwire,也没有尝试编译这个。我只看过文档中的类型。
编辑:,您可能还需要更改forYeah的类型。
Wire还有一个Category实例:
Monad m => Category (Wire s e m)Category的.操作符有以下类型:
(.) :: cat b c -> cat a b -> cat a c因此,对于Wire来说,它是:
(.) :: Monad m => Wire s e m b c -> Wire s e m a b -> Wire s e m a cfor具有以下类型:
for :: (HasTime t s, Monoid e) => t -> Wire s e m a a所以for 3会有一个类似于(HasTime Int s, Monoid e) => Wire s e m a a的类型。再加上‘s的Monad m => Wire s e m a String类型,for 3 . yeah的类型如下
(HasTime Int s, Monoid e, Monad m) => Wire s e m a String因此,我们可能可以将forYeah的类型更改为:
forYeah :: (HasTime Int s, Monoid e, Monad m) => Wire s e m a String编辑:更适合forYeah
由于整数(没有小数点)实际上等价于fromInteger的应用程序,其值为整数。和fromInteger :: (Num a) => Integer -> a,文字3实际上具有Num t => t类型。因此,我们可以选择的最佳类型可能是:
forYeah :: (Num t, HasTime t s, Monoid e, Monad m) => Wire s e m a String发布于 2013-12-02 21:33:33
当我将forYeah类型更改为
forYeah::Wire (Timed NominalDiffTime ()) () Identity a String如果省略了forYeah的类型,它也会起作用。
发布于 2013-12-02 21:48:46
我只是问GHCi这种类型是什么:
> :m Control.Wire
> :t for (3 :: Int) . pure "yes"
for 3 . pure "yes" :: (Monad m, HasTime Int s, Monoid e) => Wire s e m a [Char]
> :{
| let forYeah :: HasTime Int s => Wire s () Identity a String
| forYeah = for 3 . pure "yes"
| :}
> :t forYeah
forYeah :: HasTime Int s => Wire s () Identity a String这样就行了。但是,当询问testWire clockSession_ forYeah的类型时,我会看到一个错误,它不能将NominalDiffTime与Int相匹配,但是由于NominalDiffTime也是Num的一个实例,所以只需更改签名就相当容易了:
> :{
| let forYeah :: HasTime NominalDiffTime s => Wire s () Identity a String
| forYeah = for 3 . pure "yes"
| :}
> :t testWire clockSession_ forYeah
testWire clockSession_ forYeah :: (Applicative m, MonadIO m) => m c所以这看起来很管用。
而且,当我将yeah分别定义为
yeah :: Monad m => Wire s () m a String
yeah = pure "yes"
forYeah :: HasTime t s => Wire s () Identity a String
forYeah = for 3 . yeah问题似乎在HasTime约束中。由于您忽略了它,编译器将文本3默认为Integer类型,但是没有任何s的HasTime Integer s实例。
https://stackoverflow.com/questions/20337611
复制相似问题