为多态模式的同义词编写完整的布拉格



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

{-# 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文档说,当所有的conlikes都是多态性时,您必须键入conike。

制作?? ()时,汇编成功。但是()是什么意思?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

我不是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

相关内容

  • 没有找到相关文章

最新更新