Could this be improved using template haskell quasi-quotes/splices/etc without changing the type?

predFuse :: Exp -> Exp -> Exp
predFuse p' q' = let
x = mkName "x"
p = mkName "p"
q = mkName "q"
l = LamE [VarP p,VarP q,VarP x] (InfixE (Just (AppE (VarE p) (VarE x))) (VarE '(&&)) (Just (AppE (VarE q) (VarE x))))
in AppE (AppE l p') q'


λ> runQ [| \p q x -> p x && q x |]
LamE [VarP p_0,VarP q_1,VarP x_2] (InfixE (Just (AppE (VarE p_0) (VarE x_2))) (VarE GHC.Classes.&&) (Just (AppE (VarE q_1) (VarE x_2))))

λ> :t runQ [| \p q x -> p x && q x |]
runQ [| \p q x -> p x && q x |]
:: Language.Haskell.TH.Syntax.Quasi m => m Exp

Show thread

I’m now fairly sure this is impossible. I must bow down and accept Q into my heart

Show thread

Someone on Reddit pointed out there’s always unsafePerformIO

Show thread

@dpwiz Do you mean GHC's rewrite rules?

Coincidentally, this is an excerpt of a (not GHC) rewrite rule from a code generator I'm writing

@jmtd I see... Perhaps a less dense code layout would be sufficient.

@dpwiz what is really like is to have the big expression encoded inside oxford brackets ([| \p q x -> ...) which is type Q Exp and to escape the Q monad within the function definition so it remains Exp -> Exp -> Exp

Sign in to participate in the conversation social after-party social