Skip to content

Commit d9aed5c

Browse files
Remove uses of Data.Nat.Solver from a number of places (#2337)
* tighten imports in some files, and make imports explicit in others. Driven by staring at a partial dependency graph. * more tightening of imports * and even more tightening of imports * some explicit for precision * new Nat Lemmas to use instead of Solve in Data.Integer.Properties * use direct proofs instead of solver for all Nat proofs in these files * two more uses of Data.Nat.Solver for rather simple proofs, now made direct. * fix whitespace violations * remove some unneeded parens * minor fixups based on review
1 parent e0d58fc commit d9aed5c

File tree

6 files changed

+286
-141
lines changed

6 files changed

+286
-141
lines changed

src/Data/Digit.agda

+33-17
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@
99
module Data.Digit where
1010

1111
open import Data.Nat.Base
12-
open import Data.Nat.Properties using (_≤?_; _<?_; ≤⇒≤′; module ≤-Reasoning; m≤m+n)
13-
open import Data.Nat.Solver using (module +-*-Solver)
12+
using (ℕ; zero; suc; _<_; _/_; _%_; sz<ss; _+_; _*_; 2+; _≤′_)
13+
open import Data.Nat.Properties
14+
using (_≤?_; _<?_; ≤⇒≤′; module ≤-Reasoning; m≤m+n; +-comm; +-assoc;
15+
*-distribˡ-+; *-identityʳ)
1416
open import Data.Fin.Base as Fin using (Fin; zero; suc; toℕ)
1517
open import Data.Bool.Base using (Bool; true; false)
1618
open import Data.Char.Base using (Char)
17-
open import Data.List.Base
19+
open import Data.List.Base using (List; replicate; [_]; _∷_; [])
1820
open import Data.Product.Base using (∃; _,_)
1921
open import Data.Vec.Base as Vec using (Vec; _∷_; [])
20-
open import Data.Nat.DivMod
22+
open import Data.Nat.DivMod using (m/n<m; _divMod_; result)
2123
open import Data.Nat.Induction
24+
using (Acc; acc; <-wellFounded-fast; <′-Rec; <′-rec)
25+
open import Function.Base using (_$_)
2226
open import Relation.Nullary.Decidable using (True; does; toWitness)
2327
open import Relation.Binary.Definitions using (Decidable)
24-
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; sym; cong)
25-
open import Function.Base using (_$_)
28+
open import Relation.Binary.PropositionalEquality
29+
using (_≡_; refl; sym; cong; cong₂; module ≡-Reasoning)
2630

2731
------------------------------------------------------------------------
2832
-- Digits
@@ -87,20 +91,32 @@ toDigits base@(suc (suc k)) n = <′-rec Pred helper n
8791
cons : {m} (r : Digit base) Pred m Pred (toℕ r + m * base)
8892
cons r (ds , eq) = (r ∷ ds , cong (λ i toℕ r + i * base) eq)
8993

90-
open ≤-Reasoning
91-
open +-*-Solver
94+
lem′ : x k x + x + (k + x * k) ≡ k + x * 2+ k
95+
lem′ x k = begin
96+
x + x + (k + x * k) ≡⟨ +-assoc (x + x) k _ ⟨
97+
x + x + k + x * k ≡⟨ cong (_+ x * k) (+-comm _ k) ⟩
98+
k + (x + x) + x * k ≡⟨ +-assoc k (x + x) _ ⟩
99+
k + ((x + x) + x * k) ≡⟨ cong (k +_) (begin
100+
(x + x) + x * k ≡⟨ +-assoc x x _ ⟩
101+
x + (x + x * k) ≡⟨ cong (x +_) (cong (_+ x * k) (*-identityʳ x)) ⟨
102+
x + (x * 1 + x * k) ≡⟨ cong₂ _+_ (*-identityʳ x) (*-distribˡ-+ x 1 k ) ⟨
103+
x * 1 + (x * suc k) ≡⟨ *-distribˡ-+ x 1 (1 + k) ⟨
104+
x * 2+ k ∎) ⟩
105+
k + x * 2+ k ∎
106+
where open ≡-Reasoning
92107

93108
lem : x k r 2 + x ≤′ r + (1 + x) * (2 + k)
94109
lem x k r = ≤⇒≤′ $ begin
95-
2 + x
96-
≤⟨ m≤m+n _ _ ⟩
97-
2 + x + (x + (1 + x) * k + r)
98-
≡⟨ solve 3 (λ x r k con 2 :+ x :+ (x :+ (con 1 :+ x) :* k :+ r)
99-
:=
100-
r :+ (con 1 :+ x) :* (con 2 :+ k))
101-
refl x r k ⟩
102-
r + (1 + x) * (2 + k)
103-
110+
2 + x ≤⟨ m≤m+n _ _ ⟩
111+
2 + x + (x + (1 + x) * k + r) ≡⟨ cong ((2 + x) +_) (+-comm _ r) ⟩
112+
2 + x + (r + (x + (1 + x) * k)) ≡⟨ +-assoc (2 + x) r _ ⟨
113+
2 + x + r + (x + (1 + x) * k) ≡⟨ cong (_+ (x + (1 + x) * k)) (+-comm (2 + x) r) ⟩
114+
r + (2 + x) + (x + (1 + x) * k) ≡⟨ +-assoc r (2 + x) _ ⟩
115+
r + ((2 + x) + (x + (1 + x) * k)) ≡⟨ cong (r +_) (cong 2+ (+-assoc x x _)) ⟨
116+
r + (2 + (x + x + (1 + x) * k)) ≡⟨ cong (λ z r + (2+ z)) (lem′ x k) ⟩
117+
r + (2 + (k + x * (2 + k))) ≡⟨⟩
118+
r + (1 + x) * (2 + k) ∎
119+
where open ≤-Reasoning
104120

105121
helper : n <′-Rec Pred n Pred n
106122
helper n rec with n divMod base

src/Data/Fin/Properties.agda

+10-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
------------------------------------------------------------------------
77

