Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zify Nat.double and Nat.div2 #18729

Merged
merged 2 commits into from Apr 16, 2024
Merged

Conversation

andres-erbsen
Copy link
Contributor

@andres-erbsen andres-erbsen commented Feb 28, 2024

I just coached a new Coq user to work around this incompleteness; it didn't leave a good impression.

@andres-erbsen andres-erbsen requested review from a team as code owners February 28, 2024 22:45
@coqbot-app coqbot-app bot added the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Feb 28, 2024
andres-erbsen added a commit to andres-erbsen/tlc that referenced this pull request Feb 28, 2024
andres-erbsen added a commit to andres-erbsen/fcf that referenced this pull request Feb 28, 2024
@andres-erbsen
Copy link
Contributor Author

andres-erbsen commented Feb 28, 2024

The added change in stdlib is only needed until #18730 is merged, to retain zify's ability to see that Nat.div2 returns non-negative values.

adampetcher pushed a commit to adampetcher/fcf that referenced this pull request Feb 29, 2024
* adapt to coq/coq#18729

* require coq 8.19
charguer added a commit to charguer/tlc that referenced this pull request Mar 1, 2024
require zify-nat-double-halve package to adapt to coq/coq#18729
@andres-erbsen
Copy link
Contributor Author

@coqbot run full ci

@coqbot-app coqbot-app bot removed the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Mar 1, 2024
Copy link
Contributor

coqbot-app bot commented Mar 1, 2024

🔴 CI failures at commit 2f227a4 without any failure in the test-suite

✔️ Corresponding jobs for the base commit 139b6ce succeeded

❔ Ask me to try to extract minimal test cases that can be added to the test-suite

🏃 @coqbot ci minimize will minimize the following targets: ci-flocq, ci-neural_net_interp, ci-perennial
  • You can also pass me a specific list of targets to minimize as arguments.

⚠️ ⌛ You may want to wait until the pipeline for the base commit (139b6ce) finishes.

@andres-erbsen
Copy link
Contributor Author

@silene would you be willing to port Flocq over this change? Below is a draft patch that gets Flocq to build with this PR, but it probably isn't backwards compatible in a couple of proofs due to increased power of lia. I believe all of these should be fixable, but not understanding the proofs I could only do an ugly hack job myself.

commit f98c1989c49b72d40e78c85dcab51654d6df849d
Author: Andres Erbsen <andres-github@andres.systems>
Date:   Sat Mar 2 01:25:33 2024 +0000

    port over coq/coq#18729 (wont work on baseline)

diff --git a/src/Calc/Div.v b/src/Calc/Div.v
index 718ccae..c335309 100644
--- a/src/Calc/Div.v
+++ b/src/Calc/Div.v
@@ -73,6 +73,7 @@ Theorem Fdiv_core_correct :
   let '(m, l) := Fdiv_core m1 e1 m2 e2 e in
   inbetween_float beta m e (F2R (Float beta m1 e1) / F2R (Float beta m2 e2)) l.
 Proof.
+clear fexp.
 intros m1 e1 m2 e2 e Hm1 Hm2.
 unfold Fdiv_core.
 match goal with |- context [if ?b then ?b1 else ?b2] => set (m12 := if b then b1 else b2) end.
diff --git a/src/Calc/Sqrt.v b/src/Calc/Sqrt.v
index 0e2b822..02ca0c6 100644
--- a/src/Calc/Sqrt.v
+++ b/src/Calc/Sqrt.v
@@ -77,6 +77,7 @@ Theorem Fsqrt_core_correct :
   let '(m, l) := Fsqrt_core m1 e1 e in
   inbetween_float beta m e (sqrt (F2R (Float beta m1 e1))) l.
 Proof.
