Skip to content

Commit

Permalink
Refactoring positive integers (#1059)
Browse files Browse the repository at this point in the history
Resolves #1043.

---------

Co-authored-by: Fredrik Bakke <[email protected]>
  • Loading branch information
malarbol and fredrik-bakke authored Mar 28, 2024
1 parent 1a4ff3a commit 2cda020
Show file tree
Hide file tree
Showing 41 changed files with 2,900 additions and 1,035 deletions.
12 changes: 12 additions & 0 deletions src/elementary-number-theory.lagda.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open import elementary-number-theory.ackermann-function public
open import elementary-number-theory.addition-integer-fractions public
open import elementary-number-theory.addition-integers public
open import elementary-number-theory.addition-natural-numbers public
open import elementary-number-theory.addition-positive-and-negative-integers public
open import elementary-number-theory.addition-rational-numbers public
open import elementary-number-theory.arithmetic-functions public
open import elementary-number-theory.based-induction-natural-numbers public
Expand All @@ -30,7 +31,9 @@ open import elementary-number-theory.congruence-natural-numbers public
open import elementary-number-theory.cross-multiplication-difference-integer-fractions public
open import elementary-number-theory.cubes-natural-numbers public
open import elementary-number-theory.decidable-dependent-function-types public
open import elementary-number-theory.decidable-total-order-integers public
open import elementary-number-theory.decidable-total-order-natural-numbers public
open import elementary-number-theory.decidable-total-order-rational-numbers public
open import elementary-number-theory.decidable-total-order-standard-finite-types public
open import elementary-number-theory.decidable-types public
open import elementary-number-theory.difference-integers public
Expand Down Expand Up @@ -86,19 +89,25 @@ open import elementary-number-theory.multiplication-integer-fractions public
open import elementary-number-theory.multiplication-integers public
open import elementary-number-theory.multiplication-lists-of-natural-numbers public
open import elementary-number-theory.multiplication-natural-numbers public
open import elementary-number-theory.multiplication-positive-and-negative-integers public
open import elementary-number-theory.multiplication-rational-numbers public
open import elementary-number-theory.multiplicative-monoid-of-natural-numbers public
open import elementary-number-theory.multiplicative-units-integers public
open import elementary-number-theory.multiplicative-units-standard-cyclic-rings public
open import elementary-number-theory.multiset-coefficients public
open import elementary-number-theory.natural-numbers public
open import elementary-number-theory.negative-integers public
open import elementary-number-theory.nonnegative-integers public
open import elementary-number-theory.nonpositive-integers public
open import elementary-number-theory.nonzero-integers public
open import elementary-number-theory.nonzero-natural-numbers public
open import elementary-number-theory.ordinal-induction-natural-numbers public
open import elementary-number-theory.parity-natural-numbers public
open import elementary-number-theory.peano-arithmetic public
open import elementary-number-theory.pisano-periods public
open import elementary-number-theory.poset-of-natural-numbers-ordered-by-divisibility public
open import elementary-number-theory.positive-and-negative-integers public
open import elementary-number-theory.positive-integers public
open import elementary-number-theory.powers-integers public
open import elementary-number-theory.powers-of-two public
open import elementary-number-theory.prime-numbers public
Expand All @@ -120,7 +129,10 @@ open import elementary-number-theory.squares-natural-numbers public
open import elementary-number-theory.standard-cyclic-groups public
open import elementary-number-theory.standard-cyclic-rings public
open import elementary-number-theory.stirling-numbers-of-the-second-kind public
open import elementary-number-theory.strict-inequality-integer-fractions public
open import elementary-number-theory.strict-inequality-integers public
open import elementary-number-theory.strict-inequality-natural-numbers public
open import elementary-number-theory.strict-inequality-rational-numbers public
open import elementary-number-theory.strictly-ordered-pairs-of-natural-numbers public
open import elementary-number-theory.strong-induction-natural-numbers public
open import elementary-number-theory.sums-of-natural-numbers public
Expand Down
2 changes: 2 additions & 0 deletions src/elementary-number-theory/absolute-value-integers.lagda.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ open import elementary-number-theory.integers
open import elementary-number-theory.multiplication-integers
open import elementary-number-theory.multiplication-natural-numbers
open import elementary-number-theory.natural-numbers
open import elementary-number-theory.nonnegative-integers
open import elementary-number-theory.positive-integers

open import foundation.action-on-identifications-functions
open import foundation.coproduct-types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module elementary-number-theory.addition-integer-fractions where
open import elementary-number-theory.addition-integers
open import elementary-number-theory.integer-fractions
open import elementary-number-theory.multiplication-integers
open import elementary-number-theory.multiplication-positive-and-negative-integers

open import foundation.action-on-identifications-binary-functions
open import foundation.dependent-pair-types
Expand Down
80 changes: 17 additions & 63 deletions src/elementary-number-theory/addition-integers.lagda.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module elementary-number-theory.addition-integers where
open import elementary-number-theory.addition-natural-numbers
open import elementary-number-theory.integers
open import elementary-number-theory.natural-numbers
open import elementary-number-theory.nonnegative-integers
open import elementary-number-theory.positive-and-negative-integers
open import elementary-number-theory.positive-integers

open import foundation.action-on-identifications-binary-functions
open import foundation.action-on-identifications-functions
Expand All @@ -30,9 +33,9 @@ open import foundation.unit-type

## Idea

We introduce addition on the integers and derive its basic properties with
respect to `succ-ℤ` and `neg-ℤ`. Properties of addition with respect to
inequality are derived in `inequality-integers`.
We introduce {{#concept "addition" Disambiguation="integers" Agda=add-ℤ}} on the
[integers](elementary-number-theory.integers.md) and derive its basic properties
with respect to `succ-ℤ` and `neg-ℤ`.

## Definition

Expand Down Expand Up @@ -506,52 +509,6 @@ distributive-neg-add-ℤ (inr (inr (succ-ℕ n))) l =
by ap pred-ℤ (distributive-neg-add-ℤ (inr (inr n)) l)
```

### Addition of nonnegative integers is nonnegative

```agda
is-nonnegative-add-ℤ :
(k l : ℤ)
is-nonnegative-ℤ k is-nonnegative-ℤ l is-nonnegative-ℤ (k +ℤ l)
is-nonnegative-add-ℤ (inr (inl star)) (inr (inl star)) p q = star
is-nonnegative-add-ℤ (inr (inl star)) (inr (inr n)) p q = star
is-nonnegative-add-ℤ (inr (inr zero-ℕ)) (inr (inl star)) p q = star
is-nonnegative-add-ℤ (inr (inr (succ-ℕ n))) (inr (inl star)) star star =
is-nonnegative-succ-ℤ
( (inr (inr n)) +ℤ (inr (inl star)))
( is-nonnegative-add-ℤ (inr (inr n)) (inr (inl star)) star star)
is-nonnegative-add-ℤ (inr (inr zero-ℕ)) (inr (inr m)) star star = star
is-nonnegative-add-ℤ (inr (inr (succ-ℕ n))) (inr (inr m)) star star =
is-nonnegative-succ-ℤ
( (inr (inr n)) +ℤ (inr (inr m)))
( is-nonnegative-add-ℤ (inr (inr n)) (inr (inr m)) star star)
```

### Addition of positive integers is positive

```agda
is-positive-add-ℤ :
{x y : ℤ} is-positive-ℤ x is-positive-ℤ y is-positive-ℤ (x +ℤ y)
is-positive-add-ℤ {inr (inr zero-ℕ)} {inr (inr y)} H K = star
is-positive-add-ℤ {inr (inr (succ-ℕ x))} {inr (inr y)} H K =
is-positive-succ-ℤ
( is-nonnegative-is-positive-ℤ
( is-positive-add-ℤ {inr (inr x)} {inr (inr y)} star star))

is-positive-add-nonnegative-positive-ℤ :
{x y : ℤ} is-nonnegative-ℤ x is-positive-ℤ y is-positive-ℤ (x +ℤ y)
is-positive-add-nonnegative-positive-ℤ {inr (inl x)} {y} H H' =
is-positive-eq-ℤ refl H'
is-positive-add-nonnegative-positive-ℤ {inr (inr x)} {y} H H' =
is-positive-add-ℤ {inr (inr x)} {y} H H'

is-positive-add-positive-nonnegative-ℤ :
{x y : ℤ} is-positive-ℤ x is-nonnegative-ℤ y is-positive-ℤ (x +ℤ y)
is-positive-add-positive-nonnegative-ℤ {x} {y} H H' =
is-positive-eq-ℤ
( commutative-add-ℤ y x)
( is-positive-add-nonnegative-positive-ℤ H' H)
```

### The inclusion of ℕ into ℤ preserves addition

```agda
Expand Down Expand Up @@ -595,17 +552,14 @@ is-zero-right-add-ℤ x y H =
is-zero-left-add-ℤ y x (commutative-add-ℤ y x ∙ H)
```

### Adding negatives results in a negative

```agda
negatives-add-ℤ :
(x y : ℕ) in-neg x +ℤ in-neg y = in-neg (succ-ℕ (x +ℕ y))
negatives-add-ℤ zero-ℕ y = ap (inl ∘ succ-ℕ) (inv (left-unit-law-add-ℕ y))
negatives-add-ℤ (succ-ℕ x) y =
equational-reasoning
pred-ℤ (in-neg x +ℤ in-neg y)
= pred-ℤ (in-neg (succ-ℕ (x +ℕ y)))
by ap pred-ℤ (negatives-add-ℤ x y)
= (inl ∘ succ-ℕ) ((succ-ℕ x) +ℕ y)
by ap (inl ∘ succ-ℕ) (inv (left-successor-law-add-ℕ x y))
```
## See also

- Properties of addition with respect to positivity, nonnegativity, negativity
and nonnpositivity are derived in
[`addition-positive-and-negative-integers`](elementary-number-theory.addition-positive-and-negative-integers.md)
- Properties of addition with respect to the standard ordering on the integers
are derived in
[`inequality-integers`](elementary-number-theory.inequality-integers.md)
- Properties of addition with respect to the standard strict ordering on the
integers are derived in
[`strict-inequality-integers`](elementary-number-theory.strict-inequality-integers.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# Addition of positive, negative, nonnegative and nonpositive integers

```agda
module elementary-number-theory.addition-positive-and-negative-integers where
```

<details><summary>Imports</summary>

```agda
open import elementary-number-theory.addition-integers
open import elementary-number-theory.integers
open import elementary-number-theory.natural-numbers
open import elementary-number-theory.negative-integers
open import elementary-number-theory.nonnegative-integers
open import elementary-number-theory.nonpositive-integers
open import elementary-number-theory.positive-and-negative-integers
open import elementary-number-theory.positive-integers

open import foundation.coproduct-types
open import foundation.dependent-pair-types
open import foundation.equivalences
open import foundation.identity-types
open import foundation.injective-maps
open import foundation.unit-type
```

</details>

## Idea

When we have information about the sign of the summands, we can in many cases
deduce the sign of their sum too. The table below tabulates all such cases and
displays the corresponding Agda definition.

| `p` | `q` | `p +ℤ q` | operation |
| :---------: | :---------: | :---------: | ------------------- |
| positive | positive | positive | `add-positive-ℤ` |
| positive | nonnegative | positive | |
| positive | negative | | |
| positive | nonpositive | | |
| nonnegative | positive | positive | |
| nonnegative | nonnegative | nonnegative | `add-nonnegative-ℤ` |
| nonnegative | negative | | |
| nonnegative | nonpositive | | |
| negative | positive | | |
| negative | nonnegative | | |
| negative | negative | negative | `add-negative-ℤ` |
| negative | nonpositive | negative | |
| nonpositive | positive | | |
| nonpositive | nonnegative | | |
| nonpositive | negative | negative | |
| nonpositive | nonpositive | nonpositive | `add-nonpositive-ℤ` |

## Lemmas

### The sum of two positive integers is positive

```agda
is-positive-add-ℤ :
{x y : ℤ} is-positive-ℤ x is-positive-ℤ y is-positive-ℤ (x +ℤ y)
is-positive-add-ℤ {inr (inr zero-ℕ)} {y} H K =
is-positive-succ-is-positive-ℤ K
is-positive-add-ℤ {inr (inr (succ-ℕ x))} {y} H K =
is-positive-succ-is-positive-ℤ
( is-positive-add-ℤ {inr (inr x)} H K)
```

### The sum of a positive and a nonnegative integer is positive

```agda
is-positive-add-positive-nonnegative-ℤ :
{x y : ℤ} is-positive-ℤ x is-nonnegative-ℤ y is-positive-ℤ (x +ℤ y)
is-positive-add-positive-nonnegative-ℤ {inr (inr zero-ℕ)} {y} H K =
is-positive-succ-is-nonnegative-ℤ K
is-positive-add-positive-nonnegative-ℤ {inr (inr (succ-ℕ x))} {y} H K =
is-positive-succ-is-positive-ℤ
( is-positive-add-positive-nonnegative-ℤ {inr (inr x)} H K)
```

### The sum of a nonnegative and a positive integer is positive

```agda
is-positive-add-nonnegative-positive-ℤ :
{x y : ℤ} is-nonnegative-ℤ x is-positive-ℤ y is-positive-ℤ (x +ℤ y)
is-positive-add-nonnegative-positive-ℤ {x} {y} H K =
is-positive-eq-ℤ
( commutative-add-ℤ y x)
( is-positive-add-positive-nonnegative-ℤ K H)
```

### The sum of two nonnegative integers is nonnegative

```agda
is-nonnegative-add-ℤ :
{x y : ℤ}
is-nonnegative-ℤ x is-nonnegative-ℤ y is-nonnegative-ℤ (x +ℤ y)
is-nonnegative-add-ℤ {inr (inl x)} {y} H K = K
is-nonnegative-add-ℤ {inr (inr zero-ℕ)} {y} H K =
is-nonnegative-succ-is-nonnegative-ℤ K
is-nonnegative-add-ℤ {inr (inr (succ-ℕ x))} {y} H K =
is-nonnegative-succ-is-nonnegative-ℤ
( is-nonnegative-add-ℤ {inr (inr x)} star K)
```

### The sum of two negative integers is negative

```agda
is-negative-add-ℤ :
{x y : ℤ} is-negative-ℤ x is-negative-ℤ y is-negative-ℤ (x +ℤ y)
is-negative-add-ℤ {x} {y} H K =
is-negative-eq-ℤ
( neg-neg-ℤ (x +ℤ y))
( is-negative-neg-is-positive-ℤ
( is-positive-eq-ℤ
( inv (distributive-neg-add-ℤ x y))
( is-positive-add-ℤ
( is-positive-neg-is-negative-ℤ H)
( is-positive-neg-is-negative-ℤ K))))
```

### The sum of a negative and a nonpositive integer is negative

```agda
is-negative-add-negative-nonnegative-ℤ :
{x y : ℤ} is-negative-ℤ x is-nonpositive-ℤ y is-negative-ℤ (x +ℤ y)
is-negative-add-negative-nonnegative-ℤ {x} {y} H K =
is-negative-eq-ℤ
( neg-neg-ℤ (x +ℤ y))
( is-negative-neg-is-positive-ℤ
( is-positive-eq-ℤ
( inv (distributive-neg-add-ℤ x y))
( is-positive-add-positive-nonnegative-ℤ
( is-positive-neg-is-negative-ℤ H)
( is-nonnegative-neg-is-nonpositive-ℤ K))))
```

### The sum of a nonpositive and a negative integer is negative

```agda
is-negative-add-nonpositive-negative-ℤ :
{x y : ℤ} is-nonpositive-ℤ x is-negative-ℤ y is-negative-ℤ (x +ℤ y)
is-negative-add-nonpositive-negative-ℤ {x} {y} H K =
is-negative-eq-ℤ
( commutative-add-ℤ y x)
( is-negative-add-negative-nonnegative-ℤ K H)
```

### The sum of two nonpositive integers is nonpositive

```agda
is-nonpositive-add-ℤ :
{x y : ℤ}
is-nonpositive-ℤ x is-nonpositive-ℤ y is-nonpositive-ℤ (x +ℤ y)
is-nonpositive-add-ℤ {x} {y} H K =
is-nonpositive-eq-ℤ
( neg-neg-ℤ (x +ℤ y))
( is-nonpositive-neg-is-nonnegative-ℤ
( is-nonnegative-eq-ℤ
( inv (distributive-neg-add-ℤ x y))
( is-nonnegative-add-ℤ
( is-nonnegative-neg-is-nonpositive-ℤ H)
( is-nonnegative-neg-is-nonpositive-ℤ K))))
```

## Definitions

### Addition of positive integers

```agda
add-positive-ℤ : positive-ℤ positive-ℤ positive-ℤ
add-positive-ℤ (x , H) (y , K) = (add-ℤ x y , is-positive-add-ℤ H K)
```

### Addition of nonnegative integers

```agda
add-nonnegative-ℤ : nonnegative-ℤ nonnegative-ℤ nonnegative-ℤ
add-nonnegative-ℤ (x , H) (y , K) = (add-ℤ x y , is-nonnegative-add-ℤ H K)
```

### Addition of negative integers

```agda
add-negative-ℤ : negative-ℤ negative-ℤ negative-ℤ
add-negative-ℤ (x , H) (y , K) = (add-ℤ x y , is-negative-add-ℤ H K)
```

### Addition of nonpositive integers

```agda
add-nonpositive-ℤ : nonpositive-ℤ nonpositive-ℤ nonpositive-ℤ
add-nonpositive-ℤ (x , H) (y , K) = (add-ℤ x y , is-nonpositive-add-ℤ H K)
```
Loading

0 comments on commit 2cda020

Please sign in to comment.