Would it help to turn this upside down? Create a bottom up rewriter thingy from a partial function and apply that to the ast?