(** Formalization of real valued interval arithmetic Used in soundness proofs for error bound validator. **) Require Import Coq.Reals.Reals Coq.micromega.Psatz Coq.QArith.Qreals. Require Import Daisy.Infra.Abbrevs Daisy.Expressions Daisy.Infra.RealSimps. (** Define validity of an interval, requiring that the lower bound is less than or equal to the upper bound. Containement is defined such that if x is contained in the interval, it must lie between the lower and upper bound. **) Definition valid (intv:interval) :Prop := (IVlo intv <= IVhi intv)%R. Definition contained (x:R) (intv:interval) := (IVlo intv <= x <= IVhi intv)%R. Lemma contained_implies_valid (x:R) (intv:interval) : contained x intv -> valid intv. Proof. unfold contained, valid; intros inIntv; apply (Rle_trans _ x _); destruct inIntv; auto. Qed. (** Subset definition; when an interval is a subinterval of another **) Definition isSupersetInterval (iv1:interval) (iv2:interval) := (IVlo iv2 <= IVlo iv1)%R /\ (IVhi iv1 <= IVhi iv2)%R. Definition pointInterval (x:R) := mkInterval x x. Lemma contained_implies_subset (x:R) (intv:interval): contained x intv -> isSupersetInterval (pointInterval x) intv. Proof. intros containedIntv. unfold contained, isSupersetInterval, pointInterval in *; destruct containedIntv; split; auto. Qed. (** Definition of validity conditions for interval operations, Addition, Subtraction, Multiplication and Division **) Definition validIntervalAdd (iv1:interval) (iv2:interval) (iv3:interval) := forall a b, contained a iv1 -> contained b iv2 -> contained (a + b) iv3. Definition validIntervalSub (iv1:interval) (iv2:interval) (iv3:interval) := forall a b, contained a iv1 -> contained b iv2 -> contained (a - b) iv3. Definition validIntervalMult (iv1:interval) (iv2:interval) (iv3:interval) := forall a b, contained a iv1 -> contained b iv2 -> contained (a * b) iv3. Definition validIntervalDiv (iv1:interval) (iv2:interval) (iv3:interval) := forall a b, contained a iv1 -> contained b iv2 -> contained (a / b) iv3. Lemma validPointInterval (a:R) : contained a (pointInterval a). Proof. unfold contained; split; simpl; apply Req_le; auto. Qed. (** Now comes the old part with the computational definitions. Where possible within time, they are shown sound with respect to the definitions from before, where not, we leave this as proof obligation for daisy. **) (** TODO: Refactor into a list manipulating function instead **) Definition min4 (a:R) (b:R) (c:R) (d:R) := Rmin a (Rmin b (Rmin c d)). Definition max4 (a:R) (b:R) (c:R) (d:R) := Rmax a (Rmax b (Rmax c d)). Lemma min4_correct (a b c d:R) : (let m := min4 a b c d in m <= a /\ m <= b /\ m <= c /\ m <= d)%R. Proof. intros m. repeat split; unfold m, min4. - apply Rmin_l. - assert (forall c, Rmin b c <= b)%R by (apply Rmin_l). apply (Rle_trans _ (Rmin b (Rmin c d)) _); auto. apply Rmin_r. - assert (Rmin c d <= c)%R by (apply Rmin_l). assert (Rmin b (Rmin c d) <= c)%R. + apply (Rle_trans _ (Rmin c d) _); auto. apply Rmin_r. + apply (Rle_trans _ (Rmin b (Rmin c d)) _); auto. apply Rmin_r. - assert (Rmin c d <= d)%R by (apply Rmin_r). assert (Rmin b (Rmin c d) <= d)%R. + apply (Rle_trans _ (Rmin c d) _); auto. apply Rmin_r. + apply (Rle_trans _ (Rmin b (Rmin c d)) _); auto. apply Rmin_r. Qed. Lemma max4_correct (a b c d:R) : (let m := max4 a b c d in a <= m /\ b <= m /\ c <= m /\ d <= m)%R. Proof. intros m. repeat split; unfold m, max4. - apply Rmax_l. - assert (forall c, b <= Rmax b c)%R by (apply Rmax_l). apply (Rle_trans _ (Rmax b (Rmax c d)) _); auto. apply Rmax_r. - assert (c <= Rmax c d)%R by (apply Rmax_l). assert (c <= Rmax b (Rmax c d))%R. + apply (Rle_trans _ (Rmax c d) _); auto. apply Rmax_r. + apply (Rle_trans _ (Rmax b (Rmax c d)) _); auto. apply Rmax_r. - assert (d <= Rmax c d)%R by (apply Rmax_r). assert (d <= Rmax b (Rmax c d))%R. + apply (Rle_trans _ (Rmax c d) _); auto. apply Rmax_r. + apply (Rle_trans _ (Rmax b (Rmax c d)) _); auto. apply Rmax_r. Qed. (** Asbtract interval update function, parametric by actual operator applied. **) Definition absIntvUpd (op:R->R->R) (I1:interval) (I2:interval) := ( min4 (op (IVlo I1) (IVlo I2)) (op (IVlo I1) (IVhi I2)) (op (IVhi I1) (IVlo I2)) (op (IVhi I1) (IVhi I2)), max4 (op (IVlo I1) (IVlo I2)) (op (IVlo I1) (IVhi I2)) (op (IVhi I1) (IVlo I2)) (op (IVhi I1) (IVhi I2)) ). Definition widenInterval (Iv:interval) (v:R) := mkInterval (IVlo Iv - v) (IVhi Iv + v). Definition negateInterval (intv:interval) := mkInterval (- IVhi intv) (- IVlo intv). Lemma interval_negation_valid (intv:interval) (a:R) : contained a intv-> contained (- a) (negateInterval intv). Proof. unfold contained; destruct intv as [ivlo ivhi]; simpl; intros [ivlo_le_a a_le_ivhi]. split; apply Ropp_ge_le_contravar; apply Rle_ge; auto. Qed. Definition invertInterval (intv:interval) := mkInterval (/ IVhi intv) (/ IVlo intv). Lemma interval_inversion_valid (iv:interval) (a:R) : (IVhi iv < 0 \/ 0 < IVlo iv -> contained a iv -> contained (/ a) (invertInterval iv))%R. Proof. unfold contained; destruct iv as [ivlo ivhi]; simpl; intros [ivhi_lt_zero | zero_lt_ivlo]; intros [ivlo_le_a a_le_ivhi]; assert (ivlo <= ivhi)%R by (eapply Rle_trans; eauto); split. - assert (- / a <= - / ivhi)%R. + assert (0 < - ivhi)%R by lra. repeat rewrite Ropp_inv_permute; try lra. eapply RIneq.Rinv_le_contravar; try lra. + apply Ropp_le_ge_contravar in H0; repeat rewrite Ropp_involutive in H0; lra. - assert (- / ivlo <= - / a)%R. + repeat rewrite Ropp_inv_permute; try lra. eapply RIneq.Rinv_le_contravar; try lra. + apply Ropp_le_ge_contravar in H0; repeat rewrite Ropp_involutive in H0; lra. - eapply RIneq.Rinv_le_contravar; try lra. - eapply RIneq.Rinv_le_contravar; try lra. Qed. Definition addInterval (iv1:interval) (iv2:interval) := absIntvUpd Rplus iv1 iv2. Lemma additionIsValid iv1 iv2: validIntervalAdd iv1 iv2 (addInterval iv1 iv2). Proof. unfold validIntervalAdd. intros a b. unfold contained, addInterval, absIntvUpd, min4, max4. intros [lo_leq_a a_leq_hi] [lo_leq_b b_leq_hi]. simpl; split. (** Lower Bound **) - assert ( fst iv1 + fst iv2 <= a + b)%R as lower_bound by (apply Rplus_le_compat; auto). apply (Rle_trans _ (fst iv1 + fst iv2) _); [apply Rmin_l | auto]. (** Upper Bound **) - assert (a + b <= snd iv1 + snd iv2)%R as upper_bound by (apply Rplus_le_compat; auto). apply (Rle_trans _ (snd iv1 + snd iv2) _); [ auto | ]. apply (Rle_trans _ (Rmax (fst iv1 + snd iv2) (Rmax (snd iv1 + fst iv2) (snd iv1 + snd iv2))) _); [ | apply Rmax_r]. apply (Rle_trans _ (Rmax (snd iv1 + fst iv2) (snd iv1 + snd iv2)) _ ); [ | apply Rmax_r]. apply (Rle_trans _ (snd iv1 + snd iv2) _); [ apply Req_le; auto | apply Rmax_r]. Qed. Definition subtractInterval (I1:interval) (I2:interval) := addInterval I1 (negateInterval I2). Corollary subtractionIsValid iv1 iv2: validIntervalSub iv1 iv2 (subtractInterval iv1 iv2). Proof. unfold subtractInterval. intros a b. intros contained_1 contained_I2. rewrite Rsub_eq_Ropp_Rplus. apply additionIsValid; auto. apply interval_negation_valid; auto. Qed. Definition multInterval (iv1:interval) (iv2:interval) := absIntvUpd Rmult iv1 iv2. (** Prove validity of multiplication for the interval lattice. Prove is structurally the same as the proof done Jourdan et al. in Verasco, in FloatIntervalsForward.v TODO: Credit **) Lemma interval_multiplication_valid (I1:interval) (I2:interval) (a:R) (b:R) : contained a I1 -> contained b I2 -> contained (a * b) (multInterval I1 I2). Proof. unfold contained, multInterval, absIntvUpd, IVlo, IVhi. destruct I1 as [ivlo1 ivhi1]; destruct I2 as [ivlo2 ivhi2]; simpl. intros [lo_leq_a a_leq_hi] [lo_leq_b b_leq_hi]. pose proof (min4_correct (ivlo1 * ivlo2) (ivlo1 * ivhi2) (ivhi1 * ivlo2) (ivhi1 * ivhi2)) as [leq_lolo [leq_lohi [leq_hilo leq_hihi]]]; pose proof (max4_correct (ivlo1 * ivlo2) (ivlo1 * ivhi2) (ivhi1 * ivlo2) (ivhi1 * ivhi2)) as [lolo_leq [lohi_leq [hilo_leq hihi_leq]]]. split. (* Lower Bound *) - destruct (Rle_lt_dec a 0). + destruct (Rle_lt_dec ivhi2 0). * pose proof (Rmult_le_compat_neg_l a b ivhi2 r b_leq_hi) as ahi2_leq_ab. pose proof (Rmult_le_compat_neg_l ivhi2 a ivhi1 r0 a_leq_hi) as hihi_leq_ahi2. eapply Rle_trans. apply leq_hihi. rewrite Rmult_comm. eapply Rle_trans. apply hihi_leq_ahi2. rewrite Rmult_comm; auto. * eapply Rle_trans. apply leq_lohi. apply Rlt_le in r0. pose proof (Rmult_le_compat_l ivhi2 ivlo1 a). rewrite Rmult_comm. eapply (Rle_trans). apply H; auto. rewrite Rmult_comm. eapply (Rmult_le_compat_neg_l a); auto. + destruct (Rle_lt_dec ivlo2 0). * eapply Rle_trans. apply leq_hilo. rewrite Rmult_comm. eapply Rle_trans. apply (Rmult_le_compat_neg_l ivlo2 a ivhi1 r0 a_leq_hi). rewrite Rmult_comm. eapply (Rmult_le_compat_l a); auto. exact (Rlt_le _ _ r). * eapply Rle_trans. apply leq_lolo. apply Rlt_le in r; apply Rlt_le in r0. rewrite Rmult_comm. apply (Rle_trans _ (ivlo2 * a)). eapply (Rmult_le_compat_l ivlo2 ); auto. rewrite Rmult_comm. eapply (Rmult_le_compat_l a); auto. - destruct (Rle_lt_dec a 0). + eapply Rle_trans. apply (Rmult_le_compat_neg_l a ivlo2); auto. destruct (Rle_lt_dec ivlo2 0). * rewrite Rmult_comm. eapply Rle_trans. eapply (Rmult_le_compat_neg_l ivlo2 ivlo1); auto. rewrite Rmult_comm; auto. * rewrite Rmult_comm. eapply Rle_trans. eapply (Rmult_le_compat_l ivlo2 a ivhi1); auto. apply Rlt_le; auto. rewrite Rmult_comm; auto. + eapply Rle_trans. apply (Rmult_le_compat_l) with (r2 := ivhi2); auto. apply Rlt_le; auto. destruct (Rle_lt_dec ivhi2 0). * rewrite Rmult_comm. eapply Rle_trans. eapply (Rmult_le_compat_neg_l) with (r1:= ivlo1); auto. rewrite Rmult_comm; auto. * rewrite Rmult_comm; eapply Rle_trans. apply (Rmult_le_compat_l) with (r2 := ivhi1); auto. apply Rlt_le; auto. rewrite Rmult_comm; auto. Qed. Definition divideInterval (I1:interval) (I2:interval) := multInterval I1 (mkInterval (/ (IVhi I2)) (/ (IVlo I2))). Corollary divisionIsValid a b I1 I2: (IVhi I2 < 0 \/ 0 < IVlo I2 -> contained a I1 -> contained b I2 -> contained (a / b) (divideInterval I1 I2))%R. Proof. intros nodiv0 c_a c_b. unfold divideInterval. unfold Rdiv. eapply interval_multiplication_valid; auto. apply interval_inversion_valid; auto. Qed. (** Define the maxAbs function on R and prove some minor properties of it. **) Definition RmaxAbsFun (iv:interval) := Rmax (Rabs (fst iv)) (Rabs (snd iv)). Lemma contained_leq_maxAbs a iv: contained a iv -> (Rabs a <= RmaxAbsFun iv)%R. Proof. intros contained_a. unfold RmaxAbsFun. unfold contained in contained_a; simpl in contained_a; destruct contained_a. eapply RmaxAbs; auto. Qed. Lemma contained_leq_maxAbs_val a iv: contained a iv -> (a <= RmaxAbsFun (iv))%R. Proof. intros contained_a; unfold RmaxAbsFun. assert (Rabs a <= RmaxAbsFun iv)%R by (apply contained_leq_maxAbs; auto). eapply Rle_trans. apply Rle_abs. auto. Qed. Lemma contained_leq_maxAbs_neg_val a iv: contained a iv -> (- a <= RmaxAbsFun (iv))%R. Proof. intros contained_a; unfold RmaxAbsFun. assert (Rabs a <= RmaxAbsFun iv)%R by (apply contained_leq_maxAbs; auto). eapply Rle_trans. apply Rle_abs. rewrite Rabs_Ropp. auto. Qed. Lemma Rabs_error_bounded_maxAbs a b eps iv: (Rabs (a - b) <= eps)%R -> contained a iv -> (Rabs b <= Rabs (RmaxAbsFun iv + eps))%R. Proof. intros Rabs_le_eps contained_a. pose proof (contained_leq_maxAbs _ _ contained_a). rewrite Rabs_minus_sym in Rabs_le_eps. assert (Rabs b - Rabs a <= eps)%R. - eapply Rle_trans; [apply Rabs_triang_inv | auto]. - assert (Rabs b <= Rabs a + eps)%R by lra. assert (Rabs a + eps <= RmaxAbsFun iv + eps)%R by (apply Rplus_le_compat_r; auto). eapply Rle_trans; eauto. eapply Rle_trans; eauto. apply Rle_abs. Qed. Lemma lowerbound_iv a ivlo ivhi: contained a (ivlo, ivhi) -> (a >= Rmin (ivlo) (ivhi))%R. Proof. intros contained_a. unfold contained in contained_a; destruct contained_a. assert (ivlo <= ivhi)%R by (eapply Rle_trans; eauto). apply Rle_ge. eapply Rle_trans. apply Rmin_l. auto. Qed. Lemma distance_gives_iv a b e iv: contained a iv -> (Rabs (a - b) <= e)%R -> contained b (widenInterval iv e). Proof. intros contained_a abs_le; unfold contained in *; simpl in *. destruct contained_a as [lo_a a_hi]. rewrite Rabs_minus_sym in abs_le. unfold Rabs in abs_le. destruct Rcase_abs in abs_le. - rewrite Ropp_minus_distr in abs_le. split; lra. - lra. Qed.