Section TREES.
Variable V : Type.
Variable default: V.
Definition key := Z.
Inductive tree : Type :=
| E : tree
| T: tree -> key -> V -> tree -> tree.
Definition empty_tree : tree := E.
Fixpoint insert (x: key) (v: V) (s: tree) : tree :=
match s with
| E => T E x v E
| T a y v' b => if  x <? y then T (insert x v a) y v' b
else if y <? x then T a y v' (insert x v b)
else T a x v b
end.
Fixpoint lookup (x: key) (t : tree) : V :=
match t with
| E => default
| T tl k v tr => if x <? k then lookup x tl
else if k <? x then lookup x tr
else v
end.
Fixpoint pushdown_left (a: tree) (bc: tree) : tree :=
match bc with
| E => a
| T b y vy c => T (pushdown_left a b) y vy c
end.
Fixpoint delete (x: key) (s: tree) : tree :=
match s with
| E => E
| T a y v' b => if  x <? y then T (delete x a) y v' b
else if y <? x then T a y v' (delete x b)
else pushdown_left a b
end.
End TREES.
Arguments E {V}.
Arguments T {V} _ _ _ _.
Arguments insert {V} x v s.
Arguments lookup {V} default x t.
Arguments pushdown_left {V} a bc.
Arguments delete {V} x s.
Fixpoint treebox_rep (t: tree val) (p: val) :=
EX q: val,
data_at Tsh (tptr t_struct_tree) q p *
match t with
| E => !!(q=nullval) && emp
| T l x v r => !! (Int.min_signed <= x <= Int.max_signed /\ tc_val (tptr Tvoid) v) &&
field_at Tsh t_struct_tree [StructField _key] (Vint (Int.repr x)) q *
field_at Tsh t_struct_tree [StructField _value] v q *
treebox_rep l (field_address t_struct_tree [StructField _left] q) *
treebox_rep r (field_address t_struct_tree [StructField _right] q)
end.
Definition tree_rep (t: tree val) (q: val) :=
match t with
| E => !!(q=nullval) && emp
| T l x v r => !! (Int.min_signed <= x <= Int.max_signed /\ tc_val (tptr Tvoid) v) &&
field_at Tsh t_struct_tree [StructField _key] (Vint (Int.repr x)) q *
field_at Tsh t_struct_tree [StructField _value] v q *
treebox_rep l (field_address t_struct_tree [StructField _left] q) *
treebox_rep r (field_address t_struct_tree [StructField _right] q)
end.

Lemma treebox_rep_spec: forall t p,
treebox_rep t p = EX q: val, data_at Tsh (tptr t_struct_tree) q p * tree_rep t q.