Problem: bst_zig

Your task is to implement a Binary Search Tree (BST) and verify that it correctly implements a mathematical Set in Lean. The tree must maintain the BST Invariant. Focus on the Zig operation (single right rotation). Zig: Prove that (1) the mathematical set of values (view) remains exactly the same, (2) the BST invariant is preserved, and (3) structural correctness: the original left child becomes the new root, and the original root becomes the new right child.


import Mathlib

inductive BinarySearchTree : Type
| Empty : BinarySearchTree
| Node : Int → BinarySearchTree → BinarySearchTree → BinarySearchTree
deriving Inhabited

def view (t : BinarySearchTree) : Set Int :=
  match t with
  | BinarySearchTree.Empty => ∅
  | BinarySearchTree.Node v l r => view l ∪ view r ∪ {v}

def is_bst (t : BinarySearchTree) : Prop :=
  match t with
  | BinarySearchTree.Empty => True
  | BinarySearchTree.Node v l r =>
    (∀ x ∈ view l, x < v) ∧
    is_bst l ∧
    (∀ x ∈ view r, x > v) ∧
    is_bst r

def get_left (t : BinarySearchTree) : Option BinarySearchTree :=
  match t with
  | BinarySearchTree.Node _ l _ => some l
  | _ => none

def get_right (t : BinarySearchTree) : Option BinarySearchTree :=
  match t with
  | BinarySearchTree.Node _ _ r => some r
  | _ => none

def get_val (t : BinarySearchTree) : Option Int :=
  match t with
  | BinarySearchTree.Node v _ _ => some v
  | _ => none

def BinarySearchTree.is_node (t : BinarySearchTree) : Prop :=
  match t with
  | BinarySearchTree.Node _ _ _ => True
  | _ => False

-- Precondition definitions
@[reducible, simp]
def zig_precond (t : BinarySearchTree) : Prop :=
  -- !benchmark @start precond
  is_bst t ∧ 
  BinarySearchTree.is_node t ∧ 
  (match get_left t with | some l => BinarySearchTree.is_node l | none => False)
  -- !benchmark @end precond

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

-- Main function definition
def zig (t : BinarySearchTree) 
    (h_precond : zig_precond t) : BinarySearchTree :=
  -- !benchmark @start code
  sorry
  -- !benchmark @end code

-- Postcondition definitions
@[reducible, simp]
def zig_postcond (t : BinarySearchTree) (result : BinarySearchTree)
    (_ : zig_precond t) : Prop :=
  -- !benchmark @start postcond
  is_bst result ∧
  view result = view t ∧
  -- Structural properties
  get_val result = get_val (get_left t).get! ∧
  BinarySearchTree.is_node (get_right result).get! ∧
  get_val (get_right result).get! = get_val t
  -- !benchmark @end postcond

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

-- Proof content
theorem zig_postcond_satisfied (t : BinarySearchTree)
    (h_precond : zig_precond t) :
    zig_postcond t (zig t h_precond) h_precond := by
  -- !benchmark @start proof
  sorry
  -- !benchmark @end proof