bijective collection encodings and boolean operations with

30
Bijective Collection Encodings and Boolean Operations with Hereditarily Binary Natural Numbers Paul Tarau Department of Computer Science and Engineering University of North Texas PPDP’2014 – research supported by NSF grant 1423324 – Paul Tarau (University of North Texas) Encodings w. Hereditarily Binary Natural Numbers PPDP’2014 1 / 30

Upload: others

Post on 13-Jan-2022

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bijective Collection Encodings and Boolean Operations with

Bijective Collection Encodings and Boolean Operationswith Hereditarily Binary Natural Numbers

Paul Tarau

Department of Computer Science and EngineeringUniversity of North Texas

PPDPrsquo2014

ndash research supported by NSF grant 1423324 ndash

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 1 30

Overview

our tree-based hereditarily binary numbers apply recursively a run-lengthcompression mechanism on iterated function applications seen as ldquodigitsrdquo

they enable performing arithmetic computations symbolically and lifttractability of computations to be limited by the representation size of theiroperands rather than by their bitsizesin this paper we apply hereditarily binary numbers to derive

1 compact representations for ldquostructurally simplerdquo (sparse or dense) listssets and multisets as well as their hereditarily finite counterparts

2 bijective size-proportionate Goumldel numberings for several data typesldquovirtualizedrdquo through a generic data type transformation framework

3 a size-proportionate Goumldel numbering scheme for term algebras4 boolean operations5 operations on bitvectors and sets

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 2 30

Outline

1 Bijective base-2 numbers as iterated function applications

2 The arithmetic interpretation of hereditarily binary numbers

3 Constant average and worst case constant or loglowast operations

4 Virtual types through bijective data transformations

5 Hereditarily finite lists sets and multisets

6 A bijective size-proportionate encoding of term algebras

7 Boolean and set operations with hereditarily finite numbers

8 Conclusion

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 3 30

Bijective base-2 numbers as iterated function applications

Natural numbers can be seen as iterated applications of the functions

o(x) = 2x +1

i(x) = 2x +2

corresponding the so called bijective base-2 representation

1 = o(0)

2 = i(0)

3 = o(o(0))

4 = i(o(0))

5 = o(i(0))

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 4 30

Iterated applications of o and i some useful identities

on(k) = 2n(k +1)minus1 (1)

in(k) = 2n(k +2)minus2 (2)

and in particularon(0) = 2nminus1 (3)

in(0) = 2n+1minus2 (4)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 5 30

Hereditarily binary numbers

Hereditarily binary numbers are defined as the Haskell type T

data T = E | V T [T] | W T [T] deriving (EqReadShow)

corresponding to the recursive data type equation T= 1+TtimesTlowast+TtimesTlowast

the term E (empty leaf) corresponds to zero

the term V x xs counts the number x+1 of o applications followed by analternation of similar counts of i and o applications

the term W x xs counts the number x+1 of i applications followed by analternation of similar counts of o and i applications

the same principle is applied recursively for the counters until the emptysequence is reached

note x counts x+1 applications as we start at 0

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 6 30

The arithmetic interpretation of hereditarily binary numbers

DefinitionThe bijection n Trarr N defines the unique natural number associated to aterm of type T Its inverse is denoted t Nrarr T

n(t) =

0 if t = E

2n(x)+1minus1 if t = V x []

(n(u)+1)2n(x)+1minus1 if t = V x (yxs) and u = W y xs

2n(x)+2minus2 if t = W x []

(n(u)+2)2n(x)+1minus2 if t = W x (yxs) and u = V y xs

(5)

The computation of n(W (V E []) [EEE]) expands to(((20+1minus1+2)20+1minus2+1)20+1minus1+2)220+1minus1+1minus2 = 42

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 7 30

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 2: Bijective Collection Encodings and Boolean Operations with

Overview

our tree-based hereditarily binary numbers apply recursively a run-lengthcompression mechanism on iterated function applications seen as ldquodigitsrdquo

they enable performing arithmetic computations symbolically and lifttractability of computations to be limited by the representation size of theiroperands rather than by their bitsizesin this paper we apply hereditarily binary numbers to derive

1 compact representations for ldquostructurally simplerdquo (sparse or dense) listssets and multisets as well as their hereditarily finite counterparts

