The Quote class implements the minimal interface which is necessary for desugaring quotations.
The
Monad msuperclass is needed to stitch together the different AST fragments.newName is used when desugaring binding structures such as lambdas to generate fresh names.
Therefore the type of an untyped quotation in GHC is `Quote m => m Exp`
For many years the type of a quotation was fixed to be `Q Exp` but by more precisely specifying the minimal interface it enables the Exp to be extracted purely from the quotation without interacting with Q.
Methods
newName :: String -> m NameGenerate a fresh name, which cannot be captured.
For example, this:
f = $(do nm1 <- newName "x" let nm2 = mkName "x" return (LamE [VarP nm1] (LamE [VarP nm2] (VarE nm1))) )will produce the splice
f = \x0 -> \x -> x0In particular, the occurrence
VarE nm1refers to the bindingVarP nm1, and is not captured by the bindingVarP nm2.Although names generated by
newNamecannot be captured, they can capture other names. For example, this:g = $(do nm1 <- newName "x" let nm2 = mkName "x" return (LamE [VarP nm2] (LamE [VarP nm1] (VarE nm2))) )will produce the splice
g = \x -> \x0 -> x0since the occurrence
VarE nm2is captured by the innermost binding ofx, namelyVarP nm1.