1 Functional Programming & Standard ML Hossein Hojjat et al.

55
1 Functional Programming & Standard ML Hossein Hojjat et al.

Transcript of 1 Functional Programming & Standard ML Hossein Hojjat et al.

Page 1: 1 Functional Programming & Standard ML Hossein Hojjat et al.

1

Functional Programming & Standard ML

Hossein Hojjat et al.

Page 2: 1 Functional Programming & Standard ML Hossein Hojjat et al.

2

By doing it mathematically, you provide a firm foundation which will enable people

to go further.Robin Milner,

Turing Award Lecturer(1991) and the ML designer

Page 3: 1 Functional Programming & Standard ML Hossein Hojjat et al.

3

Outline

• Introduction

• Why functional programming?

• Some History

• Standard ML

• ML Syntax

• Programming in Standard ML

• Some Good References

Page 4: 1 Functional Programming & Standard ML Hossein Hojjat et al.

4

Introduction

Page 5: 1 Functional Programming & Standard ML Hossein Hojjat et al.

5

Introduction

• We use a zillion different programming languages:

• general purpose programming:  Fortran, Lisp, Basic, C, Pascal, C++, Java, etc.

• scripting:  Visual Basic, awk, sed, perl, tcl, sh, csh, bash, REXX, Scheme, etc.

• search: regular expressions, browser queries, SQL, etc.

Page 6: 1 Functional Programming & Standard ML Hossein Hojjat et al.

6

Introduction

• display and rendering:  PostScript, HTML, XML, VRML, etc.

• hardware:  SystemC, VHDL, Esterelle

• theorem proving and mathematics:  Mathematica, Maple, Matlab, NuPRL, Coq

• others?

Page 7: 1 Functional Programming & Standard ML Hossein Hojjat et al.

7

Introduction

• ML is very different from what most of us have seen: it is functional

• Before considering ML, we will summarize the imperative languages properties

Page 8: 1 Functional Programming & Standard ML Hossein Hojjat et al.

8

Introduction

Page 9: 1 Functional Programming & Standard ML Hossein Hojjat et al.

9

Introduction

• Design of imperative languages is based directly on the von Neumann architecture

Page 10: 1 Functional Programming & Standard ML Hossein Hojjat et al.

10

Introduction

• Programs in imperative languages rely heavily on modifying the values of a collection of variables, called the state

• Before execution, the state has some initial value σ

• During execution, each command changes the state

'...210 n

Page 11: 1 Functional Programming & Standard ML Hossein Hojjat et al.

11

Introduction

• Example: – In a sorting program, the state initially

includes an array of values

• When the program has finished, the state has been modified in such a way that these values are sorted

• Intermediate states represent progress towards this goal

Page 12: 1 Functional Programming & Standard ML Hossein Hojjat et al.

12

Introduction

• The state is typically modified by assignment commands

• By using control structures, one can execute these commands conditionally, or repeatedly, depending on other properties of the current state

Page 13: 1 Functional Programming & Standard ML Hossein Hojjat et al.

13

Introduction

• But …

Page 14: 1 Functional Programming & Standard ML Hossein Hojjat et al.

14

Introduction

• Functional programs don’t use variables - there is no state

• Therefore they cannot use assignments : there is nothing to assign to

Page 15: 1 Functional Programming & Standard ML Hossein Hojjat et al.

15

Introduction

• The idea of executing commands in sequence is meaningless

• The first command can make no difference to the second : there are not any state between them

• They are based on Mathematical functions

Page 16: 1 Functional Programming & Standard ML Hossein Hojjat et al.

16

Introduction

• Functions can be treated in exactly the same way as simpler objects like integers

• They can be passed to other functions as arguments and returned as results

• Most traditional languages provide poor facilities in these areas

Page 17: 1 Functional Programming & Standard ML Hossein Hojjat et al.

17

Introduction

• Instead of sequencing and looping, functional languages use recursive functions

Page 18: 1 Functional Programming & Standard ML Hossein Hojjat et al.

18

Introduction

• Example:

f(n) =

1 if n=1

f(5*n-1) if n is odd, n≠1

f(n/4-3) if n is even

• Question: You said there are not any variables, but what about “n”?

Page 19: 1 Functional Programming & Standard ML Hossein Hojjat et al.

19

Introduction

• “n” is an identifier• In a Functional Language, the

identifier bind to values• Variable is something that can

be assigned a value• Functions have no side effect

– They do not update any variables– It is easy to define the semantics

Page 20: 1 Functional Programming & Standard ML Hossein Hojjat et al.

20

Introduction

• Haskell is an example of a pure functional language.

• Haskell is, as of 2002, the functional language on which the most research is being performed.

• ML is not a pure functional language in that it is possible to write procedural programs (with assignments and side-effects)

Page 21: 1 Functional Programming & Standard ML Hossein Hojjat et al.

21

Why Functional Programming?

Page 22: 1 Functional Programming & Standard ML Hossein Hojjat et al.

22

Why Functional Programming?

The von Neumann bottleneck

• Backus' famous paper encouraged much interest in functional languages as a means of breaking the von-Neumann bottleneck

Page 23: 1 Functional Programming & Standard ML Hossein Hojjat et al.

23

Why Functional Programming?

• Von Neumann bottleneck: pumping single words back and forth between CPU and store

• Task of a program: change store in some major way.

• It has kept us tied to word-at-a-time thinking instead of of encouraging us to think in terms of the larger conceptual units of the task at hand.

Page 24: 1 Functional Programming & Standard ML Hossein Hojjat et al.

24

Why Functional Programming?

• The assignment statement is the von Neumann bottleneck of programming languages

• Pure functional programming languages remove state and assignments

• Concurrency possible: order of evaluation doesn’t matter

Page 25: 1 Functional Programming & Standard ML Hossein Hojjat et al.

25

Why Functional Programming?

Page 26: 1 Functional Programming & Standard ML Hossein Hojjat et al.

26

Why Functional Programming?

• System is referentially transparent if, in a fixed context, the meaning of the whole can be determined solely by the meaning of its parts.

• Independent of the surrounding expression.

Page 27: 1 Functional Programming & Standard ML Hossein Hojjat et al.

27

Why Functional Programming?

• Do we have such property in imperative languages?

• If the function has side-effects (updating a global variable, doing input or output), then f(3) + f(3) may not be the same as 2 * f(3).– The second f(3) has a different

meaning than the rst

Page 28: 1 Functional Programming & Standard ML Hossein Hojjat et al.

28

Why Functional Programming?

• Purely declarative languages guarantee referential transparency

• It makes it easier to understand how a program works

Page 29: 1 Functional Programming & Standard ML Hossein Hojjat et al.

29

Why Functional Programming?

• Many features of imperative languages have arisen by a process of abstraction from typical computer hardware

• Perhaps the right approach is not to start from the hardware and work upwards

Page 30: 1 Functional Programming & Standard ML Hossein Hojjat et al.

30

Why Functional Programming?

• “Start with programming languages as an abstract notation for specifying algorithms and then work down to the hardware.” (Dijkstra 1976)

Page 31: 1 Functional Programming & Standard ML Hossein Hojjat et al.

31

Why Functional Programming?

• Makes programming into an engineering discipline rather than a trial-and-error process

• The Formalism Principle: “Correctness should be confirmed by reasoning and not by experiment”- Marjan Sirjani

Page 32: 1 Functional Programming & Standard ML Hossein Hojjat et al.

32

Why Functional Programming?

• As a matter of fact, it is unlikely that programmers will have the patience to perform such proofs: the proofs are usually long and boring

Page 33: 1 Functional Programming & Standard ML Hossein Hojjat et al.

33

• “Beware of bugs in the above code; I have only proved it correct, not tried it.” ,Donald Knuth

Page 34: 1 Functional Programming & Standard ML Hossein Hojjat et al.

34

Why Functional Programming?

• Functional Programming is an area of current research

• ACM Conference on LISP and Functional Programming

Page 35: 1 Functional Programming & Standard ML Hossein Hojjat et al.

35

Some History

Page 36: 1 Functional Programming & Standard ML Hossein Hojjat et al.

36

Some History

• 1940s:– Alonzo Church and Haskell Curry

developed the lambda calculus, a simple but powerful mathematical theory of functions.

Page 37: 1 Functional Programming & Standard ML Hossein Hojjat et al.

37

Some History

• Alonzo Church is a famous computer scientist

• He had many doctoral students , such as Stephen C. Kleene or Alan Turing

Page 38: 1 Functional Programming & Standard ML Hossein Hojjat et al.

38

Some History

• 1960s:– John McCarthy developed Lisp, the

first functional language. Some influences from the lambda calculus, but still retained variable assignments.

Page 39: 1 Functional Programming & Standard ML Hossein Hojjat et al.

39

Some History

• 1978:– John Backus publishes award

winning article on FP, a functional language that emphasizes higher-order functions and calculating with programs.