2 bijective size-proportionate Goumldel numberings for several data typesldquovirtualizedrdquo through a generic data type transformation framework

3 a size-proportionate Goumldel numbering scheme for term algebras4 boolean operations5 operations on bitvectors and sets

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 2 30

Outline

1 Bijective base-2 numbers as iterated function applications

2 The arithmetic interpretation of hereditarily binary numbers

3 Constant average and worst case constant or loglowast operations

4 Virtual types through bijective data transformations

5 Hereditarily finite lists sets and multisets

6 A bijective size-proportionate encoding of term algebras

7 Boolean and set operations with hereditarily finite numbers

8 Conclusion

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 3 30

Bijective base-2 numbers as iterated function applications

Natural numbers can be seen as iterated applications of the functions

o(x) = 2x +1

i(x) = 2x +2

corresponding the so called bijective base-2 representation

1 = o(0)

2 = i(0)

3 = o(o(0))

4 = i(o(0))

5 = o(i(0))

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 4 30

Iterated applications of o and i some useful identities

on(k) = 2n(k +1)minus1 (1)

in(k) = 2n(k +2)minus2 (2)

and in particularon(0) = 2nminus1 (3)

in(0) = 2n+1minus2 (4)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 5 30

Hereditarily binary numbers

Hereditarily binary numbers are defined as the Haskell type T

data T = E | V T [T] | W T [T] deriving (EqReadShow)

corresponding to the recursive data type equation T= 1+TtimesTlowast+TtimesTlowast

the term E (empty leaf) corresponds to zero

the term V x xs counts the number x+1 of o applications followed by analternation of similar counts of i and o applications

the term W x xs counts the number x+1 of i applications followed by analternation of similar counts of o and i applications

the same principle is applied recursively for the counters until the emptysequence is reached

note x counts x+1 applications as we start at 0

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 6 30

The arithmetic interpretation of hereditarily binary numbers

DefinitionThe bijection n Trarr N defines the unique natural number associated to aterm of type T Its inverse is denoted t Nrarr T

n(t) =

0 if t = E

2n(x)+1minus1 if t = V x []

(n(u)+1)2n(x)+1minus1 if t = V x (yxs) and u = W y xs

2n(x)+2minus2 if t = W x []

(n(u)+2)2n(x)+1minus2 if t = W x (yxs) and u = V y xs

(5)

The computation of n(W (V E []) [EEE]) expands to(((20+1minus1+2)20+1minus2+1)20+1minus1+2)220+1minus1+1minus2 = 42

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 7 30

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 3: Bijective Collection Encodings and Boolean Operations with

Outline

1 Bijective base-2 numbers as iterated function applications

2 The arithmetic interpretation of hereditarily binary numbers

3 Constant average and worst case constant or loglowast operations

4 Virtual types through bijective data transformations

5 Hereditarily finite lists sets and multisets

6 A bijective size-proportionate encoding of term algebras

7 Boolean and set operations with hereditarily finite numbers

8 Conclusion

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 3 30

Bijective base-2 numbers as iterated function applications

Natural numbers can be seen as iterated applications of the functions

o(x) = 2x +1

i(x) = 2x +2

corresponding the so called bijective base-2 representation

1 = o(0)

2 = i(0)

3 = o(o(0))

4 = i(o(0))

5 = o(i(0))

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 4 30

Iterated applications of o and i some useful identities

on(k) = 2n(k +1)minus1 (1)

in(k) = 2n(k +2)minus2 (2)

and in particularon(0) = 2nminus1 (3)

in(0) = 2n+1minus2 (4)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 5 30

Hereditarily binary numbers

Hereditarily binary numbers are defined as the Haskell type T

data T = E | V T [T] | W T [T] deriving (EqReadShow)

corresponding to the recursive data type equation T= 1+TtimesTlowast+TtimesTlowast

the term E (empty leaf) corresponds to zero

the term V x xs counts the number x+1 of o applications followed by analternation of similar counts of i and o applications

the term W x xs counts the number x+1 of i applications followed by analternation of similar counts of o and i applications

the same principle is applied recursively for the counters until the emptysequence is reached

note x counts x+1 applications as we start at 0

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 6 30

