Chapter 10 :: Functional Languages

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.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
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.
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.
Foundations of Programming Languages: Introduction to Lambda Calculus
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
The lambda calculus David Walker CS 441. the lambda calculus Originally, the lambda calculus was developed as a logic by Alonzo Church in 1932 –Church.
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Imperative Programming
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
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.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
1 ML fun x -> e e 1 e 2 0, 1, 2,..., +, -,... true, false, if e then e else e patterns datatypes exceptions structures functors let f x = e variables These.
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.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
COMP313A Functional Programming (1)
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
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.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
Functional Programming
Operational Semantics of Scheme
Functional Programming
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
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
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.
L Calculus.
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:

Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright © 2005 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 © 2005 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 © 2005 Elsevier

Church’s model of computing is called the lambda calculus 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 © 2005 Elsevier

Mathematicians established a distinction between 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) Logic programming is tied to the notion of constructive proofs, but at a more abstract level: the logic programmer writes a set of axioms that allow the computer to discover a constructive proof for each particular set of inputs Copyright © 2005 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 The key idea: do everything by composing functions no mutable state no side effects Copyright © 2005 Elsevier

Functional Programming Concepts Necessary features, many of which are missing in some imperative languages 1st class and high-order functions serious polymorphism powerful list facilities recursion structured function returns fully general aggregates garbage collection Copyright © 2005 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 © 2005 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 Copyright © 2005 Elsevier

Functional Programming Concepts Thinking about recursion as a direct, mechanical replacement for iteration, however, is the wrong way to look at things One has to get used to thinking in a recursive style Even more important than recursion is the notion of higher-order functions Take a function as argument, or return a function as a result Great for building things Copyright © 2005 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 © 2005 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 © 2005 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 © 2005 Elsevier

A Review/Overview of Scheme As mentioned, 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 © 2005 Elsevier

A Review/Overview of Scheme As mentioned earlier, 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 (+ 3 4)  7 ((+ 3 4)))  error (the ' ' arrow means 'evaluates to‘) Copyright © 2005 Elsevier

A Review/Overview of Scheme Boolean values #t and #f Numbers Lambda expressions Quoting (+ 3 4)  7 (quote (+ 3 4))  (+ 3 4) '(+ 3 4)  (+ 3 4) Mechanisms for creating new scopes (let ((square (lambda (x) (* x x))) (plus +)) (sqrt (plus (square a) (square b)))) let* letrec Copyright © 2005 Elsevier

A Review/Overview of Scheme Conditional expressions (if (< 2 3) 4 5)  4 (cond ((< 3 2) 1) ((< 4 3) 2) (else 3))  3 Imperative stuff assignments sequencing (begin) iteration I/O (read, display) Copyright © 2005 Elsevier

A Review/Overview of Scheme Scheme standard functions (this is not a complete list): arithmetic boolean operators equivalence list operators symbol? number? complex? real? rational? integer? Copyright © 2005 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 © 2005 Elsevier

A Review/Overview of Scheme Example program - Simulation of DFA Copyright © 2005 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 © 2005 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 © 2005 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 © 2005 Elsevier

Higher-order functions 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 later ML, Miranda, and Haskell have especially nice syntax for curried functions Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus A notation/model of computation based on purely syntactic symbol manipulation, in which everything is a function Developed by Alonzo Church in the '30's as a model for computability Church was one of a crowd that also included Chomsky, Turing, Kleene, and Rosser Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus We can define things like integers in terms of a distinguished function (like the identity function) that represents zero, and a successor function that gives us all the other numbers This makes it easy to define the arithmetic operators within the notation In practice this is kind of a nuisance we will just assume the existence of arithmetic and of distinguished constant functions for numbers Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus Example of lambda expressions id λx.x const λx.2 plus λx.λy.x + y square λx.x * x hypot λx. λy.sqrt (plus (square x) (square y)) Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus Recursively, a lambda expression is (1) a name (2) an abstraction consisting of a lambda, a name, a dot, and a lambda expression (3) an application consisting of two adjacent lambda expressions (juxtaposition means function application), or (4) a parenthesized lambda expression Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus Usually application associates left-to-right, so f A B is (f A) B, rather than f (A B) Also, application has higher precedence than abstraction, so λx.A B is λx.(A B), rather than (λx.A) B – e.g., ML Parentheses are used for clarity, or to break the rules: by convention, use them around any abstraction that is used as a function or an argument: (λf.f 2) (λx.plus x x) or around any application as an argument: double (minus 5 2) Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus These rules mean that the scope of the dot extends right all the way to the first unmatched right parentheses, or the end of the whole expression if there is no such parenthesis In (λx. λy. λz.e) a b c, the initial function takes a single argument and returns a function (of one argument) that returns a function (of one argument) To reduce the expression, you substitute a for any x's in λy. λz.e, then you substitute b for any y's in what remains, and then c for any z's in what remains Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus Example: (λx. λy.x + y) 3 4 λy.(3 + y) 4 (3 + 4) 7 Free and bound variables: a variable is bound if it is introduced by a lambda For example, in λx. λy.(* x y) we have two nested lambda expressions x is free in the inner one (λy.(* x y)), but bound in the outer Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus Evaluation of lambda expressions through (1) substituting in arguments (beta reduction) (λx.times x x) y => times y y (2) renaming variables (alpha conversion) (often to avoid naming conflicts) (λx.times x x) y ==(Lz.times z z) y (3) simplification "out of order" (eta reduction) (λx.f x) => f This last rule is hard to understand; it is NOT the same as beta reduction (see next example) Copyright © 2005 Elsevier

Theoretical Foundations Lambda Calculus (λx.f x) is a function, x is not its argument But if we apply the function to, say, y: (λx.f x) y we get the same result as by applying f to y To make things concrete, suppose f = (λz.times z z) ; squaring, then (λx.f x) 3 == (λx.(λz.times z z) x) 3 => beta (λz.times z z) 3 [outer] => beta times 3 3. Using eta reduction, we get the same result (λx.f x) 3 => eta f 3 == (λz.times z z) 3 => beta times 3 3 Copyright © 2005 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 © 2005 Elsevier

Functional Programming in Perspective 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 Copyright © 2005 Elsevier