Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Functional Programming & Standard ML Hossein Hojjat et al.

Similar presentations


Presentation on theme: "1 Functional Programming & Standard ML Hossein Hojjat et al."— Presentation transcript:

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

2 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

3 3 Outline Introduction Why functional programming? Some History Standard ML ML Syntax Programming in Standard ML Some Good References

4 4 Introduction

5 5 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.

6 6 Introduction display and rendering: PostScript, HTML, XML, VRML, etc. hardware: SystemC, VHDL, Esterelle theorem proving and mathematics: Mathematica, Maple, Matlab, NuPRL, Coq others?

7 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

8 8 Introduction

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

10 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

11 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

12 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

13 13 Introduction But …

14 14 Introduction Functional programs don’t use variables - there is no state Therefore they cannot use assignments : there is nothing to assign to

15 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

16 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

17 17 Introduction Instead of sequencing and looping, functional languages use recursive functions

18 18 Introduction Example: f(n) = 1if 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”?

19 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

20 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)

21 21 Why Functional Programming?

22 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

23 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.

24 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

25 25 Why Functional Programming?

26 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.

27 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

28 28 Why Functional Programming? Purely declarative languages guarantee referential transparency It makes it easier to understand how a program works

29 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

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

31 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

32 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

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

34 34 Why Functional Programming? Functional Programming is an area of current research ACM Conference on LISP and Functional Programming

35 35 Some History

36 36 Some History 1940s: –Alonzo Church and Haskell Curry developed the lambda calculus, a simple but powerful mathematical theory of functions.

37 37 Some History Alonzo Church is a famous computer scientist He had many doctoral students, such as Stephen C. Kleene or Alan Turing

38 38 Some History 1960s: –John McCarthy developed Lisp, the first functional language. Some influences from the lambda calculus, but still retained variable assignments.

39 39 Some History 1978: –John Backus publishes award winning article on FP, a functional language that emphasizes higher- order functions and calculating with programs.

40 40 Some History Mid 1970s: –Robin Milner develops ML, the first of the modern functional languages, which introduced type inference and polymorphic types.

41 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.

42 42 Some History 1999: –The committee publishes the definition of Haskell 98, providing a long- awaited stable version of the language.

43 43 Standard ML

44 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

45 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

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

47 47 ML Syntax syntactic classsyntactic 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 | e 1 b e 2 | if e then e else e | let d 1... d n in e end | e ( e 1,..., e n ) foo, ~0.001, not b, 2 + 2, declarations d ::= val x = e | fun y ( x 1 : t 1,..., x n : t n ): t = e val one = 1 fun square(x: int): int types t ::= int | real | bool | string | char | t 1 *... * t n -> t int, string, int->int, bool*int- >bool Adapted from Cornell lectures

48 48 Programming in Standard ML Example: A simple function declaration that computes the absolute value of a real number:

49 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

50 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

51 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

52 52 Programming in Standard ML Rather big example…

53 53 Programming in Standard ML

54 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 …

55 55 Any Questions ?!


Download ppt "1 Functional Programming & Standard ML Hossein Hojjat et al."

Similar presentations


Ads by Google