Skip to content

Commit b52e3e5

Browse files
authored
WIP: Show that Data.Nat.Show.charsInBase is injective (#1258)
1 parent 4b42cd1 commit b52e3e5

File tree

6 files changed

+85
-10
lines changed

6 files changed

+85
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ Other minor additions
285285
* Added new proofs to `Data.List.Properties`:
286286
```agda
287287
reverse-injective : reverse xs ≡ reverse ys → xs ≡ ys
288+
map-injective : Injective _≡_ _≡_ f → Injective _≡_ _≡_ (map f)
288289
```
289290

290291
* Added new proofs to `Data.List.Membership.Propositional.Properties`:
@@ -471,7 +472,7 @@ Other minor additions
471472
```agda
472473
splitAt-< : ∀ m {n} i → (i<m : toℕ i ℕ.< m) → splitAt m {n} i ≡ inj₁ (fromℕ< i<m)
473474
splitAt-≥ : ∀ m {n} i → (i≥m : toℕ i ℕ.≥ m) → splitAt m {n} i ≡ inj₂ (reduce≥ i i≥m)
474-
inject≤-injective : (n≤m : n ℕ.≤ m) x y → inject≤ x n≤m ≡ inject≤ y n≤m → x ≡ y
475+
inject≤-injective : (n≤m n≤m′ : n ℕ.≤ m) x y → inject≤ x n≤m ≡ inject≤ y n≤m → x ≡ y
475476
```
476477

477478
* Added new proofs to `Data.Vec.Properties`:

src/Data/Digit/Properties.agda

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of digits and digit expansions
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Data.Digit
10+
import Data.Char.Properties as Charₚ
11+
open import Data.Nat.Base using (ℕ)
12+
open import Data.Nat.Properties using (_≤?_)
13+
open import Data.Fin.Properties using (inject≤-injective)
14+
open import Data.Product using (_,_; proj₁)
15+
open import Data.Vec.Relation.Unary.Unique.Propositional using (Unique)
16+
import Data.Vec.Relation.Unary.Unique.Propositional.Properties as Uniqueₚ
17+
open import Data.Vec.Relation.Unary.AllPairs using (allPairs?)
18+
open import Relation.Nullary.Decidable using (True; from-yes)
19+
open import Relation.Nullary.Negation using (¬?)
20+
open import Relation.Binary.PropositionalEquality as P using (_≡_; refl)
21+
open import Function using (_∘_)
22+
23+
module Data.Digit.Properties where
24+
25+
digitCharsUnique : Unique digitChars
26+
digitCharsUnique = from-yes (allPairs? (λ x y ¬? (x Charₚ.≟ y)) digitChars)
27+
28+
module _ (base : ℕ) where
29+
module _ {base≥2 base≥2′ : True (2 ≤? base)} where
30+
toDigits-injective : n m proj₁ (toDigits base {base≥2} n) ≡ proj₁ (toDigits base {base≥2′} m) n ≡ m
31+
toDigits-injective n m eq with toDigits base {base≥2} n | toDigits base {base≥2′} m
32+
toDigits-injective ._ ._ refl | _ , refl | ._ , refl = refl
33+
34+
module _ {base≤16 base≤16′ : True (base ≤? 16)} where
35+
showDigit-injective : (n m : Digit base) showDigit {base} {base≤16} n ≡ showDigit {base} {base≤16′} m n ≡ m
36+
showDigit-injective n m = inject≤-injective _ _ n m ∘ Uniqueₚ.lookup-injective digitCharsUnique _ _

src/Data/Fin/Properties.agda

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,10 @@ inject≤-idempotent {_} {suc n} {suc k} zero _ _ _ = refl
464464
inject≤-idempotent {_} {suc n} {suc k} (suc i) m≤n n≤k _ =
465465
cong suc (inject≤-idempotent i (ℕₚ.≤-pred m≤n) (ℕₚ.≤-pred n≤k) _)
466466

467-
inject≤-injective : {n m} (n≤m : n ℕ.≤ m) x y inject≤ x n≤m ≡ inject≤ y n≤m x ≡ y
468-
inject≤-injective (s≤s p) zero zero eq = refl
469-
inject≤-injective (s≤s p) (suc x) (suc y) eq =
470-
cong suc (inject≤-injective p x y (suc-injective eq))
467+
inject≤-injective : {n m} (n≤m n≤m′ : n ℕ.≤ m) x y inject≤ x n≤m ≡ inject≤ y n≤m x ≡ y
468+
inject≤-injective (s≤s p) (s≤s q) zero zero eq = refl
469+
inject≤-injective (s≤s p) (s≤s q) (suc x) (suc y) eq =
470+
cong suc (inject≤-injective p q x y (suc-injective eq))
471471

472472
------------------------------------------------------------------------
473473
-- pred

src/Data/List/Properties.agda

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ map-compose : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f
106106
map-compose [] = refl
107107
map-compose (x ∷ xs) = cong (_ ∷_) (map-compose xs)
108108

109+
map-injective : {f : A B} Injective _≡_ _≡_ f Injective _≡_ _≡_ (map f)
110+
map-injective finj {[]} {[]} eq = refl
111+
map-injective finj {x ∷ xs} {y ∷ ys} eq =
112+
let fx≡fy , fxs≡fys = ∷-injective eq in
113+
cong₂ _∷_ (finj fx≡fy) (map-injective finj fxs≡fys)
114+
109115
------------------------------------------------------------------------
110116
-- mapMaybe
111117

src/Data/Nat/Show.agda

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ show = String.fromList ∘ toDecimalChars
3838
-- Warning: when compiled the time complexity of `showInBase b n` is
3939
-- O(n) instead of the expected O(log(n)).
4040

41+
charsInBase : (base : ℕ)
42+
{base≥2 : True (2 ≤? base)}
43+
{base≤16 : True (base ≤? 16)}
44+
List Char
45+
charsInBase base {base≥2} {base≤16} = map (showDigit {base≤16 = base≤16})
46+
∘ reverse
47+
∘ proj₁
48+
∘ toDigits base {base≥2 = base≥2}
49+
4150
showInBase : (base : ℕ)
4251
{base≥2 : True (2 ≤? base)}
4352
{base≤16 : True (base ≤? 16)}
4453
String
45-
showInBase base {base≥2} {base≤16} n =
46-
String.fromList $
47-
map (showDigit {base≤16 = base≤16}) $
48-
reverse $
49-
proj₁ $ toDigits base {base≥2 = base≥2} n
54+
showInBase base {base≥2} {base≤16} = String.fromList
55+
∘ charsInBase base {base≥2} {base≤16}

src/Data/Nat/Show/Properties.agda

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of showing natural numbers
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Data.Digit using (showDigit; toDigits)
10+
open import Data.Digit.Properties using (toDigits-injective; showDigit-injective)
11+
open import Function using (_∘_)
12+
import Data.List.Properties as Listₚ
13+
open import Data.Nat.Base using (ℕ)
14+
open import Data.Nat.Properties using (_≤?_)
15+
open import Relation.Nullary.Decidable using (True)
16+
open import Relation.Binary.PropositionalEquality using (_≡_)
17+
open import Data.Nat.Show using (charsInBase)
18+
19+
module Data.Nat.Show.Properties where
20+
21+
module _ (base : ℕ) {base≥2 : True (2 ≤? base)} {base≤16 : True (base ≤? 16)} where
22+
23+
charsInBase-injective : n m charsInBase base {base≥2} {base≤16} n ≡ charsInBase base {base≥2} {base≤16} m n ≡ m
24+
charsInBase-injective n m = toDigits-injective base {base≥2} _ _
25+
∘ Listₚ.reverse-injective
26+
∘ Listₚ.map-injective (showDigit-injective _ _ _)

0 commit comments

Comments
 (0)