Type-Based Synthesis and the Inhabitation Problem

Lecture 1: Typed λ-Calculus and Combinatory Logic

Jakob Rehof TU Dortmund University, Germany

The Second IPM Advanced School on Computing Theory and Practice of Programming Languages 27-31 August 2017 Tehran, Iran

August 30, 2017

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 1 / 38 λ-Calculus

Untyped, pure λ-calculus

H.P. Barendregt: The . Its Syntax and Semantics, Studies in Logic and the Foundations of Mathematics, 2nd Edition. Elsevier Science Publishers 1984. [Bar84] Hindley and Seldin: Lambda-Calculus and Combinators, an Introduction, Cambridge University Press 2008. [HS08]

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 2 / 38 Assuming computational expressions such as x + 1 the operation of λ-abstraction constructs the expression λx.x + 1. An abstraction λx.M can be read as: “the function of x returning M(x)”. Functional expressions are anonymous. Mathematicians sometimes use notation like x 7→ x + 1, and that corresponds to λx.x + 1. In addition to the operation of abstraction there is a (dual) operation of application: If M and N are λ-expressions, then the application of M to N, written (MN), is also a λ-expression. Computation arises when abstractions are applied to arguments, for example ((λx.x + 1) 2). An expression of the form ((λx.M) N) is called a redex. Computation means substitution of arguments for the λ-abstracted variable of the function operator in a redex. For example:

((λx.x + 1) 2) →β (x + 1)[x := 2] ≡ 2 + 1

λ-Calculus in One Slide

λ-calculus is a minimalistic formalism for defining functional expressions and computations with such (a minimalistic language):

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 3 / 38 An abstraction λx.M can be read as: “the function of x returning M(x)”. Functional expressions are anonymous. Mathematicians sometimes use notation like x 7→ x + 1, and that corresponds to λx.x + 1. In addition to the operation of abstraction there is a (dual) operation of application: If M and N are λ-expressions, then the application of M to N, written (MN), is also a λ-expression. Computation arises when abstractions are applied to arguments, for example ((λx.x + 1) 2). An expression of the form ((λx.M) N) is called a redex. Computation means substitution of arguments for the λ-abstracted variable of the function operator in a redex. For example:

((λx.x + 1) 2) →β (x + 1)[x := 2] ≡ 2 + 1

λ-Calculus in One Slide

λ-calculus is a minimalistic formalism for defining functional expressions and computations with such (a minimalistic functional programming language): Assuming computational expressions such as x + 1 the operation of λ-abstraction constructs the expression λx.x + 1.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 3 / 38 Functional expressions are anonymous. Mathematicians sometimes use notation like x 7→ x + 1, and that corresponds to λx.x + 1. In addition to the operation of abstraction there is a (dual) operation of application: If M and N are λ-expressions, then the application of M to N, written (MN), is also a λ-expression. Computation arises when abstractions are applied to arguments, for example ((λx.x + 1) 2). An expression of the form ((λx.M) N) is called a redex. Computation means substitution of arguments for the λ-abstracted variable of the function operator in a redex. For example:

((λx.x + 1) 2) →β (x + 1)[x := 2] ≡ 2 + 1

λ-Calculus in One Slide

λ-calculus is a minimalistic formalism for defining functional expressions and computations with such (a minimalistic functional programming language): Assuming computational expressions such as x + 1 the operation of λ-abstraction constructs the expression λx.x + 1. An abstraction λx.M can be read as: “the function of x returning M(x)”.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 3 / 38 In addition to the operation of abstraction there is a (dual) operation of application: If M and N are λ-expressions, then the application of M to N, written (MN), is also a λ-expression. Computation arises when abstractions are applied to arguments, for example ((λx.x + 1) 2). An expression of the form ((λx.M) N) is called a redex. Computation means substitution of arguments for the λ-abstracted variable of the function operator in a redex. For example:

((λx.x + 1) 2) →β (x + 1)[x := 2] ≡ 2 + 1

λ-Calculus in One Slide

λ-calculus is a minimalistic formalism for defining functional expressions and computations with such (a minimalistic functional programming language): Assuming computational expressions such as x + 1 the operation of λ-abstraction constructs the expression λx.x + 1. An abstraction λx.M can be read as: “the function of x returning M(x)”. Functional expressions are anonymous. Mathematicians sometimes use notation like x 7→ x + 1, and that corresponds to λx.x + 1.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 3 / 38 Computation arises when abstractions are applied to arguments, for example ((λx.x + 1) 2). An expression of the form ((λx.M) N) is called a redex. Computation means substitution of arguments for the λ-abstracted variable of the function operator in a redex. For example:

((λx.x + 1) 2) →β (x + 1)[x := 2] ≡ 2 + 1

λ-Calculus in One Slide

λ-calculus is a minimalistic formalism for defining functional expressions and computations with such (a minimalistic functional programming language): Assuming computational expressions such as x + 1 the operation of λ-abstraction constructs the expression λx.x + 1. An abstraction λx.M can be read as: “the function of x returning M(x)”. Functional expressions are anonymous. Mathematicians sometimes use notation like x 7→ x + 1, and that corresponds to λx.x + 1. In addition to the operation of abstraction there is a (dual) operation of application: If M and N are λ-expressions, then the application of M to N, written (MN), is also a λ-expression.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 3 / 38 Computation means substitution of arguments for the λ-abstracted variable of the function operator in a redex. For example:

((λx.x + 1) 2) →β (x + 1)[x := 2] ≡ 2 + 1

λ-Calculus in One Slide

λ-calculus is a minimalistic formalism for defining functional expressions and computations with such (a minimalistic functional programming language): Assuming computational expressions such as x + 1 the operation of λ-abstraction constructs the expression λx.x + 1. An abstraction λx.M can be read as: “the function of x returning M(x)”. Functional expressions are anonymous. Mathematicians sometimes use notation like x 7→ x + 1, and that corresponds to λx.x + 1. In addition to the operation of abstraction there is a (dual) operation of application: If M and N are λ-expressions, then the application of M to N, written (MN), is also a λ-expression. Computation arises when abstractions are applied to arguments, for example ((λx.x + 1) 2). An expression of the form ((λx.M) N) is called a redex.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 3 / 38 λ-Calculus in One Slide

λ-calculus is a minimalistic formalism for defining functional expressions and computations with such (a minimalistic functional programming language): Assuming computational expressions such as x + 1 the operation of λ-abstraction constructs the expression λx.x + 1. An abstraction λx.M can be read as: “the function of x returning M(x)”. Functional expressions are anonymous. Mathematicians sometimes use notation like x 7→ x + 1, and that corresponds to λx.x + 1. In addition to the operation of abstraction there is a (dual) operation of application: If M and N are λ-expressions, then the application of M to N, written (MN), is also a λ-expression. Computation arises when abstractions are applied to arguments, for example ((λx.x + 1) 2). An expression of the form ((λx.M) N) is called a redex. Computation means substitution of arguments for the λ-abstracted variable of the function operator in a redex. For example:

((λx.x + 1) 2) →β (x + 1)[x := 2] ≡ 2 + 1

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 3 / 38 λ-terms

Let V denote a denumerable set of λ-variables, v1, v2, v3,....

Let x, y, z, . . . denote a denumerable set of metavariables ranging over V.

Definition 1

The set Λ of λ-terms, ranged over by M, N, P, Q, . . ., is defined inductively by:

x ∈ V ⇒ x ∈ Λ (variables) M,N ∈ Λ ⇒ (MN) ∈ Λ (application) M ∈ Λ, x ∈ V ⇒ (λxM) ∈ Λ (abstraction)

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 4 / 38 λ-terms

Notation 1

We may abbreviate (((FM1)M2) ··· Mn) by

FM1M2 ··· Mn for n ≥ 0.

We may abbreviate (λx1(λx2(··· (λxnM) ··· ))) by

λx1.λx2. ··· λxn.M for n ≥ 0.

And further, λx1.λx2. ··· λxn.M by λx1x2. ··· xn.M.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 5 / 38 λ-terms

Definition 2 (Free and bound variables)

