Skip to content

Conversation

@sorrachai
Copy link
Collaborator

We introduce an analysis of binary search on a sorted array. We model a sorted array as a function from Nat to a type equipped with a size and a sortedness property. This representation eliminates concerns about out-of-bounds indexing. With fewer proof obligations related to array indices, the analysis becomes significantly cleaner and more modular.

Copy link
Contributor

@Shreyas4991 Shreyas4991 left a comment

Choose a reason for hiding this comment

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

Basically all the definitions as well as a monadic version of binary search already exists docs. Why reinvent Finvecs which already have good API and notation support?

intentionally left unconstrained, eliminating out-of-bounds concerns.
This design substantially reduces index-related proof obligations,
leading to cleaner and more modular algorithmic analysis. -/
structure SortedArray (α : Type) [LinearOrder α] (n : ℕ) where
Copy link
Contributor

Choose a reason for hiding this comment

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

This is extraneous. Lean core and mathlib already provide a bunch of API for various containers and allow to convert to and from so-called Finvecs which are maps of the form Fin n -> \alpa

/-- Scoped notation for accessing elements of a `SortedArray`.
Writing `a[i]` abbreviates `SortedArray.get a i`, avoiding explicit projections
and improving readability in algorithm definitions such as `binarySearch`. -/
scoped notation a "[" i "]" => SortedArray.get a i
Copy link
Contributor

Choose a reason for hiding this comment

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

The notation is unnecessary. This notation works for any container type with an instance of GetElem docs

leading to cleaner and more modular algorithmic analysis. -/
structure SortedArray (α : Type) [LinearOrder α] (n : ℕ) where
/-- Total access function for the array.
The value at indices `i ≥ n` is arbitrary and irrelevant to correctness. -/
Copy link
Contributor

Choose a reason for hiding this comment

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

Additionally, Core already has a binary search on arrays

namespace Cslib.Algorithms.Lean.TimeM

set_option autoImplicit false
set_option tactic.hygienic false
Copy link
Collaborator

Choose a reason for hiding this comment

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

As addressed in your last PR, please do not unset tactic hygiene. It appears you have done so mostly to use inaccessible names after induction. The standard method here is to provide names to each branch using either with or cases.


-/

namespace Cslib.Algorithms.Lean.TimeM
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am again not sure about naming here. I would expect the TimeM namespace to be used only for the definition and utilities directly related to the monad, and this file to fall under a namespace corresponding to the file name. This is currently Cslib.Algorithms.Lean.Searching.BinarySearch, but if this is going to be nested and have multiple search procedures, maybe Cslib.Algorithms.Lean.Search.Binary is a bit more compact.

scoped notation a "[" i "]" => SortedArray.get a i


variable {α : Type} [LinearOrder α]
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is duplicated from above.

Comment on lines +77 to +78
if q = arr_a then return (some a)
else return none
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd use the same indenting for if/else as you did below for readability.

intentionally left unconstrained, eliminating out-of-bounds concerns.
This design substantially reduces index-related proof obligations,
leading to cleaner and more modular algorithmic analysis. -/
structure SortedArray (α : Type) [LinearOrder α] (n : ℕ) where
Copy link
Collaborator

@chenson2018 chenson2018 Dec 25, 2025

Choose a reason for hiding this comment

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

Before I review proofs carefully, we should discuss the design of this. I am going continue this discussion on Zulip.

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