mmzk-typeid

Introduction

A TypeID implementation in Haskell. It is a “type-safe, K-sortable, globally unique identifier” extended on top of UUIDv7.

TypeIDs are canonically encoded as lowercase strings consisting of three parts:

  1. A type prefix (at most 63 characters in all lowercase snake_case ASCII [a-z_]);
  2. An underscore ‘_’ separator;
  3. A 128-bit UUIDv7 encoded as a 26-character string using a modified base32 encoding.

For more information, please check out specification v0.3.0.

It also serves as a (temporary) UUIDv7 implementation in Haskell, since there are no official ones yet.

If you notice any issues or have any suggestions, please feel free to open an issue or contact me via email.

Highlights

In addition to the features provided by TypeID, this implementation also supports:

  1. Generating TypeIDs in a batch. They are guaranteed to have the same timestamp (up to the first 32768 ids) and of ascending order;
  2. Encoding the prefix in the type level, so that if you accidentally pass in an invalid prefix, the code won’t compile, avoiding the need for runtime checks;
  3. Support TypeID with other UUID versions. Currently v7 (default), v1, v4, and v5 are supported.

Quick start

{-# LANGUAGE OverloadedStrings #-}

import           Control.Exception
import           Data.TypeID (TypeID)
import qualified Data.TypeID as TID

main :: IO ()
main = do

  -- Make a TypeID with prefix 'mmzk':
  typeID <- TID.genTypeID "mmzk"
  putStrLn $ TID.toString typeID

  -- Get components from the TypeID:
  let prefix = TID.getPrefix typeID -- "mmzk"
      uuid   = TID.getUUID typeID
      time   = TID.getTime typeID -- A 'Word64' representing the timestamp in milliseconds

  -- Make a TypeID without prefix:
  typeID' <- TID.genTypeID ""
  print typeID'

  -- Make 10 TypeIDs in a batch. They are guaranteed to have the same timestamp and of ascending order:
  typeIDs <- TID.genTypeIDs "mmzk" 10
  mapM_ print typeIDs

  -- Parse a TypeID from string:
  case TID.parseString "mmzk_01h455vb4pex5vsknk084sn02q" of
    Left err     -> throwIO err
    Right typeID -> print typeID

For a full list of functions on TypeID, see Data.TypeID.

More Usages

TypeID with other UUID Versions

We also support TypeID using some other versions of UUID, including v1, v4 and v5, which loses the monoticity property. To use it, simply import Data.TypeID.V4 instead of Data.TypeID. The following is an example using v4:

{-# LANGUAGE OverloadedStrings #-}

import           Control.Exception
import           Data.TypeID.V4 (TypeIDV4)
import qualified Data.TypeID.V4 as TID

main :: IO ()
main = do

  -- Make a TypeID with prefix 'mmzk':
  typeID <- TID.genTypeID "mmzk"
  putStrLn $ TID.toString typeID

  -- Get components from the TypeID:
  let prefix = TID.getPrefix typeID -- "mmzk"
      uuid   = TID.getUUID typeID

  -- Make a TypeID without prefix:
  typeID' <- TID.genTypeID ""
  print typeID'

  -- Parse a TypeID from string:
  case TID.parseString "mmzk_5hjpeh96458fct8t49fnf9farw" of
    Left err     -> throwIO err
    Right typeID -> print typeID

Type-level TypeID (KindID)

When using TypeID, if we want to check if the type matches, we usually need to get the prefix of the TypeID and compare it with the desired prefix at runtime. However, with Haskell’s type system, we can do this at compile time instead. We call this TypeID with compile-time prefix a KindID.

Of course, that would require the desired prefix to be known at compile time. This is actually quite common, especially when we are using one prefix for one table in the database.

For example, suppose we have a function that takes a KindID with the prefix “user”, it may have a signature like this: f :: KindID "user" -> IO ().

Then if we try to pass in a KindID with the prefix “post”, the compiler will complain, thus removing the runtime check and the associated overhead.

All the prefixes are type-checked at compile time, so if we try to pass in invalid prefixes, the compiler (again) will complain.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

import           Control.Exception
import           Data.KindID (KindID)
import qualified Data.KindID as KID

main :: IO ()
main = do

  -- Make a KindID with prefix 'mmzk':
  kindID <- KID.genKindID @"mmzk" -- Has type `KindID "mmzk"`
  putStrLn $ KID.toString kindID

  -- Get components from the KindID:
  let prefix = KID.getPrefix kindID -- "mmzk"
      uuid   = KID.getUUID kindID
      time   = KID.getTime kindID -- A 'Word64' representing the timestamp in milliseconds

  -- Make a KindID without prefix:
  kindID' <- KID.genKindID @"" -- Has type `KindID ""`
  print kindID'

  -- Make 10 KindIDs in a batch. They are guaranteed to have the same timestamp and of ascending order:
  kindIDs <- KID.genKindIDs @"mmzk" 10
  mapM_ print kindIDs

  -- Parse a KindID from string:
  case KID.parseString @"mmzk" "mmzk_01h455vb4pex5vsknk084sn02q" of
    Left err     -> throwIO err
    Right kindID -> print kindID

For a full list of functions on KindID, see Data.KindID.

Functions with More General Types

TypeID and KindID shares many functions with the same name and functionality. So far, we are using qualified imports to diffentiate them (e.g KID.fromString and TID.fromString). Alternatively, we can use the methods of IDConv to use the same functions for both TypeID and KindID.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplication #-}

import           Control.Exception
import           Data.KindID
import           Data.TypeID

main :: IO ()
main = do

  -- Make a TypeID with prefix 'mmzk':
  typeID <- genID @TypeID "mmzk"
  print typeID

  -- Make a KindID with prefix 'mmzk':
  kindID <- genID @(KindID "mmzk")
  print kindID

  -- Parse a TypeID from string:
  case string2ID "mmzk_01h455vb4pex5vsknk084sn02q" :: Maybe TypeID of
    Left err     -> throwIO err
    Right typeID -> print typeID

  -- Parse a KindID from string:
  case string2ID "mmzk_01h455vb4pex5vsknk084sn02q" :: Maybe (KindID "mmzk") of
    Left err     -> throwIO err
    Right kindID -> print kindID

  -- Parse a KindID from string (wrong prefix):
  case string2ID "mmzk_01h455vb4pex5vsknk084sn02q" :: Maybe (KindID "foo") of
    Left err     -> throwIO err -- Will throw here as the prefix matches not
    Right kindID -> print kindID

We no longer need to use qualified imports, but on the down side, we need to add explicit type annotations. Therefore it is a matter of preference.

Note that with the class methods, the type application with Symbol no longer works as the full type must be provided. For example, string2ID @"mmzk" "mmzk_01h455vb4pex5vsknk084sn02q" will not compile.

For a full list of these functions, see Data.TypeID.Class.

KindID with Data Kinds

Instead of using raw Symbols as KindID prefixes, we can also define our custom data type for better semantics.

For example, suppose we have three tables for users, posts, and comments, and each table has a unique prefix, we can design the structure as following:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}

import           Data.KindID
import           Data.KindID.Class

data Prefix = User | Post | Comment

instance ToPrefix 'User where
  type PrefixSymbol 'User = "user"

instance ToPrefix 'Post where
  type PrefixSymbol 'Post = "post"

instance ToPrefix 'Comment where
  type PrefixSymbol 'Comment = "comment"

Now we can use Prefix as a prefix for KindIDs, e.g.

main :: IO ()
main = do
  -- ...
  userID    <- genKindID @'User -- Same as genKindID @"user"
  postID    <- genKindID @'Post -- Same as genKindID @"post"
  commentID <- genKindID @'Comment -- Same as genKindID @"comment"
  -- ...

For more information, see Data.KindID.Class.

Note

Functions not explicitly exported are considered internal and are subjected to changes.

Changes

Revision history for mmzk-typeid

0.7.1.1 – 2026-05-13

  • Support random 1.3.

  • Tested with GHC 9.14.1.

  • Kindly contributed by @ysangkok.

0.7.1.0 – 2026-03-18

  • Add custom timestamp support for V7 UUID/TypeID/KindID generation.

    • New functions genUUIDWithTime, genUUIDWithTime', and genUUIDsWithTime in Data.UUID.V7.
    • New functions genTypeIDWithTime, genTypeIDWithTime', and genTypeIDsWithTime in Data.TypeID.V7 (and re-exported from Data.TypeID).
    • New unsafe variants unsafeGenTypeIDWithTime, unsafeGenTypeIDWithTime', and unsafeGenTypeIDsWithTime in Data.TypeID.V7.Unsafe (and re-exported from Data.TypeID.Unsafe).
    • New functions genKindIDWithTime, genKindIDWithTime', and genKindIDsWithTime in Data.KindID.V7 (and re-exported from Data.KindID).
    • These functions accept a Word64 timestamp (milliseconds since Unix epoch) and do not interact with the global monotonic state.
  • Drop redundant Typeable derivation.

  • More tests.

  • Kindly contributed by @johnhampton.

0.7.0.2 – 2025-04-22

  • Fix a Haddock error on ValidPrefix related to conditional compilation.

  • Add more tests.

  • CI test on the latest GHC version.

0.7.0.1 – 2024-10-10

  • Add Generic derivation to TypeID', KindID', and UUIDVersion.

  • Test the type-level constraints of KindID using an external GHC interpreter.

  • Stop testing on GHC 9.2, replacing with tests on GHC 9.4.

  • Update description in “Data.UUID.V7” to reflect the fact that UUIDv7 standard has been finalised.

0.7.0.0 – 2024-07-03

  • Use Strings instead of Texts inside the constructors of TypeIDError so that it is easier to use the promoted constructors.

  • More complete compile-time error messages for KindID errors.

  • Hide away internal type-level programming details from Haddock.

    • Many of the type-level helper declarations are now internal.

0.6.3.1 – 2024-06-23

  • Relax the version constraint on bytestring dependency.
    • Kindly contributed by @jflanglois.

0.6.3.0 – 2024-06-01

  • Update implementation so that the prefix for KindID now conforms with specification v0.3.0.

  • More useful compile-time errors for invalid KindID prefixes.

  • TypeIDError now includes the contextual prefix when necessary to produce better error messages.

  • More tests.

  • Fix known documentation typos.

0.6.2.0 – 2024-05-28

  • Fix the bug where the first 32768 TypeIDs may not of the same timestamp.

  • Test on GHC 9.8.2.

0.6.0.1 – 2024-04-26

  • Fix typo in the maintainer’s email address.

    • Astounded at the fact that I mismatched the local part and domain name and didn’t realise it for a year.
  • More test cases on parsing.

  • Fix other typos and inconsistencies in the documentation.

0.6.0.0 – 2024-04-19

  • Update implementation to conform with specification v0.3.0.

    • Allow TypeID prefix to contain underscores.
    • Update parsing logic as well as Binary and Storable instances to reflect the changes.
    • Add tests.
  • Have a breaking change in TypeIDError to better reflect the error cases for the update in the specification.

0.5.0.2 – 2024-3-10

  • Add Typeable and Data instances for TypeID and KindID.

    • Kindly contributed by @shinzui.
  • Fix all warnings.

0.5.0.1 – 2023-9-18

  • Fix bad links in the documentation.

0.5.0.0 – 2023-08-31

  • Support TypeID and KindID with UUID suffixes of version 5.

    • They are exported in Data.TypeID.V5 and Data.KindID.V5.
  • Tests for V5 TypeID and KindID.

  • Change signature for genID_ to support UUIDv5.

  • Decide against moving the decorate method.

0.4.0.1 – 2023-08-19

  • Support TypeID and KindID with UUID suffixes of version 1.

    • They are exported in Data.TypeID.V1 and Data.KindID.V1.
  • Tests for V1 TypeID and KindID.

  • Fix documentation typos.

  • The decorate method will be moved from IDGen to IDType in the next major release.

  • The type signature for genID_ is likely to change in the next major release to support UUIDv5. Hopefully it will not affect any existing concrete functions.

0.4.0.0 – 2023-08-08

  • Support TypeID and KindID with UUID suffixes of version 4.

    • They are exported in Data.TypeID.V4 and Data.KindID.V4.
    • By default, TypeID and KindID has a UUID suffix of version 7.
    • The default TypeID and KindID is also exported via Data.TypeID.V7 and Data.KindID.V7.
    • The constructor shapes have been changed, but it should not cause any problems since they are not exported.
  • Remove deprecated nil functions.

  • Provide some default implementations for methods of IDConv.

  • Fix typoes in the Haddock.

  • Tests for V4 TypeID and KindID.

0.3.1.0 – 2023-07-23

  • Add parseStringM, parseTextM, and parseByteStringM to IDConv.

    • Instead of returning an Either, they throw an exception when the input is invalid.
  • Add unsafe methods to IDConv.

  • Implement Storable and Binary instances for TypeID and KindID.

    • These instances are experimental since the specification does not propose any serialisation format.

0.3.0.1 – 2023-07-18

  • Add a version upper-bound for ‘uuid-types’.

  • Fix documentation typos.

0.3.0.0 – 2023-07-17

  • Use ‘uuid-types’ package’s UUID instead of a custom type.

    • Data.UUID.V7 only retains the generation functions.
    • Other modules are not affected by this change.
  • Add Read and Hashable instances for TypeID and KindID.

  • Move ValidPrefix and ToPrefix to Data.KindID.Class module.

    • They are no longer exported from Data.KindID.
  • Remove deprecated functions unUUID, parseStringWithPrefix, parseTextWithPrefix, parseByteStringWithPrefix, nil, and decorate.

  • Re-implement Show instances for TypeID and KindID using pretty-print toString.

  • Implement TypeID generation based on stateless UUIDv7.

    • It is faster but does not guarantee monotonicity if multiple processes are generating TypeIDs at the same time.
  • Introduce unsafe TypeID and KindID functions for parsing and generating. They do not check the validity of the input and only behave well when the input is guaranteed to be valid.

  • Add validity check on TypeID and KindID generation.

    • checkID checks the prefix and the UUID’s version and variant.
    • checkIDWithEnv also checks that the UUID is generated in the past.
  • Deprecate nilTypeID and nilKindID since they are not useful.

  • Remove dependency on ‘transformers’.

  • Fix typos in the documentation.

  • Update README examples.

  • More tests.

0.2.0.0 – 2023-07-14

  • Implement KindID to take arbitrary prefix type.

    • It can be a Symbol as before, but it can also be any type that implements ToPrefix which dictates how to translate the prefix type to a Symbol.
  • Fix orphan instances for TypeID and KindID.

  • Add FromJSONKey and ToJSONKey instances for TypeID and KindID.

  • Introduce IDType class to unify the getPrefix, getUUID, and getTime functions of TypeID and KindID.

  • Introduce IDConv class to unify the various conversion functions between TypeID/KindID and String/Text/ByteString.

    • The original concrete functions remain, and the class is provided as an alternative.
  • Make the generation functions work with any MonadIO than just IO.

  • Introduct IDGen class to unify the generation functions for TypeID and KindID.

    • The original concrete functions remain, and the class is provided as an alternative.
  • Deprecate unUUID, parseStringWithPrefix, parseTextWithPrefix, parseByteStringWithPrefix, nil, and decorate. They are either replaced by functions of other names or are no longer necessary.

    • They will be removed in the next major version.
  • The UUID type is expected to be removed in the next major version in favour of the type from the ‘uuid-types’ package.

  • More tests.

0.1.0.0 – 2023-07-11

  • First version. Released on an unsuspecting world.

  • Implement TypeID as specified at https://github.com/jetpack-io/typeid.

  • Add unit tests.

  • Add type-level TypeID prefixes.

  • Add FromJSON and ToJSON instances for TypeID and KindID.