fix: exactly_one/1 produces wrong results for N > 2 variables#26
Merged
zachdaniel merged 2 commits intoMay 14, 2026
Merged
Conversation
The previous implementation used a binary fold with `nand/2`, which incorrectly treated the accumulated expression as a single variable at each step. For N > 2 variables this produced both logically wrong results and unnecessary expression expansion. Fixed by combining at_least_one (OR of all variables) with at_most_one/1 (pairwise negation), which is the standard encoding.
maennchen
approved these changes
May 14, 2026
Pins decimal >= 3.0 with override to resolve GHSA-rhv4-8758-jx7v in dev/test env.
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
Expression.exactly_one/1uses a binary fold withnand/2that incorrectly treats the accumulated compound expression as a single variable at each step. For N > 2 variables this produces logically wrong results: assignments that should be rejected are accepted, and vice versa.The fix uses the standard encoding:
at_least_one(OR of all variables) combined withat_most_one/1(pairwise negation). This is semantically correct for any N.Impact
Crux.exactly_one/1is the recommended replacement for the deprecatedAshPolicy.Expr.mutually_exclusive_and_collectively_exhaustive/1. Any Ash policy using N > 2 mutually exclusive predicates viaCrux.exactly_one/1will produce incorrect access control decisions.Formula.from_expression/1.Changes
exactly_one/1to useat_most_one/1combined with an OR of all variablesTest plan
mix test test/crux/expression_test.exs— all 90 tests + 48 doctests pass