diff options
author | David Knothe <dknothe314@me.com> | 2022-10-25 09:18:26 +0200 |
---|---|---|
committer | David Knothe <dknothe314@me.com> | 2023-03-03 13:10:28 +0100 |
commit | 4c070b086e8bc7c79117ee3764dc0ae13ba2fa95 (patch) | |
tree | 615838a3d5016d8f8c11ae6dc00bff9e4948ce5e /compiler/Language/Haskell/Syntax | |
parent | 2f97c86151d7eed115ddcbdee1842684aed63176 (diff) | |
download | haskell-wip/or-pats.tar.gz |
Implement Or Patterns (Proposal 0522)wip/or-pats
This commit introduces a language extension, `OrPatterns`, as described in proposal 0522.
It extends the syntax by the production
`pat -> (one of pat1, ..., patk)`.
The or-pattern `pat` succeeds iff one of the patterns `pat1`, ..., `patk` succeed, in this order.
Currently, or-patterns cannot bind variables. They are still of great use as they discourage the use of wildcard patterns in favour of writing out all "default" cases explicitly:
```
isIrrefutableHsPat pat = case pat of
...
(one of WildPat{}, VarPat{}, LazyPat{})
= True
(one of PArrPat{}, ConPatIn{}, LitPat{}, NPat{}, NPlusKPat{}, ListPat{})
= False
```
This makes code safer where data types are extended now and then - just like GHC's `Pat` in the example when adding the new `OrPat` constructor. This would be catched by `-fwarn-incomplete-patterns`, but not when a wildcard pattern was used.
- Update submodule haddock.
Diffstat (limited to 'compiler/Language/Haskell/Syntax')
-rw-r--r-- | compiler/Language/Haskell/Syntax/Extension.hs | 3 | ||||
-rw-r--r-- | compiler/Language/Haskell/Syntax/Pat.hs | 5 |
2 files changed, 7 insertions, 1 deletions
diff --git a/compiler/Language/Haskell/Syntax/Extension.hs b/compiler/Language/Haskell/Syntax/Extension.hs index 9ad16c0cd7..ce1ac48647 100644 --- a/compiler/Language/Haskell/Syntax/Extension.hs +++ b/compiler/Language/Haskell/Syntax/Extension.hs @@ -66,7 +66,7 @@ See also Note [IsPass] and Note [NoGhcTc] in GHC.Hs.Extension. -} -- | A placeholder type for TTG extension points that are not currently --- unused to represent any particular value. +-- used to represent any particular value. -- -- This should not be confused with 'DataConCantHappen', which are found in unused -- extension /constructors/ and therefore should never be inhabited. In @@ -591,6 +591,7 @@ type family XBangPat x type family XListPat x type family XTuplePat x type family XSumPat x +type family XOrPat x type family XConPat x type family XViewPat x type family XSplicePat x diff --git a/compiler/Language/Haskell/Syntax/Pat.hs b/compiler/Language/Haskell/Syntax/Pat.hs index 66b9708bfe..80edb216a5 100644 --- a/compiler/Language/Haskell/Syntax/Pat.hs +++ b/compiler/Language/Haskell/Syntax/Pat.hs @@ -52,6 +52,7 @@ import Data.Ord import Data.Int import Data.Function import qualified Data.List +import qualified Data.List.NonEmpty as NEL type LPat p = XRec p (Pat p) @@ -137,6 +138,10 @@ data Pat p -- 'GHC.Parser.Annotation.AnnOpen' @'('@ or @'(#'@, -- 'GHC.Parser.Annotation.AnnClose' @')'@ or @'#)'@ + | OrPat (XOrPat p) + (NEL.NonEmpty (LPat p)) + -- ^ Or Pattern + | SumPat (XSumPat p) -- after typechecker, types of the alternative (LPat p) -- Sum sub-pattern ConTag -- Alternative (one-based) |