# Compaction of Church Numerals

^{*}

^{†}

## Abstract

**:**

## 1. Introduction

**Contributions:**The primary contributions of this study are as follows.

- For natural numbers, we propose a novel decomposition scheme called RTP, which enables to obtain a compact representation of $\lambda $-terms that leads to the Church numerals of the numbers.
- By incorporating RTP, we propose an algorithm to perform the compaction of $\mathcal{C}(n)$ for natural number n. Moreover, we prove that the size of the $\lambda $-terms constructed by the algorithm is $\mathcal{O}({({\mathrm{slog}}_{2}n)}^{logn/loglogn})$.
- We implemented the proposed algorithm and conducted comparative experiments. With regard to the sizes of the obtained $\lambda $-terms, the results show that the method is superior to that of Yaguchi et al. [2] and is also superior to a binary representation on $\lambda $-term on average, when n is less than approximately 10,000.

**Related studies:**Better modeling for an input data leads better compression ratio. Since it is rare that we know the exact model for the input well beforehand, we usually use a universal source coding [4] or assume an appropriate model on the input to express the structure inherent in it. Kieffer and Yang [3] proposed grammar compression that uses a context-free grammar as the model. Grammar compression is a compression scheme that translates an input text into a set of context-free grammar rules, before encoding the rules. In grammar compression, a smaller set of rules gives a better compression ratio. Finding the optimal grammar is an NP-hard problem [5] but several excellent heuristic algorithms have been proposed for its solution [6,7,8,9,10,11]. Obtaining a smaller grammar is of great importance since most existing algorithms that work on grammar-compressed texts have running time dependent on the grammar sizes (see the excellent surveys e.g., [12,13]). Due to information theory arguments, any compression cannot be better than a binary encoding on all numbers. However, as stated in [1] and the above, higher-order compression can be better for a certain subset of numbers, i.e., for $n={}^{i}k$.

**Composition:**The rest of this paper is organized as follows. In Section 2, we review tetration, lambda notation, and Church numerals. In Section 3, we define the proposed RTP method and present the translation algorithm using RTP. We also prove the upper bound of the size of the $\lambda $-term produced by our algorithm. In Section 4, we discuss application to higher-order compression and present our experimental result. Conclusions are presented in Section 5.

## 2. Preliminaries

#### 2.1. Tetration and Super-Logarithm

**Definition**

**1**

**.**For natural numbers φ and t, the t-th tetration of φ, denoted by ${}^{t}\phi $, is recursively defined as follows:

**Corollary**

**1.**

**Definition**

**2**

**.**Super-logarithm, denoted by $\mathrm{slog}$, is the inverse operation of tetration, defined as ${\mathrm{slog}}_{\phi}({}^{t}\phi )=t$ with natural numbers φ and t.

#### 2.2. Lambda Terms

**Definition**

**3**

**.**Let x be a variable. Then, lambda term (λ-term) M is defined inductively as follows:

**Definition**

**4**

**.**Let x be a variable, and ${M}_{1}$ and ${M}_{2}$ be λ-terms. Then, $\beta $-reduction is a relation of λ-terms, denoted by using ${\u27f6}_{\beta}$, such that $(\lambda x.{M}_{1})\phantom{\rule{3.33333pt}{0ex}}{M}_{2}{\u27f6}_{\beta}{M}_{1}[x:={M}_{2}]$. We write ${\u27f6}_{\beta}^{*}$ for the reflexive transitive closure of ${\u27f6}_{\beta}$.

**Definition**

**5**

**.**Let x be a variable and M be a λ-term. Then, we denote the size of the λ-term by $\#M$, and inductively define the size of each λ-term as follows:

#### 2.3. Church Numerals

**Definition**

**6**

**.**For natural number n, its Church numeral, denoted by $\mathcal{C}(n)$, is

**Corollary**

**2**

