(************************************************************************)
(******       Auxiliary lemmas for establishing stability          ******)
(************************************************************************)

(* Specifications: *)
Lemma cohQ s : Coh W s -> (QueryProtocol.QCoh qnodes) (getSq s).
Lemma send_lq_case3 req_num data reqs resp to s
(N : this != to) (Qn : to \in qnodes) (H : core_state_to_data to (getLc' s to) data)
(L : local_indicator data (getLc' s this))
(M : msg_responded (getSq s) reqs resp req_num to data)
to' msg (n : heap) (C : Coh W s) (st : send_trans (Protocols.coh pq))
(H1 : st \In snd_trans pq)
(H2 : to \in qnodes)
(H4 : send_safe st to to' (getSq s) msg) :
all_hooks_fire query_hookz (plab pq) (t_snd st) s to msg to' -> Some n = send_step H4 ->
let: d := getStatelet (upd (plab pq)
(DStatelet (upd to n (dstate (getSq s)))
(dsoup (getSq s) \+ fresh (dsoup (getSq s)) \\->
Msg (TMsg (t_snd st) msg) to to' true)) s) lq in
msg_responded d reqs resp req_num to data.
Lemma send_lq_all_cases req_num data reqs resp to s
(N : this != to) (Qn : to \in qnodes) (H : core_state_to_data to (getLc' s to) data)
(L : local_indicator data (getLc' s this))
(M : [\/ msg_just_sent (getSq s) reqs resp req_num to, msg_received (getSq s) reqs resp req_num to
| msg_responded (getSq s) reqs resp req_num to data])
to' msg (n : heap) (C : Coh W s) (st : send_trans (Protocols.coh (getProtocol W (plab pq))))
(H1 : st \In get_st W (plab pq))
(H2 : to \in nodes (getProtocol W (plab pq)) (getSq s))
(H4 : send_safe st to to' (getSq s) msg) :
all_hooks_fire query_hookz (plab pq) (t_snd st) s to msg to' -> Some n = send_step H4 ->
let: d := getStatelet (upd (plab pq)
(DStatelet (upd to n (dstate (getSq s)))
(dsoup (getSq s) \+ fresh (dsoup (getSq s)) \\->
Msg (TMsg (t_snd st) msg) to to' true)) s) lq in
[\/ msg_just_sent d reqs resp req_num to,
msg_received  d reqs resp req_num to
| msg_responded d reqs resp req_num to data].
(*  A lemma covering send-cases in the lc-part of the world  *)
Lemma send_lc_all_cases req_num data reqs resp to s
(N : this != to) (Qn : to \in qnodes) (H : core_state_to_data to (getLc' s to) data)
(L : local_indicator data (getLc' s this))
(M : [\/ msg_just_sent (getSq s) reqs resp req_num to, msg_received (getSq s) reqs resp req_num to
| msg_responded (getSq s) reqs resp req_num to data])
to' msg (n : heap) (C : Coh W s) (st : send_trans (Protocols.coh (getProtocol W (plab pc))))
(H1 : st \In get_st W (plab pc))
(H2 : to \in nodes (getProtocol W (plab pc)) (getSq s))
(H4 : send_safe st to to' (getSq s) msg) :
all_hooks_fire query_hookz (plab pc) (t_snd st) s to msg to' -> Some n = send_step H4 ->
let: d := getStatelet (upd (plab pc)
(DStatelet (upd to n (dstate (getSq s)))
(dsoup (getSq s) \+ fresh (dsoup (getSq s)) \\->
Msg (TMsg (t_snd st) msg) to to' true)) s) lq in
[\/ msg_just_sent d reqs resp req_num to,
msg_received  d reqs resp req_num to
| msg_responded d reqs resp req_num to data].
Lemma recv_lq_case1 req_num reqs resp to s
(N : this != to) (Qn : to \in qnodes)
(M : msg_just_sent (getSq s) reqs resp req_num to)
i from msg (C : Coh W s) (C' : (coh pq) (getSq s))
(pf : to \in nodes pq (getSq s))
(rt : receive_trans (Protocols.coh pq)) :
rt \In rcv_trans pq -> tag msg = t_rcv rt ->
find i (dsoup (getSq s)) = Some (Msg msg from to true) ->
let: d := getStatelet (upd (plab pq)
(DStatelet (upd to (receive_step rt from msg C' pf) (dstate (getSq s)))
(consume_msg (dsoup (getSq s)) i)) s) lq in
msg_just_sent d reqs resp req_num to \/
msg_received d reqs resp req_num to.
Lemma recv_lq_case2 req_num reqs resp to s
(N : this != to) (Qn : to \in qnodes)
(M : msg_received (getSq s) reqs resp req_num to)
i from msg (C : Coh W s) (C' : (coh pq) (getSq s))
(pf : to \in nodes pq (getSq s))
(rt : receive_trans (Protocols.coh pq)) :
rt \In rcv_trans pq -> tag msg = t_rcv rt ->
find i (dsoup (getSq s)) = Some (Msg msg from to true) ->
let: d := getStatelet (upd (plab pq)
(DStatelet (upd to (receive_step rt from msg C' pf) (dstate (getSq s)))
(consume_msg (dsoup (getSq s)) i)) s) lq in
msg_received d reqs resp req_num to.
Lemma recv_lq_case3 req_num reqs resp to s data
(N : this != to) (Qn : to \in qnodes)
(M : msg_responded (getSq s) reqs resp req_num to data)
(H : core_state_to_data to (getLc' s to) data)
(L : local_indicator data (getLc' s this))
i from msg (C : Coh W s) (C' : (coh pq) (getSq s))
(pf : to \in nodes pq (getSq s))
(rt : receive_trans (Protocols.coh pq)) :
rt \In rcv_trans pq -> tag msg = t_rcv rt ->
find i (dsoup (getSq s)) = Some (Msg msg from to true) ->
let: d := getStatelet (upd (plab pq)
(DStatelet (upd to (receive_step rt from msg C' pf) (dstate (getSq s)))
(consume_msg (dsoup (getSq s)) i)) s) lq in
msg_responded d reqs resp req_num to data.