Counting terms in the binary lambda calculus - Aofa 2014

Report 3 Downloads 21 Views
Counting terms in the binary lambda calculus

Katarzyna Grygiel

Theoretical Computer Science Department Jagiellonian University in Kraków

Pierre Lescanne

Laboratoire de l'Informatique du Parallélisme École Normale Supérieure de Lyon

Analysis of Algorithms Paris, June 2014

Contents

1. Open problem: enumeration of lambda calculus terms 2. Attempts to solve the enumeration problem 3. Motivation 4. Binary lambda calculus 5. Enumerating binary lambda terms

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

1 / 28

The problem Terms in untyped lambda calculus

Lambda terms: t ::= x | λx.t | (t t)

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

2 / 28

The problem Terms in untyped lambda calculus

Lambda terms: t ::= x | λx.t | (t t)

Two alternative notions of size: |x| = 0 |λx.t| = 1 + |t| |t s| = 1 + |t| + |s|

K. Grygiel & P. Lescanne

|x| = 1 |λx.t| = 1 + |t| |t s| = 1 + |t| + |s|

Counting binary lambda terms

AofA 2014

2 / 28

The problem It's nicer to look at trees

Combinatorial interpretation by lambda trees: λx λy

@ @

λz

x z

@

λw

y x

The lambda tree corresponding to the lambda term  λx.(λz.z)x λy .y (λw .x) K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

3 / 28

The problem Open problem

How many closed lambda terms of a given size are there?

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

4 / 28

Attempts Attempts to solve the enumeration problem

• Studying lambda polynomials and their coecients • Estimating the number of simpler structures • Applying the theory of generating functions • Analyzing lambda terms of bounded unary height • Enumerating BCI/BCK terms

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

5 / 28

Attempts (|x|

Canonical formula

= 0)

Let Tn,m denote the number of terms of size n with occurrences of at most m distinct free variables: T0,m = m Tn+1,m = Tn,m+1 +

K. Grygiel & P. Lescanne

n X

Ti,m Tn−i,m

i=0

Counting binary lambda terms

AofA 2014

6 / 28

Attempts (|x|

Canonical formula

= 0)

Let Tn,m denote the number of terms of size n with occurrences of at most m distinct free variables: T0,m = m Tn+1,m = Tn,m+1 +

n X

Ti,m Tn−i,m

i=0

The sequence (Tn,0 )n≥0 (A220894 in Sloane's OEIS) enumerates closed terms. Its rst values are: 0, 1, 3, 14, 82, 579, 4741, 43977, 454283, 5159441, 63782411.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

6 / 28

Attempts Studying lambda polynomials and their coecients (|x|

= 0)

For every n ≥ 0 we associate with Tn,m a polynomial Pn (m) in m. P0 (m) = m Pn+1 (m) = Pn (m + 1) +

n X

Pi (m)Pn−i (m)

i=0

For every n ≥ 0 we have Pn (0) = Tn,0 .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

7 / 28

Attempts Studying lambda polynomials and their coecients (|x|

= 0)

For every n ≥ 0 we associate with Tn,m a polynomial Pn (m) in m. P0 (m) = m Pn+1 (m) = Pn (m + 1) +

n X

Pi (m)Pn−i (m)

i=0

For every n ≥ 0 we have Pn (0) = Tn,0 . For k > 0 and n ≥ 0, let pn[k] denote the k -th leading coecient of the polynomial Pn . KG, PL; 2013 [k]

pn

=

1



2k−1 (k − 1)! π

K. Grygiel & P. Lescanne

4n n(2k−5)/2 1 + 

 1  (2k − 3)(2k − 5) +O 4 . 8n n

Counting binary lambda terms

AofA 2014

7 / 28

Attempts Estimating the number of simpler structures

(|x|

= 0)

David, KG, Kozik, Raalli, Theyssier, Zaionc; 2011 The number of closed lambda terms of size n is bounded:   n (4 − ε)n n− ln n • from below by , ln n n   (12 + ε)n n− 3 ln n • from above by . ln n

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

8 / 28

Attempts Estimating the number of simpler structures

(|x|

= 0)

David, KG, Kozik, Raalli, Theyssier, Zaionc; 2011 The number of closed lambda terms of size n is bounded:   n (4 − ε)n n− ln n • from below by , ln n n   (12 + ε)n n− 3 ln n • from above by . ln n Therefore

K. Grygiel & P. Lescanne

Tn,0 ∼ nn (ln n)−n O(e n ).

Counting binary lambda terms

AofA 2014

8 / 28

Attempts Applying generating functions

(|x|

= 1)

Let Ln,k denote the number of lambda P terms of size n and with at most k distinct free variables and let `(z, f ) = n,k≥0 z n f k . Hence, `(z, 0) is the generating function for the sequence enumerating closed lambda terms.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

9 / 28

Attempts (|x|

Applying generating functions

= 1)

Let Ln,k denote the number of lambda P terms of size n and with at most k distinct free variables and let `(z, f ) = n,k≥0 z n f k . Hence, `(z, 0) is the generating function for the sequence enumerating closed lambda terms. Bodini, Gardy, Gittenberger; 2011 The function `(z, f ) satises the functional equation `(z, f ) = fz + z`2 (z, f ) + z`(z, f + 1).

This leads to `(z, f ) =

Therefore, r `(z, 0) =

1−

1 − 2z + 2z

K. Grygiel & P. Lescanne

1−

p

1 − 4z 2 f − 4z 2 `(z, f + 1) . 2z

q p √ 1 − 4z 2 − 2z + 2z 1 − 8z 2 − 2z + 2z 1 − . . . .

2z

Counting binary lambda terms

AofA 2014

9 / 28

Attempts Analyzing terms of bounded unary height

(|x|

= 1)

Bodini, Gardy, Gittenberger; 2011 Consider closed terms with a limited (given) number of unary nodes on each branch.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

10 / 28

Attempts Analyzing terms of bounded unary height

(|x|

= 1)

Bodini, Gardy, Gittenberger; 2011 Consider closed terms with a limited (given) number of unary nodes on each branch. The generating function for the sequence enumerating such terms is the truncated version of `(z, 0).

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

10 / 28

Attempts Analyzing terms of bounded unary height

(|x|

= 1)

Bodini, Gardy, Gittenberger; 2011 Consider closed terms with a limited (given) number of unary nodes on each branch. The generating function for the sequence enumerating such terms is the truncated version of `(z, 0). One observes the intriguing behavior of dominant singularities of such generating functions. Some singularities cancel one radicand, some cancel two. This leads to two dierent asymptotic behaviors, dependent on the value of unary height.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

10 / 28

Attempts Enumerating BCI/BCK terms

(|x|

= 1)

Special subclasses of lambda terms: BCI /BCK and their generalized versions: BCI (p)/BCK (p)

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

11 / 28

Attempts (|x|

Enumerating BCI/BCK terms

= 1)

Special subclasses of lambda terms: BCI /BCK and their generalized versions: BCI (p)/BCK (p) Bodini, Gardy, Gittenberger, Jacquot; 2013 The number of BCI (p)-terms of size (2p + 1)n − 1 is asymptotically 2p+1 

2p − 2 (1.0844 . . .) 2 p p−1





K. Grygiel & P. Lescanne

(4p + 2)p p!

n−1

p 2 −2p n 2p+1 (n − 1)!p ,

Counting binary lambda terms

p ≥ 2.

AofA 2014

11 / 28

Attempts (|x|

Enumerating BCI/BCK terms

= 1)

Special subclasses of lambda terms: BCI /BCK and their generalized versions: BCI (p)/BCK (p) Bodini, Gardy, Gittenberger, Jacquot; 2013 The number of BCI (p)-terms of size (2p + 1)n − 1 is asymptotically 2p+1 

2p − 2 (1.0844 . . .) 2 p p−1





(4p + 2)p p!

n−1

p 2 −2p n 2p+1 (n − 1)!p ,

p ≥ 2.

Bodini, Gittenberger; 2014 The number of BCK (2)-terms of size n is asymptotically   7 · 24/5 3/5 17 · 21/5 2/5 41 · 23/5 1/5 −3/5 An2n/5 2n/5 e −2n/5 exp 2−8/5 n4/5 + n − n − n n , 15 75 500

where A is some positive constant. K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

11 / 28

Motivation Motivation

• Lambda terms as programs → research on random programs and their behavior

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

12 / 28

Motivation Motivation

• Lambda terms as programs → research on random programs and their behavior • Typed lambda terms as proofs of logical formulae → research on logical entities and their properties

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

12 / 28

Motivation Motivation

• Lambda terms as programs → research on random programs and their behavior • Typed lambda terms as proofs of logical formulae → research on logical entities and their properties • Lambda calculus as the basis for functional programming → testing compilers by generating random lambda terms

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

12 / 28

Binary λ-calculus Lambda terms and de Bruijn indices

Let's eliminate names of variables from the notation of a λ-term: variables x, y , z, . . . λx.M

de Bruijn indices 1, 2, 3, . . . λM, where M is the result of substituting each x by n, where n is the number of λ's

above the given occurrence of x MN

K. Grygiel & P. Lescanne

MN

Counting binary lambda terms

AofA 2014

13 / 28

Binary λ-calculus Lambda terms and de Bruijn indices

Regular lambda term λx λy

@ @

λz

x z

@

λw

y x

The lambda term λx.(λz.z)x λy .y (λw .x)



K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

14 / 28

Binary λ-calculus Lambda terms and de Bruijn indices

Lambda term in the de Bruijn version λ

λ

@ @

λ

1 1

@

λ

1 3

The de Bruijn term λ(λ1)1 λ1(λ3)



K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

15 / 28

Binary λ-calculus Binary representation of lambda terms

Following John Tromp, we dene the binary representation of de Bruijn indices in the following way: d b, λ M = 00M dN = 01M bN b, M i bi = 1 0.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

16 / 28

Binary λ-calculus Binary representation of lambda terms

Following John Tromp, we dene the binary representation of de Bruijn indices in the following way: d b, λ M = 00M dN = 01M bN b, M i bi = 1 0.

Given a λ-term, we dene its size as the length of the corresponding binary sequence, i.e., |i| = i + 1, |λM| = |M| + 2, |M N| = |M| + |N| + 2. K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

16 / 28

Enumerating binary terms How many. . . ?

Let Sm,n denote the number of binary lambda terms of size n with at most m distinct free indices.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

17 / 28

Enumerating binary terms How many. . . ?

Let Sm,n denote the number of binary lambda terms of size n with at most m distinct free indices. Sm,0 = Sm,1 = 0, Sm,n+2 = [m ≥ n + 1] + Sm+1,n +

n X

Sm,k Sm,n−k .

k=0

The sequence (S0,n )n≥0 (A114852 in Sloane's OEIS) enumerates closed binary lambda terms of size n. Its rst 20 values are as follows: 0, 0, 0, 0, 1, 0, 1, 1, 2, 1, 6, 5, 13, 14, 37, 44, 101, 134, 298, 431. K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

17 / 28

Enumerating binary terms Generating functions again in action

Now let us dene the family of generating functions for sequences (Sm,n )n≥0 : sm (z) =

∞ X

Sm,n z n .

n=0

Most of all, we are interested in the generating function for the number of closed terms, i.e., s0 (z) =

K. Grygiel & P. Lescanne

∞ X n=0

S0,n z n .

Counting binary lambda terms

AofA 2014

18 / 28

Enumerating binary terms Recurring problem

Lemma sm (z) =

K. Grygiel & P. Lescanne

z 2 (1 − z m ) + z 2 sm+1 (z) + z 2 sm (z)2 . 1−z

Counting binary lambda terms

AofA 2014

19 / 28

Enumerating binary terms Recurring problem

Lemma sm (z) =

z 2 (1 − z m ) + z 2 sm+1 (z) + z 2 sm (z)2 . 1−z

This gives 1− sm (z) =

r

1 − 4z 4

1−z m + s m+1 (z) 1−z . 2z 2





Again we are stuck with innitely nested radicals. . .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

19 / 28

Enumerating binary terms Let's count all binary terms

Let S∞,n denote the number of all (not necessarily closed) binary terms of size n. We obtain the following recurrence relation: S∞,0 = S∞,1 = 0, S∞,n+2 = 1 + S∞,n +

n X

S∞,k S∞,n−k .

k=0

The sequence (S∞,n )n≥0 has the entry number A114851 in Sloane's OEIS. Its rst 20 values are: 0, 0, 1, 1, 2, 2, 4, 5, 10, 14, 27, 41, 78, 126, 237, 399, 745, 1292, 2404, 4259. K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

20 / 28

Enumerating binary terms No problem this time

Let s∞ (z) denote the generating function for the sequence (S∞,n )n≥0 .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

21 / 28

Enumerating binary terms No problem this time

Let s∞ (z) denote the generating function for the sequence (S∞,n )n≥0 . Theorem The number of all binary lambda terms of size n satises C S∞,n ∼ (1/ρ)n · 3/2 , n

where ρ = 0.509308127 . . . and C = 1.021874073 . . ..

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

21 / 28

Enumerating binary terms No problem this time

Let s∞ (z) denote the generating function for the sequence (S∞,n )n≥0 . Theorem The number of all binary lambda terms of size n satises C S∞,n ∼ (1/ρ)n · 3/2 , n

where ρ = 0.509308127 . . . and C = 1.021874073 . . .. For m ≥ n − 1 we have Sm,n = S∞,n . Therefore s∞ (z) =

∞ X

Sn,n z n ,

n=1

which yields that = ∞. Furthermore, s∞ (z) = lim sm (z). [z n ]sn

[z n ]s

m→∞

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

21 / 28

Enumerating binary terms Singularities of

sm (z)

Lemma Let ρm denote the dominant singularity of sm (z). Then for every m, ρm = ρ0 .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

22 / 28

Enumerating binary terms Singularities of

sm (z)

Lemma Let ρm denote the dominant singularity of sm (z). Then for every m, ρm = ρ0 .

Lemma The dominant singularity of s0 (z) is equal to the dominant singularity of s∞ (z), i.e., ρ0 = ρ = 0.509308127 . . .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

22 / 28

Enumerating binary terms Sketch of the proof

Let us dene functionals 1 − 4z 4 ( 11−z −z + F ) Φm (F ) = , 2z 2 q 1 + F) 1 − 1 − 4z 4 ( 1−z Φ∞ (F ) = . 2z 2 1−

K. Grygiel & P. Lescanne

q

Counting binary lambda terms

m

AofA 2014

23 / 28

Enumerating binary terms Sketch of the proof

Let us dene functionals 1 − 4z 4 ( 11−z −z + F ) Φm (F ) = , 2z 2 q 1 + F) 1 − 1 − 4z 4 ( 1−z Φ∞ (F ) = . 2z 2 1−

q

m

Then sm (z) = Φm (sm+1 (z)).

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

23 / 28

Enumerating binary terms Sketch of the proof

Let us dene functionals 1 − 4z 4 ( 11−z −z + F ) Φm (F ) = , 2z 2 q 1 + F) 1 − 1 − 4z 4 ( 1−z Φ∞ (F ) = . 2z 2 1−

q

m

Then sm (z) = Φm (sm+1 (z)).

For z ∈ [0, ρ] we have Φm (sm (z)) ≤ sm (z) ≤ sm+1 (z) ≤ s∞ (z). K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

23 / 28

Enumerating binary terms Sketch of the proof II

Let sem (z) denote the xed point of Φm , i.e., sem (z) is dened as the solution of the equation sem (z) = Φm (e sm (z)).

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

24 / 28

Enumerating binary terms Sketch of the proof II

Let sem (z) denote the xed point of Φm , i.e., sem (z) is dened as the solution of the equation sem (z) = Φm (e sm (z)).

For z ∈ [0, ρ] we have sem (z) ≤ sm (z) ≤ s∞ (z).

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

24 / 28

Enumerating binary terms Sketch of the proof II

Let sem (z) denote the xed point of Φm , i.e., sem (z) is dened as the solution of the equation sem (z) = Φm (e sm (z)).

For z ∈ [0, ρ] we have sem (z) ≤ sm (z) ≤ s∞ (z).

By the denition of sem (z), 2 (z) − (1 − z 2 )2 se (z) + z 2 sem m

K. Grygiel & P. Lescanne

z 2 (1 − z m ) = 0. 1−z

Counting binary lambda terms

AofA 2014

24 / 28

Enumerating binary terms Sketch of the proof II

Let sem (z) denote the xed point of Φm , i.e., sem (z) is dened as the solution of the equation sem (z) = Φm (e sm (z)).

For z ∈ [0, ρ] we have sem (z) ≤ sm (z) ≤ s∞ (z).

By the denition of sem (z), 2 (z) − (1 − z 2 )2 se (z) + z 2 sem m

z 2 (1 − z m ) = 0. 1−z

Let us denote the main singularity of sem (z) by σm . We have σm ≥ ρm ≥ ρ. K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

24 / 28

Enumerating binary terms Sketch of the proof III

The discriminant of this equation is ∆m = (1 − z 2 )2 −

K. Grygiel & P. Lescanne

4 z 4 (1 − z m ) . 1−z

Counting binary lambda terms

AofA 2014

25 / 28

Enumerating binary terms Sketch of the proof III

The discriminant of this equation is ∆m = (1 − z 2 )2 −

4 z 4 (1 − z m ) . 1−z

The value of σm is equal to the root of smallest modulus of the polynomial Pm (z) := (z − 1)∆m = 4z 4 (1 − z m ) − (1 − z)3 (1 + z)2 .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

25 / 28

Enumerating binary terms Sketch of the proof III

The discriminant of this equation is ∆m = (1 − z 2 )2 −

4 z 4 (1 − z m ) . 1−z

The value of σm is equal to the root of smallest modulus of the polynomial Pm (z) := (z − 1)∆m = 4z 4 (1 − z m ) − (1 − z)3 (1 + z)2 .

The sequence (σm )m∈N of roots of polynomials Pm (z) is decreasing and it converges to ρ.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

25 / 28

Enumerating binary terms Sketch of the proof III

The discriminant of this equation is ∆m = (1 − z 2 )2 −

4 z 4 (1 − z m ) . 1−z

The value of σm is equal to the root of smallest modulus of the polynomial Pm (z) := (z − 1)∆m = 4z 4 (1 − z m ) − (1 − z)3 (1 + z)2 .

The sequence (σm )m∈N of roots of polynomials Pm (z) is decreasing and it converges to ρ. By σm ≥ ρm ≥ ρ, we get ρm → ρ, as well.

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

25 / 28

Enumerating binary terms Sketch of the proof III

The discriminant of this equation is ∆m = (1 − z 2 )2 −

4 z 4 (1 − z m ) . 1−z

The value of σm is equal to the root of smallest modulus of the polynomial Pm (z) := (z − 1)∆m = 4z 4 (1 − z m ) − (1 − z)3 (1 + z)2 .

The sequence (σm )m∈N of roots of polynomials Pm (z) is decreasing and it converges to ρ. By σm ≥ ρm ≥ ρ, we get ρm → ρ, as well. Since all the ρm 's are equal, we obtain that ρm = ρ for every natural m. K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

25 / 28

Enumerating binary terms Number of closed binary terms

Theorem The number of closed binary binary lambda terms of size n is of exponential order S0,n ./ 1.963448 . . .n .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

26 / 28

Enumerating binary terms Conjecture

For every m,

 Sm,n ∼ o 1.963448 . . .n · n−3/2 .

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

27 / 28

Enumerating binary terms Conjecture

For every m,

 Sm,n ∼ o 1.963448 . . .n · n−3/2 .

Sm,n  n ρ n−3/2

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

27 / 28

References O. Bodini, B. Gittenberger: On the asymptotic number of BCK(2)-terms, in 2014 Proceedings of the Eleventh Workshop on Analytic Algorithmics and Combinatorics (ANALCO), pp. 25-39, 2014 O. Bodini, D. Gardy, B. Gittenberger: Lambda-terms of bounded unary height, 2011 Proceedings of the Eighth Workshop on Analytic Algorithmics and Combinatorics (ANALCO), 2011 O. Bodini, D. Gardy, B. Gittenberger, A. Jacquot: Enumeration of generalized BCI Electronic Journal of Combinatorics, 20(4):P30, 2013

lambda-terms,

R. David, K. Grygiel, J. Kozik, Ch. Raalli, G. Theyssier, M. Zaionc: Asymptotically Logical Methods in Computer Science Vol. 9(1:02) 2013, pp. 1-30 almost all lambda terms are strongly normalizing,

K. Grygiel, P. Lescanne: Counting and generating lambda terms, Journal of Functional Programming Vol. 23(5) 2013, pp. 594-628 K. Grygiel, P. Lescanne:

Counting terms in the binary lambda calculus,

2014

P. Lescanne: On counting untyped lambda terms, Theoretical Computer Science, 474:80-97, 2013 J. Tromp: Binary lambda calculus and combinatory logic. Kolmogorov Complexity and Applications, Vol. 06051 of Dagstuhl Seminar Proceedings, Schloss Dagstuhl, Germany, 2006

K. Grygiel & P. Lescanne

Counting binary lambda terms

AofA 2014

28 / 28