A(b)normal Environments Andrea Condoluci Department of Computer - - PowerPoint PPT Presentation

a b normal environments
SMART_READER_LITE
LIVE PREVIEW

A(b)normal Environments Andrea Condoluci Department of Computer - - PowerPoint PPT Presentation

A(b)normal Environments Andrea Condoluci Department of Computer Science and Engineering University of Bologna April 24 th , 2019 Joint work with B. Accattoli, G. Guerrieri & C. Sacerdoti Coen 1/30 A(b)normal Environments Crumbling


slide-1
SLIDE 1

1/30

A(b)normal Environments

Andrea Condoluci

Department of Computer Science and Engineering University of Bologna

April 24th, 2019

Joint work with

  • B. Accattoli, G. Guerrieri & C. Sacerdoti Coen
slide-2
SLIDE 2

2/30

A(b)normal Environments Crumbling Environments

Andrea Condoluci

Department of Computer Science and Engineering University of Bologna

April 24th, 2019

Joint work with

  • B. Accattoli, G. Guerrieri & C. Sacerdoti Coen
slide-3
SLIDE 3

3/30

Structure of the Presentation

Introduction The Curry-Howard-Lambek Correspondence The λ-Calculus Computational Complexity Micro-Step Evaluation Abstract Machines Crumbled Environments A(dministrative) Normal Form Crumbled Terms Crumbling The Crumble GLAM The Machine The Implementation Theorem Complexity Conclusions

slide-4
SLIDE 4

4/30

Structure of the Presentation

Introduction The Curry-Howard-Lambek Correspondence The λ-Calculus Computational Complexity Micro-Step Evaluation Abstract Machines Crumbled Environments A(dministrative) Normal Form Crumbled Terms Crumbling The Crumble GLAM The Machine The Implementation Theorem Complexity Conclusions

slide-5
SLIDE 5

5/30

The Curry-Howard Correspondence

(ax) Γ, A ⊢ A Γ, A ⊢ B (→i) Γ ⊢ A → B Γ ⊢ A → B Γ ⊢ A (→e) Γ ⊢ B

slide-6
SLIDE 6

5/30

The Curry-Howard Correspondence

(ax) Γ, x : A ⊢ x : A Γ, x : A ⊢ t : B (→i) Γ ⊢ λx.t : A → B Γ ⊢ t : A → B Γ ⊢ u : A (→e) Γ ⊢ tu : B

slide-7
SLIDE 7

5/30

The Curry-Howard Correspondence

(ax) Γ, x : A ⊢ x : A Γ, x : A ⊢ t : B (→i) Γ ⊢ λx.t : A → B Γ ⊢ t : A → B Γ ⊢ u : A (→e) Γ ⊢ tu : B ◮ Curry-Howard Correspondence: relationship between mathematical proofs and computer programs ◮ Curry-Howard-Lambek: cartesian closed categories

slide-8
SLIDE 8

6/30

The λ-Calculus

◮ At the foundations of OCaml, Haskell, . . .

Syntax

Terms t, u, s ::= x | λx.t | t u Examples λx.x (identity) λx.λy.x (1st projection)

slide-9
SLIDE 9

6/30

The λ-Calculus

◮ At the foundations of OCaml, Haskell, . . .

Syntax

Terms t, u, s ::= x | λx.t | t u Examples λx.x (identity) λx.λy.x (1st projection)

Evaluation

(Top Level) Beta Rule (λx.t) u →β t{xu} Example (λx.λy.x)t u →β (λy.t)u →β t. ◮ Reduction →β is the structural closure of →β ◮ Evaluation = reducing a λ-term t to its normal form nf(t)

slide-10
SLIDE 10

7/30

Computational Complexity in the λ-calculus

◮ Universal model of computation like Turing machines, Finite state machines, Petri nets, . . . ◮ Church-Turing thesis: computable functions ◮ Computational complexity: classify computational problems according to their inherent difficulty ⇒ How to measure the computational complexity of a λ-term? ◮ Slot and van Emde Boas’ weak invariance thesis: reasonable machines can simulate each other within a polynomial

  • verhead in time

⇒ Is λ-calculus a reasonable machine?

slide-11
SLIDE 11

8/30

Size explosion

We define the family {tn}n of λ-terms: t0 := I = λx.x tn+1 := λx.tn (x x)

slide-12
SLIDE 12

8/30

Size explosion

