为多态模式同义词编写完整的编译指示?
Writing a COMPLETE pragma for a polymorphic pattern synonym?
我有以下代码,但我不知道 ??
应该提供什么。还是多态模式不能完成?
{-# 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 都是多态的时,你必须输入 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
我不是 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
我有以下代码,但我不知道 ??
应该提供什么。还是多态模式不能完成?
{-# 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 都是多态的时,你必须输入 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
我不是 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