Combinatorics of explicit substitutions Maciej Bendkowski 1 Pierre - - PowerPoint PPT Presentation

combinatorics of explicit substitutions
SMART_READER_LITE
LIVE PREVIEW

Combinatorics of explicit substitutions Maciej Bendkowski 1 Pierre - - PowerPoint PPT Presentation

Combinatorics of explicit substitutions Maciej Bendkowski 1 Pierre Lescanne 2 1 Jagiellonian University in Krakw 2 cole normale suprieure de Lyon Computational Logic and Applications Paris, May 25th 2018 1/24 Outline -calculus and


slide-1
SLIDE 1

1/24

Combinatorics of explicit substitutions

Maciej Bendkowski1 Pierre Lescanne2

1Jagiellonian University in Kraków 2École normale supérieure de Lyon

Computational Logic and Applications Paris, May 25th 2018

slide-2
SLIDE 2

2/24

Outline

λ-calculus and substitution resolution λυ-calculus and explicit substitutions Our contribution

slide-3
SLIDE 3

3/24

β-reduction in λ-calculus

Substitution of terms for variables forms the essence of β-reduction in λ-calculus. For instance, (λx.x(yx)(λz.z))T →β T(yT)(λz.z): @ λx @ @ x @ y x λz z T @ @ T @ y T λz z

slide-4
SLIDE 4

4/24

β-reduction in λ-calculus (ii)

However, what if we substitute T with free variables under an abstraction? For instance, what if z occurs freely in T, as in the following example: @ λx @ @ y @ y y λz x T

slide-5
SLIDE 5

4/24

β-reduction in λ-calculus (ii)

However, what if we substitute T with free variables under an abstraction? For instance, what if z occurs freely in T, as in the following example: @ λx @ @ y @ y y λz x T

◮ We want to avoid the

capture of variable z in T;

slide-6
SLIDE 6

4/24

β-reduction in λ-calculus (ii)

However, what if we substitute T with free variables under an abstraction? For instance, what if z occurs freely in T, as in the following example: @ λx @ @ y @ y y λz x T

◮ We want to avoid the

capture of variable z in T;

◮ Requires keeping track

  • f variable names and

(sometimes) their renaming;

slide-7
SLIDE 7

4/24

β-reduction in λ-calculus (ii)

However, what if we substitute T with free variables under an abstraction? For instance, what if z occurs freely in T, as in the following example: @ λx @ @ y @ y y λz x T

◮ We want to avoid the

capture of variable z in T;

◮ Requires keeping track

  • f variable names and

(sometimes) their renaming;

◮ In principle feasible, but

in practice it is not a trivial operation to execute.

slide-8
SLIDE 8

5/24

β-reduction and computational effectiveness

Postulate (essentially due to de Bruijn ’78)

The number of β-reductions required to normalise a given term (in

  • ther words, evaluate the encoded computation) does not quite

reflect the computational effort required to carry out the computation.

slide-9
SLIDE 9

5/24

β-reduction and computational effectiveness

Postulate (essentially due to de Bruijn ’78)

The number of β-reductions required to normalise a given term (in

  • ther words, evaluate the encoded computation) does not quite

reflect the computational effort required to carry out the computation.

◮ It hides the non-trivial details of capture-avoiding substitution;

slide-10
SLIDE 10

5/24

β-reduction and computational effectiveness

Postulate (essentially due to de Bruijn ’78)

The number of β-reductions required to normalise a given term (in

  • ther words, evaluate the encoded computation) does not quite

reflect the computational effort required to carry out the computation.

◮ It hides the non-trivial details of capture-avoiding substitution; ◮ It does not reflect the cost of substitution resolution;

slide-11
SLIDE 11

5/24

β-reduction and computational effectiveness

Postulate (essentially due to de Bruijn ’78)

The number of β-reductions required to normalise a given term (in

  • ther words, evaluate the encoded computation) does not quite

reflect the computational effort required to carry out the computation.

◮ It hides the non-trivial details of capture-avoiding substitution; ◮ It does not reflect the cost of substitution resolution; ◮ and finally, it hides the details of evaluation strategies.

slide-12
SLIDE 12

5/24

β-reduction and computational effectiveness

Postulate (essentially due to de Bruijn ’78)

The number of β-reductions required to normalise a given term (in

  • ther words, evaluate the encoded computation) does not quite

reflect the computational effort required to carry out the computation.

◮ It hides the non-trivial details of capture-avoiding substitution; ◮ It does not reflect the cost of substitution resolution; ◮ and finally, it hides the details of evaluation strategies.

. . . and that is due to the epitheoretic substitution operation.

slide-13
SLIDE 13

6/24

β-reduction and computational effectiveness (ii)

Issue I

β-reduction hides the non-trivial details of capture-avoiding

  • substitution. Variable renaming can take time linear in the term size.

@ λx @ @ y @ y y λz x T

slide-14
SLIDE 14

7/24

β-reduction and computational effectiveness (iii)

Issue II

β-reduction does not reflect the cost of substitution resolution. Variable search can take time linear in the term size. @ λx @ @ x @ y x λz z T

slide-15
SLIDE 15

8/24

β-reduction and computational effectiveness (iv)

Issue III

β-reduction hides the details of evaluation strategies. What if the substitution is carried out non-strictly, e.g. it is suspended and evaluated on demand (perhaps even never)? Real-life1 example of an infinite lists of Fibonacci numbers:

1

fibs = 0 : 1 : next fibs

2

where

3

next (a : b : xs) = (a + b) : next (b : xs)

1Is this the real life? Is this just fantasy? (. . .) [Mercury et al. ’75]

slide-16
SLIDE 16

9/24

Effectuation of substitution

In order to effectuate substitution, its resolution should be internalised into the calculus itself. Possible solutions:

◮ investigate combinatory logic, or

slide-17
SLIDE 17

9/24

Effectuation of substitution

In order to effectuate substitution, its resolution should be internalised into the calculus itself. Possible solutions:

◮ investigate combinatory logic, or ◮ investigate calculi of explicit substitution;

slide-18
SLIDE 18

9/24

Effectuation of substitution

In order to effectuate substitution, its resolution should be internalised into the calculus itself. Possible solutions:

◮ investigate combinatory logic, or ◮ investigate calculi of explicit substitution;

Qest

Investigate quantitative aspects of substitution. For instance,

slide-19
SLIDE 19

9/24

Effectuation of substitution

In order to effectuate substitution, its resolution should be internalised into the calculus itself. Possible solutions:

◮ investigate combinatory logic, or ◮ investigate calculi of explicit substitution;

Qest

Investigate quantitative aspects of substitution. For instance,

◮ What is the average-case cost of resolving substitutions in

computations (terms) of size n? How does it change depending

  • n the assumed evaluation strategy? What contributes to its

execution time?

slide-20
SLIDE 20

9/24

Effectuation of substitution

In order to effectuate substitution, its resolution should be internalised into the calculus itself. Possible solutions:

◮ investigate combinatory logic, or ◮ investigate calculi of explicit substitution;

Qest

Investigate quantitative aspects of substitution. For instance,

◮ What is the average-case cost of resolving substitutions in

computations (terms) of size n? How does it change depending

  • n the assumed evaluation strategy? What contributes to its

execution time?

◮ What is the average-case complexity of abstract machines

executing (terminating) computations of considered calculi? Is it possible to optimise them based on the structure of typical computations?

slide-21
SLIDE 21

10/24

Reflections on combinatory logic

Normal-order reduction

If a term (combinator) T is normalisable, then the iterative contraction of the lefmost-outermost redex leads to the (unique) normal form of T. Sxyz → xz(yz) Kxy → x R0 = S K KR0 SR0 SR0R0 R1 = SR1 KR1 SR0R1 SR1R0 KR0C KSCR0 KKCR0 KSCR0R0 K(SR0)CR0 SSSR0 SSKR0 SS(SR0)R0 R2 = . . .

Figure: Rewriting rules of SK-combinators and the corresponding normal-order reduction grammars.

slide-22
SLIDE 22

11/24

Reflections on combinatory logic (ii)

Theorem (B., Grygiel and Zaionc ’17)

For each k ≥ 1, the asymptotic density µ(Rk/C) of combinators reducing in k normal-order reduction steps in the set of all combinators is positive. In particular, we have: k µ(Rk/C) 0. 1 0.08961 2 0.06417 3 0.05010 4 0.04131 5 0.03570 6 0.03119 7 0.02798

Problem

How to port these results to the realm of λ-calculus having an external substitution? . . . use explicit substitutions!

slide-23
SLIDE 23

12/24

Outline

λ-calculus and substitution resolution λυ-calculus and explicit substitutions Our contribution

slide-24
SLIDE 24

13/24

λυ-calculus (lambda upsilon calculus)

A simplistic calculus of explicit substitutions due to Lescanne ’94. T ::= N | λT | T T | T [S] S ::= T / | ⇑ (S) | ↑ N ::= 0 | SN.

Figure: Terms of λυ-calculus.

(λa)b → a[b/] (Beta) (ab)[s] → a[s](b[s]) (App) (λa)[s] → λ(a[⇑ (s)]) (Lambda) 0[a/] → a (FVar) (Sn)[a/] → n (RVar) 0[⇑ (s)] → 0 (FVarLif) (Sn)[⇑ (s)] → n[s][↑] (RVarLif) n[↑] → Sn. (VarShif)

Figure: Rewriting rules.

slide-25
SLIDE 25

14/24

λυ-calculus (ii)

Consider the term K = λx.λy.x; denoted as λλ1. Certainly, Kab →β a for each term a (in one step). Note however, that with explicit substitutions we have (λλ1)a → (λ1)[a/] → λ(1[⇑ (a/)]) → λ (0[a/][↑]) → λ (a[↑]) . The final shif operator guarantees that (potential) free indices are aptly incremented so to avoid potential variable captures. If a is closed, then a[↑] resolves simply to a, as intended.

