| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Core definition of divisibility |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- The definition of divisibility is split out from |
| 8 |
-- `Data.Nat.Divisibility` to avoid a dependency cycle with |
| 9 |
-- `Data.Nat.DivMod`. |
| 10 |
|
| 11 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 12 |
|
| 13 |
module Data.Nat.Divisibility.Core where |
| 14 |
|
| 15 |
open import Data.Nat.Base using (ℕ; _*_; _<_; NonTrivial) |
| 16 |
open import Data.Nat.Properties using ([m*n]*[o*p]≡[m*o]*[n*p]) |
| 17 |
open import Relation.Nullary.Negation using (¬_) |
| 18 |
open import Relation.Binary.Core using (Rel) |
| 19 |
open import Relation.Binary.PropositionalEquality.Core |
| 20 |
using (_≡_; refl) |
| 21 |
|
| 22 |
|
| 23 |
private |
| 24 |
variable m n o p : ℕ |
| 25 |
|
| 26 |
------------------------------------------------------------------------ |
| 27 |
-- Main definition |
| 28 |
-- |
| 29 |
-- m ∣ n is inhabited iff m divides n. Some sources, like Hardy and |
| 30 |
-- Wright's "An Introduction to the Theory of Numbers", require m to |
| 31 |
-- be non-zero. However, some things become a bit nicer if m is |
| 32 |
-- allowed to be zero. For instance, _∣_ becomes a partial order, and |
| 33 |
-- the gcd of 0 and 0 becomes defined. |
| 34 |
|
| 35 |
infix 4 _∣_ _∤_ |
| 36 |
|
| 37 |
record _∣_ (m n : ℕ) : Set where |
| 38 |
constructor divides |
| 39 |
field quotient : ℕ |
| 40 |
equality : n ≡ quotient * m |
| 41 |
|
| 42 |
_∤_ : Rel ℕ _ |
| 43 |
m ∤ n = ¬ (m ∣ n) |
| 44 |
|
| 45 |
-- Smart constructor |
| 46 |
|
| 47 |
pattern divides-refl q = divides q refl |
| 48 |
|
| 49 |
open _∣_ using (quotient) public |
| 50 |
|
| 51 |
------------------------------------------------------------------------ |
| 52 |
-- Restricted divisor relation |
| 53 |
|
| 54 |
-- Relation for having a non-trivial divisor below a given bound. |
| 55 |
-- Useful when reasoning about primality. |
| 56 |
infix 10 _HasNonTrivialDivisorLessThan_ |
| 57 |
|
| 58 |
record _HasNonTrivialDivisorLessThan_ (m n : ℕ) : Set where |
| 59 |
constructor hasNonTrivialDivisor |
| 60 |
field |
| 61 |
{divisor} : ℕ |
| 62 |
.{{nontrivial}} : NonTrivial divisor |
| 63 |
divisor-< : divisor < n |
| 64 |
divisor-∣ : divisor ∣ m |
| 65 |
|
| 66 |
------------------------------------------------------------------------ |
| 67 |
-- DEPRECATED NAMES |
| 68 |
------------------------------------------------------------------------ |
| 69 |
-- Please use the new names as continuing support for the old names is |
| 70 |
-- not guaranteed. |
| 71 |
|
| 72 |
-- Version 2.1 |
| 73 |
|
| 74 |
*-pres-∣ : o ∣ m → p ∣ n → o * p ∣ m * n |
| 75 |
*-pres-∣ {o} {m@.(q * o)} {p} {n@.(r * p)} (divides-refl q) (divides-refl r) = |
| 76 |
divides (q * r) ([m*n]*[o*p]≡[m*o]*[n*p] q o r p) |
| 77 |
|
| 78 |
{-# WARNING_ON_USAGE *-pres-∣ |
| 79 |
"Warning: *-pres-∣ was deprecated in v2.1. |
| 80 |
Please use Data.Nat.Divisibility.*-pres-∣ instead." |
| 81 |
#-} |