generic-match: First class pattern matching

[ data, library, mit ] [ Propose Tags ]

First class pattern matching.

[Skip to Readme]


[Index] [Quick Jump]


Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


  • No Candidates
Versions [RSS],,,,,
Change log
Dependencies base (>=4.12 && <4.16), generics-sop (>=0.5 && <0.6) [details]
License MIT
Copyright 2020 Samuel Schlesinger
Author Samuel Schlesinger
Category Data
Source repo head: git clone
Uploaded by sgschlesinger at 2021-08-16T05:36:48Z
Downloads 947 total (14 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-08-16 [all 1 reports]

Readme for generic-match-

[back to package description]


Hackage Build Status


An implementation of first-class pattern matches in Haskell, based on generics-sop.


Oftentimes, when writing Haskell code, we want to branch on multiple cases of a sum type, such as

data TravelMethod
  = Airplane Airport UTCTime
  | Train TrainStation UTCTime
  | Driving

For instance, lets say that we want to grab out the time. In Haskell, we can do this by writing:

timeOfTravel :: TravelMethod -> Maybe UTCTime
timeOfTravel = \case
  Airplane _airport time -> Just time
  Train _trainStation time -> Just time
  Driving -> Nothing

This is concise, and preferable to many other languages, but in this case we can do even better using this library.

timeOfTravel travelMethod = match travelMethod (Just . flip const) (Just . flip const) Nothing

In this example, perhaps we don't save much, but I hope the principle is clear. The case for using this library is when you want to branch on the contents of each different sum, and you already have functions or concise combinators to build functions that handle your inputs. For a Haskeller, this is already rather familiar, I claim!

either l r x == match x l r
maybe n j x == match x n j


data DatabaseAccess a =
    ConnectionFailure String
  | InvalidRowCount Int
  | Successful a
  deriving (GHC.Generic, Generic)

doThing :: m (DatabaseAccess Bool)


x <- doThing >>= \g -> match g error (error . show) pure


Contributions are very welcome! Feel free to create an issue or a PR or ping me on any platform to chat about whatever, especially improvements to my libraries.


I support all of the GHC versions that I typically do in my code, from 8.6.1 to 8.10.2. I build locally on Mac, and my travis builds are on Linux, so if you run into problems on Windows, let me know. If you want to be sure that a build will pass, run the test script in this repository.