slide-26
SLIDE 26

15/24

Outline

λ-calculus and substitution resolution λυ-calculus and explicit substitutions Our contribution

slide-27
SLIDE 27

16/24

Enumerative results

Proposition

Let T(z) and S(z) denote the generating functions corresponding to λυ-terms and substitutions, respectively. Then, T(z) = 1 − √1 − 4z 2z −1 whereas S(z) = 1 − √1 − 4z 2z

  • z

1 − z

  • .

In consequence [zn]T(z) =    0, for n = 0 1 n + 1 2n n

  • ,
  • therwise

[zn]S(z) =      0, for n = 0

n−1

  • k=0

1 k + 1 2k k

  • therwise.
slide-28
SLIDE 28

17/24

Enumerative results – explicit bijection

ϕ

R

  • =

λϕ(R) ϕ

  • L
  • =

Sn when ϕ(L) = n ϕ

  • L
  • =

a[⇑n+1 (s)] when ϕ(L) = a[⇑n (s)] ϕ(•) = ϕ   

  • R

   = ϕ(R)[↑] ϕ   

  • L

R    = ϕ(L)[ϕ(R)/] ϕ

  • L

R

  • =

ϕ(L) ϕ(R)

slide-29
SLIDE 29

18/24

Enumerative results – correspondence example

For instance, 0[⇑ (λ0/)] 1[↑] corresponds to

slide-30
SLIDE 30

19/24

Statistical properties

Strict substitution forms

A λυ-term t is in strict substitution form if there exist two pure (i.e. without explicit substitutions) terms a, b and a sequence t1, . . . , tn of λυ-terms such that a[b/] → t1 → · · · → tn = t and none of the above reductions is (Beta). Otherwise, t is said to be in lazy substitution form.

Theorem

Asymptotically almost all λυ-terms are in lazy substitution form.

slide-31
SLIDE 31

19/24

Statistical properties

Strict substitution forms

A λυ-term t is in strict substitution form if there exist two pure (i.e. without explicit substitutions) terms a, b and a sequence t1, . . . , tn of λυ-terms such that a[b/] → t1 → · · · → tn = t and none of the above reductions is (Beta). Otherwise, t is said to be in lazy substitution form.

Theorem

Asymptotically almost all λυ-terms are in lazy substitution form.

Proof.

Idea: Consider the class of terms without nested closures.

slide-32
SLIDE 32

20/24

Statistical properties (ii)

Substitution suspension

Let s be an substitution and t be a λυ-term. Then, s, all its subterms, and all the constructors it contains are said to be suspended in t if t contains a subterm in form of [s]; in other words, when s occurs under a closure in t.

Theorem

Let Xn be a random variable denoting the number of constructors not suspended under a closure in a random λυ-term of size n. Then, the expectation E(Xn) satisfies E(Xn) − − − →

n→∞

316 3 ≈ 105.33.

slide-33
SLIDE 33

21/24

Statistical properties (iii)

Proposition

Let R be a redex in the λυ-calculus and Xn be the corresponding random variable denoting the number of occurrences of R in a random term of size n. Then, afer standardisation, Xn admits a limiting Gaussian law. In particular, we obtain: Redex (limit) mean (limit) variance (Beta) 0.046875n 0.037354n (App) 0.031250n 0.021973n (Lambda) 0.031250n 0.025879n (VarShif) 0.015625n 0.013916n (FVar) 0.011719n 0.011124n (FVarLif) 0.007812n 0.007355n (RVar) 0.003906n 0.003799n (RVarLif) 0.002604n 0.002557n

slide-34
SLIDE 34

22/24

Conclusions

◮ Standard analytic methods provide insight into the structure of

random λυ-terms, in particular substitution resolution;

slide-35
SLIDE 35

22/24

Conclusions

◮ Standard analytic methods provide insight into the structure of

random λυ-terms, in particular substitution resolution;

◮ Typical computations are in a strong sense lazy. Substitutions

are evaluated non-strictly. In fact, almost all of the computation content is suspended;

slide-36
SLIDE 36

22/24

Conclusions

◮ Standard analytic methods provide insight into the structure of

random λυ-terms, in particular substitution resolution;

◮ Typical computations are in a strong sense lazy. Substitutions

are evaluated non-strictly. In fact, almost all of the computation content is suspended;

◮ Substitution primitives have, on average, an uneven

contribution in typical terms. We can exploit that in micro

  • ptimisations of abstract machines.
slide-37
SLIDE 37

23/24

Future work

◮ What is the cost distribution of substitution resolution, given a

random term of size n? What primitives are its main contributors?

slide-38
SLIDE 38

23/24

Future work

◮ What is the cost distribution of substitution resolution, given a

random term of size n? What primitives are its main contributors?

◮ Does normal-order reduction (think of non-strict functional

programming languages) has a similar, average-case "distribution shape" as the one for combinatory logic?

slide-39
SLIDE 39

24/24

Thank you

Thank you for your atention! (Strict questions and lazy answers)