Library prosa.classic.analysis.global.basic.bertogna_edf_theory

Require Import prosa.classic.util.all.
Require Import prosa.classic.model.arrival.basic.task prosa.classic.model.arrival.basic.job prosa.classic.model.arrival.basic.task_arrival prosa.classic.model.priority.
Require Import prosa.classic.model.schedule.global.workload prosa.classic.model.schedule.global.schedulability
               prosa.classic.model.schedule.global.response_time.
Require Import prosa.classic.model.schedule.global.basic.schedule prosa.classic.model.schedule.global.basic.platform
               prosa.classic.model.schedule.global.basic.interference prosa.classic.model.schedule.global.basic.platform
               prosa.classic.model.schedule.global.basic.constrained_deadlines.
Require Import prosa.classic.analysis.global.basic.workload_bound prosa.classic.analysis.global.basic.interference_bound_edf.
From mathcomp Require Import ssreflect ssrbool eqtype ssrnat seq fintype bigop div path.

Module ResponseTimeAnalysisEDF.

  Export Job SporadicTaskset Schedule ScheduleOfSporadicTask Workload Schedulability ResponseTime
         Priority TaskArrival WorkloadBound InterferenceBoundEDF
         Interference Platform ConstrainedDeadlines.

  Section ResponseTimeBound.

    Context {sporadic_task: eqType}.
    Variable task_cost: sporadic_task time.
    Variable task_period: sporadic_task time.
    Variable task_deadline: sporadic_task time.

    Context {Job: eqType}.
    Variable job_arrival: Job time.
    Variable job_cost: Job time.
    Variable job_deadline: Job time.
    Variable job_task: Job sporadic_task.

    Variable arr_seq: arrival_sequence Job.

    Hypothesis H_sporadic_tasks:
      sporadic_task_model task_period job_arrival job_task arr_seq.
    Hypothesis H_valid_job_parameters:
       j,
        arrives_in arr_seq j
        valid_sporadic_job task_cost task_deadline job_cost job_deadline job_task j.

    Variable ts: taskset_of sporadic_task.
    Hypothesis H_valid_task_parameters:
      valid_sporadic_taskset task_cost task_period task_deadline ts.
    Hypothesis H_constrained_deadlines:
       tsk, tsk \in ts task_deadline tsk task_period tsk.

    Hypothesis H_all_jobs_from_taskset:
       j,
        arrives_in arr_seq j job_task j \in ts.

    Variable num_cpus: nat.
    Variable sched: schedule Job num_cpus.
    Hypothesis H_jobs_come_from_arrival_sequence:
      jobs_come_from_arrival_sequence sched arr_seq.

    Hypothesis H_sequential_jobs: sequential_jobs sched.
    Hypothesis H_jobs_must_arrive_to_execute: jobs_must_arrive_to_execute job_arrival sched.
    Hypothesis H_completed_jobs_dont_execute: completed_jobs_dont_execute job_cost sched.

    Hypothesis H_at_least_one_cpu: num_cpus > 0.

    Hypothesis H_work_conserving: work_conserving job_arrival job_cost arr_seq sched.
    Hypothesis H_edf_policy: respects_JLFP_policy job_arrival job_cost arr_seq sched
                                                  (EDF job_arrival job_deadline).

    Let no_deadline_is_missed_by_tsk (tsk: sporadic_task) :=
      task_misses_no_deadline job_arrival job_cost job_deadline job_task arr_seq sched tsk.
    Let response_time_bounded_by (tsk: sporadic_task) :=
      is_response_time_bound_of_task job_arrival job_cost job_task arr_seq sched tsk.

    Let task_with_response_time := (sporadic_task × time)%type.
    Variable rt_bounds: seq task_with_response_time.

    Hypothesis H_rt_bounds_contains_all_tasks: unzip1 rt_bounds = ts.

    Let I (tsk: sporadic_task) (delta: time) :=
      total_interference_bound_edf task_cost task_period task_deadline tsk rt_bounds delta.
    Hypothesis H_response_time_is_fixed_point :
       tsk R,
        (tsk, R) \in rt_bounds
        R = task_cost tsk + div_floor (I tsk R) num_cpus.

    Hypothesis H_tasks_miss_no_deadlines:
       tsk_other R,
        (tsk_other, R) \in rt_bounds R task_deadline tsk_other.

    Section Lemmas.

      Variable tsk: sporadic_task.
      Variable R: time.
      Hypothesis H_tsk_R_in_rt_bounds: (tsk, R) \in rt_bounds.

      Variable j: Job.
      Hypothesis H_j_arrives: arrives_in arr_seq j.
      Hypothesis H_job_of_tsk: job_task j = tsk.

      Hypothesis H_j_not_completed: ~~ completed job_cost sched j (job_arrival j + R).

      Hypothesis H_all_previous_jobs_completed_on_time :
         j_other tsk_other R_other,
          arrives_in arr_seq j_other
          job_task j_other = tsk_other
          (tsk_other, R_other) \in rt_bounds
          job_arrival j_other + R_other < job_arrival j + R
          completed job_cost sched j_other (job_arrival j_other + R_other).

      Let x (tsk_other: sporadic_task) :=
        task_interference job_arrival job_cost job_task sched j tsk_other
                          (job_arrival j) (job_arrival j + R).

      Let X := total_interference job_arrival job_cost sched j (job_arrival j) (job_arrival j + R).

      Let workload_bound (tsk_other: sporadic_task) (R_other: time) :=
        W task_cost task_period tsk_other R_other R.

      Let edf_specific_bound (tsk_other: sporadic_task) (R_other: time) :=
        edf_specific_interference_bound task_cost task_period task_deadline tsk tsk_other R_other.

      Let interference_bound (tsk_other: sporadic_task) (R_other: time) :=
        interference_bound_edf task_cost task_period task_deadline tsk R (tsk_other, R_other).

      Let other_task := different_task tsk.

      Let other_tasks :=
        [seq tsk_other <- ts | other_task tsk_other].

      Section LemmasAboutInterferingTasks.

        Variable tsk_other: sporadic_task.
        Variable R_other: time.
        Hypothesis H_response_time_of_tsk_other: (tsk_other, R_other) \in rt_bounds.

        Lemma bertogna_edf_tsk_other_in_ts: tsk_other \in ts.
        Proof.
          rewrite set_mem -H_rt_bounds_contains_all_tasks.
          by apply/mapP; (tsk_other, R_other).
        Qed.

        Lemma bertogna_edf_R_other_ge_cost :
          R_other task_cost tsk_other.
        Proof.
          by rewrite [R_other](H_response_time_is_fixed_point tsk_other);
            first by apply leq_addr.
        Qed.

        Lemma bertogna_edf_workload_bounds_interference :
          x tsk_other workload_bound tsk_other R_other.
        Proof.
          unfold valid_sporadic_job in ×.
          rename H_all_previous_jobs_completed_on_time into BEFOREok,
                 H_valid_job_parameters into PARAMS,
                 H_valid_task_parameters into TASK_PARAMS,
                 H_constrained_deadlines into RESTR,
                 H_tasks_miss_no_deadlines into NOMISS.
          unfold x, task_interference.
          have INts := bertogna_edf_tsk_other_in_ts.
          apply leq_trans with (n := workload job_task sched tsk_other
                                         (job_arrival j) (job_arrival j + R));
            first by apply task_interference_le_workload.
          by ( try ( apply workload_bounded_by_W with (task_deadline0 := task_deadline) (arr_seq0 := arr_seq)
            (job_arrival0 := job_arrival) (job_cost0 := job_cost) (job_deadline0 := job_deadline) ) ||
          apply workload_bounded_by_W with (task_deadline := task_deadline) (arr_seq := arr_seq)
            (job_arrival := job_arrival) (job_cost := job_cost) (job_deadline := job_deadline));
            try (by ins); last 2 first;
            [ by apply bertogna_edf_R_other_ge_cost
            | by ins; apply NOMISS
            | by ins; apply TASK_PARAMS
            | by ins; apply RESTR
            | by ins; apply BEFOREok with (tsk_other := tsk_other)].
        Qed.

        Lemma bertogna_edf_specific_bound_holds :
          x tsk_other edf_specific_bound tsk_other R_other.
        Proof.
          ( try ( apply interference_bound_edf_bounds_interference with (job_deadline0 := job_deadline)
                                              (arr_seq0 := arr_seq) (ts0 := ts); try (by done) ) ||
          apply interference_bound_edf_bounds_interference with (job_deadline := job_deadline)
                                              (arr_seq := arr_seq) (ts := ts); try (by done));
            [ by apply bertogna_edf_tsk_other_in_ts
            | by apply H_tasks_miss_no_deadlines
            | ].
          by ins; apply H_all_previous_jobs_completed_on_time with (tsk_other := tsk_other).
        Qed.

      End LemmasAboutInterferingTasks.

      Section DerivingContradiction.

      Lemma bertogna_edf_too_much_interference : X R - task_cost tsk + 1.
      Proof.
        rename H_completed_jobs_dont_execute into COMP,
               H_valid_job_parameters into PARAMS, H_response_time_is_fixed_point into REC,
               H_job_of_tsk into JOBtsk, H_j_not_completed into NOTCOMP.
        unfold completed, valid_sporadic_job in ×.
        unfold X, total_interference; rewrite addn1.
        rewrite -(ltn_add2r (task_cost tsk)).
        rewrite addnBAC; last by rewrite [R](REC tsk) // leq_addr.
        rewrite -addnBA // subnn addn0.
        move: (NOTCOMP) ⇒ /negP NOTCOMP'.
        rewrite -ltnNge in NOTCOMP.
        apply leq_ltn_trans with (n := (\sum_(job_arrival j t < job_arrival j + R)
                                     backlogged job_arrival job_cost sched j t) +
                                   service sched j (job_arrival j + R)); last first.
        {
          rewrite -addn1 -addnA leq_add2l addn1.
          apply leq_trans with (n := job_cost j); first by done.
          by specialize (PARAMS j H_j_arrives); des; rewrite -JOBtsk.
        }
        unfold service; rewrite service_before_arrival_eq_service_during //.
        rewrite -big_split /=.
        apply leq_trans with (n := \sum_(job_arrival j i < job_arrival j + R) 1);
          first by rewrite big_const_nat iter_addn mul1n addn0 addKn.
        rewrite big_nat_cond [\sum_(_ _ < _ | true) _]big_nat_cond.
        apply leq_sum; movei /andP [/andP [GEi LTi] _].
        destruct (backlogged job_arrival job_cost sched j i) eqn:BACK;
          first by rewrite -addn1 addnC; apply leq_add.
        apply negbT in BACK.
        rewrite add0n lt0n -not_scheduled_no_service negbK.
        rewrite /backlogged negb_and negbK in BACK.
        move: BACK ⇒ /orP [/negP NOTPENDING | SCHED]; last by done.
        exfalso; apply NOTPENDING; unfold pending; apply/andP; split; first by done.
        apply/negP; red; intro BUG; apply NOTCOMP'.
        by apply completion_monotonic with (t := i); try (by done); apply ltnW.
      Qed.

      Lemma bertogna_edf_interference_by_different_tasks :
         t j_other,
          job_arrival j t < job_arrival j + R
          arrives_in arr_seq j_other
          backlogged job_arrival job_cost sched j t
          scheduled sched j_other t
          job_task j_other != tsk.
      Proof.
        rename H_all_jobs_from_taskset into FROMTS,
               H_valid_task_parameters into PARAMS,
               H_job_of_tsk into JOBtsk, H_sporadic_tasks into SPO,
               H_work_conserving into WORK,
               H_tsk_R_in_rt_bounds into INbounds,
               H_all_previous_jobs_completed_on_time into BEFOREok,
               H_tasks_miss_no_deadlines into NOMISS,
               H_constrained_deadlines into CONSTR.
        movet j_other /andP [LEt GEt] ARRother BACK SCHED.
        apply/eqP; red; intro SAMEtsk.
        move: SCHED ⇒ /existsP [cpu SCHED].
        assert (SCHED': scheduled sched j_other t).
          by apply/existsP; cpu.
        clear SCHED; rename SCHED' into SCHED.
        move: (SCHED) ⇒ PENDING.
        try ( apply scheduled_implies_pending with (job_cost0 := job_cost) (job_arrival0 := job_arrival)
          in PENDING; try (by done) ) ||
        apply scheduled_implies_pending with (job_cost := job_cost) (job_arrival := job_arrival)
          in PENDING; try (by done).
        destruct (ltnP (job_arrival j_other) (job_arrival j)) as [BEFOREother | BEFOREj].
         {
          move: (BEFOREother) ⇒ LT; rewrite -(ltn_add2r R) in LT.
          specialize (BEFOREok j_other tsk R ARRother SAMEtsk INbounds LT).
          move: PENDING ⇒ /andP [_ /negP NOTCOMP]; apply NOTCOMP.
          try ( apply completion_monotonic with (t0 := job_arrival j_other + R); try (by done) ) ||
          apply completion_monotonic with (t := job_arrival j_other + R); try (by done).
          apply leq_trans with (n := job_arrival j); last by done.
          apply leq_trans with (n := job_arrival j_other + task_deadline tsk);
            first by rewrite leq_add2l; apply NOMISS.
          apply leq_trans with (n := job_arrival j_other + task_period tsk);
            first by rewrite leq_add2l; apply CONSTR; rewrite -JOBtsk FROMTS.
          rewrite -SAMEtsk; apply SPO; try (by done); [ | by rewrite JOBtsk | by apply ltnW].
          by red; intro EQ; subst; rewrite ltnn in BEFOREother.
        }
        {
          move: PENDING ⇒ /andP [ARRIVED _].
          exploit (SPO j j_other); try (by done); [ | by rewrite SAMEtsk | ]; last first.
          {
            apply/negP; rewrite -ltnNge.
            apply leq_ltn_trans with (n := t); first by done.
            apply leq_trans with (n := job_arrival j + R); first by done.
            by rewrite leq_add2l; apply leq_trans with (n := task_deadline tsk);
              [by apply NOMISS | by rewrite JOBtsk CONSTR // -JOBtsk FROMTS].
          }
          by red; intros EQtsk; subst; rewrite /backlogged SCHED andbF in BACK.
        }
      Qed.

      Lemma bertogna_edf_all_previous_jobs_complete_by_their_period:
         t j0,
          arrives_in arr_seq j0
          t < job_arrival j + R
          job_arrival j0 + task_period (job_task j0) t
          completed job_cost sched j0
             (job_arrival j0 + task_period (job_task j0)).
      Proof.
        rename H_rt_bounds_contains_all_tasks into UNZIP,
               H_constrained_deadlines into CONSTR,
               H_tasks_miss_no_deadlines into NOMISS,
               H_all_jobs_from_taskset into FROMTS,
               H_all_previous_jobs_completed_on_time into BEFOREok.
        intros t j0 ARR0 LEt LE.
        cut ((job_task j0) \in unzip1 rt_bounds = true); last by rewrite UNZIP FROMTS.
        move ⇒ /mapP [p IN EQ]; destruct p as [tsk' R0]; simpl in *; subst tsk'.
        try ( apply completion_monotonic with (t0 := job_arrival j0 + R0) ) ||
        apply completion_monotonic with (t := job_arrival j0 + R0).
        {
          rewrite leq_add2l; apply leq_trans with (n := task_deadline (job_task j0));
            [by apply NOMISS | by apply CONSTR; rewrite FROMTS].
        }
        apply BEFOREok with (tsk_other := (job_task j0)); try by done.
        apply leq_ltn_trans with (n := t); last by done.
        apply leq_trans with (n := job_arrival j0 + task_period (job_task j0)); last by done.
        by rewrite leq_add2l; apply leq_trans with (n := task_deadline (job_task j0));
          [by apply NOMISS | apply CONSTR; rewrite FROMTS].
      Qed.

      Let other_scheduled_task (t: time) (tsk_other: sporadic_task) :=
        task_is_scheduled job_task sched tsk_other t &&
        other_task tsk_other.

      Lemma bertogna_edf_all_cpus_are_busy:
         t,
          job_arrival j t < job_arrival j + R
          backlogged job_arrival job_cost sched j t
          count (other_scheduled_task t) ts = num_cpus.
      Proof.
        rename H_all_jobs_from_taskset into FROMTS,
               H_valid_task_parameters into PARAMS,
               H_job_of_tsk into JOBtsk,
               H_sporadic_tasks into SPO,
               H_tsk_R_in_rt_bounds into INbounds,
               H_all_previous_jobs_completed_on_time into BEFOREok,
               H_tasks_miss_no_deadlines into NOMISS,
               H_rt_bounds_contains_all_tasks into UNZIP,
               H_constrained_deadlines into RESTR,
               H_work_conserving into WORK.
        unfold x, X, total_interference, task_interference.
        movet /andP [LEt LTt] BACK.
        have PLAT := platform_cpus_busy_with_interfering_tasks task_cost task_period task_deadline
          job_arrival job_cost job_task arr_seq sched.
        apply PLAT with (j := j); try (by done);
          first by apply PARAMS; rewrite -JOBtsk; apply FROMTS.
        intros j0 tsk0 ARR0 TSK0 LE.
        cut (tsk0 \in unzip1 rt_bounds = true); last by rewrite UNZIP -TSK0 FROMTS.
        move ⇒ /mapP [p IN EQ]; destruct p as [tsk' R0]; simpl in *; subst tsk'.
        try ( apply completion_monotonic with (t0 := job_arrival j0 + R0); try (by done) ) ||
        apply completion_monotonic with (t := job_arrival j0 + R0); try (by done).
        {
          rewrite leq_add2l TSK0.
          apply leq_trans with (n := task_deadline tsk0); first by apply NOMISS.
          by apply RESTR; rewrite -TSK0 FROMTS.
        }
        {
          apply BEFOREok with (tsk_other := tsk0); try (by done).
          apply leq_ltn_trans with (n := t); last by done.
          apply leq_trans with (n := job_arrival j0 + task_period tsk0); last by done.
          rewrite leq_add2l; apply leq_trans with (n := task_deadline tsk0); first by apply NOMISS.
          by apply RESTR; rewrite -TSK0 FROMTS.
        }
      Qed.

      Lemma bertogna_edf_interference_on_all_cpus :
        \sum_(tsk_k <- other_tasks) x tsk_k = X × num_cpus.
      Proof.
        have DIFFTASK := bertogna_edf_interference_by_different_tasks.
        rename H_all_jobs_from_taskset into FROMTS,
               H_valid_task_parameters into PARAMS,
               H_job_of_tsk into JOBtsk, H_sporadic_tasks into SPO,
               H_work_conserving into WORK, H_jobs_come_from_arrival_sequence into FROMSEQ,
               H_tsk_R_in_rt_bounds into INbounds,
               H_all_previous_jobs_completed_on_time into BEFOREok,
               H_tasks_miss_no_deadlines into NOMISS,
               H_rt_bounds_contains_all_tasks into UNZIP,
               H_constrained_deadlines into CONSTR.
        unfold sporadic_task_model in ×.
        unfold x, X, total_interference, task_interference.
        rewrite -big_mkcond -exchange_big big_distrl /= mul1n.
        rewrite [\sum_(_ _ < _ | backlogged _ _ _ _ _) _]big_mkcond.
        apply eq_big_nat; movet /andP [GEt LTt].
        destruct (backlogged job_arrival job_cost sched j t) eqn:BACK; last first.
        {
          rewrite (eq_bigr (fun i ⇒ 0));
            first by rewrite big_const_seq iter_addn mul0n addn0.
          by intros i _; rewrite (eq_bigr (fun i ⇒ 0));
            first by rewrite big_const_seq iter_addn mul0n addn0.
        }
        rewrite big_mkcond /=.
        rewrite exchange_big /=.
        apply eq_trans with (y := \sum_(cpu < num_cpus) 1); last by simpl_sum_const.
        apply eq_bigr; intros cpu _.
        move: (WORK j t H_j_arrives BACK cpu) ⇒ [j_other /eqP SCHED]; unfold scheduled_on in ×.
        have ARRother: arrives_in arr_seq j_other.
          by apply (FROMSEQ j_other t); apply/existsP; cpu; apply/eqP.
        rewrite (bigD1_seq (job_task j_other)) /=; last by rewrite filter_uniq; destruct ts.
        {
          rewrite (eq_bigr (fun i ⇒ 0));
            last by intros i DIFF; rewrite /task_scheduled_on SCHED;apply/eqP;rewrite eqb0 eq_sym.
          rewrite big_const_seq iter_addn mul0n 2!addn0; apply/eqP; rewrite eqb1.
          by unfold task_scheduled_on; rewrite SCHED.
        }
        rewrite mem_filter; apply/andP; split; last by apply FROMTS.
        apply DIFFTASK with (t := t); try (by done); first by auto.
        by apply/existsP; cpu; apply/eqP.
      Qed.

      Let num_tasks_exceeding delta := count (fun ix i delta) (other_tasks).

      Lemma bertogna_edf_interference_in_non_full_processors :
         delta,
          0 < num_tasks_exceeding delta < num_cpus
          \sum_(i <- other_tasks | x i < delta) x i delta × (num_cpus - num_tasks_exceeding delta).
      Proof.
        have COMP := bertogna_edf_all_previous_jobs_complete_by_their_period.
        have INV := bertogna_edf_all_cpus_are_busy.
        rename H_all_jobs_from_taskset into FROMTS,
               H_valid_task_parameters into PARAMS,
               H_job_of_tsk into JOBtsk,
               H_sporadic_tasks into SPO,
               H_tsk_R_in_rt_bounds into INbounds,
               H_all_previous_jobs_completed_on_time into BEFOREok,
               H_tasks_miss_no_deadlines into NOMISS, H_jobs_come_from_arrival_sequence into FROMSEQ,
               H_constrained_deadlines into CONSTR,
               H_sequential_jobs into SEQ.
        unfold sporadic_task_model in ×.
        movedelta /andP [HAS LT].
        rewrite -has_count in HAS.

        set some_interference_A := fun t
          has (fun tsk_kbacklogged job_arrival job_cost sched j t &&
                            (x tsk_k delta) &&
                            task_is_scheduled job_task sched tsk_k t) other_tasks.
        set total_interference_B := fun t
            backlogged job_arrival job_cost sched j t ×
            count (fun tsk_k(x tsk_k < delta) &&
                  task_is_scheduled job_task sched tsk_k t) other_tasks.

        apply leq_trans with ((\sum_(job_arrival j t < job_arrival j + R)
                              some_interference_A t) × (num_cpus - num_tasks_exceeding delta)).
        {
          rewrite leq_mul2r; apply/orP; right.
          move: HAS ⇒ /hasP HAS; destruct HAS as [tsk_a INa LEa].
          apply leq_trans with (n := x tsk_a); first by apply LEa.
          unfold x, task_interference, some_interference_A.
          apply leq_sum_nat; movet /andP [GEt LTt] _.
          destruct (backlogged job_arrival job_cost sched j t) eqn:BACK;
            last by rewrite (eq_bigr (fun x ⇒ 0)); [by simpl_sum_const | by ins].
          destruct ([ cpu, task_scheduled_on job_task sched tsk_a cpu t]) eqn:SCHED;
            last first.
          {
            apply negbT in SCHED; rewrite negb_exists in SCHED; move: SCHED ⇒ /forallP ALL.
            rewrite (eq_bigr (fun x ⇒ 0)); first by simpl_sum_const.
            by intros cpu _; specialize (ALL cpu); apply negbTE in ALL; rewrite ALL.
          }
          move: SCHED ⇒ /existsP [cpu SCHED].
          apply leq_trans with (n := 1); last first.
          {
            rewrite lt0b; apply/hasP; tsk_a; first by done.
            by rewrite LEa 2!andTb; apply/existsP; cpu.
          }
          rewrite (bigD1 cpu) /= // SCHED.
          rewrite (eq_bigr (fun x ⇒ 0)); first by simpl_sum_const; rewrite leq_b1.
          intros cpu' DIFF.
          apply/eqP; rewrite eqb0; apply/negP.
          intros SCHED'.
          move: DIFF ⇒ /negP DIFF; apply DIFF; apply/eqP.
          unfold task_scheduled_on in ×.
          destruct (sched cpu t) as [j1|] eqn:SCHED1; last by done.
          destruct (sched cpu' t) as [j2|] eqn:SCHED2; last by done.
          move: SCHED SCHED' ⇒ /eqP JOB /eqP JOB'.
          subst tsk_a; symmetry in JOB'.
          have ARR1: arrives_in arr_seq j1.
            by apply (FROMSEQ j1 t); apply/existsP; cpu; apply/eqP.
          have ARR2: arrives_in arr_seq j2.
            by apply (FROMSEQ j2 t); apply/existsP; cpu'; apply/eqP.
          assert (PENDING1: pending job_arrival job_cost sched j1 t).
          {
            apply scheduled_implies_pending; try by done.
            by apply/existsP; cpu; apply/eqP.
          }
          assert (PENDING2: pending job_arrival job_cost sched j2 t).
          {
            apply scheduled_implies_pending; try by done.
            by apply/existsP; cpu'; apply/eqP.
          }
          assert (BUG: j1 = j2).
          {
            ( try ( apply platform_at_most_one_pending_job_of_each_task with (task_cost0 := task_cost)
            (task_period0 := task_period) (task_deadline0 := task_deadline) (tsk0 := tsk)
            (job_cost0 := job_cost) (job_task0 := job_task) (sched0 := sched) (j0 := j) (t0 := t)
            (job_arrival0 := job_arrival) (arr_seq0 := arr_seq) ) ||
            apply platform_at_most_one_pending_job_of_each_task with (task_cost := task_cost)
            (task_period := task_period) (task_deadline := task_deadline) (tsk := tsk)
            (job_cost := job_cost) (job_task := job_task) (sched := sched) (j := j) (t := t)
            (job_arrival := job_arrival) (arr_seq := arr_seq));
               rewrite ?JOBtsk ?SAMEtsk //; first by apply PARAMS; rewrite -JOBtsk FROMTS.
            intros j0 tsk0 ARR0 TSK0 LE.
            by apply (COMP t); rewrite ?TSK0.
          }
          by subst j2; apply SEQ with (j := j1) (t := t).
        }

        apply leq_trans with (\sum_(job_arrival j t < job_arrival j + R)
                                   total_interference_B t).
        {
          rewrite big_distrl /=.
          apply leq_sum_nat; movet LEt _.
          unfold some_interference_A, total_interference_B.
          destruct (backlogged job_arrival job_cost sched j t) eqn:BACK;
            [rewrite mul1n /= | by rewrite has_pred0 //].

          destruct (has (fun tsk_k : sporadic_task(delta x tsk_k) &&
                     task_is_scheduled job_task sched tsk_k t) other_tasks) eqn:HAS';
            last by done.
          rewrite mul1n; move: HAS ⇒ /hasP [tsk_k INk LEk].
          unfold num_tasks_exceeding.
          apply leq_trans with (n := num_cpus -
                       count (fun i(x i delta) &&
                          task_is_scheduled job_task sched i t) other_tasks).
          {
            apply leq_sub2l.
            rewrite -2!sum1_count big_mkcond /=.
            rewrite [\sum_(_ <- _ | _ _)_]big_mkcond /=.
            apply leq_sum; intros i _.
            by destruct (task_is_scheduled job_task sched i t);
              [by rewrite andbT | by rewrite andbF].
          }
          rewrite -count_filter -[count _ other_tasks]count_filter.
          eapply leq_trans with (n := count (predC (fun tskdelta x tsk)) _);
            last by apply eq_leq, eq_in_count; red; ins; rewrite ltnNge.
          rewrite leq_subLR count_predC size_filter.
          by apply leq_trans with (n := count (other_scheduled_task t) ts);
            [by rewrite INV | by rewrite count_filter].
        }
        {
          unfold x at 2, total_interference_B.
          rewrite exchange_big /=; apply leq_sum; intros t _.
          destruct (backlogged job_arrival job_cost sched j t) eqn:BACK; last by ins.
          rewrite mul1n -sum1_count.
          rewrite big_mkcond [\sum_(i <- other_tasks | _ < _) _]big_mkcond /=.
          apply leq_sum_seq; movetsk_k IN _.
          destruct (x tsk_k < delta); [rewrite andTb | by rewrite andFb].
          destruct (task_is_scheduled job_task sched tsk_k t) eqn:SCHED; last by done.
          move: SCHED ⇒ /existsP [cpu SCHED].
          by rewrite (bigD1 cpu) /= // SCHED.
        }
      Qed.

      Lemma bertogna_edf_minimum_exceeds_interference :
         delta,
          \sum_(tsk_k <- other_tasks) x tsk_k delta × num_cpus
             \sum_(tsk_k <- other_tasks) minn (x tsk_k) delta
             delta × num_cpus.
      Proof.
        intros delta SUMLESS.
        set more_interf := fun tsk_kx tsk_k delta.
        rewrite [\sum_(_ <- _) minn _ _](bigID more_interf) /=.
        unfold more_interf, minn.
        rewrite [\sum_(_ <- _ | delta _)_](eq_bigr (fun idelta));
          last by intros i COND; rewrite leqNgt in COND; destruct (delta > x i).
        rewrite [\sum_(_ <- _ | ~~_)_](eq_big (fun ix i < delta)
                                              (fun ix i));
          [| by red; ins; rewrite ltnNge
           | by intros i COND; rewrite -ltnNge in COND; rewrite COND].

        destruct (~~ has (fun idelta x i) other_tasks) eqn:HASa.
        {
          rewrite [\sum_(_ <- _ | _ _) _]big_hasC; last by apply HASa.
          rewrite big_seq_cond; move: HASa ⇒ /hasPn HASa.
          rewrite add0n (eq_bigl (fun i(i \in other_tasks) && true));
            last by red; intros tsk_k; destruct (tsk_k \in other_tasks) eqn:INk;
              [by rewrite andTb ltnNge; apply HASa | by rewrite andFb].
          by rewrite -big_seq_cond.
        } apply negbFE in HASa.

        destruct (num_tasks_exceeding delta num_cpus) eqn:CARD.
        {
          apply leq_trans with (delta × num_tasks_exceeding delta);
            first by rewrite leq_mul2l; apply/orP; right.
          unfold num_tasks_exceeding; rewrite -sum1_count big_distrr /=.
          rewrite -[\sum_(_ <- _ | _) _]addn0.
          by apply leq_add; [by apply leq_sum; ins; rewrite muln1|by ins].
        } apply negbT in CARD; rewrite -ltnNge in CARD.

        rewrite big_const_seq iter_addn addn0; fold num_tasks_exceeding.
        apply leq_trans with (n := delta × num_tasks_exceeding delta +
                                   delta × (num_cpus - num_tasks_exceeding delta));
          first by rewrite -mulnDr subnKC //; apply ltnW.
        rewrite leq_add2l; apply bertogna_edf_interference_in_non_full_processors.
        by apply/andP; split; first by rewrite -has_count.
      Qed.

      Lemma bertogna_edf_sum_exceeds_total_interference:
        \sum_((tsk_other, R_other) <- rt_bounds | other_task tsk_other)
          minn (x tsk_other) (R - task_cost tsk + 1) > I tsk R.
      Proof.
        have GE_COST := bertogna_edf_R_other_ge_cost.
        have EXCEEDS := bertogna_edf_minimum_exceeds_interference.
        have ALLBUSY := bertogna_edf_interference_on_all_cpus.
        have TOOMUCH := bertogna_edf_too_much_interference.
        rename H_rt_bounds_contains_all_tasks into UNZIP,
          H_response_time_is_fixed_point into REC.
        apply leq_trans with (n := \sum_(tsk_other <- other_tasks) minn (x tsk_other) (R - task_cost tsk + 1));
          last first.
        {
          rewrite (eq_bigr (fun iminn (x (fst i)) (R - task_cost tsk + 1)));
            last by ins; destruct i.
          move: UNZIPUNZIP.
          assert (FILTER: filter other_task (unzip1 rt_bounds) =
                          filter other_task ts).
            by f_equal.
          unfold other_tasks; rewrite -FILTER; clear FILTER.
          rewrite -[\sum_(_ <- rt_bounds | _)_]big_filter.
          assert (SUBST: [seq i <- rt_bounds
                           | let '(tsk_other, _) := i in other_task tsk_other] =
                         [seq i <- rt_bounds | other_task (fst i)]).
          {
            by apply eq_filter; red; intro i; destruct i.
          } rewrite SUBST; clear SUBST.
          have MAP := big_map (fun xfst x) (fun itrue) (fun iminn (x i) (R - task_cost tsk + 1)).
          by rewrite -MAP; apply eq_leq; f_equal; rewrite filter_map.
        }
        
        apply ltn_div_trunc with (d := num_cpus); first by apply H_at_least_one_cpu.
        rewrite -(ltn_add2l (task_cost tsk)) -REC; last by done.
        rewrite -addn1 -leq_subLR.
        rewrite -[R + 1 - _]addnBAC; last by apply GE_COST.
        rewrite leq_divRL; last by apply H_at_least_one_cpu.
        apply EXCEEDS.
        apply leq_trans with (n := X × num_cpus);
          last by rewrite ALLBUSY.
        by rewrite leq_mul2r; apply/orP; right; apply TOOMUCH.
      Qed.

      Lemma bertogna_edf_exists_task_that_exceeds_bound :
         tsk_other R_other,
          (tsk_other, R_other) \in rt_bounds
          (minn (x tsk_other) (R - task_cost tsk + 1) > interference_bound tsk_other R_other).
      Proof.
        have SUM := bertogna_edf_sum_exceeds_total_interference.
        have BOUND := bertogna_edf_workload_bounds_interference.
        have EDFBOUND := bertogna_edf_specific_bound_holds.
        rename H_rt_bounds_contains_all_tasks into UNZIP.
        assert (HAS: has (fun tup : task_with_response_time
                            let (tsk_other, R_other) := tup in
                            (tsk_other \in ts) && other_task tsk_other &&
                              (minn (x tsk_other) (R - task_cost tsk + 1) >
                              interference_bound tsk_other R_other))
                         rt_bounds).
        {
          apply/negP; unfold not; intro NOTHAS.
          move: NOTHAS ⇒ /negP /hasPn ALL.
          rewrite -[_ < _]negbK in SUM.
          move: SUM ⇒ /negP SUM; apply SUM; rewrite -leqNgt.
          unfold I, total_interference_bound_edf.
          rewrite big_seq_cond [X in _ X]big_seq_cond.
          apply leq_sum; movetsk_k /andP [INBOUNDSk INTERFk]; destruct tsk_k as [tsk_k R_k].
          specialize (ALL (tsk_k, R_k) INBOUNDSk).
          unfold interference_bound_edf; simpl in ×.
          rewrite leq_min; apply/andP; split.
          {
            unfold interference_bound; rewrite leq_min; apply/andP; split;
              last by rewrite geq_minr.
            apply leq_trans with (n := x tsk_k); first by rewrite geq_minl.
            by apply BOUND.
          }
          {
            apply leq_trans with (n := x tsk_k); first by rewrite geq_minl.
            by apply EDFBOUND.
          }
        }
        move: HAS ⇒ /hasP HAS; destruct HAS as [[tsk_k R_k] HPk MIN].
        move: MIN ⇒ /andP [/andP [INts INTERFk] MINk].
        by tsk_k, R_k; repeat split.
      Qed.

      End DerivingContradiction.

    End Lemmas.

    Section MainProof.

      Variable tsk: sporadic_task.
      Variable R: time.
      Hypothesis H_tsk_R_in_rt_bounds: (tsk, R) \in rt_bounds.

      Theorem bertogna_cirinei_response_time_bound_edf :
        response_time_bounded_by tsk R.
      Proof.
        intros j ARRj JOBtsk.

        remember (job_arrival j + R) as ctime.

        revert H_tsk_R_in_rt_bounds.
        generalize dependent R; generalize dependent tsk; generalize dependent j.

        induction ctime as [ctime IH] using strong_ind.

        intros j ARRj tsk' JOBtsk R' EQc INbounds; subst ctime.

        assert (BEFOREok: j0 tsk R0,
                            arrives_in arr_seq j0
                            job_task j0 = tsk
                            (tsk, R0) \in rt_bounds
                            job_arrival j0 + R0 < job_arrival j + R'
                            service sched j0 (job_arrival j0 + R0) job_cost j0).
        {
            by ins; apply IH with (tsk := tsk0) (R := R0).
        }
        clear IH.

        destruct (completed job_cost sched j (job_arrival j + R')) eqn:NOTCOMP;
          first by done.
        apply negbT in NOTCOMP; exfalso.

        exploit (bertogna_edf_exists_task_that_exceeds_bound tsk' R' INbounds j ARRj JOBtsk NOTCOMP).
        {
          by ins; apply IH with (tsk := tsk_other) (R := R_other).
        }
        intro EX; destruct EX as [tsk_other [R_other [HP LTmin]]].
        unfold interference_bound_edf, interference_bound_generic in LTmin.
        rewrite minnAC in LTmin; apply min_lt_same in LTmin.
        have BASICBOUND := bertogna_edf_workload_bounds_interference R' j BEFOREok tsk_other R_other HP.
        have EDFBOUND := (bertogna_edf_specific_bound_holds tsk' R' j ARRj
                                                            JOBtsk BEFOREok tsk_other R_other HP).
        unfold minn in LTmin; clear -LTmin HP BASICBOUND EDFBOUND tsk; desf.
        {
          by apply (leq_ltn_trans BASICBOUND) in LTmin; rewrite ltnn in LTmin.
        }
        {
          by apply (leq_ltn_trans EDFBOUND) in LTmin; rewrite ltnn in LTmin.
        }
      Qed.

    End MainProof.

  End ResponseTimeBound.

End ResponseTimeAnalysisEDF.