variant of cata and optimised monad instance