Section inflate.
Variables (m: mem) (phi: rmap).
Definition inflate_initial_mem' (w: rmap) (loc: address) :=
match access_at m loc Cur with
| Some Freeable => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP
| Some Writable => YES Ews (writable_readable writable_Ews) (VAL (contents_at m loc)) NoneP
| Some Readable => YES Ers readable_Ers (VAL (contents_at m loc)) NoneP
| Some Nonempty =>
match w @ loc with PURE _ _ => w @ loc | _ => NO _ nonreadable_extern_retainer end
| None =>  NO Share.bot bot_unreadable
end.
Definition inflate_initial_mem (w: rmap): rmap :=
proj1_sig (make_rmap (inflate_initial_mem' w) (ghost_of w) _
(inflate_initial_mem'_fmap w) (ghost_of_approx w)).
Definition all_VALs (phi: rmap) :=
forall l, match phi @ l with
| YES _ _ k _ => isVAL k
| _ => True
end.
(* FIXME Build an rmap that's identical to phi except where m has allocated. *)
Definition inflate_alloc: rmap.
refine (proj1_sig (remake_rmap (fun loc =>
fmap_option (res_option (phi @ loc))
(* phi = NO *)
(fmap_option (access_at m loc Cur)
(NO Share.bot bot_unreadable)
(fun p =>
match p with
| Freeable => YES Share.top readable_share_top (VAL (contents_at m loc)) NoneP
| _ => NO Share.Lsh Lsh_nonreadable
end))
(* phi = YES *)
(fun _ => phi @ loc)) (ghost_of phi) (level phi) _ (ghost_of_approx phi))).
(* Build an [rmap] that's identical to [phi] except where [m] has stored. *)
Definition inflate_store: rmap. refine (
proj1_sig (make_rmap (fun loc =>
match phi @ loc with
| YES sh rsh (VAL _) _ => YES sh rsh (VAL (contents_at m loc)) NoneP
| YES _ _ _ _ => resource_fmap (approx (level phi)) (approx (level phi)) (phi @ loc)
| _ => phi @ loc
end) (ghost_of phi) (level phi) _ (ghost_of_approx phi))).

Lemma range_inv: forall ofs ofs' ch,
~ ofs <= ofs' < ofs + size_chunk ch ->
ofs' < ofs \/ ofs' >= ofs + size_chunk ch.