Skip to content

fix(tensorlogic): add __rmul__/__radd__ for scalar-on-left ops (AX-038)#76

Merged
jwalin-shah merged 2 commits into
mainfrom
fix/tensor-logic/rmul-radd
Jun 21, 2026
Merged

fix(tensorlogic): add __rmul__/__radd__ for scalar-on-left ops (AX-038)#76
jwalin-shah merged 2 commits into
mainfrom
fix/tensor-logic/rmul-radd

Conversation

@jwalin-shah

Copy link
Copy Markdown
Owner

Adds __rmul__ and __radd__ on Tensor so expressions like 2 * T and 2 + T route through the right side and produce the same value as T * 2 / T + 2. Without these, Python tries int.__mul__(Tensor) first and raises TypeError on a non-numeric operand type.

Ref: AGY audit 2026-06-21 TENSOR-LOGIC-OPS / AX-038.

jwalin-shah and others added 2 commits June 15, 2026 22:09
Co-Authored-By: Claude <noreply@anthropic.com>
…LOGIC-OPS)

Scalar * TensorRef previously raised TypeError because Python's
int.__mul__ returned NotImplemented. Adding reflected ops on
TensorRef / Scalar / Product / Sum makes 2*t == t*2 and 1+t == t+1.

Tests verify commutativity for Product and Sum and that reflected
ops on nested expressions produce the right Product/Sum shape.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 88e01804e6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tensor_logic/language.py
Comment on lines +64 to +69
def __rmul__(self, other) -> "Product":
# TENSOR-LOGIC-OPS: when the left operand is a Python number,
# Python calls `int.__mul__(other, self)`, which returns
# NotImplemented for our Expr. Dispatch back through as_expr
# so `2 * s` and `s * 2` produce the same Product.
return Product(as_expr(other), as_expr(self))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add normal Scalar operators for parsed scalar-left rules

For .tl rules where a numeric literal is on the left, the parser creates a Scalar first (_parse_factor) and _parse_product then evaluates expr * next_factor, so TensorRef.__rmul__ is never invoked. As a result, rules like scaled(x,y) := 2 * edge(x,y) still raise TypeError for Scalar * TensorRef while edge(x,y) * 2 works. Please add Scalar.__mul__ (and the matching __add__ for 2 + edge(x,y)) or route these parser operations through as_expr explicitly.

Useful? React with 👍 / 👎.

@jwalin-shah
jwalin-shah merged commit f8f94ef into main Jun 21, 2026
1 check failed
@jwalin-shah
jwalin-shah deleted the fix/tensor-logic/rmul-radd branch June 21, 2026 15:39
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.

1 participant