(** * SearchTree: Binary Search Trees *)
(** Binary search trees are an efficient data structure for lookup tables, that is, mappings from keys to values. The [total_map] type from Maps.v is an _inefficient_ implementation: if you add N items to your total_map, then looking them up takes N comparisons in the worst case, and N/2 comparisons in the average case.  In contrast, if your [key] type is a total order -- that is, if it has a less-than comparison that's transitive and antisymmetric [ a<b <->  ~(b<a) ] -- then one can implement binary search trees (BSTs).   We will assume you know how BSTs work; you can learn this from: - Section 3.2 of  _Algorithms, Fourth Edition_, by Sedgewick and Wayne, Addison Wesley 2011;  or - Chapter 12 of _Introduction to Algorithms, 3rd Edition_, by Cormen, Leiserson, and Rivest, MIT Press 2009.  Our focus here is to _prove the correctness of an implementation_ of binary search trees. *)
(* ################################################################# *)
(** * Total and Partial Maps *)
(** Recall the [Maps] chapter of Volume 1 (Logical Foundations), describing functions from identifiers to some arbitrary type [A]. VFA's [Maps] module is almost exactly the same, except that it implements functions from [nat] to some arbitrary type [A]. *)
(* ################################################################# *)
(** * Sections *)
(** We will use Coq's [Section] feature to structure this development, so first a brief introduction to Sections.  We'll use the example of lookup tables implemented by lists. *)
Module SectionExample1.
Definition mymap (V: Type) := list (nat*V).
Definition empty (V: Type) : mymap V := nil.
Fixpoint lookup (V: Type) (default: V) (x: nat) (m: mymap V) : V :=
match m with
| (a,v)::al => if x =? a then v else lookup V default x al
| nil => default
end.

Theorem lookup_empty (V: Type) (default: V):
forall x, lookup V default x (empty V) = default.