1 / 55

Functional Programming & Standard ML

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?

Download Presentation

Functional Programming & Standard ML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

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

  4. Introduction

  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.

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

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

  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. 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. 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. Introduction • But …

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

  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. 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. Introduction • Instead of sequencing and looping, functional languages use recursive functions

  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”?

  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. 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. Why Functional Programming?

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

  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. 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. Why Functional Programming?

  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. 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. Why Functional Programming? • Purely declarative languages guarantee referential transparency • It makes it easier to understand how a program works

  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. Why Functional Programming? • “Start with programming languages as an abstract notation for specifying algorithms and then work down to the hardware.” (Dijkstra 1976)

  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. 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. “Beware of bugs in the above code; I have only proved it correct, not tried it.” ,Donald Knuth

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

  35. Some History

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

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

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

  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. Some History • Mid 1970s: • Robin Milner develops ML, the first of the modern functional languages, which introduced type inference and polymorphic types.

  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. Some History • 1999: • The committee publishes the definition of Haskell 98, providing a long-awaited stable version of the language.

  43. Standard ML

  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. 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. ML Syntax • A program in ML, like any other language, is made up of various kinds of expressions.

  47. ML Syntax Adapted from Cornell lectures

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

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

More Related