Skip to content

Type synonym with context in pattern synonym

pattern :: (Num a, Eq a) => a
pattern Fiskur = 42

unlike

type Diskur a = Num a => a

fiskur :: Num a => a
fiskur = 42

we cannot write

type Fugl a = (Num a, Eq a) => a

pattern Fiskur :: Fugl a
pattern Fiskur = 42

-- Exp.hs:54:18-19: error: …
--     • No instance for (Eq a) arising from a pattern
--       Possible fix:
--         add (Eq a) to the context of
--           the type signature for pattern synonym ‘Fiskur’:
--             Fugl a
--     • In the pattern: 42
--       In the declaration for pattern synonym ‘Fiskur’
-- Compilation failed.

Given that the story of pattern synonym contexts is more complicated than usual patterns given provided constraints, I know there are other issues (#11524 (closed), #10928 (closed)).


For fun why not type synonym with provided and required constraints:

data T a where
  MkT :: (Show b) => a -> b -> T a

f1 :: (Eq a, Num a) => T a -> String
f1 (MkT 42 x) = show x

type MKNUMPAT a b = (Num a, Eq a) => (Show b) => b -> T a

-- pattern ExNumPat :: (Num a, Eq a) => (Show b) => b -> T a
pattern ExNumPat :: MKNUMPAT a b
pattern ExNumPat x = MkT 42 x

What would that mean in a regular type signature? CE := CP + CR where EXNUMPAT a b means the same as EXNUMPAT' a b? (#8581)

type MKNUMPAT' a b = (Num a, Eq a, Show b) => b -> T a

-- This works:
mkNumPat :: MKNUMPAT a b
mkNumPat = ExNumPat
Trac metadata
Trac field Value
Version 8.1
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information