COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types...

78
COMP3012/G53CMP: Lecture 8 Contextual Analysis: Types and Type Systems I Henrik Nilsson University of Nottingham, UK COMP3012/G53CMP: Lecture 8 – p.1/29

Transcript of COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types...

Page 1: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

COMP3012/G53CMP: Lecture 8Contextual Analysis: Types and Type Systems I

Henrik Nilsson

University of Nottingham, UK

COMP3012/G53CMP: Lecture 8 – p.1/29

Page 2: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

This Lecture

• Types and Type Systems

• Language safety

• Achieving safety through types

- Introduction: relation between static anddynamic semantics

- Operational dynamic semantics of a smallexample language.

Much of this lecture follows parts of the first fewchapters of B. C. Pierce 2002 Types andProgramming Languages closely.

COMP3012/G53CMP: Lecture 8 – p.2/29

Page 3: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (1)

Type systems are an example of lightweightformal methods:

COMP3012/G53CMP: Lecture 8 – p.3/29

Page 4: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (1)

Type systems are an example of lightweightformal methods:

• highly automated

COMP3012/G53CMP: Lecture 8 – p.3/29

Page 5: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (1)

Type systems are an example of lightweightformal methods:

• highly automated

• but with limited expressive power.

COMP3012/G53CMP: Lecture 8 – p.3/29

Page 6: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (1)

Type systems are an example of lightweightformal methods:

• highly automated

• but with limited expressive power.

A plausible definition (Pierce):

A type system is a tractable syntacticmethod for proving the absence of certainprogram behaviors by classifying phrasesaccording to the kinds of values theycompute.

COMP3012/G53CMP: Lecture 8 – p.3/29

Page 7: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (2)

Notes on the definition:

COMP3012/G53CMP: Lecture 8 – p.4/29

Page 8: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (2)

Notes on the definition:

• Static checking implied as the goal is toprove absence of certain errors.

COMP3012/G53CMP: Lecture 8 – p.4/29

Page 9: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (2)

Notes on the definition:

• Static checking implied as the goal is toprove absence of certain errors.

• Done by classifying syntactic phrases (orterms) according to the kinds of value theycompute: a type system computes a staticapproximation of the run-time behaviour.

COMP3012/G53CMP: Lecture 8 – p.4/29

Page 10: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (3)

Example: if known that two program fragmentsexp1 and exp2 compute integers (classification),then it is safe to add those numbers together(absence of errors):

exp1 + exp2

COMP3012/G53CMP: Lecture 8 – p.5/29

Page 11: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (3)

Example: if known that two program fragmentsexp1 and exp2 compute integers (classification),then it is safe to add those numbers together(absence of errors):

exp1 + exp2

Also known that the result is an integer. Whilenot known exactly which integers are involved, atleast known they are integers and nothing else(static approximation).

COMP3012/G53CMP: Lecture 8 – p.5/29

Page 12: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (4)

• “Dynamically typed” languages do not have atype system according to this definition; theyshould really be called dynamically checked.

COMP3012/G53CMP: Lecture 8 – p.6/29

Page 13: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (4)

• “Dynamically typed” languages do not have atype system according to this definition; theyshould really be called dynamically checked.

Example. In a dynamically checked language,exp1 + exp2 would be evaluated as follows:

• Evaluate exp1 and exp2

• Add results together in a manner dependingon their types (integer addition, floating pointaddition, . . . ), or signal error if not possible.

COMP3012/G53CMP: Lecture 8 – p.6/29

Page 14: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (5)

• A type system is necessarily conservative:some well-behaved programs will be rejected.

COMP3012/G53CMP: Lecture 8 – p.7/29

Page 15: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (5)

• A type system is necessarily conservative:some well-behaved programs will be rejected.

For example, typically

if complex test then S else type error

will be rejected as ill-typed, even if complex test

actually always evaluates to true because that

cannot be automatically proved in general.

COMP3012/G53CMP: Lecture 8 – p.7/29

Page 16: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (6)

• A type system checks for certain kinds of badprogram behaviour, or run-time errors.Exactly which depends on the type systemand the language design.

COMP3012/G53CMP: Lecture 8 – p.8/29

Page 17: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (6)

• A type system checks for certain kinds of badprogram behaviour, or run-time errors.Exactly which depends on the type systemand the language design.

For example: current main-stream type systemstypically

do check that arithmetic operations onlyare done on numbers

COMP3012/G53CMP: Lecture 8 – p.8/29

