| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Some examples showing where the integers and some related |
| 5 |
-- operations and properties are defined, and how they can be used |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible #-} |
| 9 |
|
| 10 |
module README.Data.Integer where |
| 11 |
|
| 12 |
-- The integers and various arithmetic operations are defined in |
| 13 |
-- Data.Integer. |
| 14 |
|
| 15 |
open import Data.Integer |
| 16 |
|
| 17 |
-- The +_ function converts natural numbers into integers. |
| 18 |
|
| 19 |
ex₁ : ℤ |
| 20 |
ex₁ = + 2 |
| 21 |
|
| 22 |
-- The -_ function negates an integer. |
| 23 |
|
| 24 |
ex₂ : ℤ |
| 25 |
ex₂ = - + 4 |
| 26 |
|
| 27 |
-- Some binary operators are also defined, including addition, |
| 28 |
-- subtraction and multiplication. |
| 29 |
|
| 30 |
ex₃ : ℤ |
| 31 |
ex₃ = + 1 + + 3 * - + 2 - + 4 |
| 32 |
|
| 33 |
-- Propositional equality and some related properties can be found |
| 34 |
-- in Relation.Binary.PropositionalEquality. |
| 35 |
|
| 36 |
open import Relation.Binary.PropositionalEquality as ≡ using (_≡_) |
| 37 |
|
| 38 |
ex₄ : ex₃ ≡ - + 9 |
| 39 |
ex₄ = ≡.refl |
| 40 |
|
| 41 |
-- Data.Integer.Properties contains a number of properties related to |
| 42 |
-- integers. Algebra defines what a commutative ring is, among other |
| 43 |
-- things. |
| 44 |
|
| 45 |
import Data.Integer.Properties as ℤ |
| 46 |
|
| 47 |
ex₅ : ∀ i j → i * j ≡ j * i |
| 48 |
ex₅ i j = ℤ.*-comm i j |
| 49 |
|
| 50 |
-- The module ≡-Reasoning in Relation.Binary.PropositionalEquality |
| 51 |
-- provides some combinators for equational reasoning. |
| 52 |
|
| 53 |
open ≡.≡-Reasoning |
| 54 |
|
| 55 |
ex₆ : ∀ i j → i * (j + + 0) ≡ j * i |
| 56 |
ex₆ i j = begin |
| 57 |
i * (j + + 0) ≡⟨ ≡.cong (i *_) (ℤ.+-identityʳ j) ⟩ |
| 58 |
i * j ≡⟨ ℤ.*-comm i j ⟩ |
| 59 |
j * i ∎ |
| 60 |
|
| 61 |
-- The module RingSolver in Data.Integer.Solver contains a solver |
| 62 |
-- for integer equalities involving variables, constants, _+_, _*_, -_ |
| 63 |
-- and _-_. |
| 64 |
|
| 65 |
open import Data.Integer.Solver using (module +-*-Solver) |
| 66 |
open +-*-Solver |
| 67 |
|
| 68 |
ex₇ : ∀ i j → i * - j - j * i ≡ - + 2 * i * j |
| 69 |
ex₇ = solve 2 (λ i j → i :* :- j :- j :* i := :- con (+ 2) :* i :* j) |
| 70 |
≡.refl |