Debian Codegraph @

doc/README/Data/Nat.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Some examples showing where the natural numbers 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.Nat where
11
12 -- The natural numbers and various arithmetic operations are defined
13 -- in Data.Nat.
14
15 open import Data.Nat using (ℕ; _+_; _*_)
16
17 -- _*_ has precedence 7 over precedence 6 of _+_
18 -- precedence of both defined in module Agda.Builtin.Nat
19 ex₁ : ℕ
20 ex₁ = 1 + 3 * 4
21
22 -- Propositional equality and some related properties can be found
23 -- in Relation.Binary.PropositionalEquality.
24
25 open import Relation.Binary.PropositionalEquality using (_≡_; refl)
26
27 ex₂ : 3 + 5 ≡ 2 * 4
28 ex₂ = refl
29
30 -- Data.Nat.Properties contains a number of properties about natural
31 -- numbers.
32
33 open import Data.Nat.Properties using (*-comm; +-identityʳ)
34
35 ex₃ : ∀ m n → m * n ≡ n * m
36 ex₃ m n = *-comm m n
37
38 -- The module ≡-Reasoning in Relation.Binary.PropositionalEquality
39 -- provides some combinators for equational reasoning.
40
41 open Relation.Binary.PropositionalEquality using (cong; module ≡-Reasoning)
42
43 ex₄ : ∀ m n → m * (n + 0) ≡ n * m
44 ex₄ m n = begin
45 m * (n + 0) ≡⟨ cong (_*_ m) (+-identityʳ n) ⟩
46 m * n ≡⟨ *-comm m n ⟩
47 n * m ∎
48 where open ≡-Reasoning
49
50 -- The module SemiringSolver in Data.Nat.Solver contains a solver
51 -- for natural number equalities involving variables, constants, _+_
52 -- and _*_.
53
54 open import Data.Nat.Solver using (module +-*-Solver)
55 open +-*-Solver using (solve; _:*_; _:+_; con; _:=_)
56
57 ex₅ : ∀ m n → m * (n + 0) ≡ n * m
58 ex₅ = solve 2 (λ m n → m :* (n :+ con 0) := n :* m) refl