[GHC] #9671: Allow expressions in patterns

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: feature request | Status: new Priority: normal | Milestone: 7.10.1 Component: Compiler | Version: Keywords: patterns, pattern | Operating System: synonyms | Unknown/Multiple Architecture: Unknown/Multiple | Type of failure: Difficulty: Unknown | None/Unknown Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- I've outlined a proposal for extending pattern synonyms to depend on terms (PatternFamilies), the name is less than ideal but I'll keep the page under that name for now. The proposal's introduction has been rewritten. The simplest use case are patterns that matches only when a set contains an element (`IsMember`) or when a set does not contain an element (`NotMember`): [[Image(wiki:PatternFamilies:member.png)]] {{{#!hs hasKeys :: Set Item -> IO () hasKeys (IsMember "keys") = leaveHouse hasKeys (NotMember "keys") = findKeys >> leaveHouse }}} or a pattern that matches a map if the key exists: [[Image(wiki:PatternFamilies:lookup.png)]] used like this: {{{#!hs age :: Map Person Age -> String age (Lookup "Alice" 60) = "Alice is 60 years old!" age (Lookup "Alice" age) = "Alice is " ++ show age ++ "." age (Lookup "Bob" _) = "No info on Alice but we know how old Bob is." age _ = "..." }}} Further details and examples can be found here: PatternFamilies, I discussed it with several people ICFP 2014. This would not require a new extension but would extend PatternSynonyms. This feature can be quite useful, especially when working with deeply nested structures (ASTs, JSON, XML, ...). If people are fine with it I will implement it. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: | Owner: Iceland_jack | Status: new Type: feature | Milestone: 7.10.1 request | Version: Priority: normal | Keywords: patterns, pattern Component: Compiler | synonyms Resolution: | Architecture: Unknown/Multiple Operating System: | Difficulty: Unknown Unknown/Multiple | Blocked By: Type of failure: | Related Tickets: None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by simonpj): Your examples all use constants, but I assume you are going to allow this: {{{ f :: Person -> Map Person Age -> Age f p (Lookup p a) = a f p _ = error ("No info on " ++ show p) }}} Now this is jolly confusing. In the pattern `(Lookup p a)`, `p` is a bound occurence while `a` is a binding occurrence. This distinction is made syntactically by view patterns: {{{ f :: Person -> Map Person Age -> Age f p ((\m -> lookup m p) -> a) = a }}} Here the stuff on the left of the view-pattern arrow `->` are bound occurrences, while the stuff on the right is binding. You could even have {{{ f p (Lookup p p) = p }}} meaning {{{ f p1 (Lookup p1 p2) = p2 }}} To me, the lack of syntactic cues for what is bound and what is binding seems quite serious. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

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

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: | Owner: Iceland_jack | Status: new Type: feature | Milestone: 7.10.1 request | Version: Priority: normal | Keywords: patterns, pattern Component: Compiler | synonyms Resolution: | Architecture: Unknown/Multiple Operating System: | Difficulty: Unknown Unknown/Multiple | Blocked By: Type of failure: | Related Tickets: None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by cactus):
The implementation is a straightforward desugaring into pattern synonyms and view patterns
That's how I started the work on pattern synonyms in the first place... spoiler alert: it never is :) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: | Owner: Iceland_jack | Status: new Type: feature | Milestone: 7.10.1 request | Version: Priority: normal | Keywords: patterns, pattern Component: Compiler | synonyms Resolution: | Architecture: Unknown/Multiple Operating System: | Difficulty: Unknown Unknown/Multiple | Blocked By: Type of failure: | Related Tickets: None/Unknown | Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Comment (by cactus): +1 to SPJ on distinguishing ''pat'' and ''expr'' arguments. In fact, I think they should be distinguished even when introducing them as formal arguments. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

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

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * cc: RyanGlScott (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by dfeuer): * cc: dfeuer (added) Comment: Is there a GHC proposal for this? I think it'd be pretty cool to get something similar, perhaps with an explicit separator between the arguments and the subpatterns. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): Is this a matter of finding good syntax or is it something fundamental? I'll get the ball rolling, help me pick this apart {{{#!hs pattern Lookup \key val <- (lookup key -> Just val) pattern IsMember \val <- (member val -> True) -- and they get marked with $ when in pattern -- (I'm making this up on the tube) age (Lookup $"Alice" age) = ... -- Simon's f p (Lookup $p p) = p }}} If it helps we could impose a separation between "argument patterns" and patterns. I want this feature at least once a week ;) so I self-servingly remain +1 if this can be sorted -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:11 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): Replying to [comment:3 cactus]:
The implementation is a straightforward desugaring into pattern synonyms and view patterns
That's how I started the work on pattern synonyms in the first place... spoiler alert: it never is :)
+1 to SPJ on distinguishing ''pat'' and ''expr'' arguments. In fact, I
;) think they should be distinguished even when introducing them as formal arguments. Can you elaborate on last part -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:12 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by mrkgnao): Is there any activity on this? How difficult would it be to implement a basic proof-of-concept? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:13 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by Artyom.Kazak): * cc: Artyom.Kazak (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:14 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by ekmett): * cc: ekmett (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:15 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by ekmett): {{{Lookup $"Alice" =}}} is syntactically ambiguous in the context where you are doing a destructuring assignment. It could be defining `$` or providing the marker. This seems to indicate that the marker should be a divider or indicator of purpose that is already reserved syntax and isn't a valid operator. `->` is taken by view patterns, but you could use (with varying degrees of absurdity) already reserved operators like `<-`, `\`, `;`, `..`, `=>` as either a separator between parameters and fields or as an indicator in a manner that would simply be filling a hole in the grammar, if you really want a visual indicator of which arguments to the pattern are inputs if we really do want a visual marker here. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:16 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#9671: Allow expressions in patterns -------------------------------------+------------------------------------- Reporter: Iceland_jack | 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: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): People seem to like `\` -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/9671#comment:17 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC