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

Make == a symmetric method #3608

Open
mahrud opened this issue Dec 11, 2024 · 1 comment
Open

Make == a symmetric method #3608

mahrud opened this issue Dec 11, 2024 · 1 comment

Comments

@mahrud
Copy link
Member

mahrud commented Dec 11, 2024

Equality is always a symmetric relation, and I think we should implement an exception for == so that for instance the method Vector == ZZ will automatically also be used if ZZ == Vector is called.

Here is a potential implementation:

eq0 = lookup(symbol==, Thing, Thing)
Thing == Thing := (a,b) -> (
    if lookup(symbol==, class b, class a)
    =!= lookup(symbol==, class a, class b)
    then b == a else eq0(a,b))

I don't like it very much, but it works:

i1 : eq0 = lookup(symbol==, Thing, Thing);

i2 : Thing == Thing := (a,b) -> (
    if lookup(symbol==, class b, class a)
    =!= lookup(symbol==, class a, class b)
    then b == a else eq0(a,b));

i3 : 1 == vector {1,2}
stdio:2:149:(3):[1]: error: no method for binary operator == applied to objects:
            1 (of class ZZ)
                              2
     ==     | 1 | (of class ZZ )
            | 2 |

i4 : Vector == ZZ := (v,f) -> matrix v == f;

i5 : 1 == vector {1,2}
stdio:4:34:(3):[2]: error: matrices have different shapes

i6 : 1 == vector {1}

o6 = true
@mahrud
Copy link
Member Author

mahrud commented Dec 11, 2024

A more involved but perhaps better solution might be to change specifically how methods on == are installed, i.e. change Type == Type := Function which has the unfortunate method key ((symbol ==, symbol =), Type, Type) to reorder the two types based on which type is younger, and also change lookup to reorder the keys based on seniority as well. Note that the method is always installed on the younger of the two objects anyway (e.g. in the example above Vector#(symbol ==, Vector, ZZ)).

Perhaps a more generalized version of this solution is to add a Symmetric option to method which would tell lookup and installMethod to reorder the types and inputs based on seniority of the types first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant