cantor-pairing-0.1.0.0: Convert data to and from a natural number representation

Cantor

Description

Cantor pairing gives us an isomorphism between a single natural number and pairs of natural numbers. This package provides a modern API to this functionality using GHC generics, allowing the encoding of arbitrary combinations of finite or countably infinite types in natural number form.

As a user, all you need to do is derive generic and get the instances for free.

# Example

import GHC.Generics
import Cantor

data MyType = MyType {
value1 :: [ Maybe Bool ]
, value2 :: Integer
} deriving (Generic)

instance Cantor MyType


A warning: this package will work with recursive types, but you *must* manually specify the cardinality. This unfortunately is necessary due to GHC generics marking all fields as recursive, regardless of whether or not they actually are. Still, it's straightforward to manually specify the cardinality:

# Recursive example

data Tree a = Leaf | Branch (Tree a) a (Tree a) deriving (Generic)

instance Cantor a => Cantor (Tree a) where
cardinality = Countable


If your type is finite, you can specify this by deriving the Finite typeclass, which is a subclass of Cantor:

# Finite example

data Color = Red | Green | Blue deriving (Generic)

instance Cantor Color
instance Finite Color

Synopsis

# Documentation

cantorEnumeration :: Cantor a => [a] Source #

Enumerates all values of a type by mapping toCantor over the naturals.

Cardinality can be either Finite or Countable. Countable cardinality entails that a type has the same cardinality as the natural numbers. Note that not all infinite types are countable: for example, Natural -> Natural is an infinite type, but it is not countably infinite; the basic intuition is that there is no possible way to enumerate all values of type Natural -> Natural without "skipping" almost all of them. This is in contrast to the naturals, where despite their being infinite, we can trivially (by definition, in fact!) enumerate all of them without skipping any.

Constructors

 Finite Integer Countable
Instances
 Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor MethodsshowList :: [Cardinality] -> ShowS # Source # Instance detailsDefined in Cantor Associated Typestype Rep Cardinality :: Type -> Type # Methods type Rep Cardinality Source # Instance detailsDefined in Cantor type Rep Cardinality = D1 (MetaData "Cardinality" "Cantor" "cantor-pairing-0.1.0.0-9psbWPZlnGKBdY6UCFstnv" False) (C1 (MetaCons "Finite" PrefixI False) (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Integer)) :+: C1 (MetaCons "Countable" PrefixI False) (U1 :: Type -> Type))

class Cantor a where Source #

The Cantor class gives a way to convert a type to and from the natural numbers, as well as specifies the cardinality of the type.

Minimal complete definition

Nothing

Methods

cardinality :: GCantor (Rep a) => Cardinality Source #

toCantor :: Integer -> a Source #

toCantor :: (Generic a, GCantor (Rep a)) => Integer -> a Source #

fromCantor :: a -> Integer Source #

fromCantor :: (Generic a, GCantor (Rep a)) => a -> Integer Source #

Instances
 Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Cantor () Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> () Source #fromCantor :: () -> Integer Source # Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor [a] Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> [a] Source #fromCantor :: [a] -> Integer Source # Cantor a => Cantor (Maybe a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (Min a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (Max a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (First a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (Last a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (Option a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (Identity a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (Sum a) Source # Instance detailsDefined in Cantor Methods Cantor a => Cantor (Product a) Source # Instance detailsDefined in Cantor Methods (Finite a, Cantor b) => Cantor (a -> b) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> a -> b Source #fromCantor :: (a -> b) -> Integer Source # (Cantor a, Cantor b) => Cantor (Either a b) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> Either a b Source #fromCantor :: Either a b -> Integer Source # (Cantor a, Cantor b) => Cantor (a, b) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> (a, b) Source #fromCantor :: (a, b) -> Integer Source # (Cantor a, Cantor b) => Cantor (Arg a b) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> Arg a b Source #fromCantor :: Arg a b -> Integer Source # Cantor (Proxy a) Source # Instance detailsDefined in Cantor Methods (Cantor a, Cantor b, Cantor c) => Cantor (a, b, c) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> (a, b, c) Source #fromCantor :: (a, b, c) -> Integer Source # Cantor a => Cantor (Const a b) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> Const a b Source #fromCantor :: Const a b -> Integer Source # (Cantor a, Cantor b, Cantor c, Cantor d) => Cantor (a, b, c, d) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> (a, b, c, d) Source #fromCantor :: (a, b, c, d) -> Integer Source # (Cantor a, Cantor b, Cantor c, Cantor d, Cantor e) => Cantor (a, b, c, d, e) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> (a, b, c, d, e) Source #fromCantor :: (a, b, c, d, e) -> Integer Source # (Cantor a, Cantor b, Cantor c, Cantor d, Cantor e, Cantor f) => Cantor (a, b, c, d, e, f) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> (a, b, c, d, e, f) Source #fromCantor :: (a, b, c, d, e, f) -> Integer Source # (Cantor a, Cantor b, Cantor c, Cantor d, Cantor e, Cantor f, Cantor g) => Cantor (a, b, c, d, e, f, g) Source # Instance detailsDefined in Cantor MethodstoCantor :: Integer -> (a, b, c, d, e, f, g) Source #fromCantor :: (a, b, c, d, e, f, g) -> Integer Source #

class Cantor a => Finite a where Source #

The Finite typeclass simply entails that the Cardinality of the set is finite.

Minimal complete definition

Nothing

Methods

Instances
 Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Finite () Source # Instance detailsDefined in Cantor Methods Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Maybe a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Min a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Max a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (First a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Last a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Option a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Identity a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Sum a) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Product a) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b) => Finite (a -> b) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b) => Finite (Either a b) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b) => Finite (a, b) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b) => Finite (Arg a b) Source # Instance detailsDefined in Cantor Methods Finite (Proxy a) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b, Finite c) => Finite (a, b, c) Source # Instance detailsDefined in Cantor Methods Finite a => Finite (Const a b) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b, Finite c, Finite d) => Finite (a, b, c, d) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b, Finite c, Finite d, Finite e) => Finite (a, b, c, d, e) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b, Finite c, Finite d, Finite e, Finite f) => Finite (a, b, c, d, e, f) Source # Instance detailsDefined in Cantor Methods (Finite a, Finite b, Finite c, Finite d, Finite e, Finite f, Finite g) => Finite (a, b, c, d, e, f, g) Source # Instance detailsDefined in Cantor Methods