The arithmetic interpretation of hereditarily binary numbers

DefinitionThe bijection n Trarr N defines the unique natural number associated to aterm of type T Its inverse is denoted t Nrarr T

n(t) =

0 if t = E

2n(x)+1minus1 if t = V x []

(n(u)+1)2n(x)+1minus1 if t = V x (yxs) and u = W y xs

2n(x)+2minus2 if t = W x []

(n(u)+2)2n(x)+1minus2 if t = W x (yxs) and u = V y xs

(5)

The computation of n(W (V E []) [EEE]) expands to(((20+1minus1+2)20+1minus2+1)20+1minus1+2)220+1minus1+1minus2 = 42

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 7 30

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 4: Bijective Collection Encodings and Boolean Operations with

Bijective base-2 numbers as iterated function applications

Natural numbers can be seen as iterated applications of the functions

o(x) = 2x +1

i(x) = 2x +2

corresponding the so called bijective base-2 representation

1 = o(0)

2 = i(0)

3 = o(o(0))

4 = i(o(0))

5 = o(i(0))

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 4 30

Iterated applications of o and i some useful identities

on(k) = 2n(k +1)minus1 (1)

in(k) = 2n(k +2)minus2 (2)

and in particularon(0) = 2nminus1 (3)

in(0) = 2n+1minus2 (4)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 5 30

Hereditarily binary numbers

Hereditarily binary numbers are defined as the Haskell type T

data T = E | V T [T] | W T [T] deriving (EqReadShow)

corresponding to the recursive data type equation T= 1+TtimesTlowast+TtimesTlowast

the term E (empty leaf) corresponds to zero

the term V x xs counts the number x+1 of o applications followed by analternation of similar counts of i and o applications

the term W x xs counts the number x+1 of i applications followed by analternation of similar counts of o and i applications

the same principle is applied recursively for the counters until the emptysequence is reached

note x counts x+1 applications as we start at 0

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 6 30

The arithmetic interpretation of hereditarily binary numbers

DefinitionThe bijection n Trarr N defines the unique natural number associated to aterm of type T Its inverse is denoted t Nrarr T

n(t) =

0 if t = E

2n(x)+1minus1 if t = V x []

(n(u)+1)2n(x)+1minus1 if t = V x (yxs) and u = W y xs

2n(x)+2minus2 if t = W x []

(n(u)+2)2n(x)+1minus2 if t = W x (yxs) and u = V y xs

(5)

The computation of n(W (V E []) [EEE]) expands to(((20+1minus1+2)20+1minus2+1)20+1minus1+2)220+1minus1+1minus2 = 42

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 7 30

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 5: Bijective Collection Encodings and Boolean Operations with

Iterated applications of o and i some useful identities

on(k) = 2n(k +1)minus1 (1)

in(k) = 2n(k +2)minus2 (2)

and in particularon(0) = 2nminus1 (3)

in(0) = 2n+1minus2 (4)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 5 30

Hereditarily binary numbers

Hereditarily binary numbers are defined as the Haskell type T

data T = E | V T [T] | W T [T] deriving (EqReadShow)

corresponding to the recursive data type equation T= 1+TtimesTlowast+TtimesTlowast

the term E (empty leaf) corresponds to zero

the term V x xs counts the number x+1 of o applications followed by analternation of similar counts of i and o applications

the term W x xs counts the number x+1 of i applications followed by analternation of similar counts of o and i applications

the same principle is applied recursively for the counters until the emptysequence is reached

note x counts x+1 applications as we start at 0

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 6 30

The arithmetic interpretation of hereditarily binary numbers

DefinitionThe bijection n Trarr N defines the unique natural number associated to aterm of type T Its inverse is denoted t Nrarr T

n(t) =

0 if t = E

2n(x)+1minus1 if t = V x []

(n(u)+1)2n(x)+1minus1 if t = V x (yxs) and u = W y xs

2n(x)+2minus2 if t = W x []

(n(u)+2)2n(x)+1minus2 if t = W x (yxs) and u = V y xs

(5)

The computation of n(W (V E []) [EEE]) expands to(((20+1minus1+2)20+1minus2+1)20+1minus1+2)220+1minus1+1minus2 = 42

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 7 30

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 6: Bijective Collection Encodings and Boolean Operations with

