Good evening! I wrote some code which leveraged Template Haskell some years ago, but I did not used Typed expressions, and I’m now wanting to explore them in order to see if I can get some value out of them.
I’m having a lot of trouble getting started. I like to poke at the expression trees in GHCi to get a feel for things, but nothing I’ve tried this evening has let me inspect a Typed expression.
I’m using template-haskell-2.16.0.0 and ghc-9.0.2, both out of date but I don’t think that’s the problem specifically. (These happen to be the versions in the stack snapshot that the program I’m building on top of uses)
Looking at the haddock docs for Typed TH, there’s a relatively early example of pretty-printing a TExp Language.Haskell.TH
It’s presented in an unusual way, to demonstrate an invalid typed-expression being caught:
>>> fmap ppr $ runQ [|| True == $$( [|| "foo" ||] ) ||]
<interactive> error:
• Couldn't match type ‘[Char]’ with ‘Bool’
however if you adjust that example to have a correctly-typed quasi-quote, you get a more fundamental error
λ> fmap ppr $ runQ [|| True ||]
<interactive>:120:17: error:
• Couldn't match expected type: Q a0
with actual type: Code m0 Bool
In other words, I think this example in the TH docs is fundamentally broken.
Moving on to try and figure out something myself, I first need to get out of the “Code” monad. examineCode :: m (TExp a)
seems to fit the bill perfectly. Alas, if I pass the result through any combination of runQ, pprint, etc that I can think of, I get nothing - nothing is printed. No error, just emptiness.
I also can’t find any exposed typed expression constructors, so I can’t figure out manually building a simple expression tree as I have done with TH’s Exp types.
Can anyone give me a clue to get started?
Thanks!