taiwan-id
Safe HaskellNone
LanguageHaskell2010

Taiwan.ID

Synopsis

Type

data ID Source #

Represents a valid 10-digit uniform identification number of the form A123456789.

By construction, invalid identification numbers are not representable by this data type.

To guarantee correctness, an ID value does not store the terminal checksum digit of the identification number it represents.

To compute the checksum digit, use checksum.

Constructors

ID 

Fields

Instances

Instances details
Finitary ID Source # 
Instance details

Defined in Taiwan.ID

Associated Types

type Cardinality ID 
Instance details

Defined in Taiwan.ID

type Cardinality ID = GCardinality (Rep ID)
Eq ID Source # 
Instance details

Defined in Taiwan.ID

Methods

(==) :: ID -> ID -> Bool #

(/=) :: ID -> ID -> Bool #

Ord ID Source # 
Instance details

Defined in Taiwan.ID

Methods

compare :: ID -> ID -> Ordering #

(<) :: ID -> ID -> Bool #

(<=) :: ID -> ID -> Bool #

(>) :: ID -> ID -> Bool #

(>=) :: ID -> ID -> Bool #

max :: ID -> ID -> ID #

min :: ID -> ID -> ID #

Generic ID Source # 
Instance details

Defined in Taiwan.ID

Methods

from :: ID -> Rep ID x #

to :: Rep ID x -> ID #

Read ID Source # 
Instance details

Defined in Taiwan.ID

Show ID Source # 
Instance details

Defined in Taiwan.ID

Methods

showsPrec :: Int -> ID -> ShowS #

show :: ID -> String #

showList :: [ID] -> ShowS #

type Cardinality ID Source # 
Instance details

Defined in Taiwan.ID

type Cardinality ID = GCardinality (Rep ID)
type Rep ID Source # 
Instance details

Defined in Taiwan.ID

Construction

fromSymbol :: forall (s :: Symbol). ValidID s => ID Source #

Constructs an ID from a type-level textual symbol.

The symbol must be exactly 10 characters in length and of the form A123456789:

>>> ID.fromSymbol @"A123456789"
ID.fromSymbol @"A123456789"

More precisely:

  • the symbol must match the regular expression ^[A-Z][1289][0-9]{8}$
  • the resultant ID must have a valid checksum.

Failure to satisfy these constraints will result in one of the following type errors:

Invalid lengths

>>> ID.fromSymbol @"A12345678"
...
... An ID must have exactly 10 characters.
...

Invalid checksums

>>> ID.fromSymbol @"A123456780"
...
... ID has invalid checksum.
...

Invalid characters

On detection of an invalid character, the resulting type error reports both the position of the character and the set of characters permitted at that position.

>>> ID.fromSymbol @"_123456789"
...
    • "_123456789"
       ^
      Character at this position must be an uppercase letter.
...
>>> ID.fromSymbol @"A_23456789"
...
    • "A_23456789"
        ^
      Character at this position must be a digit from the set {1, 2, 8, 9}.
...
>>> ID.fromSymbol @"A1_3456789"
...
    • "A1_3456789"
         ^
      Character at this position must be a digit in the range [0 .. 9].
...

Missing or empty symbols

>>> ID.fromSymbol
...
... Expected a type-level symbol of the form "A123456789".
...
>>> ID.fromSymbol @""
...
... Expected a type-level symbol of the form "A123456789".
...

fromText :: Text -> Either FromTextError ID Source #

Attempts to construct an ID from Text.

The input must be exactly 10 characters in length and of the form A123456789:

>>> ID.fromText "A123456789"
Right (ID.fromSymbol @"A123456789")

More precisely, the input must match the regular expression ^[A-Z][1289][0-9]{8}$, and the resultant ID must have a valid checksum.

This function satisfies the following law:

fromText (toText i) == Right i

data FromTextError Source #

Indicates an error that occurred while constructing an ID from Text.

Constructors

InvalidChar CharIndex CharSet

Indicates that the input text contains a character that is not allowed.

  • CharIndex specifies the position of the invalid character.
  • CharSet specifies the set of characters allowed at that position.
InvalidChecksum

Indicates that the parsed identification number has an invalid checksum.

InvalidLength

Indicates that the input text has an invalid length.

Conversion

toText :: ID -> Text Source #

Converts an ID to Text.

The output is of the form A123456789.

This function satisfies the following law:

fromText (toText i) == Right i

Validity

checksum :: ID -> Digit Source #

Computes the terminal checksum digit of an ID.

Generation

generate :: MonadRandom m => m ID Source #

Generates a random ID.

Inspection

getGender :: ID -> Gender Source #

Decodes the Gender component of an ID.

getIssuer :: ID -> Issuer Source #

Decodes the Issuer component of an ID.

getRegion :: ID -> Region Source #

Decodes the Region component of an ID.

Modification

setGender :: Gender -> ID -> ID Source #

Updates the Gender component of an ID.

setIssuer :: Issuer -> ID -> ID Source #

Updates the Issuer component of an ID.

setRegion :: Region -> ID -> ID Source #

Updates the Region component of an ID.