Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vigress8 opened this issue Aug 30, 2024 · 9 comments
Open

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

vigress8 opened this issue Aug 30, 2024 · 9 comments

Comments

@vigress8
Copy link

vigress8 commented Aug 30, 2024

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
Copy link
Collaborator

BebeSparkelSparkel commented Sep 18, 2024

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
Copy link
Collaborator

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
Copy link
Author

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

@vigress8 vigress8 changed the title Provide a pattern version of uncons? Provide a pattern version of uncons in classy-prelude Sep 18, 2024
@vigress8
Copy link
Author

It doesn't seem related to mono-traversable

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

@gregwebs
Copy link
Collaborator

sure, makes sense for classy-prelude.

@vigress8
Copy link
Author

@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
Copy link
Collaborator

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

@gregwebs
Copy link
Collaborator

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

@vigress8
Copy link
Author

vigress8 commented Sep 18, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants