Introduction to Functional Programming

Introduction to Functional Programming

Intro duction to Functional Programming Lecture Intro duction to Functional Programming John Harrison University of Cambridge Lecture Intro duction and Overview Topics covered Imp erative programming Functional programming The merits of functional programming Historical remarks Overview of the course John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Imp erative programming Imp erative or pro cedural programs rely on mo difying a state by using a sequence of commands The state is mainly mo died by the assignment v E or v E command written We can execute one command b efore another by writing them in sequence p erhaps separated by a C C semicolon Commands can b e executed conditionally using if and rep eatedly using while Programs are a series of instructions on how to mo dify the state FORTRAN Algol C Imp erative languages eg Mo dula supp ort this style of programming John Harrison University of Cambridge January Intro duction to Functional Programming Lecture An abstract view We ignore inputoutput op erations and assume that a program runs for a limited time pro ducing a result We can consider the execution in an abstract way as ! ! ! ! n The program is started with the computer in an including the inputs to the initial state program The program nishes with the computer in a nal containing the outputs of the program state n The state passes through a nite sequence of changes to get from to in general each n command may mo dify the state John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Functional programming A functional program is simply an expression and executing the program means evaluating the expression We can relate this to the imp erative E view by writing n There is no state ie there are no variables Therefore there is no assignment since theres nothing to assign to And there is no sequencing and no rep etition since one expression do es not aect another But on the p ositive side We can have recursive functions giving something comparable to rep etition Functions can b e used much more exibly higher order functions eg we can have Functional languages supp ort this style of programming John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Example the factorial The factorial function can b e written imp eratively in C as follows int factint n int x while n x x n n n return x whereas it would b e expressed in ML as a recursive function fun fact n if n then else n factn John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Why At rst sight a language without variables assignment and sequencing lo oks very impractical We will show in this course how a lot of interesting programming can b e done in the functional style Imp erative programming languages have arisen as an abstraction of the hardware from machine co de through assemblers and macro assemblers to FORTRAN and b eyond Perhaps this is the wrong approach and we should approach the task from the human side Mayb e functional languages are b etter suited to p eople But what concrete reasons are there for preferring functional languages John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Merits of functional programming By avoiding variables and assignments we gain the following advantages Clearer semantics Programs corresp ond more directly to abstract mathematical ob jects More freedom in implementation eg parallelizability By the more exible use of functions we gain Conciseness and elegance Better parametrization and mo dularity of programs Convenient ways of representing innite data John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Denotational semantics We can identify our ML factorial function with an abstract mathematical partial function Z ! Z 8 < n if n factn : ? otherwise where ? denotes undenedness since for negative arguments the program fails to terminate Once we have a state this simple interpretation no longer works Here is a C function that do esnt corresp ond to any mathematical function int randvoid static int n return n n This gives dierent results on successive calls John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Semantics of imp erative programs In order to give a corresp onding semantics to imp erative programs we need to make the state explicit For example we can mo del commands as Partial functions ! Strachey Relations on Hoare Predicate transformers ie total functions ! bool ! ! bool Dijkstra If we allow the goto statement even these are not enough and we need a semantics based on continuations Wadsworth Morris All these metho ds are quite complicated With functional programs we have a real chance of proving their correctness or the correctness of certain transformations or optimizations John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Problems with functional programs Functional programming is not without its deciencies Some things are harder to t into a purely functional mo del eg Inputoutput Interactive or continuously running programs eg editors pro cess controllers However in many ways innite data structures can b e used to accommo date these things Functional languages also corresp ond less closely to current hardware so they can b e less ecient and it can b e hard to reason ab out their time and space usage ML is not a pure functional language so you can use variables and assignments if required However most of our work is in the pure functional subset John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Historical remarks Some of the ideas b ehind functional programming go back a long way eg to lamb da calculus a logical formalism due to Alonzo Church invented in the s b efore electronic computers The earliest real functional programming language was LISP invented by McCarthy in the s However this had a numb er of defects which we will discuss later The mo dern trend really b egins with ISWIM invented by Peter Landin in the s The ML family started with Robin Milners theorem prover Edinburgh LCF in the late s The language we shall study is essentially core Standard ML but there are other imp ortant dialects notably CAML and Ob jective CAML John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Overview of the course Practicalities of interacting with ML Key functional concepts eg evaluation strategy higher order functions Polymorphic typ es Recursive functions and recursive structures Hints for eective programming Exceptions references and other imp erative features Proving programs correct John Harrison University of Cambridge January Intro duction to Functional Programming Lecture Overview of the course We want to show the p ower of ML so well nish with more substantial examples that illustrate some of the p ossibilities Symb olic dierentiation Recursive descent parsing A Prolog interpreter A theorem prover The co de for these examples will b e made available on Thor John Harrison University of Cambridge January .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    13 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us