当使用Bool和Finite 2时,为什么非详尽的模式警告?

时间:2018-02-09 22:06:24

标签: haskell types ghc

我希望通过BoolFinite 2定义新类的实例,而不创建部分功能,但它不起作用。

我的代码:

-- SO test case, re: my HasFin instance for Bool.
--
-- David Banas <capn.freako@gmail.com>
-- February 9, 2018

{-# OPTIONS_GHC -Wall #-}

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}

module Bogus.BoolHasFin where

import GHC.TypeLits
import Data.Finite
import Data.Finite.Internal (Finite(..))

class KnownNat (Card a) => HasFin a where
  type Card a :: Nat
  toFin :: a -> Finite (Card a)
  unFin :: Finite (Card a) -> a

instance HasFin Bool where
  type Card Bool = 2

  toFin False = finite 0
  toFin True  = finite 1

  unFin = \case
    Finite 0 -> False
    Finite 1 -> True

GHC编制结果:

Davids-Air-2:test dbanas$ stack ghc -- -c so_BoolHasFin.hs 

so_BoolHasFin.hs:30:11: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In a case alternative:
        Patterns not matched: (Finite p) where p is not one of {1, 0}

任何人都可以帮助我理解为什么我会收到这个警告吗? 似乎,通过unFinFinite 2的论证限制在一起,应该已经足够了。

于2018-02-10添加:

根据Conal私下提出的建议,此代码:

unFin (Finite 0) = False
unFin _          = True

消除了警告。

1 个答案:

答案 0 :(得分:6)

我认为至少在某种程度上是因为protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { var db = new dbContext(); var CustItem = db.customer.ToList(); customerDropDownList.DataSource = CustItem; customerDropDownList.DataTextField = "cust"; customerDropDownList.DataValueField = "cust"; customerDropDownList.DataBind(); } } 的定义中没有任何内容限制所包含的Finite在假定范围内({{1} } Integer}:0