(** * Semantics annotated with Owens-style trace*)
Inductive mem_event :=
Write : forall (b : block) (ofs : Z) (bytes : list memval), mem_event
| Read : forall (b:block) (ofs n:Z) (bytes: list memval), mem_event
| Alloc: forall (b:block)(lo hi:Z), mem_event
(*| Lock: drf_event | Unlock: drf_event  -- these events are not generated by core steps*)
| Free: forall (l: list (block * Z * Z)), mem_event.
Fixpoint ev_elim (m:mem) (T: list mem_event) (m':mem):Prop :=
match T with
nil => m'=m
| (Read b ofs n bytes :: R) => Mem.loadbytes m b ofs n = Some bytes /\ ev_elim m R m'
| (Write b ofs bytes :: R) => exists m'', Mem.storebytes m b ofs bytes = Some m'' /\ ev_elim m'' R m'
| (Alloc b lo hi :: R) => exists m'', Mem.alloc m lo hi = (m'',b) /\ ev_elim m'' R m'
| (Free l :: R) => exists m'', Mem.free_list m l = Some m'' /\ ev_elim m'' R m'
end.
Definition pmax (popt qopt: option permission): option permission :=
match popt, qopt with
_, None => popt
| None, _ => qopt
| Some p, Some q => if Mem.perm_order_dec p q then Some p else Some q
end.
Fixpoint cur_perm (l: block * Z) (T: list mem_event): option permission :=
match T with
nil => None
| (mu :: R) =>
let popt := cur_perm l R in
match mu, l with
| (Read b ofs n bytes), (b',ofs') =>
pmax (if eq_block b b' && zle ofs ofs' && zlt ofs' (ofs+n)
then Some Readable else None) popt
| (Write b ofs bytes), (b',ofs') =>
pmax (if eq_block b b' && zle ofs ofs' && zlt ofs' (ofs+ Zlength bytes)
then Some Writable else None) popt
| (Alloc b lo hi), (b',ofs') =>  (*we don't add a constraint relating lo/hi/ofs*)
if eq_block b b' then None else popt
| (Free l), (b',ofs') =>
List.fold_right (fun tr qopt => match tr with (b,lo,hi) =>
if eq_block b b' && zle lo ofs' && zlt ofs' hi
then Some Freeable else qopt
end)
popt l
end
end.

Lemma ev_elim_app: forall T1 m1 m2 (EV1:ev_elim m1 T1 m2) T2 m3  (EV2: ev_elim m2 T2 m3), ev_elim m1 (T1++T2) m3.