Section GreeterCoh.
(* All nodes are welcome to participate in the protocol *)
Definition nodes (d : dstatelet) : pred nid := mem fixed_nodes.
(* Defining coherence predicate for the greeter statelet *)
Definition hello := [:: 3; 1; 1; 0].
Definition counter := ptr_nat 1.
(* All messages have fixed shape *)
Definition soupCoh : Pred soup :=
[Pred s | valid s /\
forall m msg,
find m s = Some msg ->
let: from := from msg in
let: tag := tag (content msg) in
let: val := tms_cont (content msg) in
[/\ from \in fixed_nodes,
tag == 0 & behead val == hello]].
(* Coherence for the local state ensures that is has just one counter, which we will later use to count incoming messages  *)
Definition localCoh : Pred heap :=
[Pred h | exists n : nat, h = counter :-> n].
(* Overall coherence *)
Definition greeter_coh d : Prop :=
let: dl := dstate d in
let: ds := dsoup d in
[/\ soupCoh ds, dom dl =i fixed_nodes,
valid dl &
forall n, n \in fixed_nodes -> localCoh (getLocal n d)].
End GreetAux.
Definition greet_tag := 0.

(* We might need to put this statement into a separate definition *)
Lemma greet_step_coh this to d msg (pf : greet_safe this to d msg) b :
let: f := dstate d in
let: s := dsoup d  in
Some b = greet_step pf ->
let: f' := upd this b f in
let: tms := TMsg greet_tag msg in
let: s' := (post_msg s (Msg tms this to true)).1 in
coh (DStatelet f' s').