Hereditarily binary numbers

Hereditarily binary numbers are defined as the Haskell type T

data T = E | V T [T] | W T [T] deriving (EqReadShow)

corresponding to the recursive data type equation T= 1+TtimesTlowast+TtimesTlowast

the term E (empty leaf) corresponds to zero

the term V x xs counts the number x+1 of o applications followed by analternation of similar counts of i and o applications

the term W x xs counts the number x+1 of i applications followed by analternation of similar counts of o and i applications

the same principle is applied recursively for the counters until the emptysequence is reached

note x counts x+1 applications as we start at 0

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 6 30

The arithmetic interpretation of hereditarily binary numbers

DefinitionThe bijection n Trarr N defines the unique natural number associated to aterm of type T Its inverse is denoted t Nrarr T

n(t) =

0 if t = E

2n(x)+1minus1 if t = V x []

(n(u)+1)2n(x)+1minus1 if t = V x (yxs) and u = W y xs

2n(x)+2minus2 if t = W x []

(n(u)+2)2n(x)+1minus2 if t = W x (yxs) and u = V y xs

(5)

The computation of n(W (V E []) [EEE]) expands to(((20+1minus1+2)20+1minus2+1)20+1minus1+2)220+1minus1+1minus2 = 42

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 7 30

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 7: Bijective Collection Encodings and Boolean Operations with

The arithmetic interpretation of hereditarily binary numbers

DefinitionThe bijection n Trarr N defines the unique natural number associated to aterm of type T Its inverse is denoted t Nrarr T

n(t) =

0 if t = E

2n(x)+1minus1 if t = V x []

(n(u)+1)2n(x)+1minus1 if t = V x (yxs) and u = W y xs

2n(x)+2minus2 if t = W x []

(n(u)+2)2n(x)+1minus2 if t = W x (yxs) and u = V y xs

(5)

The computation of n(W (V E []) [EEE]) expands to(((20+1minus1+2)20+1minus2+1)20+1minus1+2)220+1minus1+1minus2 = 42

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 7 30

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 8: Bijective Collection Encodings and Boolean Operations with

Examples

each term canonically represents the corresponding natural number

the first few natural numbers are

0 = n E1 = n (V E [])2 = n (W E [])3 = n (V (V E []) [])4 = n (W E [E])5 = n (V E [E])

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 8 30

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 9: Bijective Collection Encodings and Boolean Operations with

An overview of constant average time and worst caseconstant or loglowast time operations with hereditarily binarynumbers

introduced in our ACM SACrsquo14 paper

mutually recursive successor s and predecessor sprime

defined on top of s and sprimeo(x) = 2x +1 and i(x) = 2x +2their inverses oprime and i prime

recognizers of odd and even numbers o_ and i_double db and its left inverse hfpower of two exp2

rArr efficient computations with towers of exponents and numbers in theirldquoneighborhoudrdquo

rArr efficient computations with sparse numbers (with a lot of 0s) or densenumbers (with a lot of 1s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 9 30

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 10: Bijective Collection Encodings and Boolean Operations with

Towers of exponents can grow tall providededthey are finite -)

22222

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 10 30

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 11: Bijective Collection Encodings and Boolean Operations with

A bijective encoding of lists of hereditarily binary numbers

we split a natural number in blocks of o and i applicationswe also need to encode and remember the parity (at the end of the list)we first implement cons and decons and then we iterate them they have the same complexity as successor s and predecessor srsquo

decons Trarr(TT)decons (V x []) = (srsquo (o x)E)decons (V x (yys)) = (xW y ys)decons(W x []) = (o xE)decons (W x (yys)) = (xV y ys)

cons (TT)rarrTcons (EE) = V E []cons(xE) | o_ x = W (orsquo x) []cons(xE) | i_ x = V (orsquo (s x)) []cons (xV y ys) = W x (yys)cons (xW y ys) = V x (yys)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 11 30

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 12: Bijective Collection Encodings and Boolean Operations with

Iterating cons and decons

they define a bijection between hereditarily binary numbers and lists ofhereditarily binary numbers

