Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.

Slides:



Advertisements
Similar presentations
CS 345: Chapter 9 Algorithmic Universality and Its Robustness
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Paradigms
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
MATHEMATICAL FOUNDATIONS SUPPLEMENTAL MATERIAL – NOT ON EXAM.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Dr. Muhammed Al-Mulhem ICS An Introduction to Functional Programming.
Interlude: Functional Programming CSE 331 Section 2 James Daly.
Imperative Programming
Chapter 10 :: Functional Languages
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
Functional Programming in Scheme
ISBN Chapter 15 Functional Programming Languages.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
Λ => Scheme for Rubyists. Scheme History Authors: Guy Steele and Gerald Sussman Structure and Interpretation of Computer Programs (SICP) by Abelson &
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 8.
Chapter Fifteen: Functional Programming Languages Lesson 12.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
1-1 An Introduction to Functional Programming Sept
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
Haskell Introduction CSCE 314 Spring CSCE 314 – Programming Studio Historical Background 1930s: Alonzo Church develops the lambda calculus, a simple.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Functional Programming
CS314 – Section 5 Recitation 9
Functional Programming
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
CS 3304 Comparative Languages
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Chapter 11 :: Functional Languages
CS 326 Programming Languages, Concepts and Implementation
CS 3304 Comparative Languages
Principles of programming languages 4: Parameter passing, Scope rules
Closures and Streams cs784(Prasad) L11Clos
Chap. 6 :: Control Flow Michael L. Scott.
FP Foundations, Scheme In Text: Chapter 14.
Chap. 6 :: Control Flow Michael L. Scott.
COP4020 Programming Languages
CSE S. Tanimoto Lambda Calculus
Functional Programming and Haskell
Chapter 15 Functional Programming 6/1/2019.
Functional Programming and Haskell
Presentation transcript:

Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott

Copyright © 2009 Elsevier Historical Origins The imperative and functional models grew out of work undertaken Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s –different formalizations of the notion of an algorithm, or effective procedure, based on automata, symbolic manipulation, recursive function definitions, and combinatorics These results led Church to conjecture that any intuitively appealing model of computing would be equally powerful as well –this conjecture is known as Church ’ s thesis

Copyright © 2009 Elsevier Historical Origins Mathematicians established a distinction between –constructive proof (one that shows how to obtain a mathematical object with some desired property) –nonconstructive proof (one that merely shows that such an object must exist, e.g., by contradiction)

Copyright © 2009 Elsevier Historical Origins Turing ’ s model of computing was the Turing machine a sort of pushdown automaton using an unbounded storage “ tape ” –the Turing machine computes in an imperative way, by changing the values in cells of its tape – like variables just as a high level imperative program computes by changing the values of variables

Copyright © 2009 Elsevier Historical Origins Church ’ s model of computing is called the lambda calculus –based on the notion of parameterized expressions (with each parameter introduced by an occurrence of the letter λ—hence the notation ’ s name. –Lambda calculus was the inspiration for functional programming –one uses it to compute by substituting parameters into expressions, just as one computes in a high level functional program by passing arguments to functions

Copyright © 2009 Elsevier Functional Programming Concepts Functional languages such as Lisp, Scheme, FP, ML, Miranda, and Haskell are an attempt to realize Church's lambda calculus in practical form as a programming language

Copyright © 2009 Elsevier Functional Programming Concepts Key Idea: Define the outputs of a program as a mathematical function of the inputs –No mutable state –No side-effects –Emphasizes recursion rather than iteration

Origins of Functional Languages AI modules for game playing in the 1950s Branch-and-bound algorithms ideally suited for recursion

Copyright © 2009 Elsevier Functional Programming Concepts Significant features, many of which are missing in some imperative languages –1st class and high-order functions –implicit, parametric polymorphism –powerful list facilities –structured function returns –fully general aggregates –garbage collection

Copyright © 2009 Elsevier Functional Programming Concepts So how do you get anything done in a functional language? –Recursion (especially tail recursion) takes the place of iteration –In general, you can get the effect of a series of assignments x := 0... x := expr1... x := expr2... from f3(f2(f1(0))), where each f expects the value of x as an argument, f1 returns expr1, and f2 returns expr2

Copyright © 2009 Elsevier Functional Programming Concepts Recursion even does a nifty job of replacing looping x := 0; i := 1; j := 100; while i < j do x := x + i*j; i := i + 1; j := j - 1 end while return x becomes f(0,1,100), where f(x,i,j) == if i < j then f (x+i*j, i+1, j-1) else x

Natural Recursive Problems Recurrences –E.g., factorial: 0! = 1 1! = 1 n! = n * (n-1)! –E.g., greatest common divisor int gcd(int a, int b) { if (a == b) return a; else if (a > b) return gcd(a - b, b); else return gcd(a, b - a); } Tree traversals Graph traversals

Speeding Up Recursion Tail recursion: Recursion in which additional computation never follows a recursive call Compiler optimizes a tail recursive function by re- using the stack frame for the function Example int gcd(int a, int b) { start: if (a == b) return; else if (a > b) { a = a - b; goto start; } else { b = b - a; goto start; } }

Transforming Recursion to Use Tail Recursion continuations: arguments that contain intermediate results which get passed to successive recursive calls Example factorial(n, product) { if (n == 0 or n == 1) return product else return factorial(n-1, product * n) }

Continuations are like imperative programming Example: The fibonacci recurrence relation fib 0 = 0 fib 1 = 1 fib n = fib n-1 + fib n-2 a natural functional implementation: fib(n) { if (n == 0) return 0 else if (n == 1) return 1 else return fib(n-1) + fib(n-2) }

Continuations (cont) An efficient C implementation int fib(int n) { int f1 = 0; f2 = 1; int i; for (i = 2; i <= n; i++) { int temp = f1 + f2; f1 = f2; f2 = temp; } return f2; }

Continuations (cont) A functional implementation of fib using continuations fib(n) { fib-helper(f1, f2, i) { if (i == n) return f2; else return fib-helper(f2, f1 + f2, i+1); } return fib-helper(0, 1, 0); }

Copyright © 2009 Elsevier Functional Programming Concepts Lisp also has (these are not necessary present in other functional languages) –homo-iconography –self-definition –read-evaluate-print Variants of LISP –Pure (original) Lisp –Interlisp, MacLisp, Emacs Lisp –Common Lisp –Scheme

Copyright © 2009 Elsevier Functional Programming Concepts Pure Lisp is purely functional; all other Lisps have imperative features All early Lisps dynamically scoped –Not clear whether this was deliberate or if it happened by accident Scheme and Common Lisp statically scoped –Common Lisp provides dynamic scope as an option for explicitly-declared special functions –Common Lisp now THE standard Lisp Very big; complicated (The Ada of functional programming)

Copyright © 2009 Elsevier Functional Programming Concepts Scheme is a particularly elegant Lisp Other functional languages –ML –Miranda –Haskell –FP Haskell is the leading language for research in functional programming

Copyright © 2009 Elsevier A Review/Overview of Scheme Scheme is a particularly elegant Lisp –Interpreter runs a read-eval-print loop –Things typed into the interpreter are evaluated (recursively) once –Anything in parentheses is a function call (unless quoted) –Parentheses are NOT just grouping, as they are in Algol-family languages Adding a level of parentheses changes meaning

Copyright © 2009 Elsevier A Review/Overview of Scheme Example program - Simulation of DFA We'll invoke the program by calling a function called 'simulate', passing it a DFA description and an input string –The automaton description is a list of three items: start state the transition function the set of final states –The transition function is a list of pairs the first element of each pair is a pair, whose first element is a state and whose second element in an input symbol if the current state and next input symbol match the first element of a pair, then the finite automaton enters the state given by the second element of the pair

Copyright © 2009 Elsevier A Review/Overview of Scheme Example program - Simulation of DFA

Copyright © 2009 Elsevier A Review/Overview of Scheme Example program - Simulation of DFA

Copyright © 2009 Elsevier Evaluation Order Revisited Applicative order –what you're used to in imperative languages –usually faster Normal order –like call-by-name: don't evaluate arg until you need it –sometimes faster –terminates if anything will (Church-Rosser theorem)

Copyright © 2009 Elsevier Evaluation Order Revisited In Scheme –functions use applicative order defined with lambda –special forms (aka macros) use normal order defined with syntax-rules A strict language requires all arguments to be well-defined, so applicative order can be used A non-strict language does not require all arguments to be well-defined; it requires normal-order evaluation

Copyright © 2009 Elsevier Evaluation Order Revisited Lazy evaluation gives the best of both worlds But not good in the presence of side effects. –delay and force in Scheme –delay creates a "promise"

Copyright © 2009 Elsevier High-Order Functions Higher-order functions –Take a function as argument, or return a function as a result –Great for building things –Currying (after Haskell Curry, the same guy Haskell is named after) For details see Lambda calculus on CD ML, Miranda, and Haskell have especially nice syntax for curried functions

Copyright © 2009 Elsevier Functional Programming in Perspective Advantages of functional languages –lack of side effects makes programs easier to understand –lack of explicit evaluation order (in some languages) offers possibility of parallel evaluation (e.g. MultiLisp) –lack of side effects and explicit evaluation order simplifies some things for a compiler (provided you don't blow it in other ways) –programs are often surprisingly short –language can be extremely small and yet powerful

Copyright © 2009 Elsevier Problems –difficult (but not impossible!) to implement efficiently on von Neumann machines lots of copying of data through parameters (apparent) need to create a whole new array in order to change one element heavy use of pointers (space/time and locality problem) frequent procedure calls heavy space use for recursion requires garbage collection requires a different mode of thinking by the programmer difficult to integrate I/O into purely functional model Functional Programming in Perspective