The Safe -Calculus William Blum Joint work with C.-H. Luke Ong - - PowerPoint PPT Presentation

the safe calculus
SMART_READER_LITE
LIVE PREVIEW

The Safe -Calculus William Blum Joint work with C.-H. Luke Ong - - PowerPoint PPT Presentation

The Safe -Calculus William Blum Joint work with C.-H. Luke Ong Oxford University Computing Laboratory BCTCS, 25 April 2007 Overview Safety: a restriction for higher-order grammars. Transposed to the -calculus, it gives rise to


slide-1
SLIDE 1

The Safe λ-Calculus

William Blum Joint work with C.-H. Luke Ong

Oxford University Computing Laboratory

BCTCS, 2–5 April 2007

slide-2
SLIDE 2

Overview

◮ Safety: a restriction for higher-order grammars. ◮ Transposed to the λ-calculus, it gives rise to the Safe λ-calculus. ◮ Safety has nice algorithmic properties, automata-theoretic and

game-semantic characterisations.

slide-3
SLIDE 3

What is the Safety Restriction?

◮ First appeared under the name “restriction of derived types” in

“IO and OI Hierarchies” by W. Damm, TCS 1982

◮ It is a syntactic restriction for higher-order grammars that

constrains the occurrences of the variables in the grammar equations according to their orders.

Theorem (Knapik, Niwi´ nski and Urzyczyn (2001,2002))

  • 1. The Monadic Second Order (MSO) model checking problem for trees

generated by safe higher-order grammars of any order is decidable.

  • 2. Automata-theoretic characterisation: Safe grammars of order n are as

expressive as pushdown automata of order n.

◮ Aehlig, de Miranda, Ong (2004) introduced the Safe λ-calculus.

slide-4
SLIDE 4

What is the Safety Restriction?

◮ First appeared under the name “restriction of derived types” in

“IO and OI Hierarchies” by W. Damm, TCS 1982

◮ It is a syntactic restriction for higher-order grammars that

constrains the occurrences of the variables in the grammar equations according to their orders.

Theorem (Knapik, Niwi´ nski and Urzyczyn (2001,2002))

  • 1. The Monadic Second Order (MSO) model checking problem for trees

generated by safe higher-order grammars of any order is decidable.

  • 2. Automata-theoretic characterisation: Safe grammars of order n are as

expressive as pushdown automata of order n.

◮ Aehlig, de Miranda, Ong (2004) introduced the Safe λ-calculus.

slide-5
SLIDE 5

What is the Safety Restriction?

◮ First appeared under the name “restriction of derived types” in

“IO and OI Hierarchies” by W. Damm, TCS 1982

◮ It is a syntactic restriction for higher-order grammars that

constrains the occurrences of the variables in the grammar equations according to their orders.

Theorem (Knapik, Niwi´ nski and Urzyczyn (2001,2002))

  • 1. The Monadic Second Order (MSO) model checking problem for trees

generated by safe higher-order grammars of any order is decidable.

  • 2. Automata-theoretic characterisation: Safe grammars of order n are as

expressive as pushdown automata of order n.

◮ Aehlig, de Miranda, Ong (2004) introduced the Safe λ-calculus.

slide-6
SLIDE 6

Simply Typed λ-Calculus

◮ Simple types A := o | A → A. ◮ The order of a type is given by order(o) = 0,

  • rder(A → B) = max(order(A) + 1, order(B)).

◮ Jugdements of the form Γ ⊢ M : T where Γ is the context, M is

the term and T is the type: (var) x : A ⊢ x : A (wk) Γ ⊢ M : A ∆ ⊢ M : A Γ ⊂ ∆ (app) Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ MN : B (abs) Γ, x : A ⊢ M : B Γ ⊢ λxA.M : A → B

◮ Example: f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) ◮ A single rule: β-reduction. e.g. (λx.M)N →β M[N/x]

slide-7
SLIDE 7

Simply Typed λ-Calculus

◮ Simple types A := o | A → A. ◮ The order of a type is given by order(o) = 0,

  • rder(A → B) = max(order(A) + 1, order(B)).

◮ Jugdements of the form Γ ⊢ M : T where Γ is the context, M is

the term and T is the type: (var) x : A ⊢ x : A (wk) Γ ⊢ M : A ∆ ⊢ M : A Γ ⊂ ∆ (app) Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ MN : B (abs) Γ, x : A ⊢ M : B Γ ⊢ λxA.M : A → B

◮ Example: f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) ◮ A single rule: β-reduction. e.g. (λx.M)N →β M[N/x]

slide-8
SLIDE 8

Simply Typed λ-Calculus

◮ Simple types A := o | A → A. ◮ The order of a type is given by order(o) = 0,

  • rder(A → B) = max(order(A) + 1, order(B)).

◮ Jugdements of the form Γ ⊢ M : T where Γ is the context, M is

the term and T is the type: (var) x : A ⊢ x : A (wk) Γ ⊢ M : A ∆ ⊢ M : A Γ ⊂ ∆ (app) Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ MN : B (abs) Γ, x : A ⊢ M : B Γ ⊢ λxA.M : A → B

◮ Example: f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) ◮ A single rule: β-reduction. e.g. (λx.M)N →β M[N/x]

slide-9
SLIDE 9

Simply Typed λ-Calculus

◮ Simple types A := o | A → A. ◮ The order of a type is given by order(o) = 0,

  • rder(A → B) = max(order(A) + 1, order(B)).

◮ Jugdements of the form Γ ⊢ M : T where Γ is the context, M is

the term and T is the type: (var) x : A ⊢ x : A (wk) Γ ⊢ M : A ∆ ⊢ M : A Γ ⊂ ∆ (app) Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ MN : B (abs) Γ, x : A ⊢ M : B Γ ⊢ λxA.M : A → B

◮ Example: f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) ◮ A single rule: β-reduction. e.g. (λx.M)N →β M[N/x]

slide-10
SLIDE 10

Simply Typed λ-Calculus

◮ Simple types A := o | A → A. ◮ The order of a type is given by order(o) = 0,

  • rder(A → B) = max(order(A) + 1, order(B)).

◮ Jugdements of the form Γ ⊢ M : T where Γ is the context, M is

the term and T is the type: (var) x : A ⊢ x : A (wk) Γ ⊢ M : A ∆ ⊢ M : A Γ ⊂ ∆ (app) Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ MN : B (abs) Γ, x : A ⊢ M : B Γ ⊢ λxA.M : A → B

◮ Example: f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) ◮ A single rule: β-reduction. e.g. (λx.M)N →β M[N/x]

slide-11
SLIDE 11

Variable Capture

The usual “problem” in λ-calculus: avoid variable capture when performing substitution: (λx.(λy.x))y →β (λy.x)[y/x] = λy.y

  • 1. Standard solution: Barendregt’s convention. Variables are

renamed so that free variables and bound variables have different names. Eg. (λx.(λy.x))y becomes (λx.(λz.x))y which reduces to (λz.x)[y/x] = λz.y Drawback: requires to have access to an unbounded supply of names to perform a given sequence of β-reductions.

  • 2. Another solution: switch to the λ-calculus `

a la de Brujin where variable binding is specified by an index instead of a name. Variable renaming then becomes unnecessary. Drawback: the conversion to nameless de Brujin λ-terms requires an unbounded supply of indices. Safety avoids the need for variable renaming!

slide-12
SLIDE 12

Variable Capture

The usual “problem” in λ-calculus: avoid variable capture when performing substitution: (λx.(λy.x))y →β (λy.x)[y/x] = λy.y

  • 1. Standard solution: Barendregt’s convention. Variables are

renamed so that free variables and bound variables have different names. Eg. (λx.(λy.x))y becomes (λx.(λz.x))y which reduces to (λz.x)[y/x] = λz.y Drawback: requires to have access to an unbounded supply of names to perform a given sequence of β-reductions.

  • 2. Another solution: switch to the λ-calculus `

a la de Brujin where variable binding is specified by an index instead of a name. Variable renaming then becomes unnecessary. Drawback: the conversion to nameless de Brujin λ-terms requires an unbounded supply of indices. Safety avoids the need for variable renaming!

slide-13
SLIDE 13

Variable Capture

The usual “problem” in λ-calculus: avoid variable capture when performing substitution: (λx.(λy.x))y →β (λy.x)[y/x] = λy.y

  • 1. Standard solution: Barendregt’s convention. Variables are

renamed so that free variables and bound variables have different names. Eg. (λx.(λy.x))y becomes (λx.(λz.x))y which reduces to (λz.x)[y/x] = λz.y Drawback: requires to have access to an unbounded supply of names to perform a given sequence of β-reductions.

  • 2. Another solution: switch to the λ-calculus `

a la de Brujin where variable binding is specified by an index instead of a name. Variable renaming then becomes unnecessary. Drawback: the conversion to nameless de Brujin λ-terms requires an unbounded supply of indices. Safety avoids the need for variable renaming!

slide-14
SLIDE 14

Variable Capture

The usual “problem” in λ-calculus: avoid variable capture when performing substitution: (λx.(λy.x))y →β (λy.x)[y/x] = λy.y

  • 1. Standard solution: Barendregt’s convention. Variables are

renamed so that free variables and bound variables have different names. Eg. (λx.(λy.x))y becomes (λx.(λz.x))y which reduces to (λz.x)[y/x] = λz.y Drawback: requires to have access to an unbounded supply of names to perform a given sequence of β-reductions.

  • 2. Another solution: switch to the λ-calculus `

a la de Brujin where variable binding is specified by an index instead of a name. Variable renaming then becomes unnecessary. Drawback: the conversion to nameless de Brujin λ-terms requires an unbounded supply of indices. Safety avoids the need for variable renaming!

slide-15
SLIDE 15

Variable Capture

The usual “problem” in λ-calculus: avoid variable capture when performing substitution: (λx.(λy.x))y →β (λy.x)[y/x] = λy.y

  • 1. Standard solution: Barendregt’s convention. Variables are

renamed so that free variables and bound variables have different names. Eg. (λx.(λy.x))y becomes (λx.(λz.x))y which reduces to (λz.x)[y/x] = λz.y Drawback: requires to have access to an unbounded supply of names to perform a given sequence of β-reductions.

  • 2. Another solution: switch to the λ-calculus `

a la de Brujin where variable binding is specified by an index instead of a name. Variable renaming then becomes unnecessary. Drawback: the conversion to nameless de Brujin λ-terms requires an unbounded supply of indices. Safety avoids the need for variable renaming!

slide-16
SLIDE 16

Variable Capture

The usual “problem” in λ-calculus: avoid variable capture when performing substitution: (λx.(λy.x))y →β (λy.x)[y/x] = λy.y

  • 1. Standard solution: Barendregt’s convention. Variables are

renamed so that free variables and bound variables have different names. Eg. (λx.(λy.x))y becomes (λx.(λz.x))y which reduces to (λz.x)[y/x] = λz.y Drawback: requires to have access to an unbounded supply of names to perform a given sequence of β-reductions.

  • 2. Another solution: switch to the λ-calculus `

a la de Brujin where variable binding is specified by an index instead of a name. Variable renaming then becomes unnecessary. Drawback: the conversion to nameless de Brujin λ-terms requires an unbounded supply of indices. Safety avoids the need for variable renaming!

slide-17
SLIDE 17

The Safe λ-Calculus

The formation rules

(var) x : A ⊢s x : A (wk) Γ ⊢s M : A ∆ ⊢s M : A Γ ⊂ ∆ (app) Γ ⊢ M : (A, . . . , Al, B) Γ ⊢s N1 : A1 . . . Γ ⊢s Nl : Al Γ ⊢s MN1 . . . Nl : B with the side-condition ∀y ∈ Γ : ord(y) ≥ ord(B) (abs) Γ, x1 : A1 . . . xn : An ⊢s M : B Γ ⊢s λx1 : A1 . . . xn : An.M : A1 → . . . → An → B with the side-condition ∀y ∈ Γ : ord(y) ≥ ord(A1 → . . . → An → B)

Property

In the Safe λ-calculus there is no need to rename variables when performing substitution.

slide-18
SLIDE 18

The Safe λ-Calculus

The formation rules

(var) x : A ⊢s x : A (wk) Γ ⊢s M : A ∆ ⊢s M : A Γ ⊂ ∆ (app) Γ ⊢ M : (A, . . . , Al, B) Γ ⊢s N1 : A1 . . . Γ ⊢s Nl : Al Γ ⊢s MN1 . . . Nl : B with the side-condition ∀y ∈ Γ : ord(y) ≥ ord(B) (abs) Γ, x1 : A1 . . . xn : An ⊢s M : B Γ ⊢s λx1 : A1 . . . xn : An.M : A1 → . . . → An → B with the side-condition ∀y ∈ Γ : ord(y) ≥ ord(A1 → . . . → An → B)

Property

In the Safe λ-calculus there is no need to rename variables when performing substitution.

slide-19
SLIDE 19

Examples

◮ Contracting the β-redex in the following term

f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) leads to variable capture: (λϕx.ϕ x)(f x) →β (λx.(f x)x). Hence the term is unsafe. Indeed, ord(x) = 0 ≤ 1 = ord(f x).

◮ The term (λϕo→oxo.ϕ x)(λyo.y) is safe.

slide-20
SLIDE 20

Examples

◮ Contracting the β-redex in the following term

f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) leads to variable capture: (λϕx.ϕ x)(f x) →β (λx.(f x)x). Hence the term is unsafe. Indeed, ord(x) = 0 ≤ 1 = ord(f x).

◮ The term (λϕo→oxo.ϕ x)(λyo.y) is safe.

slide-21
SLIDE 21

Examples

◮ Contracting the β-redex in the following term

f : o → o → o, x : o ⊢ (λϕo→oxo.ϕ x)(f x) leads to variable capture: (λϕx.ϕ x)(f x) →β (λx.(f x)x). Hence the term is unsafe. Indeed, ord(x) = 0 ≤ 1 = ord(f x).

◮ The term (λϕo→oxo.ϕ x)(λyo.y) is safe.

slide-22
SLIDE 22

Numerical functions

Church Encoding: for n ∈ N, n = λsz.snz of type I = (o → o) → o → o.

Theorem (Schwichtenberg 1976)

The numeric function representable by simply-typed terms of type I → . . . → I are exactly the multivariate polynomials extended with the conditional function: cond(t, x, y) = x, if t = 0 y, if t = n + 1 . cond can be represented by the unsafe term λFGHαx.H(λy.Gαx)(Fαx). In fact cond is not representable in the Safe λ-calculus:

Theorem

Functions representable by safe λ-expressions of type I → . . . → I are exactly the multivariate polynomials.

slide-23
SLIDE 23

Numerical functions

Church Encoding: for n ∈ N, n = λsz.snz of type I = (o → o) → o → o.

Theorem (Schwichtenberg 1976)

The numeric function representable by simply-typed terms of type I → . . . → I are exactly the multivariate polynomials extended with the conditional function: cond(t, x, y) = x, if t = 0 y, if t = n + 1 . cond can be represented by the unsafe term λFGHαx.H(λy.Gαx)(Fαx). In fact cond is not representable in the Safe λ-calculus:

Theorem

Functions representable by safe λ-expressions of type I → . . . → I are exactly the multivariate polynomials.

slide-24
SLIDE 24

Game Semantics

Let M : T be a pure simply typed term.

◮ Game-semantics provides a model of λ-calculus. M is denoted

by a strategy [ [M] ] on a game induced by T.

◮ A strategy is represented by a set of sequences of moves

together with links: each move points to a preceding move.

◮ Computation tree = canonical tree representation of a term. ◮ Traversals T rav(M) = sequences of nodes with links

respecting some formation rules.

The Correspondence Theorem

The game semantics of a term can be represented on the computation tree: T rav(M) ∼ = M

  • Reduction(T rav(M)) ∼

= [ [M] ] where M is the revealed game-semantic denotion (i.e. internal moves are uncovered).

slide-25
SLIDE 25

Game Semantics

Let M : T be a pure simply typed term.

◮ Game-semantics provides a model of λ-calculus. M is denoted

by a strategy [ [M] ] on a game induced by T.

◮ A strategy is represented by a set of sequences of moves

together with links: each move points to a preceding move.

◮ Computation tree = canonical tree representation of a term. ◮ Traversals T rav(M) = sequences of nodes with links

respecting some formation rules.

The Correspondence Theorem

The game semantics of a term can be represented on the computation tree: T rav(M) ∼ = M

  • Reduction(T rav(M)) ∼

= [ [M] ] where M is the revealed game-semantic denotion (i.e. internal moves are uncovered).

slide-26
SLIDE 26

Game Semantics

Let M : T be a pure simply typed term.

◮ Game-semantics provides a model of λ-calculus. M is denoted

by a strategy [ [M] ] on a game induced by T.

◮ A strategy is represented by a set of sequences of moves

together with links: each move points to a preceding move.

◮ Computation tree = canonical tree representation of a term. ◮ Traversals T rav(M) = sequences of nodes with links

respecting some formation rules.

The Correspondence Theorem

The game semantics of a term can be represented on the computation tree: T rav(M) ∼ = M

  • Reduction(T rav(M)) ∼

= [ [M] ] where M is the revealed game-semantic denotion (i.e. internal moves are uncovered).

slide-27
SLIDE 27

Game-semantic Characterisation of Safety

◮ Computation tree of safe terms are incrementally-bound : each

variable x is bound by the first λ-node occurring in the path to the root with order > ord(x).

◮ Using the Correspondence Theorem we can show:

Proposition

Safe terms are denoted by P-incrementally justified strategies: each P-move m points to the last O-move in the P-view with order > ord(m).

Corollary

Justification pointers attached to P-moves are redundant in the game-semantics of safe terms.

slide-28
SLIDE 28

Game-semantic Characterisation of Safety

◮ Computation tree of safe terms are incrementally-bound : each

variable x is bound by the first λ-node occurring in the path to the root with order > ord(x).

◮ Using the Correspondence Theorem we can show:

Proposition

Safe terms are denoted by P-incrementally justified strategies: each P-move m points to the last O-move in the P-view with order > ord(m).

Corollary

Justification pointers attached to P-moves are redundant in the game-semantics of safe terms.

slide-29
SLIDE 29

Game-semantic Characterisation of Safety

◮ Computation tree of safe terms are incrementally-bound : each

variable x is bound by the first λ-node occurring in the path to the root with order > ord(x).

◮ Using the Correspondence Theorem we can show:

Proposition

Safe terms are denoted by P-incrementally justified strategies: each P-move m points to the last O-move in the P-view with order > ord(m).

Corollary

Justification pointers attached to P-moves are redundant in the game-semantics of safe terms.

slide-30
SLIDE 30

Conclusion and Future Works

Conclusion: Safety is a syntactic constraint with nice algorithmic and game-semantic properties. Future works:

◮ A categorical model of Safe PCF. ◮ Complexity classes characterised with the Safe λ-calculus? ◮ Safe Idealized Algol: is contextual equivalence decidable?

Related works:

◮ Jolie G. de Miranda’s thesis on unsafe grammars. ◮ Ong introduced computation trees in LICS2006 to prove

decidability of MSO theory on infinite trees generated by higher-order grammars (whether safe or not).