Opened 4 years ago

Last modified 3 years ago

#11461 new feature request

Allow pattern synonyms to be bundled with type classes?

Reported by: mpickering Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.10.3
Keywords: PatternSynonyms Cc: RyanGlScott
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description (last modified by hvr)

One can very nearly get associated pattern synonyms by defining suitably polymorphic pattern synonyms. However, they are not quite associated as there's no way to bundle them with the class. This isn't as good as "proper" support but it would be an easy thing to implement for now if people think it worthwhile.

For a concrete example, Null is an associated pattern synonym in this style but the following program doesn't compile because it is disallowed to bundle a pattern synonym with a type class.

{-# LANGUAGE PatternSynonyms #-}

module Foo(Nullable(Null)) where

import Data.Maybe

class Nullable f where
  null :: f a -> Bool

instance Nullable (Maybe a) where
  null = isNothing

pattern Null :: Nullable f => f a
pattern Null = (null -> True)

Change History (5)

comment:1 Changed 4 years ago by Iceland_jack

How does this relate to #8583?

comment:2 Changed 4 years ago by Iceland_jack

Did you mean

instance Nullable Maybe where
  null = isNothing

pattern Null :: Nullable f => f a
pattern Null <- (null -> True)

comment:3 Changed 4 years ago by hvr

Description: modified (diff)

comment:4 Changed 4 years ago by rwbarton

Probably obvious to the pattern synonym cognoscenti but you can change the export list to

module Foo(Nullable, pattern Null) where

and use this style today.

The disadvantages relative to "real" associated patterns are

  • no association between the class and the pattern as far as import/export is concerned
  • you have to give a single top-level definition of the pattern synonym, and do the per-instance work in an auxiliary function. I think you can always do this at negligible cost (other than syntactic noise).

comment:5 Changed 3 years ago by RyanGlScott

Cc: RyanGlScott added
Note: See TracTickets for help on using tickets.