Partitioning arbitrary expressions in Haskell quasiquators

Reading through Why it's nice to quote , section 3 gives an example of splicing a variable identifier in a quasivoc.

subst [:lam | $exp:e1 $exp:e2 |] xy = let e1' = subst e1 xy e2' = subst e2 xy in [:lam | $exp:e1' $exp:e2' |] 

I understand why recursive subst calls are made outside of [:lam| ... |] [:lam| ... |] , because the antiVarE function in section 3.2 builds a TH.varE outside the variable name.

My question is how much work will it take to support arbitrary expression loops outside of the variable name?

For instance:

 subst [:lam | $exp:e1 $exp:e2 |] xy = [:lam | $exp:(subst e1 xy) $exp:(subst e2 xy) |] 
+6
source share
1 answer

Answering my question for posterity.

Turns out it was pretty simple. Using the parseExp function in haskell-src-meta , I was able to easily convert the string to an AST fragment.

In the original article, in addition to the parser changes needed to capture an expression string between parentheses, the antiExpE function can be rewritten as such.

 antiExpE :: Exp -> Maybe TH.ExpQ antiExpE (AE v) = case parseExp v of Right exp -> Just . return $ exp Left _ -> Nothing antiExpE = Nothing 
+2
source

All Articles