• Tidak ada hasil yang ditemukan

CS6848 - Principles of Programming Languages - CSE-IITM

N/A
N/A
Protected

Academic year: 2025

Membagikan "CS6848 - Principles of Programming Languages - CSE-IITM"

Copied!
4
0
0

Teks penuh

(1)

CS6848 - Principles of Programming Languages

Principles of Programming Languages

V. Krishna Nandivada

IIT Madras

*

Recap

Extensions to simply typed lambda calculus.

Pairs, Tuples and records

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 2 / 13

*

Parametric Polymorphism - System F

System F discovered by Jean-Yves Girard (1972) Polymorphic lambda-calculus by John Reynolds (1974)

Also called second-order lambda-calculus - allows quantification over types, along with terms.

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 3 / 13

*

System F

Definition of System F - an extension of simply typed lambda calculus.

Lambda calculus recall

Lambda abstraction is used to abstract terms out of terms.

Application is used to supply values for the abstract types.

System F

A mechanism for abstracting types of out terms and fill them later.

A new form of abstraction:

λX.e– parameter is a type.

Application –e[t]

called type abstractions and type applications (or instantiation).

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 4 / 13

(2)

*

Type abstraction and application

(λX.e)[t1]→[x→t1]e Examples

id=λX.λx:X.x

Type ofid:∀X.XX

applyTwice=λX.λf :XX.λa:Xf (f a) Type ofapplyTwice:∀X.(XX)XX

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 5 / 13

*

Extension

Expressions:

e::=· · · |λX.e|e[t]

Values

v::=· · · |λX.e Types

t::=· · · |∀X.t typing context:

A::=φ|A,x:t|A,X

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 6 / 13

*

Evaluation

type application 1 — e1→e01 e1[t1]→e01[t1] type appliation 2 —(λX.e1)[t1]→[X→t1]e1

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 7 / 13

*

Typing rules

type abstraction A,X`e1:t1 A`λX.e1:∀X.t1

type application A`e1:∀X.t1 A`e1[t2] : [X→t2]t1

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 8 / 13

(3)

*

Examples

id=λX.λx:X x id:∀X.X→X

type application: id[Int] :Int →Int value application:id[Int]0=0:Int applyTwice=λX.λf :X→X.λa:Xf (f a) ApplyTwiceInts=applyTwice[Int]

applyTwice[Int](λx:Int.succ(succx))3

=7

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 9 / 13

*

Polymorphic lists

List of uniform members nil:∀X.List → X

cons:∀X.X→List X→List X isnil?:∀X.List X→bool head:∀X.List X→X tail:∀X.List X→List X

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 10 / 13

*

Example

Recall: Simply typed lambda calculus - we cannot typeλx.x x.

How about in System F?

selfApp :(∀X.X→X)→(∀X.X→X)

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 11 / 13

*

(Recall) Type inference algorithm (Hindley-Milner)

Input: G: set of type equations (derived from a given program).

Output: Unificationσ

1 failure =false;σ ={}.

2 whileG6=φ and¬failure do

1 Choose and remove an equationefrom G. Sayis(s=t).

2 Ifsandtare variables, orsandtare both Int then continue.

3 Ifs=s1s2andt=t1t2, thenG=G∪ {s1=t1,s2=t2}.

4 If (s=Int andtis an arrow type) or vice versa then failure =true.

5 Ifsis a variable that does not occur int, thenσ=σ o[s:=t].

6 Iftis a variable that does not occur ins, thenσ=σ o[t:=s].

7 Ifs6=tand eithersis a variable that occurs intor vice versa then failure =true.

3 end-while.

4 if (failure = true) then output “Does not type check”. Else o/pσ.

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 12 / 13

(4)

*

Examples

a=λx.λy.x b=λf.(f 3)

c=λx.(+(head x)3) d=λf.((f 3),(f λy.y))

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 13 / 13

*

“Occurs” check

Ensures that we get finite types.

If we allow recursive types - the occurs check can be omitted.

Say in (s=t),s=Aandt=AB. Resulting type?

Example term?

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 14 / 13

Referensi

Dokumen terkait

V.Krishna Nandivada IIT Madras CS3400 15 / 35 * Master/Worker template for master int nTasks // Number of tasks int nWorkers // Number of workers public static SharedQueue

V.Krishna Nandivada IIT Madras CS6013 - Aug 2021 10 / 15 * Dependence testing for separable array references If the two array references are separable, then dependence exists if

V.Krishna Nandivada IIT Madras CS3400 30 / 57 * Sequential Consistency implementation Implementations of this model must satisfy the following: Program Order Requirement : The

A pair that is passed as an argument will be fully evaluated, before the function starts executingin call by value