Commit 8a48db8d by jonathan julou

### fuuuuuuuuuuuuuuuuuu

 From rt.restructuring.behavior Require Export schedule facts.arrivals facts.completion. From rt.util Require Import bigcat. From mathcomp Require Import seq. From rt.restructuring.behavior Require Export arrival_sequence. Section Schedule. Context {Job : JobType} {JobCost Job} {JobArrival Job}. Context {State : Type} `{ProcessorState Job State}. Variable (arr_seq : arrival_sequence Job). (* [next_state t candidate_jobs] should return the schedule state at time t given the list of pending jobs and their remaining cost *) Variable next_state : instant -> seq (Job * duration) -> State. Definition candidate_jobs (sched : schedule State) (t : instant) : seq (Job * nat):= [seq (j, remaining_cost sched j t) | j <- [seq j <- arrivals_before arr_seq t.+1 | ~~ completed_by sched j t]]. Hypothesis next_state_correct : forall t jobs j, scheduled_in j (next_state t jobs) -> j \in map fst jobs. Hypothesis next_state_service : forall t jobs j c, (j, c) \in jobs -> service_in j (next_state t jobs) <= c. Lemma next_state_remaining_cost sched j t : service_in j (next_state t (candidate_jobs sched t)) <= remaining_cost sched j t. Proof. case:(boolP (scheduled_in j (next_state t (candidate_jobs sched t)))). { move=>/next_state_correct. rewrite -map_comp map_id=>Hj. apply: next_state_service. by rewrite mem_map=>[|j1 j2 []->//]. } { move=>/negbTE Hj. by rewrite service_implies_scheduled. } Qed. Hypothesis Harr_seq : valid_arrival_sequence arr_seq. Lemma candidate_jobsP sched j t : reflect (arrives_in arr_seq j /\ pending sched j t) (j \in map fst (candidate_jobs sched t)). Proof. rewrite -map_comp/=map_id. rewrite mem_filter. apply:(iffP idP)=>[/andP[Hnot_comp Harrj]|[[tj Htj] /andP[Harrivalj ->/=]]]. { split; first (apply: in_arrivals_implies_arrived; exact: Harrj). rewrite /pending /has_arrived Hnot_comp andbT. rewrite -ltnS. apply: in_arrivals_implies_arrived_before; [|exact: Harrj]. by move:Harr_seq=>[]. } { apply: mem_bigcat_nat. - rewrite /= ltnS. exact: Harrivalj. - by move: Harr_seq=>[/(_ j tj) ->]. } Qed. Section ScheduleCompletion. Variable sched : schedule State. Variable t_start : instant. (* Given [sched] which is valid before [t_start], [complete_schedule_helper sched t_start d] is valid up to [t_start + d] *) Fixpoint complete_schedule_helper (d : duration) : schedule State := let sched' := if d is d.+1 then complete_schedule_helper d else sched in fun t => if t == t_start + d then next_state t (candidate_jobs sched' t) else sched' t. Lemma complete_schedule_helper_prefix d t : t < t_start -> complete_schedule_helper d t = sched t. Proof. move=> Ht. have{Ht} ineq: forall x, t == t_start + x = false by move=>x; apply/negbTE; rewrite neq_ltn (leq_trans Ht) ?leq_addr. elim: d=>[|d IH]; by rewrite /complete_schedule_helper ?IH ineq. Qed. Lemma complete_schedule_helper_preserves_prefix d t : t - t_start <= d -> complete_schedule_helper d t = complete_schedule_helper (t - t_start) t. Proof. elim: d=>[|d IH]; first by rewrite leqn0=>/eqP->. rewrite leq_eqVlt ltnS =>/orP[/eqP->//|ineq]. rewrite -(IH ineq)/=. suff/negbTE->: t != t_start + d.+1 by []. by rewrite neq_ltn addnC addSn ltnS addnC -leq_subLR ineq. Qed. Definition complete_schedule : schedule State := fun t => complete_schedule_helper (t - t_start) t. Lemma complete_schedule_prefix t : t < t_start -> complete_schedule t = sched t. Proof. exact: complete_schedule_helper_prefix. Qed. Lemma complete_schedule_prefix_service j : service sched j t_start = service complete_schedule j t_start. Proof. apply: eq_big_nat=>t/= Ht. by rewrite /service_at complete_schedule_prefix. Qed. Lemma complete_schedule_construction t : t_start <= t -> complete_schedule t = next_state t (candidate_jobs complete_schedule t). Proof. move=>/subnK<-. elim: (t - t_start) =>[|d IH]{t}/=. { rewrite add0n {1}/complete_schedule subnn/= addn0 eqxx. f_equal. rewrite /candidate_jobs. set s1 := filter _ _. set s2 := filter _ _. have->: s1 = s2 by apply: eq_filter=>j; rewrite /completed_by complete_schedule_prefix_service. apply: eq_map=>j. by rewrite /remaining_cost complete_schedule_prefix_service. } { rewrite {1}/complete_schedule addnK/= addnC eqxx. f_equal. rewrite /candidate_jobs. set s1 := filter _ _. set s2 := filter _ _. have->: s1 = s2. { apply: eq_filter=>j. rewrite /completed_by. do 2!f_equal. rewrite addnC addSn. apply: eq_big_nat=>t/=. by rewrite ltnS/service_at addnC -leq_subLR=>/complete_schedule_helper_preserves_prefix->. } apply: eq_map=>j. rewrite /remaining_cost. do 2!f_equal. rewrite addnC addSn. apply: eq_big_nat=>t/=. by rewrite ltnS/service_at addnC -leq_subLR=>/complete_schedule_helper_preserves_prefix->. } Qed. Section Lemmas. Hypothesis Hsched1 : forall j t, t < t_start -> scheduled_at sched j t -> arrives_in arr_seq j. Hypothesis Hsched2 : forall j t, t < t_start -> scheduled_at sched j t -> has_arrived j t. Hypothesis Hsched3 : forall j t, t <= t_start -> service sched j t <= job_cost j. Lemma complete_schedule_valid : valid_schedule complete_schedule arr_seq. Proof. repeat split. { move=> j t. rewrite /scheduled_at. case: (ltnP t t_start)=> Ht. - by move:(Ht)=>/complete_schedule_prefix->/(Hsched1 _ _ Ht). - by rewrite complete_schedule_construction// =>/next_state_correct/candidate_jobsP[]. } { move=> j t. rewrite /scheduled_at. case: (ltnP t t_start)=> Ht; first by move: (Ht)=>/complete_schedule_prefix->/(Hsched2 _ _ Ht). by rewrite complete_schedule_construction// => /next_state_correct/candidate_jobsP[_[/andP[]]]. } { move=> j. elim=>[|t IH]; first by rewrite service0. case: (ltnP t t_start)=> Ht. - apply: leq_trans; last exact: (Hsched3 j t.+1). apply: sum.leq_sum_nat => t0/= Ht0 _. by rewrite /service_at complete_schedule_prefix// (leq_trans Ht0 Ht). - rewrite -service_last_plus_before/service_at complete_schedule_construction//. move/subnK in IH. rewrite -IH addnC leq_add//. exact: next_state_remaining_cost. } Qed. End Lemmas. End ScheduleCompletion. Variable default_state : State. Definition build_schedule : schedule State := complete_schedule (fun t => default_state) 0. Lemma build_schedule_valid : valid_schedule build_schedule arr_seq. Proof. apply: complete_schedule_valid=>// j t. rewrite leqn0=>/eqP->. by rewrite service0. Qed. End Schedule.