为多态模式同义词写一个COMPLETE编译指示吗?

时间:2019-06-30 01:34:08

标签: haskell ghc pattern-synonyms

我有以下代码,但我不知道在??应该输入什么。还是无法完成多态模式?

{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns    #-}

module Data.Tuple.Single.Class
  ( Single (..)
  , pattern Single
  ) where

class Single t where
  wrap :: a -> t a
  unwrap :: t a -> a

pattern Single :: Single t => a -> t a
pattern Single a <- (unwrap -> a) where
  Single a = wrap a

{-# COMPLETE Single :: ?? #-}

GHC document说,当所有赞叹词都是多态的时,您必须键入conlike。

制作?? ()时,编译成功。但是()是什么意思? GHC表示使用情况还不完全。

{-# LANGUAGE PatternSynonyms #-}

{-# OPTIONS_GHC -Wno-orphans #-}

module Data.Tuple.Single.Only
  ( Single (..)
  , pattern Single
  ) where

import           Data.Tuple.Only         (Only (Only, fromOnly))
import           Data.Tuple.Single.Class (Single (unwrap, wrap), pattern Single)

instance Single Only where
  wrap = Only
  unwrap = fromOnly
ghci> Single a = wrap 1 :: Only Int

<interactive>:2:1: warning: [-Wincomplete-uni-patterns]
    Pattern match(es) are non-exhaustive
    In a pattern binding: Patterns not matched: _
  • GHC 8.6.5

1 个答案:

答案 0 :(得分:4)

我不是PatternSynonyms的专家,但是从它的外观来看,如果是多态模式,我们需要指定准确的类型以使其完整。

对于Only,这将是:

{-# COMPLETE Single :: Only #-}

为了示例,我们将另一个实例添加到Single


instance Single Identity where
  wrap = Identity
  unwrap (Identity a) = a

pattern Single :: Single t => a -> t a
pattern Single a <- (unwrap -> a) where
  Single a = wrap a

{-# COMPLETE Single :: Only #-}
{-# COMPLETE Single :: Identity #-}

这使GHC停止抱怨:

λ> Single a = wrap 1 :: Identity Int
λ> Single a = wrap 1 :: Only Int