snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
153 stars 64 forks source link

Provide a pattern version of `uncons` in `classy-prelude` #241

Open vigress8 opened 3 weeks ago

vigress8 commented 3 weeks ago

I'm thinking of something like:

{-# language ViewPatterns #-}

pattern x ::: xs <- (uncons -> Just (x, xs)) where
  (:::) = cons

I find myself wanting sequence-polymorphic pattern matching often, personally. It's trivial to define manually but having it in classy-prelude would make things simpler. Curious what anyone else thinks

BebeSparkelSparkel commented 4 days ago

I see many instances of case uncons xs of Just (x, xs) -> ..., so it seems like a useful addition.

However, I do not see any patterns already defined in mono-traversable. @gregwebs do you have any thoughts on adding pattern exports ghc has had them since 7.8.1 so it seems like a well established feature that does not need CPP for version checking.

@vigress8 can you comment on how to have completeness for the pattern? It seems like this will generate many warnings for incomplete patterns.

gregwebs commented 4 days ago

hmm, why would this package be the one that defines patterns to export? It doesn't seem related to mono-traversable.

I think that a custom prelude package would define this.

vigress8 commented 3 days ago

Sorry I wasn't specific, by "this package" I meant classy-prelude (which is in this repository). Edited now

vigress8 commented 3 days ago

It doesn't seem related to mono-traversable

It's related because the pattern is meant to be generic over IsSequence.

gregwebs commented 3 days ago

sure, makes sense for classy-prelude.

vigress8 commented 3 days ago

@vigress8 can you comment on how to have completeness for the pattern? It seems like this will generate many warnings for incomplete patterns.

To me the simplest way to achieve completeness seems to be

pattern Empty <- (uncons -> Nothing)
{-# complete Empty, (:::) #-}
BebeSparkelSparkel commented 3 days ago

Is the name clash acceptable https://hoogle.haskell.org/?hoogle=%3A%3A%3A

gregwebs commented 3 days ago

Probably better to just use a name and not have a symbol? At least to start?

vigress8 commented 3 days ago

The specific symbol isn't critically important to me, I just chose it for example's sake