• Keine Ergebnisse gefunden

Komponenten- und Service-orientierte Softwarekonstruktion

N/A
N/A
Protected

Academic year: 2021

Aktie "Komponenten- und Service-orientierte Softwarekonstruktion"

Copied!
19
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Komponenten- und Service-orientierte Softwarekonstruktion

Lecture 2: Types and combinators

Jakob Rehof

LS XIV – Software Engineering

TU Dortmund Sommersemester 2017

(2)

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 Γ ={(x11), . . . ,(xnn)}withxi6≡xj fori6=j. We let

Dm(Γ) ={x∈ V | ∃τ∈T.(x:τ)∈Γ}. We writeΓ, x:τ forΓ∪ {(x:τ)}.

(3)

Simple typed λ-calculus, λ

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

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

Γ ` M : τ → σ Γ ` N : τ

Γ ` M N : σ (→E)

(4)

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σ≡τ.

(5)

Basis lemma

Let Γ ↓

V

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

Lemma 2

If Γ ⊆ Γ

0

then Γ ` M : τ implies Γ

0

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

If Γ ` M : τ , then Γ ↓

FV(M)

` M : τ .

Exercise 2

Prove Lemma 2.

(6)

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.

(7)

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.

(8)

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.

(9)

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.

(10)

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).

(11)

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 )

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.

(12)

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

F HG But they are not strictly needed, for we can take

B ≡ S(KS)K

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

(13)

Combinatory bases

Exercise 7 (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.

(14)

Ξ

SKI

7→ Λ

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.

(15)

Λ 7→ Ξ

SKI

Define, 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

(16)

Λ 7→ Ξ

SKI

Define 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.

(17)

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.

(18)

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.

(19)

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τ.

Referenzen

ÄHNLICHE DOKUMENTE

metaprogramming (SCS) domain-specific languages declarative languages logic programming generative programming model driven design (MDD) verification. automatic

Use modal types 2 φ (“code of type φ”) to expose language distinction to compostion synthesis. Introduction of modal

Although these firms operated largely in different regions of the world, factors precipitating their demise have the potential to enhance our understanding of

the story of CRX (by Donald J. Zack), RPE65 gene mutations and the retina: human disease and its knockout mouse model (by T. Michael Redmond), LCA caused by a recessive CRX mutation

Die Wahrscheinlichkeit, dass wir evakuiert werden ist sehr hoch, weil wir mitten im prognostizierten Verlauf des Taifuns sind!. In diesem Fall wird unsere Expedition enden und

If we don’t catch fish below the Best Starting Length, we can maximise fishing profits when fishing rates stay below natural mortality, with stock sizes above half of the

Catching the young fish of large species like cod, results in a large reduction in population biomass.. Looking at figures 2 & 3, which fishing strategy results

To practice some of the methods commonly used in loop calculations, we will compute the fermionic contribution to the photon self-energy, as shown