| 1 |
----------------------------------------------------------------------- |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Unsigned divisibility |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- For signed divisibility see `Data.Integer.Divisibility.Signed` |
| 8 |
|
| 9 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 10 |
|
| 11 |
module Data.Integer.Divisibility where |
| 12 |
|
| 13 |
open import Function.Base using (_on_; _$_) |
| 14 |
open import Data.Integer.Base using (ℤ; _*_; ∣_∣; NonZero) |
| 15 |
open import Data.Integer.Properties using (abs-*; *-comm) |
| 16 |
import Data.Nat.Base as ℕ using (ℕ; _*_; NonZero) |
| 17 |
import Data.Nat.Divisibility as ℕ |
| 18 |
using (module ∣-Reasoning; _∣_; divides; *-monoʳ-∣; *-cancelˡ-∣) |
| 19 |
open import Level using (0ℓ) |
| 20 |
open import Relation.Binary.Core using (Rel; _Preserves_⟶_) |
| 21 |
|
| 22 |
|
| 23 |
------------------------------------------------------------------------ |
| 24 |
-- Divisibility |
| 25 |
|
| 26 |
infix 4 _∣_ |
| 27 |
|
| 28 |
_∣_ : Rel ℤ 0ℓ |
| 29 |
_∣_ = ℕ._∣_ on ∣_∣ |
| 30 |
|
| 31 |
pattern divides k eq = ℕ.divides k eq |
| 32 |
|
| 33 |
------------------------------------------------------------------------ |
| 34 |
-- Properties of divisibility |
| 35 |
|
| 36 |
*-monoʳ-∣ : ∀ k → (k *_) Preserves _∣_ ⟶ _∣_ |
| 37 |
*-monoʳ-∣ k {i} {j} i∣j = begin |
| 38 |
∣ k * i ∣ ≡⟨ abs-* k i ⟩ |
| 39 |
∣ k ∣ ℕ.* ∣ i ∣ ∣⟨ ℕ.*-monoʳ-∣ ∣ k ∣ i∣j ⟩ |
| 40 |
∣ k ∣ ℕ.* ∣ j ∣ ≡⟨ abs-* k j ⟨ |
| 41 |
∣ k * j ∣ ∎ |
| 42 |
where open ℕ.∣-Reasoning |
| 43 |
|
| 44 |
*-monoˡ-∣ : ∀ k → (_* k) Preserves _∣_ ⟶ _∣_ |
| 45 |
*-monoˡ-∣ k {i} {j} rewrite *-comm i k | *-comm j k = *-monoʳ-∣ k |
| 46 |
|
| 47 |
*-cancelˡ-∣ : ∀ k {i j} .{{_ : NonZero k}} → k * i ∣ k * j → i ∣ j |
| 48 |
*-cancelˡ-∣ k {i} {j} k*i∣k*j = ℕ.*-cancelˡ-∣ ∣ k ∣ $ begin |
| 49 |
∣ k ∣ ℕ.* ∣ i ∣ ≡⟨ abs-* k i ⟨ |
| 50 |
∣ k * i ∣ ∣⟨ k*i∣k*j ⟩ |
| 51 |
∣ k * j ∣ ≡⟨ abs-* k j ⟩ |
| 52 |
∣ k ∣ ℕ.* ∣ j ∣ ∎ |
| 53 |
where open ℕ.∣-Reasoning |
| 54 |
|
| 55 |
*-cancelʳ-∣ : ∀ k {i j} .{{_ : NonZero k}} → i * k ∣ j * k → i ∣ j |
| 56 |
*-cancelʳ-∣ k {i} {j} rewrite *-comm i k | *-comm j k = *-cancelˡ-∣ k |