types and programming languages - sjtubasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf ·...

69
Types and Programming Languages Lecture 1. Introduction & Untyped arithmetic expressions Xiaojuan Cai [email protected] BASICS Lab, Shanghai Jiao Tong University Fall, 2016

Upload: others

Post on 02-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Types and Programming Languages

Lecture 1. Introduction & Untyped arithmeticexpressions

Xiaojuan Cai

[email protected]

BASICS Lab, Shanghai Jiao Tong University

Fall, 2016

Page 2: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Why study this course?

I For credits?

I To meet the requirement that every master student shouldtake at least TWO English courses?

I Being curious about Programming Languages.

Page 3: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Why study this course?

I For credits?

I To meet the requirement that every master student shouldtake at least TWO English courses?

I Being curious about Programming Languages.

Page 4: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Why study this course?

I For credits?

I To meet the requirement that every master student shouldtake at least TWO English courses?

I Being curious about Programming Languages.

Page 5: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Programming languages

I How many programming languages in the world?

I https://en.wikipedia.org/wiki/List_of_programming_

languagesI https://en.wikipedia.org/wiki/List_of_programming_

languages_by_typeI http://99-bottles-of-beer.net/

I Which programming language is the best?I Which type of cars is the best?

I Winning an F1 raceI Taking kids to the kindergarten

Page 6: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Programming languages

I How many programming languages in the world?I https://en.wikipedia.org/wiki/List_of_programming_

languagesI https://en.wikipedia.org/wiki/List_of_programming_

languages_by_typeI http://99-bottles-of-beer.net/

I Which programming language is the best?I Which type of cars is the best?

I Winning an F1 raceI Taking kids to the kindergarten

Page 7: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Programming languages

I How many programming languages in the world?I https://en.wikipedia.org/wiki/List_of_programming_

languagesI https://en.wikipedia.org/wiki/List_of_programming_

languages_by_typeI http://99-bottles-of-beer.net/

I Which programming language is the best?

I Which type of cars is the best?I Winning an F1 raceI Taking kids to the kindergarten

Page 8: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Programming languages

I How many programming languages in the world?I https://en.wikipedia.org/wiki/List_of_programming_

languagesI https://en.wikipedia.org/wiki/List_of_programming_

languages_by_typeI http://99-bottles-of-beer.net/

I Which programming language is the best?I Which type of cars is the best?

I Winning an F1 raceI Taking kids to the kindergarten

Page 9: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Programming languages

I How many programming languages in the world?I https://en.wikipedia.org/wiki/List_of_programming_

languagesI https://en.wikipedia.org/wiki/List_of_programming_

languages_by_typeI http://99-bottles-of-beer.net/

I Which programming language is the best?I Which type of cars is the best?

I Winning an F1 raceI Taking kids to the kindergarten

Page 10: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Where do we start?

Which programming language should we start with?

A popular one or a classical one?

We will focus mostly on functional programming languagesbecause

I mutation is discouraged which makes programs safe.

I higher-order functions are very convenient.

ML-like languages: SML, OCaml, Haskell, ...

Page 11: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Where do we start?

Which programming language should we start with?A popular one or a classical one?

We will focus mostly on functional programming languagesbecause

I mutation is discouraged which makes programs safe.

I higher-order functions are very convenient.

ML-like languages: SML, OCaml, Haskell, ...

Page 12: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Where do we start?

Which programming language should we start with?A popular one or a classical one?

We will focus mostly on functional programming languagesbecause

I mutation is discouraged which makes programs safe.

I higher-order functions are very convenient.

ML-like languages: SML, OCaml, Haskell, ...

Page 13: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Where do we start?

Which programming language should we start with?A popular one or a classical one?

We will focus mostly on functional programming languagesbecause

I mutation is discouraged which makes programs safe.

I higher-order functions are very convenient.

ML-like languages: SML, OCaml, Haskell, ...

Page 14: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Why types?

There are many excellent textbooks on Programming languages,such as:

I Programming Language Pragmatics, by Michael L. Scott

I Practical Foundations of Programming Languages, by RobertHarper

I Programming Languages, Principles and Paradigms, by AllenTucker and Robert Noonan

I ...

We will focus on types because

I most language features can be discussed in the framework oftype theory, and

I language design goes hand-in-hand with type system design.

.

Page 15: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Why types?

There are many excellent textbooks on Programming languages,such as:

