警告模式保护是非详尽的,即使它是
Warning that pattern guard is non-exhaustive even though it is
我观察到一个有趣的行为,即在模式守卫中使用模式匹配并打开所有警告
{-# OPTIONS_GHC -Wall #-}
module Mood where
data Mood = Happy
| Indifferent
| Sad
deriving Show
flipMood :: Mood -> Mood
flipMood Happy = Sad
flipMood Indifferent = Indifferent
flipMood Sad = Happy
flipMood' :: Mood -> Mood
flipMood' mood
| Happy <- mood = Sad
| Indifferent <- mood = Indifferent
| Sad <- mood = Happy
尽管 flipMood
和 flipMood'
几乎在做同样的事情,但我收到以下错误消息:
Mood.hs:15:1: Warning:
Pattern match(es) are non-exhaustive
In an equation for ‘flipMood'’: Patterns not matched: _
Ok, modules loaded: Mood.
因此需要添加一个包罗万象的案例,例如
| otherwise = mood
以满足详尽性检查程序。
Core 似乎与这两个功能相同的功能一样:
flipMood =
\ ds_dTh ->
case ds_dTh of _ {
Happy -> Sad;
Indifferent -> Indifferent;
Sad -> Happy
}
flipMood' = flipMood
关闭优化后,我得到以下核心输出,似乎可以解释此行为:
flipMood' =
\ mood_axV ->
case mood_axV of wild_X9 {
__DEFAULT ->
case wild_X9 of _ {
Indifferent -> Indifferent;
Sad -> Happy
};
Happy -> Sad
}
为什么会这样?我错过了什么吗?
亲切的问候,
瑞秋
这件事已经有 10 年的历史了 ticket。基本上:ghc 中的详尽检查等待英雄。
已添加: 该问题已于今天关闭。我刚刚检查过,代码不再产生非详尽警告。希望它将成为 ghc-8.0
.
的一部分
我观察到一个有趣的行为,即在模式守卫中使用模式匹配并打开所有警告
{-# OPTIONS_GHC -Wall #-}
module Mood where
data Mood = Happy
| Indifferent
| Sad
deriving Show
flipMood :: Mood -> Mood
flipMood Happy = Sad
flipMood Indifferent = Indifferent
flipMood Sad = Happy
flipMood' :: Mood -> Mood
flipMood' mood
| Happy <- mood = Sad
| Indifferent <- mood = Indifferent
| Sad <- mood = Happy
尽管 flipMood
和 flipMood'
几乎在做同样的事情,但我收到以下错误消息:
Mood.hs:15:1: Warning:
Pattern match(es) are non-exhaustive
In an equation for ‘flipMood'’: Patterns not matched: _
Ok, modules loaded: Mood.
因此需要添加一个包罗万象的案例,例如
| otherwise = mood
以满足详尽性检查程序。
Core 似乎与这两个功能相同的功能一样:
flipMood =
\ ds_dTh ->
case ds_dTh of _ {
Happy -> Sad;
Indifferent -> Indifferent;
Sad -> Happy
}
flipMood' = flipMood
关闭优化后,我得到以下核心输出,似乎可以解释此行为:
flipMood' =
\ mood_axV ->
case mood_axV of wild_X9 {
__DEFAULT ->
case wild_X9 of _ {
Indifferent -> Indifferent;
Sad -> Happy
};
Happy -> Sad
}
为什么会这样?我错过了什么吗?
亲切的问候, 瑞秋
这件事已经有 10 年的历史了 ticket。基本上:ghc 中的详尽检查等待英雄。
已添加: 该问题已于今天关闭。我刚刚检查过,代码不再产生非详尽警告。希望它将成为 ghc-8.0
.