Recover four data bits after one bit flips

Parity checks locate an error in a seven-bit word.

program · output · proof · check · playground


The question

Bits sent over a wire or stored on a disk sometimes flip: a 0 arrives as a 1, or the other way round. A Hamming(7,4) code protects four data bits by sending seven. Data sits at positions 3, 5, 6 and 7. Positions 1, 2 and 4 hold parity bits: extra bits chosen so that certain groups of bits add up to an even number. If one bit flips, some groups become odd, and which groups are odd tells exactly which bit flipped.

Three packets arrive. The first, [1, 0, 1, 1, 1, 1, 0], is damaged.

Can we find the single flipped bit in each packet and recover the original four data bits?


Historical background

In 1950 Richard Hamming published a systematic way to use parity checks to locate and repair a single erroneous bit. The seven-position code in this example uses the position number to decide which of the three parity checks a bit participates in. A flipped bit changes exactly the checks that encode its position. Hamming, Error Detecting and Error Correcting Codes


What we tell eyemind

Each check adds the bits at its positions and takes the remainder after dividing by 2 (modulo 2): 0 if the sum is even, 1 if it is odd.

The syndrome is S1 + 2S2 + 4S4, a number from 0 to 7. Zero means no error was found; any other value is the position of the flipped bit, which the correction rule flips back. The program:

fact(received('damaged', [1, 0, 1, 1, 1, 1, 0]))
fact(received('intact', [1, 0, 1, 1, 0, 1, 0]))
fact(received('last_bit', [0, 0, 0, 0, 0, 0, 1]))
// bits(Word): every item of Word is 0 or 1.
fact(bit(0), bit(1))
fact(bits([]))
implied_by(bits([B, ...Rest]), [bit(B), bits(Rest)])
// correct(Word, Position, Syndrome, Fixed): flip the bit whose position
// equals the syndrome and copy every other bit.
fact(correct([], _, _, []))
implied_by(correct([Bit, ...Rest], Position, Syndrome, [Fixed, ...Tail]), [
  correct_bit(Position, Syndrome, Bit, Fixed), is(Next, add(Position, 1)), correct(Rest, Next, Syndrome, Tail),
])
implied_by(correct_bit(Position, Syndrome, Bit, Fixed), [eq(Position, Syndrome), is(Fixed, sub(1, Bit))])
implied_by(correct_bit(Position, Syndrome, Bit, Bit), ne(Position, Syndrome))
// Parity bits sit at positions 1, 2 and 4; data bits at 3, 5, 6 and 7.
implied_by(decode([B1, B2, B3, B4, B5, B6, B7], Syndrome, Corrected, [D1, D2, D3, D4]), [
  bits([B1, B2, B3, B4, B5, B6, B7]),
  is(S1, mod(add(add(B1, B3), add(B5, B7)), 2)), is(S2, mod(add(add(B2, B3), add(B6, B7)), 2)),
  is(S4, mod(add(add(B4, B5), add(B6, B7)), 2)), is(Syndrome, add(S1, add(mul(2, S2), mul(4, S4)))),
  correct([B1, B2, B3, B4, B5, B6, B7], 1, Syndrome, Corrected),
  unify(Corrected, [_, _, D1, _, D2, D3, D4]),
])
implies([received(Name, Word), decode(Word, Syndrome, Corrected, Payload)], decoded(Name, Syndrome, Corrected, Payload))
query(decoded(Name, Syndrome, Corrected, Payload))

What eyemind concludes

// Answers: each instance of a query() that holds.
decoded('damaged', 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])
decoded('intact', 0, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])
decoded('last_bit', 7, [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])

Each line gives the packet’s name, its syndrome, the corrected word and the four data bits.


Why the assumption matters

Correction assumes that the transmitted word was a valid codeword and that at most one bit flipped. Multiple errors can produce a plausible syndrome and be miscorrected. This seven-bit code has no extra overall parity bit for distinguishing every double-bit error from a single-bit error.


The proof, line by line

eyemind --proof writes down how it reached each answer. The full proof has 82 lines and is 6 KB (read it all). Below are its claims, its steps, and its certificate up to the end of the damaged packet, verbatim:

// Answers: each instance of a query() that holds.
decoded('damaged', 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])
decoded('intact', 0, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])
decoded('last_bit', 7, [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])

// Steps: each derived fact and what it rests on: the clause or control that
// justifies it, the bindings and the goals it uses.
step(decoded('damaged', 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0]), clause(13), {Name: 'damaged', Word: [1, 0, 1, 1, 1, 1, 0], Syndrome: 5, Corrected: [1, 0, 1, 1, 0, 1, 0], Payload: [1, 0, 1, 0]}, [received('damaged', [1, 0, 1, 1, 1, 1, 0]), decode([1, 0, 1, 1, 1, 1, 0], 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])])
step(decoded('intact', 0, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0]), clause(13), {Name: 'intact', Word: [1, 0, 1, 1, 0, 1, 0], Syndrome: 0, Corrected: [1, 0, 1, 1, 0, 1, 0], Payload: [1, 0, 1, 0]}, [received('intact', [1, 0, 1, 1, 0, 1, 0]), decode([1, 0, 1, 1, 0, 1, 0], 0, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])])
step(decoded('last_bit', 7, [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0]), clause(13), {Name: 'last_bit', Word: [0, 0, 0, 0, 0, 0, 1], Syndrome: 7, Corrected: [0, 0, 0, 0, 0, 0, 0], Payload: [0, 0, 0, 0]}, [received('last_bit', [0, 0, 0, 0, 0, 0, 1]), decode([0, 0, 0, 0, 0, 0, 1], 7, [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])])