I Programming Language Pragmatics, by Michael L. Scott

I Practical Foundations of Programming Languages, by RobertHarper

I Programming Languages, Principles and Paradigms, by AllenTucker and Robert Noonan

I ...

We will focus on types because

I most language features can be discussed in the framework oftype theory, and

I language design goes hand-in-hand with type system design.

.

Page 16: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Course overview

Benjamin C. Pierce

We will discuss in this course:1. Structural induction2. λ-calculus3. simple type systems and safety4. Production types, sum types5. Reference types, exceptions6. Subtyping7. Recursive types8. Type reconstruction9. Universal type and polymorphism10. type operators ...

Page 17: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Course overview

Benjamin C. Pierce

We will discuss in this course:1. Structural induction2. λ-calculus3. simple type systems and safety4. Production types, sum types5. Reference types, exceptions6. Subtyping7. Recursive types8. Type reconstruction9. Universal type and polymorphism10. type operators ...

Page 18: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Course overview

Benjamin C. Pierce

We will discuss in this course:1. Structural induction2. λ-calculus3. simple type systems and safety4. Production types, sum types5. Reference types, exceptions6. Subtyping7. Recursive types8. Type reconstruction9. Universal type and polymorphism10. type operators ...

Page 19: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

After this course, you can ...

I Know how to judge a language.

I Learn to think about software in“PL” way and become abetter programmer

I Confidently pick up new languages for your lifetime.

I ...

Page 20: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Course policy

I Final exam: 70%

I Homework: 30%

I TA:Yuwei Wang, [email protected]

I Course homepage:http://basics.sjtu.edu.cn/~xiaojuan/tapl2016

Page 21: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Course policy

I Final exam: 70%

I Homework: 30%

I TA:Yuwei Wang, [email protected]

I Course homepage:http://basics.sjtu.edu.cn/~xiaojuan/tapl2016

Page 22: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Outline

Introduction

Preliminaries

Untyped arithmetic expressionsAbstract syntaxInduction on termsSemantics

BooleansNumbers and Booleans

Page 23: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Types in Computer Science

Type systems is the most popular and best established lightweightformal methods.

DefinitionA type system is a tractable syntactic method for proving theabsence of certain program behaviors by classifying phrasesaccording to the kinds of values they compute.

Page 24: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Types in Computer Science

Type systems is the most popular and best established lightweightformal methods.

DefinitionA type system is a tractable syntactic method for proving theabsence of certain program behaviors by classifying phrasesaccording to the kinds of values they compute.

Page 25: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Brief history

Types system (type theory) refers to a much broader field.

I 1900. Formalized, Russell’s paradox

I 1925. Simple theory of types, Ramsey

I 1940. Simply typed λ-calculus, Church

I 1973. Constructive type theory, Martin Lof

I 1992. Pure type theory, Barendregt

I ...

Page 26: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Some definitions

I Static type system. Type checking during compile-time

I Dynamic type system. Type checking during run-time

I Static ⇒ Conservative ⇒ prove the absence of bad behaviours

I Incapable of finding all undesired program behavirous, e.g.divide by zero

I Type checkersI automatic: no manual interactionI type annotations

Page 27: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Some definitions

I Static type system. Type checking during compile-time

I Dynamic type system. Type checking during run-time

I Static ⇒ Conservative ⇒ prove the absence of bad behaviours

I Incapable of finding all undesired program behavirous, e.g.divide by zero

I Type checkersI automatic: no manual interactionI type annotations

Page 28: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Some definitions

I Static type system. Type checking during compile-time

I Dynamic type system. Type checking during run-time

I Static ⇒ Conservative ⇒ prove the absence of bad behaviours

I Incapable of finding all undesired program behavirous, e.g.divide by zero

I Type checkersI automatic: no manual interactionI type annotations

Page 29: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Some definitions

I Static type system. Type checking during compile-time

I Dynamic type system. Type checking during run-time

I Static ⇒ Conservative ⇒ prove the absence of bad behaviours

I Incapable of finding all undesired program behavirous, e.g.divide by zero

I Type checkersI automatic: no manual interactionI type annotations

Page 30: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

What types good for

I Detecting errors early.

I Maintenance tools.

I Abstracting

I Documentation

I Efficiency

Applications: network security, program analysis, theorem prover,database, xml, ...

Language design goes hand-in-hand with type system design.

