to_gray(N, G) :- N0 is N >> 1, G is N xor N0. from_gray(G, N) :- ( G > 0 -> S is G >> 1, from_gray(S, N0), N is G xor N0 ; N is G ).