Section MapDefs.
(** We now define maps from ad to A. *)
Variable A : Type.
Inductive Map :=
| M0 : Map
| M1 : ad -> A -> Map
| M2 : Map -> Map -> Map.
(** The semantics of maps is given by the function [MapGet]. The semantics of a map [m] is a partial, finite function from [ad] to [A]: *)
Fixpoint MapGet (m:Map) : ad -> option A :=
match m with
| M0 => fun a:ad => None
| M1 x y => fun a:ad => if N.eqb x a then Some y else None
| M2 m1 m2 =>
fun a:ad =>
match a with
| N0 => MapGet m1 N0
| Npos xH => MapGet m2 N0
| Npos (xO p) => MapGet m1 (Npos p)
| Npos (xI p) => MapGet m2 (Npos p)
end
end.
Definition newMap := M0.
Definition MapSingleton := M1.
Definition eqm (g g':ad -> option A) := forall a:ad, g a = g' a.
Fixpoint MapPut1 (a:ad) (y:A) (a':ad) (y':A) (p:positive) {struct p} :
Map :=
match p with
| xO p' =>
let m := MapPut1 (N.div2 a) y (N.div2 a') y' p' in
match Nbit0 a with
| false => M2 m M0
| true => M2 M0 m
end
| _ =>
match Nbit0 a with
| false => M2 (M1 (N.div2 a) y) (M1 (N.div2 a') y')
| true => M2 (M1 (N.div2 a') y') (M1 (N.div2 a) y)
end
end.

Lemma MapGet_if_commute :
forall (b:bool) (m m':Map) (a:ad),
MapGet (if b then m else m') a = (if b then MapGet m a else MapGet m' a).