From 1985112967837bce2e00a86673c22560a6fb01ac Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Fri, 18 Sep 2026 09:12:34 -0500 Subject: [PATCH] Measure section 10.1.6's Hangul problem instead of postponing it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The four tailored methods for the trailing weights problem were on the "deliberately later" list. They are not implemented, and the reason turns out not to be "later": CLDR does not implement them either, and doing so would mean disagreeing with ICU. Syllables are decomposed to jamo as S1.1 requires, so the problem is reachable. All four methods turn on giving the trailing consonants a weight above every other script, so that `L1V1T1` outranks `L1V1Xa` for any following character. CLDR's root does not: its leading consonants run 0x4771..0x47ED, vowels 0x47EF..0x4835, trailing consonants 0x484D..0x48A4, and then Hiragana begins at 0x48D6 with Han derived above 0xFB40. So this library orders that section's own shapes L1L2V1 < 가 < 가a < 가가 < L1V1V2 < 각 < 각a < 각一 < 가一 with `가一` last: a syllable followed by a Han ideograph outranks the same syllable with a trailing consonant, where Korean dictionary order compares syllable by syllable and would have it the other way round. `L1L2V1` first is the same problem from the other end -- the data method would make `L1L2` a contraction, and without one the second leading consonant is compared against a vowel and wins. ICU gives the same order, and now says so on every run. Those shapes are in the oracle's root corpus: the precomposed forms beside the decomposed ones, each syllable alone and followed by a character from a script below Hangul and one above. The root corpus went from 496 comparisons to 1,035 and agrees in all nine modes. Nothing in this project compared a jamo sequence against what followed it before, because every corpus entry is one code point or one of a tailoring's own sequences -- which is precisely the shape the trailing weights problem cannot take. That gap was hidden by a surprise about Korean. `ko.xml`'s `standard` collation tailors no Hangul at all: it is 2,212 `&<<*` chains ordering *Hanja* by Korean reading, which is why the corpus is ideographs and why its 28,452,196 comparisons say nothing about jamo. The conjoining jamo in that file are all in its `search` and `searchjl` collations, which are for matching rather than sorting and are not compiled. So how Hangul sorts here is the root table's answer and no tailoring's, and the root corpus is where the shapes belong. There is also a test pinning that order, since DESIGN.md now states it, and one asserting the precomposed syllables weigh exactly what their decompositions do -- which is what makes the anomaly a property of the weights rather than of how the text was written. Cited as section 10.1.5 throughout before this; 10.1.5 is Reserved Weights and Hangul Collation is 10.1.6. Verified: the ICU oracle at 0 differences in all nine modes, both conformance suites unchanged, 104,000 fuzz inputs with no findings. Co-Authored-By: Claude Opus 5 (1M context) --- DESIGN.md | 67 +++++++++++++++++++++++++++++++++++++++++-- README.md | 12 ++++++-- src/collator.zig | 56 ++++++++++++++++++++++++++++++++++++ tools/oracle_dump.zig | 57 +++++++++++++++++++++++++++++++----- 4 files changed, 180 insertions(+), 12 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index b47a32f..740fedf 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -613,6 +613,64 @@ shows at the default strength, where the fourth level is not written at all — it is what a key costs to a caller who asks for `quaternary` or `identical`, which is what the conformance runner asks for on all 872,037 of its cases. +### Hangul and the trailing weights problem + +§10.1.6 describes four tailored methods — simple, data, terminator, +interleaving — for the problem §10.1.4 raises: a Hangul syllable written as +conjoining jamo has a different length depending on whether it has a trailing +consonant, so `L V` and `L V T` are two and three weights, and what follows +the syllable lands where the `T` would have been. None of the four is +implemented here. What follows is what was measured instead, because the +honest reason is not "later" but "CLDR does not do it either". + +**Syllables are decomposed, as S1.1 requires**, so U+AC00 is weighed as +U+1100 U+1161 and the problem is reachable. The root table's jamo weights are +contiguous and in code point order: + +| | primary weights | +|---|---| +| `a` … `z` | 0x23EC … 0x26EE | +| L, the leading consonants | 0x4771 … 0x47ED | +| V, the vowels | 0x47EF … 0x4835 | +| T, the trailing consonants | 0x484D … 0x48A4 | +| Hiragana あ | 0x48D6 | +| Han | derived, from 0xFB40 | + +§10.1.6's methods all turn on making `T` outrank every other script, so that +`L1V1T1` sorts above `L1V1Xa` for any following character Xa. CLDR's root +does not: Hiragana and Han sit above T. So the anomaly is present, and this is +what this library orders — with the shapes named as §10.1.6 names them, and +`가` for `L1V1` and `각` for `L1V1T1`: + +``` +L1L2V1 < 가 < 가a < 가가 < L1V1V2 < 각 < 각a < 각一 < 가一 +``` + +`가一` last is the problem in one line: a syllable followed by a Han +ideograph outranks the same syllable with a trailing consonant, where Korean +dictionary order compares syllable by syllable and would put `가一` before +`각`. `L1L2V1` first is the same problem from the other end — the data method +would make `L1L2` a contraction, and without one the second leading consonant +is compared against a vowel and wins. + +**ICU gives the same order**, which is what settles it. Those shapes are in +the oracle's root corpus now — the precomposed forms beside the decomposed +ones, each syllable alone and followed by a character from a script below +Hangul and one above — and all 1,035 comparisons agree in every mode. So the +anomaly is CLDR's answer rather than this implementation's, and applying any +of §10.1.6's methods would make this library disagree with ICU and with CLDR +on Korean text. That is a decision for whoever wants a Korean tailoring beyond +CLDR's, not a defect to fix. + +It is worth saying where CLDR's Korean rules *do* go, since the answer was a +surprise: `ko.xml`'s `standard` collation is 2,212 +`&<<*` chains that order **Hanja by their Korean +reading**, and tailors no Hangul at all. The conjoining jamo in that file are +all in its `search` and `searchjl` collations, which are for matching rather +than sorting and are not compiled here. Korean's 28,452,196 oracle +comparisons are therefore about ideographs, and before these shapes were added +nothing in this project compared a jamo sequence against what followed it. + ### The rest of §9.1, and why the data settles most of it The one thing §9.1.4 says that is not done here is picking COMMON by @@ -1755,9 +1813,12 @@ and the sections above say which and how. What is left: in the root table and in every tailoring, a table of leading and trailing combining classes, and a fallback rule. It would want revisiting for a workload of composed kana, where the ceiling is nearer a tenth. -- **§10.1.5 Hangul.** The first version decomposes Hangul syllables to jamo as - S1.1 requires and takes the trailing-weight consequences, which is - conformant. The tailored methods in §10.1.5 belong with tailoring. +- **§10.1.6 Hangul** -- measured and declined rather than pending. "Hangul + and the trailing weights problem" above has the order this library produces + for that section's own shapes, the root table's jamo weights that explain + it, and the reason applying any of the four methods would mean disagreeing + with ICU and with CLDR. It was cited here as §10.1.5 for a while, which is + *Reserved Weights*; Hangul Collation is §10.1.6. - **§9.3.3 repertoire restriction**, `-Dducet-repertoire=`, which would generate a table covering named blocks or scripts and nothing else. Not built; the generator is written so that it could be. diff --git a/README.md b/README.md index 786784b..9c46b28 100644 --- a/README.md +++ b/README.md @@ -204,8 +204,16 @@ canonical closure in the root table and in every tailoring. `DESIGN.md` has the numbers and the reason the closure cannot simply be read out of `allkeys_CLDR.txt`. -Not here, and for later: the tailored Hangul methods of -[section 10.1.5][hangul]. +Not here, and measured rather than merely postponed: the four tailored Hangul +methods of [section 10.1.6][hangul]. Syllables are decomposed to jamo as S1.1 +requires, so the trailing weights problem is reachable — a syllable followed +by a Han ideograph outranks the same syllable with a trailing consonant, where +Korean dictionary order would have it the other way round. **ICU gives the +same order**, because CLDR's root does not put the trailing consonants above +every other script and CLDR's Korean rules tailor no Hangul at all — their +2,212 chains order Hanja by Korean reading. Those shapes are in the oracle's +root corpus now, so the agreement is checked rather than assumed; applying any +of the four methods would mean disagreeing with both ICU and CLDR. `DESIGN.md` has the reasoning, the measurements the representation is built on, and what was decided against. diff --git a/src/collator.zig b/src/collator.zig index e46d224..cd9cad4 100644 --- a/src/collator.zig +++ b/src/collator.zig @@ -959,3 +959,59 @@ test "an uncased element in the middle makes a spelling mixed" { try testing.expectEqual(std.math.Order.gt, try compare(upper_first, lower, title)); try testing.expectEqual(std.math.Order.lt, try compare(lower_first, lower, title)); } + +test "section 10.1.6's Hangul shapes, in the order CLDR's root puts them" { + // The trailing weights problem of section 10.1.4, pinned. Syllables are + // decomposed to jamo as S1.1 requires, so a syllable written as `L V` is + // two weights and `L V T` is three, and whatever follows the syllable + // lands where the `T` would have been. + // + // Section 10.1.6's four tailored methods all turn on giving `T` a weight + // above every other script, so that `L1V1T1` outranks `L1V1Xa` for any + // following character. CLDR's root does not: its trailing consonants stop + // at 0x48A4 and Hiragana begins at 0x48D6, with Han derived above + // 0xFB40. So `가一` outranks `각`, where Korean dictionary order compares + // syllable by syllable and would have it the other way round. + // + // This is CLDR's answer rather than this library's: ICU orders these + // identically, which the oracle checks on every run -- these shapes are + // in its root corpus. The test is here so that a change to any of it + // fails loudly, since `DESIGN.md` states this order. + if (table.cldr_root.stage1.len == 0) return error.SkipZigTest; + const c: Collator = .{ .table = table.cldr_root }; + + const ll_v = [_]u21{ 0x1100, 0x1102, 0x1161 }; // L1 L2 V1, Old Korean + const ga = [_]u21{ 0x1100, 0x1161 }; // 가, as L1 V1 + const ga_a = [_]u21{ 0x1100, 0x1161, 'a' }; // a script below Hangul + const ga_ga = [_]u21{ 0x1100, 0x1161, 0x1100, 0x1161 }; + const l_vv = [_]u21{ 0x1100, 0x1161, 0x1165 }; // L1 V1 V2, Old Korean + const gag = [_]u21{ 0x1100, 0x1161, 0x11A8 }; // 각, as L1 V1 T1 + const gag_a = [_]u21{ 0x1100, 0x1161, 0x11A8, 'a' }; + const gag_han = [_]u21{ 0x1100, 0x1161, 0x11A8, 0x4E00 }; + const ga_han = [_]u21{ 0x1100, 0x1161, 0x4E00 }; // a script above Hangul + + const ascending = [_][]const u21{ + &ll_v, &ga, &ga_a, &ga_ga, &l_vv, &gag, &gag_a, &gag_han, &ga_han, + }; + for (ascending[0 .. ascending.len - 1], ascending[1..]) |lower, higher| { + const a = try c.sortKeyAllocCodePoints(testing.allocator, lower); + defer testing.allocator.free(a); + const b = try c.sortKeyAllocCodePoints(testing.allocator, higher); + defer testing.allocator.free(b); + try testing.expectEqual(std.math.Order.lt, orderKeys(a, b)); + } + + // And the precomposed syllables weigh exactly what their decompositions + // do, which is what makes the anomaly a property of the weights rather + // than of how the text happened to be written. + for ([_]struct { u21, []const u21 }{ + .{ 0xAC00, &ga }, + .{ 0xAC01, &gag }, + }) |pair| { + const composed = try c.sortKeyAllocCodePoints(testing.allocator, &.{pair[0]}); + defer testing.allocator.free(composed); + const decomposed = try c.sortKeyAllocCodePoints(testing.allocator, pair[1]); + defer testing.allocator.free(decomposed); + try testing.expectEqualSlices(u8, decomposed, composed); + } +} diff --git a/tools/oracle_dump.zig b/tools/oracle_dump.zig index 2f976af..a4f72bb 100644 --- a/tools/oracle_dump.zig +++ b/tools/oracle_dump.zig @@ -19,12 +19,13 @@ //! ## What is compared //! //! `und` is the untailored root, and its corpus is a general one -- letters, -//! accents, case, punctuation, digits, scripts that take derived weights. It -//! is the baseline, and it matters: ICU orders by the *CLDR* root, so running -//! this against `collate.ducet` measures the distance between DUCET and CLDR -//! rather than anything about this implementation. Against -//! `collate.cldr_root` it should be close to zero, and whatever is left is a -//! finding. +//! accents, case, punctuation, digits, scripts that take derived weights, and +//! section 10.1.6's Hangul shapes, which are there because how Hangul sorts +//! is the root's answer and no tailoring's. It is the baseline, and it +//! matters: ICU orders by the *CLDR* root, so running this against +//! `collate.ducet` measures the distance between DUCET and CLDR rather than +//! anything about this implementation. Against `collate.cldr_root` it should +//! be close to zero, and whatever is left is a finding. //! //! Every locale the build was asked for is compared as well, and its corpus //! is built out of its own tailoring: the code points it overrides, the @@ -32,6 +33,11 @@ //! precisely the comparisons the tailoring exists to change, so they are the //! ones worth asking about. //! +//! What that cannot reach is a sequence followed by something, since every +//! entry is one code point or one of the tailoring's own sequences. That is +//! the trailing weights problem of section 10.1.4, and it is why the Hangul +//! shapes are written out rather than assembled from the corpus. +//! //! ## The modes //! //! `--mode=tertiary` is the default and the one CI runs: tertiary strength, @@ -388,6 +394,43 @@ fn tailoringCorpus( } } +/// The rows of section 10.1.6's tables, as strings, for the root corpus. +/// +/// They belong to the root collation rather than to any tailoring, which was +/// worth finding out: CLDR's Korean rules tailor no Hangul at all. Its +/// `standard` collation is 2,212 `&<<*` chains that +/// order *Hanja* by their Korean reading, and the conjoining jamo in `ko.xml` +/// are all in its `search` and `searchjl` collations, which are for matching +/// rather than sorting and are not compiled. So how Hangul sorts here is the +/// root table's answer, and the root corpus is where these have to go. +/// +/// U+1100 is the leading consonant KIYEOK and U+1102 NIEUN; U+1161 is the +/// vowel A and U+1165 EO; U+11A8 is the trailing consonant KIYEOK. `a` is a +/// character from a script sorted before Hangul and U+4E00 one sorted after +/// -- those two are what make the rows about what *follows* a syllable rather +/// than about the syllable itself, which is the whole of the trailing weights +/// problem. +/// +/// The precomposed forms are here beside the decomposed ones because they are +/// canonically equivalent to them and so must sort equal: U+AC00 is the +/// syllable GA and U+AC01 GAG. +const hangul_shapes = [_][]const u21{ + &.{ 0x1100, 0x1161 }, // L1 V1 + &.{ 0x1100, 0x1161, 'a' }, // L1 V1 Xb + &.{ 0x1100, 0x1161, 0x4E00 }, // L1 V1 Xa + &.{ 0x1100, 0x1161, 0x11A8 }, // L1 V1 T1 + &.{ 0x1100, 0x1161, 0x11A8, 'a' }, + &.{ 0x1100, 0x1161, 0x11A8, 0x4E00 }, + &.{ 0x1100, 0x1161, 0x1165 }, // L1 V1 V2, which only Old Korean has + &.{ 0x1100, 0x1102, 0x1161 }, // L1 L2 V1, likewise + &.{ 0x1100, 0x1161, 0x1102, 0x1161 }, // two syllables, L1 V1 L2 V2 + &.{0xAC00}, // the same syllable precomposed + &.{0xAC01}, + &.{ 0xAC00, 'a' }, + &.{ 0xAC00, 0x4E00 }, + &.{ 0xAC00, 0xAC00 }, +}; + /// Words whose order depends on a secondary or tertiary setting: the four /// French spellings section 3.8.1 uses to show what a backwards secondary /// level does, and the same letters cased both ways. @@ -465,4 +508,4 @@ const root_corpus = [_][]const u21{ &.{0x3042}, &.{0x30A2}, // Greek and Cyrillic, which the two roots group differently. &.{0x03B1}, &.{0x0410}, -}; +} ++ hangul_shapes; -- knotmirror 0.0.1