Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
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.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Functional Programming Language (Introduction and Scheme) Lecture 17 – Functional Programming, Spring.
School of Computing and Mathematics, University of Huddersfield CAS810: WEEK 3 LECTURE: LAMBDA CALCULUS PRACTICAL/TUTORIAL: (i) Do exercises given out.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction 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.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
0 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)
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 Programming Chapter 14. History of Functional Languages Lisp is the second oldest language Motivated by a need to do symbolic, rather than.
Introduction to the λ-Calculus and Functional Programming Languages Arne Kutzner Hanyang University 2015.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Functional Programming Languages Chapter 14
Lambda Calculus History and Syntax. History The lambda calculus is a formal system designed to investigate function definition, function application and.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
© 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.
Chapter 9: Functional Programming in a Typed Language.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Compiling Functional Programs Mooly Sagiv Chapter 7
CS 363 Comparative Programming Languages Functional Languages: Scheme.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
Types and Programming Languages Lecture 6 Simon Gay Department of Computing Science University of Glasgow 2006/07.
COMP313A Functional Programming (1)
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
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 
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Haskell Introduction CSCE 314 Spring CSCE 314 – Programming Studio Historical Background 1930s: Alonzo Church develops the lambda calculus, a simple.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
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
Functional Programming
Functional Programming Languages
Conditional Expressions
CS 550 Programming Languages Jeremy Johnson
Functional Programming
A lightening tour in 45 minutes
More Functional Programming
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
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Announcements Quiz 6 HW7 due Tuesday, October 30
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
L Calculus.
COP4020 Programming Languages
CSE S. Tanimoto Lambda Calculus
Modern Programming Languages Lecture 18 Fakhar Lodhi
15.2 Mathematical Functions
Presentation transcript:

Functional Languages

Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution

History LISP is one of the oldest languages (‘56- ’59)! AI & John McCarthy Roots: –Church’s Lambda Calculus (Entscheidungsproblem!) –Church’s students

Functional Languages LISP and its family –LISP, Common LISP, Scheme Haskell Miranda ML FP (John Backus)

Recent Events Paul Graham – Hackers and Painters

Lambda Calculus Abstract definition of functions x.x*x Application: ( x.x*x)5 Multiple arguments x.( y.x*y)

Evaluation Bound Variables x.(x+y) Substitution –M[x/N] – sub N in for x in expression M –Valid when free variables in N are not bound in M –Variables can be renamed

Evaluation (cont.) Beta Reduction – ( x.x+y)(z+w) –( x.x+y)[x/(z+w)] –(z+w)+y

Two sides of the Lambda Calculus The programming language side is what we have seen Also – used for type systems The theory side used to proof the Entscheidungsproblem

Defining Numbers 0 = f. x.(x) 1 = f. x.(fx) 2 = f. x.(f(fx)) Succ = n. ( f.( x.(f(n f) x))) Add = m. ( n. ( f.( x.((mf)((nf)x))))))

Combinatory Calculus I = x.x K = x. y.x S = x. y. z.xz(yz)

Church-Rosser Theorem Order of application and reduction does not matter!

LISP Syntax – parenthesis! Functions are written in prefix list form: –(add 4 5)

LISP data structure CELL A List “(a b c)” a bc

Evaluation LISP assumes a list is a function unless evaluation is stopped! (a b c) - apply function a to arguments b and c Quote: ‘(a b c) – a list not to be evaluated

List Functions List Accessors car: (car ‘(a b c)) -> a cdr (cdr ‘(a b c)) -> (b c)

List Functions List constructors list (list ‘a ‘b) -> (a b) cons (cons ‘a ‘b) -> (a.b) What is happening here?

Symbolic Differentiation Example Pg 226

Haskell Better syntax! Named for Haskell Curry – a logician 1999

Expressions Use normal infix notation Lists as a fundamental data type (most functional languages provide this) –Enumeration -> evens=[0, 2, 4, 6] –Generation -> Moreevens = [ 2*x | x <- [0, ]] –Construction 8:[] – [8]

Expressions (cont.) List accessors – head and tail –head evens – 0 –tail evens – [2,4,6] List concatenation –[1, 2] ++ [3, 4] – [1, 2, 3, 4]

Control Flow If The Else statements Functions: name :: Domain -> Range name | ….

Example Function max3 :: Int -> Int -> Int -> Int Max3 x y z | x >=y && x >= z = x | y >= x && y >= z = y | otherwise = z

Recursion and Iteration Recursion follows the obvious pattern Iteration uses lists: –Fact n = product [1.. N] Another pattern for recursion on lists –mySum [] = 0 –mySum [x : xs] = x + mySum[xs]

Implementation Lazy evaluation Eager evaluation Graph reduction

SECD Machine Four Data Structures (lists) S – stack, expression evaluation E – environment list of C – control string (e.g. program) D – dump, the previous state for function return

Data types Expr – –ID(identifier) –Lambda(identifier, expr) –Application(expr 1, expr 2 ) (apply symbol) WHNF – –INT(number) –Primary(WHNF -> WHNF) –Closure(expr, identifier, list( (identifier, WHNF)))

Data types (cont.) Stack – list (WHNF) Environment – list ( ) Control – list ( expr) Dump – list ( ) State – –

Evaluate Function Evaluate maps State -> WHNF Cases for Control List empty: Evaluate(Result::S,E,nil,nil) -> Result Evaluate(x::S,E,nil,(S1,E1,C1)) -> Evaluate(x::S1,E1,C1,D)

Evaluate Function (cont.) Evaluate(S,E,ID(x)::C,D) -> Evaluate(LookUp(x,E)::S,E,C,D) Evaluate(S,E,LAMBDA(id,expr),D) -> Evaluate( Closure(expr,id,E1)::S, E,C,D)

Evaluate Function (cont.) -> Evaluate(nil, (id,arg)::E1, expr, (S,E,C)::D)

Evaluate Function (cont.) -> Evaluate(f(arg)::S,E,C,D) Evaluate(S,E,Application(fun,arg)::C,D) ->

Functional Programming Little used in commercial circles Used in some research circle May hold the future for parallel computation