Automatically generate sum type members -> [String]

Can I use either template Haskell, GHC Generics, or something else to get the list of Constructors in Codes? Then I could do `map show codeSumTypeMembers` and get `["A0100A","A0500A"]`? Here's an example of solving this problem manually: data Codes = A0100A | A0500A deriving Show codeExists "A0100A" = True codeExists "A0500A" = True codeExists _ = False main = print $ codeExists xmlElementName where xmlElementName = "A0500A" -- λ> main -- True

readConstr [1] from Data.Data will do the trick. DataType for a type can be retrieved with dataTypeOf. ---- 1: https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Data.html#v:readC... On 5/19/2015 1:20 AM, Cody Goodman wrote:
Can I use either template Haskell, GHC Generics, or something else to get the list of Constructors in Codes? Then I could do `map show codeSumTypeMembers` and get `["A0100A","A0500A"]`?
Here's an example of solving this problem manually:
data Codes = A0100A | A0500A deriving Show
codeExists "A0100A" = True codeExists "A0500A" = True codeExists _ = False
main = print $ codeExists xmlElementName where xmlElementName = "A0500A"
-- λ> main -- True _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On Mon, May 18, 2015 at 7:20 PM, Cody Goodman wrote: Can I use either template Haskell, GHC Generics, or something else to
get the list of Constructors in Codes? Then I could do `map show
codeSumTypeMembers` and get `["A0100A","A0500A"]`? Derive Enum and Bounded as well, and you can then use minBound and maxBound
along with show. This will only work for nullary constructors, though.
IIRC, with Data.Typeable (deriving (Typeable)) you can do something like
this for non-nullary constructors as well. It's more painful, though.
--
brandon s allbery kf8nh sine nomine associates
allbery.b@gmail.com ballbery@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

