[英]Haskell MonadWriter type signature
Noob question on MonadWriter: 关于MonadWriter的Noob问题:
monadWrite :: Writer String Int
monadWrite = writer (3003, "log 1\n")
Why is it that String
comes first in the typesig and Int
second, while 3003
is clearly an Int
while "log 1\\n"
is a String
. 为什么
String
在typesig和Int
second中排在第一位,而3003
显然是Int
而"log 1\\n"
是String
。 Trivial I know, but I would like to understand. 琐事,我知道,但我想明白。
There is no particular reason to put the result ( 3003
) first and the output ( "log 1\\n"
) second in the argument to writer
. 没有特别的理由将结果(
3003
)放在第一位,将输出( "log 1\\n"
)放在参数的第二位给writer
。 The order was, I suppose, chosen to correspond to the internal representation of WriterT
: 我想,订单选择与
WriterT
的内部表示相对WriterT
:
newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }
(for Writer
, m
is identity). (对于
Writer
, m
是身份)。
However, in the type signature for Writer
, the order of arguments matters. 但是,在
Writer
的类型签名中,参数的顺序很重要。 If we look at, for example, the Functor
typeclass with a member 例如,如果我们查看带有成员的
Functor
类型类
fmap :: (a -> b) -> f a -> f b
it makes it possible to substitute Writer String
(or generally, Writer result
) for f
and obtain 它使得可以用
f
替换Writer String
(或者通常是Writer result
)并获得
fmap :: (a -> b) -> Writer result a -> Writer result b
which is exactly the correct order of arguments. 这正是参数的正确顺序。 Swapping them would make implementing
Functor
impossible (without some trickery). 交换它们会使
Functor
不可能实现(没有一些技巧)。
This is true for all types/functions which take more than one parameter: the only way they can be used as one-argument types/functions is by varying the last argument, not the other ones. 对于所有带有多个参数的类型/函数都是如此:它们可以用作单参数类型/函数的唯一方法是改变最后一个参数,而不是其他参数。
See related questions discussing similar issues: 查看讨论类似问题的相关问题:
Switch order of arguments for instance declaration in Haskell 切换Haskell中实例声明的参数顺序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.