feat(pattern): add operator sugar for combinators - #50
Merged
Conversation
Problem - The pattern algebra had only function-call forms (neg, any, all), which read noisily in case lists compared to the boolean operators they model. Implementation - Add operator! for neg(p), operator|| for any(a, b), and operator&& for all(a, b). - Declare the overloads in ptn::pat::base: every pattern derives from pattern_base, making that namespace ADL-associated for all pattern types (concrete patterns live in ptn::pat::detail and ADL does not ascend namespaces). - Constrain operands to patterns that are not guard predicates, so guard-level && / || keep their pred_and / pred_or semantics. Tests - Nine cases in tests_combinator: type identity of !p vs neg(p), or/and/chained/double-bang behavior, sugar on type patterns, and guard-operator non-interference. - Full suite: 204/204 (Clang 20, C++17). Notes - `>>` binds tighter than || and &&, so combined patterns need parentheses in a case: (lit(1) || lit(2)) >> handler. Unary ! needs none. Documented in docs/api.md and README.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add operator forms for the pattern combinators:
!pforneg(p),(a || b)forany(a, b), and(a && b)forall(a, b). The pattern algebra now reads like the boolean logic it models.Changes
negation.hpp:operator!equivalent toneg(p).combinator.hpp:operator||equivalent toany(a, b);operator&&equivalent toall(a, b).ptn::pat::base: every pattern derives frompattern_base, which makes that namespace ADL-associated for all pattern types. (Concrete patterns live inptn::pat::detail, and ADL does not ascend enclosing namespaces — declaring the operators inptn::patwould make them unfindable.)&&/||inside[...]keep theirpred_and/pred_orsemantics — verified by test.docs/api.mdcombinator sections, roadmap WIP entry, README negation example.Testing
tests_combinator.cpp: type identity of!pvsneg(p), or/and/chained-or/double-bang behavior, sugar mixing pattern kinds (is<int> || is<std::string>), and guard-operator non-interference.>>binds tighter than||/&&, so combined patterns need parentheses in a case —(lit(1) || lit(2)) >> handler. Unary!needs none.