Skip to content

Commit 4fe221a

Browse files
committed
Add floor and some properties
1 parent 88589fa commit 4fe221a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lib/Coqlib.v

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,43 @@ Proof.
542542
lia.
543543
Qed.
544544

545+
(** Floor: [floor n amount] returns the greatest multiple of [amount]
546+
less than or equal to [n]. *)
547+
548+
Definition floor (n: Z) (amount: Z) := (n / amount) * amount.
549+
550+
Lemma floor_interval:
551+
forall x y, y > 0 -> floor x y <= x < floor x y + y.
552+
Proof.
553+
unfold floor; intros.
554+
generalize (Z_div_mod_eq x y H) (Z_mod_lt x y H).
555+
set (q := x / y). set (r := x mod y). intros. lia.
556+
Qed.
557+
558+
Lemma floor_divides:
559+
forall x y, y > 0 -> (y | floor x y).
560+
Proof.
561+
unfold floor; intros. exists (x / y); auto.
562+
Qed.
563+
564+
Lemma floor_same:
565+
forall x y, y > 0 -> (y | x) -> floor x y = x.
566+
Proof.
567+
unfold floor; intros. rewrite (Zdivide_Zdiv_eq y x) at 2; auto; lia.
568+
Qed.
569+
570+
Lemma floor_align_interval:
571+
forall x y, y > 0 ->
572+
floor x y <= align x y <= floor x y + y.
573+
Proof.
574+
unfold floor, align; intros.
575+
replace (x / y * y + y) with ((x + 1 * y) / y * y).
576+
assert (A: forall a b, a <= b -> a / y * y <= b / y * y).
577+
{ intros. apply Z.mul_le_mono_nonneg_r. lia. apply Z.div_le_mono; lia. }
578+
split; apply A; lia.
579+
rewrite Z.div_add by lia. lia.
580+
Qed.
581+
545582
(** * Definitions and theorems on the data types [option], [sum] and [list] *)
546583

547584
Set Implicit Arguments.

0 commit comments

Comments
 (0)