• 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!
5
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 / 19

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.

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

(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 / 19

*

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 / 19

*

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 / 19

*

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 / 19

(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 / 19

*

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 / 19

Example

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

How about in System F?

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

Church literals

Booleans

tru=λt.λf.t fls=λt.λf.f

Idea: A predicate will returntruorfls.

We can writeif pred s1 else s2as (pred s1 s2)

(4)

*

Building on booleans

and=λb.λc.b cfls or= ?λb.λc.btruc not= ?

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

*

Building pairs

pair=λf.λs.λb.b f s To build a pair: pair v w fst=λp.ptru

snd=λp.pfls

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

*

Church numerals

c0=λs.λz.z c1=λs.λz.s z c2=λs.λz.s s z c3=λs.λz.s s s z Intuition

Each numbernis represented by a combinatorcn.

cntakes an arguments(for successor) andz(for zero) and applys, ntimes, toz.

c0andflsare exactly the same!

This representation is similarto the unary representation we studies before.

scc=λn.λs.λz.s(n s z)

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 15 / 19

*

Discussion on type inference

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 16 / 19

(5)

*

(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. Say is(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) 17 / 19

*

Examples - derive the types

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

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

V.Krishna Nandivada (IIT Madras) CS6848 (IIT Madras) 18 / 19

“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?

What if we are interested in System F - what happens to the type inference? (undecidable in general)

Self study.

Referensi

Dokumen terkait

The input variables at the initial states of the traces of a program store the values of its input data: we write σ[0]i to denote the value of the input data stored in the input

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

• enum months {jan = 1, feb, march, april, may, jun, jul, aug, sep, oct, nov, dec}; • When a value is explicitly specified jan=1 then it starts counting from there • Values start from

* Connection between axiomatic and operational semantics Semantics of Valid assertions Soundness Completeness V.Krishna Nandivada IIT Madras CS6848 IIT Madras 21 / 27 * Validity

Enumerate Devices int deviceCount; cudaGetDeviceCount&deviceCount; int device; for device = 0; device < deviceCount; ++device { cudaDeviceProp deviceProp;

of single glass [U value of 1.13] with an inside temperature of 70°F and an outside temperature of 0°Fwould have 791 BTUH heat loss: A 10 x U 1.13 x ∆T 70 = 791 Btu/hr Since the

* A Stack Machine Interpreter define eval-action lambda action stack let op-code car action case op-code halt car stack incr eval-action cdr action cons + car stack 1 cdr stack

* Outline V.Krishna Nandivada IIT Madras CS6848 IIT Madras 9 / 1 * Small step semantics In small step semantics, one step of computation = either one primitive operation, or inline