on master help sign in
Fix totality of chr
521ef9bEdwin Brady
libs/prelude/Prelude/Bits.idr+1
@@ -5,6 +5,7 @@ import Builtins
5import Prelude.Basics
5import Prelude.Basics
6import Prelude.Bool
6import Prelude.Bool
7import Prelude.Cast
7import Prelude.Cast
8+import Prelude.Chars
8import Prelude.Classes
9import Prelude.Classes
9import Prelude.Foldable
10import Prelude.Foldable
10import Prelude.Nat
11import Prelude.Nat
libs/prelude/Prelude/Cast.idr+1−3
@@ -1,5 +1,6 @@
1module Prelude.Cast
1module Prelude.Cast
2
2
3+import Prelude.Bool
3import public Builtins
4import public Builtins
4
5
5||| Type class for transforming a instance of a data type to another type.
6||| Type class for transforming a instance of a data type to another type.
@@ -31,9 +32,6 @@ instance Cast Int Float where
31instance Cast Int Integer where
32instance Cast Int Integer where
32 cast = prim__sextInt_BigInt
33 cast = prim__sextInt_BigInt
33
34
34−instance Cast Int Char where
35− cast = prim__intToChar
36−
37-- Float casts
35-- Float casts
38
36
39instance Cast Float String where
37instance Cast Float String where
libs/prelude/Prelude/Chars.idr+10−4
@@ -1,14 +1,20 @@
1−module Prelude.Char
1+module Prelude.Chars
2-- Functions operating over Chars
2-- Functions operating over Chars
3
3
4import Prelude.Bool
4import Prelude.Bool
5import Prelude.Classes
5import Prelude.Classes
6import Prelude.List
6import Prelude.List
7+import Prelude.Cast
7import Builtins
8import Builtins
8
9
9||| Return the ASCII representation of the character.
10||| Return the ASCII representation of the character.
10chr : Int -> Char
11chr : Int -> Char
12+chr x = if (x >= 0 && x < 0x11000)
11−chr x = prim__intToChar x
13+ then assert_total (prim__intToChar x)
14+ else '\0'
15+
16+instance Cast Int Char where
17+ cast = chr
12
18
13||| Convert the number to its ASCII equivalent.
19||| Convert the number to its ASCII equivalent.
14ord : Char -> Int
20ord : Char -> Int
@@ -48,14 +54,14 @@ isNL x = x == '\r' || x == '\n'
48||| Non-letters are ignored.
54||| Non-letters are ignored.
49toUpper : Char -> Char
55toUpper : Char -> Char
50toUpper x = if (isLower x)
56toUpper x = if (isLower x)
51− then (prim__intToChar (prim__charToInt x - 32))
57+ then assert_total (prim__intToChar (prim__charToInt x - 32))
52 else x
58 else x
53
59
54||| Convert a letter to the corresponding lower-case letter, if any.
60||| Convert a letter to the corresponding lower-case letter, if any.
55||| Non-letters are ignored.
61||| Non-letters are ignored.
56toLower : Char -> Char
62toLower : Char -> Char
57toLower x = if (isUpper x)
63toLower x = if (isUpper x)
58− then (prim__intToChar (prim__charToInt x + 32))
64+ then assert_total (prim__intToChar (prim__charToInt x + 32))
59 else x
65 else x
60
66
61||| Returns true if the character is a hexadecimal digit i.e. in the range [0-9][a-f][A-F]
67||| Returns true if the character is a hexadecimal digit i.e. in the range [0-9][a-f][A-F]
src/Idris/Primitives.hs+1−1
@@ -124,7 +124,7 @@ primitives =
124 Prim (sUN "prim__charToInt") (ty [(AType (ATInt ITChar))] (AType (ATInt ITNative))) 1 (c_charToInt)
124 Prim (sUN "prim__charToInt") (ty [(AType (ATInt ITChar))] (AType (ATInt ITNative))) 1 (c_charToInt)
125 (1, LChInt ITNative) total,
125 (1, LChInt ITNative) total,
126 Prim (sUN "prim__intToChar") (ty [(AType (ATInt ITNative))] (AType (ATInt ITChar))) 1 (c_intToChar)
126 Prim (sUN "prim__intToChar") (ty [(AType (ATInt ITNative))] (AType (ATInt ITChar))) 1 (c_intToChar)
127− (1, LIntCh ITNative) total,
127+ (1, LIntCh ITNative) partial,
128 Prim (sUN "prim__strToFloat") (ty [StrType] (AType ATFloat)) 1 (c_strToFloat)
128 Prim (sUN "prim__strToFloat") (ty [StrType] (AType ATFloat)) 1 (c_strToFloat)
129 (1, LStrFloat) total,
129 (1, LStrFloat) total,
130 Prim (sUN "prim__floatToStr") (ty [(AType ATFloat)] StrType) 1 (c_floatToStr)
130 Prim (sUN "prim__floatToStr") (ty [(AType ATFloat)] StrType) 1 (c_floatToStr)