Problem: bst_search

Your task is to implement a Binary Search Tree and verify that it correctly implements a mathematical Set in Dafny. The tree must maintain the BST Invariant: for any node with value $v$, all values in the left subtree are less than $v$ and all values in the right subtree are greater than $v$. Here, you just need to focus on the search operation. Search: Prove that the method returns true if and only if the target value is a member of the mathematical set represented by the tree's view. Note that you don't need to consider the balancing and rotation of a BST, and you can implement the naive BST.

// Following is the block for necessary definitions
// <preamble>
datatype Tree = Empty | Node(val: int, left: Tree, right: Tree)

function view(tree: Tree): set<int>
    decreases tree
{
    match tree
    case Empty => {}
    case Node(val, left, right) => view(left) + view(right) + {val}
}

predicate is_bst(tree: Tree)
    decreases tree
{
    match tree
    case Empty => true
    case Node(val, left, right) =>
        (forall x :: x in view(left) ==> x < val) &&
        is_bst(left) &&
        (forall x :: x in view(right) ==> x > val) &&
        is_bst(right)
}
// </preamble>

// Following is the block for potential helper specifications
// <helpers>

// </helpers>

// Following is the block for proofs of lemmas
// <proofs>

// </proofs>

// Following is the block for the main specification
// <spec>
method search(tree: Tree, v: int) returns (res: bool)
    requires v >= 0 
    requires is_bst(tree)
    ensures res == (v in view(tree))
// </spec>
// <code>
{
    // TODO: Implement the bst search operation here.
}
// </code>