**.**Let ${n}_{1}$ and ${n}_{2}$ be natural numbers. Then we obtain three λ-terms $\mathrm{Add}({n}_{1},{n}_{2})$, $\mathrm{Mul}({n}_{1},{n}_{2})$, and $\mathrm{Tet}({n}_{1},{n}_{2})$, such that $\mathrm{Add}({n}_{1},{n}_{2}){\u27f6}_{\beta}^{*}\mathcal{C}({n}_{1}+{n}_{2})$, $\mathrm{Mul}({n}_{1},{n}_{2}){\u27f6}_{\beta}^{*}\mathcal{C}({n}_{1}\xb7{n}_{2})$, and $\mathrm{Tet}({n}_{1},{n}_{2}){\u27f6}_{\beta}^{*}\mathcal{C}({}^{{n}_{2}}{n}_{1})$, respectively, as follows:

#### 2.4. Binary Expression of Natural Numbers on $\lambda $-Terms

**Definition**

**7**

**.**For natural number n, assume that its binary representation is ${B}_{k}{B}_{k-1}\cdots {B}_{2}{B}_{1}$ with ${B}_{i}\in \{0,1\}$ for $1\le i\le k$. Then, the binary expression of n on λ-terms, denoted by $\mathcal{B}(n)$, is

## 3. Proposed Method

#### 3.1. Basic Idea

**Step****1:**- Decompose n into a special form of arithmetic expression, called TAE.
**Step****2:**- Translate the arithmetic expression into a $\lambda $-term having the form of $M\phantom{\rule{3.33333pt}{0ex}}\mathcal{C}({\phi}^{*})$.
**Step****3:**- Apply the above Step 1 and 2 for the natural number ${\phi}^{*}$ recursively.

#### 3.2. Tetrational Arithmetic Expression (TAE)

**Definition**

**8**

**.**Let N be an arbitrary natural number. Then, tetrational arithmetic expression (TAE) E is inductively defined as follows:

#### 3.3. Translation from TAE to $\lambda $-Term

**Definition**

**9**

**.**Let n and φ be natural numbers. We say a λ-term M is a corresponding functional λ-term (CFLT) of TAE $E[n]$ if both of the following conditions hold for M:

- 1.
- M has the form of $(\lambda p.\lambda f.\lambda x.\widehat{M})\phantom{\rule{3.33333pt}{0ex}}\mathcal{C}(\phi )$, where $\widehat{M}$ is a λ-term that contains no Church numerals.
- 2.
- $M{\u27f6}_{\beta}^{*}\mathcal{C}(n)$.

**Lemma**

**1.**

**Proof.**

**Definition**

**10**

**.**Let x be a variable, ${M}_{1}$ and ${M}_{2}$ be λ-terms, and ${\tilde{\u27f6}}_{\beta}$ be a special β-reduction from $(\lambda x.{M}_{1})\phantom{\rule{3.33333pt}{0ex}}{M}_{2}$ to ${M}_{1}[x:={M}_{2}]$ which defined only if at least one of the following holds:

- 1.
- x occurs in ${M}_{1}$ only once (in other words, x is linear in ${M}_{1}$),
- 2.
- $\#{M}_{2}=1$.

Algorithm 1 Translation from ${E}_{\phi}$ to its CFLT. |

Input: ${E}_{\phi}$ |

Output: $\Lambda ({E}_{\phi})$ |

1: function translate(${E}_{\phi}$) |

2: if ${E}_{\phi}=\phi $ then |

3: return $(\lambda p.\lambda f.\lambda x.p\phantom{\rule{3.33333pt}{0ex}}f\phantom{\rule{3.33333pt}{0ex}}x)$ |

4: else if ${E}_{\phi}={}^{t}\phi $ then |

