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

Allow some function equality comparison #1978

Merged
merged 3 commits into from
Jul 1, 2024

Conversation

yannham
Copy link
Member

@yannham yannham commented Jun 26, 2024

Relax the initial restriction on comparing functions, introduced in #975, which is that comparing a function to anything else errors out. In practice comparing a function to a value of some other type isn't a problem, and can be useful to allow patterns like fun x => if x != null then ... else ... without having first to defensively check if x is a function, because that would fail at runtime. One can think for example of quick contracts such as let IsZero = from_predicate ((==) 0), which currently fails with a dynamic type error if we try to do (fun x => x) | IsZero. We'd need to do something like from_predicate (fun x => !(std.is_function x) && x == 0), which is annoying.

Instead, we only forbid comparison between two function-like values (functions, match expressions and custom contracts) and between two opaque foreign values. Comparing a function-like value to a number of a string is fine and just returns false.

The initial motivation for restricting function comparison is that it's never something that is meaningful to do, so it's most probably an error, and that it breaks reflexivity of ==, because f != f when f is a function. Arguably, the relaxed restriction proposed in this PR still fits the bill.

For context, this came up in #1975, where custom contracts is internally a pair of function (immediate, delayed), each of which can be null, but then we can't do control flow such as if immediate != null then .... While the %typeof% work-around does work, it doesn't feel right.

Relax the initial restriction on comparing functions, which is that
comparing a function to anything else error out. In practice comparing a
function to a value of some other type isn't a problem, and can be
useful to allow patterns like `if x != null then ... else ...` without
having first to check if `x` is a function, because that would fail at
runtime.

Instead, we only forbid comparison between two function-like values
(functions, match expressions and custom contracts) and between two
opaque foreign values.
@yannham yannham requested a review from jneem June 26, 2024 18:05
@github-actions github-actions bot temporarily deployed to pull request June 26, 2024 18:07 Inactive
Copy link
Member

@jneem jneem left a comment

Choose a reason for hiding this comment

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

Maybe there could also be a test that comparing a function to a number doesn't fail?

core/src/eval/operation.rs Show resolved Hide resolved
Check the relaxed function comparison, which errors out when trying to
compare two functions, but can compare a function to a value of a
different type (by always returning false).

Tests that functions can't be compared were already there, but this
commit adds tests that function can be compared to other values.
@github-actions github-actions bot temporarily deployed to pull request July 1, 2024 08:24 Inactive
@yannham
Copy link
Member Author

yannham commented Jul 1, 2024

Maybe there could also be a test that comparing a function to a number doesn't fail?

Fair! Added in the latest commits.

@yannham yannham added this pull request to the merge queue Jul 1, 2024
Merged via the queue into master with commit 89639ca Jul 1, 2024
5 checks passed
@yannham yannham deleted the feat/extend-function-equality branch July 1, 2024 11:36
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.

2 participants