Komponenten- und Service-orientierte Softwarekonstruktion
Lecture 2: Types and combinators
Jakob Rehof
LS XIV – Software Engineering
TU Dortmund Sommersemester 2017
Simple types
Definition 1 (Simple types)
LetVdenote a denumerable set oftype variables, ranged over by metavariables α, β, γ, . . ., and letbrange over a setBoftype constants. The setTofsimple 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 asatoms. We writeτ→σ→ρfor τ →(σ→ρ).
Atype environmentis a finite setΓoftype assumptionsof the form Γ ={(x1:τ1), . . . ,(xn:τn)}withxi6≡xj fori6=j. We let
Dm(Γ) ={x∈ V | ∃τ∈T.(x:τ)∈Γ}. We writeΓ, x:τ forΓ∪ {(x:τ)}.
Simple typed λ-calculus, λ
→Γ, x : τ ` x : τ (var)
Γ, x : τ ` M : σ Γ ` λx.M : τ → σ (→I)
Γ ` M : τ → σ Γ ` N : τ
Γ ` M N : σ (→E)
Explicit typing
Γ, x:τ`∗x:τ(var)
Γ, x:τ`∗M:σ Γ`∗λx:τ.M :τ →σ(→I)
Γ`∗M:τ →σ Γ`∗N :τ Γ`∗M N:σ (→E)
Exercise 1
Show by induction onM: ifΓ`∗M :σandΓ`∗M :τ, thenσ≡τ.
Basis lemma
Let Γ ↓
V= {(x : τ ) ∈ Γ | x ∈ V }.
Lemma 2
If Γ ⊆ Γ
0then Γ ` M : τ implies Γ
0` M : τ , If Γ ` M : τ , then FV(M ) ⊆ Dm(Γ),
If Γ ` M : τ , then Γ ↓
FV(M)` M : τ .
Exercise 2
Prove Lemma 2.
Inversion
Lemma 3 (Generation lemma)
Suppose thatΓ`M :τ.
1 IfM≡x, thenΓ = Γ0, x:τ,
2 IfM≡λx.N, thenτ≡ρ→σandΓ, x:ρ`N :σ,
3 IfM≡P Q, thenΓ`P:σ→τ andΓ`Q:σfor someσ.
Proof.
Immediate by inspection of the last rule used in the derivation ofΓ`M:τ.
Exercise 3
Call a termM typable, if Γ`M :τ for someΓandτ.
Show that every subterm of a typable term is typable.
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 4 (Substitution)
1
If Γ, x : τ ` M : σ and Γ ` N : τ , then Γ ` M [x := N ] : σ.
2
If Γ ` M : τ , then S(Γ) ` M : S(τ ), for any type substitution S.
Exercise 4
Prove Lemma 4 by induction on derivations.
Subject reduction
Theorem 5 (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 5
Complete the proof sketch of Theorem 5.
Strong normalization
Definition 6
Aλ-termM is calledstrongly normalizing, if everyβ-reduction sequence starting from M is finite, andweakly normalizing, if there exists a finite reduction sequence starting fromM.
Theorem 7
Every typable term inλ→ is strongly normalizing.
Proof.
See lecture notes.
Combinators
LetCbe a set ofcombinator symbols, ranged over byX, Y, Z. The setΞC of combinatory expressions, ranged over byF, G, H are defined inductively by:
X∈ C ⇒X∈ΞC, x∈ V ⇒x∈ΞC, F, G∈ΞC⇒(F G)∈ΞC
Consider the set SKI={S,K,I}of combinator symbols (referred to as a combinatory base) and define the notion ofweak reductionwonΞSKIby setting, for all
X, Y, Z∈ΞSKI:
IF w F
KF G w F
SF GH w (F H)(GH)
Let→w andw be the reduction relations onΞSKIinduced byw, by closingw
under contextsC::= []|(CF)|(F C), (F∈ΞSKI).
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 6
Show that the combinator I can be coded in terms of S and K. Hint:
Notice that (KF)(KF )
wF .
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.
Combinatory bases
Sch¨ onfinkel used, in addition to S and K, the combinators B and C with the definitions
BF GH
BF GH CF GH
CF HG But they are not strictly needed, for we can take
B ≡ S(KS)K
C ≡ S(S(K(S(KS)K))S)(KK)
Combinatory bases
Exercise 7 (One point basis)
Define the combinator X by the rule
(XF )
X((F S)K)
Show that (XX)
XSK(KK).
Show that X(X(XX))
XK.
Show that X(X(X(XX)))
XS.
Conclude that {X} is complete with respect to SKI-calculus.
Ξ
SKI7→ Λ
Define the map( )Λ: ΞSKI→Λby induction on expressions inΞSKI: (x)Λ ≡ x, forx∈ V
(I)Λ ≡ λx.x (K)Λ ≡ λxy.x (S)Λ ≡ λxyz.(xz)(yz) (F G)Λ ≡ (F)Λ(G)Λ
Proposition 1
IfF wG, then(F)Λβ (G)Λ.
Exercise 8
Prove Proposition 1 by induction on the length ofF wG.
Λ 7→ Ξ
SKIDefine, for eachx∈ V, the “bracket abstraction” map [x]: ΞSKI→ΞSKIby induction on expressions inΞSKI:
[x]x ≡ I
[x]F ≡ KF, ifx6∈FV(F) [x](F G) ≡ S([x]F)([x]G), otherwise
Proposition 2 (Combinatory completeness)
1 ∀F∈ΞSKI.∀G∈ΞSKI.([x]F)GwF[x:=G]
2 ∀F∈ΞSKI.([x]F)Λβ λx.(F)Λ
3 ∀x∈ V.∀F ∈ΞSKI.∃H∈ΞSKI.∀G∈ΞSKI. HGwF[x:=G]
Exercise 9
Λ 7→ Ξ
SKIDefine the map( )Ξ: Λ→ΞSKIby induction onλ-terms:
(x)Ξ ≡ x, forx∈ V (M N)Ξ ≡ (M)Ξ(N)Ξ
(λx.M)Ξ ≡ [x](M)Ξ
Proposition 3
For allM∈Λ, one has((M)Ξ)ΛβM.
Exercise 10
Prove Proposition 3.
Combinatory logic SKI
Γ, x:τ `skix:τ(var)
Γ`skiI:τ →τ(I)
Γ`skiK:τ →σ→τ(K)
Γ`skiS: (τ→σ→ρ)→(τ →σ)→τ →ρ(S) Γ`skiF:τ→σ Γ`skiG:τ
Γ`ski(F G) :σ (→E)
Notice that variablesxhave fixed,monomorphic types, whereas combinatorsS,K,I have infinitely many types (their types areschematicorpolymorphic). We shall return to this important point in Lecture5.
Combinatory logic SKI
Lemma 8 (Deduction theorem for SKI)
IfΓ, x:σ`skiF :τ, thenΓ`ski[x]F :σ→τ.
Proposition 4
1 IfΓ`skiF:τ, thenΓ`Λ(F)Λ:τ.
2 IfΓ`ΛM:τ, thenΓ`ski(M)Ξ:τ.
Exercise 11
Prove Proposition 4. Hint: The first statement is proven by induction on the derivation ofΓ`skiF :τ. The second statement is proven by induction on the derivation of Γ`ΛM :τ using Lemma 8.
Decision problems
Type checking(Γ`M :τ?). GivenΓ,M andτ, doesΓ`M :τ hold?
Typability(?`M :?). GivenM, 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λ→ ispspace-complete. See Lecture3.
Inhabitation can be seen as a program synthesis problem:
ConstructprogramM satisfyingspecificationτ.