【问题标题】:Warning that pattern guard is non-exhaustive even though it is警告模式保护并非详尽无遗,即使它是
【发布时间】:2015-02-11 12:51:52
【问题描述】:

当使用模式匹配和模式保护并且所有警告都打开时,我观察到一个有趣的行为

{-# 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

尽管 flipMoodflipMood' 几乎做同样的事情,但我收到以下错误消息:

Mood.hs:15:1: Warning:
    Pattern match(es) are non-exhaustive
    In an equation for ‘flipMood'’: Patterns not matched: _
Ok, modules loaded: Mood.

因此需要添加一个 catch all case 之类的

| otherwise = mood

满足穷举检查器。

这两个函数的行为相同,核心似乎很好:

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
    }

为什么会这样?我错过了什么吗?

亲切的问候, 雷乔

【问题讨论】:

    标签: haskell pattern-matching pattern-guards


    【解决方案1】:

    有一个长 10 岁的 ticket 关于那个。基本上:在 ghc 中的详尽检查正在等待英雄。

    补充:问题已于今天结束。我刚刚检查过,代码不再产生非详尽的警告。希望它将成为ghc-8.0 的一部分。

    【讨论】:

    • 我想这说明了一些事情:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多