Page 31: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Outline

Introduction

Preliminaries

Untyped arithmetic expressionsAbstract syntaxInduction on termsSemantics

BooleansNumbers and Booleans

Page 32: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Relations

I An n-place relation is a set R ⊆ S1 × S2 × · · · × Sn.

I A two-place relation R on sets S and T is called a binaryrelation. We often write s R t instead of (s, t) ∈ R.

I The ”mixfix” concrete syntax, e.g, Γ ` s : T means “the triple(Γ, s,T ) in the typing relation”.

I P is preserved by R if whenever we have s R t and P(s), wealso have P(t).

Page 33: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Relations

I An n-place relation is a set R ⊆ S1 × S2 × · · · × Sn.

I A two-place relation R on sets S and T is called a binaryrelation. We often write s R t instead of (s, t) ∈ R.

I The ”mixfix” concrete syntax, e.g, Γ ` s : T means “the triple(Γ, s,T ) in the typing relation”.

I P is preserved by R if whenever we have s R t and P(s), wealso have P(t).

Page 34: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Functions

I dom(R): the domain of a relation R on sets S and T is theset of elements s ∈ S such that (s, t) ∈ R for some t.

I A relation R on sets S and T is called a partial function if,whenever (s, t1) ∈ R and (s, t2) ∈ R, we have t1 = t2. Ifdom(R) = S , then R is a total function.

I We write f (x) ↑ to mean “f is undefined on x,” and f (x) ↓ tomean “f is defined on x.”

Page 35: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Ordered setsA binary relation R on a set S is

I Reflexive: ∀x ∈ S .x R x .

I Transitive: x R y ∧ y R z implies x R z .

I Symmetric: x R y implies y R x .

I Antisymmetric: x R y ∧ y R x implies x = y .

1. Preorder (or Quasi order): Reflexive + Transitive

2. Equivalence: Preorder + Symmetric

3. Partial order: Preorder + Antisymmetric

4. Total order: Partial oder + (∀x , y ∈ S .x R y ∨ y R x)

5. Well quasi order: Preorder + (Any infinite sequence containsan increasing pair)

6. Well founded order: Preorder + (No infinite decreasingsequences)

Quiz: 1. Can Transitivity + Symmetry indicate Reflexivity?2. Give examples of these orders.

Page 36: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Ordered setsA binary relation R on a set S is

I Reflexive: ∀x ∈ S .x R x .

I Transitive: x R y ∧ y R z implies x R z .

I Symmetric: x R y implies y R x .

I Antisymmetric: x R y ∧ y R x implies x = y .

1. Preorder (or Quasi order): Reflexive + Transitive

2. Equivalence: Preorder + Symmetric

3. Partial order: Preorder + Antisymmetric

4. Total order: Partial oder + (∀x , y ∈ S .x R y ∨ y R x)

5. Well quasi order: Preorder + (Any infinite sequence containsan increasing pair)

6. Well founded order: Preorder + (No infinite decreasingsequences)

Quiz: 1. Can Transitivity + Symmetry indicate Reflexivity?2. Give examples of these orders.

Page 37: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Ordered setsA binary relation R on a set S is

I Reflexive: ∀x ∈ S .x R x .

I Transitive: x R y ∧ y R z implies x R z .

I Symmetric: x R y implies y R x .

I Antisymmetric: x R y ∧ y R x implies x = y .

1. Preorder (or Quasi order): Reflexive + Transitive

2. Equivalence: Preorder + Symmetric

3. Partial order: Preorder + Antisymmetric

4. Total order: Partial oder + (∀x , y ∈ S .x R y ∨ y R x)

5. Well quasi order: Preorder + (Any infinite sequence containsan increasing pair)

6. Well founded order: Preorder + (No infinite decreasingsequences)

Quiz: 1. Can Transitivity + Symmetry indicate Reflexivity?2. Give examples of these orders.

Page 38: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Ordered setsA binary relation R on a set S is

I Reflexive: ∀x ∈ S .x R x .

I Transitive: x R y ∧ y R z implies x R z .

I Symmetric: x R y implies y R x .

I Antisymmetric: x R y ∧ y R x implies x = y .

1. Preorder (or Quasi order): Reflexive + Transitive

2. Equivalence: Preorder + Symmetric

3. Partial order: Preorder + Antisymmetric

4. Total order: Partial oder + (∀x , y ∈ S .x R y ∨ y R x)

5. Well quasi order: Preorder + (Any infinite sequence containsan increasing pair)

6. Well founded order: Preorder + (No infinite decreasingsequences)

Quiz: 1. Can Transitivity + Symmetry indicate Reflexivity?2. Give examples of these orders.

Page 39: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Inductions

I Ordinary induction on natural numbersIf P(0)and for all i , P(i) implies P(i + 1),then P(n) holds for all n.

I Complete induction on natural numbersIf, for each natural number k,

given P(i) for all i < kwe can show P(k)

then P(n) holds for all n.

Page 40: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Outline

Introduction

Preliminaries

Untyped arithmetic expressionsAbstract syntaxInduction on termsSemantics

BooleansNumbers and Booleans

Page 41: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Untyped systems

I Untyped arithmetic expressions

I Untyped λ-calculus

Page 42: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Introduction

t ::= true constant truefalse constant falseif t then t else t conditional0 constant zerosucc t successorpred t predecessoriszero t zero test

I BNF grammar

I t is metavariable.

I For simplicity, we use arabic numbers, e.g. 3 stands for(succ (succ (succ 0)))

I Currently, if (succ 0) then true else (pred 0) is a validterm.

Page 43: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Other ways to give syntax definition

The set of terms is the smallest set T such thatI Inductively.

I {true, false, 0} ⊆ T ;I if t1 ∈ T , then {succ t1, pred t1, iszero t1} ⊆ T ;I if t1, t2, t3 ∈ T , then if t1 then t2 else t3 ∈ T

I By inference rules

true ∈ T false ∈ T 0 ∈ T

t1 ∈ T

succ t1 ∈ T

t1 ∈ T

pred t1 ∈ T

t1 ∈ T

iszero t1 ∈ T

t1, t2, t3 ∈ T

if t1 then t2 else t3 ∈ T

Page 44: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Other ways to give syntax definition, cont’d

I Concretely.For each natural number i , define Si as follows:

S0 = ∅Si+1 = {true, false, 0}

∪{succ t1, pred t1, iszero t1 | t1 ∈ Si}∪{if t1 then t2 else t3 | t1, t2, t3 ∈ Si}

S =⋃

i Si

Lemma. S = T.Quiz. What if we change the concrete definition of S to

S0 = {true, false, 0}Si+1 = {succ t1, pred t1, iszero t1 | t1 ∈ Si}

∪{if t1 then t2 else t3 | t1, t2, t3 ∈ Si}

Page 45: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Other ways to give syntax definition, cont’d

I Concretely.For each natural number i , define Si as follows:

S0 = ∅Si+1 = {true, false, 0}

∪{succ t1, pred t1, iszero t1 | t1 ∈ Si}∪{if t1 then t2 else t3 | t1, t2, t3 ∈ Si}

S =⋃

i Si

Lemma. S = T.Quiz. What if we change the concrete definition of S to

S0 = {true, false, 0}Si+1 = {succ t1, pred t1, iszero t1 | t1 ∈ Si}

∪{if t1 then t2 else t3 | t1, t2, t3 ∈ Si}

Page 46: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Inductive structure

For any t ∈ T , one of three things must be true about t:

1. t is constant

2. t has form succ t1, pred t1, or iszero t1

3. t has form if t1 then t2 else t3.

Two ways to use this observation: inductive definition andinductive proof.

Page 47: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Inductive defintion

consts(true) = {true}consts(false) = {false}

consts(0) = {0}consts(succ t1) = consts(t1)consts(pred t1) = consts(t1)

consts(iszero t1) = consts(t1)consts(if t1 then t2 else t3) = consts(t1) ∪ consts(t2) ∪ consts(t3)

Quiz. 1. Give an inductive definition of size, which is the size ofthe syntax tree of a term t.2. Give an inductive definition of depth, which is the height of thesyntax tree of a term t.

Page 48: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Inductive defintion

consts(true) = {true}consts(false) = {false}

consts(0) = {0}consts(succ t1) = consts(t1)consts(pred t1) = consts(t1)

consts(iszero t1) = consts(t1)consts(if t1 then t2 else t3) = consts(t1) ∪ consts(t2) ∪ consts(t3)

Quiz. 1. Give an inductive definition of size, which is the size ofthe syntax tree of a term t.2. Give an inductive definition of depth, which is the height of thesyntax tree of a term t.

Page 49: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

size(true) = 1size(false) = 1

size(0) = 1size(succ t1) = size(t1) + 1size(pred t1) = size(t1) + 1

size(iszero t1) = size(t1) + 1size(if t1 then t2 else t3) = size(t1) + size(t2) + size(t3) + 1

Lemma. |consts(t)| ≤ size(t).Proof. By induction on the structure of t.

Page 50: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Principles of induction on terms

I Induction on depth:If, for each term s,

given P(r) for all r such that depth(r) < depth(s),we can show P(s),

then P(t) holds for all t.

I Induction on size:If, for each term s,

given P(r) for all r such that size(r) < size(s),we can show P(s),

then P(t) holds for all t.

I Structural Induction:If, for each term s,

given P(r) for all immediate subterms r of s,we can show P(s),

then P(t) holds for all t.

Page 51: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Outline

Introduction

Preliminaries

Untyped arithmetic expressionsAbstract syntaxInduction on termsSemantics

BooleansNumbers and Booleans

Page 52: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Semantics of languages

I Operational semantics. It specifies the behavior of PL bydefining an abstract machine.

I Denotational semantics. The meaning of a term is taken to besome mathematical object (a number or a function).

I Axiomatic semantics. It takes the laws themselves as thedefinition of the language.

Page 53: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

A toy language – Booleans

Syntax

t ::= termstrue constant truefalse constant falseif t then t else t conditional

v ::= valuestrue true valuefalse false value

Page 54: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Evaluation rules for Booleans

Evaluation

E-IfTrueif true then t2 else t3 −→ t2

E-IfFalseif false then t2 else t3 −→ t3

E-Ift1 −→ t ′1

if t1 then t2 else t3 −→ if t ′1 then t2 else t3

E-IfTrue and E-IfFalse are also called computation rules andE-If is called congruence rule.Quiz. Evaluate the following terms:

I true

I

if (if false then false else false) then true else false

Page 55: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Evaluation rules for Booleans

Evaluation

E-IfTrueif true then t2 else t3 −→ t2

E-IfFalseif false then t2 else t3 −→ t3

E-Ift1 −→ t ′1

if t1 then t2 else t3 −→ if t ′1 then t2 else t3

E-IfTrue and E-IfFalse are also called computation rules andE-If is called congruence rule.

Quiz. Evaluate the following terms:

I true

I

if (if false then false else false) then true else false

Page 56: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Evaluation rules for Booleans

Evaluation

E-IfTrueif true then t2 else t3 −→ t2

E-IfFalseif false then t2 else t3 −→ t3

E-Ift1 −→ t ′1

if t1 then t2 else t3 −→ if t ′1 then t2 else t3

E-IfTrue and E-IfFalse are also called computation rules andE-If is called congruence rule.Quiz. Evaluate the following terms:

I true

I

if (if false then false else false) then true else false

Page 57: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Derivation tree of One-step evaluation

sdef= if true then false else false

tdef= if s then false else false

udef= if false then false else false

E-IfE-If

E-IfTrues −→ false

t −→ uif t then false else false −→ if u then false else false

Theorem 3.5.4 [Determinacy of one-step evaluation]: Ift −→ t ′ and t −→ t ′′, then t ′ = t ′′.Proof. By induction on the depth of the derivation tree.

Page 58: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Derivation tree of One-step evaluation

sdef= if true then false else false

tdef= if s then false else false

udef= if false then false else false

E-IfE-If

E-IfTrues −→ false

t −→ uif t then false else false −→ if u then false else false

Theorem 3.5.4 [Determinacy of one-step evaluation]: Ift −→ t ′ and t −→ t ′′, then t ′ = t ′′.

Proof. By induction on the depth of the derivation tree.

Page 59: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Derivation tree of One-step evaluation

sdef= if true then false else false

tdef= if s then false else false

udef= if false then false else false

E-IfE-If

E-IfTrues −→ false

t −→ uif t then false else false −→ if u then false else false

Theorem 3.5.4 [Determinacy of one-step evaluation]: Ift −→ t ′ and t −→ t ′′, then t ′ = t ′′.Proof. By induction on the depth of the derivation tree.

Page 60: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Normal form and multi-step evaluation

I A term t is in normal form if no evaluation rule can appliy toit.

Theorem 3.5.7: Every value is in normal form.Theorem 3.5.8: If t is in normal form, then it is a value.

I The multi-step evaluation relation −→∗ is the reflexive,transitive closure of −→.

Theorem 3.5.11 [Uniqueness of normal forms]: If t −→∗ u andt −→∗ u′ where u, u′ are normal forms, then u = u′.Theorem 3.5.12 [Termination of evaluation]: For every term tthere is some normal form u such that t −→∗ u.

Page 61: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Normal form and multi-step evaluation

I A term t is in normal form if no evaluation rule can appliy toit.

Theorem 3.5.7: Every value is in normal form.Theorem 3.5.8: If t is in normal form, then it is a value.

I The multi-step evaluation relation −→∗ is the reflexive,transitive closure of −→.

Theorem 3.5.11 [Uniqueness of normal forms]: If t −→∗ u andt −→∗ u′ where u, u′ are normal forms, then u = u′.Theorem 3.5.12 [Termination of evaluation]: For every term tthere is some normal form u such that t −→∗ u.

Page 62: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Arithmatic Expression

Syntax

t ::= terms· · ·0 constant zerosucc t successorpred t predecessoriszero t zero test

v ::= values· · ·nv numeric value

nv ::= numeric values0 zero valuesucc nv successor value

Page 63: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Evaluation rulesQuiz. Give the definition of evaluation rules to guarantee

[Determinacy of one-step evaluation]:If t −→ t ′ and t −→ t ′′, then t ′ = t ′′.

Evaluation

E-PredZeropred 0 −→ 0

E-IszeroZeroiszero 0 −→ true

E-PredSuccpred (succ nv) −→ nv

E-IszeroSucciszero (succ nv) −→ false

E-Predt1 −→ t ′1

pred t1 −→ pred t ′1E-Iszero

t1 −→ t ′1iszero t1 −→ iszero t ′1

E-Succt1 −→ t ′1

succ t1 −→ succ t ′1

Page 64: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Evaluation rulesQuiz. Give the definition of evaluation rules to guarantee

[Determinacy of one-step evaluation]:If t −→ t ′ and t −→ t ′′, then t ′ = t ′′.

Evaluation

E-PredZeropred 0 −→ 0

E-IszeroZeroiszero 0 −→ true

E-PredSuccpred (succ nv) −→ nv

E-IszeroSucciszero (succ nv) −→ false

E-Predt1 −→ t ′1

pred t1 −→ pred t ′1E-Iszero

t1 −→ t ′1iszero t1 −→ iszero t ′1

E-Succt1 −→ t ′1

succ t1 −→ succ t ′1

Page 65: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Normal form and stuckness

I What if we change E-PredSucc to pred (succ t) −→ t?Does it still satisfy [Determinacy of one-step transition]?

I Note there are meaningless terms, such asif 0 then (succ true) else (iszeoro false).

I A term t is stuck if it is in normal form but not a value.

Page 66: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Normal form and stuckness

I What if we change E-PredSucc to pred (succ t) −→ t?Does it still satisfy [Determinacy of one-step transition]?

I Note there are meaningless terms, such asif 0 then (succ true) else (iszeoro false).

I A term t is stuck if it is in normal form but not a value.

Page 67: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Normal form and stuckness

I What if we change E-PredSucc to pred (succ t) −→ t?Does it still satisfy [Determinacy of one-step transition]?

I Note there are meaningless terms, such asif 0 then (succ true) else (iszeoro false).

I A term t is stuck if it is in normal form but not a value.

Page 68: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Conclusion

I Types are very important for PLs.

I This course will give a full view of type systems from thesimplest one to full-fledged one.

I Fundamental concepts for PLs:I syntax, defined inductively, concretely, ...I inductive proofs are very important for PLs, especially,

structural induction.I operational semantics plays more and more important roles.

We define evaluation rules by using operational transitions.

I Properties such as [Determinacy of one-step evaluation],[Uniqueness of normal forms], and [Termination] areimportant for a good language design.

Page 69: Types and Programming Languages - SJTUbasics.sjtu.edu.cn/~xiaojuan/tapl2016/files/lec1.pdf · Ordered sets A binary relation R on a set S is I Re exive: 8x 2S:x R x. I Transitive:

Homework

I 3.3.4, 3.5.10, 3.5.13, 3.5.18.

I Install OCaml and get familiar with this language.http://ocaml.org/