Skip to content

Commit 9e28c6e

Browse files
authored
[ fix #667 ] add parsing and showing for numeric types
1 parent 2f8402f commit 9e28c6e

File tree

9 files changed

+176
-37
lines changed

9 files changed

+176
-37
lines changed

CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ Deprecated names
146146
filter⁺ ↦ filter-⊆
147147
```
148148

149+
* In `Data.Integer`:
150+
```agda
151+
show ↦ Data.Integer.Show.show
152+
```
153+
154+
* In `Data.Rational`:
155+
```agda
156+
show ↦ Data.Rational.Show.show
157+
```
158+
149159
New modules
150160
-----------
151161

@@ -222,6 +232,13 @@ New modules
222232
System.Environment.Primitive
223233
```
224234

235+
* Added the following `Show` modules:
236+
```agda
237+
Data.Fin.Show
238+
Data.Integer.Show
239+
Data.Rational.Show
240+
```
241+
225242
* Added bindings for Haskell's `System.Exit`:
226243
```
227244
System.Exit
@@ -486,6 +503,16 @@ Other minor additions
486503
lines : String → List String
487504
```
488505

506+
* Added new function to `Data.Maybe.Base`:
507+
```agda
508+
when : Bool → A → Maybe A
509+
```
510+
511+
* Added new function to `Data.Nat.Show`:
512+
```agda
513+
readMaybe : (base : ℕ) → {base≤16 : True (base ≤? 16)} → String → Maybe ℕ
514+
```
515+
489516
* Added new functions to `Data.Tree.AVL`:
490517
```agda
491518
foldr : (∀ {k} → Val k → A → A) → A → Tree V → A

src/Data/Fin/Show.agda

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Showing finite numbers
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.Fin.Show where
10+
11+
open import Data.Fin.Base using (Fin; toℕ; fromℕ<)
12+
open import Data.Maybe.Base using (Maybe; nothing; just; _>>=_)
13+
open import Data.Nat as ℕ using (ℕ; _≤?_; _<?_)
14+
import Data.Nat.Show as ℕ
15+
open import Data.String.Base using (String)
16+
open import Function.Base
17+
open import Relation.Nullary using (yes; no)
18+
open import Relation.Nullary.Decidable using (True)
19+
20+
show : {n} Fin n String
21+
show = ℕ.show ∘′ toℕ
22+
23+
readMaybe : {n} base {base≤16 : True (base ≤? 16)} String Maybe (Fin n)
24+
readMaybe {n} base {pr} str = do
25+
nat ℕ.readMaybe base {pr} str
26+
case nat <? n of λ where
27+
(yes pr) just (fromℕ< pr)
28+
(no ¬pr) nothing

src/Data/Integer.agda

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
module Data.Integer where
1313

14-
import Data.Nat.Show as ℕ
15-
open import Data.Sign as Sign using (Sign)
16-
open import Data.String.Base using (String; _++_)
17-
1814
------------------------------------------------------------------------
1915
-- Re-export basic definition, operations and queries
2016

@@ -23,7 +19,20 @@ open import Data.Integer.Properties public
2319
using (_≟_; _≤?_; _<?_)
2420

2521
------------------------------------------------------------------------
26-
-- Conversions
22+
-- Deprecated
23+
24+
-- Version 0.17
25+
26+
open import Data.Integer.Properties public
27+
using (◃-cong; drop‿+≤+; drop‿-≤-)
28+
renaming (◃-inverse to ◃-left-inverse)
29+
30+
-- Version 1.5
31+
-- Show
32+
33+
import Data.Nat.Show as ℕ
34+
open import Data.Sign as Sign using (Sign)
35+
open import Data.String.Base using (String; _++_)
2736

2837
show : String
2938
show i = showSign (sign i) ++ ℕ.show ∣ i ∣
@@ -32,11 +41,7 @@ show i = showSign (sign i) ++ ℕ.show ∣ i ∣
3241
showSign Sign.- = "-"
3342
showSign Sign.+ = ""
3443

35-
------------------------------------------------------------------------
36-
-- Deprecated
37-
38-
-- Version 0.17
39-
40-
open import Data.Integer.Properties public
41-
using (◃-cong; drop‿+≤+; drop‿-≤-)
42-
renaming (◃-inverse to ◃-left-inverse)
44+
{-# WARNING_ON_USAGE show
45+
"Warning: show was deprecated in v1.5.
46+
Please use Data.Integer.Show's show instead."
47+
#-}

src/Data/Integer/Show.agda

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Showing integers
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.Integer.Show where
10+
11+
open import Data.Integer.Base using (ℤ; +_; -[1+_])
12+
open import Data.Nat.Base using (suc)
13+
open import Data.Nat.Show using () renaming (show to showℕ)
14+
open import Data.String.Base using (String; _++_)
15+
16+
------------------------------------------------------------------------
17+
-- Show
18+
19+
-- Decimal notation
20+
-- Time complexity is O(log₁₀(n))
21+
22+
show : String
23+
show (+ n) = showℕ n
24+
show -[1+ n ] = "-" ++ showℕ (suc n)

src/Data/Maybe/Base.agda

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ _<∣>_ : Maybe A → Maybe A → Maybe A
106106
just x <∣> my = just x
107107
nothing <∣> my = my
108108

109+
-- Just when the boolean is true
110+
111+
when : Bool A Maybe A
112+
when b c = map (const c) (boolToMaybe b)
113+
109114
------------------------------------------------------------------------
110115
-- Aligning and zipping
111116

src/Data/Nat/Show.agda

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,72 @@
88

99
module Data.Nat.Show where
1010

11-
open import Data.Char as Char using (Char)
11+
open import Data.Bool.Base using (_∧_)
12+
open import Data.Char.Base as Char using (Char)
1213
open import Data.Digit using (showDigit; toDigits; toNatDigits)
13-
open import Function.Base using (_∘_; _$_)
14-
open import Data.List.Base using (List; []; _∷_; map; reverse)
14+
open import Data.List.Base as List using (List; []; _∷_)
15+
open import Data.List.Categorical using (module TraversableA)
16+
open import Data.Maybe.Base as Maybe using (Maybe; nothing; _<∣>_; when)
17+
import Data.Maybe.Categorical as Maybe
1518
open import Data.Nat
1619
open import Data.Product using (proj₁)
1720
open import Data.String as String using (String)
21+
open import Function.Base
1822
open import Relation.Nullary.Decidable using (True)
1923

2024
------------------------------------------------------------------------
21-
-- Conversion from unary representation to the representation by the
22-
-- given base.
25+
-- Read
2326

24-
toDigitChar : (n : ℕ) Char
25-
toDigitChar n = Char.fromℕ (n + (Char.toℕ '0'))
27+
readMaybe : base {base≤16 : True (base ≤? 16)} String Maybe ℕ
28+
readMaybe _ "" = nothing
29+
readMaybe base = Maybe.map convert
30+
∘′ TraversableA.mapA Maybe.applicative readDigit
31+
∘′ String.toList
2632

27-
toDecimalChars : List Char
28-
toDecimalChars = map toDigitChar ∘ toNatDigits 10
33+
where
34+
35+
convert : List ℕ
36+
convert = List.foldl (λ acc d base * acc + d) 0
37+
38+
char0 = Char.toℕ '0'
39+
char9 = Char.toℕ '9'
40+
chara = Char.toℕ 'a'
41+
charf = Char.toℕ 'f'
42+
43+
readDigit : Char Maybe ℕ
44+
readDigit c = digit Maybe.>>= λ n when (n <ᵇ base) n where
45+
46+
charc = Char.toℕ c
47+
48+
dec = when ((char0 ≤ᵇ charc) ∧ (charc ≤ᵇ char9)) (charc ∸ char0)
49+
hex = when ((chara ≤ᵇ charc) ∧ (charc ≤ᵇ charf)) (10 + charc ∸ chara)
50+
digit = dec <∣> hex
2951

3052
------------------------------------------------------------------------
3153
-- Show
3254

55+
-- Decimal notation
3356
-- Time complexity is O(log₁₀(n))
3457

58+
toDigitChar : Char
59+
toDigitChar n = Char.fromℕ (n + Char.toℕ '0')
60+
61+
toDecimalChars : List Char
62+
toDecimalChars = List.map toDigitChar ∘′ toNatDigits 10
63+
3564
show : String
3665
show = String.fromList ∘ toDecimalChars
3766

67+
-- Arbitrary base betwen 2 & 16.
3868
-- Warning: when compiled the time complexity of `showInBase b n` is
3969
-- O(n) instead of the expected O(log(n)).
4070

4171
charsInBase : (base : ℕ)
4272
{base≥2 : True (2 ≤? base)}
4373
{base≤16 : True (base ≤? 16)}
4474
List Char
45-
charsInBase base {base≥2} {base≤16} = map (showDigit {base≤16 = base≤16})
46-
∘ reverse
75+
charsInBase base {base≥2} {base≤16} = List.map (showDigit {base≤16 = base≤16})
76+
List.reverse
4777
∘ proj₁
4878
∘ toDigits base {base≥2 = base≥2}
4979

src/Data/Rational.agda

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@
88

99
module Data.Rational where
1010

11-
open import Data.Integer as ℤ using (ℤ; +_)
12-
open import Data.String using (String; _++_)
13-
1411
------------------------------------------------------------------------
1512
-- Publicly re-export contents of core module and queries
1613

1714
open import Data.Rational.Base public
1815
open import Data.Rational.Properties public
1916
using (_≟_; _≤?_; _<?_)
2017

21-
------------------------------------------------------------------------
22-
-- Method for displaying rationals
23-
24-
show : String
25-
show p = ℤ.show (↥ p) ++ "/" ++ ℤ.show (↧ p)
26-
2718
------------------------------------------------------------------------
2819
-- Deprecated
2920

3021
-- Version 1.0
3122

3223
open import Data.Rational.Properties public
3324
using (drop-*≤*; ≃⇒≡; ≡⇒≃)
25+
26+
-- Version 1.5
27+
28+
import Data.Integer.Show as ℤ
29+
open import Data.String using (String; _++_)
30+
31+
show : String
32+
show p = ℤ.show (↥ p) ++ "/" ++ ℤ.show (↧ p)
33+
34+
{-# WARNING_ON_USAGE show
35+
"Warning: show was deprecated in v1.5.
36+
Please use Data.Rational.Show's show instead."
37+
#-}

src/Data/Rational/Show.agda

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Showing rational numbers
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
module Data.Rational.Show where
10+
11+
import Data.Integer.Show as ℤ
12+
open import Data.Rational.Base
13+
open import Data.String.Base using (String; _++_)
14+
15+
show : String
16+
show p = ℤ.show (↥ p) ++ "/" ++ ℤ.show (↧ p)

src/Text/Printf.agda

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ open import Function.Nary.NonDependent
2020
open import Function
2121
open import Strict
2222

23-
import Data.Char.Base as Cₛ
24-
import Data.Integer as ℤₛ
25-
import Data.Float as Fₛ
26-
import Data.Nat.Show as ℕₛ
23+
import Data.Char.Base as Cₛ
24+
import Data.Integer.Show as ℤₛ
25+
import Data.Float as Fₛ
26+
import Data.Nat.Show as ℕₛ
2727

2828
open import Text.Format as Format hiding (Error)
2929
open import Text.Printf.Generic

0 commit comments

Comments
 (0)