88
{-# OPTIONS --cubical-compatible --safe #-}
9-
{-# OPTIONS --warn=noUserWarning #-} -- for deprecated _≺_ (issue #1726)
9+
{-# OPTIONS --warn=noUserWarning #-} -- for deprecated _≺_ and _≻toℕ_ (issue #1726)
1010

1111
module Data.Fin.Properties where
1212

@@ -21,7 +21,6 @@ open import Data.Fin.Patterns
2121
open import Data.Nat.Base as ℕ
2222
using (ℕ; zero; suc; s≤s; z≤n; z<s; s<s; s<s⁻¹; _∸_; _^_)
2323
import Data.Nat.Properties as ℕ
24-
open import Data.Nat.Solver
2524
open import Data.Unit.Base using (⊤; tt)
2625
open import Data.Product.Base as Product
2726
using (∃; ∃₂; _×_; _,_; map; proj₁; proj₂; uncurry; <_,_>)
@@ -669,12 +668,14 @@ toℕ-combine {suc m} {n} i@0F j = begin
669668
n ℕ.* toℕ i ℕ.+ toℕ j ∎
670669
where open ≡-Reasoning
671670
toℕ-combine {suc m} {n} (suc i) j = begin
672-
toℕ (combine (suc i) j) ≡⟨⟩
673-
toℕ (n ↑ʳ combine i j) ≡⟨ toℕ-↑ʳ n (combine i j) ⟩
674-
n ℕ.+ toℕ (combine i j) ≡⟨ cong (n ℕ.+_) (toℕ-combine i j) ⟩
675-
n ℕ.+ (n ℕ.* toℕ i ℕ.+ toℕ j) ≡⟨ solve 3 (λ n i j n :+ (n :* i :+ j) := n :* (con 1 :+ i) :+ j) refl n (toℕ i) (toℕ j) ⟩
676-
n ℕ.* toℕ (suc i) ℕ.+ toℕ j ∎
677-
where open ≡-Reasoning; open +-*-Solver
671+
toℕ (combine (suc i) j) ≡⟨⟩
672+
toℕ (n ↑ʳ combine i j) ≡⟨ toℕ-↑ʳ n (combine i j) ⟩
673+
n ℕ.+ toℕ (combine i j) ≡⟨ cong (n ℕ.+_) (toℕ-combine i j) ⟩
674+
n ℕ.+ (n ℕ.* toℕ i ℕ.+ toℕ j) ≡⟨ ℕ.+-assoc n _ (toℕ j) ⟨
675+
n ℕ.+ n ℕ.* toℕ i ℕ.+ toℕ j ≡⟨ cong (λ z z ℕ.+ n ℕ.* toℕ i ℕ.+ toℕ j) (ℕ.*-identityʳ n) ⟨
676+
n ℕ.* 1 ℕ.+ n ℕ.* toℕ i ℕ.+ toℕ j ≡⟨ cong (ℕ._+ toℕ j) (ℕ.*-distribˡ-+ n 1 (toℕ i) ) ⟨
677+
n ℕ.* toℕ (suc i) ℕ.+ toℕ j ∎
678+
where open ≡-Reasoning
678679

679680
combine-monoˡ-< : {i j : Fin m} (k l : Fin n)
680681
i < j combine i k < combine j l
@@ -688,7 +689,7 @@ combine-monoˡ-< {m} {n} {i} {j} k l i<j = begin-strict
688689
n ℕ.* toℕ j ≤⟨ ℕ.m≤m+n (n ℕ.* toℕ j) (toℕ l) ⟩
689690
n ℕ.* toℕ j ℕ.+ toℕ l ≡⟨ toℕ-combine j l ⟨
690691
toℕ (combine j l) ∎
691-
where open ℕ.≤-Reasoning; open +-*-Solver
692+
where open ℕ.≤-Reasoning
692693

693694
combine-injectiveˡ : (i : Fin m) (j : Fin n) (k : Fin m) (l : Fin n)
694695
combine i j ≡ combine k l i ≡ k

src/Data/Integer/Properties.agda

+13-39
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import Algebra.Lattice.Construct.NaturalChoice.MinMaxOp as LatticeMinMaxOp
1616
import Algebra.Properties.AbelianGroup
1717
open import Data.Bool.Base using (T; true; false)
1818
open import Data.Integer.Base renaming (suc to sucℤ)
19+
open import Data.Integer.Properties.NatLemmas
1920
open import Data.Nat.Base as ℕ
2021
using (ℕ; suc; zero; _∸_; s≤s; z≤n; s<s; z<s; s≤s⁻¹; s<s⁻¹)
2122
hiding (module )
2223
import Data.Nat.Properties as ℕ
23-
open import Data.Nat.Solver
2424
open import Data.Product.Base using (proj₁; proj₂; _,_; _×_)
2525
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]′)
2626
open import Data.Sign.Base as Sign using (Sign)
@@ -45,7 +45,6 @@ open import Algebra.Consequences.Propositional
4545
open import Algebra.Structures {A = ℤ} _≡_
4646
module ℤtoℕ = Morphism.Definitions ℤ ℕ _≡_
4747
module ℕtoℤ = Morphism.Definitions ℕ ℤ _≡_
48-
open +-*-Solver
4948

5049
private
5150
variable
@@ -1337,15 +1336,6 @@ pred-mono (+≤+ m≤n) = ⊖-monoˡ-≤ 1 m≤n
13371336
*-zero : Zero 0ℤ _*_
13381337
*-zero = *-zeroˡ , *-zeroʳ
13391338

1340-
private
1341-
lemma : m n o o ℕ.+ (n ℕ.+ m ℕ.* suc n) ℕ.* suc o
1342-
≡ o ℕ.+ n ℕ.* suc o ℕ.+ m ℕ.* suc (o ℕ.+ n ℕ.* suc o)
1343-
lemma =
1344-
solve 3 (λ m n o o :+ (n :+ m :* (con 1 :+ n)) :* (con 1 :+ o)
1345-
:= o :+ n :* (con 1 :+ o) :+
1346-
m :* (con 1 :+ (o :+ n :* (con 1 :+ o))))
1347-
refl
1348-
13491339
*-assoc : Associative _*_
13501340
*-assoc +0 _ _ = refl
13511341
*-assoc i +0 _ rewrite ℕ.*-zeroʳ ∣ i ∣ = refl
@@ -1354,14 +1344,14 @@ private
13541344
| ℕ.*-zeroʳ ∣ i ∣
13551345
| ℕ.*-zeroʳ ∣ sign i Sign.* sign j ◃ ∣ i ∣ ℕ.* ∣ j ∣ ∣
13561346
= refl
1357-
*-assoc -[1+ m ] -[1+ n ] +[1+ o ] = cong (+_ ∘ suc) (lemma m n o)
1358-
*-assoc -[1+ m ] +[1+ n ] -[1+ o ] = cong (+_ ∘ suc) (lemma m n o)
1359-
*-assoc +[1+ m ] +[1+ n ] +[1+ o ] = cong (+_ ∘ suc) (lemma m n o)
1360-
*-assoc +[1+ m ] -[1+ n ] -[1+ o ] = cong (+_ ∘ suc) (lemma m n o)
1361-
*-assoc -[1+ m ] -[1+ n ] -[1+ o ] = cong -[1+_] (lemma m n o)
1362-
*-assoc -[1+ m ] +[1+ n ] +[1+ o ] = cong -[1+_] (lemma m n o)
1363-
*-assoc +[1+ m ] -[1+ n ] +[1+ o ] = cong -[1+_] (lemma m n o)
1364-
*-assoc +[1+ m ] +[1+ n ] -[1+ o ] = cong -[1+_] (lemma m n o)
1347+
*-assoc -[1+ m ] -[1+ n ] +[1+ o ] = cong (+_ ∘ suc) (inner-assoc m n o)
1348+
*-assoc -[1+ m ] +[1+ n ] -[1+ o ] = cong (+_ ∘ suc) (inner-assoc m n o)
1349+
*-assoc +[1+ m ] +[1+ n ] +[1+ o ] = cong (+_ ∘ suc) (inner-assoc m n o)
1350+
*-assoc +[1+ m ] -[1+ n ] -[1+ o ] = cong (+_ ∘ suc) (inner-assoc m n o)
1351+
*-assoc -[1+ m ] -[1+ n ] -[1+ o ] = cong -[1+_] (inner-assoc m n o)
1352+
*-assoc -[1+ m ] +[1+ n ] +[1+ o ] = cong -[1+_] (inner-assoc m n o)
1353+
*-assoc +[1+ m ] -[1+ n ] +[1+ o ] = cong -[1+_] (inner-assoc m n o)
1354+
*-assoc +[1+ m ] +[1+ n ] -[1+ o ] = cong -[1+_] (inner-assoc m n o)
13651355

13661356
private
13671357

@@ -1400,26 +1390,10 @@ private
14001390
rewrite +-identityʳ y
14011391
| +-identityʳ (sign y Sign.* sign x ◃ ∣ y ∣ ℕ.* ∣ x ∣)
14021392
= refl
1403-
*-distribʳ-+ -[1+ m ] -[1+ n ] -[1+ o ] = cong (+_) $
1404-
solve 3 (λ m n o (con 2 :+ n :+ o) :* (con 1 :+ m)
1405-
:= (con 1 :+ n) :* (con 1 :+ m) :+
1406-
(con 1 :+ o) :* (con 1 :+ m))
1407-
refl m n o
1408-
*-distribʳ-+ +[1+ m ] +[1+ n ] +[1+ o ] = cong (+_) $
1409-
solve 3 (λ m n o (con 1 :+ n :+ (con 1 :+ o)) :* (con 1 :+ m)
1410-
:= (con 1 :+ n) :* (con 1 :+ m) :+
1411-
(con 1 :+ o) :* (con 1 :+ m))
1412-
refl m n o
1413-
*-distribʳ-+ -[1+ m ] +[1+ n ] +[1+ o ] = cong -[1+_] $
1414-
solve 3 (λ m n o m :+ (n :+ (con 1 :+ o)) :* (con 1 :+ m)
1415-
:= (con 1 :+ n) :* (con 1 :+ m) :+
1416-
(m :+ o :* (con 1 :+ m)))
1417-
refl m n o
1418-
*-distribʳ-+ +[1+ m ] -[1+ n ] -[1+ o ] = cong -[1+_] $
1419-
solve 3 (λ m n o m :+ (con 1 :+ m :+ (n :+ o) :* (con 1 :+ m))
1420-
:= (con 1 :+ n) :* (con 1 :+ m) :+
1421-
(m :+ o :* (con 1 :+ m)))
1422-
refl m n o
1393+
*-distribʳ-+ -[1+ m ] -[1+ n ] -[1+ o ] = cong (+_) $ assoc₁ m n o
1394+
*-distribʳ-+ +[1+ m ] +[1+ n ] +[1+ o ] = cong +[1+_] $ ℕ.suc-injective (assoc₂ m n o)
1395+
*-distribʳ-+ -[1+ m ] +[1+ n ] +[1+ o ] = cong -[1+_] $ assoc₃ m n o
1396+
*-distribʳ-+ +[1+ m ] -[1+ n ] -[1+ o ] = cong -[1+_] $ assoc₄ m n o
14231397
*-distribʳ-+ -[1+ m ] -[1+ n ] +[1+ o ] = begin
14241398
(suc o ⊖ suc n) * -[1+ m ] ≡⟨ cong (_* -[1+ m ]) ([1+m]⊖[1+n]≡m⊖n o n) ⟩
14251399
(o ⊖ n) * -[1+ m ] ≡⟨ distrib-lemma m n o ⟩
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Some extra lemmas about natural numbers only needed for
5+
-- Data.Integer.Properties (for distributivity)
6+
------------------------------------------------------------------------
7+
8+
{-# OPTIONS --cubical-compatible --safe #-}
9+
10+
module Data.Integer.Properties.NatLemmas where
11+
12+
open import Data.Nat.Base using (ℕ; _+_; _*_; suc)
13+
open import Data.Nat.Properties
14+
using (*-distribʳ-+; *-assoc; +-assoc; +-comm; +-suc)
15+
open import Function.Base using (_∘_)
16+
open import Relation.Binary.PropositionalEquality
17+
using (_≡_; cong; cong₂; sym; module ≡-Reasoning)
18+
19+
open ≡-Reasoning
20+
21+
inner-assoc : m n o o + (n + m * suc n) * suc o
22+
≡ o + n * suc o + m * suc (o + n * suc o)
23+
inner-assoc m n o = begin
24+
o + (n + m * suc n) * suc o ≡⟨ cong (o +_) (begin
25+
(n + m * suc n) * suc o ≡⟨ *-distribʳ-+ (suc o) n (m * suc n) ⟩
26+
n * suc o + m * suc n * suc o ≡⟨ cong (n * suc o +_) (*-assoc m (suc n) (suc o)) ⟩
27+
n * suc o + m * suc (o + n * suc o) ∎) ⟩
28+
o + (n * suc o + m * suc (o + n * suc o)) ≡⟨ +-assoc o _ _ ⟨
29+
o + n * suc o + m * suc (o + n * suc o) ∎
30+
31+
private
32+
assoc-comm : a b c d a + b + c + d ≡ (a + c) + (b + d)
33+
assoc-comm a b c d = begin
34+
a + b + c + d ≡⟨ cong (_+ d) (+-assoc a b c) ⟩
35+
a + (b + c) + d ≡⟨ cong (λ z a + z + d) (+-comm b c) ⟩
36+
a + (c + b) + d ≡⟨ cong (_+ d) (+-assoc a c b) ⟨
37+
(a + c) + b + d ≡⟨ +-assoc (a + c) b d ⟩
38+
(a + c) + (b + d) ∎
39+
40+
assoc-comm′ : a b c d a + (b + (c + d)) ≡ a + c + (b + d)
41+
assoc-comm′ a b c d = begin
42+
a + (b + (c + d)) ≡⟨ cong (a +_) (+-assoc b c d) ⟨
43+
a + (b + c + d) ≡⟨ cong (λ z a + (z + d)) (+-comm b c) ⟩
44+
a + (c + b + d) ≡⟨ cong (a +_) (+-assoc c b d) ⟩
45+
a + (c + (b + d)) ≡⟨ +-assoc a c _ ⟨
46+
a + c + (b + d) ∎
47+
48+
assoc₁ : m n o (2 + n + o) * (1 + m) ≡ (1 + n) * (1 + m) + (1 + o) * (1 + m)
49+
assoc₁ m n o = begin
50+
(2 + n + o) * (1 + m) ≡⟨ cong (_* (1 + m)) (assoc-comm 1 1 n o) ⟩
51+
((1 + n) + (1 + o)) * (1 + m) ≡⟨ *-distribʳ-+ (1 + m) (1 + n) (1 + o) ⟩
52+
(1 + n) * (1 + m) + (1 + o) * (1 + m) ∎
53+
54+
assoc₂ : m n o (1 + n + (1 + o)) * (1 + m) ≡ (1 + n) * (1 + m) + (1 + o) * (1 + m)
55+
assoc₂ m n o = *-distribʳ-+ (1 + m) (1 + n) (1 + o)
56+
57+
assoc₃ : m n o m + (n + (1 + o)) * (1 + m) ≡ (1 + n) * (1 + m) + (m + o * (1 + m))
58+
assoc₃ m n o = begin
59+
m + (n + (1 + o)) * (1 + m) ≡⟨ cong (m +_) (*-distribʳ-+ (1 + m) n (1 + o)) ⟩
60+
m + (n * (1 + m) + (1 + o) * (1 + m)) ≡⟨ +-assoc m _ _ ⟨
61+
(m + n * (1 + m)) + (1 + o) * (1 + m) ≡⟨ +-suc _ _ ⟩
62+
(1 + n) * (1 + m) + (m + o * (1 + m)) ∎
63+
64+
assoc₄ : m n o m + (1 + m + (n + o) * (1 + m)) ≡ (1 + n) * (1 + m) + (m + o * (1 + m))
65+
assoc₄ m n o = begin
66+
m + (1 + m + (n + o) * (1 + m)) ≡⟨ +-suc _ _ ⟩
67+
1 + m + (m + (n + o) * (1 + m)) ≡⟨ cong (λ z suc (m + (m + z))) (*-distribʳ-+ (suc m) n o) ⟩
68+
1 + m + (m + (n * (1 + m) + o * (1 + m))) ≡⟨ cong suc (assoc-comm′ m m _ _) ⟩
69+
(1 + n) * (1 + m) + (m + o * (1 + m)) ∎

0 commit comments

Comments
 (0)