[英]GHC 7.10.x migration: why can't one write “pure = return” in the instance Applicative?
我正在閱讀一篇關於GHC 7.10.x Migration的文章。 有一些修復錯誤的建議。
GHC說
No instance for (Applicative ...)
如果GHC抱怨這一點
Foo.hs:7:10:沒有實例聲明的超類引起的(Applicative Foo)實例在'Monad Foo'的實例聲明中,修復此錯誤的一種簡單方法是定義一個Applicative(可能還有一個Functor) )instance:instance Functor Foo,其中fmap = liftM - 或者: - fmap = m >> = pure。 F
instance Applicative Foo where -- NB: DO NOT USE `pure = return` pure = {- move the definition of `return` from the `Monad` instance here -} (<*>) = ap {- defined in Control.Monad -} -- or alternatively: -- f1 <*> f2 = f1 >>= \\v1 -> f2 >>= (pure . v1) -- NB: DO NOT USE `(*>) = (>>)` (*>) = {- move the definition of `>>` from the `Monad` instance here -} instance Monad Foo where return = pure {- definition moved to `Applicative(pure)` -} (>>) = (*>) {- definition moved to `Applicative((*>))` -} {- ...retain other previous definitions... -}
有NB: DO NOT USE `pure = return`
而DO NOT USE `(*>) = (>>)
。 為什么不能使用它?
PS我試圖使用它,它已被編譯。
正如@WillSewell在評論中暗示的那樣,從base-4.8.0.0
現在return
類型類Monad
有一個默認實現:
class Applicative m => Monad m where
return :: a -> m a
return = pure
通過定義pure = return
並忘記手動實現return
,可以創建一個無限循環的定義,它將傳遞編譯並且只能在運行時檢測到。
>>
現在是一個不同的故事。 它有一個默認的實現繼電器>>=
only:
(>>) :: forall a b. m a -> m b -> m b
m >> k = m >>= \_ -> k
並且通過*> = >>
你將不會創建一個無限循環,除非你在>>=
的定義中使用*>
,但是有一些關注 base
可能的下一個主要變化(這可能會改變>>
的默認實現) >>
to >> = *>
)因此*> = >>
不鼓勵前向兼容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.