Popularity
4.1
Growing
Activity
0.0
Stable
13
1
1

Monthly Downloads: 24
Programming language: Haskell
License: MIT License
Tags: Data    
Latest version: v0.2.0.0

cantor-pairing alternatives and similar packages

Based on the "Data" category.
Alternatively, view cantor-pairing alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of cantor-pairing or a related project?

Add another 'Data' Package

README

cantor-pairing

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,Cantor)

This should work nicely even with simple inductive types:

Recursive example

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

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,Cantor,Finite)

Mutually-recursive types

If you have mutually-recursive types, unfortunately you'll need to manually specify the cardinality for now, but you can still get the to/from encodings for free:

data Foo = FooNil | Foo Bool Bar deriving (Generic,Show)
data Bar = BarNil | Bar Bool Foo deriving (Generic,Show)

instance Cantor Foo where
  cardinality = Countable
instance Cantor Bar