12 Implementing Representation Invariants

Implementing the representation invariant is good!

Idiom:

let rep_ok (x : t) : t =
    if (* check_ri *) then x
    else failwith "RI"

This is another example of defensive programming.

Let's implement it for ListSet.

This is an expensive way to code. You probably don't want to do this in a production enviorment. Possibly, you want to be able to turn on rep_ok, or replace it with something similar. For example, you could replace rep_ok with let rep_ok lst = lst, then if you ever need to do a hotfix, you can find the bug right away. This is another really good example of defensive programming.