5: return $(\lambda p.\lambda f.\lambda x.{\overbrace{p\phantom{\rule{3.33333pt}{0ex}}p\phantom{\rule{3.33333pt}{0ex}}\cdots \phantom{\rule{3.33333pt}{0ex}}p}}^{t}\phantom{\rule{3.33333pt}{0ex}}f\phantom{\rule{3.33333pt}{0ex}}x)$ |

6: else if ${E}_{\phi}={E}_{\phi}^{(1)}+{E}_{\phi}^{(2)}$ then |

7: $(\lambda p.\lambda f.\lambda x.{\widehat{M}}^{(1)}):=$ translate(${E}_{\phi}^{(1)}$) |

8: $(\lambda p.\lambda f.\lambda x.{\widehat{M}}^{(2)}):=$ translate(${E}_{\phi}^{(2)}$) |

9: return $(\lambda p.\lambda f.\lambda x.(\lambda y.{\widehat{M}}^{(1)}[x:=y])\phantom{\rule{3.33333pt}{0ex}}((\lambda y.{\widehat{M}}^{(2)}[x:=y])\phantom{\rule{3.33333pt}{0ex}}x))$ |

10: else if ${E}_{\phi}={E}_{\phi}^{(1)}\xb7{E}_{\phi}^{(2)}$ then |

11: $(\lambda p.\lambda f.\lambda x.{\widehat{M}}^{(1)}):=$ translate(${E}_{\phi}^{(1)}$) |

12: $(\lambda p.\lambda f.\lambda x.{\widehat{M}}^{(2)}):=$ translate(${E}_{\phi}^{(2)}$) |

13: return $(\lambda p.\lambda f.\lambda x.(\lambda g.\lambda y.{\widehat{M}}^{(1)}[f:=g,x:=y])\phantom{\rule{3.33333pt}{0ex}}(\lambda y.{\widehat{M}}^{(2)}[x:=y])\phantom{\rule{3.33333pt}{0ex}}x)$ |

14: end if |

15: end function |

16: |

17: $M:=$ translate(${E}_{\phi}$) |

18: ${M}^{*}:=$ the $\lambda $-term generated by simplification of M |

19: return$({M}^{*}\phantom{\rule{3.33333pt}{0ex}}\mathcal{C}(\phi ))$ |

#### 3.4. Recursive Tetrational Partitioning (RTP)

**Definition**

**11**

**.**Let n and $\phi \ge 2$ be natural numbers, and $r=n\phantom{\rule{0.277778em}{0ex}}mod\phantom{\rule{0.277778em}{0ex}}\phi $. Then, recursive tetrational partitioning (RTP) is a method of decomposing n into ${E}_{\phi}[n-r]+r$. Let ${T}_{\phi}[n]$ denote a TAE generated by RTP, then RTP recursively decompose n in the manner described as follows:

**Theorem**

**1.**

**Proof.**

**Theorem**

**2.**

**Proof.**

**Lemma**

**2.**

**Proof.**

**Lemma**

**3.**

**Proof.**

#### 3.5. Further Compaction

**Theorem**

**3.**

**Proof.**

Algorithm 2 Generation of $\mathcal{L}(n)$ for a given natural number n. |

Input: n |

Output: $\mathcal{L}(n)$ |

1: function generate(n) |

2: if $n\le 8$ then |

3: return $\mathcal{C}(n)$ |

4: else |

5: $\phi :=2$, ${\phi}^{*}:=\phi $, $\mathit{minsize}:=\infty $, $M:=\mathit{NULL}$ |

6: while $\phi \le \lfloor \sqrt{n}\rfloor $ do |

7: $r:=n\phantom{\rule{0.277778em}{0ex}}mod\phantom{\rule{0.277778em}{0ex}}\phi $ |

8: ${E}_{\phi}+r:={T}_{\phi}[n]$ |

9: $((\lambda p.\lambda f.\lambda x.\widehat{M})\phantom{\rule{3.33333pt}{0ex}}\mathcal{C}(\phi )):=\Lambda ({E}_{\phi})$ |

10: $\Lambda ({T}_{\phi}[n]):=((\lambda p.\lambda f.\lambda x.\widehat{M}[x:=({\overbrace{f\phantom{\rule{3.33333pt}{0ex}}(f\phantom{\rule{3.33333pt}{0ex}}\cdots \phantom{\rule{3.33333pt}{0ex}}(f}}^{r}\phantom{\rule{3.33333pt}{0ex}}x)\cdots ))])\phantom{\rule{3.33333pt}{0ex}}\mathcal{C}(\phi ))$ |

11: if $\#\Lambda ({T}_{\phi}[n])<\mathit{minsize}$ then |

12: ${\phi}^{*}\leftarrow \phi $ |

13: $\mathit{minsize}\leftarrow \#\Lambda ({T}_{\phi}[n])$ |

14: $M\leftarrow $ the function term of $\Lambda ({T}_{\phi}[n])$ |

15: end if |

16: $\phi \leftarrow \phi +1$ |

17: end while |

18: ${M}_{\mathit{arg}}:=$ generate(${\phi}^{*}$) |

19: return $(M\phantom{\rule{3.33333pt}{0ex}}{M}_{\mathit{arg}})$ |

20: end if |

21: end function |

22: |

23: return generate(n) |

**Theorem**

**4.**

**Lemma**

**4.**

**Proof.**

**Proof**

**of**

**Theorem**

**4.**

## 4. Application to Higher-Order Compression and Comparative Experiments

**Definition**

**12**

**.**Let x be a variable and $a\in \Sigma $. Then, extended λ-term $\tilde{M}$ is defined inductively as follows:

## 5. Conclusions

## Author Contributions

## Funding

## Acknowledgments

## Conflicts of Interest

## References

