Debian Codegraph @

src/Data/Nat/Binary/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Natural numbers represented in binary.
5 ------------------------------------------------------------------------
6
7 -- This module contains an alternative formulation of ℕ that is
8 -- still reasonably computationally efficient without having to use
9 -- built-in functions.
10
11 {-# OPTIONS --cubical-compatible --safe #-}
12
13 module Data.Nat.Binary.Base where
14
15 open import Algebra.Bundles.Raw using (RawMagma; RawMonoid)
16 open import Algebra.Core using (Op₂)
17 open import Data.Bool.Base using (if_then_else_)
18 open import Data.Nat.Base as ℕ using (ℕ)
19 open import Data.Sum.Base using (_⊎_)
20 open import Function.Base using (_on_)
21 open import Level using (0ℓ)
22 open import Relation.Binary.Core using (Rel)
23 open import Relation.Binary.PropositionalEquality.Core using (_≡_)
24 open import Relation.Nullary.Negation.Core using (¬_)
25
26 ------------------------------------------------------------------------
27 -- Definition
28
29 data ℕᵇ : Set where
30 zero : ℕᵇ
31 2[1+_] : ℕᵇ → ℕᵇ -- n → 2*(1+n) = nonzero even numbers
32 1+[2_] : ℕᵇ → ℕᵇ -- n → 1 + 2*n = odd numbers
33
34 ------------------------------------------------------------------------
35 -- Ordering relations
36
37 infix 4 _<_ _>_ _≤_ _≥_ _≮_ _≯_ _≰_ _≱_
38
39 data _<_ : Rel ℕᵇ 0ℓ where
40 0<even : ∀ {x} → zero < 2[1+ x ]
41 0<odd : ∀ {x} → zero < 1+[2 x ]
42 even<even : ∀ {x y} → x < y → 2[1+ x ] < 2[1+ y ]
43 even<odd : ∀ {x y} → x < y → 2[1+ x ] < 1+[2 y ]
44 odd<even : ∀ {x y} → x < y ⊎ x ≡ y → 1+[2 x ] < 2[1+ y ]
45 odd<odd : ∀ {x y} → x < y → 1+[2 x ] < 1+[2 y ]
46 -- In these constructors "even" stands for nonzero even.
47
48 _>_ : Rel ℕᵇ 0ℓ
49 x > y = y < x
50
51 _≤_ : Rel ℕᵇ 0ℓ
52 x ≤ y = x < y ⊎ x ≡ y
53
54 _≥_ : Rel ℕᵇ 0ℓ
55 x ≥ y = y ≤ x
56
57 _≮_ : Rel ℕᵇ 0ℓ
58 x ≮ y = ¬ (x < y)
59
60 _≯_ : Rel ℕᵇ 0ℓ
61 x ≯ y = ¬ (x > y)
62
63 _≰_ : Rel ℕᵇ 0ℓ
64 x ≰ y = ¬ (x ≤ y)
65
66 _≱_ : Rel ℕᵇ 0ℓ
67 x ≱ y = ¬ (x ≥ y)
68
69 ------------------------------------------------------------------------
70 -- Basic operations
71
72 double : ℕᵇ → ℕᵇ
73 double zero = zero
74 double 2[1+ x ] = 2[1+ 1+[2 x ] ]
75 double 1+[2 x ] = 2[1+ (double x) ]
76
77 suc : ℕᵇ → ℕᵇ
78 suc zero = 1+[2 zero ]
79 suc 2[1+ x ] = 1+[2 (suc x) ]
80 suc 1+[2 x ] = 2[1+ x ]
81
82 pred : ℕᵇ → ℕᵇ
83 pred zero = zero
84 pred 2[1+ x ] = 1+[2 x ]
85 pred 1+[2 x ] = double x
86
87 ------------------------------------------------------------------------
88 -- Addition, multiplication and certain related functions
89
90 infixl 6 _+_
91 infixl 7 _*_
92
93 _+_ : Op₂ ℕᵇ
94 zero + y = y
95 x + zero = x
96 2[1+ x ] + 2[1+ y ] = 2[1+ suc (x + y) ]
97 2[1+ x ] + 1+[2 y ] = suc 2[1+ (x + y) ]
98 1+[2 x ] + 2[1+ y ] = suc 2[1+ (x + y) ]
99 1+[2 x ] + 1+[2 y ] = suc 1+[2 (x + y) ]
100
101 _*_ : Op₂ ℕᵇ
102 zero * _ = zero
103 _ * zero = zero
104 2[1+ x ] * 2[1+ y ] = double 2[1+ x + (y + x * y) ]
105 2[1+ x ] * 1+[2 y ] = 2[1+ x + y * 2[1+ x ] ]
106 1+[2 x ] * 2[1+ y ] = 2[1+ y + x * 2[1+ y ] ]
107 1+[2 x ] * 1+[2 y ] = 1+[2 x + y * 1+[2 x ] ]
108
109 ------------------------------------------------------------------------
110 -- Conversion between ℕᵇ and ℕ
111
112 toℕ : ℕᵇ → ℕ
113 toℕ zero = 0
114 toℕ 2[1+ x ] = 2 ℕ.* (ℕ.suc (toℕ x))
115 toℕ 1+[2 x ] = ℕ.suc (2 ℕ.* (toℕ x))
116
117 fromℕ : ℕ → ℕᵇ
118 fromℕ n = helper n n
119 module fromℕ where
120 helper : ℕ → ℕ → ℕᵇ
121 helper 0 _ = zero
122 helper (ℕ.suc n) (ℕ.suc w) =
123 if (n ℕ.% 2 ℕ.≡ᵇ 0)
124 then 1+[2 helper (n ℕ./ 2) w ]
125 else 2[1+ helper (n ℕ./ 2) w ]
126 -- Impossible case
127 helper _ 0 = zero
128
129 -- An alternative slower definition
130 fromℕ' : ℕ → ℕᵇ
131 fromℕ' 0 = zero
132 fromℕ' (ℕ.suc n) = suc (fromℕ' n)
133
134 -- An alternative ordering lifted from ℕ
135
136 infix 4 _<ℕ_
137
138 _<ℕ_ : Rel ℕᵇ 0ℓ
139 _<ℕ_ = ℕ._<_ on toℕ
140
141 ------------------------------------------------------------------------
142 -- Other functions
143
144 -- Useful in some termination proofs.
145 size : ℕᵇ → ℕ
146 size zero = 0
147 size 2[1+ x ] = ℕ.suc (size x)
148 size 1+[2 x ] = ℕ.suc (size x)
149
150 ------------------------------------------------------------------------
151 -- Constants
152
153 0ᵇ = zero
154 1ᵇ = suc 0ᵇ
155 2ᵇ = suc 1ᵇ
156 3ᵇ = suc 2ᵇ
157 4ᵇ = suc 3ᵇ
158 5ᵇ = suc 4ᵇ
159 6ᵇ = suc 5ᵇ
160 7ᵇ = suc 6ᵇ
161 8ᵇ = suc 7ᵇ
162 9ᵇ = suc 8ᵇ
163
164 ------------------------------------------------------------------------
165 -- Raw bundles for _+_
166
167 +-rawMagma : RawMagma 0ℓ 0ℓ
168 +-rawMagma = record
169 { _≈_ = _≡_
170 ; _∙_ = _+_
171 }
172
173 +-0-rawMonoid : RawMonoid 0ℓ 0ℓ
174 +-0-rawMonoid = record
175 { _≈_ = _≡_
176 ; _∙_ = _+_
177 ; ε = 0ᵇ
178 }
179
180 ------------------------------------------------------------------------
181 -- Raw bundles for _*_
182
183 *-rawMagma : RawMagma 0ℓ 0ℓ
184 *-rawMagma = record
185 { _≈_ = _≡_
186 ; _∙_ = _*_
187 }
188
189 *-1-rawMonoid : RawMonoid 0ℓ 0ℓ
190 *-1-rawMonoid = record
191 { _≈_ = _≡_
192 ; _∙_ = _*_
193 ; ε = 1ᵇ
194 }