I don't think the GHC API provides an AST interface (maybe this is wrong), but Template Haskell does. If you create expressions using a structure Language.Haskell.TH Exp, you can create functions / declarations and use them with syntax $(someTHFunction).
A pretty serious caveat is that TH only works at compile time, so you need to pre-generate everything. If you want to use TH at run time, I think you will need to pretty much print the haskell AST template, and then use the GHC API in the resulting string.
source
share