| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Digits and digit expansions |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Digit where |
| 10 |
|
| 11 |
open import Data.Nat.Base |
| 12 |
using (ℕ; zero; suc; _<_; _/_; _%_; sz<ss; _+_; _*_; 2+; _≤′_) |
| 13 |
open import Data.Nat.Properties |
| 14 |
using (_≤?_; _<?_; ≤⇒≤′; module ≤-Reasoning; m≤m+n; +-comm; +-assoc |
| 15 |
; *-distribˡ-+; *-identityʳ) |
| 16 |
open import Data.Fin.Base as Fin using (Fin; zero; suc; toℕ) |
| 17 |
open import Data.Bool.Base using (Bool; true; false) |
| 18 |
open import Data.Char.Base using (Char) |
| 19 |
open import Data.List.Base using (List; replicate; [_]; _∷_; []) |
| 20 |
open import Data.Product.Base using (∃; _,_) |
| 21 |
open import Data.Vec.Base as Vec using (Vec; _∷_; []) |
| 22 |
open import Data.Nat.DivMod using (m/n<m; _divMod_; result) |
| 23 |
open import Data.Nat.Induction |
| 24 |
using (Acc; acc; <-wellFounded-fast; <′-Rec; <′-rec) |
| 25 |
open import Function.Base using (_$_) |
| 26 |
open import Relation.Nullary.Decidable using (True; does; toWitness) |
| 27 |
open import Relation.Binary.Definitions using (Decidable) |
| 28 |
open import Relation.Binary.PropositionalEquality |
| 29 |
using (_≡_; refl; sym; cong; cong₂; module ≡-Reasoning) |
| 30 |
|
| 31 |
------------------------------------------------------------------------ |
| 32 |
-- Digits |
| 33 |
|
| 34 |
-- Digit b is the type of digits in base b. |
| 35 |
|
| 36 |
Digit : ℕ → Set |
| 37 |
Digit b = Fin b |
| 38 |
|
| 39 |
-- Some specific digit kinds. |
| 40 |
|
| 41 |
Decimal = Digit 10 |
| 42 |
Bit = Digit 2 |
| 43 |
|
| 44 |
-- Some named digits. |
| 45 |
|
| 46 |
0b : Bit |
| 47 |
0b = zero |
| 48 |
|
| 49 |
1b : Bit |
| 50 |
1b = suc zero |
| 51 |
|
| 52 |
------------------------------------------------------------------------ |
| 53 |
-- Converting between `ℕ` and `expansions of ℕ` |
| 54 |
|
| 55 |
toNatDigits : (base : ℕ) {base≤16 : True (1 ≤? base)} → ℕ → List ℕ |
| 56 |
toNatDigits base@(suc zero) n = replicate n 1 |
| 57 |
toNatDigits base@(suc (suc _)) n = aux (<-wellFounded-fast n) [] |
| 58 |
where |
| 59 |
aux : {n : ℕ} → Acc _<_ n → List ℕ → List ℕ |
| 60 |
aux {zero} _ xs = (0 ∷ xs) |
| 61 |
aux {n@(suc _)} (acc wf) xs with does (0 <? n / base) |
| 62 |
... | false = (n % base) ∷ xs -- Could this more simply be n ∷ xs here? |
| 63 |
... | true = aux (wf (m/n<m n base sz<ss)) ((n % base) ∷ xs) |
| 64 |
|
| 65 |
------------------------------------------------------------------------ |
| 66 |
-- Converting between `ℕ` and expansions of `Digit base` |
| 67 |
|
| 68 |
Expansion : ℕ → Set |
| 69 |
Expansion base = List (Digit base) |
| 70 |
|
| 71 |
-- fromDigits takes a digit expansion of a natural number, starting |
| 72 |
-- with the _least_ significant digit, and returns the corresponding |
| 73 |
-- natural number. |
| 74 |
|
| 75 |
fromDigits : ∀ {base} → Expansion base → ℕ |
| 76 |
fromDigits [] = 0 |
| 77 |
fromDigits {base} (d ∷ ds) = toℕ d + fromDigits ds * base |
| 78 |
|
| 79 |
-- toDigits b n yields the digits of n, in base b, starting with the |
| 80 |
-- _least_ significant digit. |
| 81 |
-- |
| 82 |
-- Note that the list of digits is always non-empty. |
| 83 |
|
| 84 |
toDigits : (base : ℕ) {base≥2 : True (2 ≤? base)} (n : ℕ) → |
| 85 |
∃ λ (ds : Expansion base) → fromDigits ds ≡ n |
| 86 |
toDigits base@(suc (suc k)) n = <′-rec Pred helper n |
| 87 |
where |
| 88 |
|
| 89 |
Pred = λ n → ∃ λ ds → fromDigits ds ≡ n |
| 90 |
|
| 91 |
cons : ∀ {m} (r : Digit base) → Pred m → Pred (toℕ r + m * base) |
| 92 |
cons r (ds , eq) = (r ∷ ds , cong (λ i → toℕ r + i * base) eq) |
| 93 |
|
| 94 |
lem′ : ∀ x k → x + x + (k + x * k) ≡ k + x * 2+ k |
| 95 |
lem′ x k = begin |
| 96 |
x + x + (k + x * k) ≡⟨ +-assoc (x + x) k _ ⟨ |
| 97 |
x + x + k + x * k ≡⟨ cong (_+ x * k) (+-comm _ k) ⟩ |
| 98 |
k + (x + x) + x * k ≡⟨ +-assoc k (x + x) _ ⟩ |
| 99 |
k + ((x + x) + x * k) ≡⟨ cong (k +_) (begin |
| 100 |
(x + x) + x * k ≡⟨ +-assoc x x _ ⟩ |
| 101 |
x + (x + x * k) ≡⟨ cong (x +_) (cong (_+ x * k) (*-identityʳ x)) ⟨ |
| 102 |
x + (x * 1 + x * k) ≡⟨ cong₂ _+_ (*-identityʳ x) (*-distribˡ-+ x 1 k ) ⟨ |
| 103 |
x * 1 + (x * suc k) ≡⟨ *-distribˡ-+ x 1 (1 + k) ⟨ |
| 104 |
x * 2+ k ∎) ⟩ |
| 105 |
k + x * 2+ k ∎ |
| 106 |
where open ≡-Reasoning |
| 107 |
|
| 108 |
lem : ∀ x k r → 2 + x ≤′ r + (1 + x) * (2 + k) |
| 109 |
lem x k r = ≤⇒≤′ $ begin |
| 110 |
2 + x ≤⟨ m≤m+n _ _ ⟩ |
| 111 |
2 + x + (x + (1 + x) * k + r) ≡⟨ cong ((2 + x) +_) (+-comm _ r) ⟩ |
| 112 |
2 + x + (r + (x + (1 + x) * k)) ≡⟨ +-assoc (2 + x) r _ ⟨ |
| 113 |
2 + x + r + (x + (1 + x) * k) ≡⟨ cong (_+ (x + (1 + x) * k)) (+-comm (2 + x) r) ⟩ |
| 114 |
r + (2 + x) + (x + (1 + x) * k) ≡⟨ +-assoc r (2 + x) _ ⟩ |
| 115 |
r + ((2 + x) + (x + (1 + x) * k)) ≡⟨ cong (r +_) (cong 2+ (+-assoc x x _)) ⟨ |
| 116 |
r + (2 + (x + x + (1 + x) * k)) ≡⟨ cong (λ z → r + (2+ z)) (lem′ x k) ⟩ |
| 117 |
r + (2 + (k + x * (2 + k))) ≡⟨⟩ |
| 118 |
r + (1 + x) * (2 + k) ∎ |
| 119 |
where open ≤-Reasoning |
| 120 |
|
| 121 |
helper : ∀ n → <′-Rec Pred n → Pred n |
| 122 |
helper n rec with n divMod base |
| 123 |
... | result zero r eq = ([ r ] , sym eq) |
| 124 |
... | result (suc x) r refl = cons r (rec (lem x k (toℕ r))) |
| 125 |
|
| 126 |
------------------------------------------------------------------------ |
| 127 |
-- Showing digits |
| 128 |
|
| 129 |
-- The characters used to show the first 16 digits. |
| 130 |
|
| 131 |
digitChars : Vec Char 16 |
| 132 |
digitChars = |
| 133 |
'0' ∷ '1' ∷ '2' ∷ '3' ∷ '4' ∷ '5' ∷ '6' ∷ '7' ∷ '8' ∷ '9' ∷ |
| 134 |
'a' ∷ 'b' ∷ 'c' ∷ 'd' ∷ 'e' ∷ 'f' ∷ [] |
| 135 |
|
| 136 |
-- showDigit shows digits in base ≤ 16. |
| 137 |
|
| 138 |
showDigit : ∀ {base} {base≤16 : True (base ≤? 16)} → Digit base → Char |
| 139 |
showDigit {base≤16 = base≤16} d = |
| 140 |
Vec.lookup digitChars (Fin.inject≤ d (toWitness base≤16)) |