[GHC] #8583: Associated pattern synonyms

#8583: Associated pattern synonyms ------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: Keywords: | Operating System: Unknown/Multiple Architecture: Unknown/Multiple | Type of failure: None/Unknown Difficulty: Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | ------------------------------------+------------------------------------- The PatternSynonyms wiki page has a section on (typeclass-)associated pattern synonyms: {{{ class ListLike l where pattern Nil :: l a pattern Cons :: a -> l a -> a isNil :: l a -> Bool isNil Nil = True isNil (Cons _ _) = False append :: l a -> l a -> l a instance ListLike [] where pattern Nil = [] pattern Cons x xs = x:xs append = (++) headOf :: (ListLike l) => l a -> Maybe a headOf Nil = Nothing headOf (Cons x _) = Just x }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------ Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: 5144 Blocking: | Related Tickets: -------------------------------------+------------------------------------ Changes (by cactus): * type: bug => feature request -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature | Status: new request | Milestone: Priority: normal | Version: Component: Compiler | Keywords: pattern synonyms Resolution: | Architecture: Unknown/Multiple Operating System: | Difficulty: Unknown Unknown/Multiple | Blocked By: 5144 Type of failure: | Related Tickets: None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Changes (by cactus): * cc: cactus (added) * keywords: => pattern synonyms -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature | Status: new request | Milestone: Priority: normal | Version: Component: Compiler | Keywords: PatternSynonyms Resolution: | Architecture: Unknown/Multiple Operating System: | Difficulty: Unknown Unknown/Multiple | Blocked By: 5144 Type of failure: | Related Tickets: None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Changes (by cactus): * keywords: pattern synonyms => PatternSynonyms -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: Operating System: Unknown/Multiple | PatternSynonyms Type of failure: None/Unknown | Architecture: Blocked By: 5144 | Unknown/Multiple Related Tickets: | Test Case: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Description changed by bgamari: Old description:
The PatternSynonyms wiki page has a section on (typeclass-)associated pattern synonyms:
{{{ class ListLike l where pattern Nil :: l a pattern Cons :: a -> l a -> a isNil :: l a -> Bool isNil Nil = True isNil (Cons _ _) = False append :: l a -> l a -> l a
instance ListLike [] where pattern Nil = [] pattern Cons x xs = x:xs append = (++)
headOf :: (ListLike l) => l a -> Maybe a headOf Nil = Nothing headOf (Cons x _) = Just x }}}
New description: The PatternSynonyms wiki page has a section on (typeclass-)associated pattern synonyms: {{{#!hs class ListLike l where pattern Nil :: l a pattern Cons :: a -> l a -> a isNil :: l a -> Bool isNil Nil = True isNil (Cons _ _) = False append :: l a -> l a -> l a instance ListLike [] where pattern Nil = [] pattern Cons x xs = x:xs append = (++) headOf :: (ListLike l) => l a -> Maybe a headOf Nil = Nothing headOf (Cons x _) = Just x }}} -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: Operating System: Unknown/Multiple | PatternSynonyms Type of failure: None/Unknown | Architecture: Blocked By: 5144 | Unknown/Multiple Related Tickets: | Test Case: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Description changed by bgamari: Old description:
The PatternSynonyms wiki page has a section on (typeclass-)associated pattern synonyms:
{{{#!hs class ListLike l where pattern Nil :: l a pattern Cons :: a -> l a -> a isNil :: l a -> Bool isNil Nil = True isNil (Cons _ _) = False append :: l a -> l a -> l a
instance ListLike [] where pattern Nil = [] pattern Cons x xs = x:xs append = (++)
headOf :: (ListLike l) => l a -> Maybe a headOf Nil = Nothing headOf (Cons x _) = Just x }}}
New description: The PatternSynonyms wiki page has a section on (typeclass-)associated pattern synonyms: {{{#!hs class ListLike l where pattern Nil :: l a pattern Cons :: a -> l a -> a isNil :: l a -> Bool isNil Nil = True isNil (Cons _ _) = False append :: l a -> l a -> l a instance ListLike [] where pattern Nil = [] pattern Cons x xs = x:xs append = (++) headOf :: (ListLike l) => l a -> Maybe a headOf Nil = Nothing headOf (Cons x _) = Just x }}} -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): -------------------------------------+------------------------------------- Changes (by rwbarton): * cc: carter (added) Comment: Definitely would be useful, but this might require a new implementation strategy since it gives pattern synonyms essentially first-class status, right? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): -------------------------------------+------------------------------------- Comment (by goldfire): Yes yes yes. I want this! But it's tricky, even forgetting about implementation for the moment. Let's pretend that all types have to be explicit for a sec, and I'll continue the example from the original post: {{{#!hs headOfList :: forall a. [a] -> Maybe a headOfList (Nil @[] @a) = Nothing @a headOfList (Cons @[] @a x _) = Just x }}} There's something very strange going on here. It looks, in those patterns, that we're pattern-matching on a ''type''. This, of course, is not allowed, because types are erased. Upon further inspection, we're not really doing this. The first type parameter to `Nil` and `Cons` are ''inputs''. Contrast to the second type parameter and the term parameters (to `Cons`), which are ''outputs''. Another way to see this is to think about how `Nil @[] @a` and `Nil @Seq @a` are both valid patterns, but `Nil @l @Int` would be terrible, as it requires doing a type check at runtime. So I wonder if implementing this without terrible hacks might depend on PatternFamilies which (despite its name) allows for a mix of inputs and outputs in pattern synonyms. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

Upon further inspection, we're not really doing this. The first type
#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): -------------------------------------+------------------------------------- Comment (by nomeata): parameter to Nil and Cons are inputs This seems to be #9671 in disguise. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): This can also be faked using prisms and pattern synonyms: [https://www.reddit.com/r/haskell/comments/38owdh/question_would_an_associate... reddit comment]. For visible type application in patterns per [comment:7 Richard's comment] see #11350. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by rwbarton): See #11461 for how to approximate this feature in current GHC. Richard, how does your comment:7 apply here? Or to the example in #11224? The status quo seems to be that type arguments are "inputs", rather than "outputs", of the pattern synonym. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by goldfire): I retract comment:7. I have no idea what I was thinking. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:11 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * cc: RyanGlScott (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:12 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): If this gets implement, please consider default associated patterns {{{#!hs class HasZero n where pattern Zero :: n default pattern Zero :: (Num n, Eq n) => n pattern Zero = 0 }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:13 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by carter): I too would like this. My goal/motivation would be to use data families to have smart strict unboxed datatypes with uniform syntax. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:14 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by Shayan-Najd): * cc: Shayan-Najd (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:15 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Shayan-Najd): I am writing to resurrect this ticket! Nowadays, we can use `COMPLETE` pragmas to group together an arbitrary collection of synonyms. Why not also grouping the so called polymorphic pattern synonyms under type classes and having the totality condition derived from the association? (seems the most natural place for `COMPLETE`). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:16 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by goldfire): Given our new ghc-proposals process, I think this should go via that route. The process seems to working well, and I know I have benefited through submitting my own proposals there and getting helpful feedback. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:17 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8583: Associated pattern synonyms -------------------------------------+------------------------------------- Reporter: cactus | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: Resolution: | Keywords: | PatternSynonyms Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: 5144 | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): Quick reminder: I need this to work with GND. Using an `Int` instance of the above class {{{#!hs deriving instance HasZero Int }}} I want to be able to derive (`newtype`, not `anyclass`) {{{#!hs newtype USD = USD Int deriving newtype HasZero -- instance HasZero USD where -- pattern Zero :: USD -- pattern Zero <- (coerce -> Zero::Int) -- where Zero = coerce (Zero::Int) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8583#comment:18 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC