lecture 0 an introduction to the fundamentals of...

46
Starter Questions Feel free to discuss these with your neighbour: Are there things computers cannot do? How do we know what a program will do without running it? What does i = i++ + 1; do in C? Can I tell how long a program will run for before running it? Can I tell if it will ever stop running? What did Part 1 have to do with computation? Why should I come to these lectures? Giles Reger Lecture 0 March 2017 1 / 48

Upload: others

Post on 28-Mar-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Starter Questions

Feel free to discuss these with your neighbour:

Are there things computers cannot do?

How do we know what a program will do without running it?

What does i = i++ + 1; do in C?

Can I tell how long a program will run for before running it?

Can I tell if it will ever stop running?

What did Part 1 have to do with computation?

Why should I come to these lectures?

Giles Reger Lecture 0 March 2017 1 / 48

Page 2: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Lecture 0An Introduction to the Fundamentals of Computation

(Part II)COMP11212

Giles Reger

March 2017

Giles Reger Lecture 0 March 2017 2 / 48

Page 3: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

What are we doing?

We will be looking at three topics:

1 Computability

2 Correctness

3 Complexity

This lecture will start by introducing and motivating those topics.

Giles Reger Lecture 0 March 2017 3 / 48

Page 4: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

But Why?

It is important to note that we are doing Theoretical Computer Science!

Part 1 contained practical tools that every computer scientist should know

Part 2 includes one such tool (complexity analysis) and a few abstractconcepts that every computer scientist should know (with some practicalapplications).

Giles Reger Lecture 0 March 2017 4 / 48

Page 5: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

What is a Computer?

Giles Reger Lecture 0 March 2017 5 / 48

Page 6: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Giles Reger Lecture 0 March 2017 5 / 48

Page 7: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Giles Reger Lecture 0 March 2017 5 / 48

Page 8: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Do we need a Computer forComputation?

Giles Reger Lecture 0 March 2017 5 / 48

Page 9: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

What is Computation?

We can ask this from two perspectives:

1 What is a computer doing?

2 What is happening when we compute, for example, 1 + 2?

At a suitable level of abstraction, the answer is manipulating symbols

It is important to point out that there is no universally accepted,well-defined, succinct description of what computation is

Computability predates Modern Computers

The concept of computability was introduced by Alan Turing in 1936

The Manchester Baby ran its first program on 21 June 1948

Giles Reger Lecture 0 March 2017 6 / 48

Page 10: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Three Things

1 Computation:

2 Function:

3 Program:

Giles Reger Lecture 0 March 2017 7 / 48

Page 11: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Three Things

1 Computation: The active process of carrying out operations

2 Function:

3 Program:

Giles Reger Lecture 0 March 2017 7 / 48

Page 12: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Three Things

1 Computation: The active process of carrying out operations

2 Function: A mathematical object that transforms inputs to outputs

3 Program:

Giles Reger Lecture 0 March 2017 7 / 48

Page 13: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Three Things

1 Computation: The active process of carrying out operations

2 Function: A mathematical object that transforms inputs to outputs

3 Program: A static description of the operations to perform

Giles Reger Lecture 0 March 2017 7 / 48

Page 14: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Three Things

1 Computation: The active process of carrying out operations

2 Function: A mathematical object that transforms inputs to outputs

3 Program: A static description of the operations to perform

To compute a function we run a program

Giles Reger Lecture 0 March 2017 7 / 48

Page 15: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Three Things

1 Computation: The active process of carrying out operations

2 Function: A mathematical object that transforms inputs to outputs

3 Program: A static description of the operations to perform

To compute a function we run a program

To make programs unambiguous we need a model of computation.

Giles Reger Lecture 0 March 2017 7 / 48

Page 16: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

The while Language

We will use a simple programming language as our model of computation

We will not be using Turing Machines, but they will come up.

The while language is a very simple language that is not usable as a realprogramming language.

But in its favour:

It contains the core familiar constructs from an imperative language

It is simple enough to describe and use in an undergraduate course

It is equally expressive as Java, Python, or Turing Machines

Giles Reger Lecture 0 March 2017 8 / 48

Page 17: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

A Sample Program in while

Programs in while look like this

r := x ;d := 0 ;whi le y ≤ r do ( d := d+1; r := r−y )

This program computes the function

f (x , y) = (d , r) = (x div y , x mod y)

whenever y > 0 ∧ x ≥ 0

Giles Reger Lecture 0 March 2017 9 / 48

Page 18: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Questions

1 What functions can we compute i.e. are there functions that cannotbe computed by any program?

2 Does a program P correctly compute a function f ?

3 Given programs P1 and P2 that both compute a function f , whichone is the better program?

Giles Reger Lecture 0 March 2017 10 / 48

Page 19: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Questions

1 What functions can we compute i.e. are there functions that cannotbe computed by any program? COMPUTABILITY

2 Does a program P correctly compute a function f ?

3 Given programs P1 and P2 that both compute a function f , whichone is the better program?

Giles Reger Lecture 0 March 2017 10 / 48

Page 20: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Questions

1 What functions can we compute i.e. are there functions that cannotbe computed by any program? COMPUTABILITY

2 Does a program P correctly compute a function f ? CORRECTNESS

3 Given programs P1 and P2 that both compute a function f , whichone is the better program?

Giles Reger Lecture 0 March 2017 10 / 48

Page 21: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Questions

1 What functions can we compute i.e. are there functions that cannotbe computed by any program? COMPUTABILITY

2 Does a program P correctly compute a function f ? CORRECTNESS

3 Given programs P1 and P2 that both compute a function f , whichone is the better program? COMPLEXITY

Giles Reger Lecture 0 March 2017 10 / 48

Page 22: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Questions

1 What functions can we compute i.e. are there functions that cannotbe computed by any program? COMPUTABILITY

2 Does a program P correctly compute a function f ? CORRECTNESS

3 Given programs P1 and P2 that both compute a function f , whichone is the better program? COMPLEXITY

The Three C’s of Computer Science

Giles Reger Lecture 0 March 2017 10 / 48

Page 23: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Computability

We will see:

How to prove that there are uncomputable functions

The famous Halting Problem as an example of an uncomputablefunction

That all sensible models of computation are equivalent, this is knownas the Church-Turing thesis

That such languages are Universal, they can implement each other

But why?

There is no point trying to write a program for an uncomputablefunction

The existence of uncomputable (and intractable) functions motivatesthe development of techniques to find approximate solutions, orpractically useful heuristics

Giles Reger Lecture 0 March 2017 11 / 48

Page 24: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Is it Correct?

1 pub l i c i n t f ( i n t [ ] a r r a y ){2 i n t l e n = a r r a y . l e n g t h ;3 i n t sum = 14 f o r ( i n t i =0; i<=l e n ; i ++){5 sum = sum∗ a r r a y [ i ] ;6 }7 return sum ;8 }

Giles Reger Lecture 0 March 2017 12 / 48

Page 25: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Is it Correct?

1 pub l i c i n t f ( i n t [ ] a r r a y ){2 i n t l e n = a r r a y . l e n g t h ;3 i n t sum = 1 ;4 f o r ( i n t i =0; i<l e n ; i ++){5 sum = sum∗ a r r a y [ i ] ;6 }7 return sum ;8 }

Giles Reger Lecture 0 March 2017 13 / 48

Page 26: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Is it Correct? Does this correctly compute the product ofthe values in the given array?

1 pub l i c i n t f ( i n t [ ] a r r a y ){2 i n t l e n = a r r a y . l e n g t h ;3 i n t p r o d u c t = 1 ;4 f o r ( i n t i =0; i<l e n ; i ++){5 p r o d u c t = p r o d u c t ∗ a r r a y [ i ] ;6 }7 return p r o d u c t ;8 }

Giles Reger Lecture 0 March 2017 14 / 48

Page 27: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Is it Correct? Does this correctly compute the product ofthe values in the given array?

1 pub l i c i n t f ( i n t [ ] a r r a y ){2 i n t l e n = a r r a y . l e n g t h ;3 i n t p r o d u c t = 1 ;4 f o r ( i n t i =0; i<l e n ; i ++){5 p r o d u c t = p r o d u c t ∗ a r r a y [ i ] ;6 }7 return p r o d u c t ;8 }

We could test the program, but we would theoretically need to considerevery possible input.

Instead, in this course we see how to prove correctness

Giles Reger Lecture 0 March 2017 14 / 48

Page 28: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Does this correctly compute the product. . .1 pub l i c i n t f ( i n t [ ] a r r a y ){2 i n t p r o d u c t =1; b o o l i s P r o d u c t = f a l s e ;3 do{4 p r o d u c t = random . n e x t I n t ( ) ;5 i s P r o d u c t=true ;6 i n t check = p r o d u c t ;7 f o r ( i n t i =0; i<a r r a y . l e n g t h ; i ++){8 i f ( p r o d u c t%a r r a y [ i ] != 0){9 i s P r o d u c t=f a l s e ; break ;

10 }11 check = check / a r r a y [ i ] ;12 }13 i s P r o d u c t = i s P r o d u c t && ( check ==1);14 }15 whi le ( ! i s P r o d u c t ) ;16 return p r o d u c t ;17 }

Giles Reger Lecture 0 March 2017 15 / 48

Page 29: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Correctness Summary

To answer the correctness question we need a specification

We need to treat termination separately

What I haven’t done is motivate this topic by referring to

Arianne 5 where integer overflow caused the loss of 370m USD

Intel Pentium 5 where a floating point bug cost Intel over 475m USD

Therac-25 where a radiation therapy machine caused at least 5 deaths

A 1983 Soviet nuclear early warning system bug almost causing WW3

Now I have

Giles Reger Lecture 0 March 2017 16 / 48

Page 30: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Which is Better? (they both correctly find the maximum)

pub l i c i n t max ( i n t [ ] a ){i n t max = a [ 0 ] ;i n t l e n = a . l e n g t h ;f o r ( i n t i =1; i<l e n ; i ++){

i f ( a [ i ]>max ){max = a [ i ] ;

}}return max ;

}

pub l i c i n t max ( i n t [ ] a ){i n t l e n = a . l e n g t h ;f o r ( i n t i =0; i<l e n ; i ++){

boolean isMax=true ;f o r ( i n t j =0; j<l e n ; j ++){

i f ( a [ i ]<a [ j ] ) {isMax=f a l s e ;

}}i f ( isMax ){

return a [ i ] ;}

}// u n r e a c h a b l ereturn 0 ;

}

Giles Reger Lecture 0 March 2017 17 / 48

Page 31: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Asymptotic Complexity

In this course we will meet a special tool for talking about complexitycalled asymptotic complexity analysis

The general idea is to abstract the complexity of a program as afunction of its inputs

We introduce Big-O notation where O(g(x)) = f (x) if a× g(x)eventually dominates f (x) i.e. for big enough inputs a× g(x) will bebigger than f (x) for some constant a

In the above we call the first max O(n) and the second max O(n2)

Giles Reger Lecture 0 March 2017 18 / 48

Page 32: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Space Tradeoff

We can make anything run in constant time if we precompute the results

Is this cheating?

count =0;i n t r e s u l t =1;whi le ( count<n ){

i f ( i s P r i m e ( r e s u l t ) ){count++;

}r e s u l t ++;

}

i f ( n==1){ return 2 ;}i f ( n==2){ return 3 ;}. . .i f ( n==1000){ return 7919 ;}. . .i f ( n==10000){ return 104729;}. . .return otherMethod ( n ) ;

Giles Reger Lecture 0 March 2017 19 / 48

Page 33: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Organisation

Week 6: The while language

Week 7: Coding and counting programs (Tuesday only)

Week 8: Computability

Week 9: Correctness

Week 10 and 11: Correctness and Complexity

All exercises for Examples Classes will appear on the course website

Giles Reger Lecture 0 March 2017 20 / 48

Page 34: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

How to Define a Programming Language

A program just describes some things we need to do:

Go to the shops and get 2 pints of milk. If they have eggs get 6.

Put the value of x in y and the value of y in x

English (and natural language in general) is highly ambiguous. We need aprecise language for giving instructions.

But how precise is good enough?

Giles Reger Lecture 0 March 2017 22 / 48

Page 35: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

How to Define a Programming Language

Every language needs:

a well-defined syntax i.e. the form that programs are allowed to take

a well-defined semantics i.e. what a program means

All languages have a well-defined syntax.

Not all languages have well-defined semantics. Very few have a formalsemantics, which is what we see in this course.

Instead, they rely on informal descriptions or reference implementations.

Giles Reger Lecture 0 March 2017 23 / 48

Page 36: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Formal Semantics

Meaning of a program ≡ what function it computes

There are three main approaches:

1 Operational: define what it means to interpret/execute programs

2 Axiomatic: define a program by what it provably does

3 Denotational: transform programs into some other metalanguage

We will see the first two in this course

ALGOL 68 was the first (and possibly one of the last) majorlanguage for which a full formal definition was made before itwas implemented.

C.H.A. Koster

Giles Reger Lecture 0 March 2017 24 / 48

Page 37: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

The while language

Is small enough to understand and be useful

Is powerful enough to capture any other language we might want

We will present its syntax and operational semantics

Giles Reger Lecture 0 March 2017 25 / 48

Page 38: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Syntax

The syntax can be given as a grammar (this is the usual way)

S ::= x := a | skip | S1; S2 | if b then S1 else S2 | while b do Sb ::= true | false | a1 = a2 | a1 ≤ a2 | ¬b | b1 ∧ b2

a ::= x | n | a1 + a2 | a1 − a2 | a1 × a2

Why is this ambiguous?

We can use brackets ( and ) to resolve ambiguities

For example, (if x < 0 then x := −x); z := y − x

In the above n stands for numbers (e.g. integers Z). Later we see that thisis sufficient (what does that mean?).

Giles Reger Lecture 0 March 2017 26 / 48

Page 39: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Example

What does this program do?

x :=1; y :=5;whi le ( y>0)( x :=x+x ; y :=y−1;)

We assume variables can have some initial values: this is inputWe assume we can read the variables at the end: this is output

Giles Reger Lecture 0 March 2017 27 / 48

Page 40: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Example

What does this program do?

x :=1;whi le ( y>0)( x :=x+x ; y :=y−1;)

We assume variables can have some initial values: this is inputWe assume we can read the variables at the end: this is output

Giles Reger Lecture 0 March 2017 28 / 48

Page 41: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Extending the Syntax

What if we want to write

i f b then S

when we only have

i f b then S1 e l s e S2

Giles Reger Lecture 0 March 2017 29 / 48

Page 42: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Extending the Syntax

We can define syntactic extensions in terms of existing syntax

Jif b then SK ≡ Jif b then S else skipK

This idea should be familiar.

In propositional logic we only need ∧ and ¬ i.e.

(A ∨ B) ≡ ¬(¬A ∧ ¬B)

In fact, we only need Nand to be functionally complete. There is a relatednotion of Turing complete here, which we will meet later.

Giles Reger Lecture 0 March 2017 30 / 48

Page 43: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Another Program

We will use this program in examples a lot, so you will be familiar with itby the end of the course.

r := x ;d := 0 ;whi le y ≤ r do ( d := d+1; r := r−y )

This program computes the function

f (x , y) = (d , r) = (x div y , x mod y)

whenever y > 0 ∧ x ≥ 0

Giles Reger Lecture 0 March 2017 31 / 48

Page 44: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Semantics

To define the semantics of the while language, we need ways to

Represent the current values of variables i.e. the state

Evaluate the arithmetic and logical expressions

Describe how program statements transform state

Giles Reger Lecture 0 March 2017 32 / 48

Page 45: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Examples of what’s coming later

< if (0 ≤ x) then skip else x := 0− x , [x 7→ −17] >⇒ < x := 0− x , [x 7→ −17] > Rule [ifff]⇒ [x 7→ 17] Rule [ass]

< x := 0; while (x > 0) (x := x − 1), [] >⇒ < while (x > 0) (x := x − 1), [x 7→ 0] > Rule [ass]⇒ < if (x > 0) then (x := x − 1;

while (x > 0) (x := x − 1)) else skip, [x 7→ 0] > Rule [while]⇒ < skip, [x 7→ 0] > Rule [ifff]⇒ [x 7→ 0] Rule [skip]

Giles Reger Lecture 0 March 2017 33 / 48

Page 46: Lecture 0 An Introduction to the Fundamentals of ...syllabus.cs.manchester.ac.uk/ugt/2017/COMP11212/lecture0.pdfGiles Reger Lecture 0 March 2017 17 / 48 Asymptotic Complexity In this

Take Home Points

To compute a function we run a program

The while language is an abstraction of what all computers do

The ideas of correctness and complexity have formal meaning

A programming language needs a syntax and semantics

Giles Reger Lecture 0 March 2017 34 / 48