Skip to content

RFC: Add pattern synonyms to base

Since we have pattern synonyms it's worth considering if some belong in base:

Data.Complex.Lens contains patterns that could be defined in base, here are some more suggestions:

Data.Array

pattern ArrayIx :: Ix i => (i, i) -> [(i, e)] -> Array i e
pattern ArrayIx low'high xs <- ((\arr -> (bounds arr, assocs arr)) -> (low'high, xs)) 
  where ArrayIx low'high xs = array low'high xs

Data.Bits

pattern ZeroBits :: (Eq a, Bits a) => a                                         
pattern ZeroBits <- ((== zeroBits) -> True) 
  where ZeroBits = zeroBits                                                        

pattern BitSize :: Bits a => Int -> a                                            
pattern BitSize n <- (bitSizeMaybe -> Just n)                                        

pattern Signed :: Bits a => a                                                   
pattern Signed <- (isSigned -> True)                                               

pattern Unsigned :: Bits a => a                                                 
pattern Unsigned <- (isSigned -> False)                                              

pattern PopCount :: Bits a => Int -> a                                           
pattern PopCount n <- (popCount -> n)                                                

Data.Char

pattern ControlChar :: Char                                                         
pattern ControlChar <- (isControl -> True)                                           

pattern SpaceChar :: Char                                                           
pattern SpaceChar <- (isSpace -> True)                                               

Data.Complex

import Data.Complex (Complex, conjugate, polar, mkPolar)
import qualified Data.Complex as C

pattern Conjugate :: Num a => Complex a -> Complex a
pattern Conjugate a <- (conjugate -> a) 
  where Conjugate a = conjugate a

pattern Polar :: RealFloat a => a -> a -> Complex a
pattern Polar m theta <- (polar -> (m, theta))
  where Polar m theta = mkPolar m theta

-- See https://github.com/ekmett/lens/issues/653
pattern Real :: Num a => a -> Complex a
pattern Real r <- r C.:+ _
  where Real r =  r C.:+ 0

pattern Imaginary :: Num a => a -> Complex a
pattern Imaginary i <- _ C.:+ i
  where Imaginary i =  0 C.:+ i

pattern (:+) :: a -> a -> Complex a
pattern (:+) { realPart, imagPart } = realPart C.:+ imagPart 

GHC.Float

pattern NegativeZero :: RealFloat a => a
pattern NegativeZero <- (isNegativeZero -> True)
  where NegativeZero = -0

pattern Denormalized :: RealFloat a => a
pattern Denormalized <- (isDenormalized -> True)

pattern NaN :: RealFloat a => a
pattern NaN <- (isNaN -> True)
  where NaN = 0 / 0

-- How ever negative infinity is handled
pattern Infinity :: RealFloat a => a
pattern Infinity <- (isInfinite -> True)
  where Infinity = 1 / 0

Foreign.Ptr

pattern NullPtr :: Ptr a
pattern NullPtr <- ((==) nullPtr -> True)
  where NullPtr = nullPtr

Used here

Edited by Icelandjack
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information