Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New composition macro #220

Merged
merged 4 commits into from
Jun 8, 2023
Merged

New composition macro #220

merged 4 commits into from
Jun 8, 2023

Conversation

gilch
Copy link
Owner

@gilch gilch commented Jun 8, 2023

Experimental. Unstable. Getting better! See also #218.

Renamed the# to my#. Breaking change if you were using that.
Added nil#. It's one of those questionable, but maybe too-useful-to-omit macros I've been wanting for a while. Not sure if I'll keep it.

Probably nobody else is doing this yet, but I also broke Sybil tests with a modification to the compiler. Direct recursive macros no longer repeat the same expansion comment, but just keep a tally with an additional #.

The new composition macro (going with "synexpand") just does compile-time rewrites. There's no longer a run-time stack. I considered just composing functions at read time, which would have worked pretty well, but doing everything at compile time comes out even cleaner. And what's more, macros often work as terms in the mini-language and I kind of get apply for free. We could have almost gotten this much using normal-order function application with the composed function approach, which requires memoization for efficient evaluation, but that's too hard to shoehorn that into Python's pervasive mutability. Even a lot of the builtins return mutable iterators. Not as nice as Clojure's seqs. Python 2 was nicer in some ways.

PACK no longer requires a MARK. It will use the entire stack if there's no MARK to stop it short of that. Because they can now be unbalanced, I've changed them to <> instead of []. Because this creates a tuple at compile time, it can be used as an apply. If you just want the data itself, you can put `@ in the head to make it a list or quote it with quote:<.

The syntax expands directly to expressions, but common use cases would want a function. Like most macros, you can convert to a function by wrapping in a lambda, although this still can't use anything only knowable at run time (including, perhaps, the number of arguments, making variadic functions a problem). The easiest way would be to use X# and friends, but this still requires you to spell out the points, when most of the point of this was to be point-free. So I added helpers ^#, ^^#, ^^^#, and ^^^^# for arities 1-4.

gilch added 4 commits June 7, 2023 00:03
Prepends a # instead of adding another line.
Now compile-time rewrites instead of a run-time stack. Enables the use of macro terms in many cases, but requires adapters to use like functions.
@gilch gilch merged commit 253fc69 into master Jun 8, 2023
@gilch gilch deleted the new-compose branch June 8, 2023 01:12
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