- Kobayashi, N.; Matsuda, K.; Shinohara, A.; Yaguchi, K. Functional programs as compressed data. High. Order Symb. Comput.
**2012**, 25, 39–84. [Google Scholar] [CrossRef] - Yaguchi, K.; Kobayashi, N.; Shinohara, A. Efficient algorithm and coding for higher-rder compression. In Proceedings of the 2014 Data Compression Conference (DCC2014), Snowbird, UT, USA, 26–28 March 2014; p. 434. [Google Scholar]
- Kieffer, J.C.; Yang, E.H. Grammar-based codes: A new class of universal lossless source codes. IEEE Trans. Inf. Theory
**2000**, 46, 737–754. [Google Scholar] [CrossRef] - Cover, T.M.; Thomas, J.A. Elements of Information Theory (Wiley Series in Telecommunications and Signal Processing); Wiley-Interscience: New York, NY, USA, 2006. [Google Scholar]
- Charikar, M.; Lehman, E.; Liu, D.; Panigrahy, R.; Prabhakaran, M.; Sahai, A.; Shelat, A. The smallest grammar problem. IEEE Trans. Inf. Theory
**2005**, 51, 2554–2576. [Google Scholar] [CrossRef] - Larsson, N.J.; Moffat, A. Off-line dictionary-based compression. Proc. IEEE
**2000**, 88, 1722–1732. [Google Scholar] [CrossRef] - Maruyama, S.; Tanaka, Y.; Sakamoto, H.; Takeda, M. Context-sensitive grammar transform: Compression and pattern matching. In Proceedings of the String Processing and Information Retrieval (SPIRE2008), Melbourne, Australia, 10–12 November 2008; pp. 27–38. [Google Scholar]
- Masaki, T.; Kida, T. Online grammar transformation based on re-pair algorithm. In Proceedings of the Data Compression Conference (DCC2016), Snowbird, UT, USA, 30 March–1 April 2016; pp. 349–358. [Google Scholar]
- Nevill-Manning, C.G.; Witten, I.H.; Maulsby, D.L. Compression by induction of hierarchical grammars. In Proceedings of the Data Compression Conference (DCC’94), Snowbird, UT, USA, 29–31 March 1994; pp. 244–253. [Google Scholar]
- Takabatake, Y.; Sakamoto, H. A space-optimal grammar compression. In Proceedings of the 25th Annual European Symposium on Algorithms (ESA 2017), Vienna, Austria, 4–6 September 2017; Leibniz International Proceedings in Informatics (LIPIcs). Volume 87, pp. 67:1–67:15. [Google Scholar] [CrossRef]
- Ohno, T.; Goto, K.; Takabatake, Y.; I, T.; Sakamoto, H. LZ-ABT: A practical algorithm for α-balanced grammar compressio. In Proceedings of the 29th International Workshop on Combinatorial Algorithms (IWOCA 2018), Singapore, 16–19 July 2018; Springer: Singapore, 2018; pp. 323–335. [Google Scholar]
- Rytter, W. Grammar compression, LZ-encodings, and string algorithms with implicit input. In Automata, Languages, and Programming (ICALP2004); Díaz, J., Karhumäki, J., Lepistö, A., Sannella, D., Eds.; Springer: Berlin/Heidelberg, Germany, 2004; pp. 15–27. [Google Scholar]
- Lohrey, M. Algorithmics on SLP-compressed strings: A survey. Groups Complex. Cryptol.
**2012**, 4, 241–299. [Google Scholar] [CrossRef] - Furuya, I.; Kida, T. Compaction of church numerals for higher-order compression. In Proceedings of the Data Compression Conference (DCC2018), Snowbird, UT, USA, 27–30 March 2018; p. 408. [Google Scholar]
- Mogensen, T.A. An investigation of compact and efficient number representations in the pure lambda calculus. In Revised Papers from the 4th International Andrei Ershov Memorial Conference on Perspectives of System Informatics (PSI ’02): Akademgorodok, Novosibirsk, Russia; Springer-Verlag: London, UK, 2001; pp. 205–213. [Google Scholar]
- Tromp, J. Binary lambda calculus and combinatory logic. In Kolmogorov Complexity and Applications; Hutter, M., Merkle, W., Vitanyi, P.M., Eds.; Number 06051 in Dagstuhl Seminar Proceedings; Internationales Begegnungs- und Forschungszentrum fuer Informatik (IBFI): Schloss Dagstuhl, Germany, 2006. [Google Scholar]
- Takeda, K.; Kobayashi, N.; Yaguchi, K.; Shinohara, A. Compact bit encoding schemes for simply-typed lambda-terms. In Proceedings of the 21st ACM SIGPLAN International Conference on Functional Programming, Nara, Japan, 18–24 September 2016; Volume 51, pp. 146–157. [Google Scholar]

n | $\#\mathcal{C}(\mathit{n})$ | $\#\mathbf{\Lambda}({\mathit{T}}_{\mathit{\phi}}[\mathit{n}])$ | n | $\#\mathcal{C}(\mathit{n})$ | $\#\mathbf{\Lambda}({\mathit{T}}_{\mathit{\phi}}[\mathit{n}])$ |
---|---|---|---|---|---|

9 | 21 | 20 | 13 | 29 | 26 |

10 | 23 | 22 | 14 | 31 | 28 |

11 | 25 | 24 | 15 | 33 | 28 |

12 | 27 | 24 |

© 2019 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (http://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Furuya, I.; Kida, T.
Compaction of Church Numerals. *Algorithms* **2019**, *12*, 159.
https://doi.org/10.3390/a12080159

**AMA Style**

Furuya I, Kida T.
Compaction of Church Numerals. *Algorithms*. 2019; 12(8):159.
https://doi.org/10.3390/a12080159

**Chicago/Turabian Style**

Furuya, Isamu, and Takuya Kida.
2019. "Compaction of Church Numerals" *Algorithms* 12, no. 8: 159.
https://doi.org/10.3390/a12080159