• 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

Structural subtyping Unification algorithm

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

*

Recall

e ::= x | λ x.e | e 1 e 2 | c | succ e

x ∈ Identifier (infinite set of variables) c ∈ Integer

v :: = c|λ x.e t :: = Int |t → t

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

*

Extending a language

Extend the language grammar that will lead to new terms.

Extend the allowed values.

Extend the types.

New operational semantics.

New typing rules.

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

(2)

*

Pairs

Expressions

e ::= · · · |(e 1 ,e 2 )|e.1|e.2 Values

v ::= · · · |(v 1 , v 2 ) Types

t ::= · · · |t 1 × t 2

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

*

New operational semantics

First element:

(Pair β 1) (v 1 ,v 2 ).1 → v 1 Second element:

(Pair β 2) (v 1 ,v 2 ).2 → v 2

Projection 1 e → e 0 e.1 → e 0 .1

Projection 2 e → e 0 e.2 → e 0 .2

Pair Evaluation 1 e 1 → e 0 1 (e 1 ,e 2 ) → (e 0 1 ,e 2 )

Pair Evaluation 2 e 2 → e 0 2 (v 1 ,e 2 ) → (v 1 ,e 0 2 )

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

*

Typing rules for pairs

Pair A ` e 1 : t 1 A ` e 2 : t 2 A ` (e 1 ,e 2 ) : t 1 × t 2

Projection 1 A ` e : t 1 ×t 2 A ` e.1 : t 1

Projection 2 A ` e : t 1 ×t 2 A ` e.2 : t 2

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

*

Properties of pairs

The components are evaluated left to right.

The pair must be fully evaluated to get the components.

A pair that is passed as an argument will be fully evaluated, before the function starts executing(in call by value semantics).

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

(3)

*

Tuples

Expressions

e ::= · · · |(e i∈1..n i )|e.i Values

v ::= · · · |(v i∈1..n i ) Types

t ::= · · · |(t i i∈1..n )

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

*

New operational semantics

Element j:

(β )(v i∈1..n i ).j → v j

Projection 1 e → e 0 e.i → e 0 .i

Tuple Evaluation

e j → e 0 j

(v i∈1..j−1 i , e j ,e k∈j+1..n k ) → (v i∈1..j−1 i ,e 0 j , e k∈j+1..n k )

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

*

Typing rules for tuples

Tuple A ` ∀i e i : t i

A ` (e i∈1..n i ) : (t i∈1..n i )

Projection A ` e : (t i∈1..n ) A ` e.j : t j

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

*

Records

Expressions

e ::= · · · |(l i = e i∈1..n i )|e.l Values

v ::= · · · |(l i = v i∈1..n i ) Types

t ::= · · · |(l i : t i∈1..n i )

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

(4)

*

New operational semantics

Element j:

β reduction (l i : v i∈1..n i ).l j → v j

Projection 1 e → e 0 e.l → e 0 .l

Record Evaluation

e j → e 0 j

(l i = v i∈1..j−1 i , l j = e j , l k = e k∈j+1..n k ) → (l i = v i∈1..j−1 i , l j = e 0 j , l k = e k∈j+1..n k )

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

*

Typing rules for tuples

Tuple A ` ∀i e i : t i

A ` (l i = e i∈1..n i ) : (l i : t i∈1..n i )

Projection A ` e : (l i : t i∈1..n ) A ` e.l j : t j

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

*

Polymorphism - motivation

AppTwiceInt = λf : Int → Int .λ x : Int .f (f x)

AppTwiceRcd = λ f : (l : Int ) → (l : Int ).λ x : (l : Int ).f (f x) AppTwiceOther =

λ f : (Int → Int ) → (Int → Int ).λ x : (Int → Int ).f (f x)

Breaks the idea of abstraction: Each significant piece of

functionality in a program should be implemented in just one place in the source code.

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

*

Polymorphism - variations

Type systems allow single piece of code to be used with multiple types are collectively known as polymorphic systems.

Variations:

Parametric polymorphism: Single piece of code to be typed generically (also known as, let polymorphism, first-class polymorphism, or ML-style polymorphic).

Restricts polymorphism to top-level let bindings.

Disallows functions from taking polymorphic values as arguments.

Uses variables in places of actual types and may instantiate with actual types if needed.

Example: ML, Java Generics

(let ((apply lambda f. lambda a (f a))) (let ((a (apply succ 3)))

(let ((b (apply zero? 3))) ...

Ad-hoc polymorphism - allows a polymorphic value to exhibit different behaviors when viewed using different types.

Example: function Overloading, Java instanceof operator.

subtype polymorphism: A single term may get many types using subsumption.

Java 1.5 onwards admits Parametric, Ad-hoc and subtype polymorphism.

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

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