We define the family {tn}n of λ-terms: t0 := I = λx.x tn+1 := λx.tn (x x) ◮ t0 = nf(t0) = λx.x (0 steps) ◮ t1 = λx.t0 (x x) = λx.I (x x) →β λx.x x (1 steps) ◮ tn+1 = λx.tn (x x) →β λx.(λx.x x)(x x) →β λx.(x x)(x x)

slide-13
SLIDE 13

8/30

Size explosion

We define the family {tn}n of λ-terms: t0 := I = λx.x tn+1 := λx.tn (x x) ◮ t0 = nf(t0) = λx.x (0 steps) ◮ t1 = λx.t0 (x x) = λx.I (x x) →β λx.x x (1 steps) ◮ tn+1 = λx.tn (x x) →β λx.(λx.x x)(x x) →β λx.(x x)(x x) Note that: ◮ |tn| = O(n) ◮ tn →n

β nf(tn)

◮ | nf(tn)| = Ω(2n)

slide-14
SLIDE 14

8/30

Size explosion

We define the family {tn}n of λ-terms: t0 := I = λx.x tn+1 := λx.tn (x x) ◮ t0 = nf(t0) = λx.x (0 steps) ◮ t1 = λx.t0 (x x) = λx.I (x x) →β λx.x x (1 steps) ◮ tn+1 = λx.tn (x x) →β λx.(λx.x x)(x x) →β λx.(x x)(x x) Note that: ◮ |tn| = O(n) ◮ tn →n

β nf(tn)

◮ | nf(tn)| = Ω(2n) ⇒ Evaluation looks exponential in n and |t|

slide-15
SLIDE 15

9/30

Abstract Machines & Complexity

◮ Abstract machines = conceptual models of execution of λ-terms ◮ Enable a quantitative analysis of evaluation:

◮ Bound the asymptotic overhead as a function of the size of the initial term (|t|) and the number of β-steps (n) ◮ Study the efficiency of abstract machines from a complexity point of view.

◮ First result: Accattoli, Dal Lago (2014) (Leftmost-Outermost) β-Reduction is Invariant, Indeed Efficient machines can reduce λ-terms with only bi-linear

  • verhead, i.e. in time O(|t| · n).
slide-16
SLIDE 16

10/30

Micro-Step Evaluation

Computer tools based on the λ-calculus do not implement β-reduction literally. ◮ The substitution t{xv} performed when firing a redex is not an atomic operation. ◮ If performed without restrictions, it leads to size-explosion (exponential overhead). ◮ Abstract machines employ micro-step evaluation: ◮ The substitutions due to β-steps are delayed and represented as new entries in an environment, and substitution is decomposed as to act on one variable occurrence at a time.

slide-17
SLIDE 17

11/30

(Global) Environment

Environment: the data structure that stores the delayed substitutions of the previously reduced beta-redexes: term environment (λx.t) u e . . . . . .

slide-18
SLIDE 18

11/30

(Global) Environment

Environment: the data structure that stores the delayed substitutions of the previously reduced beta-redexes: term environment (λx.t) u e t [xu]e . . . . . .

slide-19
SLIDE 19

11/30

(Global) Environment

Environment: the data structure that stores the delayed substitutions of the previously reduced beta-redexes: term environment (λx.t) u e t [xu]e . . . . . . Controlled, linear substitution: term environment x [xu]e . . . . . .

slide-20
SLIDE 20

11/30

(Global) Environment

Environment: the data structure that stores the delayed substitutions of the previously reduced beta-redexes: term environment (λx.t) u e t [xu]e . . . . . . Controlled, linear substitution: term environment x [xu]e u [xu]e . . . . . .

slide-21
SLIDE 21

12/30

Dump

In abstract machines: search for redexes = “commutative” transition steps.

Example

The CbV strategy forces the abstract machine to reduce the argument before firing a β-redex. The dump stores the terms to be evaluated next: dump term env (λx.t) u e . . . . . . . . .

slide-22
SLIDE 22

12/30

Dump

In abstract machines: search for redexes = “commutative” transition steps.

Example

The CbV strategy forces the abstract machine to reduce the argument before firing a β-redex. The dump stores the terms to be evaluated next: dump term env (λx.t) u e λx.t u e . . . . . . . . .

slide-23
SLIDE 23

13/30

Applicative Stack

The applicative stack stores the values that correspond to already evaluated arguments: dump term stack env s u e

slide-24
SLIDE 24

13/30

Applicative Stack

