[GHC] #9671: Allow expressions in patterns

GHC ghc-devs at haskell.org
Wed Oct 8 22:36:41 UTC 2014


#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


More information about the ghc-tickets mailing list