// Certificate: the derived facts and every answer of each call the questions make.
derived(decoded(A, B, C, D), [decoded('damaged', 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0]), decoded('intact', 0, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0]), decoded('last_bit', 7, [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])])
called(decoded(A, B, C, D), [decoded('damaged', 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0]), decoded('intact', 0, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0]), decoded('last_bit', 7, [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])])
called(decode([1, 0, 1, 1, 1, 1, 0], E, F, G), [decode([1, 0, 1, 1, 1, 1, 0], 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])])
called(bits([1, 0, 1, 1, 1, 1, 0]), [bits([1, 0, 1, 1, 1, 1, 0])])
called(bits([0, 1, 1, 1, 1, 0]), [bits([0, 1, 1, 1, 1, 0])])
called(bits([1, 1, 1, 1, 0]), [bits([1, 1, 1, 1, 0])])
called(bits([1, 1, 1, 0]), [bits([1, 1, 1, 0])])
called(bits([1, 1, 0]), [bits([1, 1, 0])])
called(bits([1, 0]), [bits([1, 0])])
called(bits([0]), [bits([0])])
called(bits([]), [bits([])])
called(correct([1, 0, 1, 1, 1, 1, 0], 1, 5, H), [correct([1, 0, 1, 1, 1, 1, 0], 1, 5, [1, 0, 1, 1, 0, 1, 0])])
called(correct_bit(1, 5, 1, I), [correct_bit(1, 5, 1, 1)])
called(correct([0, 1, 1, 1, 1, 0], 2, 5, J), [correct([0, 1, 1, 1, 1, 0], 2, 5, [0, 1, 1, 0, 1, 0])])
called(correct_bit(2, 5, 0, K), [correct_bit(2, 5, 0, 0)])
called(correct([1, 1, 1, 1, 0], 3, 5, L), [correct([1, 1, 1, 1, 0], 3, 5, [1, 1, 0, 1, 0])])
called(correct_bit(3, 5, 1, M), [correct_bit(3, 5, 1, 1)])
called(correct([1, 1, 1, 0], 4, 5, N), [correct([1, 1, 1, 0], 4, 5, [1, 0, 1, 0])])
called(correct_bit(4, 5, 1, O), [correct_bit(4, 5, 1, 1)])
called(correct([1, 1, 0], 5, 5, P), [correct([1, 1, 0], 5, 5, [0, 1, 0])])
called(correct_bit(5, 5, 1, Q), [correct_bit(5, 5, 1, 0)])
called(correct([1, 0], 6, 5, R), [correct([1, 0], 6, 5, [1, 0])])
called(correct_bit(6, 5, 1, S), [correct_bit(6, 5, 1, 1)])
called(correct([0], 7, 5, T), [correct([0], 7, 5, [0])])
called(correct_bit(7, 5, 0, U), [correct_bit(7, 5, 0, 0)])
called(correct([], 8, 5, V), [correct([], 8, 5, [])])

It has three parts:

Read the damaged packet’s part as sentences:

The other two packets go the same way, with syndrome 0 (nothing flipped) and syndrome 7 (the last bit flipped).


Checked: what the report says

A proof is only worth something if someone else can check it. eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself. It answers the question decoded(A, B, C, D) from the listing, requires the listing to be closed (rule 13 derives no decoded fact it leaves out), and replays each listed fact’s step: it runs rule 13 again, taking the answer of each decode call from its table. Each table it consults is checked by expanding its call one level against the program, with every parity sum, comparison and bit flip calculated again and every inner call answered from its table. A table the check does not use fails it. This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
decoded('damaged', 5, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])
decoded('intact', 0, [1, 0, 1, 1, 0, 1, 0], [1, 0, 1, 0])
decoded('last_bit', 7, [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0])

verdict('checked')

The verdict is checked: the three decodings follow from the program, and they are every answer of the original question. It certifies the decoder’s result, subject to the channel assumption above.

The example’s regression test covers all 16 payloads with no error and with each of the seven possible single-bit errors: 128 cases.


Try it

eyemind examples/hamming-code.js
eyemind --proof examples/hamming-code.js
eyemind --goal "decode([0, 0, 1, 0, 0, 0, 0], S, C, D)" examples/hamming-code.js

The syndrome is 3; correction recovers the all-zero word and payload.

To see the single-error assumption fail, take the intact word and flip two bits, 6 and 7: decoding [1, 0, 1, 1, 0, 0, 1] gives syndrome 1, so eyemind “corrects” bit 1 and reports payload [1, 0, 0, 1] instead of the true [1, 0, 1, 0]. A word containing anything but 0 and 1, such as [0, 0, 0, 0, 0, 0, 2], gets no answer at all.


Takeaway

A few extra bits are enough to find and fix a single error. The proof shows which checks failed and which bit they point to, so the correction is never just “trust the decoder”.