[英]Why Nothing == (pure Nothing) returns False in Haskell?
似乎我理解錯了,但我嘗試了以下方法:
GHCi, version 8.6.5
Nothing == Nothing
=> True
Nothing == (pure Nothing)
=> False
pure Nothing
=> Nothing
你能解釋一下這里發生了什么嗎?
您代碼中的兩個pure Nothing
使用不同的pure
。
如果檢查pure Nothing
的類型,您會發現所選的pure
版本取決於類型f
。
GHCi> :t pure Nothing
pure Nothing :: Applicative f => f (Maybe a)
當您在交互模式下輸入pure Nothing
時, f
被推斷為IO
並打印IO
操作的結果。 這是 GHCi 提供的快捷方式,在常規 Haskell 代碼中不會發生。
GHCi> pure Nothing
Nothing
GHCi> pure Nothing :: IO (Maybe ())
Nothing
但是,當比較pure Nothing
和Nothing
時, f
被推斷為Maybe
。 這創建了兩層Maybe
,使類型Maybe (Maybe a)
GHCi> Nothing == pure Nothing
False
GHCi> Just Nothing == pure Nothing
True
GHCi> pure Nothing :: Maybe (Maybe ())
Just Nothing
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.