Skip to content

Conversation

Bumblebee00
Copy link
Contributor

@Bumblebee00 Bumblebee00 commented Aug 8, 2025

this pr aims to solve the oooomm problem described in this issue

Proposed solution

basically it works like this: when the rule is created with the macro, another argument is passed to the function that creates the matchers. This argument is a function that given a dictionary of corrispondences (slot, matched value), substitutes every slot with its mached value in the condition expression (passed after the where keyboard) and evaluates it to true or false, this function is called condition. Then every time a term matches succeeds and should call the next matcher or the succes function, before that I put a check, the check_conditions function. check_conditions tries (with a try block) to call condition. If we are not yet at the end of the matching process, the dictionary will not be complete and the condition function will error, so the check_conditions will return true and make the matching process continue. If instead condition doesnt error, it means we are at the end of the matching process, it will be true or false, and depending on that we continue or no the matching process.

With this implementation we have:

julia> r = @rule (~x)^(~m)*(~y)^(~n) => (~x, ~m, ~y, ~n) where (~m)^(~n)==8
(~x) ^ ~m * (~y) ^ ~n => ((~x, ~m, ~y, ~n) where (~m) ^ ~n == 8)

julia> r((a^2)*(b^3)) 
(a, 2, b, 3)

julia> r((b^2)*(a^3)) 
(b, 2, a, 3)

Problems of the proposed solution

As said before if we are not yet at the end of the matching process ,the dictionary of (slot, matched value) will be probably incomplete, and probably the condition expression cannot be built and evaluated.

In particular if one has a condition that requires a lot of matched variables, and the commutative operation is not the first of the expression, for every possible rule match the condition expression will try to be build but without success, and the rule will be evaluated only at the end, effectively making this proposed solution behave in the same way as the original code. For example

julia> r2 = @rule mod((~x)^(~m)*(~y)^(~n) , ~z) => (~x, ~m, ~y, ~n) where ((~z)!==nothing &&  (~m)^(~n)==8)
mod((~x) ^ ~m * (~y) ^ ~n, ~z) => ((~x, ~m, ~y, ~n) where ~z !== nothing && (~m) ^ ~n == 8)

julia> r2(mod((a^2)*(b^3),1))
(a, 2, b, 3)

julia> r2(mod((b^2)*(a^3),1))
nothing

in this rule the ~z slot is not matched before the matchers try al the possible combinations of arguments in the multiplication. Therfore the conditions can never be built

In[518]:= Foo[Mod[(x_^m_) (y_^n_), a_]] := x /; a == 1 && m^n == 8
Foo[Mod[x^3*y^2, 1]]
Foo[Mod[x^2*y^3, 1]]

Out[519]= y

Out[520]= x

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