@@ -26,74 +26,74 @@ open import Relation.Binary.PropositionalEquality
26
26
-- Definition
27
27
28
28
infixl 7 _divℕ_ _div_ _modℕ_ _mod_
29
- _divℕ_ : (dividend : ℤ) (divisor : ℕ) {≢0 : False (divisor ℕ.≟ 0 ) } → ℤ
30
- (+ n divℕ d) {d≠0} = + (n NDM./ d) {d≠0}
31
- (-[1+ n ] divℕ d) {d≠0} with (ℕ.suc n NDM.divMod d) {d≠0}
29
+ _divℕ_ : (dividend : ℤ) (divisor : ℕ) .{{_ : ℕ.NonZero divisor} } → ℤ
30
+ (+ n divℕ d) = + (n NDM./ d)
31
+ (-[1+ n ] divℕ d) with (ℕ.suc n NDM.divMod d)
32
32
... | NDM.result q Fin.zero eq = - (+ q)
33
33
... | NDM.result q (Fin.suc r) eq = -[1+ q ]
34
34
35
- _div_ : (dividend divisor : ℤ) {≢0 : False (∣ divisor ∣ ℕ.≟ 0 ) } → ℤ
36
- ( n div d) {d≢0} = (sign d ◃ 1 ) ℤ.* (n divℕ ∣ d ∣) {d≢0}
35
+ _div_ : (dividend divisor : ℤ) .{{_ : NonZero divisor} } → ℤ
36
+ n div d = (sign d ◃ 1 ) ℤ.* (n divℕ ∣ d ∣)
37
37
38
- _modℕ_ : (dividend : ℤ) (divisor : ℕ) {≢0 : False (divisor ℕ.≟ 0 ) } → ℕ
39
- (+ n modℕ d) {d≠0} = ( n NDM.% d) {d≠0}
40
- (-[1+ n ] modℕ d) {d≠0} with ( ℕ.suc n NDM.divMod d) {d≠0}
38
+ _modℕ_ : (dividend : ℤ) (divisor : ℕ) .{{_ : ℕ.NonZero divisor} } → ℕ
39
+ (+ n modℕ d) = n NDM.% d
40
+ (-[1+ n ] modℕ d) with ℕ.suc n NDM.divMod d
41
41
... | NDM.result q Fin.zero eq = 0
42
42
... | NDM.result q (Fin.suc r) eq = d ℕ.∸ ℕ.suc (Fin.toℕ r)
43
43
44
- _mod_ : (dividend divisor : ℤ) {≢0 : False (∣ divisor ∣ ℕ.≟ 0 ) } → ℕ
45
- ( n mod d) {d≢0} = ( n modℕ ∣ d ∣) {d≢0}
44
+ _mod_ : (dividend divisor : ℤ) .{{_ : NonZero divisor} } → ℕ
45
+ n mod d = n modℕ ∣ d ∣
46
46
47
47
------------------------------------------------------------------------
48
48
-- Properties
49
49
50
- n%ℕd<d : ∀ n d {d≢0} → ( n modℕ d) {d≢0} ℕ.< d
51
- n%ℕd<d (+ n) sd@(ℕ.suc d) = NDM.m%n<n n d
52
- n%ℕd<d -[1+ n ] sd@(ℕ.suc d) with ℕ.suc n NDM.divMod sd
50
+ n%ℕd<d : ∀ n d .{{_ : ℕ.NonZero d}} → n modℕ d ℕ.< d
51
+ n%ℕd<d (+ n) d = NDM.m%n<n n d
52
+ n%ℕd<d -[1+ n ] d with ℕ.suc n NDM.divMod d
53
53
... | NDM.result q Fin.zero eq = ℕ.s≤s ℕ.z≤n
54
- ... | NDM.result q (Fin.suc r) eq = ℕ.s≤s (NProp.m∸n≤m d (Fin.toℕ r))
54
+ ... | NDM.result q (Fin.suc r) eq = ℕ.s≤s (NProp.m∸n≤m _ (Fin.toℕ r))
55
55
56
- n%d<d : ∀ n d {d≢0} → ( n mod d) {d≢0} ℕ.< ℤ.∣ d ∣
57
- n%d<d n (+ ℕ.suc d) = n%ℕd<d n (ℕ.suc d)
58
- n%d<d n -[1+ d ] = n%ℕd<d n (ℕ.suc d)
56
+ n%d<d : ∀ n d .{{_ : NonZero d}} → n mod d ℕ.< ℤ.∣ d ∣
57
+ n%d<d n (+ d) = n%ℕd<d n d
58
+ n%d<d n -[1+ d ] = n%ℕd<d n (ℕ.suc d)
59
59
60
- a≡a%ℕn+[a/ℕn]*n : ∀ n d {d≢0} → n ≡ + (n modℕ d) {d≢0} + (n divℕ d) {d≢0} * + d
61
- a≡a%ℕn+[a/ℕn]*n (+ n) sd@(ℕ.suc d) = let q = n NDM./ sd ; r = n NDM.% sd in begin
60
+ a≡a%ℕn+[a/ℕn]*n : ∀ n d .{{_ : ℕ.NonZero d}} → n ≡ + (n modℕ d) + (n divℕ d) * + d
61
+ a≡a%ℕn+[a/ℕn]*n (+ n) d = let q = n NDM./ d ; r = n NDM.% d in begin
62
62
+ n ≡⟨ cong +_ (NDM.m≡m%n+[m/n]*n n d) ⟩
63
- + (r ℕ.+ q ℕ.* sd) ≡⟨ pos-+-commute r (q ℕ.* sd ) ⟩
64
- + r + + (q ℕ.* sd) ≡⟨ cong (_+_ (+ (+ n modℕ sd ))) (sym (pos-distrib-* q sd )) ⟩
65
- + r + + q * + sd ∎ where open ≡-Reasoning
66
- a≡a%ℕn+[a/ℕn]*n -[1+ n ] sd@(ℕ.suc d) with (ℕ.suc n) NDM.divMod (ℕ.suc d)
63
+ + (r ℕ.+ q ℕ.* d) ≡⟨ pos-+-commute r (q ℕ.* d ) ⟩
64
+ + r + + (q ℕ.* d) ≡⟨ cong (_+_ (+ (+ n modℕ d ))) (sym (pos-distrib-* q d )) ⟩
65
+ + r + + q * + d ∎ where open ≡-Reasoning
66
+ a≡a%ℕn+[a/ℕn]*n -[1+ n ] d with (ℕ.suc n) NDM.divMod d
67
67
... | NDM.result q Fin.zero eq = begin
68
68
-[1+ n ] ≡⟨ cong (-_ ∘′ +_) eq ⟩
69
- - + (q ℕ.* sd ) ≡⟨ cong -_ (sym (pos-distrib-* q sd )) ⟩
70
- - (+ q * + sd ) ≡⟨ neg-distribˡ-* (+ q) (+ sd ) ⟩
71
- - (+ q) * + sd ≡⟨ sym (+-identityˡ (- (+ q) * + sd )) ⟩
72
- + 0 + - (+ q) * + sd ∎ where open ≡-Reasoning
69
+ - + (q ℕ.* d ) ≡⟨ cong -_ (sym (pos-distrib-* q d )) ⟩
70
+ - (+ q * + d ) ≡⟨ neg-distribˡ-* (+ q) (+ d ) ⟩
71
+ - (+ q) * + d ≡⟨ sym (+-identityˡ (- (+ q) * + d )) ⟩
72
+ + 0 + - (+ q) * + d ∎ where open ≡-Reasoning
73
73
... | NDM.result q (Fin.suc r) eq = begin
74
- let sd = ℕ.suc d; sr = ℕ.suc (Fin.toℕ r); sq = ℕ.suc q in
74
+ let sr = ℕ.suc (Fin.toℕ r); sq = ℕ.suc q in
75
75
-[1+ n ]
76
76
≡⟨ cong (-_ ∘′ +_) eq ⟩
77
- - + (sr ℕ.+ q ℕ.* sd )
78
- ≡⟨ cong -_ (pos-+-commute sr (q ℕ.* sd )) ⟩
79
- - (+ sr + + (q ℕ.* sd ))
80
- ≡⟨ neg-distrib-+ (+ sr) (+ (q ℕ.* sd )) ⟩
81
- - + sr - + (q ℕ.* sd )
82
- ≡⟨ cong (_-_ (- + sr)) (sym (pos-distrib-* q sd )) ⟩
83
- - + sr - (+ q) * (+ sd )
77
+ - + (sr ℕ.+ q ℕ.* d )
78
+ ≡⟨ cong -_ (pos-+-commute sr (q ℕ.* d )) ⟩
79
+ - (+ sr + + (q ℕ.* d ))
80
+ ≡⟨ neg-distrib-+ (+ sr) (+ (q ℕ.* d )) ⟩
81
+ - + sr - + (q ℕ.* d )
82
+ ≡⟨ cong (_-_ (- + sr)) (sym (pos-distrib-* q d )) ⟩
83
+ - + sr - (+ q) * (+ d )
84
84
≡⟨⟩
85
- - + sr - pred (+ sq) * (+ sd )
86
- ≡⟨ cong (_-_ (- + sr)) (*-distribʳ-+ (+ sd ) (- + 1 ) (+ sq)) ⟩
87
- - + sr - (- (+ 1 ) * + sd + (+ sq * + sd ))
88
- ≡⟨ cong (_+_ (- (+ sr))) (neg-distrib-+ (- (+ 1 ) * + sd ) (+ sq * + sd )) ⟩
89
- - + sr + (- (-[1+ 0 ] * + sd ) + - (+ sq * + sd ))
90
- ≡⟨ cong₂ (λ p q → - + sr + (- p + q)) (-1*n≡-n (+ sd ))
91
- (neg-distribˡ-* (+ sq) (+ sd )) ⟩
92
- - + sr + ((- - + sd ) + -[1+ q ] * + sd )
93
- ≡⟨ sym (+-assoc (- + sr) (- - + sd ) (-[1+ q ] * + sd )) ⟩
94
- (+ sd - + sr) + -[1+ q ] * + sd
95
- ≡⟨ cong (_+ -[1+ q ] * + sd ) (fin-inv d r) ⟩
96
- + (sd ℕ.∸ sr) + -[1+ q ] * + sd
85
+ - + sr - pred (+ sq) * (+ d )
86
+ ≡⟨ cong (_-_ (- + sr)) (*-distribʳ-+ (+ d ) (- + 1 ) (+ sq)) ⟩
87
+ - + sr - (- (+ 1 ) * + d + (+ sq * + d ))
88
+ ≡⟨ cong (_+_ (- (+ sr))) (neg-distrib-+ (- (+ 1 ) * + d ) (+ sq * + d )) ⟩
89
+ - + sr + (- (-[1+ 0 ] * + d ) + - (+ sq * + d ))
90
+ ≡⟨ cong₂ (λ p q → - + sr + (- p + q)) (-1*n≡-n (+ d ))
91
+ (neg-distribˡ-* (+ sq) (+ d )) ⟩
92
+ - + sr + ((- - + d ) + -[1+ q ] * + d )
93
+ ≡⟨ sym (+-assoc (- + sr) (- - + d ) (-[1+ q ] * + d )) ⟩
94
+ (+ d - + sr) + -[1+ q ] * + d
95
+ ≡⟨ cong (_+ -[1+ q ] * + d ) (fin-inv _ r) ⟩
96
+ + (d ℕ.∸ sr) + -[1+ q ] * + d
97
97
∎ where
98
98
99
99
open ≡-Reasoning
@@ -104,52 +104,52 @@ a≡a%ℕn+[a/ℕn]*n -[1+ n ] sd@(ℕ.suc d) with (ℕ.suc n) NDM.divMod (ℕ.s
104
104
ℕ.suc d ⊖ ℕ.suc (Fin.toℕ k) ≡⟨ ⊖-≥ (ℕ.s≤s (FProp.toℕ≤n k)) ⟩
105
105
+ (d ℕ.∸ Fin.toℕ k) ∎ where open ≡-Reasoning
106
106
107
- [n/ℕd]*d≤n : ∀ n d {d≢0} → (n divℕ d) {d≢0} ℤ.* ℤ.+ d ℤ.≤ n
107
+ [n/ℕd]*d≤n : ∀ n d .{{_ : ℕ.NonZero d}} → (n divℕ d) ℤ.* ℤ.+ d ℤ.≤ n
108
108
[n/ℕd]*d≤n n (ℕ.suc d) = let q = n divℕ ℕ.suc d; r = n modℕ ℕ.suc d in begin
109
109
q ℤ.* ℤ.+ (ℕ.suc d) ≤⟨ n≤m+n r ⟩
110
110
ℤ.+ r ℤ.+ q ℤ.* ℤ.+ (ℕ.suc d) ≡⟨ sym (a≡a%ℕn+[a/ℕn]*n n (ℕ.suc d)) ⟩
111
111
n ∎ where open ≤-Reasoning
112
112
113
- div-pos-is-divℕ : ∀ n d {d≢0} → ( n div + d) {d≢0} ≡ ( n divℕ d) {d≢0}
113
+ div-pos-is-divℕ : ∀ n d .{{_ : ℕ.NonZero d}} → n div ( + d) ≡ n divℕ d
114
114
div-pos-is-divℕ n (ℕ.suc d) = *-identityˡ (n divℕ ℕ.suc d)
115
115
116
- div-neg-is-neg-divℕ : ∀ n d {d≢0} {∣d∣≢0} → ( n div (- ℤ. + d)) {∣d∣≢0} ≡ - (n divℕ d) {d≢0}
116
+ div-neg-is-neg-divℕ : ∀ n d .{{_ : ℕ.NonZero d}} .{{_ : NonZero (- + d)}} → n div (- + d) ≡ - (n divℕ d)
117
117
div-neg-is-neg-divℕ n (ℕ.suc d) = -1*n≡-n (n divℕ ℕ.suc d)
118
118
119
- 0≤n⇒0≤n/ℕd : ∀ n d {d≢0} → + 0 ℤ.≤ n → + 0 ℤ.≤ (n divℕ d) {d≢0}
119
+ 0≤n⇒0≤n/ℕd : ∀ n d .{{_ : ℕ.NonZero d}} → +0 ℤ.≤ n → +0 ℤ.≤ (n divℕ d)
120
120
0≤n⇒0≤n/ℕd (+ n) d (+≤+ m≤n) = +≤+ ℕ.z≤n
121
121
122
- 0≤n⇒0≤n/d : ∀ n d {d≢0} → + 0 ℤ.≤ n → + 0 ℤ.≤ d → + 0 ℤ.≤ (n div d) {d≢0}
123
- 0≤n⇒0≤n/d n (+ d) {d≢0} 0≤n (+≤+ 0≤d)
124
- rewrite div-pos-is-divℕ n d {d≢0}
125
- = 0≤n⇒0≤n/ℕd n d {d≢0} 0≤n
126
-
127
- [n/d]*d≤n : ∀ n d {d≢0} → (n div d) {d≢0} ℤ.* d ℤ.≤ n
128
- [n/d]*d≤n n (+ ℕ.suc d) = begin let sd = ℕ.suc d in
129
- n div + sd * + sd ≡⟨ cong (_* (+ sd )) (div-pos-is-divℕ n sd ) ⟩
130
- n divℕ sd * + sd ≤⟨ [n/ℕd]*d≤n n sd ⟩
131
- n ∎ where open ≤-Reasoning
132
- [n/d]*d≤n n -[1+ d ] = begin let sd = ℕ.suc d in
133
- n div (- + sd ) * - + sd ≡⟨ cong (_* (- + sd )) (div-neg-is-neg-divℕ n sd ) ⟩
134
- - (n divℕ sd) * - + sd ≡⟨ sym (neg-distribˡ-* (n divℕ sd ) (- + sd )) ⟩
135
- - (n divℕ sd * - + sd) ≡⟨ neg-distribʳ-* (n divℕ sd ) (- + sd ) ⟩
136
- n divℕ sd * + sd ≤⟨ [n/ℕd]*d≤n n sd ⟩
137
- n ∎ where open ≤-Reasoning
138
-
139
- n<s[n/ℕd]*d : ∀ n d {d≢0} → n ℤ.< suc (( n divℕ d) {d≢0} ) ℤ.* ℤ.+ d
140
- n<s[n/ℕd]*d n sd@(ℕ.suc d) = begin-strict
141
- n ≡⟨ a≡a%ℕn+[a/ℕn]*n n sd ⟩
142
- ℤ. + r ℤ. + q ℤ. * + sd <⟨ +-monoˡ-< (q ℤ. * + sd ) (ℤ.+<+ (n%ℕd<d n sd )) ⟩
143
- + sd ℤ. + q ℤ. * + sd ≡⟨ sym (suc-* q (+ sd )) ⟩
144
- suc (n divℕ ℕ.suc d) * + sd ∎ where
145
- open ≤-Reasoning ; q = n divℕ sd ; r = n modℕ sd
146
-
147
- a≡a%n+[a/n]*n : ∀ a n {≢0} → a ≡ + (a mod n) {≢0} + (a div n) {≢0} * n
148
- a≡a%n+[a/n]*n n (+ ℕ.suc d) = begin
149
- let sd = ℕ.suc d; r = n modℕ sd ; q = n divℕ sd ; qsd = q * + sd in
150
- n ≡⟨ a≡a%ℕn+[a/ℕn]*n n sd ⟩
151
- + r + qsd ≡⟨ cong (λ p → + r + p * + sd ) (sym (div-pos-is-divℕ n sd )) ⟩
152
- + r + n div + sd * + sd ∎ where open ≡-Reasoning
122
+ 0≤n⇒0≤n/d : ∀ n d .{{_ : NonZero d}} → +0 ℤ.≤ n → +0 ℤ.≤ d → +0 ℤ.≤ (n div d)
123
+ 0≤n⇒0≤n/d n (+ d) {{ d≢0} } 0≤n (+≤+ 0≤d)
124
+ rewrite div-pos-is-divℕ n d {{ d≢0} }
125
+ = 0≤n⇒0≤n/ℕd n d 0≤n
126
+
127
+ [n/d]*d≤n : ∀ n d .{{_ : NonZero d}} → (n div d) ℤ.* d ℤ.≤ n
128
+ [n/d]*d≤n n (+ d) = begin
129
+ n div + d * + d ≡⟨ cong (_* (+ d )) (div-pos-is-divℕ n d ) ⟩
130
+ n divℕ d * + d ≤⟨ [n/ℕd]*d≤n n d ⟩
131
+ n ∎ where open ≤-Reasoning
132
+ [n/d]*d≤n n -[1+ d-1 ] = begin let d = ℕ.suc d-1 in
133
+ n div (- + d ) * - + d ≡⟨ cong (_* (- + d )) (div-neg-is-neg-divℕ n d ) ⟩
134
+ - (n divℕ d) * - + d ≡⟨ sym (neg-distribˡ-* (n divℕ d ) (- + d )) ⟩
135
+ - (n divℕ d * - + d) ≡⟨ neg-distribʳ-* (n divℕ d ) (- + d ) ⟩
136
+ n divℕ d * + d ≤⟨ [n/ℕd]*d≤n n d ⟩
137
+ n ∎ where open ≤-Reasoning
138
+
139
+ n<s[n/ℕd]*d : ∀ n d .{{_ : ℕ.NonZero d}} → n ℤ.< suc (n divℕ d) ℤ.* ℤ.+ d
140
+ n<s[n/ℕd]*d n d = begin-strict
141
+ n ≡⟨ a≡a%ℕn+[a/ℕn]*n n d ⟩
142
+ + r + q * + d <⟨ +-monoˡ-< (q * + d ) (ℤ.+<+ (n%ℕd<d n d )) ⟩
143
+ + d + q * + d ≡⟨ sym (suc-* q (+ d )) ⟩
144
+ suc (n divℕ d) * + d ∎ where
145
+ open ≤-Reasoning ; q = n divℕ d ; r = n modℕ d
146
+
147
+ a≡a%n+[a/n]*n : ∀ a n .{{_ : NonZero n}} → a ≡ + (a mod n) + (a div n) * n
148
+ a≡a%n+[a/n]*n n (+ d) = begin
149
+ let r = n modℕ d ; q = n divℕ d ; qsd = q * + d in
150
+ n ≡⟨ a≡a%ℕn+[a/ℕn]*n n d ⟩
151
+ + r + qsd ≡⟨ cong (λ p → + r + p * + d ) (sym (div-pos-is-divℕ n d )) ⟩
152
+ + r + n div + d * + d ∎ where open ≡-Reasoning
153
153
a≡a%n+[a/n]*n n -[1+ d ] = begin
154
154
let sd = ℕ.suc d; r = n modℕ sd; q = n divℕ sd; qsd = q * + sd in
155
155
n ≡⟨ a≡a%ℕn+[a/ℕn]*n n sd ⟩
0 commit comments