Conversation
…-aware fold) by introducing topoShapeSort to require a structure's internal components to be processed first. Graph structures require careful sorting to make this work correctly.
…dering Replace sortByArity with topoShapeSort, adding Kahn's algorithm for correct within-bucket ordering of GAnnotation and GOther elements. Annotations that annotate other annotations (and GOther-of-GOther chains) are now guaranteed to receive their dependencies' results in subResults, fixing the silent-miss bug where paraGraph would fold containing elements before their contents when both were in the same shape-class bucket. - withinBucketTopoSort: new Kahn's algorithm helper; cycle members appended in input order after non-cycle elements, no error raised - topoShapeSort: renamed from sortByArity, gains GraphValue v constraint, applies withinBucketTopoSort to GAnnotation and GOther buckets - paraGraph/paraGraphWithSeed: updated call sites; subResults lookup simplified to mapMaybe - Haddock updated for paraGraph (Processing Order + subResults contract), paraGraphFixed (topoShapeSort reference + cycle note), and module header (shape vocabulary replacing arity language) - Four new tests: annotation-of-annotation ordering (T004), cycle soft-failure (T005), SC-006 fold correctness (T006), GOther ordering (T007) - 842 tests, 0 failures; zero remaining sortByArity references Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR introduces a structure-aware folding order for graph operations by implementing the topoShapeSort function to replace sortByArity. The change ensures that graph elements are processed in the correct order, respecting both shape class priorities and within-bucket dependencies. This is particularly important for annotations and GOther elements that can reference elements of their own type.
Changes:
- Renamed
sortByAritytotopoShapeSortand implemented Kahn's algorithm for within-bucket topological sorting in annotation andGOtherbuckets - Updated
paraGraph,paraGraphFixed, andparaGraphWithSeeddocumentation to reflect the new ordering semantics and explicitsubResultscontract - Added comprehensive test coverage for annotation-of-annotation,
GOther-of-GOther, and cycle handling scenarios - Created extensive reference documentation at
docs/reference/features/para-graph.md
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| libs/pattern/src/Pattern/Graph/Transform.hs | Core implementation: renamed function, added withinBucketTopoSort with Kahn's algorithm, updated documentation |
| libs/pattern/tests/Spec/Pattern/Graph/TransformSpec.hs | Added 4 new test cases for annotation-of-annotation ordering, cycle handling, fold correctness, and GOther ordering |
| docs/reference/features/para-graph.md | New comprehensive reference guide covering mental model, processing order, laws, examples, and cross-language notes |
| specs/037-topo-shape-sort/*.md | Complete specification suite including tasks, spec, research, plan, data model, contracts, and quickstart guide |
| CLAUDE.md | Technology tracking update for feature dependencies |
| cabal.project.local | Removed tests: True configuration (unrelated to feature) |
| apps/gramref-cli/gramref-cli.cabal | Changed from Custom to Simple build type (unrelated to feature) |
| apps/gramref-cli/Setup.hs | Deleted custom setup code for man page installation (unrelated to feature) |
Comments suppressed due to low confidence (1)
apps/gramref-cli/Setup.hs:1
- The deletion of this entire Setup.hs file (68 lines of custom setup code for man page installation) appears unrelated to the "037-topo-shape-sort" feature. This is not mentioned in the PR description or any of the specification documents. This change should be explained in the PR description or done in a separate commit/PR.
| @@ -1,2 +1 @@ | |||
| ignore-project: False | |||
There was a problem hiding this comment.
This change appears unrelated to the "037-topo-shape-sort" feature described in the PR. The removal of the tests: True configuration from cabal.project.local is not mentioned in the PR description or any of the specification documents. If this is an intentional cleanup, it should be explained in the PR description or done in a separate commit/PR.
This pull request introduces a new structure-aware folding order for graph operations in Haskell's
Pattern.Graph.Transformmodule, centered around the newtopoShapeSortfunction and its integration into theparaGraphfamily of folds. The changes ensure that graph elements are processed in an order that respects both containment and dependency, particularly for annotations and other complex elements. This is thoroughly documented in a new reference guide.Key changes:
Structural Folding and Processing Order
Introduced the
topoShapeSortfunction to replace the previous flat arity-based sort. This function partitions elements by shape class (nodes, relationships, walks, annotations, other), then applies a topological sort within the annotation and "other" buckets using Kahn's algorithm to respect within-bucket dependencies and handle cycles gracefully.Updated
paraGraphandparaGraphWithSeedto usetopoShapeSortfor element ordering, ensuring each element receives sub-results for all previously processed direct sub-elements, and handling cycles by omitting unprocessed sub-results. [1] [2]Improved the
subResultscontract in documentation and code: sub-results for each element are now a best-effort list, possibly omitting results for sub-elements involved in dependency cycles.Documentation and Reference
Added a comprehensive reference document
docs/reference/features/para-graph.mddetailing the mental model, processing order, sub-result contract, cycle handling, and usage examples forparaGraphandtopoShapeSort. This serves as a guide for both users and implementers.Enhanced module documentation in
Pattern.Graph.Transformto explain the new shape-based classification and folding order.Technology Tracking
CLAUDE.mdto reflect the addition ofData.Map.Strict,Data.List (foldl'), andData.Maybe (mapMaybe)as active technologies for the037-topo-shape-sortfeature, and noted the in-memory, pure algorithmic nature of the implementation. [1] [2]These changes provide a robust, well-documented foundation for structure-aware graph folding, enabling correct and efficient analysis of complex graph structures with annotations, cycles, and arbitrary containment.