The set of free variables of M, denoted FV(M), is defined by induction on M: FV(x) = {x} FV(MN) = FV(M) ∪ FV(N) FV(λx.M) = FV(M) \{x} A variable x ∈ FV(M) is called bound in λx.M (by that λ). A term M ∈ Λ is called closed, if FV(M) = ∅.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 6 / 38 λ-terms

Definition 3 (Syntactic identity)

We write M ≡ N to denote that M and N are the same term under renaming of bound variables (α-conversion). For example,

(λx.x)z ≡ (λx.x)z (λx.x)z ≡ (λy.y)z (λx.x)x ≡ (λy.y)x (λx.x)z 6≡ (λx.y)z (provided x 6≡ y)

Note that x ≡ y is possible, unless we have stipulated that x 6≡ y.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 7 / 38 λ-terms

Definition 4 (Substitution)

Let M[x := N] denote the result of substituting N for the free occurrences of x in M, defined by induction on M:

x[x := N] ≡ N y[x := N] ≡ y, if x 6≡ y (PQ)[x := N] ≡ (P [x := N]Q[x := N]) (λy.P )[x := N] ≡ λy.P [x := N], if x 6≡ y (λx.P )[x := N] ≡ λx.P

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 8 / 38 λ-terms

Lemma 5 (Substitution lemma)

Assume x 6≡ y and x 6∈ FV(P ).Then

M[x := N][y := P ] ≡ M[y := P ][x := N[y := P ]]

Exercise 1

Prove Lemma 5 by induction on M.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 9 / 38 Notion of reduction

Definition 6

A notion of reduction on a set D is a binary relation £ ⊆ D × D.

Definition 7 (Notion of β-reduction)

The notion of β-reduction is the relation £β ⊆ Λ × Λ defined by

(λx.M)N £β M[x := N]

for all M,N ∈ Λ. An expression of the form (λx.M)N is called a redex.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 10 / 38 Contexts

Contexts C over Λ are expressions of the form

C ::= [] | (CM) | (MC) | λx.C

We think of [] as a hole and of C as a term with a hole in it.

If C is a context and M a term then C[M] is the term which arises from exchanging [] with M (“hole-filling”).

For example, if C ≡ λx.λy.y[], then

C[λz.(xz)] ≡ λx.λy.y(λz.(xz))

Note that variable capture can happen through hole-filling.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 11 / 38 β-reduction

Definition 8

A notion of reduction £ ⊆ Λ × Λ satisfying

∀C.M £ N ⇒ C[M] £ C[N] is called a reduction relation. The smallest reduction relation containing £ is called the reduction relation induced by £ (sometimes also called the compatible closure of £).

Definition 9 (β-reduction)

The relation →β (β-reduction) is the reduction relation induced by £β .

If M →β N, then N is called a reduct of M.

A normal form is a term with no redex.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 12 / 38 β-reduction

Definition 10 (Multi-step β-reduction)

The relation β is the reflexive transitive closure of →β.

Definition 11 (β-conversion)

The relation =β (β-conversion) is the reflexive symmetric transitive closure of →β.

If M =β N, we say that M and N are β-equivalent.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 13 / 38 λ-calculus combinators

A λ-calculus combinator is a closed λ-term.

Some interesting combinators are:

I ≡ λx.x, K ≡ λxy.x, K∗ ≡ λxy.y, S ≡ λxyz.(xz)(yz)

ω ≡ λx.xx, Ω ≡ ωω

Y ≡ λf.(λx.f(xx))(λx.f(xx)), Θ ≡ (λx.λy.(y(xxy)))(λx.λy.(y(xxy)))

Exercise 2

Show that, for all terms F , we have YF =β F (YF ) and ΘF β F (ΘF ).

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 14 / 38 Confluence

Theorem 12 (Church-Rosser, confluence)

For every M,P1,P2 ∈ Λ, if M β P1 and M β P2, then there exists Q ∈ Λ such that P1 β Q and P2 β Q.

Corollary 13 (Uniqueness of normal forms)

If N1 and N2 are normal forms such that M β N1 and M β N2, then N1 ≡ N2. If P =β Q, then P and Q have the same normal form if any.

Exercise 3

Prove Corollary 13, by using Theorem 12.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 15 / 38 Church numerals

For P,M ∈ Λ, define P n(M) by induction on n ≥ 0:

P 0(M) ≡ M P n+1(M) ≡ P (P n(M))

n The Church numerals c0, c1, c2,... are defined by cn ≡ λf.λx.f (x).

Define A+ ≡ λxypq.xp(ypq), A× ≡ λxyz.x(yz), Ae ≡ λxy.yx. Then one can prove:

A+cncm =β cn+m,

A×cncm =β cn×m,

Aecncm =β cnm (m > 0).

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 16 / 38 Turing completeness

k A numerical function f : N → N is called λ-definable if there exists a combinator Pf such that

Pf cn1 ... cnk =β cf(n1,...,nk)

Theorem 14 (Kleene)

Every recursive function is λ-definable.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 17 / 38 Typed λ-Calculus

Some general references:

Barendregt: Lambda Calculus with Types, Handbook of Logic in , vol. 2, Oxford University Press 1993. [Bar93] Hindley and Seldin: Lambda-Calculus and Combinators, an Introduction, Cambridge University Press 2008. [HS08] Barendregt, Dekkers, Statman: Lambda Calculus with Types. Perspectives in Logic. Cambridge University Press 2013. [BDS13] Sørensen and Urzyczyn: Lectures on the Curry-Howard Isomorphism. Studies in Logic and the Foundations of Mathematics, 149, Elsevier 2006. [SU06] Nederpelt and Geuvers: and Formal Proof. An Introduction, Cambridge University Press 2014. [NG14] (higher, dependent and inductive, types).

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 18 / 38 There are function types τ → σ, whenever τ and σ are types. We write Γ ` M : τ to express that λ-term M has type τ under the type assumptions for the free variables in M given by Γ In many programming languages we write down types for functions like this:

int strlen(string x){ M }

In type theory we write ` M : string → int

The type system is a deductive system for deriving (`) well-typings of such a form. Type assumptions are needed:

{strlen : string → int, s : string} ` strlen(s) : int

Simple types in one slide

There are atomic types (type constants such as int, bool, real etc.) and type variables α, β, γ etc.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 19 / 38 We write Γ ` M : τ to express that λ-term M has type τ under the type assumptions for the free variables in M given by Γ In many programming languages we write down types for functions like this:

int strlen(string x){ M }

In type theory we write ` M : string → int

The type system is a deductive system for deriving (`) well-typings of such a form. Type assumptions are needed:

{strlen : string → int, s : string} ` strlen(s) : int

Simple types in one slide

There are atomic types (type constants such as int, bool, real etc.) and type variables α, β, γ etc. There are function types τ → σ, whenever τ and σ are types.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 19 / 38 In many programming languages we write down types for functions like this:

int strlen(string x){ M }

In type theory we write ` M : string → int

The type system is a deductive system for deriving (`) well-typings of such a form. Type assumptions are needed:

{strlen : string → int, s : string} ` strlen(s) : int

Simple types in one slide

There are atomic types (type constants such as int, bool, real etc.) and type variables α, β, γ etc. There are function types τ → σ, whenever τ and σ are types. We write Γ ` M : τ to express that λ-term M has type τ under the type assumptions for the free variables in M given by Γ

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 19 / 38 In type theory we write ` M : string → int

The type system is a deductive system for deriving (`) well-typings of such a form. Type assumptions are needed:

{strlen : string → int, s : string} ` strlen(s) : int

Simple types in one slide

There are atomic types (type constants such as int, bool, real etc.) and type variables α, β, γ etc. There are function types τ → σ, whenever τ and σ are types. We write Γ ` M : τ to express that λ-term M has type τ under the type assumptions for the free variables in M given by Γ In many programming languages we write down types for functions like this:

int strlen(string x){ M }

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 19 / 38 The type system is a deductive system for deriving (`) well-typings of such a form. Type assumptions are needed:

{strlen : string → int, s : string} ` strlen(s) : int

Simple types in one slide

There are atomic types (type constants such as int, bool, real etc.) and type variables α, β, γ etc. There are function types τ → σ, whenever τ and σ are types. We write Γ ` M : τ to express that λ-term M has type τ under the type assumptions for the free variables in M given by Γ In many programming languages we write down types for functions like this:

int strlen(string x){ M }

In type theory we write ` M : string → int

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 19 / 38 Type assumptions are needed:

{strlen : string → int, s : string} ` strlen(s) : int

Simple types in one slide

There are atomic types (type constants such as int, bool, real etc.) and type variables α, β, γ etc. There are function types τ → σ, whenever τ and σ are types. We write Γ ` M : τ to express that λ-term M has type τ under the type assumptions for the free variables in M given by Γ In many programming languages we write down types for functions like this:

int strlen(string x){ M }

In type theory we write ` M : string → int

The type system is a deductive system for deriving (`) well-typings of such a form.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 19 / 38 Simple types in one slide

There are atomic types (type constants such as int, bool, real etc.) and type variables α, β, γ etc. There are function types τ → σ, whenever τ and σ are types. We write Γ ` M : τ to express that λ-term M has type τ under the type assumptions for the free variables in M given by Γ In many programming languages we write down types for functions like this:

int strlen(string x){ M }

In type theory we write ` M : string → int

The type system is a deductive system for deriving (`) well-typings of such a form. Type assumptions are needed:

{strlen : string → int, s : string} ` strlen(s) : int

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 19 / 38 Simple types

Definition 15 (Simple types)

Let V denote a denumerable set of type variables, ranged over by metavariables α, β, γ, . . ., and let b range over a set B of type constants. The set T of simple types, ranged over by τ, σ, ρ, . . ., is defined inductively by:

α ∈ V ⇒ α ∈ T b ∈ B ⇒ b ∈ T τ ∈ T, σ ∈ T ⇒ τ → σ ∈ T

Type variables and type constants are referred to as atoms. We write τ → σ → ρ for τ → (σ → ρ).

A type environment is a finite set Γ of type assumptions of the form Γ = {(x1 : τ1),..., (xn : τn)} with xi 6≡ xj for i 6= j. We let Dm(Γ) = {x ∈ V | ∃τ ∈ T. (x : τ) ∈ Γ}. We write Γ, x : τ for Γ ∪ {(x : τ)}.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 20 / 38 Simple typed λ-calculus, λ→

(var) Γ, x : τ ` x : τ

Γ, x : τ ` M : σ (→I) Γ ` λx.M : τ → σ

Γ ` M : τ → σ Γ ` N : τ (→E) Γ ` MN : σ

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 21 / 38 Since such a derivation can be constructed for arbitrary τ we can see that

∅ ` λx.x : τ → τ, for all τ

is a meta-theorem of the system.

Simple typed λ-calculus, λ→

(var) {x : τ} ` x : τ (→I) ∅ ` λx.x : τ → τ

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 22 / 38 Simple typed λ-calculus, λ→

(var) {x : τ} ` x : τ (→I) ∅ ` λx.x : τ → τ Since such a derivation can be constructed for arbitrary τ we can see that

∅ ` λx.x : τ → τ, for all τ

is a meta-theorem of the system.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 22 / 38 Explicit typing

(var) Γ, x : τ `∗ x : τ

Γ, x : τ `∗ M : σ (→I) Γ `∗ λx: τ.M : τ → σ

Γ `∗ M : τ → σ Γ `∗ N : τ (→E) Γ `∗ MN : σ

Exercise 4

Show by induction on M: if Γ `∗ M : σ and Γ `∗ M : τ, then σ ≡ τ.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 23 / 38 Basis lemma

Let Γ ↓V = {(x : τ) ∈ Γ | x ∈ V }.

Lemma 16

If Γ ⊆ Γ0 then Γ ` M : τ implies Γ0 ` M : τ, If Γ ` M : τ, then FV(M) ⊆ Dm(Γ),

If Γ ` M : τ, then Γ ↓FV(M)` M : τ.

Exercise 5

Prove Lemma 16.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 24 / 38 Inversion

Lemma 17 (Generation lemma)

Suppose that Γ ` M : τ.

1 If M ≡ x, then Γ = Γ0, x : τ, 2 If M ≡ λx.N, then τ ≡ ρ → σ and Γ, x : ρ ` N : σ, 3 If M ≡ PQ, then Γ ` P : σ → τ and Γ ` Q : σ for some σ.

Proof.

Immediate by inspection of the last rule used in the derivation of Γ ` M : τ.

Exercise 6

Call a term M typable, if Γ ` M : τ for some Γ and τ.

Show that every subterm of a typable term is typable.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 25 / 38 Substitutivity

A type substitution is a map S : V → T, and it is lifted homomorphically to a map S : T → T. Let S(Γ) = {(x : S(τ)) | (x : τ) ∈ Γ}.

Lemma 18 (Substitution)

1 If Γ, x : τ ` M : σ and Γ ` N : τ, then Γ ` M[x := N]: σ. 2 If Γ ` M : τ, then S(Γ) ` M : S(τ), for any type substitution S.

Exercise 7

Prove Lemma 18 by induction on derivations.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 26 / 38 Subject reduction

Theorem 19 (Subject Reduction)

If Γ ` M : τ and M β N, then Γ ` N : τ.

Proof.

(Sketch) First prove the statement for the case when M is a redex and N its reduct, using the substitution lemma. Then prove the statement when M →β N by reduction of a redex R in M, by induction on C where M ≡ C[R]. Then prove the statement by induction in the length of the reduction M β N.

Exercise 8

Complete the proof sketch of Theorem 19.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 27 / 38 Strong normalization

Definition 20

A λ-term M is called strongly normalizing, if every β-reduction sequence starting from M is finite (i.e., leading to a normal form), and weakly normalizing, if there exists some reduction sequence to normal form starting from M.

Theorem 21

Every typable term in λ→ is strongly normalizing.

Proof.

See lecture notes.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 28 / 38 Combinators

Let C be a set of combinator symbols, ranged over by X,Y,Z. The set ΞC of combinatory expressions, ranged over by F, G, H are defined inductively by:

X ∈ C ⇒ X ∈ ΞC,

x ∈ V ⇒ x ∈ ΞC,

F,G ∈ ΞC ⇒ (FG) ∈ ΞC

Consider the set SKI = {S, K, I} of combinator symbols (referred to as a combinatory base) and define the notion of weak reduction £w on ΞSKI by setting, for all X,Y,Z ∈ ΞSKI: IF £w F KFG £w F SF GH £w (FH)(GH)

Let →w and w be the reduction relations on ΞSKI induced by £w, by closing £w under contexts C ::= [] | (CF ) | (FC), (F ∈ ΞSKI).

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 29 / 38 Combinatory bases

By choosing different sets B ⊆ C of combinators (such as B = SKI = {S, K, I}) we can study different combinatory calculi, since in each case we can consider a B-calculus generated from the combinators in B. In such cases, we refer to the set B as a combinatory base.

Exercise 9

Show that the combinator I can be coded in terms of S and K. Hint: Notice that (KF )(KF ) w F . In other words, the base SKI = {S, K, I} is redundant. Or, in yet other words, the base SK = {S, K} is complete with respect to SKI-calculus. For this reason, one also talks about SK-calculus.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 30 / 38 Combinatory bases

Sch¨onfinkel used, in addition to S and K, the combinators B and C with the definitions BF GH £B F GH CF GH £C FHG But they are not strictly needed, for we can take

B ≡ S(KS)K C ≡ S(S(K(S(KS)K))S)(KK)

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 31 / 38 Combinatory bases

Exercise 10 (One point basis)

Define the combinator X by the rule

(XF ) £X ((F S)K)

Show that (XX) X SK(KK).

Show that X(X(XX)) X K.

Show that X(X(X(XX))) X S. Conclude that {X} is complete with respect to SKI-calculus.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 32 / 38 ΞSKI 7→ Λ

Define the map ( )Λ :ΞSKI → Λ by induction on expressions in ΞSKI:

(x)Λ ≡ x, for x ∈ V (I)Λ ≡ λx.x (K)Λ ≡ λxy.x (S)Λ ≡ λxyz.(xz)(yz) (FG)Λ ≡ (F )Λ(G)Λ

Proposition 1

If F w G, then (F )Λ β (G)Λ.

Exercise 11

Prove Proposition 1 by induction on the length of F w G.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 33 / 38 Λ 7→ ΞSKI

Define, for each x ∈ V, the “bracket abstraction” map [x] :ΞSKI → ΞSKI by induction on expressions in ΞSKI: [x]x ≡ I [x]F ≡ KF, if x 6∈ FV(F ) [x](FG) ≡ S([x]F )([x]G), otherwise

Proposition 2 (Combinatory completeness)

1 ∀F ∈ ΞSKI.∀G ∈ ΞSKI. ([x]F )G w F [x := G] 2 ∀F ∈ ΞSKI. ([x]F )Λ β λx.(F )Λ 3 ∀x ∈ V. ∀F ∈ ΞSKI. ∃H ∈ ΞSKI. ∀G ∈ ΞSKI.HG w F [x := G]

Exercise 12

Prove Proposition 2.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 34 / 38 Λ 7→ ΞSKI

Define the map ( )Ξ :Λ → ΞSKI by induction on λ-terms:

(x)Ξ ≡ x, for x ∈ V (MN)Ξ ≡ (M)Ξ(N)Ξ (λx.M)Ξ ≡ [x](M)Ξ

Proposition 3

For all M ∈ Λ, one has ((M)Ξ)Λ β M.

Exercise 13

Prove Proposition 3.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 35 / 38 Combinatory logic SKI

(var) Γ, x : τ `ski x : τ

(I) Γ `ski I : τ → τ

(K) Γ `ski K : τ → σ → τ

(S) Γ `ski S :(τ → σ → ρ) → (τ → σ) → τ → ρ

Γ ` F : τ → σ Γ ` G : τ ski ski (→E) Γ `ski (FG): σ

Notice that variables x have fixed, monomorphic types, whereas combinators S, K, I have infinitely many types (their types are schematic or polymorphic). We shall return to this important point in Lecture 5.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 36 / 38 Combinatory logic SKI

Lemma 22 ( for SKI)

If Γ, x : σ `ski F : τ, then Γ `ski [x]F : σ → τ.

Proposition 4

1 If Γ `ski F : τ, then Γ `Λ (F )Λ : τ.

2 If Γ `Λ M : τ, then Γ `ski (M)Ξ : τ.

Exercise 14

Prove Proposition 4. Hint: The first statement is proven by induction on the derivation of Γ `ski F : τ. The second statement is proven by induction on the derivation of Γ `Λ M : τ using Lemma 22.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 37 / 38 Decision problems

Type checking (Γ ` M : τ?). Given Γ, M and τ, does Γ ` M : τ hold? Typability (? ` M :?). Given M, are there Γ and τ such that Γ ` M : τ? Inhabitation (Γ `?: τ). Given Γ and τ, does there exist a term (“inhabitant”) M such that Γ ` M : τ?

Type checking and typability for λ→ are linear time solvable.

Inhabitation for λ→ is pspace-complete. See Lecture 2.

Inhabitation can be seen as a program synthesis problem:

Construct program M satisfying specification τ.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 38 / 38 H. P. Barendregt. The Lambda Calculus. Its Syntax and Semantics. Studies in Logic and the Foundations of Mathematics, 2nd Edition. Elsevier Science Publishers, 1984. H. P. Barendregt. Lambda Calculus with Types, volume 2. Oxford University Press, 1993. H. P. Barendregt, W. Dekkers, and R. Statman. Lambda Calculus with Types. Perspectives in Logic, Cambridge University Press, 2013. J. R. Hindley and J. P. Seldin. Lambda-calculus and Combinators, an Introduction. Cambridge University Press, 2008. R. Nederpelt and H. Geuvers. Type Theory and Formal Proof. An Introduction. Cambridge University Press, 2014.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 38 / 38 M.H. Sørensen and P. Urzyczyn. Lectures on the Curry-Howard Isomorphism, volume 149 of Studies in Logic and the Foundations of Mathematics. Elsevier, 2006.

J. Rehof (TU Dortmund) IPM Tehran August 30, 2017 38 / 38