Definition dec_share_nonidentity (sh: Share.t) : {~identity sh}+{identity sh} :=
(Sumbool.sumbool_not _ _ (dec_share_identity sh)).
Definition perm_of_sh (sh: Share.t): option permission :=
if writable0_share_dec sh
then if eq_dec sh Share.top
then Some Freeable
else Some Writable
else if readable_share_dec sh
then Some Readable
else if eq_dec sh Share.bot
then None
else Some Nonempty.
Functional Scheme perm_of_sh_ind := Induction for perm_of_sh Sort Prop.
Definition contents_at (m: mem) (loc: address) : memval :=
ZMap.get (snd loc) (PMap.get (fst loc) (mem_contents m)).
Definition contents_cohere (m: mem) (phi: rmap) :=
forall rsh sh v loc pp, phi @ loc = YES rsh sh (VAL v) pp -> contents_at m loc = v /\ pp=NoneP.
Definition valshare (r: resource) : share :=
match r with
| YES sh rsh _ _ => Share.glb Share.Rsh sh
| _ => Share.bot
end.
Definition res_retain' (r: resource) : Share.t :=
match r with
| NO sh _ => sh
| YES sh _ _ _ => Share.glb Share.Lsh sh
| PURE _ _ => Share.top
end.
Definition perm_of_res (r: resource) :=
(*  perm_of_sh (res_retain' r) (valshare r). *)
match r with
| NO sh _ => if eq_dec sh Share.bot then None else Some Nonempty
| PURE _ _ => Some Nonempty
| YES sh rsh (VAL _) _ => perm_of_sh sh
| YES sh rsh _ _ => Some Nonempty
end.
(*To do a case analysis over perm_of_res, use: functional induction (perm_of_res_explicit r1) using perm_of_res_expl_ind We define the induction shceme bellow. *)
Definition perm_of_res_lock_explicit
(r : compcert_rmaps.RML.R.resource):=
match r with
| compcert_rmaps.RML.R.NO _ _ => None
| compcert_rmaps.RML.R.YES sh _ (compcert_rmaps.VAL _) _ => None
| compcert_rmaps.RML.R.YES sh _ (compcert_rmaps.LK _ _) _ =>
if writable0_share_dec (Share.glb Share.Rsh sh)
then if eq_dec (Share.glb Share.Rsh sh) Share.top then Some Freeable else Some Writable
else if readable_share_dec (Share.glb Share.Rsh sh) then Some Readable else
if eq_dec  (Share.glb Share.Rsh sh) Share.bot then None else Some Nonempty
| compcert_rmaps.RML.R.YES sh _ (compcert_rmaps.FUN _ _) _ => None
| compcert_rmaps.RML.R.PURE _ _ => None
end.
Functional Scheme perm_of_res_lock_expl_ind := Induction for perm_of_res_lock_explicit Sort Prop.
Definition perm_of_res' (r: resource) :=
(*  perm_of_sh (res_retain' r) (valshare r). *)
match r with
| NO sh _ => if eq_dec sh Share.bot then None else Some Nonempty
| PURE _ _ => Some Nonempty
| YES sh _ _ _ => perm_of_sh sh
end.
Definition perm_of_res_lock (r: resource) :=
(*  perm_of_sh (res_retain' r) (valshare r). *)
match r with
| YES sh rsh (LK _ _) _ => perm_of_sh (Share.glb Share.Rsh sh)
| _ => None
end.
(*To do a case analysis over perm_of_res_lock, use: functional induction (perm_of_res_lock_explicit r1) using perm_of_res_lock_expl_ind We define the induction shceme bellow. *)
Definition perm_of_res_explicit
(r : compcert_rmaps.RML.R.resource):=
match r with
| compcert_rmaps.RML.R.NO sh _ => if eq_dec sh Share.bot then None else Some Nonempty
| compcert_rmaps.RML.R.YES sh _ (compcert_rmaps.VAL _) _ =>
if writable0_share_dec sh
then if eq_dec sh Share.top then Some Freeable else Some Writable
else
if readable_share_dec sh
then Some Readable
else if eq_dec sh Share.bot then None else Some Nonempty
| compcert_rmaps.RML.R.YES sh _ (compcert_rmaps.LK _ _) _ => Some Nonempty
| compcert_rmaps.RML.R.YES sh _ (compcert_rmaps.FUN _ _) _ => Some Nonempty
| compcert_rmaps.RML.R.PURE _ _ => Some Nonempty
end.
Functional Scheme perm_of_res_expl_ind := Induction for perm_of_res_explicit Sort Prop.
(*Definition perm_of_res_lock (r: resource) := (*  perm_of_sh (res_retain' r) (valshare r). *)
match r with
| NO sh => if eq_dec sh Share.bot then None else Some Nonempty
| PURE _ _ => Some Nonempty
| YES rsh sh (LK _) _ => perm_of_sh rsh (pshare_sh sh)
| YES rsh sh (CT _) _ => perm_of_sh rsh (pshare_sh sh)
| YES rsh sh _ _ => Some Nonempty
end. *)

Lemma perm_of_sh_fullshare: perm_of_sh fullshare = Some Freeable.