The applicative stack stores the values that correspond to already evaluated arguments: dump term stack env s u e s u e

slide-25
SLIDE 25

13/30

Applicative Stack

The applicative stack stores the values that correspond to already evaluated arguments: dump term stack env s u e s u e . . . . . . . . . s v e′

slide-26
SLIDE 26

13/30

Applicative Stack

The applicative stack stores the values that correspond to already evaluated arguments: dump term stack env s u e s u e . . . . . . . . . s v e′

  • s

v e′

slide-27
SLIDE 27

13/30

Applicative Stack

The applicative stack stores the values that correspond to already evaluated arguments: dump term stack env s u e s u e . . . . . . . . . s v e′

  • s

v e′ . . . . . . . . . λx.t v e′′

slide-28
SLIDE 28

13/30

Applicative Stack

The applicative stack stores the values that correspond to already evaluated arguments: dump term stack env s u e s u e . . . . . . . . . s v e′

  • s

v e′ . . . . . . . . . λx.t v e′′ t

  • [xv]e′′

. . . . . .

slide-29
SLIDE 29

14/30

The Open GLAM

◮ Open terms ◮ Global environments ◮ LAM = Leroy Abstract Machine

Dump Code Stack Global Env Dump Code Stack Global Env D tu π E c1 D :t♦π u ǫ E D :t♦π λx.u ǫ E c2 D t λx.u@ǫ : π E D :t♦π x π′ E c3 D t x@π′ : π E if E(x) = ⊥ D λx.t φ:π E m D t π [xφ]E D x π E[xφ]E ′ e D φα π E[xφ]E ′

⇒ Exponential overhead

slide-30
SLIDE 30

15/30

The Easy GLAMOUr

Optimization: only substitute abstractions, because inert terms do not create new redexes!

Dump Code Stack Global Env Dump Code Stack Global Env D tu π E c1 D :t♦π u ǫ E D :t♦π λx.u ǫ E c2 D t λx.u@ǫ : π E D :t♦π x π′ E c3 D t x@π′ : π E if E(x) = ⊥ or E(x) = y@π′′ D λx.t φ:π E m D t π [xφ]E D x π E[xλy.u@ǫ]E ′ e D λy.uα π E[xλy.u@ǫ]E ′

slide-31
SLIDE 31

16/30

Recap: Some Issues

◮ Efficient machines for evaluating closed λ-terms (relevant for programming languages) require several optimizations to handle efficiently the evaluation of open terms (needed to implement proof assistants). ⇒ Crumble Machines scale easily to the open case ◮ The implementation of proof assistants requires also strong reductions, i.e. evaluating terms under abstractions. This makes more intricate the search for redexes, exasperating the proliferation of auxiliary machine data structures. ⇒ Crumble Machines only use environments

slide-32
SLIDE 32

17/30

Structure of the Presentation

Introduction The Curry-Howard-Lambek Correspondence The λ-Calculus Computational Complexity Micro-Step Evaluation Abstract Machines Crumbled Environments A(dministrative) Normal Form Crumbled Terms Crumbling The Crumble GLAM The Machine The Implementation Theorem Complexity Conclusions

slide-33
SLIDE 33

18/30

A(dministrative) Normal Form

◮ Historically, the transformation to ANF has been introduced via a detailed and involved analysis of abstract machines working on CPS terms. ◮ Idea: decomposing applications ◮ Terms with iterated application like (tu)s or t(us) are then represented with environment entries (aka explicit substitutions), as: xs, [xtu] and tx, [xus] ◮ If the transformation is applied recursively on t, u, and s, then all applications have the form vv′, i.e. they only involve values (value = variable or application).

slide-34
SLIDE 34

19/30

Crumbled Terms

Crumbles c ::= (t, e) Crumbled Terms t, u, s ::= v | v v Values v ::= x | λx.c Environments e ::= ǫ | e[xt] ◮ The body of abstractions is itself a crumble, because the crumbling transforms the terms also under abstractions. ◮ Crumbles are not closures!

slide-35
SLIDE 35

20/30

The Crumbling Transformation

Translation t : λ-terms → crumbles. Auxiliary translation v : λ-values → crumbled values: x := x λx.t := λx.t v := (v, ǫ) vv′ := (vv′, ǫ) tv := (xv, [xs]e) if t is not a value and t = (s, e), with x fresh tu := tx @ ([xs]e) if u is not a value and u = (s, e), with x fresh

Example

I I (I I) I = (x I, [xy z][yI I][zI I]) where I = λx.(x, ǫ).

slide-36
SLIDE 36

21/30

Evaluation: the Intuition

Intuition: search for redex = right-to-left visit of the environment I I (I I) I = (x I, [xy z][yI I][zI I]) → (x I, [xy z][yI I][zx′][x′I]) → (x I, [xy z][yI I][zI][x′I]) → (x I, [xy z][yx′′][x′′I][zI][x′I]) → (x I, [xy z][yI][x′′I][zI][x′I]) → · · ·

slide-37
SLIDE 37

22/30

slide-38
SLIDE 38

23/30

Structure of the Presentation

Introduction The Curry-Howard-Lambek Correspondence The λ-Calculus Computational Complexity Micro-Step Evaluation Abstract Machines Crumbled Environments A(dministrative) Normal Form Crumbled Terms Crumbling The Crumble GLAM The Machine The Implementation Theorem Complexity Conclusions

slide-39
SLIDE 39

24/30

The Crumble GLAM - Syntax

◮ The machine works directly on crumbled environments of the form: e | e ◮ We use a marker “|” to separate the unevaluated from the evaluated parts of the environment.

slide-40
SLIDE 40

25/30

The Pointed Crumble GLAM - Evaluation

e[x(λy.c) v] | e →m e[xc′][y′v] | e (i) e[yx] | e →evar e[ye(x)] | e (ii) e[yxv] | e →el e[ye(x)v] | e (ii) e[xt] | e →c e | [xt]e (iii) where

  • i. λy.c is α-renamed to λy′.c′ and c′ is plugged in e[x·]
  • ii. only if x ∈ dom(e)
  • iii. if none of the other rules is applicable, i.e. when t is an

abstraction or when t is x or xv but x is not defined in e

slide-41
SLIDE 41

26/30

Recall: The “Easy” GLAMOUr

Dump Code Stack Global Env Dump Code Stack Global Env D tu π E c1 D :t♦π u ǫ E D :t♦π λx.u ǫ E c2 D t λx.u@ǫ : π E D :t♦π x π′ E c3 D t x@π′ : π E if E(x) = ⊥ or E(x) = y@π′′ D λx.t φ:π E m D t π [xφ]E D x π E[xλy.u@ǫ]E ′ e D λy.uα π E[xλy.u@ǫ]E ′

slide-42
SLIDE 42

27/30

The Implementation Theorem

Claim: The Crumble GLAM implements CbV ◮ Let ·↓ the read back from crumbles to λ-terms. ◮ We call a crumble reachable if it can be obtained with an evaluation sequence starting from the · translation of a λ-terms.

Theorem (Implementation)

Let c be a reachable crumble.

  • 1. Initialization:

t↓ = t

  • 2. Projection:

if c →m c′ then c↓ →CbV c′

↓.

  • 3. Transparency:

if c →a c′ then c↓ = c′

↓ (for a ∈ {evar, el})

  • 4. Determinism:

→ is deterministic.

  • 5. Progress:

If c is normal then c↓ is →βv -normal.

slide-43
SLIDE 43

28/30

Complexity of the Crumble GLAM

In previous work, we showed that a cause for quadratic overhead were malicious chains of renamings = environments of the form [x1x2][x2x3] · · · [xnλy.c] substituting variables for variables and finally leading to an abstraction. The Crumble GLAM is not affected by this quadratic overhead because:

  • 1. it performs substitution steps →e while transversing the

environment from right to left, thus consuming the chain, and

  • 2. the multiplicative step →m may increase the length of these

chains by only a constant, which makes them harmless.

Theorem (Reasonable & Efficient)

For any machine run ρ: c0 →∗ c, the transitions in ρ cost all together O((|ρ|m + 1) · |c0|) on a RAM.

slide-44
SLIDE 44

29/30

Structure of the Presentation

Introduction The Curry-Howard-Lambek Correspondence The λ-Calculus Computational Complexity Micro-Step Evaluation Abstract Machines Crumbled Environments A(dministrative) Normal Form Crumbled Terms Crumbling The Crumble GLAM The Machine The Implementation Theorem Complexity Conclusions

slide-45
SLIDE 45

30/30

Conclusions

Design More complex representation of terms Less machine data structures Efficiency Search for next redex = scan the crumble from right to left As efficient as ordinary machines with global environments Scalability Scales up seamlessly from the closed case to the more delicate case of open terms Next step: simpler and efficient machines for strong evaluation