cons and decons are average constant and worst case loglowast2rArr complexity of to_list and from_list is proportional to the numberof blocks of o and i applications

to_list T rarr [T]to_list z | e_ z = []to_list z = x to_list y where (xy) = decons z

from_list [T] rarr Tfrom_list [] = Efrom_list (xxs) = cons (xfrom_list xs)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 12 30

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 13: Bijective Collection Encodings and Boolean Operations with

Bijections between lists sets and multisets

a multiset like [441333] could be represented canonically by firstordering it as [133344] and then computing the differences betweenconsecutive elements[x0x1 xi xi+1 ]rarr [x0x1minus x0 xi+1minus xi ]this gives [120010] with the first (1) followed by [20010]

list2mset mset2list list2set set2list [T] rarr [T]

list2mset [] = []list2mset (nns) = scanl add n ns

mset2list [] = []mset2list (mms) = m zipWith sub ms (mms)

for sets we ensure all elements increase by 1 so they end up all different

list2set = (map srsquo) list2mset (map s)set2list = (map srsquo) mset2list (map s)

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 13 30

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 14: Bijective Collection Encodings and Boolean Operations with

Virtual types through bijective data transformations

data Iso a b = Iso (ararrb) (brarra)from (Iso f _) = fto (Iso _ frsquo) = frsquo

ldquomorphingrdquo between data types is provided by the combinator as

as Iso a b rarr Iso c b rarr c rarr aas that this x = to that (from this x)

we define ldquovirtual typesrdquo as bijections to a ldquohubour tree-based natural numbers provide the hub nat

nat = Iso id id

the collection types for lists sets and multisets are bijections to nat

list mset set Iso [T] Tlist = Iso from_list to_listmset = Iso from_mset to_msetset = Iso from_set to_set

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 14 30

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 15: Bijective Collection Encodings and Boolean Operations with

Morphing between virtual types

