Getting list of constructors of a Type

Dear Haskell-Café, As part of my Google Summer of Code, I need to get the list of data constructors of a certain data type on a specific scenario. In my case, I'm getting a value of type Type (by calling exprType). I was expecting to be able to call getInfo directly on that value of type Type, but it doesn't have a name nor the constructors of Type are public. Thus, I'm not able to get that information :( Thanks in advance, Alejandro

I'm not sure what exactly you're trying to do, but with Template Haskell it's easy:
test :: Name -> Q [Dec] test name = do info <- reify name ...
Then call it from another module with the type name as argument (double single quotes):
data Test = A | B
test ''Test
The `Info` type returned by `reify` is described in: http://hackage.haskell.org/package/template-haskell-2.9.0.0/docs/Language-Ha... - Nils Am 03.06.2014 21:13, schrieb Alejandro Serrano Mena:
Dear Haskell-Café, As part of my Google Summer of Code, I need to get the list of data constructors of a certain data type on a specific scenario. In my case, I'm getting a value of type Type (by calling exprType). I was expecting to be able to call getInfo directly on that value of type Type, but it doesn't have a name nor the constructors of Type are public. Thus, I'm not able to get that information :(
Thanks in advance, Alejandro
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Alejandro,
If I understand correctly, you're using the InteractiveEval module from the
GHC API. That module exposes a very specific interface, and sometimes
you'll need to do things beyond that - my approach has then been to look at
the implementation of these things, copy/paste code, and follow the types.
This will take you through other modules like HscTypes and so on, but you
might be able to figure out how to do what you want. The GHC source code
isn't great, but is fairly understandable if you stare at it for long
enough.
I also very highly encourage you to document what you did on the wiki page
for GHC API. The GHC API is in **dire** need of more explained example code
using it.
-- Andrew
On Tue, Jun 3, 2014 at 3:01 PM, Nils Schweinsberg
I'm not sure what exactly you're trying to do, but with Template Haskell it's easy:
test :: Name -> Q [Dec]
test name = do info <- reify name ...
Then call it from another module with the type name as argument (double single quotes):
data Test = A | B
test ''Test
The `Info` type returned by `reify` is described in:
http://hackage.haskell.org/package/template-haskell-2.9. 0.0/docs/Language-Haskell-TH-Syntax.html#t:Info
- Nils
Am 03.06.2014 21:13, schrieb Alejandro Serrano Mena:
Dear Haskell-Café, As part of my Google Summer of Code, I need to get the list of data constructors of a certain data type on a specific scenario. In my case, I'm getting a value of type Type (by calling exprType). I was expecting to be able to call getInfo directly on that value of type Type, but it doesn't have a name nor the constructors of Type are public. Thus, I'm not able to get that information :(
Thanks in advance, Alejandro
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

My aim is to extend the capabilities of ghc-mod to do case split. For that
matter, I'm using the GHC module of the ghc package (the same ghc-mod
uses). Via the exprType function I get a Type value. Then, I would like to
be able to call getInfo (everything from the GHC module) to get the
information of that type.
I'm not sure if using Template Haskell is something I can use, but I will
definitely try :)
2014-06-04 0:01 GMT+02:00 Nils Schweinsberg
I'm not sure what exactly you're trying to do, but with Template Haskell it's easy:
test :: Name -> Q [Dec]
test name = do info <- reify name ...
Then call it from another module with the type name as argument (double single quotes):
data Test = A | B
test ''Test
The `Info` type returned by `reify` is described in:
http://hackage.haskell.org/package/template-haskell-2.9. 0.0/docs/Language-Haskell-TH-Syntax.html#t:Info
- Nils
Am 03.06.2014 21:13, schrieb Alejandro Serrano Mena:
Dear Haskell-Café, As part of my Google Summer of Code, I need to get the list of data constructors of a certain data type on a specific scenario. In my case, I'm getting a value of type Type (by calling exprType). I was expecting to be able to call getInfo directly on that value of type Type, but it doesn't have a name nor the constructors of Type are public. Thus, I'm not able to get that information :(
Thanks in advance, Alejandro
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Alejandro,
From your use case, it looks like you need splitTyConApp (from GHC's Type module). Then, tyConDataCons will do the trick. Perhaps splitTyConApp should be in the GHC module itself. If you think so, submit a bug report and patch, please!
I don't think Template Haskell will work for you here -- mixing calls from the GHC module and TH is a recipe for disaster.
I hope this helps,
Richard
On Jun 4, 2014, at 8:30 AM, Alejandro Serrano Mena
My aim is to extend the capabilities of ghc-mod to do case split. For that matter, I'm using the GHC module of the ghc package (the same ghc-mod uses). Via the exprType function I get a Type value. Then, I would like to be able to call getInfo (everything from the GHC module) to get the information of that type.
I'm not sure if using Template Haskell is something I can use, but I will definitely try :)
2014-06-04 0:01 GMT+02:00 Nils Schweinsberg
: I'm not sure what exactly you're trying to do, but with Template Haskell it's easy: test :: Name -> Q [Dec] test name = do info <- reify name ...
Then call it from another module with the type name as argument (double single quotes):
data Test = A | B
test ''Test
The `Info` type returned by `reify` is described in:
http://hackage.haskell.org/package/template-haskell-2.9.0.0/docs/Language-Ha...
- Nils
Am 03.06.2014 21:13, schrieb Alejandro Serrano Mena: Dear Haskell-Café, As part of my Google Summer of Code, I need to get the list of data constructors of a certain data type on a specific scenario. In my case, I'm getting a value of type Type (by calling exprType). I was expecting to be able to call getInfo directly on that value of type Type, but it doesn't have a name nor the constructors of Type are public. Thus, I'm not able to get that information :(
Thanks in advance, Alejandro
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Alejandro Serrano Mena
writes: Dear Haskell-Café,As part of my Google Summer of Code, I need to get the list of data constructors of a certain data type on a specific scenario.
Hi Alejandro, have you considered Data/Typeable or Generics? I'm not quite sure what you're looking for, but see my (similar?) requests: http://www.haskell.org/pipermail/haskell-cafe/2013-October/111133.html http://www.haskell.org/pipermail/generics/2013-December/000549.html And the helpful replies. Beware re Nils' suggestion that I think TH has changed with GHC7.8 such that type inspection at compile time is not so easy. (Sorry to be vague, and others will no doubt correct me.) AntC
participants (5)
-
Alejandro Serrano Mena
-
Andrew Gibiansky
-
AntC
-
Nils Schweinsberg
-
Richard Eisenberg