-
Notifications
You must be signed in to change notification settings - Fork 13
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
Deforest within the core language #180
Deforest within the core language #180
Conversation
This form is intended to express any deforestable expression, allowing the core language to express deforestation semantics, which, formerly, we were not able to do within the language and thus resorted to matching, and optimizing, host language syntax, leading to a "host" of problems. This new form is groundwork to enable compiler optimizations being defined purely on the core language, thus representing a clean boundary, or contract, between Qi and the host (Racket). The initial implementation here just introduces the form, and code generation for `filter` specifically, as a proof of concept for the more generic and extensible planned implementation. See the meeting notes for more, e.g.: https://github.com/drym-org/qi/wiki/Qi-Meeting-Jun-21-2024#implementing-it
Have it just wrap the a priori syntax so that we can transition the functionality to the architecture based on the new core form.
Avoid matching host expressions literally and instead match the newly introduced `#%deforestable` core form.
As deforestable forms like `map` are now macros (syntax) rather than partial application of host language functions, they are not affected by threading direction.
The function positions in deforestable operations are Racket expr positions, but we want them to be Qi floe positions instead. This modifies the code generation step to recursively invoke codegen on these nested floe positions.
Based on recent discussions, as a general maxim: Our core language should be rich enough to express desired optimizations. Initially, as this wasn't the case, we were performing deforestation by matching host language forms. This of course meant that we were constrained to Racket syntax in such functional operations. Now that we are broadening our core language to express deforestation, in keeping with the above maxim, we would prefer to support Qi syntax in function positions in these operations. Towards this goal, this new syntax for the `#%deforestable` core form introduces support for `floe` positions. Right now, it simply segregates arguments into `expr` and `floe` positions so that these are appropriately expanded. The code generation still matches the name of the functional list transformation (e.g. `map`, `filter`) and "hardcodes" the known invocation of the corresponding underlying operation. Eventually we hope to make deforestation user-extensible to arbitrary functional list (at least) operations. At that stage, we wouldn't have this kind of standard information that we could leverage during code generation, so we will need to modify the syntax of `#%deforestable` to encode enough information to be able to perform appropriate code generation for arbitrary user-defined operations. We are not there yet :)
As the deforestation pass generates escaped Racket, we need to compile any higher-order `floe` positions in the fusable list operations at this stage, since the regular code generation step at the end of compilation would not operate on these resultant escaped expressions.
PS I still get compilation errors in qi-sdk at the tip of this branch (or on the deforest-all-the-things branch). The errors are different, though:
|
- use left-threading in most tests - one test using right-threading to validate deforestation is invariant to threading direction - use `range` with syntactically specified arguments; remove tests using templates - consolidate `deforest-pass` tests since we no longer have a separate test suite for individual applications of the deforestation rewrite rule (should we?)
This is ready for review. I've updated the PR description with the list of changes. One change to discuss is that since
|
Which behavior would be gone?
Maybe I'm misreading "currently"—does that refer to |
Sorry, I've been using "formerly" and "currently" in confusing ways :) On the main branch, we support
and so on. This is because
We could use bindings to replace templates, like:
How do we feel about this? |
I think it's a bit odd that Does this change happen only when using |
It's only with
Is there another behavior you'd prefer? |
Hm. It might be annoying to have different enough semantics that
Sorry, I'm probably mixing concerns here. I think this touches on the semantics of range as a flow, though: I think I'd expect something like (using syntax-parse for convenience of notation)
That still (probably) doesn't get you to template support, but it does allow more variations, right? I'm probably getting mixed up. I could see things like In any case, I'm happy with the current version (since it's segregated to (A piece of the puzzle: how is |
Yes, we could add support for more arguments and the usual templates over time (maybe even in the near future) if we want. One thing to note is that the many permutations of range syntax was responsible for a lot of complexity in pattern matching, and in the runtime implementation too IIRC (some of which is still in the code and is probably unused at the moment). It would be worth seeing whether the change to re: how
If range were not deforested, then we would generate the first list in full before the deforested portion begins at the |
Ah, sorry: by "connected" I meant "where are the relevant pieces of implementation." The translation of range to a thunk doesn't help me see how a later pass can look for range to optimize it, which is one reason I'm confused. |
Ah, well on the second point, we are now only optimizing Qi core language expressions, not host expressions as we do on the main branch. So, we would never optimize it further once it has already been compiled by the deforestation pass -- at that point, it's now the Racket compiler's responsibility. If on the other hand the deforestation pass does not optimize a On the former point, where are the pieces of the implementation, I think @dzoep is most familiar with that. |
But since he is on vacation on the coast of France 🧑🎨 🚗 🏖️ , I'll take a stab at it! I'd look in |
When a nested form has a different chirality (threading direction) than a containing form, normalization would not collapse them, but deforestation may not care about the difference. Possible approaches: A. Introduce normalization rules designed to detect when change of chirality is irrelevant. B. Look for patterns in the deforestation pass involving differing threading directions Probably (A) is the right approach, and we could introduce a set of chirality normalization rules that "trim" forms on either end of a nested form which could be collapsed into the containing form. This would include anything that isn't a host language function application (which is the only case where chirality matters). Actually, thinking again, chirality is already represented in the core language simply as the presence of a blanket template in a function application form, and nested threading is already collapsed by normalization, so, I'm not sure anymore why this test is failing ¯\_(ツ)_/¯
You may be amused by the latest two commits @benknoble and @dzoep . Now I'm wondering if I should squash the two commits and make it look like I did it right the first time, or retain the actual sequence of events revealing my confusion 😆 |
I'm still confused: isn't |
@benknoble |
To clarify, then: does that mean that with qi/list it's not possible to flow a function into a filter on a list, or both a function and a list into a filter? |
@benknoble Yes, that's right. We could potentially support the kind of positional behavior that we have for regular functions with templates and so on, do you think we should? It might add a lot of complexity but I'm not sure. At that point, we should probably evaluate whether we should be supporting positional behavior across the board for all Qi forms (and not just functions). If we can find a simple, maintainable, way to do it, that could be interesting. |
I'm not sure I'm going that far.
It just seems like this means requiring Again, from the perspective of " |
@benknoble I'd be open to supporting the full complement of template syntax, But also, that doesn't need to be part of this PR necessarily. The present PR's main purpose is to restructure Any other comments on this PR? |
@benknoble We discussed more today in the meeting and said that for some forms (like list-oriented forms), for values computed at runtime, it would be preferable to support templates rather than rely only on bindings, so we brainstormed a possible scheme for defining such forms. I didn't catch all the details but I've summarized it in the meeting notes. We felt that it would be good to merge the present PR before venturing down these directions though, and then continue against the integration branch. Further comments welcome, and thank you! |
Oh, yes: that's all perfectly fine RE: merging to continue work. I probably should have deferred the design discussion to a more appropriate place than a PR review :) |
Re-reading, this seems to be the essence of what I needed for the thunk question: that translation is the "last fallback," if you will. I'm still surprised it's nullary and won't consume further arguments, but the idea that multiple passes first get a stab but you need a final, unoptimized implementation too makes sense. |
It's hard to say what the right place for a design review is - I'm glad you raised your concerns somewhere @benknoble 😄 @benknoble @dzoep Any other feedback? If not, then I think we can probably merge this tomorrow. |
@benknoble re: the runtime for |
Merging now, thank you! |
9c4bff6
into
drym-org:deforest-all-the-things
We were formerly doing deforestation against host language bindings. That is, sequences of host-language functions that were deforestable from Qi's perspective were matched in the Qi compiler and provided deforested implementations. As these were host functions, they did not require us to specify a runtime in Qi.
This PR introduces an explicit core language form
#%deforestable
, intended to express all deforestable operations within the language. These deforestable operations are now Qi macros inqi/list
expanding to the#%deforestable
core form, and we then match sequences of#%deforestable
in the compiler in order to apply deforestation, keeping compilation formally contained within Qi. These macros also support Qi syntax in higher-order positions.Finally, we must provide a runtime for
#%deforestable
in the code generation step, to capture any such operations that were not deforested. Currently, we explicitly encode the runtimes for each operation, likemap
,filter
, etc., and this achieves the end-to-end behavior we are looking for. Going forward, we would like to define a more generic syntax for#%deforestable
, along with an API (a set of macros, maybe resemblingdefine-qi-deforestable-producer
,transformer
,consumer
) that would allow us to specify the runtime via this interface so that we don't need to encode the runtime directly in the compiler.Summary of Changes
#%deforestable
core formmap
,filter
, and other existing deforested operations inqi/list
range
syntax to accept only syntactic arguments (at least for now)qi/list
qi/list
Migrated from dzoep#2 to use the new integration branch.
Public Domain Dedication
(Why: The freely released, copyright-free work in this repository represents an investment in a better way of doing things called attribution-based economics. Attribution-based economics is based on the simple idea that we gain more by giving more, not by holding on to things that, truly, we could only create because we, in our turn, received from others. As it turns out, an economic system based on attribution -- where those who give more are more empowered -- is significantly more efficient than capitalism while also being stable and fair (unlike capitalism, on both counts), giving it transformative power to elevate the human condition and address the problems that face us today along with a host of others that have been intractable since the beginning. You can help make this a reality by releasing your work in the same way -- freely into the public domain in the simple hope of providing value. Learn more about attribution-based economics at drym.org, tell your friends, do your part.)