/
nat.agda
131 lines (96 loc) · 2.65 KB
/
nat.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
module nat where
open import product
open import bool
----------------------------------------------------------------------
-- datatypes
----------------------------------------------------------------------
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
nat = ℕ
----------------------------------------------------------------------
-- syntax
----------------------------------------------------------------------
infixl 10 _*_
infixl 9 _+_ _∸_
infixl 8 _<_ _=ℕ_ _≤_ _>_ _≥_
-- pragmas to get decimal notation:
{-# BUILTIN NATURAL ℕ #-}
----------------------------------------------------------------------
-- operations
----------------------------------------------------------------------
---------------------------------------
-- basic arithmetic operations
---------------------------------------
_+_ : ℕ → ℕ → ℕ
zero + n = n
suc m + n = suc (m + n)
{-# BUILTIN NATPLUS _+_ #-}
_*_ : ℕ → ℕ → ℕ
zero * n = zero
suc m * n = n + (m * n)
{-# BUILTIN NATTIMES _*_ #-}
pred : ℕ → ℕ
pred 0 = 0
pred (suc n) = n
_∸_ : ℕ → ℕ → ℕ
m ∸ zero = m
zero ∸ suc n = zero
suc m ∸ suc n = m ∸ n
-- see nat-division.agda for division function
{-# BUILTIN NATMINUS _∸_ #-}
square : ℕ → ℕ
square x = x * x
--------------------------------------------------
-- comparisons
--------------------------------------------------
_<_ : ℕ → ℕ → 𝔹
0 < 0 = ff
0 < (suc y) = tt
(suc x) < (suc y) = x < y
(suc x) < 0 = ff
_=ℕ_ : ℕ → ℕ → 𝔹
0 =ℕ 0 = tt
suc x =ℕ suc y = x =ℕ y
_ =ℕ _ = ff
_≤_ : ℕ → ℕ → 𝔹
x ≤ y = (x < y) || x =ℕ y
_>_ : ℕ → ℕ → 𝔹
a > b = b < a
_≥_ : ℕ → ℕ → 𝔹
a ≥ b = b ≤ a
min : ℕ → ℕ → ℕ
min x y = if x < y then x else y
max : ℕ → ℕ → ℕ
max x y = if x < y then y else x
data compare-t : Set where
compare-lt : compare-t
compare-eq : compare-t
compare-gt : compare-t
compare : ℕ → ℕ → compare-t
compare 0 0 = compare-eq
compare 0 (suc y) = compare-lt
compare (suc x) 0 = compare-gt
compare (suc x) (suc y) = compare x y
iszero : ℕ → 𝔹
iszero 0 = tt
iszero _ = ff
parity : ℕ → 𝔹
parity 0 = ff
parity (suc x) = ~ (parity x)
_pow_ : ℕ → ℕ → ℕ
x pow 0 = 1
x pow (suc y) = x * (x pow y)
factorial : ℕ → ℕ
factorial 0 = 1
factorial (suc x) = (suc x) * (factorial x)
is-even : ℕ → 𝔹
is-odd : ℕ → 𝔹
is-even 0 = tt
is-even (suc x) = is-odd x
is-odd 0 = ff
is-odd (suc x) = is-even x
----------------------------------------------------------------------
iter : ℕ → ∀{ℓ}{X : Set ℓ} → (X → X) → X → X
iter 0 f x = x
iter (suc n) f x = f (iter n f x)