Writing a COMPLETE pragma for a polymorphic pattern synonym?
Asked Answered
L

1

7

I have the following code and I don't know what should feed at ??. Or cannot polymorphic patterns make complete?

{-# 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 says that when all the conlikes are polymorphic you must type conlike.

When making ?? (), the compilation is successful. But what does () mean? And GHC says still non-exhaustive on usage.

{-# 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
Laurinda answered 30/6, 2019 at 1:34 Comment(2)
That wiki article suggests you can use {-# COMPLETE Single :: () #-}—have you tried that?Lianne
@JonPurdy I reflected your comment.Laurinda
J
5

I am no expert on PatternSynonyms, but from the looks of it, in case of polymorphic patterns we need to specify exact types that make them complete.

In the case of Only this would be:

{-# COMPLETE Single :: Only #-}

For the sake of example let's add another instance to 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 #-}

Which makes GHC to stop complaining:

λ> Single a = wrap 1 :: Identity Int
λ> Single a = wrap 1 :: Only Int
Jennine answered 1/7, 2019 at 12:32 Comment(1)
Addition: GHC says that Orphan COMPLETE pragmas not supported, a COMPLETE pragma must mention at least one data constructor or pattern synonym defined in the same module.Laurinda

© 2022 - 2024 — McMap. All rights reserved.