solution for exercise 7.3
loop inversion on AST