You can get this information through the Data.Data module. There are two
typeclasses which can be (have to be, I believe) derived for your data
type, Data and Typeable. The Data class lets you access a representation of
your type including type constructors.
To set this up, you need to import Data.Data and enable the
DeriveDataTypeable extension:
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Data
data Codes = A0100A | A0500A deriving (Show, Data, Typeable)
Now you can get a representation of the type:
λ> dataTypeOf (undefined :: Codes)
DataType {tycon = "Main.Codes", datarep = AlgRep [A0100A,A0500A]}
Since your type is an algebraic data type (AlgRep), the DataRep contains
its constructors, which you can inspect with functions from the Data.Data
module.
On Mon, May 18, 2015 at 4:20 PM, Cody Goodman wrote: Can I use either template Haskell, GHC Generics, or something else to
get the list of Constructors in Codes? Then I could do `map show
codeSumTypeMembers` and get `["A0100A","A0500A"]`? Here's an example of solving this problem manually: data Codes = A0100A | A0500A deriving Show codeExists "A0100A" = True
codeExists "A0500A" = True
codeExists _ = False main = print $ codeExists xmlElementName
where xmlElementName = "A0500A" -- λ> main
-- True
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Using GHC Generics:
http://hackage.haskell.org/package/generic-deriving-1.7.0/docs/Generics-Deri...
On Tue, May 19, 2015 at 1:29 AM, Tikhon Jelvis
You can get this information through the Data.Data module. There are two typeclasses which can be (have to be, I believe) derived for your data type, Data and Typeable. The Data class lets you access a representation of your type including type constructors.
To set this up, you need to import Data.Data and enable the DeriveDataTypeable extension:
{-# LANGUAGE DeriveDataTypeable #-} import Data.Data
data Codes = A0100A | A0500A deriving (Show, Data, Typeable)
Now you can get a representation of the type:
λ> dataTypeOf (undefined :: Codes) DataType {tycon = "Main.Codes", datarep = AlgRep [A0100A,A0500A]}
Since your type is an algebraic data type (AlgRep), the DataRep contains its constructors, which you can inspect with functions from the Data.Data module.
On Mon, May 18, 2015 at 4:20 PM, Cody Goodman < codygman.consulting@gmail.com> wrote:
Can I use either template Haskell, GHC Generics, or something else to get the list of Constructors in Codes? Then I could do `map show codeSumTypeMembers` and get `["A0100A","A0500A"]`?
Here's an example of solving this problem manually:
data Codes = A0100A | A0500A deriving Show
codeExists "A0100A" = True codeExists "A0500A" = True codeExists _ = False
main = print $ codeExists xmlElementName where xmlElementName = "A0500A"
-- λ> main -- True _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Also there is good article by Chris Done:
http://chrisdone.com/posts/data-typeable. Hope this will be helpful.
вт, 19 мая 2015 г. в 5:20, Adam Bergmark
Using GHC Generics: http://hackage.haskell.org/package/generic-deriving-1.7.0/docs/Generics-Deri...
On Tue, May 19, 2015 at 1:29 AM, Tikhon Jelvis
wrote: You can get this information through the Data.Data module. There are two typeclasses which can be (have to be, I believe) derived for your data type, Data and Typeable. The Data class lets you access a representation of your type including type constructors.
To set this up, you need to import Data.Data and enable the DeriveDataTypeable extension:
{-# LANGUAGE DeriveDataTypeable #-} import Data.Data
data Codes = A0100A | A0500A deriving (Show, Data, Typeable)
Now you can get a representation of the type:
λ> dataTypeOf (undefined :: Codes) DataType {tycon = "Main.Codes", datarep = AlgRep [A0100A,A0500A]}
Since your type is an algebraic data type (AlgRep), the DataRep contains its constructors, which you can inspect with functions from the Data.Data module.
On Mon, May 18, 2015 at 4:20 PM, Cody Goodman < codygman.consulting@gmail.com> wrote:
Can I use either template Haskell, GHC Generics, or something else to get the list of Constructors in Codes? Then I could do `map show codeSumTypeMembers` and get `["A0100A","A0500A"]`?
Here's an example of solving this problem manually:
data Codes = A0100A | A0500A deriving Show
codeExists "A0100A" = True codeExists "A0500A" = True codeExists _ = False
main = print $ codeExists xmlElementName where xmlElementName = "A0500A"
-- λ> main -- True _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On Tue, May 19, 2015 at 2:20 AM, Cody Goodman
data Codes = A0100A | A0500A deriving Show
codeExists "A0100A" = True codeExists "A0500A" = True codeExists _ = False
data Codes = A0100A | A0500A deriving Read codeExists code = case reads code of [(_, "")] -> True _ -> False Or with -XPatternGuards: codeExists code | [(_, "")] <- reads code = True | otherwise = False

On Tue, May 19, 2015 at 12:03 PM, Anatoly Zaretsky
data Codes = A0100A | A0500A deriving Read
codeExists code = case reads code of [(_, "")] -> True _ -> False
Or with -XPatternGuards:
codeExists code | [(_, "")] <- reads code = True | otherwise = False
Oops, should have checked the types before posting: codeExists code = case reads code :: [(Codes, String)] of [(_, "")] -> True _ -> False Otherwise the compiler could not guess the right Read instance.

Hi, Am Dienstag, den 19.05.2015, 12:03 +0300 schrieb Anatoly Zaretsky:
On Tue, May 19, 2015 at 2:20 AM, Cody Goodman
wrote: data Codes = A0100A | A0500A deriving Show
codeExists "A0100A" = True codeExists "A0500A" = True codeExists _ = False
data Codes = A0100A | A0500A deriving Read
codeExists code = case reads code of [(_, "")] -> True _ -> False
Or with -XPatternGuards:
codeExists code | [(_, "")] <- reads code = True | otherwise = False
since base-4.6, there is readMaybe :: Read a => String -> Maybe a in Text.Read. So it might be nicer to write codeExists code = isJust (readMaybe code :: Maybe Codes) Greetings, Joachim -- Joachim “nomeata” Breitner mail@joachim-breitner.de • http://www.joachim-breitner.de/ Jabber: nomeata@joachim-breitner.de • GPG-Key: 0xF0FBF51F Debian Developer: nomeata@debian.org
participants (8)
-
Adam Bergmark
-
Anatoly Zaretsky
-
Brandon Allbery
-
Cody Goodman
-
David Kraeutmann
-
Geraldus
-
Joachim Breitner
-
Tikhon Jelvis