gt as set nat (t 123)[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt map n it[013456]gt map t it[EV E []V (V E []) []W E [E]V E [E]W (V E []) []]gt n (as nat set it)123

combinators that borrow operations from another virtual type

borrow2 Iso c b rarr (crarrcrarrc) rarr Iso a b rarr (ararrararra)borrow2 lender op borrower x y =

as borrower lender (op xrsquo yrsquo) wherexrsquo= as lender borrower xyrsquo= as lender borrower y

ex sets will borrow bitwise boolean operationsPaul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 15 30

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 16: Bijective Collection Encodings and Boolean Operations with

Hereditarily finite lists sets and multisets generically

data H = H [H] deriving (EqReadShow)

the function t2h lifts the a transformer f defined from type T to acollection type to its hereditarily finite correspondent

t2h (T rarr [T]) rarr T rarr Ht2h f E = H []t2h f n = H (map (t2h f) (f n))

the function h2t lifts the a transformer f defined from a collection type totype T to its hereditarily finite correspondent

h2t ([T] rarr T) rarr H rarr Th2t g (H []) = Eh2t g (H hs) = g (map (h2t g) hs)

if f and g are inverses then so are t2h f and h2t g

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 16 30

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 17: Bijective Collection Encodings and Boolean Operations with

Virtual types associated to hereditarily finite collection types

Our virtual data types for hereditarily finite lists multisets and sets hfl hfmand hfs are defined in terms of h2t and t2h

hfl hfm hfs Iso H Thfl = Iso (h2t from_list) (t2h to_list)hfm = Iso (h2t from_mset) (t2h to_mset)hfs = Iso (h2t from_set) (t2h to_set)

gt as hfs nat (sub (exp2 (exp2 (exp2 (exp2 (t 2))))) (t 5))H [H [H []]H [H [H []]H [H []H [H [H [H [H []]]]]]]]gt n (bitsize (as nat hfs it))65535

PropositionThese encodingsdecodings of hereditarily finite lists sets and multisets ashereditarily binary numbers are size-proportionate

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 17 30

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 18: Bijective Collection Encodings and Boolean Operations with

A bijective size-proportionate encoding of term algebras

devising a Goumldel numbering scheme for term algebras that is bothsize-proportionate and bijective is a difficult task

it involves a fairly sophisticated rankingunranking algorithm for Catalanfamilies in combination with a generalization of Cantorrsquos pairing functionto tuples (see our ICLPrsquo2013 paper)

the solution to the same problem using hereditarily binary numbers isstrikingly simple

the basic intuition is that we avoid exponential blow-up as we aremapping trees to trees rather than to strings of bits

rArr bijective and size-proportionate Goumldel numberings of tree-likestructures (in particular term algebras) becomes straightforward

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 18 30

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 19: Bijective Collection Encodings and Boolean Operations with

A bijective size-proportionate Goumldel numbering of termalgebras

data Term a = Var a | Const a | Fun a [Term a]

toTerm T rarr Term TtoTerm E = Var EtoTerm (V x []) = Var (s x)toTerm (W x []) = Const xtoTerm (V x xs) = Fun (o x) (map toTerm xs)toTerm (W x xs) = Fun (db x) (map toTerm xs)

fromTerm Term T rarr TfromTerm (Var E) = EfromTerm (Var y) = V (srsquo y) []fromTerm (Const x) = W x []fromTerm (Fun k xs) | o_ k = V (orsquo k) (map fromTerm xs)fromTerm (Fun k xs) = W (hf k) (map fromTerm xs)

term Iso (Term T) Tterm = Iso fromTerm toTerm

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 19 30

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 20: Bijective Collection Encodings and Boolean Operations with

Examples

gt fromTerm (Fun E [Fun E [Fun E [Const E]]])W E [W E [W E [W E []]]]gt n (bitsize it)262146gt fromTerm (Fun E [Fun E [Fun E [Fun E [Const E]]]])W E [W E [W E [W E [W E []]]]]gt n (bitsize (bitsize it))262146

the first term corresponds to a large (262146 bits) number computed as

(2(2(220+2minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2+1minus1+2)20+1minus2

the second is already a giant 2262146 bit number

we have used trees of type Term T rather than the more obvious typeTerm N to ensure that the encoding is size proportionate both ways

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 20 30

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 21: Bijective Collection Encodings and Boolean Operations with

Automorphisms of N from automorphisms of T

A simple bijection Trarr T is provided by the dual operation that flipstoplevel constructors V and W

it reinterprets all o operations as ıoperations and vice-versa

it is therefore its own inverse (an involution)

it can be ldquoborrowedrdquo by the type N of ordinary natural numbers

gt map (borrow1 nat dual bitnat) [015][0216543141312111098730]gt map (borrow1 nat dual bitnat) it[0123456789101112131415]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 21 30

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 22: Bijective Collection Encodings and Boolean Operations with

A concept of asymmetric duality

a more interesting permutation of T is provided by working on thehereditarily finite list equivalent of an object in T a member of theCatalan family (see upcoming ICTACrsquo14 paper on their arithmetic)

hdual H rarr Hhdual (H []) = H []hdual (H (xxs)) = H (hdual (H xs) ys) where

H ys = hdual x

tdual T rarr Ttdual = borrow1 hfl hdual nat

also an involution

gt map (borrow1 nat tdual bitnat) [017][01492720562310943020471641144294967295]gt map (borrow1 nat tdual bitnat) it[01234567891011121314151617]

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 22 30

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 23: Bijective Collection Encodings and Boolean Operations with

Example

the trees of type T associated to random natural numbers are much widerthan tallthe involution tdual flips between relatively small random numbers (withhigh Kolmogorov complexity) and giant numbers with a regular structure

E

V

0

V

0 1

2

E

W

0 1

V

0

V

0

Figure Duals with trees folded into DAGs arcs marked with order of children on theleft t 17 and on the right tdual of (t 17) = t 4294967295

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 23 30

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 24: Bijective Collection Encodings and Boolean Operations with

Bitvector operations

we implement bitvector operations to work ldquoone block of on or in

applications at a timerdquo

target large but sparse boolean formulas

evaluate such formulas ldquoall value-combinations at a timerdquo whenrepresented as bitvectors of size 22n

such operations will be tractable with our trees provided that they have arelatively small representation size despite their large bitsizemain idea of the algorithms in the paper

1 split and align block fragments of the same size2 perform the boolean operation between the blocks as if they were single

bits3 fuse the resulting blocks into larger blocks when possible

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 24 30

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 25: Bijective Collection Encodings and Boolean Operations with

Examples

our bitwise operations can be efficiently applied to giant numbers

for instance (2212345+1) XOR (226789minus1) is computed as

gt bitwiseXor (s (exp2 (exp2 (t 12345))))(srsquo (exp2 (exp2 (t 6789))))

W (V (W E [EEV (V E []) []EEEEE]) [])[V (W E [EEV (V E []) [] EEEEE])[W (V E []) [V E []V E []EV E []EEE]]]

gt n (tsize it) -- tsize computes the size of the tree39

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 25 30

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 26: Bijective Collection Encodings and Boolean Operations with

Set operations

with help from the data transformation operation borrow2 we can usebitvectors for set operations

setIntersection [T]rarr[T]rarr[T]setIntersection = borrow2 nat bitwiseAnd set

setUnion [T]rarr[T]rarr[T]setUnion = borrow2 nat bitwiseOr set

example

gt map n (setUnion (map t [1234])(map t [2367]))[123467]

sparse or dense sets containing very large sparse or dense elementsbenefit significantly from this encoding if despite the large bitsizesinvolved they are represented as compact trees

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 26 30

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 27: Bijective Collection Encodings and Boolean Operations with

Boolean formula evaluation

var(nk) column k of a truth table for a function with n variables

Knuth gives a compact formula for them

var(nk) = (22n minus1) (22nminuskminus1+1) (6)

instead of doing the division we compute them as a concatenation ofalternating blocks of 1 and 0 bits to take advantage of our efficient blockoperations

could we use hereditarily binary numbers as a representation of booleanformulas with potential application to circuits

as they can be seen as a compact representation of the truth tables ofsparse or or dense boolean formulas one will need only to find the bitcorresponding to an input described by a row in the truth table

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 27 30

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 28: Bijective Collection Encodings and Boolean Operations with

Hereditarily finite natural numbers as circuits

bijective base-2 representation of natural numbers can be seen assuccessive listings of the columns in truth tables for 01 n-argumentboolean functions

(7 [000])(8 [100])(9 [010])(13[011])(14[111])

the bijective base-2 representation of natural numbers in[2nminus1 2n+1minus2] describes the inputs in the truth table of a n-argumentboolean function

rArr the functions bitval and nthBit in the paper navigate to a given bitthrough the tree

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 28 30

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 29: Bijective Collection Encodings and Boolean Operations with

Conclusion

we have shown previously that hereditarily binary numbers favor by asuper-exponential factor arithmetic operations on numbers inneighborhoods of towers of exponents of two

we show in this paper that hereditarily binary numbers also provide auniform mechanism for representing lists multisets and sets of naturalnumbers through simple and efficiently computable bijections

in contrast to bitstring representations these bijections are sizeproportionate

this property extends to hereditarily finite sets multisets and lists

as an application we derive a size-proportionate bijective Goumldelnumbering scheme for term algebras

rArr hereditarily binary numbers provide a unique representation for keymathematical objects mapped to each other through bijections betweenldquovirtual data typesrdquo expressed in terms of Haskell combinators

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 29 30

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion
Page 30: Bijective Collection Encodings and Boolean Operations with

Links

the paper is a literate program our Haskell code is athttpwwwcseuntedu~tarauresearch2014HBShs

it imports code from the our ACM SACrsquo14 paper athttpwwwcseuntedu~tarauresearch2014HBinhs

a draft version of the ACM SACrsquo14 paper is athttpwwwcseuntedu~tarauresearch2014HBinpdf

new arithmetic and number theoretical algorithms with HBNs athttpwwwcseuntedu~tarauresearch2014hbinxpdf

an alternative Scala based implementation of HBNs is at athttpcodegooglecompgiant-numbers

Paul Tarau (University of North Texas) Encodings w Hereditarily Binary Natural Numbers PPDPrsquo2014 30 30

  • Bijective base-2 numbers as iterated function applications
  • The arithmetic interpretation of hereditarily binary numbers
  • Constant average and worst case constant or log operations
  • Virtual types through bijective data transformations
  • Hereditarily finite lists sets and multisets
  • A bijective size-proportionate encoding of term algebras
  • Boolean and set operations with hereditarily finite numbers
  • Conclusion