View patterns

2019-01-23

I’m learning about the GHC ViewPatterns language extension at the same time as learning about Data.Sequence. I like the the ability to pattern-match on sequences a lot. Unfortunately, it looks like the exhaustiveness checker cannot detect when all possible patterns are covered. Here’s an example:

Running this script results in the following compiler warnings:

/path/to/ViewPatternDemo.hs:11:1: warning: [-Wincomplete-patterns]
    Pattern match(es) are non-exhaustive
    In an equation for ‘joinShow’: Patterns not matched: _
   |
11 | joinShow (viewl -> EmptyL) = ""
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
-}

I’m going to see if there are known GHC bugs related to this issue.

Update: 2019-01-25

It turns out that the PatternSynonyms language extension can do more or less the same thing but with nicer syntax:

I think pattern synonyms are strictly less powerful than view patterns which is why (I think) GHC’s exhaustiveness checker works better with them.

Related posts

oset-0.4.0.1 released
Alternative newtype wrappers for oset
New packages
oset

Tags

Haskell
GHC
View patterns
Pattern synonyms
oset

Content © 2024 Richard Cook. All rights reserved.