(* PERSONAL MEM: Is the contents of the global memory, with the juice of a single thread and the Cur that corresponds to that juice.*)
Definition acc_coh:= fun m phi pr => @max_acc_coh_acc_coh m phi (max_coh pr).
Definition personal_mem {m phi} (pr : mem_cohere' m phi) : juicy_mem:=
mkJuicyMem
(@juicyRestrict phi m (acc_coh pr))
phi
(juicyRestrictContentCoh (acc_coh pr) (cont_coh pr))
(juicyRestrictAccCoh (acc_coh pr))
(juicyRestrictMaxCoh (acc_coh pr) (max_coh pr))
(juicyRestrictAllocCoh (acc_coh pr) (all_coh pr)).
Definition juicy_sem := (FSem.F _ _ JuicyFSem.t) _ the_sem.
(* Definition juicy_step := (FSem.step _ _ JuicyFSem.t) _ _ the_sem. *)
Program Definition first_phi (tp : thread_pool) : rmap := (@getThreadR _ _ _ 0%nat tp _).
Next Obligation.
unfold OrdinalPool.containsThread.
destruct num_threads.
simpl.
ssromega.
Defined.
Program Definition level_tp (tp : thread_pool) := level (first_phi tp).
Definition tp_level_is_above n tp :=
(forall i (cnti : containsThread tp i), le n (level (getThreadR cnti))) /\
(forall i R phi, lockRes(resources:=LockInvsAndResources) tp i = Some (R, Some phi) -> le n (level phi)).
Definition tp_level_is n tp :=
(forall i (cnti : containsThread tp i), level (getThreadR cnti) = n) /\
(forall i R phi, lockRes(resources:=LockInvsAndResources) tp i = Some (R, Some phi) -> level phi = n).
Definition cnt_from_ordinal tp : forall i : ordinal (pos.n (num_threads tp)), containsThread tp i.
intros [i pr]; apply pr. Defined.
Definition age_tp_to (k : nat) (tp : thread_pool) : thread_pool :=
match tp with
mk n pool maps lset =>
mk n pool
((age_to k) oo maps)
(AMap.map (fun '(R, r) => (R, option_map (age_to k) r)) lset)
end.

(* Specifications: *)
Lemma level_age_tp_to tp k : tp_level_is_above k tp -> tp_level_is k (age_tp_to k tp).
Lemma map_compose {A B C} (g : A -> B) (f : B -> C) l : map (f oo g) l = map f (map g l).
Lemma join_list_age_to k l Phi :
le k (level Phi) ->
join_list l Phi ->
join_list (map (age_to k) l) (age_to k Phi).
Lemma join_list'_age_to k l (Phi : option res) :
(match Phi with None => Logic.True | Some phi => le k (level phi) end) ->
join_list' l Phi ->
join_list' (map (fun '(R, r) => (R, option_map (age_to k) r)) l) (option_map (age_to k) Phi).
Lemma join_all_age_to k tp Phi :
le k (level Phi) ->
join_all tp Phi ->
join_all (age_tp_to k tp) (age_to k Phi).
Lemma perm_of_age rm age loc :
perm_of_res (age_to age rm @ loc) = perm_of_res (rm @ loc).
Lemma perm_of_age_lock rm age loc :
perm_of_res_lock (age_to age rm @ loc) = perm_of_res_lock (rm @ loc).
Lemma almost_empty_perm: forall rm,
res_predicates.almost_empty rm ->
forall loc, Mem.perm_order'' (Some Nonempty) (perm_of_res (rm @ loc)).
Lemma cnt_age {js i age} :
containsThread (age_tp_to age js) i ->
containsThread js i.
Lemma cnt_age' {js i age} :
containsThread js i ->
containsThread (age_tp_to age js) i.
Lemma age_getThreadCode:
forall i tp age cnt cnt',
@getThreadC _ _ _ i tp cnt = @getThreadC _ _ _ i (age_tp_to age tp) cnt'.