forked from agda/agda-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.agda
More file actions
87 lines (65 loc) · 2.88 KB
/
Core.agda
File metadata and controls
87 lines (65 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
------------------------------------------------------------------------
-- The Agda standard library
--
-- Core definition of divisibility
------------------------------------------------------------------------
-- The definition of divisibility is split out from
-- `Data.Nat.Divisibility` to avoid a dependency cycle with
-- `Data.Nat.DivMod`.
{-# OPTIONS --cubical-compatible --safe #-}
module Data.Nat.Divisibility.Core where
open import Data.Nat.Base using (ℕ; _*_; NonZero; ≢-nonZero; ≢-nonZero⁻¹; _<_)
open import Data.Nat.Properties
open import Level using (0ℓ)
open import Relation.Nullary.Negation using (¬_; contraposition)
open import Relation.Binary.Core using (Rel)
open import Relation.Binary.PropositionalEquality
using (_≡_; refl; cong₂; module ≡-Reasoning)
------------------------------------------------------------------------
-- Definition
--
-- m ∣ n is inhabited iff m divides n. Some sources, like Hardy and
-- Wright's "An Introduction to the Theory of Numbers", require m to
-- be non-zero. However, some things become a bit nicer if m is
-- allowed to be zero. For instance, _∣_ becomes a partial order, and
-- the gcd of 0 and 0 becomes defined.
infix 4 _∣_ _∤_
record _∣_ (m n : ℕ) : Set where
constructor divides
field quotient : ℕ
equality : n ≡ quotient * m
quotient≢0 : .{{NonZero n}} → NonZero quotient
quotient≢0 rewrite equality = m*n≢0⇒m≢0 quotient
n≡m*quotient : n ≡ m * quotient
n≡m*quotient rewrite equality = *-comm quotient m
quotient>1 : (m<n : m < n) → 1 < quotient
quotient>1 m<n = ≰⇒> λ q≤1 → n≮n n (begin-strict
n ≡⟨ equality ⟩
quotient * m ≤⟨ *-monoˡ-≤ m q≤1 ⟩
1 * m ≡⟨ *-identityˡ m ⟩
m <⟨ m<n ⟩
n ∎) where open ≤-Reasoning
quotient< : (1<m : 1 < m) .{{_ : NonZero n}} → quotient < n
quotient< 1<m = begin-strict
quotient <⟨ m<m*n quotient m 1<m ⟩
quotient * m ≡⟨ equality ⟨
n ∎ where open ≤-Reasoning; instance _ = quotient≢0
_∤_ : Rel ℕ 0ℓ
m ∤ n = ¬ (m ∣ n)
-- smart constructor
pattern divides-refl q = divides q refl
-- smart destructor
module _ {m n} (m∣n : m ∣ n) (open _∣_ m∣n using (quotient; n≡m*quotient)) where
quotient∣ : quotient ∣ n
quotient∣ = divides m n≡m*quotient
-- exports
open _∣_ using (quotient; quotient≢0; quotient>1; quotient<) public
------------------------------------------------------------------------
-- Basic properties
*-pres-∣ : ∀ {m n o p} → o ∣ m → p ∣ n → o * p ∣ m * n
*-pres-∣ {m} {n} {o} {p} (divides c m≡c*o) (divides d n≡d*p) =
divides (c * d) (begin
m * n ≡⟨ cong₂ _*_ m≡c*o n≡d*p ⟩
(c * o) * (d * p) ≡⟨ [m*n]*[o*p]≡[m*o]*[n*p] c o d p ⟩
(c * d) * (o * p) ∎)
where open ≡-Reasoning