How to create a list of each constructor value

Hello, I have a type like this data InputType = CristalK6C | MarsFlyscan | MarsSbs | SixsFlyMedH | SixsFlyMedV | SixsFlyMedVEiger | SixsFlyMedVS70 | SixsFlyScanUhv | SixsFlyScanUhv2 | SixsFlyScanUhvTest | SixsFlyScanUhvUfxc | SixsSbsFixedDetector | SixsSbsMedH | SixsSbsMedV | SixsSbsMedVFixDetector deriving (Eq, Show) I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances. I do not want to type two time the values. instance Arbitrary InputType where arbitrary = oneof (map pure [ CristalK6C , MarsFlyscan , MarsSbs , SixsFlyMedH , SixsFlyMedV , SixsFlyMedVEiger , SixsFlyMedVS70 , SixsFlyScanUhv , SixsFlyScanUhv2 , SixsFlyScanUhvTest , SixsFlyScanUhvUfxc , SixsSbsFixedDetector , SixsSbsMedH , SixsSbsMedV , SixsSbsMedVFixDetector ] ) Thanks for your help Frederic

On Wed, 30 Nov 2022, PICCA Frederic-Emmanuel wrote:
Hello, I have a type like this
data InputType = CristalK6C | MarsFlyscan | MarsSbs | SixsFlyMedH | SixsFlyMedV | SixsFlyMedVEiger | SixsFlyMedVS70 | SixsFlyScanUhv | SixsFlyScanUhv2 | SixsFlyScanUhvTest | SixsFlyScanUhvUfxc | SixsSbsFixedDetector | SixsSbsMedH | SixsSbsMedV | SixsSbsMedVFixDetector deriving (Eq, Show)
I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances.
deriving (Enum, Bounded) then use [minBound .. maxBound]

Thanks you all for these rapid answers ----- Le 30 Nov 22, à 16:21, Henning Thielemann lemming@henning-thielemann.de a écrit :
On Wed, 30 Nov 2022, PICCA Frederic-Emmanuel wrote:
Hello, I have a type like this
data InputType = CristalK6C | MarsFlyscan | MarsSbs | SixsFlyMedH | SixsFlyMedV | SixsFlyMedVEiger | SixsFlyMedVS70 | SixsFlyScanUhv | SixsFlyScanUhv2 | SixsFlyScanUhvTest | SixsFlyScanUhvUfxc | SixsSbsFixedDetector | SixsSbsMedH | SixsSbsMedV | SixsSbsMedVFixDetector deriving (Eq, Show)
I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances.
deriving (Enum, Bounded)
then use [minBound .. maxBound]

Hi, Am Mittwoch, dem 30.11.2022 um 16:21 +0100 schrieb Henning Thielemann:
I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances.
deriving (Enum, Bounded)
then use [minBound .. maxBound]
if you want to golf it a bit more, then [minBound..] works as well. (I vaguely remember that there was a variant that didn’t even need Bounded, just, but I don’t recall it right now. Maybe it was [toEnum 0..]? but that only works for certain instances.) Cheers, Joachim -- Joachim Breitner mail@joachim-breitner.de http://www.joachim-breitner.de/

On Sat, 3 Dec 2022, Joachim Breitner wrote:
Hi,
Am Mittwoch, dem 30.11.2022 um 16:21 +0100 schrieb Henning Thielemann:
I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances.
deriving (Enum, Bounded)
then use [minBound .. maxBound]
if you want to golf it a bit more, then
[minBound..]
works as well.
Wouldn't this create an infinite tail of undefined values?

Hi, Am Samstag, dem 03.12.2022 um 20:12 +0100 schrieb Henning Thielemann:
if you want to golf it a bit more, then
[minBound..]
works as well.
Wouldn't this create an infinite tail of undefined value
That’s what I thought until I learned about this. Anyways, ghci says it doesn’t: ghci> [False .. ] [False,True] ghci> [LT .. ] [LT,EQ,GT] [x..] is just enumFrom x, and the docs for Enum says
enumFrom and enumFromThen should be defined with an implicit bound,
https://hackage.haskell.org/package/base-4.17.0.0/docs/Prelude.html#t:Enum Cheers, Joachim -- Joachim Breitner mail@joachim-breitner.de http://www.joachim-breitner.de/

