Problem: longest_common_subsequence

Your task is to implement the Longest Common Subsequence (LCS) algorithm and verify its correctness in Lean. Given two strings (or sequences of characters), find the length of the longest subsequence present in both. Postconditions: Result equals the value defined by the standard recursive LCS specification.


import Mathlib

def lcs_spec (s t : List Char) : Nat :=
  match s, t with
  | [], _ => 0
  | _, [] => 0
  | x :: xs, y :: ys =>
    if x = y then 1 + lcs_spec xs ys
    else max (lcs_spec xs (y :: ys)) (lcs_spec (x :: xs) ys)

-- Precondition definitions
@[reducible, simp]
def solve_longest_common_subsequence_precond (s t : List Char) : Prop :=
  -- !benchmark @start precond
  s.length ≤ 3000 ∧ t.length ≤ 3000
  -- !benchmark @end precond

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

-- Main function definition
def solve_longest_common_subsequence (s t : List Char)
    (h_precond : solve_longest_common_subsequence_precond s t) : Nat :=
  -- !benchmark @start code
  sorry
  -- !benchmark @end code

-- Postcondition definitions
@[reducible, simp]
def solve_longest_common_subsequence_postcond (s t : List Char) (result : Nat)
    (_ : solve_longest_common_subsequence_precond s t) : Prop :=
  -- !benchmark @start postcond
  result = lcs_spec s t
  -- !benchmark @end postcond

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

-- Proof content
theorem solve_longest_common_subsequence_postcond_satisfied (s t : List Char)
    (h_precond : solve_longest_common_subsequence_precond s t) :
    solve_longest_common_subsequence_postcond s t (solve_longest_common_subsequence s t h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof