Compaction of Church Numerals

: In this study, we address the problem of compaction of Church numerals. Church numerals are unary representations of natural numbers on the scheme of lambda terms. We propose a novel decomposition scheme from a given natural number into an arithmetic expression using tetration, which enables us to obtain a compact representation of lambda terms that leads to the Church numeral of the natural number. For natural number n , we prove that the size of the lambda term obtained by the proposed method is O (( slog 2 n ) ( log n /loglog n ) ) . Moreover, we experimentally conﬁrmed that the proposed method outperforms binary representation of Church numerals on average, when n is less than approximately 10,000 .


Introduction
The goal of this study is to obtain a compact lambda term (λ-term) that leads to the Church numeral of a given natural number. Church numerals are unary representations of natural numbers on λ-terms. Let C(n) be the Church numeral for natural number n; then C(n) is as follows: Namely, the length of the Church numeral increases linearly with n. We want a more compact representation for n in the scheme of λ-terms. We refer to this task as the compaction of Church numerals. Using a binary representation for n is a natural idea, which enables to obtain a λ-term whose size is Θ(log n). Another idea is to decompose n into an arithmetic expression and to represent it as a corresponding λ-term of the expression. This may reduce the size of the λ-term. For example, n = 500 can be decomposed into 5 × 10 × 10. The λ-term corresponding to this expression is given as (λp.λq.λ f .λx.p (q (q f )) x) C(5) C(10), which is much smaller than C(500). In this idea, tetration can be used for the decomposition. Tetration is the next hyperoperation after exponentiation; it is defined as iterated exponentiation, e.g., the third tetration of two 3 2 = 2 2 2 = 16. Since a function for calculating i k on Church numerals is written as a λ-term whose size is O(i + k), the size of the λ-term for n = i k achieves o(log n).
In this paper, we propose the Recursive tetrational partitioning (RTP) method to decompose a natural number using tetration. We also present an algorithm to generate a compact λ-term by using RTP. Moreover, we prove that the size of the obtained λ-term is O((slog 2 n) log n/ log log n ), where slog 2 n is super-logarithm, which is the inverse operation of tetration. It is slightly worse than O(log n), but we also prove that the size of the λ-term achieves O(slog 2 n) in some cases, and our experimental result shows that our method outperforms a binary representation on λ-term on average, when n is less than approximately 10,000.
The compaction of Church numerals can be applied to data compression. Kobayashi et al. [1] proposed a compression method called higher-order compression that uses extended λ-terms as the data model. Their method translates an input data to a compact λ-term and then encodes the obtained λ-term. When a pattern consecutively appears n times in the input, such a part can be represented as a λ-term with C(n). Thus efficient compaction of C(n) will help for higher-order compression when the data contains many repetitive patterns.
Yaguchi et al. [2] recently proposed an efficient algorithm for higher-order compression. They utilized a simply typed λ-term for efficient modeling and encoding. Differing from Kobayashi et al.'s method where each context occurring more than once in an input is extracted, Yaguchi et al.'s method extracts the most frequent context up to a certain size. They state in [2] that their method often achieves better performance than a grammar compression [3] with regard to compression ratio. Of course, their method can also be applied to the compaction of C(n), while it takes time for repeating context extraction. We confirmed via experiments that our method tends to produce more compact λ-terms for Church numerals than Yaguchi et al.'s method. Note that our method can be easily incorporated into Yaguchi et al.'s method.

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

1.
For natural numbers, we propose a novel decomposition scheme called RTP, which enables to obtain a compact representation of λ-terms that leads to the Church numerals of the numbers.

2.
By incorporating RTP, we propose an algorithm to perform the compaction of C(n) for natural number n. Moreover, we prove that the size of the λ-terms constructed by the algorithm is O((slog 2 n) log n/ log log n ).

3.
We implemented the proposed algorithm and conducted comparative experiments. With regard to the sizes of the obtained λ-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 λ-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 λ-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. Previously, one page abstract of this work has been published in the proceedings of Data Compression Conference 2016 [14]. This paper is the full version article of [14].
The following corollary is easily induced from Definition 1.

Corollary 1.
For natural numbers ϕ and t, it holds that log ϕ t ϕ = t−1 ϕ.
Definition 2 (Super-logarithm). Super-logarithm, denoted by slog, is the inverse operation of tetration, defined as slog ϕ ( t ϕ) = t with natural numbers ϕ and t.
The iterated logarithm of n, denoted by log * n, is also known as the number of times the logarithm function must be applied to n before it becomes less than or equal to 1. For positive numbers, the super-logarithm is essentially equivalent to the iterated logarithm, i.e., it holds that log * n = slog e n for any n > 0. However, note that in the O(.)-notation the size of super-logarithm depends on its base, that is, O(slog a n) = O(slog b n) if a = b.

Lambda Terms
Definition 3 (Lambda terms (λ-terms)). Let x be a variable. Then, lambda term (λ-term) M is defined inductively as follows: We call λ-terms formed λx.M and M 1 M 2 λ-abstraction and functional application, respectively. Let V 1 and V 2 be the sets of the variables occurring in M 1 and M 2 , respectively, then, we identify M 1 with M 2 if there is an isomorphism from V 1 to V 2 . M[x 1 := M 1 , x 2 := M 2 , . . . ] denotes the λ-term such that all occurrences of x 1 , x 2 , . . . in M are replaced by M 1 , M 2 , . . . , respectively.
We show in parentheses "(" and ")" the precedence of combining λ-terms. However, for simplicity, we omit these parentheses based on standard omission rules, that is, we consider that functional applications are left-associative and prior to λ-abstractions. For example, we consider Definition 4 (β-reduction). Let x be a variable, and M 1 and M 2 be λ-terms. Then, β-reduction is a relation of λ-terms, denoted by using −→ β , such that (λx.
We define the sizes of λ-terms as follows. The definition can be found in [1].

Definition 5 (Sizes of λ-terms)
. 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: For λ-terms M 1 and M 2 , we say that M 2 is more compact than M 1 if #M 2 < #M 1 . Compaction of a λ-term M 1 means to find a λ-term M 2 such that M 1 N −→ * β M and M 2 N −→ * β M for every N with a λ-term M , and #M 2 < #M 1 .
In this paper, we assume Word RAM as the computational model. Thus, we ignore the size of names and pointers when we discuss size and space.

Church Numerals
It is known that natural numbers are represented as Church numerals on λ-terms. Also, some λ-terms enabling arithmetic operations on Church numerals are provided.

Binary Expression of Natural Numbers on λ-Terms
Instead of Church numerals, we can represent natural numbers on λ-terms based on binary representation as follows [15].

Definition 7 (Binary representation on λ-terms).
For natural number n, assume that its binary representation is B k B k−1 · · · B 2 B 1 with B i ∈ {0, 1} for 1 ≤ i ≤ k. Then, the binary expression of n on For example, the binary expression of 57 is 111001, then its binary expression on λ-terms is x)))))).

Basic Idea
Let n be a natural number. As stated in Section 1, we can reduce #C(n) by using decomposition of n into an arithmetic expression. For the example, for n = 500, we can decompose it to an arithmetic expression 5 · 10 · 10 (more precisely, (5 · 10) · 10). A corresponding λ-term M of the expression is (10)) C(10), and its size is 85, while #C(500) = 1003. Here, M −→ * β C(500). Moreover, we can make M more compact by combining two λ-abstractions into a single λ-abstraction, such as (λp.λq.λ f .λx.p (q (q f )) x) C(5) C(10). This also generates C(500) through β-reduction similar to M, while its size is just 51.
We perform compaction of C(n) in the following three steps.
Step 1: Decompose n into a special form of arithmetic expression, called TAE.
Step 2: Translate the arithmetic expression into a λ-term having the form of M C(ϕ * ).
Step 3: Apply the above Step 1 and 2 for the natural number ϕ * recursively. For Step 1, we introduce TAEs in Section 3.2. For Step 2, we describe a translation method in Section 3.3. There are many ways to achieve decomposition of a natural number into a TAE, and the size of the translated λ-term changes depending on the expression. Then, we consider how we effectively obtain a TAE such that the translated λ-term of the TAE is compact. We propose RTP in Section 3.4, which is a heuristic approach decomposing a natural number into a TAE. Finally, for Step 3, we discuss further recursive compaction of Church numerals in Section 3.5.

Tetrational Arithmetic Expression (TAE)
We define tetrational arithmetic expression (TAE), denote by E, as a special form of arithmetic expression.
Definition 8 (Tetrational arithmetic expressions (TAEs)). Let N be an arbitrary natural number. Then, tetrational arithmetic expression (TAE) E is inductively defined as follows: If the evaluated result of a TAE E is a natural number n, we write E[n] for the TAE. Especially, let E ϕ denote a restricted form of TAE with a natural number ϕ, defined as where t is an arbitrary natural number.
Informally speaking, reducing the number of natural numbers occurring in TAEs is effective for compaction of corresponding λ-terms, since it enables to reduce the number of the kinds of argument terms in the λ-terms; note that we can transform (λp.λq.
As seen above, the corresponding λ-term seems to become long. However, we can translate the TAE to a compact λ-term. Definition 9 (Corresponding functional λ-terms (CFLT)). 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 (λp.λ f .λx.M) C(ϕ), whereM is a λ-term that contains no Church numerals.

2.
M −→ * β C(n). For a given TAE E, We denote a CFLT of E by Λ(E).
For TAE E ϕ , the following lemma holds.
Proof. We prove the lemma by induction. Considering λ-terms stated in Corollary 2, for E ϕ = ϕ and E ϕ = t ϕ, their CFLTs are obtained as ϕ and E ϕ be TAEs. Then, we assume that there are CFLTs of E ϕ and E ϕ . Moreover, by Definition 9, we assume that Λ(E Here, CFLTs of E ϕ + E ϕ and E ϕ · E ϕ are obtained as 1. x occurs in M 1 only once (in other words, x is linear in M 1 ), 2.
By using simplification for (λx.M 1 ) M 2 , we can reduce the size of the λ-term since #M 1 [x := From the above discussion, we can translate a given TAE E ϕ into a compact λ-term by applying simplification after translating E ϕ into the CFLT in the manner of the proof of Lemma 1. We show the translating algorithm in Algorithm 1. The time complexity of Algorithm 1 depends on the total number of the occurrences of addition and multiple in the input TAE.

Algorithm 1
Translation from E ϕ to its CFLT.

Recursive Tetrational Partitioning (RTP)
We consider how we obtain an effective E ϕ [n] for givenn and ϕ in the sense that the CFLT becomes more compact. E ϕ [n] is more effective if it consists of fewer arithmetic operations because the size of the CFLT increases with the number of arithmetic operations occurring in it. For example, forn = 12 and ϕ = 3, 3 · 3 + 3 is more effective than 3 + 3 + 3 + 3. We propose recursive tetrational partitioning (RTP), which is a heuristic method to find such an effective TAE for givenn and ϕ. Definition 11 (Recursive tetrational partitioning (RTP)). Let n and ϕ ≥ 2 be natural numbers, and r = n mod ϕ. Then, recursive tetrational partitioning (RTP) is a method of decomposing n into E ϕ [n − r] + r. Let T ϕ [n] denote a TAE generated by RTP, then RTP recursively decompose n in the manner described as follows: where k is the maximum natural number such that k ϕ ≤ n, and for 1 ≤ i ≤ k, p i is the natural number such that 0 ≤ p i · i ϕ < i+1 ϕ and r i = p i mod ϕ. Here, if p i − r i = 0 or 1, we omit the corresponding terms or coefficients in the generated TAE, respectively.
Proof. We prove the statement by induction. For n ≤ ϕ, the statement clearly holds. Otherwise, let k be the maximum natural number such that k ϕ ≤ n,n = n − (n mod ϕ), and P = {p k , p k−1 , · · · , p 1 } be a set such that Then, it is sufficient to prove that P is uniquely determined forn since k,n, and each r i are uniquely determined from n, ϕ, and P. By Definition 11, 0 ≤ p i · i ϕ < i+1 ϕ holds for 1 ≤ i ≤ k. We assume that p k is not the maximum natural number such that p k · k ϕ ≤n, then holds. On the other hand, also holds. By (3) and (4), ∑ k i=1 p i · i ϕ <n holds. However, this contradicts to (2). Therefore, p k must be the maximum one. Similar discussion holds for p k−1 , p k−2 , · · · , p 2 , that is, for 2 ≤ i ≤ k − 1, p i is the maximum natural number such that p i · i ϕ ≤n mod i+1 ϕ. By (2), p 1 is unique if p k , p k−1 · · · , p 2 are unique. Hence, P is uniquely determined forn and k. r f ( f · · · ( f x) · · · ))]) C(ϕ).

2
For the size of Λ(T ϕ [n]), the following two lemmas hold.

Lemma 2. For a given T ϕ [n]
with natural numbers n and ϕ ≥ 2, let N a and N m be the numbers of addition and multiplication occurring in T ϕ [n], respectively. Also, for tetration n 2 n 1 , we call n 2 second argument of tetration, then let N t be the sum of second arguments of all tetration occurring in T ϕ [n]. Then, holds with r = n mod ϕ.

Further Compaction
Let Λ(T ϕ [n]) = M C(ϕ). Hereinafter, we call M and C(ϕ) function term and argument term, respectively, similarly to the λ-terms stated in Corollary 2. Moreover, for a given natural number n, ϕ * denotes a natural number such that ϕ * ≥ 2 and #Λ(T ϕ * [n]) is smaller than any other #Λ(T ϕ [n]), where ϕ = ϕ * . Lemma 3 implies that there is room for more compaction of . This holds recursively, that is, we can obtain a compact λ-term L(n) such that L(n) −→ * β C(n) and #L(n) ≤ #Λ(T ϕ * [n]) as where M is the function term of Λ(T ϕ * [n]). We show an algorithm generating L(n) for a given n in Algorithm 2. In line 9, we use Algorithm 1 to obtain Λ(E ϕ ). Assume that O(α) is the time complexity of Algorithm 1, then as shown in Algorithm 2, O(α √ n) time is required to obtain ϕ * once. For the size of L(n), the following theorem holds. Proof. By Definition 11 and Lemma 3, holds. Since t = slog ϕ n, the statement holds. 2 As seen in the above Theorem 3, by using proposed method, we can obtain L(n) which is a tetrationally compact expression of C(n) for some n. This is properly smaller than B(n) which we introduced in Definition 7. However, Theorem 3 refers to only special cases of n. For general n, the following theorem holds. We need the following lemma to prove Theorem 4.

Proof of Theorem 4.
By the definition of L(n), we assume that L(n) = M ϕ * L(ϕ * ), where M ϕ * is the function term of Λ(T ϕ * [n]). By Definition 5, the following holds: First, we consider the size of M ϕ * . For a given natural number n, we inductively define a TAÊ T 2 (n) as follows: where k is the maximum natural number such that k 2 ≤ n, and for 1 ≤ i ≤ k, each p i is the natural number such that 0 ≤ p i · i 2 < i+1 2 and r i = p i mod 2. If p i = 0 or 1, we omit the corresponding terms or coefficients in the TAE, similarly to T 2 [n]. WhileT 2 (n) is not E 2 [n] (since there is a case that the evaluated result ofT 2 (n) is not equal to n),T 2 (n) is clearly a TAE E ϕ . Therefore, by Lemma 1, a CFLT Λ(T 2 (n)) exists. By Lemma 2, the upper bound of #Λ(T 2 (n)) depends on the number of the arithmetic operations occurring inT 2 (n). Let N a and N m be the numbers of addition and multiplication occurring in T 2 [n], respectively. Also, for tetration n 2 n 1 , we call n 2 second argument of tetration, then let N t be the sum of second arguments of all tetration occurring in T 2 [n]. Let us denoteN a ,N m , andN t similarly forT 2 (n). Here, N a ≤N a , N m ≤N m , and N t ≤N t hold, since in T 2 [n], no terms increase compared tô T 2 (n) while the terms i 2 following i 2 ·T 2 (n) is reduced if r i = 0. Then, follows Lemma 2. Note that (n mod 2) ≤ 1. LetN(n) = 14N a + 8N m + 2N t + 11, then by the above, holds. We assume that ρ is the depth of the recursion inT 2 (n). Then, kN(p k − r k ) = O(k ρ ) holds. Therefore, by (12), holds. Here, ( k 2) ρ ≤ n ⇐⇒ ρ log k 2 ≤ log n holds. Thus, by Corollary 1, ρ ≤ log n log k 2 < log n log (slog 2 n)−1 2 = log n log log n holds. By Definition 2, k ≤ slog 2 n. Then, follows (13). By Lemma 4, #M ϕ * is bounded by Λ(T 2 [n]) since the function term of Λ(T 2 [n]) is smaller than Λ(T 2 [n]) itself. Then, by (14), we obtain Second, we consider the size of L(ϕ * ). If ϕ * ≤ 8, it is constant since L(ϕ * ) = C(ϕ * ) and #C(ϕ * ) ≤ C(8) = 19. Otherwise, #L(ϕ * ) ≤ Λ(T ϕ * 1 [ϕ * ]) holds with a natural number ϕ * 1 . By the definition of L(n), it recursively holds until ϕ * i ≤ 8. Thus, by (11) and (15), holds, where C is a constant and m is a natural number such that ϕ * m ≤ 8. Here, by Lemma 2, follows (16). Note that ∑ ∞ i=1 F(x i ) < 2F(x 1 ) holds for function F(x) and natural number x such that F(x i ) ≥ 0 and x i+1 ≤ √ x i for 1 ≤ i.

Application to Higher-Order Compression and Comparative Experiments
We implemented our method stated in Section 3, and conducted an experiment compared to the binary expression on λ-terms stated in Definition 7 and λ-terms generated by using the method stated in [2] (we call the method YKS).
Let B(n) denote the binary expression of n on λ-terms. As seen in Section 3, our proposed method generates a compact λ-term L(n) for a given C(n). While C(n) has the size of Θ(n), the size of the λ-term L(n) achieves O(slog 2 n) for some n, as shown in Theorem 3. However, as shown in Theorem 4, the size of L(n) becomes O((slog 2 n) log n/ log log n ) for general n, which is greater than that of B(n), Θ(log n). On the other hand, YKS is a method of higher-order compression, which generates an extended λ-term for a given text. Here, a text is an element of Σ * , where Σ is an alphabet. Let L YKS (n) be the generated extended λ-term by using YKS for a given text a n c with a, c ∈ Σ. Then, we can find that the size of L YKS (n) achieves O(slog 2 n) for some n, similarly to L(n).
We define extended λ-terms following to [2]. We show that the application of B(n) and L(n) to extended λ-terms is directly possible. (Extended λ-terms and their sizes). Let x be a variable and a ∈ Σ. Then, extended λ-termM is defined inductively as follows:M

Definition 12
The size ofM, denoted by #M, is inductively defined as follows: Similar to normal λ-terms, we define β-reduction, variable replacement notationM 1 [x :=M 2 ], and simplification on extended λ-terms. Clearly, we can regard any normal λ-terms as extended λ-terms.
From B(n) and L(n), we can obtain extended λ-termsB(n) andL(n) such thatB(n) −→ * βS (n) and L(n) −→ * βS (n), respectively, as follows. By Definition 7 and the definition of L(n), we assume that B(n) = (λp. respectively. Note that B(n) a c −→ * βS (n) and L(n) a c −→ * βS (n) holds, since B(n) −→ * β C(n), L(n) −→ * β C(n), and C(n) a c −→ * βS (n). Here,B(n) andL(n) are the results of simplification for B(n) a c and L(n) a c, respectively. Figures 1 and 2 show the experimental results. In both of them, "Binary", "YKS", and "Proposed" denote the methods using the binary expression on λ-terms, YKS, and ours, respectively. The horizontal axis shows the repetition number n of given text a n c. Then, we compare the sizes of the generated extended λ-terms by using these three methods, for the given a n c.   In Figure 1, the vertical axis shows the sizes ofB(n), L YKS (n), andL(n). The inequality #L(n) ≤ #B(n) holds in 5187 out of 10,000 cases within the range [1,10000]. The average ratio #L(n)/#B(n) for the range is approximately 0.9962. Similarly, the inequality #L(n) ≤ #L YKS (n) holds in 5959 cases, and the average ratio #L(n)/#L YKS (n) is approximately 0.9321.
In Figure 2, the vertical axis shows the ratio (the average size of cumulative sum of extended λ-terms from 1 to n)/#B(n). That is, Figure 2 shows how #L YKS (n) and #L(n) increase compared to #B(n) on average. As can be seen, #L(n) tends to be greater than #B(n) when n is greater than 10,000. We consider that it is consistent with the theoretical upper bound O((slog 2 n) log n/ log log n ), stated in Theorem 4.

Conclusions
In this paper, we addressed the problem of compaction of Church numerals. For given natural number n, by using proposed RTP, we decompose n into an arithmetic expression, which enables to obtain a compact λ-term leading to the Church numeral of n. We proved that the size of the obtained λ-term becomes O((slog 2 n) log n/ log log n ). Moreover, we experimentally confirmed that the λ-terms produced by our method tend to be smaller than binary expressions on λ-terms on average, when the given number is less than approximately 10,000. We have not proved the lower bound of the size in the worst case; it is our future work.
The compaction of Church numerals can be applied to higher-order compression, that uses extended λ-terms as the data model. In the procedure of higher-order compression, a repetitive part in the input can be represented as a λ-term with C(n). Thus, efficient compaction of C(n) will help for improving the compression performance of higher-order compression.
In data compression, data models are finally encoded in bit sequences. For bit encoding of λ-terms, Tromp [16] proposed a method for untyped λ-terms. Very recently, Takeda et al. [17] proposed an efficient encoding scheme for simply-typed λ-terms. Finding an efficient bit encoding for our method is also our future work.