Mapping Positions Through Unicode Normalization
This doc explains how we map a position in a normalized string back to a position in the caller's original input. Read this if you're working on NormalizedPositionMap, want to know why the position translation is designed the way it is, or want to be convinced that position translation works.
If you just want to use normalization in a grammar, start with these:
- Primer3.md explains what normalization is and how to pick a form.
- UnicodeInternalsArchitecture.md, Normalization
explains how a grammar selects a normalization form and how
Compilenormalizes rule literals into that form. - Primer4.md shows security uses of FormKC.
- UnicodeGotchas.md lists what normalization can't fix.
Background
Unicode lets you write the same visible character multiple ways. The letter "é" can be stored as one .NET char (U+00E9) or as two chars ("e" plus U+0301, the combining acute). Both render identically. The "fi" ligature can be one char (U+FB01) or two ("f" plus "i"). A Korean syllable can be one precomposed character or several separate component characters.
Normalization converts text to one chosen representation. .NET's string.Normalize(NormalizationForm form) does this. The four forms split into two pairs:
FormCandFormDare the canonical forms. They convert between precomposed and decomposed Unicode (like "é" turning into "e" plus combining acute, or back). They don't expand ligatures, don't change ASCII width, and don't change anything visible.FormKCandFormKDare the compatibility forms. They do everything the canonical forms do, then go further. They expand ligatures ("fi" becomes the two characters "fi"), unify half-width and full-width Latin, flatten superscripts and other typographic variants, and handle other "looks similar but stored differently" cases.
An extended grapheme cluster is Unicode's default approximation of one
user-perceived character, even when it's stored as multiple chars. "é" is one
extended grapheme cluster whether stored as 1 char or 2. In this document,
grapheme and cluster are readability shorthands for the extended grapheme
clusters produced by the parser's GraphemeSegmentation. They don't mean a
language-specific linguistic grapheme. The proof uses the full term wherever
the distinction matters.
The problem
By default, the parser normalizes input before lexing, so the positions the engine works with are offsets into the normalized string. The caller wants the corresponding position in the original string they started with, so editor highlighting points at the right place. This comes up two ways: a failure position when parsing fails, and a symbol's source range (Symbol.SourceRange) on a successful parse. Both go through NormalizedPositionMap.TranslateToOriginal.
Two examples show why this isn't trivial.
The "fi" ligature: the "fi" ligature (one char) becomes "fi" (two chars) under FormKC. If the lexer fails at position 1 in the normalized string (between the 'f' and 'i'), there's no separate 'i' in the original. Both came from the one ligature char. The right answer is position 0 in the original, so editor highlighting points at the whole ligature.
Korean compatibility jamo: the two characters U+3131 and U+314F (Korean compatibility jamo, displayed as "ㄱㅏ") compose into one syllable character 가 (U+AC00) under FormKC. Two original chars become one normalized char. If the lexer fails at position 1 in the normalized string (end of 가), the right answer in the original is position 2 (after both original chars). Simple char counting gets this wrong.
Why not use ICU?
ICU (International Components for
Unicode) is the Unicode Consortium's implementation library. Its
Normalizer2
UTF-8 normalization API can populate an
Edits
object that records how source spans correspond to changed and unchanged result
spans. If this project always normalized through ICU, it could use that mapping
directly.
This parser, however, supports both its built-in normalizer and
.NET's String.Normalize.
The .NET API returns the normalized string but exposes no edit map. Even when
the .NET runtime uses ICU internally, its edits aren't exposed through
String.Normalize. The normalizer built into the parser could produce its own edit map, but
runtime mode would still need another solution.
ICU therefore demonstrates an alternative approach that is possible due to the data it has available. Unfortunately, it isn't one this parser can use in all supported configurations, so we need an alternative. Because it uses data that isn't available after normalization is done, it also doesn't prove the validity of the comparison-based algorithm used here. The validity of the algorithm this parser uses is proved independently below.
The mapping algorithm
Let N be one Unicode normalization operation: FormC, FormD, FormKC, or
FormKD.
Let O be the original string and let Z = N(O) be the normalized string.
Assume that normalization succeeds and every call uses this same N.
Succeeding also means O has no unpaired surrogates (the appendix
explains why). Because
.NET stores strings as UTF-16, the algorithm works with UTF-16 code units, and
compares them ordinally (meaning as exact byte value comparisons).
The algorithm divides O and Z into paired spans. Each original span
contains one or more whole extended grapheme clusters, and its paired span is
its normalization. The corresponding spans are only valid when this is true:
O: | C0 | C1 | ... | Ck |
Z: | N(C0) | N(C1) | ... | N(Ck) |
The vertical lines are paired boundaries. Boundary positions are UTF-16 indexes because that's the coordinate system used by .NET. Every boundary used by the algorithm is also a Unicode code-point boundary. A span is the text between two boundaries.
For FormKC and FormKD, one original extended grapheme cluster may not be enough
to form the next pair of boundaries since those forms can convert multiple graphemes to one normalized grapheme. The algorithm therefore starts with the next unclaimed
original grapheme, normalizes it, and compares the result with the next
unclaimed text in Z. If no prefix of the unclaimed text in Z matches, the algorithm adds another whole
original grapheme and tries again. If a match is found, their endpoints are the
proposed next pair of boundaries.
The comparison alone establishes only that N(C) appears as a prefix of the unclaimed text in Z. The
important question is whether that match could be accidental: could accepting
it leave a suffix of Z that isn't the normalization of the remaining suffix
of O? The proof below shows that this can't happen, and thus that
continuing the algorithm on the remaining suffix of O will segment the
entire string.
Proof of correctness
The theorem to prove is that the algorithm terminates and produces zero or more chunks. For nonempty input, write them as:
O = C0 + C1 + ... + Ck
such that:
Z = N(C0) + N(C1) + ... + N(Ck)
For empty input, the list of chunks is empty and both concatenations are the empty string.
Furthermore, at every accepted pair of boundaries, the entire original string from its start to the original boundary must normalize to the normalized string from its start to the corresponding normalized boundary. Likewise, the original text after the boundary must normalize to the normalized text after its corresponding boundary. This stronger statement rules out accidental prefix matches and allows the same operation to be repeated iteratively on each remaining suffix until the end of the string.
Results used by the proof
The proof uses Lemmas 0, 1, 7, 8, and 9 below. All five are proved in the appendix, with the relevant Unicode citations.
Lemma 0: Normalization and decomposition leave the empty string unchanged (proof). If
εis the empty string, thenN(ε) = εandD(ε) = ε.Lemma 1: Extended grapheme cluster rules apply directly to any string (proof). Each candidate
Cis one or more extended grapheme clusters found by segmentingOas it is, without normalizing it first. This result says that's the segmentation Unicode defines.Lemma 7: A code-point-aligned substring of normalized text is normalized (proof). Every prefix of
Zused below is code-point-aligned (Lemma 2) and, because it's a substring of normalized text, is therefore normalized.Lemma 8: Normalizing a concatenation directly equals normalizing its parts, then the whole (proof). For any strings
XandY:N(X + Y) = N(N(X) + N(Y))The outer call to
Non the right side is essential. Unicode doesn't guarantee thatN(X) + N(Y)is normalized, as discussed in the linked proof.Lemma 9: The unchanged-prefix lemma (proof). If
QandVare normalized and the following equality holds:N(Q + V) = Q + S,then the suffix must be:
S = VThis lemma doesn't assume that no normalization operation crossed the join. It proves that, if the complete normalized prefix is unchanged, any such interaction can't leave a different normalized suffix behind.
The invariant
Throughout the proof, + means string concatenation. A vertical bar | marks
a boundary between two concatenated strings. Thus, O = P + U and O = P | U state the same equality: O consists
of P immediately followed by U. The proof uses the two notations
interchangeably, using | when the boundary itself is important.
Now, for the invariant: Suppose the algorithm has already accepted an original prefix P (which might be empty). Let U be
all the original text that remains. The accepted pair of boundaries must
satisfy:
O = P | U
Z = N(P) | N(U) (1)
This is the algorithm's invariant. It says that the prefix of Z ending at the
accepted boundary is exactly N(P), the normalization of the original prefix
P. It also says that the complete unclaimed suffix of Z is exactly N(U),
the normalization of the unclaimed original suffix U. This is what allows
the algorithm to repeat the same process on U to find the remaining pairs of
boundaries.
Initially, P = ε and U = O, so
Lemma 0
gives Z = N(O) = N(P) + N(U). The invariant therefore holds. If O is
empty, the algorithm immediately returns no chunks and maps its only boundary,
offset zero, to offset zero. The remaining proof assumes that U is nonempty.
A successful comparison preserves the invariant
The algorithm chooses a candidate C from the beginning of U, initially one
whole extended grapheme cluster. The cluster comes from segmenting O as it
is, which
Lemma 1
shows gives the segmentation Unicode defines. Let R be everything after C:
U = C + R
Recall the invariant:
O = P | U
Z = N(P) | N(U)
Substituting U = C + R gives:
O = P | C + R
Z = N(P) | N(C + R) (2)
The algorithm now normalizes the candidate C by itself, producing N(C).
It compares that result ordinally with the same number of UTF-16 code units at
the beginning of N(C + R), the unclaimed suffix of Z in equation (2).
The comparison succeeds only if those code units are exactly equal, so a
successful comparison establishes that N(C) is a prefix of N(C + R). Let
S be everything in that suffix after the exact match:
O: | P | C | R |
Z: | N(P) | N(C) | S |
In the invariant's notation, that diagram says:
O = P | C + R
Z = N(P) | N(C) + S (3)
Equations (2) and (3) both describe Z, and both begin with the same accepted
prefix N(P), so the text after that prefix must be equal too:
N(C + R) = N(C) + S (4)
Now, Lemma 8,
“Normalizing a concatenation directly equals normalizing its parts, then the whole,”
states that for any strings X and Y:
N(X + Y) = N(N(X) + N(Y))
Apply the rule with X = C and Y = R:
N(C + R) = N(N(C) + N(R)) (5)
Equations (4) and (5) have the same left-hand side, so their right-hand sides are equal:
N(N(C) + N(R)) = N(C) + S (6)
“Lemma 9: The unchanged-prefix lemma” states:
If Q and V are normalized and N(Q + V) = Q + S, then S = V
Apply the lemma with Q = N(C) and V = N(R). We must verify all three of its
premises to use that lemma:
N(C)is normalized because it's a normalization result.N(R)is normalized because it's a normalization result.Equation (6) above establishes the third premise:
N(N(C) + N(R)) = N(C) + S
All three premises hold, so Lemma 9 gives:
S = N(R) (7)
This proves that the remaining suffix S of Z is exactly N(R), which is
the normalization of the remaining suffix R of O. Equations (4) and (7)
together say N(C + R) = N(C) + N(R), so the unclaimed suffix of Z in
equation (2) splits into those two pieces. The accepted boundary hasn't moved
yet, so the complete state is now:
O = P | C + R
Z = N(P) | N(C) + N(R) (8)
^ current accepted pair of boundaries
To accept C and move the invariant to the next boundary, we must convert this
to the invariant form, which is:
O = P + C | R
Z = N(P + C) | N(R) (9)
Equation (8) says that the prefix of Z ending immediately after the matched
N(C) is N(P) + N(C). Before accepting the proposed boundary after C,
equation (9) requires us to prove that this prefix is equal to N(P + C).
UAX #15 explains why that equality requires proof:
“In using normalization functions, it is important to realize that none of the Normalization Forms are closed under string concatenation.”
(from UAX #15 for Unicode 16.0, Concatenation of Normalized Strings)
Therefore, even though N(P) and N(C) are each normalized, their
concatenation may not be. So we need to prove that:
N(P + C) = N(P) + N(C)
Recall from equation (8) that:
Z = N(P) + N(C) + N(R)
The prefix N(P) + N(C) starts at the beginning of Z and ends after the
complete normalization result N(C), so both of its ends are code-point
boundaries (Lemma 2). It's
therefore a code-point-aligned substring of normalized Z and is itself
normalized by “Lemma 7: A substring of normalized text is normalized.”
In other words:
N(N(P) + N(C)) = N(P) + N(C) (10)
Recall that we are trying to prove N(P + C) = N(P) + N(C).
To finish the proof, we must connect N(P + C) to N(N(P) + N(C)).
Apply Lemma 8,
“Normalizing a concatenation directly equals normalizing its parts, then the whole,”
with X = P and Y = C:
N(P + C) = N(N(P) + N(C)) (11)
Equation (10) shows that the right side of equation (11) is equal to
N(P) + N(C), giving:
N(P + C) = N(P) + N(C) (12)
Equation (12) is the proof we were looking for. It holds here because its
right-hand side is a code-point-aligned substring of the normalized string
Z. It isn't a general rule that N(P + C) = N(P) + N(C).
Now, recall that equation (8) gives the current known state:
O = P | C + R
Z = N(P) | N(C) + N(R)
By equation (12), we can replace N(P) + N(C) with N(P + C). Moving C into
the accepted prefix then gives:
O = P + C | R
Z = N(P + C) | N(R) (13)
^ newly accepted pair of boundaries
Equation (13) is the original invariant, now at the newly accepted pair of
boundaries. The algorithm can therefore accept C and repeat the same
operation with P + C as the accepted prefix and R as the unclaimed suffix.
Notice what this proof does and doesn't establish. It proves that the accepted
prefix of Z is exactly N(P + C) and that the text remaining in Z is
exactly N(R). It doesn't claim that no combining mark moved across the join
during an intermediate normalization step. Such movement can occur and later
become invisible, as the
example in Lemma 9's proof
shows. Lemma 9 is precisely what makes
the conclusion valid without tracking where each code point came from.
The search can't get stuck
The preceding section shows what happens when a comparison succeeds. Let's now look at what happens when it fails.
Recall the full invariant at the currently accepted pair of boundaries:
O = P | U
Z = N(P) | N(U)
Here, P is the accepted original prefix and U is the unclaimed original
suffix. The candidate then splits U as:
U = C + R
Here, C is the candidate being tested and R is the original text after it.
When a comparison fails, the algorithm moves the next nonempty extended
grapheme cluster from R into C and tries again. If every shorter candidate
fails, C eventually contains all of U, leaving no original text after it:
C = U
R = ε
N(R) = ε
The last equality is Lemma 0 applied to the empty remainder.
The invariant says that the unclaimed suffix of Z is N(U). Because the
final candidate is C = U, its normalization is the same string:
remaining Z = N(U) = N(C)
That final candidate must match. The original string contains finitely many
extended grapheme clusters, so the algorithm can't keep growing C forever
and can't run out of text without finding a successful candidate.
The algorithm first tests the next original cluster alone, then the next two clusters together, and so on. Therefore, the first candidate that matches contains the fewest possible whole original clusters. Every candidate is built from whole clusters, so none can end in the middle of one.
Proof summary
Initially the invariant is true. Every successful comparison preserves it at a later pair of boundaries, and a successful candidate is always eventually found. Every accepted candidate consumes at least one original extended grapheme cluster, so only finitely many iterations are possible. By induction, the algorithm reaches the ends of both strings and produces:
O = C0 + C1 + ... + Ck
Z = N(C0) + N(C1) + ... + N(Ck)
At every intermediate cut, not merely at the end, the original prefix and
suffix normalize to the corresponding prefix and suffix of Z. Thus every
accepted pair of boundaries is valid, every accepted original span is locally
shortest among the whole-cluster candidates, and the algorithm consumes all of
O and Z.
Turning the paired boundaries into a position
If the requested position in Z is exactly an accepted boundary, the algorithm
returns its paired boundary in O. If it lies strictly inside an accepted
normalized span N(Ci), normalization may provide no exact original boundary
for it, so the algorithm returns the beginning of the paired original span Ci.
This approach is the project's position convention, not a further Unicode
theorem.
Optimization for FormC/D
The general comparison algorithm proved above works for FormC and FormD.
However, a simpler algorithm is possible for FormC and FormD due to the
properties of Unicode. The simpler algorithm takes one extended grapheme
cluster from O and one extended grapheme cluster from Z, and pairs them
without performing the comparison. It can repeat that step until both strings
end.
For this optimization to be valid, we must establish both of the following statements from the Unicode Standard:
OandZcan each be segmented directly into extended grapheme clusters.- The resulting clusters correspond one-for-one and in the same order.
Requirement 1: O and Z can each be segmented directly
Lemma 1
establishes this. UAX #29 defines extended grapheme cluster boundaries on
FormD text, but its rules can be applied directly to a string in any form and
give the same segmentation. So O, which might not be in any normalization
form at all, and Z, whichever form N produced, can each be segmented as
they are.
This proves only that each segmentation can be performed directly. It doesn't,
by itself, prove that the first extended grapheme cluster of O pairs with the
first extended grapheme cluster of Z, the second pairs with the second, and
so on.
Requirement 2
proves that.
Requirement 2: The resulting clusters correspond one-for-one and in the same order
UAX #29 states the property needed for this requirement in two places. The Grapheme Cluster Boundaries section describes it:
“A key feature of Unicode grapheme clusters (both legacy and extended) is that they remain unchanged across all canonically equivalent [italics added] forms of the underlying text. Thus the boundaries remain unchanged whether the text is in NFC or NFD. Using a grapheme cluster as the fundamental unit of matching thus provides a very clear and easily explained basis for canonically equivalent matching. This is important for applications from searching to regular expressions.”
(from UAX #29 for Unicode 16.0, Grapheme Cluster Boundaries)
The implementation notes say the same thing more precisely, and spell out that the matching boundaries can sit at different offsets:
“The boundary specifications are stated in terms of text normalized according to Normalization Form NFD (see Unicode Standard Annex #15, “Unicode Normalization Forms” [UAX15]). In practice, normalization of the input is not required. To ensure that the same results are returned for canonically equivalent text (that is, the same boundary positions will be found, although those may be represented by different offsets), the grapheme cluster boundary specification has the following features:”
- “There is never a break within a sequence of nonspacing marks.”
- “There is never a break between a base character and subsequent nonspacing marks.”
(from UAX #29 for Unicode 16.0, Implementation Notes, Normalization)
An extended grapheme cluster is the span between two consecutive extended-
grapheme-cluster boundaries. So if O and Z are canonically equivalent,
these statements give them the same boundary positions, possibly at different
UTF-16 offsets, and the spans between those boundaries (the extended grapheme
clusters) correspond in the same order.
We must therefore prove that O and Z are canonically equivalent. The
Unicode Standard defines canonical equivalence precisely:
“D70 Canonical equivalent: Two character sequences are said to be canonical equivalents if their full canonical decompositions are identical.”
(from The Unicode Standard 16.0, Section 3.7.2, definition D70)
The standard explains what full means for decomposition:
“A full decomposition of a character sequence results from decomposing each of the characters in the sequence until no characters can be further decomposed.”
(from The Unicode Standard 16.0, Section 3.7, definition D64)
D68 defines canonical decomposition as performing that recursive canonical decomposition and then applying canonical ordering:
“D68 Canonical decomposition: The decomposition of a character or character sequence that results from recursively applying the canonical mappings found in the Unicode Character Database and those described in Section 3.12, Conjoining Jamo Behavior, until no characters can be further decomposed, and then reordering nonspacing marks according to Section 3.11, Normalization Forms.”
(from The Unicode Standard 16.0, Section 3.7.2, definition D68)
Finally, D118 identifies FormD as that canonical decomposition operation:
“D118 Normalization Form D (NFD): The Canonical Decomposition of a coded character sequence.”
(from The Unicode Standard 16.0, Section 3.11.7, definition D118)
In summary, D64 and D68 specify the full, canonically ordered decomposition,
D118 identifies its result as FormD, and D70 says that equality of those
decompositions is canonical equivalence. Therefore, proving that O and Z
are canonically equivalent reduces to proving:
FormD(O) = FormD(Z)
Lemma 3 already proves
this. Its equation (A1) states, for every string X:
D(N(X)) = D(X) (A1)
D is FormD whenever N is FormC or FormD, which is the case here. Setting
X = O gives:
FormD(N(O)) = FormD(O)
Z is defined as N(O), so the left side is FormD(Z):
FormD(Z) = FormD(O)
That's the equality the goal above asked for, so O and Z are canonically
equivalent under definition D70, for FormD as well as FormC. The UAX #29
statements quoted at the beginning of this requirement therefore apply
directly: their extended-grapheme-cluster boundaries correspond one-for-one in
left-to-right order, although corresponding boundaries may occur at different
UTF-16 offsets. Because each extended grapheme cluster is the span between two
consecutive boundaries, the clusters also correspond one-for-one in the same
order.
Write those clusters as:
O = G0 + G1 + ... + Gm
Z = H0 + H1 + ... + Hm
There's one Hi for every Gi. This correspondence (not merely the
direct-segmentation rule in
Requirement 1)
is what allows taking one extended
grapheme cluster from each string on every iteration and pairing them as
canonically equivalent spellings of the same cluster. Each side advances by
the UTF-16 length of its own cluster, so the two offsets don't have to be equal.
Conclusion: why the FormC/FormD algorithm is valid
Requirement 1
establishes that O and Z can each be segmented directly.
Requirement 2
establishes that canonical normalization preserves those
clusters in the same order, so there's exactly one Hi for every Gi.
The algorithm may therefore take one extended grapheme cluster from each
string and pair them on every iteration. The two clusters may occupy different
numbers of UTF-16 code units, so their numeric offsets may differ, but each
side advances by its own cluster's length. Because the two strings have the
same number of clusters, both sides reach the end together. The boundary after
each Hi therefore maps to the boundary after its paired Gi. A position
inside Hi maps to the start of Gi, as required by the position-mapping rule.
Why this simple algorithm doesn't work for FormKC and FormKD
UAX #29's guarantee applies to canonical equivalence. FormKC and FormKD also apply compatibility mappings, so their results don't have to be canonically equivalent to the original. Compatibility normalization can turn one original extended grapheme cluster into several normalized clusters or several original clusters into one normalized cluster. A one-for-one lockstep algorithm therefore can't be used for these forms. They use the comparison algorithm proved above.
Caveats
Two things about the proof above are worth noting.
Caveat 1: the guarantee is descriptive prose, not a conformance clause
UAX #29 explicitly states that grapheme clusters remain unchanged across canonically equivalent forms, but presents that guarantee in explanatory prose rather than as a separately numbered conformance requirement. This proof treats it as a promised property of Unicode's default extended-grapheme-cluster rules and data.
Caveat 2: neither segmenter this project ships runs that exact configuration
The guarantee is for UAX #29's
default extended-grapheme-cluster rules
over Unicode's own data, and neither segmenter this project ships runs
exactly that configuration. The built-in GraphemeSegmentation omits GB9c to
match .NET 10, and the runtime StringInfo segmenter has whatever rule set
and data version the installed .NET has, which may not match the normalizer's.
The gap is small: the built-in segmenter runs the Unicode 16
rules over the Unicode 16 data minus GB9c, a rule that only joins more text
into a cluster, and .NET 10's StringInfo is the same rule set over the same
data (the differential tests under Lexing/Unicode ensure it). Rather than
argue that the missing rule can't matter, the test project checks the
property directly. NormalizationTests compares the lockstep walker against
a brute-force reference that never assumes the property, for FormC and FormD
over the curated example rows, and the opt-in NormalizationConformanceTests
sweep does the same over every line of the Unicode 16 normalization
conformance file, against whichever segmenter and normalizer the test process
resolves to.
Appendix: Unicode details behind the comparison proof
This appendix proves the ten lemmas that the main comparison proof and the FormC/FormD optimization rely on, each from definitions and statements in official Unicode sources.
Several of the lemmas compare strings through their fully decomposed forms and use N and D to indicate this for shorthand. For
the selected normalization operation N, let D denote its corresponding
Unicode-defined fully decomposed form:
Selected N |
D |
|---|---|
| FormC or FormD | FormD |
| FormKC or FormKD | FormKD |
The lemmas are:
- Lemma 0:
Normalization and decomposition leave the empty string unchanged:
N(ε) = εandD(ε) = ε. - Lemma 1: Extended grapheme cluster rules can be applied directly to any string, whatever its normalization form.
- Lemma 2: Every cut used by the mapping algorithm is at a code-point boundary.
- Lemma 3:
Normalizing a string doesn't change its decomposition:
D(N(X)) = D(X). - Lemma 4: Canonical ordering is stable: code points with equal combining classes don't exchange places.
- Lemma 5:
Decomposing a concatenation is equivalent to concatenating the two
decompositions and ordering the complete result:
D(X + Y) = canonically order (D(X) + D(Y)). - Lemma 6:
Strings with the same decomposition have the same selected
normalized form: if
D(X) = D(Y), thenN(X) = N(Y). - Lemma 7: A substring of normalized text is normalized.
- Lemma 8:
Normalizing a concatenation directly equals normalizing its parts, then
the whole:
N(X + Y) = N(N(X) + N(Y)). - Lemma 9:
The unchanged-prefix lemma: if
QandVare normalized andN(Q + V) = Q + S, thenS = V.
Lemma 0: Normalization and decomposition leave the empty string unchanged
Let ε denote the empty string. We will prove that:
N(ε) = ε
D(ε) = ε
UAX #15 defines its substring notation as follows:
“X[i, j] The substring of X that includes all code units after offset i and before offset j; for example, if X is “abc”, then X[1,2] is “b””
It also defines the available offsets:
“An offset into a Unicode string is a number from 0 to n, where n is the length of the string and indicates a position that is logically between Unicode code units (or at the very front or end in the case of 0 or n, respectively).”
Taking i = j = 0 selects no code units, so X[0,0] is the empty string.
The empty string is therefore a substring of every string. UAX #15 then
states:
“all of the Normalization Forms are closed under substringing.”
(from UAX #15 for Unicode 16.0, Concatenation of Normalized Strings)
Therefore the empty substring of a string normalized in any of the four forms is itself normalized in that form. UAX #15 defines what that means:
“isNFx(s) is true if and only if toNFX(s) is identical to s.”
Apply that statement with s = ε. For each normalization form NFx, it gives
toNFx(ε) = ε. Both N and D select one of those four forms, so the two
equations above follow.
This also agrees directly with the normalization algorithms: the empty string contains no code points to decompose, no adjacent pair to reorder, and no characters to compose.
Lemma 1: Extended grapheme cluster rules apply directly to any string
Both algorithms in this document segment text into extended grapheme clusters
by applying UAX #29's rules to the string as it is, without normalizing it
first. The comparison algorithm segments O. The FormC/FormD optimization
segments O and Z. This lemma establishes that applying the rules directly
is allowed and gives the segmentation Unicode defines.
UAX #29 identifies the relevant specification explicitly:
“The following is a general specification for grapheme cluster boundaries—language-specific rules in [CLDR] should be used where available.”
(from UAX #29 for Unicode 16.0, Default Grapheme Cluster Boundary Specification)
For that extended-grapheme-cluster-boundary specification, Section 2 explains how the rules can be applied directly:
“To maintain canonical equivalence, all of the following specifications are defined on text normalized in form NFD, as defined in Unicode Standard Annex #15, “Unicode Normalization Forms” [UAX15]. Boundaries never occur within a combining character sequence or conjoining sequence, so the boundaries within non-NFD text can be derived from corresponding boundaries in the NFD form of that text. For convenience, the default rules have been written so that they can be applied directly to non-NFD text and yield equivalent results.”
In plain English, the specification uses FormD to define where the extended- grapheme-cluster boundaries must be. However, an implementation can apply the extended-grapheme-cluster rules directly to a string without first converting it to FormD and obtain the equivalent segmentation.
That covers any string, whatever its normalization form. So it covers O,
which might not be in any normalization form at all, and Z, whichever form
N produced. Both can be segmented directly into extended grapheme clusters.
Lemma 2: Every comparison cut is at a code-point boundary
Here, a cut is where the algorithm's comparison divides a string: at the end of a
candidate C in the original text, or at the end of its complete normalization
result N(C) in the normalized text. This lemma shows that neither cut can fall
between the two UTF-16 code units of a supplementary code point. The proof
needs this guarantee before treating a span of normalized text as a Unicode
substring and applying
Lemma 7.
We must prove that two kinds of comparison cuts are code-point-aligned:
- On the original side, candidates contain whole extended grapheme clusters. We must show that the boundaries of those clusters can't split a code point.
- On the normalized side, a cut occurs after a complete normalization result
N(C). We must show that this result ends after a complete code point.
We couldn't find an official Unicode statement that says either conclusion outright. Therefore, rather than assume code-point alignment, we must prove it. The first subsection below explains why the input has no unpaired surrogates, which both arguments depend on. The two after it then prove the two kinds of cuts from the relevant Unicode definitions and rules.
Assumption: The input has no unpaired surrogates
The main proof assumes that normalization succeeds. In this project, that also
means O is well-formed UTF-16. A .NET string can hold an unpaired surrogate
half, and such a string isn't a coded character sequence in Unicode's sense
(definition D12, quoted below), so none of the
Unicode definitions this document cites apply to it. Whether
String.Normalize rejects such input depends on the runtime: .NET throws,
and Unity's Mono returns the text unchanged. The parser therefore doesn't rely
on the runtime. Both normalizer modes scan for an unpaired surrogate before
normalizing (UnicodeNormalization.FindFirstUnnormalizableIndex) and report
an error if they find one. So O, and every candidate cut from it, is a
sequence of whole code points. The
normalized-side subsection shows the same for Z and
every other normalization result.
Original-side cuts
The parser finds cluster boundaries in O by applying UAX #29's rules to O
as it is, which
Lemma 1
shows gives the segmentation Unicode defines. The point to establish is that
those rules can manipulate breaks only before, between, or after complete code
points. An arbitrary UTF-16 code-unit offset (such as the offset between the
two code units of a surrogate pair) isn't a position these rules can make into
a boundary. Once that is proved, the definition of an extended grapheme cluster
shows that the boundaries of each candidate span C in the original text
can't split a code point.
Why the rules can manipulate only code-point boundaries. UAX #29 applies its rules in sequence to decide whether a boundary exists at an offset:
“The rules are numbered for reference and are applied in sequence to determine whether there is a boundary at any given offset.”
Each rule places a boundary symbol between a left and right expression:
“Each rule consists of a left side, a boundary symbol (see Table 1), and a right side.”
Each rule has exactly one boundary symbol. UAX #29 states that as the first of its rule constraints:
“Single boundaries. Each rule has exactly one boundary position.”
Table 1 lists three boundary symbols. The third one doesn't appear in any of the grapheme cluster rules, which use only the first two:
÷“Boundary (allow break here)”
דNo boundary (do not allow break here)”
→“Treat whatever on the left side as if it were what is on the right side”
Before applying the rules, each code point is classified with a
boundary-property value such as CR, LF, Extend, or SpacingMark. The
left and right expressions match the resulting sequence of code-point
classifications:
“The left and right sides use the boundary property values in regular expressions.”
For example, CR × LF means that, when a complete code point classified as
CR is followed by a complete code point classified as LF, no boundary is
allowed in the gap between them.
sot and eot mean “start of text” and “end of text,” respectively
(UAX #29 for Unicode 16.0, Notation).
The operand Any represents any code point:
“This is not a property value; it is used in the rules to represent any code point.”
(from UAX #29 for Unicode 16.0, Grapheme_Cluster_Break Property Values,
Any)
The following rules show how this notation can manipulate breaks only at the text edges or between complete code points:
GB1 sot ÷ Any
GB2 Any ÷ eot
GB3 CR × LF
GB999 Any ÷ Any
(UAX #29 for Unicode 16.0, Grapheme Cluster Boundary Rules)
GB1manipulates the position before the first code point: it permits a break at the start of the text.GB2manipulates the position after the last code point: it permits a break at the end of the text.GB3manipulates the gap between the adjacentCRandLFcode points: it suppresses a break there.GB999manipulates the gap between any two adjacent code points: it permits a break there if no earlier rule has decided that position.
GB999 supplies a default decision for every internal gap because each Any
matches a complete code point. An earlier rule can override that decision, as
GB3 does for a CR followed by LF, but it uses the same notation to change
the decision at the gap. It can't move the gap inside either code point. The
other rules likewise place their boundary symbol between expressions that match
complete code points, so they can change the break decision at a gap but can't
create a break inside a code point.
The rules establish where extended grapheme-cluster boundaries can occur. Definition D61 then specifies what lies between those boundaries:
“Extended grapheme cluster: The text between extended grapheme cluster boundaries as specified by Unicode Standard Annex #29, ‘Unicode Text Segmentation.’”
(from The Unicode Standard 16.0, Section 3.6.2, definition D61)
An extended grapheme cluster starts and ends at two of those boundaries. Since
the rules can place them only at the text edges or between complete code
points, the cluster can't start or end inside a code point. Each candidate
C consists of whole clusters, so its start and end are code-point boundaries
too.
Normalized-side cuts
The question here is about output: can the complete normalization result
N(C) end inside a code point? Defining the input as a coded character
sequence isn't enough to answer that. We must check what the normalization
steps produce.
The Unicode Standard defines a coded character sequence as:
“D12 Coded character sequence: An ordered sequence of one or more code points.”
(from The Unicode Standard 16.0, Section 3.4, definition D12)
This matters because the operation definitions below use the word character. The normalization section explicitly explains that usage:
“The specification of Unicode Normalization Forms applies to all Unicode coded character sequences (D12). For clarity of exposition in the definitions and rules specified here, the terms “character” and “character sequence” are used, but coded character sequences refer also to sequences containing noncharacters or reserved code points. Unicode Normalization Forms are specified for all Unicode code points, and not just for ordinary, assigned graphic characters.”
(from The Unicode Standard 16.0, Section 3.11.3, Specification of Unicode Normalization Forms)
Thus, character in the following normalization steps refers to a complete code-point element of the sequence, not to a UTF-16 code unit or part of a code point.
The normalization steps operate on those complete code points and produce complete code points. First, a decomposition mapping replaces a character with a sequence of one or more characters:
“D62 Decomposition mapping: A mapping from a character to a sequence of one or more characters that is a canonical or compatibility equivalent, and that is listed in the character names list or described in Section 3.12, Conjoining Jamo Behavior.”
(from The Unicode Standard 16.0, Section 3.7, definition D62)
Canonical ordering exchanges the positions of whole characters in a decomposed sequence:
“D109 Canonical Ordering Algorithm: In a decomposed character sequence D, exchange the positions of the characters in each Reorderable Pair until the sequence contains no more Reorderable Pairs.”
(from The Unicode Standard 16.0, Section 3.11.5, definition D109)
A Starter L is defined as a code point:
“D107 Starter: Any code point (assigned or not) with combining class of zero (ccc = 0).”
(from The Unicode Standard 16.0, Section 3.11.4, definition D107)
It defines a Primary Composite P as a character:
“D114 Primary composite: A Canonical Decomposable Character (D69) which is not a Full Composition Exclusion.”
(from The Unicode Standard 16.0, Section 3.11.6, definition D114)
In the Unicode rule below, C names a single character being composed. It
isn't the candidate span C used elsewhere in this document.
Rule R1 identifies C as a character in the coded character sequence:
“R1 Seek back (left) in the coded character sequence from the character C to find the last Starter L preceding C in the character sequence.”
(from The Unicode Standard 16.0, Section 3.11.6, definition D117, rule R1)
With L, C, and P thus identified as complete code-point elements, R2
specifies the replacement and deletion:
“R2 If there is such an L, and C is not blocked from L, and there exists a Primary Composite P which is canonically equivalent to the sequence
<L, C>, then replace L by P in the sequence and delete C from the sequence.”(from The Unicode Standard 16.0, Section 3.11.6, definition D117, rule R2)
R2 therefore replaces complete L with complete P and removes complete
C. It doesn't modify only part of a code point.
Definitions D118-D121 specify which of those steps each normalization form uses:
“D118 Normalization Form D (NFD): The Canonical Decomposition of a coded character sequence.”
“D119 Normalization Form KD (NFKD): The Compatibility Decomposition of a coded character sequence.”
“D120 Normalization Form C (NFC): The Canonical Composition of the Canonical Decomposition of a coded character sequence.”
“D121 Normalization Form KC (NFKC): The Canonical Composition of the Compatibility Decomposition of a coded character sequence.”
Thus, a complete normalization result N(C) is a sequence of whole code
points, regardless of which of the four forms N selects. Its endpoint can't
split a code point. Each comparison begins at an already established
code-point boundary, and both possible new endpoints (the end of C and the
end of N(C)) are also code-point boundaries.
The comparison matches N(C) immediately after the already accepted prefix
N(P), so the text in Z before the proposed cut is exactly N(P) + N(C).
Both are complete normalization results and therefore sequences of complete
code points. Their concatenation also ends at a code-point boundary. Because
it's an exact prefix of Z, the proposed cut in Z is at that same boundary.
Lemma 3: Normalization preserves the decomposition
Recall from the beginning of this section that D(X) is the complete, canonically ordered decomposition of X:
FormD when N is FormC or FormD, and FormKD when N is FormKC or FormKD.
We will prove that, for every string X:
D(N(X)) = D(X) (A1)
If X = ε, Lemma 0 makes both sides ε, so equation (A1) holds. The
remaining argument covers nonempty X.
UAX #15's Design Goals section states:
“Another consequence of the definitions is that any chain of normalizations is equivalent to a single normalization, which is:”
- “a compatibility normalization, if any normalization is a compatibility normalization”
- “a composition normalization, if the final normalization is a composition normalization”
The same section then says “For example, the following table lists equivalent chains of two transformations:” and presents the table below. The highlighted entries give the identities needed to prove equation (A1). Lemma 6 uses two more entries from the same table.
UAX #15 two-step normalization table
toNFC(x) means apply FormC to x, toNFD(x) means apply FormD,
toNFKC(x) means apply FormKC, and toNFKD(x) means apply FormKD. In
toNFD(toNFC(x)), the inner FormC step happens first, then FormD. Each
entry below a column heading gives the same result as that heading. The table
is transcribed from UAX #15. The highlighting is ours.
“
toNFC(x)toNFD(x)toNFKC(x)toNFKD(x)=toNFC(toNFC(x))=toNFC(toNFD(x))=toNFD(toNFC(x))=toNFD(toNFD(x))=toNFC(toNFKC(x))=toNFC(toNFKD(x))=toNFKC(toNFC(x))=toNFKC(toNFD(x))=toNFKC(toNFKC(x))=toNFKC(toNFKD(x))=toNFD(toNFKC(x))=toNFD(toNFKD(x))=toNFKD(toNFC(x))=toNFKD(toNFD(x))=toNFKD(toNFKC(x))=toNFKD(toNFKD(x))”
The highlighted cells cover all four choices for N: the toNFD(x) column
covers FormC and FormD, and the toNFKD(x) column covers FormKC and FormKD.
In every case, decomposing after N gives the same result as decomposing X
directly. This proves the claim we set out to establish in equation (A1):
D(N(X)) = D(X) (A1)
Lemma 4: Canonical ordering is stable
Here, stable has its standard sorting meaning: when two code points have the same sorting key, ordering doesn't reverse their relative order. This use of stable is unrelated to Unicode's guarantees about normalization remaining stable across Unicode versions.
For canonical ordering, that sorting key is called the canonical combining class
(ccc). Each code point has one. Definitions D108 and D109 state both the
condition for reordering and the algorithm that uses it:
“D108 Reorderable pair: Two adjacent characters A and B in a coded character sequence
<A, B>are a Reorderable Pair if and only ifccc(A) > ccc(B) > 0.”“D109 Canonical Ordering Algorithm: In a decomposed character sequence D, exchange the positions of the characters in each Reorderable Pair until the sequence contains no more Reorderable Pairs.”
(from The Unicode Standard 16.0, Section 3.11.5, Canonical Ordering Algorithm, D108-D109)
D108 never permits two code points with the same ccc to exchange places.
Since D109 swaps only adjacent code points, equal-ccc code points retain
their original order. That's the stability property.
Sorting is a separate point. A code point with ccc = 0 is called a starter.
D108 can't exchange a starter with a neighbor, so starters stay in place. D109
stops only when no Reorderable Pair remains. At that point, every maximal
stretch of nonstarters (between starters or at either end of the string) is in
nondecreasing ccc order. That sorted order, together with the preserved order
of equal-ccc code points, uniquely determines the result within each stretch.
Canonical ordering is therefore a stable sort within each such stretch.
Lemma 5: Decomposing a concatenation orders the two decompositions together
For all strings X and Y:
D(X + Y) = canonically order (D(X) + D(Y)) (A2)
If either string is empty, equation (A2) follows immediately from Lemma 0.
For example, if X = ε, both sides reduce to D(Y): D(Y) is already
canonically ordered, so ordering ε + D(Y) leaves it unchanged. The case
Y = ε is symmetric. The remaining argument covers two nonempty strings.
Unicode definition D64 states:
“A full decomposition of a character sequence results from decomposing each of the characters in the sequence until no characters can be further decomposed.”
(from The Unicode Standard 16.0, Section 3.7, definition D64)
The point of D64 here is that raw decomposition expands each character (and any
decomposable characters it produces) without changing the order of the original
sequence. Thus the raw decomposition of X + Y is the raw decomposition of X
followed by the raw decomposition of Y.
The core specification states that ordering follows full decomposition:
“Logically, to get the NFD or NFKD (maximally decomposed) normalization form for a Unicode string, one first computes the full decomposition of that string and then applies the Canonical Ordering Algorithm to it.”
(from The Unicode Standard 16.0, Section 3.11.7, Definition of Normalization Forms)
UAX #15 describes the same two stages:
“Once a string has been fully decomposed, any sequences of combining marks that it contains are put into a well-defined order.”
(from UAX #15 for Unicode 16.0, Description of the Normalization Process)
Applying D to X and Y separately performs both stages on each piece:
recursive decomposition followed by canonical ordering.
Consequently, D(X) and D(Y) have each already been canonically ordered, but
their concatenation may still need additional ordering across the point where
the pieces meet. Canonically ordering D(X) + D(Y) performs that remaining
work. By Lemma 4, the ordering is
stable, so this produces the same result as canonically ordering the complete
raw decomposition of X + Y at once: each maximal stretch of nonstarters ends
up in nondecreasing ccc order, and within each class the entries from X
keep their original order and come before the entries from Y.
Lemma 9's proof below walks through the
same merge in detail. This proves equation (A2).
Lemma 6: Equal decompositions give equal normalized forms
We will prove that D(X) = D(Y) implies N(X) = N(Y).
If either string is empty, the premise forces both to be empty. Definition D62,
quoted in Lemma 2,
says that decomposing each character produces one or more characters, and
canonical ordering doesn't delete any. A nonempty string therefore can't have
an empty decomposition. Lemma 0 then gives N(X) = N(Y) = ε. The remaining
argument covers two nonempty strings.
There are two cases.
When N is FormD or FormKD, N is D itself (definitions D118 and D119,
quoted in
Lemma 2), so the
premise D(X) = D(Y) already says N(X) = N(Y).
When N is FormC or FormKC, the
two-step normalization table in
Lemma 3 includes these
two entries:
toNFC(x) = toNFC(toNFD(x))
toNFKC(x) = toNFKC(toNFKD(x))
These entries give the required result directly:
- If
Nis FormC, thenDis FormD. The first entry says that normalizingXto FormC gives the same result as normalizingD(X)to FormC. The same is true forY. The premise says thatD(X)andD(Y)are the same string. Normalizing that string to FormC must therefore give the same result in both cases, soFormC(X) = FormC(Y). - If
Nis FormKC, thenDis FormKD. The second entry gives the same argument. The premise says thatD(X)andD(Y)are the same string. Normalizing that string to FormKC must therefore give the same result in both cases, soFormKC(X) = FormKC(Y).
Definitions D120 and D121 (also quoted in Lemma 2) say the same thing directly. FormC is “the Canonical Composition of the Canonical Decomposition of a coded character sequence” and FormKC is “the Canonical Composition of the Compatibility Decomposition of a coded character sequence”, and the Canonical Composition Algorithm (definition D117, quoted in Lemma 2) is a deterministic procedure on the decomposed sequence. Equal decompositions feed equal input to the same procedure.
In either case, the selected N gives N(X) = N(Y), as required.
Lemma 7: A substring of normalized text is normalized
UAX #15 states:
"all of the Normalization Forms are closed under substringing."
(from UAX #15 for Unicode 16.0, Concatenation of Normalized Strings)
Therefore any substring cut from normalized text at code-point boundaries is normalized on its own. Lemma 2 establishes that the cuts used here satisfy that condition.
Lemma 8: Normalizing a concatenation directly equals normalizing its parts, then the whole
The rule to be proved is that, for any strings X and Y:
N(X + Y) = N(N(X) + N(Y)) (A5)
In words, normalizing two concatenated strings produces the same result as normalizing each string first, concatenating those results, and then normalizing the complete concatenation.
If either string is empty, equation (A5) follows from Lemma 0 and
normalization's idempotence.
For example, if X = ε, its left side is N(Y), while its right side reduces
to N(N(Y)) = N(Y). The case Y = ε is symmetric. The remaining argument
covers two nonempty strings.
Lemma 5, equation (A2), is the following general rule:
D(left + right)
= canonically order (D(left) + D(right)) (A2)
Substitute N(X) for left and N(Y) for right:
D(N(X) + N(Y))
= canonically order (D(N(X)) + D(N(Y))) (A3)
Lemma 3, equation (A1),
says that D(N(X)) = D(X) and D(N(Y)) = D(Y). Substitute those equal
strings into equation (A3):
D(N(X) + N(Y))
= canonically order (D(X) + D(Y))
Lemma 5, equation (A2), also says:
D(X + Y) = canonically order (D(X) + D(Y))
The two decompositions equal the same ordered expression, so they equal each other:
D(N(X) + N(Y)) = D(X + Y) (A4)
Equation (A4) states that the two strings N(X) + N(Y) and X + Y have the
same decomposition.
Lemma 6 says that
two strings with the same decomposition have the same normalized form. Applying
that lemma directly to these two strings gives:
N(N(X) + N(Y)) = N(X + Y)
Equality is symmetric, so the two sides can be reversed:
N(X + Y) = N(N(X) + N(Y)) (A5)
This is the rule stated at the beginning of the section. Substituting C for
X and R for Y gives the rule used in the main proof.
Equation (A5) doesn't say that N(X) + N(Y) is already normalized. The outer
call to N handles any ordering or composition required across the join. UAX
#15 explicitly warns:
“none of the Normalization Forms are closed under string concatenation.”
(from UAX #15 for Unicode 16.0, Concatenation of Normalized Strings)
Lemma 9: The unchanged-prefix lemma
We must prove:
If
QandVare normalized andN(Q + V) = Q + S,then
S = V.
The proof has five steps.
Proof step 1: S is normalized
N(Q + V) is normalized. The premise says that it's equal to Q + S.
Because Q is normalized (assumption of the lemma), it's a sequence of
complete code points. It's an exact prefix of N(Q + V), so the cut after
Q is a code-point boundary. Thus S is a code-point-aligned suffix (and
therefore a substring) of normalized text.
Lemma 7 states that
such a substring is normalized.
Therefore S is normalized too.
Proof step 2: Derive equality of the two ordered decompositions
The lemma assumes:
N(Q + V) = Q + S
Fully decompose both equal strings. In other words, apply the same function
D to both sides of that equality:
D(N(Q + V)) = D(Q + S)
Lemma 3, equation (A1),
states that D(N(X)) = D(X). With X = Q + V, it allows us to replace the
left side D(N(Q + V)) with D(Q + V):
D(Q + V) = D(Q + S) (A6)
Lemma 5, equation (A2), states:
D(left + right)
= canonically order (D(left) + D(right)) (A2)
Apply it first with left = Q and right = V:
D(Q + V) = canonically order (D(Q) + D(V))
Apply it again with left = Q and right = S:
D(Q + S) = canonically order (D(Q) + D(S))
Substitute both expansions into equation (A6):
canonically order (D(Q) + D(V))
= canonically order (D(Q) + D(S)) (A7)
Proof step 3: Define and justify the recovery procedure
Equation (A7) says that canonically ordering D(Q) + D(V) and
D(Q) + D(S) produces the same string. We can't cancel D(Q) directly
because canonical ordering has been applied to each entire concatenation and
may move some of its code points. To prove D(V) = D(S), we must first
determine exactly how ordering can mix the two concatenated inputs.
Recall what Lemma 4 outlined in terms of sorting:
Canonical ordering swaps an adjacent pair only when the left code point has a
greater positive ccc than the right one. A starter has ccc = 0, so it can't
participate in a swap, and no code point can cross it. Code points with equal
ccc values also never swap. Consequently, starters remain fixed, while each
stretch of nonstarters is sorted by ccc without changing the relative order
of equal-ccc code points.
Therefore only the trailing nonstarters of D(Q) and the leading
nonstarters of the following decomposition (D(V) on the left side of
equation (A7), or D(S) on the right) can intermix.
For brevity, we'll use the term following string to refer to D(V) on the left side of
equation (A7) or D(S) on the right. Given the known D(Q) and the result of
canonically ordering D(Q) + following string, the recovery procedure below recovers the
following string.
Here's how: To separate their contributions, first split the known string D(Q) into two
parts:
D(Q) = unchanged prefix + trailing nonstarters
The trailing nonstarters are the complete stretch of nonstarters at the end
of D(Q). They're empty if D(Q) ends with a starter. The unchanged prefix
is everything in D(Q) before that stretch. Split the following string in
the opposite way:
following string = leading nonstarters + unchanged rest
The leading nonstarters are the complete stretch of nonstarters at the
beginning of the following string. The unchanged rest is empty or begins
with its first starter. D(Q), D(V), and D(S) are already ordered
internally, so ordering their concatenation needs to merge only the two
nonstarter stretches where the strings meet:
canonically order (D(Q) + following string)
= unchanged prefix
+ stable merge (trailing nonstarters, leading nonstarters)
+ unchanged rest
Here's a schematic example. The numbers are ccc values, q labels code
points from D(Q), and r labels code points from the following string:
before stable merge: 220:q1 230:q2 230:q3 220:r1 220:r2 232:r3
after stable merge: 220:q1 220:r1 220:r2 230:q2 230:q3 232:r3
Stable ordering keeps a code-point occurrence from D(Q) before one from the
following string when their ccc values are the same. After the stable merge,
if you remove q1 from the start of the merged 220 group and q2,q3 from the
start of the merged 230 group, what remains is exactly
220:r1 220:r2 232:r3, the leading nonstarters of the following string.
This shows how the merge may interleave the two nonstarter stretches, but it
preserves the order within each stretch.
This matters because the recovery procedure must recover the actual code-point
sequence of the following string from the ordered result, not merely its
ccc values. The actual
output doesn't record origins, the q and r labels are only for the schematic. Even if some code points are
identical, D(Q) tells us how
many q occurrences are at the front of each merged ccc group. Remove that
many from the front of the group. What remains within the merged stretch is
exactly the occurrences from the following string, so identical code-point
values don't make the recovery ambiguous.
We know D(Q), so we can use the process above to remove its trailing nonstarters
from the merged stretch. After removing D(Q)'s contribution, what remains is
the leading nonstarters of the following string followed by its unchanged
rest, which together make up the complete following string.
Thus, the recovery procedure can recover the exact following string from the result of canonically order (D(Q) + following string) since we know that result and the original value of D(Q). Now, let's use this recovery procedure in the proof.
Proof step 4: Apply the recovery procedure to equation (A7)
Equation (A7) states:
canonically order (D(Q) + D(V))
= canonically order (D(Q) + D(S)) (A7)
Let T name the single ordered string on both sides of equation (A7):
T = canonically order (D(Q) + D(V))
T = canonically order (D(Q) + D(S))
The recovery procedure has exactly the same two inputs in both cases: the
ordered sequence T and the known contribution D(Q). It can therefore
recover the following string by removing the contribution of D(Q) like
this:
- Remove the unchanged prefix of
D(Q)from the start ofT. Proof step 3 showed that onlyD(Q)'s trailing nonstarters can intermix with the following string, so this prefix appears unchanged at the start ofT. - Within each merged
cccgroup, remove from the front the known number of code-point occurrences contributed by the trailing nonstarters ofD(Q). Stable ordering preserves those occurrences and their relative order and places them before occurrences from the following string with the sameccc. Only their positions in the complete merged stretch may have changed. - Everything not removed is the recovered following string.
In both cases, the procedure is applied to the same string T using the same
known D(Q). It therefore makes the same removals and leaves the same
remainder. The preceding argument proved that this remainder is D(V) in the
first equality and D(S) in the second. Therefore:
D(V) = D(S) (A8)
This argument doesn't assume that no nonstarter crossed the join during normalization. It proves only what is needed: identical ordered results made from the same known left input must have identical right inputs.
Proof step 5: Equal decompositions give equal normalized strings
This lemma assumes N(Q + V) = Q + S. Thus the input suffix after Q is V,
while the output suffix after Q is S:
before normalization: Q | V
after normalization: Q | S because N(Q + V) = Q + S
Proof step 4 established that these two suffixes have the same decomposition:
D(V) = D(S) (A8)
Equation (A8) says that applying D to V and S produces the same
decomposed string.
Lemma 6 states
that strings with the same decomposition have the same normalized form.
Therefore:
N(V) = N(S) (A9)
At the beginning of this lemma, we assumed that V is normalized. Proof step
1 established that S is also normalized. Saying that a string is
normalized means that
it's already in the normalization form N, so applying N again returns
exactly the same string. UAX #15 calls this property idempotence and states:
“All of the transformations are idempotent: that is,”
toNFC(toNFC(x)) = toNFC(x)toNFD(toNFD(x)) = toNFD(x)toNFKC(toNFKC(x)) = toNFKC(x)toNFKD(toNFKD(x)) = toNFKD(x)
The assumption that V is normalized therefore means:
N(V) = V
Proof step 1 established that S is also normalized, so:
N(S) = S
Substitute both equalities into equation (A9):
V = N(V) = N(S) = S
Therefore
S = V. That's exactly the conclusion required by the lemma:
Q and V are normalized
N(Q + V) = Q + S
therefore S = V
In plain language, if normalizing two already-normalized strings leaves the
first string Q unchanged as the complete prefix, then the text after Q
must be the original second string V. Normalization can't leave Q
unchanged while silently replacing V with some different suffix S.
The lemmas used in the proof apply to FormC, FormD, FormKC, and FormKD, so Lemma 9 holds for all four forms.
Aside: movement across the join can be invisible
A tempting proof of Lemma 9 would go like this:
QandVare each already normalized.- Assume that, because
Qremains unchanged at the start of the output, normalization didn't move or combine any code points across theQ | Vjoin. - Under that assumption, normalization acts on
QandVindependently. Because each is already normalized, the result must beQ + V. - The lemma's premise also says that the result is
Q + S. ThereforeQ + V = Q + S, and removing the common prefixQgivesV = S.
The problem is step 2: an unchanged output prefix doesn't prove that nothing crossed the join during normalization. The following example shows why.
Let N be FormC, let Q be Ậ (U+1EAC, LATIN CAPITAL LETTER A WITH
CIRCUMFLEX AND DOT BELOW), and let V be U+0323, COMBINING DOT BELOW. Both Q
and V are already normalized. Before normalization, their join is:
U+1EAC[Q] | U+0323[V]
(The [Q] and [V] labels record where each code-point occurrence came from,
they aren't part of the string.) FormC first canonically decomposes the entire
input and orders the resulting sequence before composing it again. To track
what happens across the Q | V join, begin with the decomposition step.
Decomposing U+1EAC produces A, a dot below, and a circumflex. U+0323 has no
decomposition mapping, so decomposing V leaves it as U+0323. The number
before each code point is its ccc value:
before ordering: 0:U+0041[Q] 220:U+0323[Q] 230:U+0302[Q] 220:U+0323[V]
after ordering: 0:U+0041[Q] 220:U+0323[Q] 220:U+0323[V] 230:U+0302[Q]
The second line shows that canonical ordering places class 220 before class
230, so the dot from V moves left across the circumflex from Q.
Canonical composition first combines U+0041 (A) with the U+0323 COMBINING
DOT BELOW that came from Q, producing U+1EA0 (Ạ). The U+0323 from V
can't combine with U+1EA0, so it remains between U+1EA0 and U+0302.
Canonical composition can still combine characters on opposite sides of it.
Unicode explicitly notes:
“a Non-blocked Pair need not be contiguous.”
(from The Unicode Standard 16.0, Section 3.11.6, definition D116)
Under definition D115's blocking rule,
the intervening U+0323 would block the following U+0302 only if its ccc were
zero or at least U+0302's ccc. Its ccc is 220, which is less than U+0302's
230, so it doesn't block the composition. U+1EA0 therefore combines with
U+0302 COMBINING CIRCUMFLEX to produce U+1EAC (Ậ). The unabsorbed U+0323
from V remains after U+1EAC:
U+1EAC U+0323
That's exactly Q + V. The final prefix is still the exact Q, even though
the dot originating in V crossed a decomposed code point originating in Q.
The decomposition and combining classes are recorded in the
Unicode 16.0 Character Database.
The example doesn't disprove S = V. In fact, it satisfies S = V. To use
the shortcut, we'd have to prove that anything crossing the join always
“comes back” without changing the suffix, but that's essentially what Lemma 9
is trying to prove. Assuming it would be circular.