-
Notifications
You must be signed in to change notification settings - Fork 16
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
enumerate Function #306
Comments
Relude has |
|
Yes good point, should be |
kmett noted on X that |
Such a function already exists in the |
One of the issues with |
I find it a bit of an odd argument. On the one hand, the CLC constantly requests proposers to demonstrate that new API is actually useful, which is done by showing its use in an actual package. But then we complain that it's already defined in some other package and will cause compatibility issues? |
For which instances? I can think of theoretical types where this would happen, say a type representing a bounded range of floating point numbers. If the bounded range was between -1 and 1 inclusive then If there is one then I think the |
It's no argument for or against, just a gentle suggestion if the proposal author wishes to proceed, that the libraries exposing an identical or similar function be notified of this change, |
See #110 (comment) for Neil's opinion on name clashes with |
To save people a click:
|
I'm supportive of the change. Bikeshedding:
I cannot come up with a practical example. |
I wasn't previously aware that
Unfortunately, all the machinery around Bringing things into My first preference then is to encourage But if people want to push on with bringing a function like this into
|
@endgame data Color = Yellow | Red | Blue deriving (Enum)
data MoreColors = OldColor Color | Green | Magenta | Cyan deriving (Enum) And as the groundwork is shaky, building on top is not very smart. The |
One of “better” Another design (which extends towards infinite, but countable sets) is https://hackage.haskell.org/package/cantor-pairing-0.2.0.2/docs/Cantor.html All in all, I think getting a better |
When declaring a simple enumeration of values it's common to make it just an instance of Once you learn about/remember It's a short enough but is unfortunate (as in less joy) that a simple enumeration of values requires that. Having a Having |
@bcardiff you highlight the problem with Arguably, That won't solve all the problems with |
Agreed. That seems sufficient reason to me to encourage people to write An alternative colour for the bikeshed: if everyone else thinks this should go ahead, a name like |
@phadej Is that true? Couldn't you do instance (Enum a, Enum b) => Enum (Either a b) where
enumerate = enumerate @a ++ enumerate @b
toEnum i = enumerate !! i
fromEnum x = fromJust $ elemIndex x enumerate |
@brandonchinn178 that's terrible instance. Technically correct; |
I like the one with required type argument, but it will be out of line with As a non-native speaker I don't really have an opinion on @mpscholten how would you like to proceed? Is there enough feedback for you to converge on a final proposal? |
I'd love to use a required type argument here as well, but as mentioned earlier already it feels inconsistent with the existing base enum functions. -- Thanks everyone for the input! @Bodigrim yes I think we have enough feedback for the final proposal. Final ProposalAdd a function -- | Returns a list of all values of an enum type
--
-- 'enumerate' is often used to list all values of a custom enum data structure, such as a custom Color enum below:
--
-- @
-- data Color = Yellow | Red | Blue
-- deriving (Enum, Bounded, Show)
--
-- allColors :: [Color]
-- allColors = enumerate
-- -- Result: [Yellow, Red, Blue]
-- @
--
-- Note that you need to derive the 'Bounded' type class as well, only 'Enum' is not enough.
-- 'Enum' allows for sequential enumeration, while 'Bounded' provides the 'minBound' and 'maxBound' values.
--
-- 'enumerate' is commonly used together with the TypeApplications syntax. Here is an example of using 'enumerate' to retrieve all values of the 'Ordering' type:
--
-- >> enumerate @Ordering
-- [LT, EQ, GT]
--
-- The '@' symbol here is provided by the TypeApplications language extension.
--
enumerate :: (Enum a, Bounded a) => [a]
enumerate = [minBound .. maxBound] |
Is there a rationale for including |
The "correct" behavior here is probably debatable, but non-termination could be a nasty surprise for seemingly reasonable instances. enumerateMax :: (Enum a, Bounded a) => [a]
enumerateMax = [minBound .. maxBound]
enumerateNoMax :: (Enum a, Bounded a) => [a]
enumerateNoMax = [minBound ..]
newtype Mod5 = MkMod5 Int
deriving stock (Eq, Show)
instance Bounded Mod5 where
minBound = MkMod5 0
maxBound = MkMod5 4
instance Enum Mod5 where
toEnum x = MkMod5 (x `mod` 5)
fromEnum (MkMod5 x) = x λ. enumerateMax :: [Mod5]
[MkMod5 0,MkMod5 1,MkMod5 2,MkMod5 3,MkMod5 4]
λ. enumerateNoMax :: [Mod5]
[MkMod5 0,MkMod5 1,MkMod5 2,MkMod5 3,MkMod5 4,MkMod5 0,MkMod5 1, ...] |
Dear CLC members, any non-binding opinions on #306 (comment)? I'm in favor. @tomjaguarpaw @mixphix @hasufell @velveteer @angerman @parsonsmatt @mpscholten the next formal step is to prepare a GHC MR. Please follow the steps at the end of "how" section at https://github.com/haskell/core-libraries-committee/blob/main/PROPOSALS.md#the-how. |
Thanks! Created the GHC MR at https://gitlab.haskell.org/ghc/ghc/-/merge_requests/13755 |
@Bodigrim what are the next steps to move this forward after opening the GHC MR? |
Sorry @mpscholten, this fall through the cracks. Dear CLC members, let's vote on the proposal to add Such function is well-attested in prior art in packages such as This is a purely additive change. @tomjaguarpaw @hasufell @mixphix @velveteer @parsonsmatt @angerman +1 from me. I have defined such function in private project preludes more than once. |
+1 |
-1
|
+1 |
While I can see @tomjaguarpaw's reasoning, I don't see the reductive argument to be strong enough to sway me into -1. As such weak, but +1. |
@parsonsmatt @velveteer just a gentle reminder to vote. |
+1 |
It would be nice to have a function to retrieve all values from an enum:
In IHP we have a helper function for this called
allEnumValues
:E.g. here is an example I just randomly picked from one of our apps: Rendering all timezones:
Here is a GitHub Search showing tons of adhoc implementations of this function across many haskell projects: https://github.com/search?q=allEnumValues+language%3AHaskell+&type=code
There is already a PR for this proposal via https://gitlab.haskell.org/ghc/ghc/-/merge_requests/11321 (there it's called
enumerate
instead ofallEnumValues
)The text was updated successfully, but these errors were encountered: