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

Fix bug in search_sorted (we weren't testing for equality at the end) #1296

Closed
wants to merge 1 commit into from

Conversation

dougalm
Copy link
Collaborator

@dougalm dougalm commented May 20, 2023

No description provided.

@axch
Copy link
Collaborator

axch commented May 22, 2023

No good deed goes unpunished, eh? There are two reasonable search_sorted APIs: the one this PR implements and the one it replaces. To wit, search_sorted can be defined to return the index of an exact match of the input if it exists, as this PR does; or search_sorted can be defined to return the largest index whose value is less than or equal to the input, if it exists, as it used to. The former is better for testing membership in sets, whereas the latter is better for implementing piecewise-constant functions, such as the CDF of a categorical distribution. The test suite uses it that way in at least one place, hence the failure.

I think we should have both. The exact-match-only API is easily implemented in terms of the largest-smaller-index API (and now that we have the case-of-case optimization, doing it that way shouldn't even incur any performance cost, provided the optimization triggers). We might even try to change the type of largest-smaller-index API to return some type like Post n instead of Maybe n to imply that the Nothing case actually means "the query is smaller than the smallest element of the array" rather than "the query is not present in the array".

axch added a commit to axch/dex-lang that referenced this pull request Jun 23, 2023
Context: google-research#1296 (comment)

The more basic API, still named `search_sorted`, returns a `Post n`.
The idea is that the elements of `xs` are fence sections, and we find
the position between them (inclusive on either end) where `x` falls in
the ordering.

In terms of this, we now define `search_sorted_exact` (better name?),
which returns a `Maybe n`, which is the index of an element of `xs`
that equals `x` exactly, or `Nothing` if such does not exist.

Also reorder the prelude slightly to try to both maintain semantic
groupings and respect name resolution dependencies.
@axch
Copy link
Collaborator

axch commented Jun 23, 2023

Superseded by #1315.

@axch axch closed this Jun 23, 2023
axch added a commit that referenced this pull request Jun 23, 2023
Context: #1296 (comment)

The more basic API, still named `search_sorted`, returns a `Post n`.
The idea is that the elements of `xs` are fence sections, and we find
the position between them (inclusive on either end) where `x` falls in
the ordering.

In terms of this, we now define `search_sorted_exact` (better name?),
which returns a `Maybe n`, which is the index of an element of `xs`
that equals `x` exactly, or `Nothing` if such does not exist.

Also reorder the prelude slightly to try to both maintain semantic
groupings and respect name resolution dependencies.
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