Page 40: 1 Functional Programming & Standard ML Hossein Hojjat et al.

40

Some History

• Mid 1970s:– Robin Milner develops ML, the first of

the modern functional languages, which introduced type inference and polymorphic types.

Page 41: 1 Functional Programming & Standard ML Hossein Hojjat et al.

41

Some History

• Late 1970s - 1980s:– David Turner develops a number of

lazy functional languages leading up to Miranda, a commercial product.

• 1988:– A committee of prominent researchers

publishes the first definition of Haskell, a standard lazy functional language.

Page 42: 1 Functional Programming & Standard ML Hossein Hojjat et al.

42

Some History

• 1999:– The committee publishes the definition

of Haskell 98, providing a long-awaited stable version of the language.

Page 43: 1 Functional Programming & Standard ML Hossein Hojjat et al.

43

Standard ML

Page 44: 1 Functional Programming & Standard ML Hossein Hojjat et al.

44

Standard ML

• Historically, ML stands for metalanguage

• General-purpose functional programming language

• Developed by Robin Milner and others in the late 1970s at Edinburgh University

Page 45: 1 Functional Programming & Standard ML Hossein Hojjat et al.

45

Standard ML

• In 1969 Dana Scott introduced LCF, his Logic for Computable Functions

• It was a core higher-order call-by-name functional programming language with arithmetic, booleans and recursion at all types

• That lead to Milner et al's LCF system and then the programming language ML

Page 46: 1 Functional Programming & Standard ML Hossein Hojjat et al.

46

ML Syntax

• A program in ML, like any other language, is made up of various kinds of expressions.

Page 47: 1 Functional Programming & Standard ML Hossein Hojjat et al.

47

ML Syntax

syntactic class syntactic variable(s) and grammar rule(s) examples

identifiers x, y a, x, y, x_y, ...

constants c

...~2, ~1, 0, 1, 2 (integers) 1.0, ~0.001, 3.141 (reals)true, false (booleans)"hello", "", "!" (strings)#"A", #" " (characters)

unary operator u ~, not, size, ...

binary operators b +, *, -, >, <, >=, <=, ^, ...

expressions (terms)

e ::= x  |  c  |  u e  |  e1 b e2  | if e then e else e  |  

let d1...dn in e end  |  e (e1, ..., en)foo, ~0.001, not b, 2 + 2, 

declarations d ::= val x = e  |  fun y (x1:t1, ..., xn:tn): t = e val one = 1fun square(x: int): int

typest ::= int  |  real  |  bool  |  string  |  char  |  t1*...*tn->t

int, string, int->int, bool*int->bool

Adapted from Cornell lectures

Page 48: 1 Functional Programming & Standard ML Hossein Hojjat et al.

48

Programming in Standard ML

• Example: A simple function declaration that computes the absolute value of a real number:

Page 49: 1 Functional Programming & Standard ML Hossein Hojjat et al.

49

Programming in Standard ML

• The SML prompt lets you type either a term or a declaration that binds a variable to a term

• Running an ML program is just evaluating a term– The ML evaluator takes the left-most

expression that is not a value and reduces it to some simpler expression. Eventually the whole expression is a value and then evaluation stops: the program is done

Page 50: 1 Functional Programming & Standard ML Hossein Hojjat et al.

50

Programming in Standard ML

• For example consider evaluating abs(2.0+1.0):

abs(2.0+1.0) →

abs(3.0) →

if 3.0 < 0.0 then ~3.0 else 3.0 →

if false then ~3.0 else 3.0 →

3.0

Page 51: 1 Functional Programming & Standard ML Hossein Hojjat et al.

51

Programming in Standard ML

• The let expression works by:– Evaluating all of its bindings.– Those bindings are substituted into

the body of the let  expression (the expression in between in...end)

• Example:let val x = 1+4 in x*3 →let val x = 5 in x*3 → 5*3 → 15

Page 52: 1 Functional Programming & Standard ML Hossein Hojjat et al.

52

Programming in Standard ML

• Rather big example…

Page 53: 1 Functional Programming & Standard ML Hossein Hojjat et al.

53

Programming in Standard ML

Page 54: 1 Functional Programming & Standard ML Hossein Hojjat et al.

54

Some Good References

• Functional Programming Using Standard ML : Ake Wikstrom

• Introduction to Functional Programming : John Harrison

• Introduction to Standard ML,Robert Harper

• Cornell CS312 lectures

• And lots more …

Page 55: 1 Functional Programming & Standard ML Hossein Hojjat et al.

55

Any Questions ?!