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
*
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
*
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
*
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