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

equality and hash for terms and schemas #241

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

kleinschmidt
Copy link
Member

based on #174 (upstream user/repo seems to have been deleted) with a few tweaks

Comment on lines +59 to +64
length(first.schema) != length(second.schema) && return false
for key in keys(first)
!haskey(second, key) && return false
second[key] != first[key] && return false
end
true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this identical to first == second? If not, maybe worth a comment. (Note that this throws if the dict contains missing, maybe that's OK though.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I think you're right. Probably fine to just check first.schema == second.schema since that's really what this check is about

Comment on lines +57 to +58
first === second && return true
first.schema === second.schema && return true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this will be wrong if the dict contains missing (recursively). Can this happen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a key or a value? Not possible either way at the moment (unless it's a pathological manuallly constructed instance)

Copy link
Member

@nalimilan nalimilan Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a value I think, as for keys dicts use isequal. Note that this also applies if the value contains an object which contains a missing value (whatever the number of recursions).

true
end

Base.hash(schema::Schema, h::UInt) = hash(schema.schema, h)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes (e.g. for arrays and tuples) we add an arbitrary constant to h (type-specific) to ensure that hash(schema) != hash(schema.schema). Not sure whether it's worth it here though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wondered about that too. Easy to do here, a bit tricker in the case of the terms (where the types might not be the asme for things that we want to be ==, e.g. different instances of a function term whre the anonymous function is different but the underlying expression is the same).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And that can "corrupt" the type of containers because of the ahem zealous use of type parameters in this codebase

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't necessarily need to hash the type: you can just define a constant and use it for all types which compare ==.

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.

3 participants