Section CommonTheorems.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Proof using.
unfold uniqueIndices.
eauto using NoDup_map_elim.
Qed.
Proof using.
intros.
simpl in *. intuition;
find_apply_hyp_hyp; intuition.
Qed.
Proof using.
induction ys; intros; simpl in *.
- break_match; intuition.
- break_match; intuition.
subst. apply sorted_cons; eauto.
intros. eauto using subseq_In.
Qed.
Proof using.
induction l; intros.
- simpl in *. intuition.
- simpl in *. intuition.
+ subst. auto with *.
+ find_apply_hyp_hyp. lia.
Qed.
Proof using.
induction l; intros.
- simpl in *. intuition.
- simpl in *. intuition.
+ subst. auto with *.
+ find_apply_hyp_hyp. lia.
Qed.
Proof using.
intros. intro.
find_apply_lem_hyp maxIndex_is_max; auto.
subst. lia.
Qed.
Proof using.
destruct l; intros; simpl in *; eauto; congruence.
Qed.
Proof using.
induction l; intros; simpl; auto.
repeat break_match; intuition.
- find_inversion. eauto using subseq_refl.
- right. find_reverse_rewrite. auto.
Qed.
Proof using.
intros. eauto using removeAfterIndex_subseq, sorted_subseq.
Qed.
Proof using.
eauto using removeAfterIndex_subseq, subseq_In.
Qed.
Proof using.
induction l; intros; intro.
- intuition.
- simpl in *. break_match; try discriminate. intuition.
+ subst. rewrite <- beq_nat_refl in *. discriminate.
+ find_copy_apply_hyp_hyp. intuition. break_if; do_bool; eauto. lia.
Qed.
Proof using.
induction l; intros.
- discriminate.
- simpl in *. break_match.
+ find_inversion. auto.
+ break_if; eauto; discriminate.
Qed.
Proof using.
induction l; intros.
- discriminate.
- simpl in *. break_match.
+ find_inversion. apply beq_nat_true in Heqb. auto.
+ break_if; eauto; discriminate.
Qed.
Proof using.
eauto using subseq_NoDup, removeAfterIndex_subseq.
Qed.
Notation disjoint xs ys := (forall x, In x xs -> In x ys -> False).
Proof using.
induction xs; intros.
- intuition.
- simpl in *. break_if; simpl in *; intuition.
subst. do_bool. lia.
Qed.
Proof using.
induction xs; intros.
- simpl in *. intuition.
- simpl in *.
break_if; simpl in *; do_bool; intuition; subst; auto.
find_apply_hyp_hyp. lia.
Qed.
Proof using.
induction xs; intros.
- simpl in *. intuition.
- simpl in *.
break_match; simpl in *; intuition;
subst; do_bool;
match goal with
| e : entry, H : forall _, _ = _ \/ _ -> _ |- _ =>
specialize (H e)
end;
intuition.
+ eauto using removeAfterIndex_le_In.
+ find_apply_hyp_hyp. intuition.
match goal with
| _ : eIndex ?e <= ?li, _ : eIndex ?e > eIndex ?e' |- _ =>
assert (eIndex e' <= li) by lia
end.
eauto using removeAfterIndex_le_In.
Qed.
Proof using.
induction xs; intros.
- reflexivity.
- simpl.
find_copy_apply_hyp_hyp.
repeat (break_if; simpl in *; intuition); try discriminate.
do_bool. lia.
Qed.
Proof using.
induction xs; intros; simpl.
- auto.
- repeat (break_match; simpl); intuition; try discriminate.
+ eauto using subseq_refl.
+ do_bool. assert (j < i) by lia.
rewrite removeAfterIndex_le with (j := i) (i := j) at 1; auto; lia.
+ do_bool. assert (i < j) by lia.
rewrite removeAfterIndex_le with (i := i) (j := j) at 2; auto; lia.
Qed.
Proof using.
auto using subseq_subseq_eq, removeAfterIndex_2_subseq.
Qed.
Proof using.
intros.
pose proof Min.min_spec i j. intuition.
- find_rewrite. rewrite removeAfterIndex_le with (i := i) (j := j) at 2;
eauto using removeAfterIndex_comm; lia.
- find_rewrite.
rewrite <- removeAfterIndex_le with (i := j) (j := i);
auto; lia.
Qed.
Proof using.
induction xs; intros; simpl in *; intuition; break_match; try discriminate.
- subst. do_bool. congruence.
- do_bool. break_if; eauto.
do_bool. find_apply_hyp_hyp. intuition.
Qed.
Proof using.
intros.
eapply NoDup_map_elim with (f := eIndex); eauto using findAtIndex_in, removeAfterIndex_in.
apply findAtIndex_index in H0.
apply findAtIndex_index in H1.
congruence.
Qed.
Proof using.
unfold uniqueIndices.
induction ys; intros.
- simpl in *. break_match; intuition.
- simpl in *. break_match; intuition.
+ simpl. constructor.
+ subst. simpl in *. invc H0. constructor; auto.
intro. apply H3.
eapply subseq_In; eauto.
apply subseq_map. auto.
+ subst. invc H0. eauto.
Qed.
Proof using.
induction xs; intros.
- simpl. auto.
- simpl. repeat break_match; auto.
+ find_inversion. eauto.
+ congruence.
Qed.
Proof using.
eauto using subseq_In, subseq_findGtIndex.
Qed.
Proof using.
induction entries; intros.
- simpl in *. intuition.
- simpl in *. break_if; intuition.
+ subst. in_crush.
+ subst. do_bool. lia.
+ do_bool. find_apply_hyp_hyp. lia.
Qed.
Definition contiguous_range_exact_lo xs lo :=
(forall i,
lo < i <= maxIndex xs ->
exists e,
eIndex e = i /\
In e xs) /\
(forall e,
In e xs ->
lo < eIndex e).
Proof with eauto using subseq_uniqueIndices, removeAfterIndex_subseq.
intros...
Qed.
Proof using.
destruct xs; intros.
- simpl. lia.
- destruct ys; simpl in *.
+ match goal with
| [ H : forall _, _ = _ \/ _ -> _, a : entry |- _ ] =>
solve [ specialize (H a); intuition ]
end.
+ match goal with
| [ H : forall _, _ = _ \/ _ -> _ |- eIndex ?a <= _ ] =>
specialize (H a); intuition
end; subst; auto.
find_apply_hyp_hyp. lia.
Qed.
Proof using.
destruct xs; intros.
- simpl in *. lia.
- simpl in *. eauto.
Qed.
Proof using.
induction l; intuition.
Qed.
Proof using.
intros.
apply maxIndex_subset; eauto using removeAfterIndex_sorted.
intros. eauto using removeAfterIndex_in.
Qed.
Proof using.
induction l; intros; simpl in *; intuition.
- subst. break_if; do_bool; try lia.
reflexivity.
- break_if; simpl in *.
+ do_bool.
match goal with
| H : forall _, In _ _ -> _ |- _ =>
specialize (H2 e)
end. intuition. lia.
+ eauto.
Qed.
Proof using.
intros.
destruct es; simpl in *.
- lia.
- congruence.
Qed.
Proof using one_node_params.
intros new current prev e Hsorted Huniq Hinv. intros. red. intros.
intuition.
- destruct (le_lt_dec i prev).
+ unfold contiguous_range_exact_lo in *. intuition.
match goal with
| H: forall _, _ < _ <= _ current -> _, H' : In _ current |- _ =>
specialize (H i); apply maxIndex_is_max in H'; auto; forward H; intuition
end.
break_exists. exists x. intuition.
apply in_or_app. right. subst.
eapply removeAfterIndex_le_In; eauto.
+ pose proof maxIndex_app new (removeAfterIndex current prev). intuition.
* find_rewrite.
unfold contiguous_range_exact_lo in *. intuition.
match goal with
| H: forall _, _ < _ <= _ new -> _ |- _ =>
specialize (H i); auto; forward H; intuition
end. break_exists. exists x. intuition.
* subst. simpl in *. clean.
exfalso.
pose proof maxIndex_removeAfterIndex current (eIndex e) e.
intuition.
- unfold contiguous_range_exact_lo in *.
do_in_app. intuition.
+ firstorder auto with zarith.
+ firstorder using removeAfterIndex_in.
Qed.
Proof using.
intros.
exists x. intuition.
Qed.
Proof using.
induction entries; intros; simpl in *; intuition.
- break_if; simpl in *; intuition; right; eapply IHentries; eauto.
- break_if;
simpl in *; intuition.
+ do_bool. subst. lia.
+ simpl in *; intuition; eapply IHentries; eauto.
Qed.
Proof using.
intros entries x Hsorted; intros. specialize (H i).
conclude H ltac:(lia).
break_exists. exists x0. intuition.
apply findGtIndex_sufficient; auto; lia.
Qed.
Proof using.
intros. destruct entries; simpl; auto.
break_if; simpl; intuition.
Qed.
Proof using.
intros.
pose proof findAtIndex_in _ _ _ H.
pose proof findAtIndex_index _ _ _ H.
eapply uniqueIndices_elim_eq; eauto.
Qed.
Definition entries_match' entries entries' :=
forall e e' e'',
eIndex e = eIndex e' ->
eTerm e = eTerm e' ->
In e entries ->
In e' entries' ->
eIndex e'' <= eIndex e ->
(In e'' entries -> In e'' entries').
Proof using.
unfold entries_match, entries_match'.
intros. intuition.
- eapply H; eauto.
- eapply (H e' e); eauto with *.
Qed.
Definition contiguous
(prevLogIndex : logIndex)
(prevLogTerm : term)
(leaderLog entries : list entry) : Prop :=
(prevLogIndex = 0 \/
exists e, findAtIndex leaderLog prevLogIndex = Some e /\
eTerm e = prevLogTerm) /\
(forall e,
In e leaderLog ->
eIndex e > prevLogIndex ->
eIndex e <= maxIndex entries ->
In e entries) /\
forall e e',
eIndex e = eIndex e' ->
eTerm e = eTerm e' ->
In e entries ->
In e' leaderLog ->
e = e'.
Proof using.
unfold entries_match. intuition.
Qed.
Proof using.
intros.
unfold entries_match in *.
intros. intuition.
- apply H with (e:=e')(e':=e); auto.
repeat find_rewrite. auto.
- apply H with (e:=e')(e':=e); auto.
repeat find_rewrite. auto.
Qed.
Proof using.
unfold advanceCurrentTerm. intros.
break_if; auto.
Qed.
Proof using.
unfold tryToBecomeLeader.
intros. find_inversion. auto.
Qed.
Proof using.
unfold handleRequestVote.
intros.
repeat (break_match; try discriminate; repeat (find_inversion; simpl in *));
auto using advanceCurrentTerm_same_log.
Qed.
Proof using.
unfold handleRequestVoteReply.
intros. repeat break_match; simpl; auto using advanceCurrentTerm_same_log.
Qed.
Proof using.
unfold advanceCurrentTerm. intros.
break_if; auto.
Qed.
Proof using.
intros. unfold handleTimeout, tryToBecomeLeader in *.
break_match; find_inversion; subst; auto.
Qed.
Proof using.
intros. unfold handleClientRequest in *.
break_match; find_inversion; subst; auto.
Qed.
Proof using.
unfold tryToBecomeLeader.
intros. find_inversion. auto.
Qed.
Proof using.
unfold handleRequestVote.
intros.
repeat (break_match; try discriminate; repeat (find_inversion; simpl in *));
auto using advanceCurrentTerm_same_lastApplied.
Qed.
Proof using.
unfold handleRequestVoteReply.
intros. repeat break_match; simpl; auto using advanceCurrentTerm_same_lastApplied.
Qed.
Proof using.
eauto using findAtIndex_in, findAtIndex_index.
Qed.
Proof using.
intros. split.
- lia.
- etransitivity; eauto. apply maxIndex_is_max; auto.
Qed.
Proof using.
intros.
assert (x = x').
- eapply uniqueIndices_elim_eq; eauto.
- subst. auto.
Qed.
Proof using.
induction l; intros.
- simpl in *. intuition.
- simpl in *. intuition; break_if; subst; do_bool.
+ auto.
+ congruence.
+ f_equal. eauto using uniqueIndices_elim_eq with *.
+ break_if; eauto.
* do_bool. find_apply_hyp_hyp. lia.
* eapply IHl; auto. unfold uniqueIndices in *.
simpl in *. solve_by_inversion.
Qed.

Theorem sorted_uniqueIndices :
forall l,
sorted l -> uniqueIndices l.