Problem: linear_search

Your task is to implement the Linear Search algorithm to find the lower bound index and verify its correctness in Lean. Given a sequence of integers (not necessarily sorted, though typically used with sorting in mind for lower_bound semantics, the spec here just asks for the partition point if it were sorted or just the first element >= target), find the first index k such that seq[k] >= target. Returns seq.size if no such element is found. Postconditions: (1) result <= seq.size. (2) All elements before 'result' are < target. (3) All elements from 'result' onwards (if sorted) or just the element at 'result' matches the condition? Wait, the Dafny spec says "forall i < result ==> s[i] < target" and "forall i >= result ==> s[i] >= target". This implies the array MUST be sorted for this spec to hold for ALL i >= result. However, the Dafny spec provided does NOT explicitly require `is_sorted` in the precondition for `linear_search` in the file view (wait, let me check the file content again... checked: explicitly requires `is_sorted(s)`). So it is Lower Bound on a sorted array using Linear Search. Implementation should be O(N).


import Mathlib

-- Precondition definitions
@[reducible, simp]
def linear_search_lower_bound_precond (seq : Array Int) (target : Int) : Prop :=
  -- !benchmark @start precond
  seq.size ≤ 0x7FFFFFFF ∧
  (∀ i j : Nat, i < j ∧ j < seq.size → seq.getD i 0 ≤ seq.getD j 0)
  -- !benchmark @end precond

-- !benchmark @start auxcode
-- !benchmark @end auxcode

-- Main function definitions
def linear_search_lower_bound (seq : Array Int) (target : Int) (h_precond : linear_search_lower_bound_precond seq target) : Nat :=
  -- !benchmark @start code
  sorry
  -- !benchmark @end code

-- Postcondition definitions
@[reducible, simp]
def linear_search_lower_bound_postcond (seq : Array Int) (target : Int) (result : Nat) (h_precond : linear_search_lower_bound_precond seq target) : Prop :=
  -- !benchmark @start postcond
  result ≤ seq.size ∧
  (∀ i : Nat, i < result → seq.getD i 0 < target) ∧
  (∀ i : Nat, result ≤ i ∧ i < seq.size → seq.getD i 0 ≥ target)
  -- !benchmark @end postcond

-- !benchmark @start lemma
-- !benchmark @end lemma

-- Proof content
theorem linear_search_lower_bound_postcond_satisfied (seq : Array Int) (target : Int) (h_precond : linear_search_lower_bound_precond seq target) :
    linear_search_lower_bound_postcond seq target (linear_search_lower_bound seq target h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof