by type checking and rewriting