560 likes | 693 Views
Functional Programming & Standard ML. Hossein Hojjat et al. 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. Outline. Introduction Why functional programming?
E N D
Functional Programming & Standard ML Hossein Hojjat et al.
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
Outline • Introduction • Why functional programming? • Some History • Standard ML • ML Syntax • Programming in Standard ML • Some Good References
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.
Introduction • display and rendering: PostScript, HTML, XML, VRML, etc. • hardware: SystemC, VHDL, Esterelle • theorem proving and mathematics: Mathematica, Maple, Matlab, NuPRL, Coq • others?
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
Introduction • Design of imperative languages is based directly on the von Neumann architecture
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
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
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
Introduction • But …
Introduction • Functional programs don’t use variables - there is no state • Therefore they cannot use assignments : there is nothing to assign to
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
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
Introduction • Instead of sequencing and looping, functional languages use recursive functions
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”?
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
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)
Why Functional Programming? • Backus' famous paper encouraged much interest in functional languages as a means of breaking the von-Neumann bottleneck The von Neumann bottleneck
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.
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
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.
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
Why Functional Programming? • Purely declarative languages guarantee referential transparency • It makes it easier to understand how a program works
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
Why Functional Programming? • “Start with programming languages as an abstract notation for specifying algorithms and then work down to the hardware.” (Dijkstra 1976)
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
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
“Beware of bugs in the above code; I have only proved it correct, not tried it.” ,Donald Knuth
Why Functional Programming? • Functional Programming is an area of current research • ACM Conference on LISP and Functional Programming
Some History • 1940s: • Alonzo Church and Haskell Curry developed the lambda calculus, a simple but powerful mathematical theory of functions.
Some History • Alonzo Church is a famous computer scientist • He had many doctoral students , such as Stephen C. Kleene or Alan Turing
Some History • 1960s: • John McCarthy developed Lisp, the first functional language. Some influences from the lambda calculus, but still retained variable assignments.
Some History • 1978: • John Backus publishes award winning article on FP, a functional language that emphasizes higher-order functions and calculating with programs.
Some History • Mid 1970s: • Robin Milner develops ML, the first of the modern functional languages, which introduced type inference and polymorphic types.
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.
Some History • 1999: • The committee publishes the definition of Haskell 98, providing a long-awaited stable version of the language.
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
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
ML Syntax • A program in ML, like any other language, is made up of various kinds of expressions.
ML Syntax Adapted from Cornell lectures
Programming in Standard ML • Example: A simple function declaration that computes the absolute value of a real number:
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
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