Page 18: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (6)

• A type system checks for certain kinds of badprogram behaviour, or run-time errors.Exactly which depends on the type systemand the language design.

For example: current main-stream type systemstypically

do check that arithmetic operations onlyare done on numbersdo not check that the second operand ofdivision is not zero, that array indices arewithin bounds.

COMP3012/G53CMP: Lecture 8 – p.8/29

Page 19: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Types and Type Systems (7)

• The safety or soundness of a type systemmust be judged with respect to its own set ofrun-time errors.

COMP3012/G53CMP: Lecture 8 – p.9/29

Page 20: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Language Safety (1)

Language safety is a contentious notion. Apossible definition (Pierce):

A safe language is one that protects itsown abstractions.

COMP3012/G53CMP: Lecture 8 – p.10/29

Page 21: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Language Safety (1)

Language safety is a contentious notion. Apossible definition (Pierce):

A safe language is one that protects itsown abstractions.

For example: a Java object should behave as anobject; e.g. it would be bad if it was destroyed bycreation of some other object.

COMP3012/G53CMP: Lecture 8 – p.10/29

Page 22: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Language Safety (1)

Language safety is a contentious notion. Apossible definition (Pierce):

A safe language is one that protects itsown abstractions.

For example: a Java object should behave as anobject; e.g. it would be bad if it was destroyed bycreation of some other object.

Other examples: lexical scope rules, visibility at-

tributes (public, protected, . . . ).

COMP3012/G53CMP: Lecture 8 – p.10/29

Page 23: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Language Safety (2)

• Language safety not the same as statictyping: safety can be achieved through statictyping and/or dynamic run-time checks.

COMP3012/G53CMP: Lecture 8 – p.11/29

Page 24: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Language Safety (2)

• Language safety not the same as statictyping: safety can be achieved through statictyping and/or dynamic run-time checks.

• Scheme is a dynamically checked safe language.

COMP3012/G53CMP: Lecture 8 – p.11/29

Page 25: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Language Safety (2)

• Language safety not the same as statictyping: safety can be achieved through statictyping and/or dynamic run-time checks.

• Scheme is a dynamically checked safe language.

• Even statically typed languages usually usesome dynamic checks; e.g.:

- checking of array bounds

- down-casting (e.g. Java)

- checking for division by zero

- pattern-matching failure

COMP3012/G53CMP: Lecture 8 – p.11/29

Page 26: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Language Safety (3)

Some examples of statically and dynamicallychecked safe and unsafe high-level languages:

Statically chkd Dynamically chkd

Safe ML, Haskell,Java

Lisp,Scheme,Perl, Python,Postscript

Unsafe C, C++Certain Basicdialects

COMP3012/G53CMP: Lecture 8 – p.12/29

Page 27: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (1)

• Detecting errors early

COMP3012/G53CMP: Lecture 8 – p.13/29

Page 28: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (1)

• Detecting errors earlyPrograms in richly typed languages often "justwork". Why?

COMP3012/G53CMP: Lecture 8 – p.13/29

Page 29: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (1)

• Detecting errors earlyPrograms in richly typed languages often "justwork". Why?

- Simple, common mistakes very often leadto type inconsistencies.

COMP3012/G53CMP: Lecture 8 – p.13/29

Page 30: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (1)

• Detecting errors earlyPrograms in richly typed languages often "justwork". Why?

- Simple, common mistakes very often leadto type inconsistencies.

- Programmers forced to think a bit harder.

COMP3012/G53CMP: Lecture 8 – p.13/29

Page 31: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (1)

• Detecting errors earlyPrograms in richly typed languages often "justwork". Why?

- Simple, common mistakes very often leadto type inconsistencies.

- Programmers forced to think a bit harder.

• Enforcing disciplined programming

COMP3012/G53CMP: Lecture 8 – p.13/29

Page 32: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (1)

• Detecting errors earlyPrograms in richly typed languages often "justwork". Why?

- Simple, common mistakes very often leadto type inconsistencies.

- Programmers forced to think a bit harder.

• Enforcing disciplined programmingType systems are the backbone of

- Modules

- Classes

COMP3012/G53CMP: Lecture 8 – p.13/29

Page 33: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (2)

• Documentation

COMP3012/G53CMP: Lecture 8 – p.14/29

Page 34: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (2)

• Documentation

- Unlike comments, type signatures willalways remain current.

COMP3012/G53CMP: Lecture 8 – p.14/29

Page 35: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (2)

• Documentation

- Unlike comments, type signatures willalways remain current.

• Efficiency

COMP3012/G53CMP: Lecture 8 – p.14/29

Page 36: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (2)

• Documentation

- Unlike comments, type signatures willalways remain current.

• Efficiency

- First use of types in computing was todistinguish between integer and floatingpoint numbers.

COMP3012/G53CMP: Lecture 8 – p.14/29

Page 37: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Advantages of Typing (2)

• Documentation

- Unlike comments, type signatures willalways remain current.

• Efficiency

- First use of types in computing was todistinguish between integer and floatingpoint numbers.

- Elimination of many of the dynamic checksthat otherwise would have been needed toguarantee safety.

COMP3012/G53CMP: Lecture 8 – p.14/29

Page 38: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Disadvantages of Typing

Type systems sometimes do get in the way:

COMP3012/G53CMP: Lecture 8 – p.15/29

Page 39: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Disadvantages of Typing

Type systems sometimes do get in the way:

• Simple things can become quite complicatedif have to work around the type system.(Example: heterogeneous lists in Haskell)

COMP3012/G53CMP: Lecture 8 – p.15/29

Page 40: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Disadvantages of Typing

Type systems sometimes do get in the way:

• Simple things can become quite complicatedif have to work around the type system.(Example: heterogeneous lists in Haskell)

• Sometimes it becomes impossible to do whatone wants to do, at least not without loss ofefficiency.

COMP3012/G53CMP: Lecture 8 – p.15/29

Page 41: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Disadvantages of Typing

Type systems sometimes do get in the way:

• Simple things can become quite complicatedif have to work around the type system.(Example: heterogeneous lists in Haskell)

• Sometimes it becomes impossible to do whatone wants to do, at least not without loss ofefficiency.

Increasingly sophisticated type systems, whichkeep track of more invariants, can help.

COMP3012/G53CMP: Lecture 8 – p.15/29

Page 42: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Disadvantages of Typing

Type systems sometimes do get in the way:

• Simple things can become quite complicatedif have to work around the type system.(Example: heterogeneous lists in Haskell)

• Sometimes it becomes impossible to do whatone wants to do, at least not without loss ofefficiency.

Increasingly sophisticated type systems, whichkeep track of more invariants, can help.

But that can make the type systems harder tounderstand and less automatic.

COMP3012/G53CMP: Lecture 8 – p.15/29

Page 43: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Static and Dynamic Semantics

In summary:

• A type system statically proves propertiesabout the dynamic behaviour of a programs.

• To make precise exactly what theseproperties are, and formally prove that a typesystem achieves its goals, both the

- static semantics

- dynamic semantics

must first be formalized.

COMP3012/G53CMP: Lecture 8 – p.16/29

Page 44: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Example Language: Abstract Syntax

Example language. (Will be extended later.)

t → terms:

true constant true

| false constant false

| if t then t else t conditional

| 0 constant zero

| succ t successor

| pred t predecessor

| iszero t zero test

COMP3012/G53CMP: Lecture 8 – p.17/29

Page 45: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values (1)

The values of a language are a subset of theterms that are possible results of evaluation.

COMP3012/G53CMP: Lecture 8 – p.18/29

Page 46: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values (1)

The values of a language are a subset of theterms that are possible results of evaluation.

I.e. the values are the meaning of termsaccording to the dynamic semantics of thelanguage.

COMP3012/G53CMP: Lecture 8 – p.18/29

Page 47: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values (1)

The values of a language are a subset of theterms that are possible results of evaluation.

I.e. the values are the meaning of termsaccording to the dynamic semantics of thelanguage.

• The evaluation rules are going to be such thatno evaluation is possible for values.

COMP3012/G53CMP: Lecture 8 – p.18/29

Page 48: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values (1)

The values of a language are a subset of theterms that are possible results of evaluation.

I.e. the values are the meaning of termsaccording to the dynamic semantics of thelanguage.

• The evaluation rules are going to be such thatno evaluation is possible for values.

• A term to which no evaluation rule applies is anormal form.

COMP3012/G53CMP: Lecture 8 – p.18/29

Page 49: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values (1)

The values of a language are a subset of theterms that are possible results of evaluation.

I.e. the values are the meaning of termsaccording to the dynamic semantics of thelanguage.

• The evaluation rules are going to be such thatno evaluation is possible for values.

• A term to which no evaluation rule applies is anormal form.

• All values are normal forms.COMP3012/G53CMP: Lecture 8 – p.18/29

Page 50: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values (2)

v → values:

true true value

| false false value

| nv numeric value

nv → numeric values:

0 zero value

| succ nv successor value

COMP3012/G53CMP: Lecture 8 – p.19/29

Page 51: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (1)

t −→ t′ is an evaluation relation on terms. Read:

t evaluates to t′ in one step.

COMP3012/G53CMP: Lecture 8 – p.20/29

Page 52: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (1)

t −→ t′ is an evaluation relation on terms. Read:

t evaluates to t′ in one step.

The evaluation relation constitutes an operational(dynamic) semantics for the example language.

COMP3012/G53CMP: Lecture 8 – p.20/29

Page 53: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (1)

t −→ t′ is an evaluation relation on terms. Read:

t evaluates to t′ in one step.

The evaluation relation constitutes an operational(dynamic) semantics for the example language.

if true then t2 else t3 −→ t2 (E-IFTRUE)

COMP3012/G53CMP: Lecture 8 – p.20/29

Page 54: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (1)

t −→ t′ is an evaluation relation on terms. Read:

t evaluates to t′ in one step.

The evaluation relation constitutes an operational(dynamic) semantics for the example language.

if true then t2 else t3 −→ t2 (E-IFTRUE)

if false then t2 else t3 −→ t3 (E-IFFALSE)

COMP3012/G53CMP: Lecture 8 – p.20/29

Page 55: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (1)

t −→ t′ is an evaluation relation on terms. Read:

t evaluates to t′ in one step.

The evaluation relation constitutes an operational(dynamic) semantics for the example language.

if true then t2 else t3 −→ t2 (E-IFTRUE)

if false then t2 else t3 −→ t3 (E-IFFALSE)

t1 −→ t′1

if t1 then t2 else t3−→ if t′

1then t2 else t3

(E-IF)

COMP3012/G53CMP: Lecture 8 – p.20/29

Page 56: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (1)

• Recall that a mathematical relation can beunderstood as a (possibly infinite) set of pairsof “related things”. For example:

{(1, 1), (1, 2), (1, 3), (2, 2), (2, 3)} ⊆ (≤)

COMP3012/G53CMP: Lecture 8 – p.21/29

Page 57: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (1)

• Recall that a mathematical relation can beunderstood as a (possibly infinite) set of pairsof “related things”. For example:

{(1, 1), (1, 2), (1, 3), (2, 2), (2, 3)} ⊆ (≤)

• The idea of our “one step evaluation relation”is that the “related things” are terms and thatone term is related to another iff the firstevaluates to the second in one step.

COMP3012/G53CMP: Lecture 8 – p.21/29

Page 58: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (1)

• Recall that a mathematical relation can beunderstood as a (possibly infinite) set of pairsof “related things”. For example:

{(1, 1), (1, 2), (1, 3), (2, 2), (2, 3)} ⊆ (≤)

• The idea of our “one step evaluation relation”is that the “related things” are terms and thatone term is related to another iff the firstevaluates to the second in one step.

• For example:

(if true then succ 0 else 0,succ 0) ∈ (−→)

COMP3012/G53CMP: Lecture 8 – p.21/29

Page 59: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (2)

• But the evaluation relation is infinite . . .

COMP3012/G53CMP: Lecture 8 – p.22/29

Page 60: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (2)

• But the evaluation relation is infinite . . .so we can’t enumerate all pairs.

COMP3012/G53CMP: Lecture 8 – p.22/29

Page 61: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (2)

• But the evaluation relation is infinite . . .so we can’t enumerate all pairs.

• Instead, (schematic) inference rules areused to specify relations:

Premise1 Premise2 . . . Premisen

Conclusion

(en.wikipedia.org/wiki/Rule_of_inference)

COMP3012/G53CMP: Lecture 8 – p.22/29

Page 62: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (2)

• But the evaluation relation is infinite . . .so we can’t enumerate all pairs.

• Instead, (schematic) inference rules areused to specify relations:

Premise1 Premise2 . . . Premisen

Conclusion

(en.wikipedia.org/wiki/Rule_of_inference)

• If there are no premises, the line is often omitted:

Conclusion or Conclusion

COMP3012/G53CMP: Lecture 8 – p.22/29

Page 63: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (3)

• Schematic means that universally quantifiedvariables are allowed in the rules. For example:

if true then t2 else t3 −→ t2

COMP3012/G53CMP: Lecture 8 – p.23/29

Page 64: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (3)

• Schematic means that universally quantifiedvariables are allowed in the rules. For example:

if true then t2 else t3 −→ t2

• Such a rule schema actually stands for aninfinite set of rules:

. . .

if true then 0 else 0 −→ 0

if true then succ 0 else 0 −→ succ 0

if true then true else false −→ true

. . .COMP3012/G53CMP: Lecture 8 – p.23/29

Page 65: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Evaluation Relation??? (4)

• The domain of a variable is often specified bynaming conventions. E.g. the name of avariable may indicate some specific syntacticcategory such as t, v, or nv :

t1 −→ t′1

if t1 then t2 else t3−→ if t′

1then t2 else t3

pred (succ nv 1) −→ nv 1

COMP3012/G53CMP: Lecture 8 – p.24/29

Page 66: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (2)

t1 −→ t′1

succ t1 −→ succ t′1

(E-SUCC)

COMP3012/G53CMP: Lecture 8 – p.25/29

Page 67: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (2)

t1 −→ t′1

succ t1 −→ succ t′1

(E-SUCC)

pred 0 −→ 0 (E-PREDZERO)

COMP3012/G53CMP: Lecture 8 – p.25/29

Page 68: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (2)

t1 −→ t′1

succ t1 −→ succ t′1

(E-SUCC)

pred 0 −→ 0 (E-PREDZERO)

pred (succ nv 1) −→ nv 1 (E-PREDSUCC)

COMP3012/G53CMP: Lecture 8 – p.25/29

Page 69: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (2)

t1 −→ t′1

succ t1 −→ succ t′1

(E-SUCC)

pred 0 −→ 0 (E-PREDZERO)

pred (succ nv 1) −→ nv 1 (E-PREDSUCC)

t1 −→ t′1

pred t1 −→ pred t′1

(E-PRED)

COMP3012/G53CMP: Lecture 8 – p.25/29

Page 70: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (3)

iszero 0 −→ true (E-ISZEROZERO)

COMP3012/G53CMP: Lecture 8 – p.26/29

Page 71: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (3)

iszero 0 −→ true (E-ISZEROZERO)

iszero (succ nv 1) −→ false (E-ISZEROSUCC)

COMP3012/G53CMP: Lecture 8 – p.26/29

Page 72: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (3)

iszero 0 −→ true (E-ISZEROZERO)

iszero (succ nv 1) −→ false (E-ISZEROSUCC)

t1 −→ t′1

iszero t1 −→ iszero t′1

(E-ISZERO)

COMP3012/G53CMP: Lecture 8 – p.26/29

Page 73: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

One Step Evaluation Relation (4)

Let’s evaluate some terms according to −→:

• pred (pred 0)

• if (iszero (pred (succ 0)))then (pred 0) else (succ 0)

• if 0 then 0 else 0

(On the whiteboard.)

COMP3012/G53CMP: Lecture 8 – p.27/29

Page 74: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values and Stuck Terms

Note that:

• Values cannot be evaluated further. E.g.:

- true

- 0

- succ (succ 0)

COMP3012/G53CMP: Lecture 8 – p.28/29

Page 75: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Values and Stuck Terms

Note that:

• Values cannot be evaluated further. E.g.:

- true

- 0

- succ (succ 0)

• Certain “obviously nonsensical” states arestuck: the term cannot be evaluated further,but it is not a value. For example:

if 0 then pred 0 else 0

COMP3012/G53CMP: Lecture 8 – p.28/29

Page 76: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Stuckness and Run-Time Errors

• We let the notion of getting stuck modelrun-time errors.

COMP3012/G53CMP: Lecture 8 – p.29/29

Page 77: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Stuckness and Run-Time Errors

• We let the notion of getting stuck modelrun-time errors.

• The goal of a type system is thus toguarantee that a program never gets stuck!

COMP3012/G53CMP: Lecture 8 – p.29/29

Page 78: COMP3012/G53CMP: Lecture 8 - cs.nott.ac.ukpsznhn/COMP3012/LectureNotes... · This Lecture • Types and Type Systems • Language safety • Achieving safety through types-Introduction:

Stuckness and Run-Time Errors

• We let the notion of getting stuck modelrun-time errors.

• The goal of a type system is thus toguarantee that a program never gets stuck!

These ideas will be made more precise next time.

COMP3012/G53CMP: Lecture 8 – p.29/29