Proof using rmri rlmli si rri.
unfold lifted_maxIndex_sanity, maxIndex_lastApplied, maxIndex_commitIndex.
intros.
intuition; simpl in *; repeat find_higher_order_rewrite; update_destruct_simplify; auto.
- erewrite handleAppendEntries_lastApplied by eauto.
assert (sorted (log d)) by (eauto using lifted_handleAppendEntries_logs_sorted).
match goal with
| _ : handleAppendEntries ?h ?s ?t ?n ?pli ?plt ?es ?ci = (?s', ?m) |- _ =>
pose proof handleAppendEntries_log_detailed
h s t n pli plt es ci s' m
end.
intuition; repeat find_rewrite.
+ eauto.
+ subst.
destruct (le_lt_dec (lastApplied (snd (nwState net (pDst p))))
(maxIndex (log d))); auto.
exfalso.
assert (In p (nwPackets net)) by (find_rewrite; intuition).
assert (exists x, eIndex x = maxIndex (log d) /\ In x (log (snd (nwState net (pDst p))))).
{
eapply contiguous_range_exact_lo_elim_exists.
- eapply lifted_entries_contiguous_invariant. auto.
- split.
+ find_apply_lem_hyp maxIndex_non_empty. break_exists.  break_and.
repeat find_rewrite.
eapply contiguous_range_exact_lo_elim_lt.
* eapply lifted_entries_contiguous_nw_invariant; eauto.
* auto.
+ eapply le_trans; [|eauto]. simpl in *. omega.
}
break_exists. break_and.
eapply findAtIndex_None; [|eauto| |]; eauto.
apply msg_lifted_sorted_host; auto.
+ subst.
destruct (le_lt_dec (lastApplied (snd (nwState net (pDst p))))
(maxIndex (log d))); auto.
exfalso.
assert (In p (nwPackets net)) by (find_rewrite; intuition).
break_exists; intuition. find_apply_lem_hyp findAtIndex_elim; intuition.
find_eapply_lem_hyp msg_lifted_sms_nw; eauto;
[|eapply msg_commit_recorded_lift_intro; eauto;
left; repeat find_rewrite; auto using lt_le_weak].
intuition.
* subst.
assert (0 < eIndex x) by (eapply lifted_entries_contiguous_invariant; eauto).
omega.
* destruct (log d); intuition. simpl in *.
intuition; subst; auto.
find_apply_hyp_hyp. omega.
+ destruct (le_lt_dec (lastApplied (snd (nwState net (pDst p)))) pli); intuition;
[eapply le_trans; [| apply sorted_maxIndex_app]; auto;
break_exists; break_and;
erewrite maxIndex_removeAfterIndex by (eauto; apply msg_lifted_sorted_host; auto);
auto|]; [idtac].
destruct (le_lt_dec (lastApplied (snd (nwState net (pDst p)))) (maxIndex es)); intuition;
[match goal with
| |- context [ maxIndex (?ll1 ++ ?ll2) ] =>
pose proof maxIndex_app ll1 ll2
end; simpl in *; intuition|]; [idtac].
assert (exists x, eIndex x = maxIndex es /\ In x (log (snd (nwState net (pDst p))))).
{
eapply contiguous_range_exact_lo_elim_exists.
- eapply lifted_entries_contiguous_invariant. auto.
- split.
+ find_apply_lem_hyp maxIndex_non_empty. break_exists.  break_and.
repeat find_rewrite.
destruct es.
* simpl in *. intuition.
* simpl.  subst.
{ eapply le_lt_trans with (m := eIndex x).
- omega.
- eapply contiguous_range_exact_lo_elim_lt.
+ eapply lifted_entries_contiguous_nw_invariant; eauto.
+ intuition.
}
+ eapply le_trans; [|eauto]. simpl in *. omega.
}
break_exists. intuition.
match goal with
| H : findAtIndex _ _ = None |- _ =>
eapply findAtIndex_None with (x1 := x) in H
end; eauto.
* congruence.
* apply msg_lifted_sorted_host. auto.
+   destruct (le_lt_dec (lastApplied (snd (nwState net (pDst p)))) (maxIndex es)); intuition;
[match goal with
| |- context [ maxIndex (?ll1 ++ ?ll2) ] =>
pose proof maxIndex_app ll1 ll2
end; simpl in *; intuition|]; [idtac].
exfalso.
assert (In p (nwPackets net)) by (find_rewrite; intuition).
break_exists; intuition. find_apply_lem_hyp findAtIndex_elim; intuition.
find_copy_apply_lem_hyp maxIndex_non_empty.
break_exists. intuition.
find_eapply_lem_hyp msg_lifted_sms_nw; eauto;
[|eapply msg_commit_recorded_lift_intro; eauto;
left; repeat find_rewrite; auto using lt_le_weak].
match goal with
| _ : In ?x es, _ : maxIndex es = eIndex ?x |- _ =>
assert (pli < eIndex x)
by ( eapply contiguous_range_exact_lo_elim_lt; eauto;
eapply lifted_entries_contiguous_nw_invariant; eauto)
end.
intuition.
match goal with
| H : _ = _ ++ _ |- _ => symmetry in H
end.
destruct es; intuition;
simpl in *;
intuition; subst_max; intuition;
repeat clean;
match goal with
| _ : eIndex ?x' = eIndex ?x, H : context [eIndex ?x'] |- _ =>
specialize (H x); conclude H ltac:(apply in_app_iff; auto)
end; intuition.
- assert (sorted (log d)) by (eauto using lifted_handleAppendEntries_logs_sorted).
match goal with
| _ : handleAppendEntries ?h ?s ?t ?n ?pli ?plt ?es ?ci = (?s', ?m) |- _ =>
pose proof handleAppendEntries_log_detailed
h s t n pli plt es ci s' m
end.
intuition; repeat find_rewrite; try apply max_min_thing;
match goal with
| H : context [ lastApplied _ ] |- _ => clear H
end.
+ eauto.
+ subst.
destruct (le_lt_dec (commitIndex (snd (nwState net (pDst p))))
(maxIndex (log d))); auto.
exfalso.
assert (In p (nwPackets net)) by (find_rewrite; intuition).
assert (exists x, eIndex x = maxIndex (log d) /\ In x (log (snd (nwState net (pDst p))))).
{
eapply contiguous_range_exact_lo_elim_exists.
- eapply lifted_entries_contiguous_invariant. auto.
- split.
+ find_apply_lem_hyp maxIndex_non_empty. break_exists.  break_and.
repeat find_rewrite.
eapply contiguous_range_exact_lo_elim_lt.
* eapply lifted_entries_contiguous_nw_invariant; eauto.
* auto.
+ eapply le_trans; [|eauto]. simpl in *. omega.
}
break_exists. intuition.
find_eapply_lem_hyp findAtIndex_None; eauto.
apply msg_lifted_sorted_host. auto.
+ subst.
destruct (le_lt_dec (commitIndex (snd (nwState net (pDst p))))
(maxIndex (log d))); auto.
exfalso.
assert (In p (nwPackets net)) by (find_rewrite; intuition).
break_exists; intuition. find_apply_lem_hyp findAtIndex_elim; intuition.
find_eapply_lem_hyp msg_lifted_sms_nw; eauto;
[|eapply msg_commit_recorded_lift_intro; eauto;
right; repeat find_rewrite; intuition].
intuition.
* subst.
assert (0 < eIndex x) by (eapply lifted_entries_contiguous_invariant; eauto).
omega.
* destruct (log d); intuition. simpl in *.
intuition; subst; auto.
find_apply_hyp_hyp. omega.
+ destruct (le_lt_dec (commitIndex (snd (nwState net (pDst p)))) pli); intuition;
[eapply le_trans; [| apply sorted_maxIndex_app]; auto;
break_exists; intuition;
erewrite maxIndex_removeAfterIndex; eauto; apply msg_lifted_sorted_host; auto|]; [idtac].
destruct (le_lt_dec (commitIndex (snd (nwState net (pDst p)))) (maxIndex es)); intuition;
[match goal with
| |- context [ maxIndex (?ll1 ++ ?ll2) ] =>
pose proof maxIndex_app ll1 ll2
end; simpl in *; intuition|]; [idtac].
assert (exists x, eIndex x = maxIndex es /\ In x (log (snd (nwState net (pDst p))))).
{
eapply contiguous_range_exact_lo_elim_exists.
- eapply lifted_entries_contiguous_invariant. auto.
- split.
+ find_apply_lem_hyp maxIndex_non_empty. break_exists.  break_and.
repeat find_rewrite.
destruct es.
* simpl in *. intuition.
* simpl.  subst.
{ eapply le_lt_trans with (m := eIndex x).
- omega.
- eapply contiguous_range_exact_lo_elim_lt.
+ eapply lifted_entries_contiguous_nw_invariant; eauto.
+ intuition.
}
+ eapply le_trans; [|eauto]. simpl in *. omega.
}
break_exists. intuition.
match goal with
| H : findAtIndex _ _ = None |- _ =>
eapply findAtIndex_None with (x1 := x) in H
end; eauto.
* congruence.
* apply msg_lifted_sorted_host; auto.
+ destruct (le_lt_dec (commitIndex (snd (nwState net (pDst p)))) (maxIndex es)); intuition;
[match goal with
| |- context [ maxIndex (?ll1 ++ ?ll2) ] =>
pose proof maxIndex_app ll1 ll2
end; simpl in *; intuition|]; [idtac].
exfalso.
assert (In p (nwPackets net)) by (find_rewrite; intuition).
break_exists; intuition. find_apply_lem_hyp findAtIndex_elim; intuition.
find_copy_apply_lem_hyp maxIndex_non_empty.
break_exists. intuition.
find_eapply_lem_hyp msg_lifted_sms_nw; eauto;
[|eapply msg_commit_recorded_lift_intro; eauto;
right; repeat find_rewrite; intuition].
match goal with
| _ : In ?x es, _ : maxIndex es = eIndex ?x |- _ =>
assert (pli < eIndex x)
by (eapply contiguous_range_exact_lo_elim_lt; eauto;
eapply lifted_entries_contiguous_nw_invariant; eauto)
end.
intuition.
* match goal with
| H : _ = _ ++ _ |- _ => symmetry in H
end.
destruct es; intuition;
simpl in *;
intuition; subst_max; intuition;
repeat clean;
match goal with
| _ : eIndex ?x' = eIndex ?x, H : context [eIndex ?x'] |- _ =>
specialize (H x); conclude H ltac:(apply in_app_iff; auto)
end; intuition.
Qed.

Lemma lifted_maxIndex_sanity_append_entries_reply :
msg_refined_raft_net_invariant_append_entries_reply lifted_maxIndex_sanity.