Transcript Lecture 1

Functional Programming
GC16 / 3011
Chris Clack
06/01/10
1
Functional Programming
• This Lecture:
•
•
•
•
Introduction
Simple example
Haiku or Karate? (industrial example)
The Course
06/01/10
2
Introduction
• How many programming languages do you
think exist?
• Java, C++, Cobol, Fortran, Basic, APL, C, Pascal, Haskell,
Miranda, Prolog, LISP…
•
How many languages can you use?
•
Are all these other languages just “junk”?
•
Why can’t you use Java for everything?
06/01/10
3
Introduction
• What’s COOL about HOT languages?
• What’s NEAT about GARBAGE?
• How can a LAZY language improve performance?
• Some languages support “infinite” data
structures…(…how do they do that?)
06/01/10
4
Introduction
Java
C++
FORTRAN
LISP
SML
Miranda
Haskell
Prolog
• Imperative versus Declarative
• Functional versus Logic
• “instructing a machine” versus “solving a
problem”
06/01/10
5
Simple Example
• Assume “results” is the name of a variable that
contains a sequence of 100 integers;
• write code to select all those less than 10
06/01/10
6
Simple Example - imperative
int small[100];
for (j=0,k=0; j<100; j++){
if (results[j]<10) {
small[k]=results[j];
k++;
}
}
return(small);
06/01/10
7
Simple Example - functional
filter (< 10) results
06/01/10
8
Haiku or Karate?
• FP languages are renowned for their elegance
• But - are they like Japanese “Haiku” poetry?
• (elegant but not very practical)
• Or - are they like Karate?
• (elegant and useful in a fight)
06/01/10
9
Haiku or Karate?
Prototyping O-O Designs
Using FP Techniques
06/01/10
10
Haiku or Karate? – the problem
• Commercial Constraints
• The world’s largest IT Consultancy
• An international Settlement Bank
• A “mission-critical” financial system
• Over 100 programmers
• C++ required by client
06/01/10
11
Haiku or Karate? – the problem
• The System:
A network of components
06/01/10
12
Haiku or Karate? – the problem
• Requirements:
– Discrete-event simulation of component network
– Prototyping of central (optimisation and
approximation) algorithms
06/01/10
13
The main problem is C++
06/01/10
14
Haiku or Karate? – constraints
• C++ “not viable” for prototyping/simulation
• take too long to develop underlying components
• rapid-prototyping O-O language (e.g. Smalltalk)
“not desirable”
• raises question about overall suitability of C++
•
Alternative approach: use a Functional
Language (“Miranda”)
• HOT, LAZY, with GARBAGE COLLECTION
• NO POINTERS. NO ASSIGNMENT
06/01/10
15
Haiku or Karate? – selling points
• Speed and Clarity with which algorithms can
be:
• expressed
• validated
• Can simulate key O-O designs in detail
• with minimal detail for other components
• Access to expertise:
• a “champion”
06/01/10
16
Haiku or Karate? – selling points
• Note: Speed of execution was almost totally
irrelevant!
06/01/10
17
Haiku or Karate? - techniques
• Modelling the component network
file1
c1
c2
c3
a
c4
file2
c5
c
c8
c6
c7
06/01/10
c9
b
d
18
Haiku or Karate? - techniques
c1
= read “file1”
c5
= read “file2”
c9
= b (c2, c6, c8)
(c2, c3) = a (c1, c4)
(c4, c6) = c (c5, c7)
(c7, c8) = d c3
•
•
Recursive (looping) functions a, b, c and d
c1, c2 etc. are potentially-infinite streams of
(time, value) events
06/01/10
19
Haiku or Karate? - techniques
• Simulating behaviour
• simple behavioural executable specification
• expression-based
• synthetic (statistical) data generation
• Could also use Miranda algorithms as
specification for subsequent implementation in
C++
06/01/10
20
Haiku or Karate? - results
• Rapid Development
» About five times faster than C++
•
Concise Expression
» 6 pages of Miranda = about 25 pages of C++
•
Simulation and specification of complex
processes
» design optimised early in lifecycle
» confidence increased through validation on real data
06/01/10
21
Haiku or Karate? - results
•
•
•
•
Almost NO errors in prototype code
Vast reduction in errors in final C++ code
Viewed as a commercial advantage
Promoted within Andersen Consulting - part of
“Method/1”
• “champion” now promoted to Manager
06/01/10
22
The Course
• Focus:
•
•
•
•
Fab Features
Ingenious Implementation
Programming Skills
Add another language/paradigm to your CV
• Vehicle:
• A functional language “Miranda”
• Elegant implementation technology
• Extends to other languages (e.g. Java)
• Coursework: 10% (1 coursework, may be MCT)
06/01/10
23
The Course - Lectures
• LECTURES:
• Monday 12-1pm
• Tuesday 9-10am
• Friday 11-12noon
25 Gordon Square 107
MPEB 1.02
Roberts 110
• PRACTICALS (TBA: if/when I have a demonstrator!):
• Friday 4-5pm
• Tuesday 9-10am
MPEB 1.21
DOUBLE BOOKED
• mostly for GC16, less relevant for 3011
• Demonstrator – TBA
06/01/10
24
The Course - Topics
• “Lambda Calculus” - a simple introduction
• Introduction to Miranda
• developing some programming skills
• developing problem-solving (thinking) skills
• Introduction to implementation techniques
• graph reduction
• memory management (cf. C, C++, algorithms)
• garbage collection (cf. Java)
06/01/10
25
The Course - Books
• Programming with Miranda, Clack, Myers and Poon,
Prentice-Hall 1994 ISBN 0-13-192592-X.
• Research Directions in Parallel Functional Programming,
Hammond and Michaelson (Eds.), Springer, ISBN 1-85233092-9, 1999
• The Implementation of Functional Programming Languages,
Peyton-Jones, Prentice-Hall 1986
• Garbage Collection: Algorithms for Automatic Dynamic
Memory Management, Jones and Lins, Wiley 1996 ISBN 0
471 94148 4
06/01/10
26
The Course - Communication
• [email protected][email protected]
([email protected])
([email protected])
• http://www.cs.ucl.ac.uk/teaching/3c11
• http://www.cs.ucl.ac.uk/teaching/D16
06/01/10
27