Section LeaderCompleteness.
Context {orig_base_params : BaseParams}.
Context {one_node_params : OneNodeParams orig_base_params}.
Context {raft_params : RaftParams orig_base_params}.
Context {rri : raft_refinement_interface}.
Context {pwti : prefix_within_term_interface}.
Context {lltsi : leaderLogs_term_sanity_interface}.
Context {llpi : leaderLogs_preserved_interface}.
Context {eewci : every_entry_was_created_interface}.
Context {llvwli : leaderLogs_votesWithLog_interface}.
Context {aevwli : allEntries_votesWithLog_interface}.
Context {vwlsi : votesWithLog_sorted_interface}.
Context {taifoi : terms_and_indices_from_one_interface}.
Context {lllmi : leaderLogs_entries_match_interface}.
Fixpoint contradicting_leader_logs_on_leader l t e :=
match l with
| (t', log') :: l' =>
if (sumbool_and _ _ _ _
(lt_dec t t')
(sumbool_not _ _
(in_dec entry_eq_dec e log'))) then
(t', log') :: contradicting_leader_logs_on_leader l' t e
else
contradicting_leader_logs_on_leader l' t e
| [] => []
end.
Fixpoint contradicting_leader_logs net nodes t e : list (term * name * list entry) :=
match nodes with
| h :: nodes' => (map (fun l => (fst l, h, snd l))
(contradicting_leader_logs_on_leader (leaderLogs (fst (nwState net h))) t e))
++ contradicting_leader_logs net nodes' t e
| [] => []
end.
Definition minimal_contradicting_leader_log net t e :=
argmin (fun l => fst (fst l)) (contradicting_leader_logs net nodes t e).
Proof using.
induction l; intros; simpl in *; intuition; subst;
repeat break_match; subst; simpl in *; intuition eauto;
congruence.
Qed.
Proof using.
intros.
induction nodes; simpl in *; intuition; subst;
find_apply_lem_hyp app_eq_nil; intuition;
find_apply_lem_hyp map_eq_nil; auto.
Qed.
Proof using.
intros.
find_apply_lem_hyp argmin_None.
eapply contradicting_leader_logs_on_leader_empty; eauto.
eapply contradicting_leader_logs_empty; eauto.
apply all_fin_all.
Qed.
Proof using.
intros; induction l; simpl in *; intuition;
repeat break_match; subst; simpl in *; intuition;
find_inversion; intuition.
Qed.
Proof using.
induction nodes; intros; simpl in *; intuition.
do_in_app. intuition.
do_in_map. find_inversion. rewrite <- surjective_pairing. auto.
Qed.
Proof using.
induction ll; intros; simpl in *; intuition.
repeat break_match; simpl in *; intuition eauto.
Qed.
Proof using.
induction ll; intros; simpl in *; intuition.
repeat break_match; simpl in *; intuition eauto.
find_inversion. auto.
Qed.
Proof using.
induction ll; intros; simpl in *; intuition.
repeat break_match; simpl in *; intuition; repeat find_inversion; eauto.
Qed.
Proof using.
induction ll; intros; simpl in *; intuition;
repeat break_match; repeat find_inversion; simpl in *; intuition.
Qed.
Proof using.
induction nodes; intros; simpl in *; intuition.
apply in_or_app.
subst. left. apply in_map_iff. eexists. intuition eauto. simpl. auto.
Qed.
Proof using.
unfold minimal_contradicting_leader_log.
intros.
find_apply_lem_hyp argmin_elim. intuition.
- eauto using in_contradicting_leader_logs_on_leader_term_lt,
in_contradicting_leader_logs.
- eauto using in_contradicting_leader_logs,
in_contradicting_leader_logs_on_leader_in_leaderLog.
- eauto using in_contradicting_leader_logs,
in_contradicting_leader_logs_on_leader_not_in_log.
- destruct (le_lt_dec t'' t); auto.
destruct (le_lt_dec t' t''); auto.
destruct (in_dec entry_eq_dec e l'); auto.
find_eapply_lem_hyp contradicting_leader_logs_on_leader_complete; eauto.
find_eapply_lem_hyp contradicting_leader_logs_complete; [|solve [apply all_fin_all]].
find_apply_hyp_hyp. simpl in *. lia.
Qed.
Proof using.
destruct l; simpl; eauto.
Qed.

Lemma maxIndex_zero_or_entry :
forall l,
maxIndex l = 0 \/ exists e, In e l /\ eIndex e = maxIndex l.