+clear fexp.
 intros m1 e1 e Hm1 He.
 unfold Fsqrt_core.
 set (m' := Zmult _ _).
diff --git a/src/Pff/Pff.v b/src/Pff/Pff.v
index e058003..5046292 100644
--- a/src/Pff/Pff.v
+++ b/src/Pff/Pff.v
@@ -17514,33 +17514,11 @@ Hypothesis D5: (Closest b radix (t3-x2y2)%R  t4).
 
 Lemma SLe: (2 <= s).
 unfold s; auto with zarith.
-assert (2<= t-Nat.div2 t)%Z; auto with zarith.
-apply Zmult_le_reg_r with 2%Z; try lia.
-replace ((t-Nat.div2 t)*2)%Z with (2*t-2*Nat.div2 t)%Z by ring.
-replace (2*Nat.div2 t)%Z with (Z_of_nat (Nat.double (Nat.div2 t))).
-case (Nat.Even_or_Odd t); intros I.
-rewrite <- Even_double by easy. lia.
-apply Z.le_trans with (2*t+1-(S ( Nat.double (Nat.div2 t))))%Z; try lia.
-rewrite <- Odd_double by easy. lia.
-unfold Nat.double; rewrite inj_plus; ring.
 Qed.
 
 
 Lemma SGe: (s <= t-2).
-unfold s.
-cut (2<= Nat.div2 t)%Z. lia.
-apply Zmult_le_reg_r with 2%Z; try lia.
-replace (Nat.div2 t*2)%Z with (Z_of_nat (Nat.double (Nat.div2 t))).
-case (Nat.Even_or_Odd t); intros I.
-rewrite <- Even_double by easy. lia.
-apply Z.le_trans with (-1+(S ( Nat.double (Nat.div2 t))))%Z. 2: lia.
-rewrite <- Odd_double; auto with zarith.
-case (Zle_lt_or_eq 4 t); auto with zarith.
-intros I2; absurd (Nat.Odd t); auto.
-intros I3; apply Nat.Even_Odd_False with t; auto.
-replace t with (4) by auto with zarith.
-now exists 2.
-unfold Nat.double; rewrite inj_plus; ring.
+unfold s. nia.
 Qed.
 
 Lemma s2Ge: (t <= s + s)%Z.
@@ -17558,18 +17536,7 @@ Qed.
 
 
 Lemma s2Le: (s + s <= t + 1)%Z.
-unfold s.
-rewrite inj_minus1; auto with zarith.
-2: generalize (Nat.lt_div2 t); auto with zarith.
-assert (t<= 2*(Nat.div2 t)+1)%Z; auto with zarith.
-case (Nat.Even_or_Odd t); intros I.
-apply Z.le_trans with  ((Nat.double (Nat.div2 t)+1))%Z.
-2:unfold Nat.double; rewrite inj_plus; auto with zarith.
-rewrite <- Even_double; auto with zarith.
-apply Z.le_trans with ((S ( Nat.double (Nat.div2 t))))%Z; auto with zarith.
-2: rewrite inj_S; unfold Z.succ; auto with zarith.
-2: unfold Nat.double; rewrite inj_plus; auto with zarith.
-rewrite <- Odd_double; auto with zarith.
+unfold s. nia.
 Qed.
 
 
@@ -17722,8 +17689,8 @@ apply Zmult_lt_compat_l; auto with zarith.
 intros I; rewrite <- I.
 rewrite Zmult_0_l; apply Z.mul_pos_pos; apply Zpower_nat_less; lia.
 rewrite pGivesBound; rewrite <- Zpower_nat_is_exp; auto with zarith.
-apply Zpower_nat_monotone_le; auto with zarith.
-generalize s2Le; auto with zarith.
+apply Zpower_nat_monotone_le; auto with zarith;
+try solve [generalize s2Le; auto with zarith].
 apply sym_eq; unfold vNum in |- *.
 apply
  trans_eq
@@ -17902,19 +17869,19 @@ fold FtoRradix in H'; rewrite Z3 in H'; rewrite <- Ny in H'; auto with real.
 apply trans_eq with (0+ty)%R; auto with real.
 unfold s; apply SLe; auto.
 unfold s; apply SGe; auto.
-right; auto.
-elim Veltkamp with radix b s t y p' q' hy; auto with zarith arith.
-2: apply SLe; auto.
-2: apply SGe; auto.
-2: elim Cy; auto.
+right; auto. {
+elim Veltkamp with radix b s t y p' q' hy; auto with zarith arith;
+try solve [ apply SLe; auto];
+try solve [ apply SGe; auto];
+try solve [ elim Cy; auto].
 intros T1 T; elim T; intros hy' T'; elim T'; intros G1 T''; elim T''; intros ; clear T1 T T' T''.
 unfold FtoRradix; rewrite <- G1.
  apply ClosestZero with (Bound (P_of_succ_nat (pred (Z.abs_nat (Zpower_nat radix (t - s)))))
                                (dExp b)) (t-s) (FtoR radix y)%R; auto with zarith.
-apply p'GivesBound; solve [auto with zarith].
+apply p'GivesBound; solve [auto with zarith]. }
 cut (s <= t-2) ;[idtac | apply SGe; auto].
-unfold s; auto with zarith.
-unfold FtoRradix; apply ClosestZero with b t (x*y)%R; auto with zarith.
+unfold s; auto with zarith; intros.
+unfold FtoRradix. apply ClosestZero with b t (x*y)%R; auto with zarith.
 rewrite <- Ny; ring.
 elim bimplybplusNorm with radix b s t y; auto.
 2: unfold s; apply SLe; auto.
@@ -18165,7 +18132,7 @@ unfold FtoRradix; rewrite <- G1.
  apply ClosestZero with (Bound (P_of_succ_nat (pred (Z.abs_nat (Zpower_nat radix (t - s)))))
             (dExp b)) (t-s) (FtoR radix x)%R; auto with zarith.
 apply p'GivesBound; auto with zarith.
-cut  (s <= t - 2); [unfold s; auto with zarith| apply SGe; auto].
+cut  (s <= t - 2); [unfold s; auto with zarith; intros| apply SGe; auto].
 unfold FtoRradix; apply ClosestZero with b t (x*y)%R; auto with zarith.
 rewrite <- Ny; ring.
 elim bimplybplusNorm with radix b s t x; auto.
@@ -18858,7 +18825,7 @@ unfold FtoRradix; rewrite <- G1.
 apply ClosestZero with (Bound (P_of_succ_nat (pred (Z.abs_nat (Zpower_nat radix (t - s)))))
             (dExp b)) (t-s) (FtoR radix x)%R; auto with zarith.
 apply p'GivesBound; auto with zarith.
-cut (s <= t-2); [unfold s; auto with zarith | apply SGe; auto].
+cut (s <= t-2); [unfold s; auto with zarith; intros | apply SGe; auto].
 unfold FtoRradix; apply ClosestZero with b t (x*y)%R; auto with zarith.
 rewrite <- Ny; ring.
 case (Req_dec 0%R y); intros Nx.
@@ -18909,7 +18876,7 @@ unfold FtoRradix; rewrite <- G1.
 apply ClosestZero with (Bound (P_of_succ_nat (pred (Z.abs_nat (Zpower_nat radix (t - s)))))
             (dExp b)) (t-s) (FtoR radix y)%R; auto with zarith.
 apply p'GivesBound; auto with zarith.
-cut (s <= t-2); [unfold s; auto with zarith | apply SGe; auto].
+cut (s <= t-2); [unfold s; auto with zarith; intros | apply SGe; auto].
 unfold FtoRradix; apply ClosestZero with b t (x*y)%R; auto with zarith.
 rewrite <- Nx; ring.
 apply Dekker2_aux; auto.
diff --git a/src/Pff/Pff2Flocq.v b/src/Pff/Pff2Flocq.v
index 6691ff2..5ce37cb 100644
--- a/src/Pff/Pff2Flocq.v
+++ b/src/Pff/Pff2Flocq.v
@@ -903,11 +903,11 @@ assert (TT: (Z.div2 prec = Nat.div2 (Z.abs_nat prec))%Z).
 rewrite Nat.div2_div, Z.div2_div, Nat2Z.inj_div; simpl.
 rewrite inj_abs; lia.
 rewrite <- TT.
-rewrite inj_abs; try lia.
-rewrite Z.max_r; try lia.
-assert (Z.div2 prec <= prec)%Z; try lia.
-rewrite Z.div2_div; apply Zlt_le_weak.
-apply Z_div_lt; lia.
+rewrite inj_abs; try lia;
+try rewrite Z.max_r; try lia;
+assert (Z.div2 prec <= prec)%Z; try lia;
+try rewrite Z.div2_div; try apply Zlt_le_weak;
+try apply Z_div_lt; lia.
 (* *)
 assert (D:(((- dExp (make_bound beta prec emin) <= Pff.Fexp fx + Pff.Fexp fy)%Z ->
         (FtoR beta fx * FtoR beta fy = FtoR beta fr + FtoR beta ft4)) /\
diff --git a/src/Prop/Relative.v b/src/Prop/Relative.v
index 1925e7b..77cdd22 100644
--- a/src/Prop/Relative.v
+++ b/src/Prop/Relative.v
@@ -504,6 +504,7 @@ Proof. apply Rmult_le_pos; [lra|apply bpow_ge_0]. Qed.
 
 Lemma u_ro_lt_1 : (u_ro < 1)%R.
 Proof.
+clear choice.
 unfold u_ro; apply (Rmult_lt_reg_l 2); [lra|].
 rewrite <-Rmult_assoc, Rinv_r, Rmult_1_l, Rmult_1_r; [|lra].
 apply (Rle_lt_trans _ (bpow 0));

andres-erbsen added a commit to andres-erbsen/perennial that referenced this pull request Mar 2, 2024
andres-erbsen added a commit to andres-erbsen/neural-net-coq-interp that referenced this pull request Mar 2, 2024
andres-erbsen added a commit to andres-erbsen/neural-net-coq-interp that referenced this pull request Mar 2, 2024
andres-erbsen added a commit to andres-erbsen/neural-net-coq-interp that referenced this pull request Mar 2, 2024
JasonGross pushed a commit to JasonGross/neural-net-coq-interp that referenced this pull request Mar 2, 2024
@coqbot-app coqbot-app bot added the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Mar 25, 2024
@andres-erbsen
Copy link
Contributor Author

@coqbot run full ci

1 similar comment
@andres-erbsen
Copy link
Contributor Author

@coqbot run full ci

@coqbot-app coqbot-app bot removed the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Mar 26, 2024
@andres-erbsen andres-erbsen added the request: full CI Use this label when you want your next push to trigger a full CI. label Mar 26, 2024
@coqbot-app coqbot-app bot removed the request: full CI Use this label when you want your next push to trigger a full CI. label Mar 26, 2024
Copy link
Contributor

coqbot-app bot commented Mar 26, 2024

🔴 CI failure at commit 8c8c7bb without any failure in the test-suite

✔️ Corresponding job for the base commit d2c3b06 succeeded

❔ Ask me to try to extract a minimal test case that can be added to the test-suite

🏃 @coqbot ci minimize will minimize the following target: ci-flocq
  • You can also pass me a specific list of targets to minimize as arguments.

@andres-erbsen
Copy link
Contributor Author

@coq/micromega-maintainers I think this PR is ready.

Mtac2 failure is unrelated (in an mlg file). Here is an updated backwards-compatible patch for flocq (@silene please apply):

diff --git a/src/Pff/Pff.v b/src/Pff/Pff.v
index e058003..0d9c9a7 100644
--- a/src/Pff/Pff.v
+++ b/src/Pff/Pff.v
@@ -17551,7 +17551,7 @@ apply Z.le_trans with  (Nat.double (Nat.div2 t)).
 unfold Nat.double; rewrite inj_plus; auto with zarith.
 rewrite <- Even_double; auto with zarith.
 apply Z.le_trans with (-1+(S ( Nat.double (Nat.div2 t))))%Z.
-rewrite inj_S; unfold Z.succ; auto with zarith.
+rewrite inj_S; unfold Z.succ; auto with zarith;
 unfold Nat.double; rewrite inj_plus; auto with zarith.
 rewrite <- Odd_double by easy. lia.
 Qed.
@@ -17566,9 +17566,9 @@ case (Nat.Even_or_Odd t); intros I.
 apply Z.le_trans with  ((Nat.double (Nat.div2 t)+1))%Z.
 2:unfold Nat.double; rewrite inj_plus; auto with zarith.
 rewrite <- Even_double; auto with zarith.
-apply Z.le_trans with ((S ( Nat.double (Nat.div2 t))))%Z; auto with zarith.
-2: rewrite inj_S; unfold Z.succ; auto with zarith.
-2: unfold Nat.double; rewrite inj_plus; auto with zarith.
+apply Z.le_trans with ((S ( Nat.double (Nat.div2 t))))%Z; auto with zarith;
+try solve [rewrite inj_S; unfold Z.succ; auto with zarith;
+  unfold Nat.double; rewrite inj_plus; auto with zarith].
 rewrite <- Odd_double; auto with zarith.
 Qed.
 

@fajb
Copy link
Contributor

fajb commented Mar 28, 2024

I can merge when the flocq patch is applied. @silene don't hesitate to ping @fajb

@fajb fajb self-assigned this Mar 28, 2024
@fajb fajb added the part: micromega The lia, nia, lra, nra and psatz tactics. Also the legacy omega tactic. label Mar 28, 2024
@fajb fajb added this to the 8.20+rc1 milestone Mar 28, 2024
@silene
Copy link
Contributor

silene commented Apr 16, 2024

Done.

@fajb
Copy link
Contributor

fajb commented Apr 16, 2024

Thanks @silene

@coqbot: merge now

Copy link
Contributor

coqbot-app bot commented Apr 16, 2024

@fajb: You cannot merge this PR because:

  • There is no kind: label.

@fajb fajb added the kind: enhancement Enhancement to an existing user-facing feature, tactic, etc. label Apr 16, 2024
@fajb
Copy link
Contributor

fajb commented Apr 16, 2024

@coqbot: merge now

@coqbot-app coqbot-app bot merged commit c1f50fd into coq:master Apr 16, 2024
7 of 9 checks passed
@fajb
Copy link
Contributor

fajb commented Apr 16, 2024

Thanks @andres-erbsen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement Enhancement to an existing user-facing feature, tactic, etc. part: micromega The lia, nia, lra, nra and psatz tactics. Also the legacy omega tactic.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants