(*TODO: Move to ev_semantics*)
Definition in_free_list (b : block) ofs xs :=
exists x, List.In x xs /\
let '(b', lo, hi) := x in
b = b' /\
(lo <= ofs < hi)%Z.
Fixpoint in_free_list_trace (b : block) ofs es :=
match es with
| event_semantics.Free l :: es =>
in_free_list b ofs l \/ in_free_list_trace b ofs es
| _ :: es =>
in_free_list_trace b ofs es
| [:: ] =>
False
end.

(* Specifications: *)
(** If (b, ofs) is in the list of freed addresses then the permission was Freeable and became None or it was not allocated*)
Lemma ev_elim_free_1:
forall m ev m' b ofs,
ev_elim m ev m' ->
in_free_list_trace b ofs ev ->
(permission_at m b ofs Cur = Some Freeable \/
~ Mem.valid_block m b) /\
permission_at m' b ofs Cur = None /\
Mem.valid_block m' b /\
exists e, List.In e ev /\
match e with
| event_semantics.Free _ => True
| _ => False
end.
(** If (b, ofs) is not in the list of freed locations and b is a valid block then the permissions remains the same (cannot be freed or allocated)*)
Lemma ev_elim_free_2:
forall m ev m' b ofs,
ev_elim m ev m' ->
~ in_free_list_trace b ofs ev ->
Mem.perm_order'' (permission_at m' b ofs Cur) (permission_at m b ofs Cur).
Lemma free_list_cases:
forall l m m' b ofs
(Hfree: Mem.free_list m l = Some m'),
(permission_at m b ofs Cur = Some Freeable /\
permission_at m' b ofs Cur = None) \/
(permission_at m b ofs Cur = permission_at m' b ofs Cur).
Lemma elim_perm_valid_block:
forall m T m' b ofs ofs' bytes
(Hintv: Intv.In ofs' (ofs, (ofs + Z.of_nat (length bytes))%Z))
(Helim: ev_elim m T m')
(Hvalid: Mem.valid_block m b),
(permission_at m b ofs' Cur = Some Freeable /\ permission_at m' b ofs' Cur = None) \/
((List.In (event_semantics.Write b ofs bytes) T ->
Mem.perm_order'' (permission_at m b ofs' Cur) (Some Writable) /\
Mem.perm_order'' (permission_at m' b ofs' Cur) (Some Writable)) /\
(forall n,
List.In (event_semantics.Read b ofs n bytes) T ->
Mem.perm_order'' (permission_at m b ofs' Cur) (Some Readable) /\
Mem.perm_order'' (permission_at m' b ofs' Cur) (Some Readable))).