Nearest-codeword quantization for finite codebooks #
A codebook is a finite table, so the natural quantizer is exhaustive: scan every word, keep the
finite entries, and select one whose denotation is closest to the target. This module provides
that quantizer, nearestCode, together with the theorem that its result minimizes the distance
|x - c| over all finite codewords c.
The tie policy is fixed and simple: the scan runs in ascending order of the unsigned word value, and a candidate replaces the current best only when it is strictly closer. A tie therefore resolves to the lower word. Applications that need a different tie rule, or a rule for reserved words, can still define their own quantizer against the same table.
The scalar type only needs subtraction, negation, and a linear order so that |x - c| is
defined and comparable. No ordered-group axioms are used by the minimality proof.
A word appears in finiteEntries with a value exactly when the table decodes it to that
value.
The first finite codeword whose denotation minimizes |x - c|, scanning words in ascending
unsigned order and replacing the current best only on a strict improvement. Ties resolve to the
lower word. The result is none exactly when the table has no finite entry.
Instances For
Folding closerEntry from a present accumulator always yields a present result.
The fold invariant behind nearestCode: a selected entry comes from the scanned list or the
initial accumulator, and it is at least as close to x as every scanned entry and as the initial
accumulator.
The selected codeword is finite and its denotation is at least as close to x as the denotation
of every finite codeword in the table.
A table with at least one finite word always has a nearest codeword.