-
Notifications
You must be signed in to change notification settings - Fork 6
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
Potential nontermination for flat operators #53
Comments
Pardon my slight digression, but this seems like the right issue on which to ask a design question. Why are the orderless and flat properties implemented in the match function? It seems that most of the same functionality could be implemented with rules? Lets assume that flatness is implemented with a flattening rule, and orderlessness is implemented with an ordering rule. If one wanted to match the terms Edit: Okay I see now how Orderless properties make it easier to implement EvalRules, for instance. It would be possible to implement without a context, but one would need to make special eval rules for orderless functions, etc. https://github.com/HarrisonGrodin/Rewrite.jl/blob/03af1708d18f17efbc6e604b58c4d4943930607b/src/rule.jl#L98-L104 |
TL;DR: variables are hard. Unfortunately, orderless matching is fairly necessary, even for pattern-based rules; the underlying reason is that there's no logical ordering for variables. Consider the following (albeit somewhat contrived) example: rs = @term RULES [
x + 0 + 5 => x + 5
]
a = @term(sin(1) + 0 + 5)
b = @term(0 + 3 + 5) I would argue that no sensible ordering would remove the need for orderless matching. In general, it seems reasonable that constants should be ordered by magnitude. It seems that we should keep It does seem like a significant speedup for orderless matching could be achieved under the knowledge that terms are automatically ordered, though, simply moving variables through the term without modifying the relative order of the rest of the arguments. (I've opened #55 for this purpose.) Flat matching is "less required" from a theoretical point of view, since rules can be initially rewritten to prefer left- or right-associativity (see Completion.jl). However, from a practical perspective, it's an extremely useful feature to have, especially since Rewrite follows the Julian precedent of treating associative/flat operations as if they have variable arity. Let rs = @term RULES [
f(x, x) => x^2
]
@syms a b
normalize(@term(f(a, b, a, b)), rs)
# @term(f(a, b)^2) Here, Rewrite is able to leverage flat matching to interpret Dealing with the properties themselves by writing rules is typically the best approach. In addition to |
Thanks for the detailed response! This has been very helpful, and I have a renewed respect for the |
I think I recently found a simple instance of this:
seems to not terminate. |
The clash between flattening rules and flat pattern matching causes nontermination with some flat operators. For example:
This exists regardless of whether the flat operator is explicitly given in the rule (e.g. current
master
) or if the operator is variable (e.g. #52).The text was updated successfully, but these errors were encountered: