Section inner.
Variable client : clientId.
Variables id : nat.
Variable out : output.
Proof using.
unfold in_output_trace.
intros.
destruct (find (fun p => match snd p with
| inr l => match find (is_client_response client id out) l with
| Some x => true
| None => false
end
| _ => false
end) tr) eqn:?.
- find_apply_lem_hyp find_some. break_and.
repeat break_match; try discriminate.
find_apply_lem_hyp find_some. break_and.
unfold is_client_response, in_output_list in *.
break_match; try discriminate. break_if; try discriminate. do_bool. break_and. do_bool. subst.
left. exists l, (fst p). clean.
break_and. do_bool.
break_if; try congruence. subst. intuition.
find_reverse_rewrite.
rewrite <- surjective_pairing.
auto.
- right. intro. break_exists. break_and.
eapply find_none in Heqo; eauto.
simpl in *. break_match; try discriminate.
unfold in_output_list in *. break_exists.
find_eapply_lem_hyp find_none; eauto.
simpl in *. find_apply_lem_hyp Bool.andb_false_elim.
break_if; repeat (intuition; do_bool).
break_if; congruence.
Qed.
Proof using.
intros. unfold in_output_trace in *.
break_exists_exists.
intuition. do_in_app; intuition.
exfalso. eauto.
Qed.
Proof using.
intros.
unfold in_output_list in *.
break_exists; do_in_app; intuition eauto.
Qed.
Proof using.
intuition.
Qed.
Proof using.
intros. unfold doLeader, advanceCommitIndex in *.
repeat break_match; find_inversion; intuition eauto using key_in_output_list_empty.
Qed.
Proof using.
intros. unfold handleInput, handleTimeout, handleClientRequest, tryToBecomeLeader in *.
repeat break_match; find_inversion; intuition eauto using in_output_list_empty;
unfold in_output_list in *; break_exists; simpl in *; intuition; congruence.
Qed.
Proof using.
induction l; intros; simpl in *; intuition; eauto.
repeat break_match; simpl in *; eauto;
repeat find_insterU; break_exists; eexists; f_equal; eauto.
Qed.
Proof using.
eauto using deduplicate_log'_app.
Qed.
Proof using.
unfold in_output_trace.
simpl. intros. break_exists. intuition.
Qed.
Proof using.
unfold in_output_trace.
intuition.
break_exists. simpl in *. intuition.
find_inversion. auto.
Qed.
Proof using.
unfold in_output_list.
intuition.
Qed.
Proof using.
unfold in_output_trace.
intuition. break_exists_exists. simpl in *. intuition.
find_inversion.
Qed.
Proof using.
unfold in_output_list. simpl. intuition. discriminate.
Qed.
Proof using.
unfold handleInput, handleTimeout, handleInput, tryToBecomeLeader, handleClientRequest.
intuition.
repeat break_match; repeat find_inversion; eauto using in_output_trace_not_nil.
- exfalso. eapply in_output_list_not_leader_singleton; eauto.
- exfalso. eapply in_output_list_not_leader_singleton; eauto.
Qed.
Proof using.
unfold in_output_list.
simpl. intuition.
find_inversion. auto.
Qed.
Proof using.
induction l; simpl; intros; repeat break_match.
- discriminate.
- find_inversion. auto.
- intuition.
Qed.
Proof using.
unfold getLastId.
eauto using assoc_Some_In.
Qed.
Proof using.
induction xs; intros; simpl; auto using f_equal.
Qed.
Proof using.
induction es; simpl; intuition; repeat break_match; repeat find_inversion; do_bool.
- lia.
- auto.
- discriminate.
- f_equal. destruct (clientId_eq_dec (eClient a) (eClient e)).
+ repeat find_rewrite. find_injection.
eapply IHes with (n := eId a); auto with *.
now rewrite get_set_same.
+ eapply IHes; eauto.
rewrite get_set_diff by auto. auto.
- eauto.
- f_equal. destruct (clientId_eq_dec (eClient a) (eClient e)).
+ repeat find_rewrite. discriminate.
+ eapply IHes; eauto.
rewrite get_set_diff by auto. auto.
Qed.
Proof using.
induction es; simpl; intuition; repeat break_match; eauto using f_equal;
repeat find_reverse_rewrite.
- f_equal. subst. eapply deduplicate_log'_snoc_drop_keys; eauto.
now rewrite get_set_same.
- subst. do_bool. eapply deduplicate_log'_snoc_drop_keys; eauto; auto with *.
- f_equal. subst. eapply deduplicate_log'_snoc_drop_keys; eauto.
now rewrite get_set_same.
Qed.
Proof using.
intros. eapply deduplicate_log'_snoc_drop_es; eauto.
Qed.
Proof using.
induction es; intros; simpl in *; intuition.
- break_match; simpl in *; auto.
break_if; simpl in *; auto.
do_bool.
find_insterU. conclude_using eauto. lia.
- repeat break_match; simpl in *; auto.
+ f_equal. eapply IHes; eauto.
intros. do_bool.
destruct (clientId_eq_dec (eClient a) (eClient e)).
* repeat find_rewrite.
find_rewrite_lem get_set_same. find_injection.
find_insterU. conclude_using eauto. intuition.
* find_erewrite_lem get_set_diff.
eauto.
+ f_equal. eapply IHes; eauto.
intros. do_bool.
destruct (clientId_eq_dec (eClient a) (eClient e)).
* repeat find_rewrite.
find_rewrite_lem get_set_same. find_injection.
match goal with
| H : forall _, ?x = _ \/ _ -> _ |- _ =>
specialize (H x)
end.
intuition.
* find_erewrite_lem get_set_diff.
eauto.
Qed.
Proof using.
intros.
eapply deduplicate_log'_snoc_split; eauto.
intros. simpl in *. congruence.
Qed.
Proof using.
unfold execute_log.
intros.
rewrite execute_log'_app. auto.
Qed.
Proof using.
unfold applyEntry.
intros.
repeat break_match; repeat find_inversion. simpl.
rewrite execute_log_app. simpl. repeat break_let.
simpl in *. congruence.
Qed.
Proof using.
intros.
destruct (find (fun e' => andb (if clientId_eq_dec (eClient e') (eClient e) then true else false)
(eId e <=? eId e')) es) eqn:?.
- left. find_apply_lem_hyp find_some.
break_if; repeat (break_and; do_bool); try congruence.
intuition eauto using deduplicate_log_snoc_drop.
- right.
match goal with
| |- _ /\ ?P =>
assert P; [|intuition; eauto using deduplicate_log_snoc_split]
end.
intros.
find_eapply_lem_hyp find_none; eauto.
simpl in *. break_if; repeat (do_bool; intuition); try congruence.
Qed.
Proof using.
intros; induction l; simpl in *; intuition.
- subst. break_if; congruence.
- break_match. subst. break_if; try congruence.
auto.
Qed.
Proof using.
intros.
unfold getLastId in *.
eauto using assoc_None.
Qed.
Proof using.
unfold output_correct.
intros.
break_exists.
intuition.
pose proof (deduplicate_log_app xs ys). break_exists.
repeat find_rewrite.
rewrite app_ass in *. simpl in *.
eexists. eexists. eexists. eexists. eexists.
intuition eauto.
Qed.
Proof using.
unfold applyEntry.
intros.
repeat break_match; repeat find_inversion.
simpl in *. intuition.
subst.
unfold output_correct.
rewrite deduplicate_log_snoc_split by auto.
destruct (execute_log (deduplicate_log es)) eqn:?.
eexists. eexists. exists []. eexists. eexists. intuition eauto.
- rewrite execute_log_app. repeat find_rewrite. simpl in *. find_rewrite. eauto.
- rewrite rev_app_distr. simpl. auto.
Qed.
Proof using.
unfold cacheApplyEntry.
intros.
repeat break_match; repeat find_inversion; simpl in *; intuition.
- do_bool. subst. eauto using output_correct_monotonic, getLastId_Some_In.
- eapply applyEntry_output_correct; eauto.
do_bool. assert (n < eId e) by auto with *.
intros. assert (eId e' <= n) by eauto. lia.
- eapply applyEntry_output_correct; eauto.
intros.
find_apply_hyp_hyp.
break_exists. break_and.
repeat find_rewrite. discriminate.
Qed.
Proof using.
intros.
unfold cacheApplyEntry in *.
repeat break_match; repeat find_inversion.
- find_apply_hyp_hyp. break_exists. break_and.
erewrite deduplicate_log_snoc_drop; eauto.
do_bool. lia.
- find_apply_hyp_hyp. break_exists. break_and.
erewrite deduplicate_log_snoc_drop; eauto.
do_bool. lia.
- find_copy_apply_hyp_hyp. break_exists. break_and.
pose proof (deduplicate_log_cases es e).
intuition; break_exists; intuition; repeat find_rewrite.
+ do_bool. assert (eId x0 <= n) by eauto. lia.
+ eapply applyEntry_stateMachine_correct; eauto.
- pose proof (deduplicate_log_cases es e).
intuition; break_exists; intuition; repeat find_rewrite.
+ eapply_prop_hyp In In. break_exists. break_and.
repeat find_rewrite. discriminate.
+ eapply applyEntry_stateMachine_correct; eauto.
Qed.
Proof using.
unfold deduplicate_log.
intros. eauto using deduplicate_log'_In_if.
Qed.
Proof using.
unfold applyEntry.
intros.
repeat break_match; repeat find_inversion. intuition.
Qed.
Proof using.
unfold cacheApplyEntry.
intros.
repeat break_match_hyp; repeat find_inversion; do_bool; intuition eauto with *;
right; (split; [solve [apply applyEntry_clientCache; auto]|]); auto.
right. do_bool. eexists. eexists. intuition eauto. lia.
Qed.
Proof using.
unfold getLastId.
intros.
congruence.
Qed.
Proof using.
intros.
unfold cacheApplyEntry in *.
repeat break_match; try find_inversion; simpl in *; repeat find_rewrite; eauto.
- do_bool.
unfold applyEntry in *.
repeat break_match; find_inversion.
unfold getLastId in *. simpl in *.
destruct (clientId_eq_dec (eClient e) c); subst.
+ rewrite get_set_same. find_rewrite.
find_inversion. eauto.
+ rewrite get_set_diff in *; auto.
repeat find_rewrite. eauto.
- unfold applyEntry in *.
repeat break_match; find_inversion.
unfold getLastId in *. simpl in *.
destruct (clientId_eq_dec (eClient e) c); subst.
+ repeat find_rewrite.  congruence.
+ rewrite get_set_diff in *; auto.
repeat find_rewrite. eauto.
Qed.
Proof using.
intros.
eapply cacheAppliedEntry_clientCache_preserved in H; eauto.
break_exists. intuition. repeat find_rewrite. find_inversion. auto.
Qed.

Lemma applyEntries_output_correct :
forall l c i o h st os st' es,
applyEntries h st l = (os, st') ->
in_output_list c i o os ->
(stateMachine st = snd (execute_log (deduplicate_log es))) ->
(forall c i o,
getLastId st c = Some (i, o) ->
output_correct c i o es) ->
(forall c i o e',
getLastId st c = Some (i, o) ->
In e' es -> eClient e' = c -> eId e' <= i) ->
(forall e',
In e' es ->
exists i o,
getLastId st (eClient e') = Some (i, o) /\ eId e' <= i) ->
output_correct c i o (es ++ l).