(*+ Resource maps *)
(** In this file: - common results about lists - definition of resource maps, ignoring problems related to step indexing *)
(*+ List functions *)
Fixpoint nth {A} (l : list A) i :=
match l with
| nil => None
| x :: l =>
match i with
| O => Some x
| S i => nth l i
end
end.
Fixpoint nth_upd {A} (l : list A) i x :=
match l with
| nil => nil
| y :: l =>
match i with
| O => x :: l
| S i => y :: nth_upd l i x
end
end.
Ltac ifeq :=
match goal with
[ H : context [ eq_nat_dec ?x ?x ] |- _ ] => destruct (eq_nat_dec x x) as [_|]; [|congruence]
| [ |- context [ eq_nat_dec ?x ?x ] ] => destruct (eq_nat_dec x x) as [_|]; [|congruence]
| [ H : context [ eq_nat_dec ?x ?y ] |- _ ] => destruct (eq_nat_dec x y)
| [ |- context [ eq_nat_dec ?x ?y ] ] => destruct (eq_nat_dec x y)
end.
(*+ Resource maps *)
(** Here, we avoid solving the indirection equation by abstracting over the desired properties, not worrying about step indexing to add predicates in the "memory" (i.e. the resource maps).  Note that it is also possible to instantiate the variable [lock] with a deep embedding of the desired resource invariants without cheating. *)
Variable lock : Set.
Definition val := (nat + lock)%type.
Definition map := nat -> option val.
Variable interp_lock : lock -> (map -> Prop).
Definition emptymap : map := fun _ => None.
Definition eqmap (m m' : map) := forall x, m x = m' x.
Instance eqmap_equiv : Equivalence eqmap.
Definition upd {A} (m : nat -> A) x e :=
fun y => if eq_nat_dec x y then e else m y.
Definition updatemap (m : map) (x e : nat) : option map :=
match m x with Some _ => Some (upd m x (Some (inl e))) | None => None end.
Definition assertmap (m : map) (x : nat) : Prop :=
match m x with Some (inl (S _)) => True | _ => False end.
(*+ Join relation on resources and resource maps *)
Definition joincell (a b : option val) : option (option val) :=
match a, b with
| None, None => Some None
| Some a, None | None, Some a => Some (Some a)
| Some _, Some _ => None
end.
Definition join (m n o : map) : Prop := forall x, joincell (m x) (n x) = Some (o x).
Definition joins (m n : map) : Prop := exists o, join m n o.
Add Parametric Morphism : join with signature eqmap ==> eqmap ==> eqmap ==> iff as join_morphism.

Lemma join_eqmap {m n o o'}  : join m n o -> join m n o' -> eqmap o o'.