Skip to content

Extend MergeEquivalentAnalyses to cover templates and rules - #569

Closed
jtmaxwell3 wants to merge 5 commits into
masterfrom
extend-MergeEquivalentAnalyses
Closed

jtmaxwell3 wants to merge 5 commits into
masterfrom
extend-MergeEquivalentAnalyses

Conversation

@jtmaxwell3

@jtmaxwell3 jtmaxwell3 commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

This extends MergeEquivalentAnalyses to merge the results of individual templates and rules, not just the results of a stratum. If the templates and rules always produce a subset of the input during unapplication, then analysis should be polynomial in the length of the input instead of exponential, although readout for synthesis will still be exponential.


This change is Reviewable

@jtmaxwell3
jtmaxwell3 marked this pull request as draft October 27, 2026 20:03
@codecov-commenter

codecov-commenter commented Oct 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.23077% with 36 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.89%. Comparing base (9f284a3) to head (57cb4e6).

Files with missing lines Patch % Lines
src/SIL.Machine.Morphology.HermitCrab/Word.cs 27.58% 18 Missing and 3 partials ⚠️
src/SIL.Machine.Morphology.HermitCrab/Morpher.cs 35.29% 9 Missing and 2 partials ⚠️
...chine.Morphology.HermitCrab/AnalysisStratumRule.cs 97.95% 1 Missing ⚠️
src/SIL.Machine/Rules/CombinationRuleCascade.cs 75.00% 0 Missing and 1 partial ⚠️
src/SIL.Machine/Rules/ParallelRuleBatch.cs 0.00% 1 Missing ⚠️
src/SIL.Machine/Rules/PermutationRuleCascade.cs 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #569      +/-   ##
==========================================
- Coverage   74.93%   74.89%   -0.05%     
==========================================
  Files         454      454              
  Lines       38490    38554      +64     
  Branches     5322     5332      +10     
==========================================
+ Hits        28843    28875      +32     
- Misses       8495     8524      +29     
- Partials     1152     1155       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

}
}

private IEnumerable<Word> ApplyTemplatesAndRules(Word input)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: F6. No tests cover the new analysis path: the diff touches nothing under tests/. ApplyTemplatesAndRules, PopUnapplied, the ExpandAlternatives rewrite, and the compounding Source fix all change analysis output, and AGENTS.md asks for a focused test with each behavior change. At minimum, check that unordered parses give the same results with merging on and off, including a compound and a repeated affix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F6 still applies at 6d69bd1: the rewrite changes the merge path again (per-rule FilterResults, Source chains kept in merge mode, output taken from wordCache.Values), and nothing under tests/ changed. A focused test should check that parses match with merging on and off, for linear and unordered strata.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F6 still applies at 275ca01: this push changes the merge path again (MaxAlternatives counted in FilterResults, an early syntactic-feature rejection in LexicalLookup, cached ExpandAlternatives), and the PR still changes nothing under tests/. A focused test should check that parses match with merging on and off, for Linear and Unordered strata.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F6 still applies at 836df6f: this push changes the merge path again (duplicates are now dropped inside the cascades when merging is off, and ExpandAlternatives deduplicates by value), and the PR still changes nothing under tests/. A focused test should check that parses match with merging on and off, for Linear and Unordered strata.

@claude

This comment has been minimized.

@claude

This comment has been minimized.

@claude

This comment has been minimized.

@claude

claude Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Verdict: approve with fixes.

Most important: F6 (AnalysisStratumRule.cs): the PR still adds no tests, and every round has changed analysis output. Parses with merging on and off, for Linear and Unordered strata, are not compared anywhere.

Counts: Critical 0, Important 1 open (F6), Low 0 open.

Ran: gh pr checks 569: Build on ubuntu-22.04 and windows-latest pending, claude-review pending, changes pass, advisory Comment hygiene pass (not evidence of the strict scan). Locally on the merge of 836df6f into master, dotnet test tests/SIL.Machine.Morphology.HermitCrab.Tests passed 176 of 176.

Could not verify: I traced, but did not run, the claim that deduplicating inside the cascades with merging off loses no analysis. Word.ValueEquals compares _mruleApps, so a duplicate only comes from an equal input whose exploration already ran. MaxAlternatives behavior against master is also traced, not run.

Public API, target framework, package, machine.py parity: unchanged since 6d69bd1. RuleCascade and RuleBatch in SIL.Machine (netstandard2.0) still gain the public Apply(TData, Func<IEnumerable<TData>, IEnumerable<TData>>) overload, and RuleCascade.Apply(TData) is still non-virtual. No machine.py parity change verified.

Findings: F1 addressed; F2 addressed; F3 addressed; F4 addressed; F5 addressed; F6 open; F7 addressed; F8 addressed; F9 addressed; F10 addressed; F11 addressed.

Reviewed at 836df6f

@johnml1135 johnml1135 mentioned this pull request Nov 12, 2026

@johnml1135 johnml1135 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach holds up on grammars FieldWorks can produce, and the HermitCrab tests pass (176/176). Before it lands:

  1. Tests: merging is on by default and no test compares parse sets with MergeEquivalentAnalyses true vs false (Linear and Unordered strata, templates, compounding).
  2. PopUnapplied has no callers, so its ExpandAlternatives branch never runs (inline).
  3. _expandedAlternatives keeps a set alive on every word in the Source chain (inline).
  4. RuleCascade.Apply(TData) is no longer virtual (inline).
  5. The LexicalLookup syntactic-feature pre-filter (Morpher.cs:450) is a separate change that also appears in #583 and #627. Please describe it here or leave it to #627.

// Special case.
alternatives.Add(this);
}
else if (Source.MorphologicalRuleUnapplicationCount > MorphologicalRuleUnapplicationCount)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch needs PopUnapplied (line 359), which has no callers, so it never runs. If it did, it would drop the word's root, non-heads and realizational changes. Please remove both.

private int _mruleAppCount = 0;
private readonly IList<Word> _alternatives = new List<Word>();
private readonly ConcurrentBag<Word> _alternatives = new ConcurrentBag<Word>();
private HashSet<Word> _expandedAlternatives = null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caches a set on every word in the Source chain, and returned parses keep the chain alive. Can it be computed per call, or shown to stay small?

}

public abstract IEnumerable<TData> Apply(TData input);
public IEnumerable<TData> Apply(TData input)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apply(TData) is no longer virtual, a breaking change for subclasses outside SIL.Machine. Keep it virtual, or note it in the release.

@jtmaxwell3 jtmaxwell3 closed this Nov 12, 2026
@jtmaxwell3

Copy link
Copy Markdown
Collaborator Author

Originally, this pull request sped parsing up. But after the edge prefilter code was added, it slowed parsing down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants