If I have GADT like:
data Pkt (m::Msg) (d::Dir) where
GetResourcesPkt :: Pkt 'ResourcesM 'Ask
MyResourcesPkt :: MyResources -> Pkt 'ResourcesM 'Ans
....
how to get in Template Haskell names GetResolurcesPkt, MyResourcesPkt? I plan to generate case-s in compile time and similar, so I need MyResourcesPkt to generate code like:
case x of
MyResourcesPkt rs -> ...
I think about some function like getCons 'ResourcesM 'Ask => GetResourcesPkt (as Name?) or similar. Is it possible at all?
Maybe I need to know their arity too, if it's impossible to do {} or {..} in TH.
You can inspect the GADT using
reifyor a package liketh-abstractionthat provides more predictable wrappers aroundreify.Best practice is probably to use
th-abstraction, but for your specific example, the wayth-abstractionhandles GADTs might make it more difficult, so maybe you want to stick withreify.I'm not sure how useful a function like
getConswill be, but here's how you could implement it.First, as an important debugging tool, it's helpful to know how to dump the result of
reifyto the console. Unlike many TH functions,reifycan't be run directly inIO(e.g., from the GHCi prompt). Instead, it needs to run in "real" compile-time Template Haskell code in theQmonad, like in the following example:At compile time, this prints the following to the console:
or, reformatted:
So, as a first crack at writing
getCons, you might try:Then, in a separate module, you can use it on your
Pkttype:When compiled, this prints the matched constructor name: