Pure Functional Programming Functional Programming and Reasoning Dr - - PowerPoint PPT Presentation

pure functional programming
SMART_READER_LITE
LIVE PREVIEW

Pure Functional Programming Functional Programming and Reasoning Dr - - PowerPoint PPT Presentation

Pure Functional Programming Functional Programming and Reasoning Dr Hans Georg Schaathun University of Surrey Spring 2010 Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 1 / 52 Outline Testing and Proof 1 Pure Functional


slide-1
SLIDE 1

Pure Functional Programming

Functional Programming and Reasoning Dr Hans Georg Schaathun

University of Surrey

Spring 2010

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 1 / 52

slide-2
SLIDE 2

Outline

1

Testing and Proof

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 2 / 52

slide-3
SLIDE 3

This week

This week we aim to answer two questions:

How do we know that our program is correct? What’s so special about functional languages?

Students should able to

use testing to verify that functions and programs are correct use (simple) reasoning to verify that functions are correct understand when testing and reasoning (respectively) is appropriate understand the concept of side effects, including when they are useful and when they should be avoided

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 3 / 52

slide-4
SLIDE 4

This week

This week we aim to answer two questions:

How do we know that our program is correct? What’s so special about functional languages?

Students should able to

use testing to verify that functions and programs are correct use (simple) reasoning to verify that functions are correct understand when testing and reasoning (respectively) is appropriate understand the concept of side effects, including when they are useful and when they should be avoided

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 3 / 52

slide-5
SLIDE 5

Testing and Proof

Outline

1

Testing and Proof Verifying programs A little logic Testing and induction Reason and deduction

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 4 / 52

slide-6
SLIDE 6

Testing and Proof Verifying programs

Outline

1

Testing and Proof Verifying programs A little logic Testing and induction Reason and deduction

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 5 / 52

slide-7
SLIDE 7

Testing and Proof Verifying programs

Understanding Programs

How can we know what a function (program) does?

1

Try it in an implementation (hugs)

2

Evaluate it by hand (pen and scrap paper)

3

Analyse it mathematically (proof and argument)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 6 / 52

slide-8
SLIDE 8

Testing and Proof Verifying programs

Understanding Programs

How can we know what a function (program) does?

1

Try it in an implementation (hugs)

2

Evaluate it by hand (pen and scrap paper)

3

Analyse it mathematically (proof and argument)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 6 / 52

slide-9
SLIDE 9

Testing and Proof Verifying programs

Testing

Selected test cases

Has everyone used testing? Select a number of test cases.

Specify input Determine expected output Run, and check if output is as expected

Finite number of test cases

A test is not complete

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 7 / 52

slide-10
SLIDE 10

Testing and Proof Verifying programs

Testing

Selected test cases

Has everyone used testing? Select a number of test cases.

Specify input Determine expected output Run, and check if output is as expected

Finite number of test cases

A test is not complete

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 7 / 52

slide-11
SLIDE 11

Testing and Proof Verifying programs

Testing

Selected test cases

Has everyone used testing? Select a number of test cases.

Specify input Determine expected output Run, and check if output is as expected

Finite number of test cases

A test is not complete

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 7 / 52

slide-12
SLIDE 12

Testing and Proof Verifying programs

Analysis and generalisation

Can you logically prove that the program is correct? Has anyone ever tried it?

  • Generalise. Aim at a proof valid for all cases.

Not limited by a finite number of cases.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 8 / 52

slide-13
SLIDE 13

Testing and Proof Verifying programs

Analysis and generalisation

Can you logically prove that the program is correct? Has anyone ever tried it?

  • Generalise. Aim at a proof valid for all cases.

Not limited by a finite number of cases.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 8 / 52

slide-14
SLIDE 14

Testing and Proof Verifying programs

Analysis and generalisation

Can you logically prove that the program is correct? Has anyone ever tried it?

  • Generalise. Aim at a proof valid for all cases.

Not limited by a finite number of cases.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 8 / 52

slide-15
SLIDE 15

Testing and Proof Verifying programs

Hand Evaluation

When is hand evaluation useful? Development: trial and error

bug tracking

Small cases; easy to do by hand Understand the algorithm

recognise patterns help to generalise a definition (often recursive)

Like testing, limited to a small number of test cases

advantage: track the calculation to find errors

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 9 / 52

slide-16
SLIDE 16

Testing and Proof Verifying programs

Hand Evaluation

When is hand evaluation useful? Development: trial and error

bug tracking

Small cases; easy to do by hand Understand the algorithm

recognise patterns help to generalise a definition (often recursive)

Like testing, limited to a small number of test cases

advantage: track the calculation to find errors

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 9 / 52

slide-17
SLIDE 17

Testing and Proof Verifying programs

Hand Evaluation

When is hand evaluation useful? Development: trial and error

bug tracking

Small cases; easy to do by hand Understand the algorithm

recognise patterns help to generalise a definition (often recursive)

Like testing, limited to a small number of test cases

advantage: track the calculation to find errors

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 9 / 52

slide-18
SLIDE 18

Testing and Proof Verifying programs

Hand Evaluation

When is hand evaluation useful? Development: trial and error

bug tracking

Small cases; easy to do by hand Understand the algorithm

recognise patterns help to generalise a definition (often recursive)

Like testing, limited to a small number of test cases

advantage: track the calculation to find errors

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 9 / 52

slide-19
SLIDE 19

Testing and Proof Verifying programs

Hand Evaluation

When is hand evaluation useful? Development: trial and error

bug tracking

Small cases; easy to do by hand Understand the algorithm

recognise patterns help to generalise a definition (often recursive)

Like testing, limited to a small number of test cases

advantage: track the calculation to find errors

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 9 / 52

slide-20
SLIDE 20

Testing and Proof Verifying programs

Evaluate it

Why it is a good idea

Testing tells you if there is a mistake Manual evaluation can tell you where there is an error

1

Do you apply the correct guard?

2

Is there a typo at an early stage? Of course, only appropriate for small examples

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 10 / 52

slide-21
SLIDE 21

Testing and Proof Verifying programs

Evaluate it

Why it is a good idea

Testing tells you if there is a mistake Manual evaluation can tell you where there is an error

1

Do you apply the correct guard?

2

Is there a typo at an early stage? Of course, only appropriate for small examples

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 10 / 52

slide-22
SLIDE 22

Testing and Proof Verifying programs

Evaluate it

Why it is a good idea

Testing tells you if there is a mistake Manual evaluation can tell you where there is an error

1

Do you apply the correct guard?

2

Is there a typo at an early stage? Of course, only appropriate for small examples

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 10 / 52

slide-23
SLIDE 23

Testing and Proof Verifying programs

Evaluate it

For example

1

factorial 0 = 1

2

factorial n = n*factorial n factorial 3 = 3*factorial 2 (line 2) = 3*2*factorial 1 (line 2) = 6*1*factorial 0 (line 2) = 6*1 (line 1) = 6 (multiplication)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 11 / 52

slide-24
SLIDE 24

Testing and Proof Verifying programs

Evaluate it

For example

1

factorial 0 = 1

2

factorial n = n*factorial n factorial 3 = 3*factorial 2 (line 2) = 3*2*factorial 1 (line 2) = 6*1*factorial 0 (line 2) = 6*1 (line 1) = 6 (multiplication)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 11 / 52

slide-25
SLIDE 25

Testing and Proof Verifying programs

Principles of Software Development

Testing and Verification

Break the problem into smaller pieces

separate functions for each subproblem

Verify each function separately

starting with the most fundamental ones using testing or analysis as appropriate

If the test fails

you have little code to search for the bug

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 12 / 52

slide-26
SLIDE 26

Testing and Proof A little logic

Outline

1

Testing and Proof Verifying programs A little logic Testing and induction Reason and deduction

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 13 / 52

slide-27
SLIDE 27

Testing and Proof A little logic

A little logic

Consider a statement such as

1

Sheep are white

2

Therefore, Dolly the Sheep is white

This is an implication

There is an assumption (1) and a consequence (2)

The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

slide-28
SLIDE 28

Testing and Proof A little logic

A little logic

Consider a statement such as

1

Sheep are white

2

Therefore, Dolly the Sheep is white

This is an implication

There is an assumption (1) and a consequence (2)

The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

slide-29
SLIDE 29

Testing and Proof A little logic

A little logic

Consider a statement such as

1

Sheep are white

2

Therefore, Dolly the Sheep is white

This is an implication

There is an assumption (1) and a consequence (2)

The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

slide-30
SLIDE 30

Testing and Proof A little logic

A little logic

Consider a statement such as

1

Sheep are white

2

Therefore, Dolly the Sheep is white

This is an implication

There is an assumption (1) and a consequence (2)

The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

slide-31
SLIDE 31

Testing and Proof A little logic

A little logic

Consider a statement such as

1

Sheep are white

2

Therefore, Dolly the Sheep is white

This is an implication

There is an assumption (1) and a consequence (2)

The validity of the assumption is uncertain But if the assumption is true, there is no option for the consequence Also note that if (2) is false then (1) must be false

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 14 / 52

slide-32
SLIDE 32

Testing and Proof A little logic

Logical Notation

Sheep are white ⇒ Dolly the Sheep is white Dolly the Sheep is white ⇐ Sheep are white

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 15 / 52

slide-33
SLIDE 33

Testing and Proof A little logic

Logical Notation

Sheep are white ⇒ Dolly the Sheep is white Dolly the Sheep is white ⇐ Sheep are white

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 15 / 52

slide-34
SLIDE 34

Testing and Proof A little logic

Checking the Validity

Two separate questions

1

Is the assumption (1) true?

2

Is the inference (⇒) valid?

The consequence (2) follows if both answers are yes

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 16 / 52

slide-35
SLIDE 35

Testing and Proof A little logic

Deduction

Sheep are white ⇒ Dolly the Sheep is white Example of Deduction

i.e. a valid inference

W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W

Hence d ∈ W

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

slide-36
SLIDE 36

Testing and Proof A little logic

Deduction

Sheep are white ⇒ Dolly the Sheep is white Example of Deduction

i.e. a valid inference

W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W

Hence d ∈ W

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

slide-37
SLIDE 37

Testing and Proof A little logic

Deduction

Sheep are white ⇒ Dolly the Sheep is white Example of Deduction

i.e. a valid inference

W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W

Hence d ∈ W

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

slide-38
SLIDE 38

Testing and Proof A little logic

Deduction

Sheep are white ⇒ Dolly the Sheep is white Example of Deduction

i.e. a valid inference

W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W

Hence d ∈ W

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

slide-39
SLIDE 39

Testing and Proof A little logic

Deduction

Sheep are white ⇒ Dolly the Sheep is white Example of Deduction

i.e. a valid inference

W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W

Hence d ∈ W

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

slide-40
SLIDE 40

Testing and Proof A little logic

Deduction

Sheep are white ⇒ Dolly the Sheep is white Example of Deduction

i.e. a valid inference

W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W

Hence d ∈ W

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

slide-41
SLIDE 41

Testing and Proof A little logic

Deduction

Sheep are white ⇒ Dolly the Sheep is white Example of Deduction

i.e. a valid inference

W be the set of all white things S be the set of all sheep d ∈ S is Dolly the Sheep We have assumed S ⊂ W We have d ∈ S ⊂ W

Hence d ∈ W

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 17 / 52

slide-42
SLIDE 42

Testing and Proof A little logic

Induction

Generalisation for a special case

Dolly the Sheep is white ⇒ Sheep are white

This would be an invalid inference (generalising from a special case)

Known as induction Attempts to generalise from specific information

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 18 / 52

slide-43
SLIDE 43

Testing and Proof A little logic

Induction

Generalisation for a special case

Dolly the Sheep is white ⇒ Sheep are white

This would be an invalid inference (generalising from a special case)

Known as induction Attempts to generalise from specific information

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 18 / 52

slide-44
SLIDE 44

Testing and Proof A little logic

Induction

Generalisation for a special case

Dolly the Sheep is white ⇒ Sheep are white

This would be an invalid inference (generalising from a special case)

Known as induction Attempts to generalise from specific information

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 18 / 52

slide-45
SLIDE 45

Testing and Proof A little logic

Learning by Observation

Dolly is white

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 19 / 52

slide-46
SLIDE 46

Testing and Proof A little logic

Learning by Observation

All (?) sheep are white

Not quite. Many sheep are white.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 20 / 52

slide-47
SLIDE 47

Testing and Proof A little logic

Learning by Observation

All (?) sheep are white

Not quite. Many sheep are white.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 20 / 52

slide-48
SLIDE 48

Testing and Proof A little logic

Learning by Observation

Not all sheep are white

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 21 / 52

slide-49
SLIDE 49

Testing and Proof A little logic

Learning by Observation

Conclusion

  • Observable. A white sheep exists.

Not observable. All sheep are white.

  • Observable. A non-white sheep exists.
  • Observable. Not all sheep are white.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

slide-50
SLIDE 50

Testing and Proof A little logic

Learning by Observation

Conclusion

  • Observable. A white sheep exists.

Not observable. All sheep are white.

  • Observable. A non-white sheep exists.
  • Observable. Not all sheep are white.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

slide-51
SLIDE 51

Testing and Proof A little logic

Learning by Observation

Conclusion

  • Observable. A white sheep exists.

Not observable. All sheep are white.

  • Observable. A non-white sheep exists.
  • Observable. Not all sheep are white.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

slide-52
SLIDE 52

Testing and Proof A little logic

Learning by Observation

Conclusion

  • Observable. A white sheep exists.

Not observable. All sheep are white.

  • Observable. A non-white sheep exists.
  • Observable. Not all sheep are white.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 22 / 52

slide-53
SLIDE 53

Testing and Proof Testing and induction

Outline

1

Testing and Proof Verifying programs A little logic Testing and induction Reason and deduction

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 23 / 52

slide-54
SLIDE 54

Testing and Proof Testing and induction

Testing does special cases only

1

Program works for input dolly

2

Program works for any input (of type Sheep) The former is observable – by testing The latter cannot be tested And the induction (1) → (2) is not valid

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 24 / 52

slide-55
SLIDE 55

Testing and Proof Testing and induction

Choosing test cases

Black Box Testing

Testing cannot prove that the program works

but it can prove that it does not find the black sheep in the flock

Choosing test cases is a key skill appropriate broad selection

maximises the chances of finding a black sheep

Black Box Testing

choose test cases based on the specification with no knowledge of the implementation

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

slide-56
SLIDE 56

Testing and Proof Testing and induction

Choosing test cases

Black Box Testing

Testing cannot prove that the program works

but it can prove that it does not find the black sheep in the flock

Choosing test cases is a key skill appropriate broad selection

maximises the chances of finding a black sheep

Black Box Testing

choose test cases based on the specification with no knowledge of the implementation

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

slide-57
SLIDE 57

Testing and Proof Testing and induction

Choosing test cases

Black Box Testing

Testing cannot prove that the program works

but it can prove that it does not find the black sheep in the flock

Choosing test cases is a key skill appropriate broad selection

maximises the chances of finding a black sheep

Black Box Testing

choose test cases based on the specification with no knowledge of the implementation

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

slide-58
SLIDE 58

Testing and Proof Testing and induction

Choosing test cases

Black Box Testing

Testing cannot prove that the program works

but it can prove that it does not find the black sheep in the flock

Choosing test cases is a key skill appropriate broad selection

maximises the chances of finding a black sheep

Black Box Testing

choose test cases based on the specification with no knowledge of the implementation

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 25 / 52

slide-59
SLIDE 59

Testing and Proof Testing and induction

Example: minThree

minThree :: Int -> Int -> Int -> Int Returns the smallest of three integers Which inputs do we have to test? Three different numbers

(small,middle,large); (small,large,middle); (large,middle,small); etc. Two equal + one smaller Two equal + one larger Three equal

At least one test case from each category

not so important to test both (1, 2, 3) and (1, 2, 4)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 26 / 52

slide-60
SLIDE 60

Testing and Proof Testing and induction

Example: minThree

minThree :: Int -> Int -> Int -> Int Returns the smallest of three integers Which inputs do we have to test? Three different numbers

(small,middle,large); (small,large,middle); (large,middle,small); etc. Two equal + one smaller Two equal + one larger Three equal

At least one test case from each category

not so important to test both (1, 2, 3) and (1, 2, 4)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 26 / 52

slide-61
SLIDE 61

Testing and Proof Testing and induction

Example: minThree

minThree :: Int -> Int -> Int -> Int Returns the smallest of three integers Which inputs do we have to test? Three different numbers

(small,middle,large); (small,large,middle); (large,middle,small); etc. Two equal + one smaller Two equal + one larger Three equal

At least one test case from each category

not so important to test both (1, 2, 3) and (1, 2, 4)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 26 / 52

slide-62
SLIDE 62

Testing and Proof Reason and deduction

Outline

1

Testing and Proof Verifying programs A little logic Testing and induction Reason and deduction

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 27 / 52

slide-63
SLIDE 63

Testing and Proof Reason and deduction

Analyse it

Go general

Testing and evaluation consider special cases

Analytical argument aim to generalise

Good argument is about

being structured and systematic covering all cases

This is about practice and talent

so let’s go through an example

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 28 / 52

slide-64
SLIDE 64

Testing and Proof Reason and deduction

Analyse it

Go general

Testing and evaluation consider special cases

Analytical argument aim to generalise

Good argument is about

being structured and systematic covering all cases

This is about practice and talent

so let’s go through an example

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 28 / 52

slide-65
SLIDE 65

Testing and Proof Reason and deduction

Analyse it

Go general

Testing and evaluation consider special cases

Analytical argument aim to generalise

Good argument is about

being structured and systematic covering all cases

This is about practice and talent

so let’s go through an example

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 28 / 52

slide-66
SLIDE 66

Testing and Proof Reason and deduction

Analyse it

A sample function implementation

Is the following a correct calculation of maximum? minThree x y z | x < y && x < z = x | y < x && y < z = y | otherwise = z

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 29 / 52

slide-67
SLIDE 67

Testing and Proof Reason and deduction

Analyse it

Continued

1

| x < y && x < z = x

Clearly, x is strictly smaller than y and z. Correct.

2

| y < x && y < z = y

Clearly, x is strictly smaller than x and z. Correct.

Which cases remain for otherwise?

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 30 / 52

slide-68
SLIDE 68

Testing and Proof Reason and deduction

Analyse it

Continued

1

| x < y && x < z = x

Clearly, x is strictly smaller than y and z. Correct.

2

| y < x && y < z = y

Clearly, x is strictly smaller than x and z. Correct.

Which cases remain for otherwise?

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 30 / 52

slide-69
SLIDE 69

Testing and Proof Reason and deduction

Analyse it

Continued

1

| x < y && x < z = x

Clearly, x is strictly smaller than y and z. Correct.

2

| y < x && y < z = y

Clearly, x is strictly smaller than x and z. Correct.

Which cases remain for otherwise?

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 30 / 52

slide-70
SLIDE 70

Testing and Proof Reason and deduction

Analyse it

The otherwise cases

1

We may have z < x and z < y,

when z is the correct output.

2

We may have x = y = z,

when z is correct (as would x or y be)

3

Cases with two equal inputs

... next slide ...

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 31 / 52

slide-71
SLIDE 71

Testing and Proof Reason and deduction

Analyse it

The otherwise cases

1

We may have z < x and z < y,

when z is the correct output.

2

We may have x = y = z,

when z is correct (as would x or y be)

3

Cases with two equal inputs

... next slide ...

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 31 / 52

slide-72
SLIDE 72

Testing and Proof Reason and deduction

Analyse it

The otherwise cases

1

We may have z < x and z < y,

when z is the correct output.

2

We may have x = y = z,

when z is correct (as would x or y be)

3

Cases with two equal inputs

... next slide ...

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 31 / 52

slide-73
SLIDE 73

Testing and Proof Reason and deduction

Analyse it

Two equal inputs

z = x

z ≤ y : otherwise applies – correct z > y : Case 2 applies – correct

z = y

z ≤ x : otherwise applies – correct z > x : Case 1 applies – correct

x = y

z ≤ x : otherwise applies – correct z > x : otherwise applies – minimum is x (=y)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 32 / 52

slide-74
SLIDE 74

Testing and Proof Reason and deduction

Analyse it

Two equal inputs

z = x

z ≤ y : otherwise applies – correct z > y : Case 2 applies – correct

z = y

z ≤ x : otherwise applies – correct z > x : Case 1 applies – correct

x = y

z ≤ x : otherwise applies – correct z > x : otherwise applies – minimum is x (=y)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 32 / 52

slide-75
SLIDE 75

Testing and Proof Reason and deduction

Analyse it

Two equal inputs

z = x

z ≤ y : otherwise applies – correct z > y : Case 2 applies – correct

z = y

z ≤ x : otherwise applies – correct z > x : Case 1 applies – correct

x = y

z ≤ x : otherwise applies – correct z > x : otherwise applies – minimum is x (=y)

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 32 / 52

slide-76
SLIDE 76

Testing and Proof Reason and deduction

Analysis

Summary

We found the bug

by carefully checking every case, class by class the classes cover all possible inputs

Suppose there were no bug

would this method allow us to prove correctness?

Yes – we have not only found the bug

we know exactly which inputs for which the output is wrong

For each class of inputs, it is either correct or wrong

and any possible inputs falls in at least one class

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

slide-77
SLIDE 77

Testing and Proof Reason and deduction

Analysis

Summary

We found the bug

by carefully checking every case, class by class the classes cover all possible inputs

Suppose there were no bug

would this method allow us to prove correctness?

Yes – we have not only found the bug

we know exactly which inputs for which the output is wrong

For each class of inputs, it is either correct or wrong

and any possible inputs falls in at least one class

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

slide-78
SLIDE 78

Testing and Proof Reason and deduction

Analysis

Summary

We found the bug

by carefully checking every case, class by class the classes cover all possible inputs

Suppose there were no bug

would this method allow us to prove correctness?

Yes – we have not only found the bug

we know exactly which inputs for which the output is wrong

For each class of inputs, it is either correct or wrong

and any possible inputs falls in at least one class

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

slide-79
SLIDE 79

Testing and Proof Reason and deduction

Analysis

Summary

We found the bug

by carefully checking every case, class by class the classes cover all possible inputs

Suppose there were no bug

would this method allow us to prove correctness?

Yes – we have not only found the bug

we know exactly which inputs for which the output is wrong

For each class of inputs, it is either correct or wrong

and any possible inputs falls in at least one class

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 33 / 52

slide-80
SLIDE 80

Testing and Proof Reason and deduction

The bug fix

How do you correct the bug in the code? (one way) replace < by <= in the guards Exercise:

run through the analysis above for the corrected code are you convinced that the implementation is correct for any input?

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 34 / 52

slide-81
SLIDE 81

Testing and Proof Reason and deduction

The bug fix

How do you correct the bug in the code? (one way) replace < by <= in the guards Exercise:

run through the analysis above for the corrected code are you convinced that the implementation is correct for any input?

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 34 / 52

slide-82
SLIDE 82

Testing and Proof Reason and deduction

The bug fix

How do you correct the bug in the code? (one way) replace < by <= in the guards Exercise:

run through the analysis above for the corrected code are you convinced that the implementation is correct for any input?

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 34 / 52

slide-83
SLIDE 83

Testing and Proof Reason and deduction

White Box Testing

Black Box Testing

consider the implementation as a black box no knowledge of the implementation

White Box Testing

analyse the implementation to choose test cases

In particular, look at the guards

when evaluation branches, use test sets for each branch

This often helps adding important test cases otherwise forgotten

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

slide-84
SLIDE 84

Testing and Proof Reason and deduction

White Box Testing

Black Box Testing

consider the implementation as a black box no knowledge of the implementation

White Box Testing

analyse the implementation to choose test cases

In particular, look at the guards

when evaluation branches, use test sets for each branch

This often helps adding important test cases otherwise forgotten

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

slide-85
SLIDE 85

Testing and Proof Reason and deduction

White Box Testing

Black Box Testing

consider the implementation as a black box no knowledge of the implementation

White Box Testing

analyse the implementation to choose test cases

In particular, look at the guards

when evaluation branches, use test sets for each branch

This often helps adding important test cases otherwise forgotten

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

slide-86
SLIDE 86

Testing and Proof Reason and deduction

White Box Testing

Black Box Testing

consider the implementation as a black box no knowledge of the implementation

White Box Testing

analyse the implementation to choose test cases

In particular, look at the guards

when evaluation branches, use test sets for each branch

This often helps adding important test cases otherwise forgotten

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 35 / 52

slide-87
SLIDE 87

Pure Functional Programming

Outline

1

Testing and Proof

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 36 / 52

slide-88
SLIDE 88

Pure Functional Programming

Pure functional language

Haskell is a pure functional language

therefore, lends itself well to reasoning

It adhers to a strict set of principles

contrary to languages providing imperative and functional features together

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 37 / 52

slide-89
SLIDE 89

Pure Functional Programming

What is a program

Functional : list of definitions

  • ne expression being evaluated at the end

Imperative : list of instructions

executed one by one modifying variables (system state)

Time is important in imperative programming

instructions are executed in order and instructions can redefine objects

Definitions are constant in functional programming

nothing changes over time

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 38 / 52

slide-90
SLIDE 90

Pure Functional Programming

Referential transparency

All calls to the same function

same input ⇒ same output

Two major benefits

the compiler can optimise (avoid recomputation) the developer can make mathematical proofs

This is not true for imperative programming

as functions may depend on the system state (variables) such dependencies may be hard to spot, and makes reasoning more difficult

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 39 / 52

slide-91
SLIDE 91

Pure Functional Programming

Referential transparency

All calls to the same function

same input ⇒ same output

Two major benefits

the compiler can optimise (avoid recomputation) the developer can make mathematical proofs

This is not true for imperative programming

as functions may depend on the system state (variables) such dependencies may be hard to spot, and makes reasoning more difficult

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 39 / 52

slide-92
SLIDE 92

Pure Functional Programming

Referential transparency

All calls to the same function

same input ⇒ same output

Two major benefits

the compiler can optimise (avoid recomputation) the developer can make mathematical proofs

This is not true for imperative programming

as functions may depend on the system state (variables) such dependencies may be hard to spot, and makes reasoning more difficult

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 39 / 52

slide-93
SLIDE 93

Pure Functional Programming

Side-effects

Functions have no side-effect

all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables

Pure functional languages forbid side-effects completely Avoiding side-effects is good practice

easier to reuse code

  • ther applications often need

same return value different side-effects (output)

e.g. computational back-end without IO can be combined with different user interfaces

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

slide-94
SLIDE 94

Pure Functional Programming

Side-effects

Functions have no side-effect

all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables

Pure functional languages forbid side-effects completely Avoiding side-effects is good practice

easier to reuse code

  • ther applications often need

same return value different side-effects (output)

e.g. computational back-end without IO can be combined with different user interfaces

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

slide-95
SLIDE 95

Pure Functional Programming

Side-effects

Functions have no side-effect

all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables

Pure functional languages forbid side-effects completely Avoiding side-effects is good practice

easier to reuse code

  • ther applications often need

same return value different side-effects (output)

e.g. computational back-end without IO can be combined with different user interfaces

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

slide-96
SLIDE 96

Pure Functional Programming

Side-effects

Functions have no side-effect

all communication is via the output cannot modify behaviour of other functions cannot do Input/Output (IO) directly cannot modify variables

Pure functional languages forbid side-effects completely Avoiding side-effects is good practice

easier to reuse code

  • ther applications often need

same return value different side-effects (output)

e.g. computational back-end without IO can be combined with different user interfaces

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 40 / 52

slide-97
SLIDE 97

Pure Functional Programming

Side-effects

An example

function PseudoRandom() state := state * 17822228932 + 178324742 return state mod 256 You want a new pseudo-random number every time Side-effects is useful in this case

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 41 / 52

slide-98
SLIDE 98

Pure Functional Programming

Side-effects

An example

function PseudoRandom() state := state * 17822228932 + 178324742 return state mod 256 You want a new pseudo-random number every time Side-effects is useful in this case

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 41 / 52

slide-99
SLIDE 99

Pure Functional Programming

Other side-effects

The most common side-effect is output This is often a bad side-effect You don’t always want the same output Some users will need the calculations without output Separation is useful

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 42 / 52

slide-100
SLIDE 100

Pure Functional Programming

Imperative programming

including Java

Global state (memory)

methods (functions) can change the state method behaviour can depend on state

Different calls to f can give different output

typical example: getchar (read keyboard input)

  • ne call removes a character from input buffer

changes the value of the next call

Functional principles can be used in imperative languages

avoid side-effects when they are not necessary separate computations and IO

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 43 / 52

slide-101
SLIDE 101

Pure Functional Programming

Imperative versus Functional

Imperative programming:

list of actions to be performed by the computer

Functional programming:

list of mathematical definitions and declarations

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 44 / 52

slide-102
SLIDE 102

Definedness and Termination

Outline

1

Testing and Proof

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 45 / 52

slide-103
SLIDE 103

Definedness and Termination

Termination

Definition Termination means that a function completes and returns a value. Termination is a desired property It does happen that a function never terminates

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 46 / 52

slide-104
SLIDE 104

Definedness and Termination

Example

Termination

fac :: Int -> Int fac 0 = 1 fac n = n*fac(n-1) fac 2 terminates (and returns 2) fac (-2) does not terminate

fac (-2) = (−2) · (−3) · (−4) . . . The evaluation continues to expand the expression infinitely

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 47 / 52

slide-105
SLIDE 105

Definedness and Termination

Undefined expression

Function calls which do not terminate (like fac (-2))

lead to undefined expressions

Undefined expressions lead to other undefined expressions

We know that 0 · a = 0 for any number a . . . what about 0*fac (-2)?

When fac (-2) is undefined,

then 0*fac (-2) cannot be defined.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 48 / 52

slide-106
SLIDE 106

Definedness and Termination

Undefined expression

Function calls which do not terminate (like fac (-2))

lead to undefined expressions

Undefined expressions lead to other undefined expressions

We know that 0 · a = 0 for any number a . . . what about 0*fac (-2)?

When fac (-2) is undefined,

then 0*fac (-2) cannot be defined.

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 48 / 52

slide-107
SLIDE 107

Definedness and Termination

Free variables and quantifiers

square x = x*x x here is called a free variable For which values of x is the definition valid? We normally mean for every x Explicitely: ∀x, square x = x*x Or restrict it: ∀x ∈ {0, 1, 2, 3, . . .}, square x = x*x

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 49 / 52

slide-108
SLIDE 108

Definedness and Termination

Free variables and quantifiers

square x = x*x x here is called a free variable For which values of x is the definition valid? We normally mean for every x Explicitely: ∀x, square x = x*x Or restrict it: ∀x ∈ {0, 1, 2, 3, . . .}, square x = x*x

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 49 / 52

slide-109
SLIDE 109

Definedness and Termination

Free variables and quantifiers

square x = x*x x here is called a free variable For which values of x is the definition valid? We normally mean for every x Explicitely: ∀x, square x = x*x Or restrict it: ∀x ∈ {0, 1, 2, 3, . . .}, square x = x*x

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 49 / 52

slide-110
SLIDE 110

Definedness and Termination

Free variables and quantifiers

square x = x*x x here is called a free variable For which values of x is the definition valid? We normally mean for every x Explicitely: ∀x, square x = x*x Or restrict it: ∀x ∈ {0, 1, 2, 3, . . .}, square x = x*x

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 49 / 52

slide-111
SLIDE 111

Definedness and Termination

Error handling

Use the error statement for error handling fac :: Int -> Int fac n | n < 0 = error "Undefined for negative numbers" | n == 0 = 1 | otherwise = n*fac(n-1) error interrupts evaluation

very odd behaviour for a function

For a more advanced and functional error handling

see Thompson Chapter 14.4

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 50 / 52

slide-112
SLIDE 112

Conclusion

Outline

1

Testing and Proof

2

Pure Functional Programming

3

Definedness and Termination

4

Conclusion

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 51 / 52

slide-113
SLIDE 113

Conclusion

Summary

You should be familiar with every method of validating code

testing manual evaluation mathematical argument

Use all of them

not always all in every case though

Dr Hans Georg Schaathun Pure Functional Programming Spring 2010 52 / 52