Pretty-printer: parenthesize not X operands of infix ops (closes #987)#989
Merged
Conversation
`precedence` returned `None` for the `IfThen(_, _, _, Bool false)` AST that desugars `not X` (and the `X ≠ Y` sugar). So `op_arg_str` never wrapped a `not X` argument of `=`, `<`, `+`, etc. -- but the parsers' `NOT` branch consumes `parse_term_equal`, meaning the printed `(not (P and Q)) = (...)` reparsed as `not ((P and Q) = (...))` with the outer connective flipping from `=` to `not`. De Morgan's laws (`lib/Base.pf:not_and`, `not_or`, `double_neg`) were the canonical witnesses; the sweep found the same drift in 14 other `lib/` files. Return `0` from `precedence` for the `not X` shape so it is strictly below every `infix_precedence` value (1..8), forcing parens in `op_arg_str`. - `test/should-validate/not_paren_roundtrip.pf`: De Morgan-shaped regression fixture covering `(not (P and Q)) = ((not P) or (not Q))` and the dual. - `test-deduce.py`: register the new fixture under `PARSER_ROUND_TRIP_FILES` and promote `lib/Base.pf` from the equivalence-only block into the round-trip block (it round-trips cleanly with the fix). Refs #931. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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
precedencereturnedNonefor theIfThen(_, _, _, Bool false)ASTthat desugars
not X(and theX ≠ Ysugar). Soop_arg_strneverwrapped a
not Xargument of=,<,+, etc. — but the parsers'NOTbranch consumesparse_term_equal, meaning the printed(not (P and Q)) = (...)reparsed asnot ((P and Q) = (...))withthe outer connective flipping from
=tonot. De Morgan's laws(
lib/Base.pf:not_and,not_or,double_neg) were the canonicalwitnesses; the sweep found the same drift in 14 other
lib/files.Return
0fromprecedencefor thenot Xshape so it is strictlybelow every
infix_precedencevalue (1..8), forcing parens inop_arg_str.test/should-validate/not_paren_roundtrip.pf: De Morgan-shapedregression fixture covering
(not (P and Q)) = ((not P) or (not Q))and the dual.
test-deduce.py: register the new fixture underPARSER_ROUND_TRIP_FILESand promotelib/Base.pffrom theequivalence-only block into the round-trip block (it round-trips
cleanly with the fix).
Closes #987. Refs #931.
The sweep also surfaced four other unrelated drift categories in
lib/(public-import visibility,apply IH[…] to …AllElim/ApplyDefsFact nesting, the
∅set literal, andapply f to a, apply g to bPTuple flattening); those are tracked together in #988 and nottouched here.
Test plan
make static— ruff + mypy (incl.--no-site-packages) cleanpython3.13 test-deduce.py --equiv— passes (curated grammarcorpus + pretty-print round trip)
python3.13 test-deduce.py— full local suite passes(
cli + lib + passable + errors + warns + equiv, 141s)Claude session — fallback UUID:
c957d956-19db-4a98-882f-cc68756db73b🤖 Generated with Claude Code