roc-id
Safe HaskellNone
LanguageHaskell2010

ROC.ID

Synopsis

Type

data ID Source #

Represents a valid 10-digit ROC (Taiwan) 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 ROC.ID

Associated Types

type Cardinality ID 
Instance details

Defined in ROC.ID

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

Defined in ROC.ID

Methods

from :: ID -> Rep ID x #

to :: Rep ID x -> ID #

Read ID Source # 
Instance details

Defined in ROC.ID

Show ID Source # 
Instance details

Defined in ROC.ID

Methods

showsPrec :: Int -> ID -> ShowS #

show :: ID -> String #

showList :: [ID] -> ShowS #

Eq ID Source # 
Instance details

Defined in ROC.ID

Methods

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

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

Ord ID Source # 
Instance details

Defined in ROC.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 #

type Cardinality ID Source # 
Instance details

Defined in ROC.ID

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

Defined in ROC.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.

getLocation :: ID -> Location Source #

Decodes the Location component of an ID.

getNationality :: ID -> Nationality Source #

Decodes the Nationality component of an ID.

Modification

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

Updates the Gender component of an ID.

setLocation :: Location -> ID -> ID Source #

Updates the Location component of an ID.

setNationality :: Nationality -> ID -> ID Source #

Updates the Nationality component of an ID.