Groups with a ternary equivalence relation