fun*_*ial 5 haskell functional-programming frp netwire
我刚刚开始使用netwire,我遇到了很多基础问题.
以下代码对我来说很好:
main :: IO ()
main = testWire clockSession_ (for 3 . yeah)
yeah :: Monad m => Wire s () m a String
yeah = pure "yes"
Run Code Online (Sandbox Code Playgroud)
但这不是:
main :: IO ()
main = testWire clockSession_ forYeah
forYeah :: (Show b, Show e) => Wire s e Identity a b
forYeah = for 3 . yeah
Run Code Online (Sandbox Code Playgroud)
失败并出错:
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
Run Code Online (Sandbox Code Playgroud)
所以我改成了:
forYeah :: Show e => Wire s e Identity a String
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
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
Run Code Online (Sandbox Code Playgroud)
将其更改为:
forYeah :: Wire s () Identity a String
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
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
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会发生这种情况以及如何修复我的第二个代码示例?
编辑:这是此问题的完整、可编译、可运行的解决方案:
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
Run Code Online (Sandbox Code Playgroud)
这是最初的答案,讨论了为什么我们应该更改 的类型yeah
,然后对 的类型进行必要的更改forYeah
:
将 的类型更改yeah
为Monad m => Wire s e m a String
。Monad m => (Wire s e m a)
有一个Applicative
实例,因此应该存在,而无需指定in类型pure
的第二个类型参数是。Wire
yeah
()
注意:我不使用 netwire,也没有尝试编译它。我只查看了文档中的类型。
编辑:您可能还需要更改forYeah
.
Wire
还有一个Category
实例:
Monad m => Category (Wire s e m)
Run Code Online (Sandbox Code Playgroud)
Category
的.
运算符具有以下类型:
(.) :: cat b c -> cat a b -> cat a c
Run Code Online (Sandbox Code Playgroud)
所以对于Wire
s 来说是:
(.) :: Monad m => Wire s e m b c -> Wire s e m a b -> Wire s e m a c
Run Code Online (Sandbox Code Playgroud)
for
有以下类型:
for :: (HasTime t s, Monoid e) => t -> Wire s e m a a
Run Code Online (Sandbox Code Playgroud)
所以for 3
会有像这样的类型(HasTime Int s, Monoid e) => Wire s e m a a
。与 yes 的类型结合Monad m => Wire s e m a String
,for 3 . yeah
会得到类似的类型
(HasTime Int s, Monoid e, Monad m) => Wire s e m a String
Run Code Online (Sandbox Code Playgroud)
所以我们可能可以将类型更改forYeah
为:
forYeah :: (HasTime Int s, Monoid e, Monad m) => Wire s e m a String
Run Code Online (Sandbox Code Playgroud)
编辑:更好的类型forYeah
由于整数(不带小数点)实际上相当于将 fromInteger 应用于数字值作为 Integer , and 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
Run Code Online (Sandbox Code Playgroud)