refine invariant checking in `Writer` and `PrettyWriter`