| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Refined.Extras.Predicates.Text
Synopsis
- data SymEqualTo (c :: Symbol)
- data Space
- data Lower
- data Upper
- data Alpha
- data AlphaNum
- data Letter
- data Mark
- data Number
- data Punctuation
- data Symbol
- data Separator
- data Control
- data Digit
- data OctDigit
- data HexDigit
- data Ascii
- data Latin1
- data AsciiUpper
- data AsciiLower
- data AsciiAlpha
- data AsciiAlphaNum
Symbol Equality
data SymEqualTo (c :: Symbol) Source #
Predicate equality for symbols.
Examples
>>>validate @(SymEqualTo "c") Proxy 'c'Nothing
>>>showRefineException <$> validate @(SymEqualTo "abc") Proxy 'c'Just "RefineOtherException (SymEqualTo \"abc\") \"c is not a single Char\""
>>>validate @(SymEqualTo "abc") Proxy "abc"Nothing
>>>showRefineException <$> validate @(SymEqualTo "123") @Text Proxy "abc"Just "RefineOtherException (SymEqualTo \"123\") \"abc does not equal the predicate\""
Since: 0.1.0.0
Instances
Char Predicates
This section models the boolean functions defined in Data.Char. See that module for more information regarding these definitions.
These instances are defined for Char and Word8, though they can be
extended to String, Text, and ByteString via
All.
Note: Although Char has instances for all of these predicates, some are
missing for Word8 (and by extension ByteString).
This is due to Word8's size restriction, i.e., a single byte
0 <= b <= 255. Thus, predicates that extend over the entirety of the
unicode range (e.g. Alpha) do not have Word8 instances, as this could be
misleading. For instance, consider the unicode character "ɦ" (U+0266):
>>>:{let txt = T.singleton ch -- ch == 0x0266 i.e. ɦ in isRight $ refine @(All Alpha) txt :} True
This Char is part of the alpha unicode category, so the refinement
succeeds. On the other hand, suppose we have a Word8 instance that
performs the obvious :isAlpha . w2c
>>>:{let bs = encodeUtf8 $ T.singleton ch in isRight $ refine @(All Alpha) bs :} False
The problem is that we are checking the underlying bytes if they satisfy
isAlpha, but this is only true for Ascii alpha characters. Morally, our
bytestring is this structure:
>>>BS.foldr (:) [] (encodeUtf8 $ T.singleton ch)[201,166]
Our Alpha refinement fails because the individual byte components of
"ɦ" are not themselves considered "alpha" characters (indeed this will only
occur due to chance). These are the options:
- Outlaw
Word8/ByteStringinstances completely. - Provide the naive
implementation forisAlpha.w2cWord8. - Implement
ByteStringinstances by first converting toText, i.e., do not use its underlying fold. - Provide
Word8instances only when they coincide withChar(i.e. ascii/latin1 predicates). In this case,ByteStringworks as expected; that is, we can make assertions based on the underlying bytes, but nothing that requires a specific encoding, and we do not get surprised byText/ByteStringmismatches.
Of these, only one and four are reasonable. Two is out because it can have confusing semantics (illustrated above).
Three is rejected because the API is no longer consistent, and we
have to arbitrarily assume the ByteString shares its
Text encoding (i.e. UTF-8).
One is defensible, but we choose option four, reasoning that it could be useful to assert that a given bytestring contains only ascii numbers or alpha characters while avoiding the pitfalls of reusing predicates intended for arbitrary unicode.
Unicode
These predicates are for unicode code points, hence they are available for
Char (thus String, Text).
Predicate for a isSpace.
Examples
>>>validate @Space Proxy ' 'Nothing>>>validate @Space Proxy '\r'Nothing
>>>showRefineException <$> validate @Space Proxy 'a'Just "RefineOtherException (Space) \"a is not a space character\""
Since: 0.1.0.0
Predicate for isLower.
Examples
>>>validate @Lower Proxy 'c'Nothing
>>>showRefineException <$> validate @Lower Proxy 'C'Just "RefineOtherException (Lower) \"C is not lowercase\""
Since: 0.1.0.0
Predicate for isUpper.
Examples
>>>validate @Upper Proxy 'C'Nothing
>>>showRefineException <$> validate @Upper Proxy 'c'Just "RefineOtherException (Upper) \"c is not uppercase\""
Since: 0.1.0.0
Predicate for isAlpha.
Examples
>>>validate @Alpha Proxy 'c'Nothing
>>>validate @Alpha Proxy 'ɦ'Nothing
>>>showRefineException <$> validate @Alpha Proxy '7'Just "RefineOtherException (Alpha) \"7 is not an alphabetic character\""
Since: 0.1.0.0
Predicate for isAlphaNum.
Examples
>>>validate @AlphaNum Proxy 'a'Nothing
>>>validate @AlphaNum Proxy '1'Nothing
>>>showRefineException <$> validate @AlphaNum Proxy '!'Just "RefineOtherException (AlphaNum) \"! is not an alpha-numeric character\""
Since: 0.1.0.0
Predicate for isLetter.
Examples
>>>validate @Letter Proxy 'f'Nothing
>>>showRefineException <$> validate @Letter Proxy '\r'Just "RefineOtherException (Letter) \"\\r is not a letter\""
Since: 0.1.0.0
Predicate for isMark.
Examples
>>>validate @Mark Proxy '\x20DD'Nothing
>>>showRefineException <$> validate @Mark Proxy 'a'Just "RefineOtherException (Mark) \"a is not a mark\""
Since: 0.1.0.0
Predicate for isNumber.
Examples
>>>validate @Number Proxy '2'Nothing
>>>showRefineException <$> validate @Number Proxy 'a'Just "RefineOtherException (Number) \"a is not a number\""
Since: 0.1.0.0
data Punctuation Source #
Predicate for isPunctuation.
Examples
>>>validate @Punctuation Proxy '!'Nothing
>>>showRefineException <$> validate @Punctuation Proxy 'a'Just "RefineOtherException (Punctuation) \"a is not punctuation\""
Since: 0.1.0.0
Instances
| Generic Punctuation Source # | |||||
Defined in Refined.Extras.Predicates.Text Associated Types
| |||||
| Predicate Punctuation Char Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy Punctuation -> Char -> Maybe RefineException Source # | |||||
| type Rep Punctuation Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text | |||||
Predicate for isSymbol.
Examples
>>>validate @Symbol Proxy '$'Nothing
>>>showRefineException <$> validate @Symbol Proxy 'a'Just "RefineOtherException (Symbol) \"a is not a symbol\""
Since: 0.1.0.0
Predicate for isSeparator.
Examples
>>>validate @Separator Proxy ' 'Nothing
>>>showRefineException <$> validate @Separator Proxy 'a'Just "RefineOtherException (Separator) \"a is not a separator\""
Since: 0.1.0.0
Ascii/Latin1
These predicates are for ascii/latin1. Thus they will work for Char
(and String, Text) and Word8 (hence ByteString).
Predicate for isControl.
Examples
>>>validate @Control Proxy '\r'Nothing
>>>showRefineException <$> validate @Control Proxy 'a'Just "RefineOtherException (Control) \"a is not a control character\""
Since: 0.1.0.0
Instances
| Generic Control Source # | |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Control Word8 Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Control Char Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| type Rep Control Source # | Since: 0.1.0.0 |
Predicate for isDigit.
Examples
>>>validate @Digit Proxy '1'Nothing
>>>showRefineException <$> validate @Digit Proxy 'a'Just "RefineOtherException (Digit) \"a is not a digit\""
Since: 0.1.0.0
Instances
| Generic Digit Source # | |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Digit Word8 Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Digit Char Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| type Rep Digit Source # | Since: 0.1.0.0 |
Predicate for isOctDigit.
Examples
>>>validate @OctDigit Proxy '4'Nothing
>>>showRefineException <$> validate @OctDigit Proxy '9'Just "RefineOtherException (OctDigit) \"9 is not an octal digit\""
Since: 0.1.0.0
Instances
| Generic OctDigit Source # | |
Defined in Refined.Extras.Predicates.Text | |
| Predicate OctDigit Word8 Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| Predicate OctDigit Char Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| type Rep OctDigit Source # | Since: 0.1.0.0 |
Predicate for isHexDigit.
Examples
>>>validate @HexDigit Proxy '1'Nothing
>>>validate @HexDigit Proxy 'f'Nothing
>>>showRefineException <$> validate @HexDigit Proxy 'g'Just "RefineOtherException (HexDigit) \"g is not a hexadecimal digit\""
Since: 0.1.0.0
Instances
| Generic HexDigit Source # | |
Defined in Refined.Extras.Predicates.Text | |
| Predicate HexDigit Word8 Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| Predicate HexDigit Char Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| type Rep HexDigit Source # | Since: 0.1.0.0 |
Predicate for isAscii.
Examples
>>>validate @Ascii Proxy 'a'Nothing
>>>showRefineException <$> validate @Ascii Proxy '\x20DD'Just "RefineOtherException (Ascii) \"\\8413 is not ascii\""
Since: 0.1.0.0
Instances
| Generic Ascii Source # | |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Ascii Word8 Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Ascii Char Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| type Rep Ascii Source # | Since: 0.1.0.0 |
Predicate for Latin1.
Examples
>>>validate @Latin1 Proxy 'a'Nothing
>>>showRefineException <$> validate @Latin1 Proxy '\x20DD'Just "RefineOtherException (Latin1) \"\\8413 is not latin1\""
Since: 0.1.0.0
Instances
| Generic Latin1 Source # | |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Latin1 Word8 Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| Predicate Latin1 Char Source # | Since: 0.1.0.0 |
Defined in Refined.Extras.Predicates.Text | |
| type Rep Latin1 Source # | Since: 0.1.0.0 |
data AsciiUpper Source #
Predicate for isAsciiUpper.
Examples
>>>validate @AsciiUpper Proxy 'A'Nothing
>>>showRefineException <$> validate @AsciiUpper Proxy 'a'Just "RefineOtherException (AsciiUpper) \"a is not uppercase ascii\""
Since: 0.1.0.0
Instances
| Generic AsciiUpper Source # | |||||
Defined in Refined.Extras.Predicates.Text Associated Types
| |||||
| Predicate AsciiUpper Word8 Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiUpper -> Word8 -> Maybe RefineException Source # | |||||
| Predicate AsciiUpper Char Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiUpper -> Char -> Maybe RefineException Source # | |||||
| type Rep AsciiUpper Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text | |||||
data AsciiLower Source #
Predicate for isAsciiLower.
Examples
>>>validate @AsciiLower Proxy 'a'Nothing
>>>showRefineException <$> validate @AsciiLower Proxy 'A'Just "RefineOtherException (AsciiLower) \"A is not lowercase ascii\""
Since: 0.1.0.0
Instances
| Generic AsciiLower Source # | |||||
Defined in Refined.Extras.Predicates.Text Associated Types
| |||||
| Predicate AsciiLower Word8 Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiLower -> Word8 -> Maybe RefineException Source # | |||||
| Predicate AsciiLower Char Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiLower -> Char -> Maybe RefineException Source # | |||||
| type Rep AsciiLower Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text | |||||
data AsciiAlpha Source #
Predicate for isAscii and isAlpha, primarily intended for Word8.
Redundant for Char, as this is equivalent to (Ascii && Alpha), but we
include Char for completeness.
Examples
>>>validate @AsciiAlpha Proxy 'a'Nothing
>>>showRefineException <$> validate @AsciiAlpha Proxy '1'Just "RefineOtherException (AsciiAlpha) \"1 is not alpha ascii\""
>>>showRefineException <$> validate @AsciiAlpha Proxy 'ɦ'Just "RefineOtherException (AsciiAlpha) \"\\614 is not alpha ascii\""
Since: 0.1.0.0
Instances
| Generic AsciiAlpha Source # | |||||
Defined in Refined.Extras.Predicates.Text Associated Types
| |||||
| Predicate AsciiAlpha Word8 Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiAlpha -> Word8 -> Maybe RefineException Source # | |||||
| Predicate AsciiAlpha Char Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiAlpha -> Char -> Maybe RefineException Source # | |||||
| type Rep AsciiAlpha Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text | |||||
data AsciiAlphaNum Source #
Predicate for isAscii and isAlphaNum, primarily intended for
Word8. Redundant for Char, as this is equivalent to
(Ascii && AlphaNum), but we include Char for completeness.
Examples
>>>validate @AsciiAlphaNum Proxy 'a'Nothing
>>>validate @AsciiAlphaNum Proxy '1'Nothing
>>>showRefineException <$> validate @AsciiAlphaNum Proxy '1'Nothing
>>>showRefineException <$> validate @AsciiAlphaNum Proxy 'ɦ'Just "RefineOtherException (AsciiAlphaNum) \"\\614 is not alpha-numeric ascii\""
Since: 0.1.0.0
Instances
| Generic AsciiAlphaNum Source # | |||||
Defined in Refined.Extras.Predicates.Text Associated Types
| |||||
| Predicate AsciiAlphaNum Word8 Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiAlphaNum -> Word8 -> Maybe RefineException Source # | |||||
| Predicate AsciiAlphaNum Char Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text Methods validate :: Proxy AsciiAlphaNum -> Char -> Maybe RefineException Source # | |||||
| type Rep AsciiAlphaNum Source # | Since: 0.1.0.0 | ||||
Defined in Refined.Extras.Predicates.Text | |||||