Skip to content

Infix, Prefix, Postfix: do not format their first argument as a whole - #2014

Merged
rocky merged 2 commits into
Mathics3:masterfrom
tombouctou:fix-format-infix-self
Oct 17, 2026
Merged

rocky merged 2 commits into
Mathics3:masterfrom
tombouctou:fix-format-infix-self

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

A format rule that wraps the expression it formats in Infix, Prefix or Postfix made printing hit $RecursionLimit:

In[1]:= Format[p[t__]] := Infix[p[t], "~"]; p[a, b]
master:  $RecursionLimit::reclim: Recursion depth of 200 exceeded.
branch:  a ~ b

Before boxing, do_format_element formats every subexpression of the result of a format rule. It therefore formatted p[a, b] inside the Infix again, which gave the same Infix, and so on.

Infix, Prefix and Postfix display only the elements of their first argument, never the argument itself. Now only those elements are formatted. Defining a format this way is the usual way to give an expression an operator form, and code written for WMA relies on it: the Toda-bracket package we are running on Mathics3 (see #2001) has Format[t_o] := Infix[t, "∘"] /; Length[t] > 1.

Format[pLeft[t__]] := Infix[pLeft[t], "~", 300, Left]; pLeft[pLeft[a, b], c]   a ~ b ~ c
Format[pPre[x_]] := Prefix[pPre[x], "!"]; pPre[a]                             !a
Format[pPost[x_]] := Postfix[pPost[x], "!!"]; pPost[a]                        a!!

Tests: five new cases in test_layout in test/format/test_format.py. All five fail on master.

🤖 Generated with Claude Code

Format[p[t__]] := Infix[p[t], "~"] made printing p[a, b] hit
$RecursionLimit. Before boxing, do_format_element formats every
subexpression of the result of a format rule, so it formatted p[a, b]
inside the Infix again, which gave the same Infix, and so on.

Infix, Prefix and Postfix display only the elements of their first
argument, never the argument itself, so only those elements are
formatted now. This is
how WMA handles such definitions, which are the usual way to give an
expression an operator form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mmatera

mmatera commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Makes sense. LGTM

@rocky
rocky merged commit 20e28c2 into Mathics3:master Oct 17, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants