• Tidak ada hasil yang ditemukan

CS6848 - Principles of Programming Languages - CSE-IITM

N/A
N/A
Protected

Academic year: 2024

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

*

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

*

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

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

*

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

*

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

*

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

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

*

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

*

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

*

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

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

*

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

Referensi

Dokumen terkait

V.Krishna Nandivada IIT Madras CS6848 IIT Madras 10 / 31 * Representation of types - as functions Denote an alphabetΣthat contains all the labels and paths of the type tree.. We can

Type systems allow single piece of code to be used with multiple types are collectively known as polymorphic systems.. Parametric polymorphism: Single piece of code to be typed

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

* JavaCC input One file header token specification for lexical analysis grammar Example of a token specification: TOKEN : { < INTEGER_LITERAL: ["1"-"9"] ["0"-"9"]* | "0" > }

* 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

V.Krishna Nandivada IIT Madras CS6848 IIT Madras 22 / 24 * Type soundness Corollary Well typed programs cannot go wrong.. Proof Say we have a well typed

* JavaCC input One file header token specification for lexical analysis grammar Example of a token specification: TOKEN : { < INTEGER_LITERAL: ["1"-"9"] ["0"-"9"]* | "0" > }

V.Krishna Nandivada IIT Madras CS6848 IIT Madras 13 / 25 * Type soundness A type system for a programming language is sound if well-typed programs cannot go wrong.. A program goes