Skip to content

feat(stdlib): add variadic arithmetic macros and in-place inc/dec#1529

Open
sqrew wants to merge 4 commits intocarp-lang:masterfrom
sqrew:stdlib-arithmetic-ergonomics
Open

feat(stdlib): add variadic arithmetic macros and in-place inc/dec#1529
sqrew wants to merge 4 commits intocarp-lang:masterfrom
sqrew:stdlib-arithmetic-ergonomics

Conversation

@sqrew
Copy link
Copy Markdown
Contributor

@sqrew sqrew commented Apr 11, 2026

This PR introduces several ergonomic improvements to the arithmetic facilities in the Carp standard library.

Changes:

  • Variadic Macros: Added sum, product, sub, and div. These allow for more idiomatic Lisp-style arithmetic like (sum 1 2 3 4).
  • Unary Negation: (sub x) now correctly expands to (neg x).
  • In-place Operations: Added inc! and dec! macros for cleaner variable updates.
  • Robust Expansion: Implemented a recursive left-associative expansion (build-vararg-left) to ensure correct results for non-commutative operations like subtraction and division.
  • Tests: Integrated 10 new test cases into test/macros.carp.

Disclaimer: This PR was built with the assistance of Gemini (an AI assistant) working in collaboration with @sqrew to improve the Carp standard library ergonomics.

Copy link
Copy Markdown
Member

@hellerve hellerve left a comment

Choose a reason for hiding this comment

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

I like this in general!

One thing: if we want to express that integers form a ring, we should handle identity in sum and product(0 and 1, respectively). That’s not necessary.

@hellerve
Copy link
Copy Markdown
Member

Ci seems unhappy. 365m runtime seems, uh, excessive.

- Added sum, product, sub, and div macros.\n- Added inc! and dec! for in-place updates.\n- Implemented left-associative expansion for non-commutative operations.\n- Added comprehensive tests in test/macros.carp.
@sqrew sqrew force-pushed the stdlib-arithmetic-ergonomics branch from fc1bd7f to e7f14b3 Compare April 27, 2026 12:53
Comment thread core/Macros.carp
Comment on lines +462 to +468
(doc inc! "Increments a variable in-place.")
(defmacro inc! [var]
(list 'set! var (list 'inc var)))

(doc dec! "Decrements a variable in-place.")
(defmacro dec! [var]
(list 'set! var (list 'dec var)))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just realized we already have ++ and --, which do the same thing. I actually like this naming better. What do the others think? I think one should go in favor of the other in any case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The thing I like about inc!/dec! is that the ! makes it clear it's an in place mutation and that it's a macro. I personally prefer ++ (because it's cooler) and totally spaced that it was even here. I'm totally open to either option honestly.

@sqrew
Copy link
Copy Markdown
Contributor Author

sqrew commented May 4, 2026

Turns out the CI issues were name conflicts causing a hang up during macro expansion with common names like sum/sub/mul/div etc, so I've added a -variadic suffix (ie sum-variadic, etc) to each of them. This definitely makes it ugly as hell. Unsure if we really want them added to the language in that form. Thoughts from you guys?

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.

2 participants