Derive Enum and Bounded and use [minBound .. maxBound] :: [InputType]
On Wed, Nov 30, 2022 at 10:19 AM PICCA Frederic-Emmanuel
Hello, I have a type like this
data InputType = CristalK6C | MarsFlyscan | MarsSbs | SixsFlyMedH | SixsFlyMedV | SixsFlyMedVEiger | SixsFlyMedVS70 | SixsFlyScanUhv | SixsFlyScanUhv2 | SixsFlyScanUhvTest | SixsFlyScanUhvUfxc | SixsSbsFixedDetector | SixsSbsMedH | SixsSbsMedV | SixsSbsMedVFixDetector deriving (Eq, Show)
I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances. I do not want to type two time the values.
instance Arbitrary InputType where arbitrary = oneof (map pure [ CristalK6C , MarsFlyscan , MarsSbs , SixsFlyMedH , SixsFlyMedV , SixsFlyMedVEiger , SixsFlyMedVS70 , SixsFlyScanUhv , SixsFlyScanUhv2 , SixsFlyScanUhvTest , SixsFlyScanUhvUfxc , SixsSbsFixedDetector , SixsSbsMedH , SixsSbsMedV , SixsSbsMedVFixDetector ] )
Thanks for your help
Frederic _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- brandon s allbery kf8nh allbery.b@gmail.com

Also, you can simplify the "map pure (...)" by using `elements` :)
https://hackage.haskell.org/package/QuickCheck-2.14.2/docs/Test-QuickCheck.h...
On Wed, Nov 30, 2022 at 5:23 PM Brandon Allbery
Derive Enum and Bounded and use [minBound .. maxBound] :: [InputType]
On Wed, Nov 30, 2022 at 10:19 AM PICCA Frederic-Emmanuel
wrote: Hello, I have a type like this
data InputType = CristalK6C | MarsFlyscan | MarsSbs | SixsFlyMedH | SixsFlyMedV | SixsFlyMedVEiger | SixsFlyMedVS70 | SixsFlyScanUhv | SixsFlyScanUhv2 | SixsFlyScanUhvTest | SixsFlyScanUhvUfxc | SixsSbsFixedDetector | SixsSbsMedH | SixsSbsMedV | SixsSbsMedVFixDetector deriving (Eq, Show)
I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances. I do not want to type two time the values.
instance Arbitrary InputType where arbitrary = oneof (map pure [ CristalK6C , MarsFlyscan , MarsSbs , SixsFlyMedH , SixsFlyMedV , SixsFlyMedVEiger , SixsFlyMedVS70 , SixsFlyScanUhv , SixsFlyScanUhv2 , SixsFlyScanUhvTest , SixsFlyScanUhvUfxc , SixsSbsFixedDetector , SixsSbsMedH , SixsSbsMedV , SixsSbsMedVFixDetector ] )
Thanks for your help
Frederic _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
-- brandon s allbery kf8nh allbery.b@gmail.com _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Also, `enumerate` is a common function for `[minBound .. maxBound]`, found in many libraries, e.g. `extra`: https://hackage.haskell.org/package/extra-1.7.12/docs/Data-List-Extra.html#v...

Also, you can simplify the "map pure (...)" by using `elements` :)
thanks another question I have plenty of instance build like this instance Arbitrary TypeA where arbitrary = TypeA <$> arbitrary <*> arbitrary ... <*> arbitrary is there a convenient way to avoid typing this it seems purely mechanical thanks Fred

On Wed, 30 Nov 2022, PICCA Frederic-Emmanuel wrote:
Also, you can simplify the "map pure (...)" by using `elements` :)
thanks
another question I have plenty of instance build like this
instance Arbitrary TypeA where arbitrary = TypeA <$> arbitrary <*> arbitrary ... <*> arbitrary
uncurry TypeA <$> arbitrary uncurry3 TypeA <$> arbitrary would be a bit shorter if this is important.

You can use `generic-arbitrary` like this ```haskell import Test.QuickCheck.Arbitrary.Generic data CristalK6C ... deriving Generic deriving (Arbitrary) via GenericArbitrary CristalK6C ``` On 30.11.2022 21:18, PICCA Frederic-Emmanuel wrote:
Hello, I have a type like this
data InputType = CristalK6C | MarsFlyscan | MarsSbs | SixsFlyMedH | SixsFlyMedV | SixsFlyMedVEiger | SixsFlyMedVS70 | SixsFlyScanUhv | SixsFlyScanUhv2 | SixsFlyScanUhvTest | SixsFlyScanUhvUfxc | SixsSbsFixedDetector | SixsSbsMedH | SixsSbsMedV | SixsSbsMedVFixDetector deriving (Eq, Show)
I want to create a list of each values in order to create meaningfull error message for the user and simplify my Arbitrary instances. I do not want to type two time the values.
instance Arbitrary InputType where arbitrary = oneof (map pure [ CristalK6C , MarsFlyscan , MarsSbs , SixsFlyMedH , SixsFlyMedV , SixsFlyMedVEiger , SixsFlyMedVS70 , SixsFlyScanUhv , SixsFlyScanUhv2 , SixsFlyScanUhvTest , SixsFlyScanUhvUfxc , SixsSbsFixedDetector , SixsSbsMedH , SixsSbsMedV , SixsSbsMedVFixDetector ] )
Thanks for your help
Frederic _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
participants (7)
-
Brandon Allbery
-
Bryan Richter
-
Dan Dart
-
Henning Thielemann
-
Joachim Breitner
-
PICCA Frederic-Emmanuel
-
s9gf4ult