Haskell“永远”式签名 [英] Haskell `forever` type signature
问题描述
在Haskell为什么的类型签名永远
forever :: Monad m => m a - > m b
特别是为什么它不是 :: Monad m => m a - > m a
?
当然,我们所采取的monad类型在永远不会改变?
一个函数如:
forever':: Monad m => m a - > ma
永远'=永远
似乎工作完全一样。
永远的类型签名旨在表明它的结果永远运行。具体而言,无法编写类型为 m b
(其返回值为多态)的操作,该操作终止并生成非底部值。建议同样的事情的另一种方式是 forever':: m a - > m空格
。
另一个答案是只是说它是定义的函数中最常用的类型,所以这就是它被给出。
Prelude>让永远m =令x = m>> x in x
Prelude> :永远永远
永远:: Monad m => m a - > mb
现在,它应该有不同的定义:
forever :: Applicative f => f a - > f b
forever a = let x = a *> x in x
In Haskell why is type-signature of forever
forever :: Monad m => m a -> m b
Specifically why isn't it just :: Monad m => m a -> m a
?
Surely the type of monad we are acting upon doesn't change half way through forever
?
A function such as:
forever' :: Monad m => m a -> m a
forever' = forever
seems to work exactly the same.
The type signature of forever
is crafted to suggest that its result runs forever. Specifically, there is no way to write an action of type m b
(polymorphic in its return value) that terminates and yields a non-bottom value. An alternative way to suggest the same thing would be forever' :: m a -> m Void
.
Another answer is to just say that this is the most general type available for the function as it's defined, so that's the one it was given.
Prelude> let forever m = let x = m >> x in x
Prelude> :t forever
forever :: Monad m => m a -> m b
These days, it probably should be defined differently:
forever :: Applicative f => f a -> f b
forever a = let x = a *> x in x
这篇关于Haskell“永远”式签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!