Composition of Logs
You’ve seen how to model types and pure functions as a category. I also mentioned that there is a way to model side effects, or non-pure functions, in category theory. Let’s have a look at one such example: functions that log or trace their execution. Something that, in an imperative language, would likely be implemented by mutating some global state, as in:
string logger; bool negate(bool b) { logger += "Not so! "; return !b; }
You know that this is not a pure function, because its memoized version would fail to produce a log. This function has side effects.