functional languages

33
Functional Languages

Upload: opa

Post on 06-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

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!) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Functional Languages

Functional Languages

Page 2: Functional Languages

Why?

• Referential Transparency

• Functions as first class objects

• Higher level of abstraction

• Potential for parallel execution

Page 3: Functional Languages

History

• LISP is one of the oldest languages (‘56-’59)!

• AI & John McCarthy

• Roots:– Church’s Lambda Calculus

(Entscheidungsproblem!)– Church’s students

Page 4: Functional Languages

Functional Languages

• LISP and its family– LISP, Common LISP, Scheme

• Haskell

• Miranda

• ML

• FP (John Backus)

Page 5: Functional Languages

Recent Events

• Paul Graham – Hackers and Painters

Page 6: Functional Languages

Lambda Calculus

• Abstract definition of functions

x.x*x

Application:

(x.x*x)5

Multiple arguments

x.(y.x*y)

Page 7: Functional Languages

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

Page 8: Functional Languages

Evaluation (cont.)

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

Page 9: Functional Languages

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

Page 10: Functional Languages

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

Page 11: Functional Languages

Combinatory Calculus

• I = x.x

• K = x.y.x

• S = x.y.z.xz(yz)

Page 12: Functional Languages

Church-Rosser Theorem

• Order of application and reduction does not matter!

Page 13: Functional Languages

LISP

• Syntax – parenthesis!

• Functions are written in prefix list form:– (add 4 5)

Page 14: Functional Languages

LISP data structureCELL

A List “(a b c)”

a b c

Page 15: Functional Languages

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

Page 16: Functional Languages

List Functions

List Accessors

car: (car ‘(a b c)) -> a

cdr (cdr ‘(a b c)) -> (b c)

Page 17: Functional Languages

List Functions

List constructors

list (list ‘a ‘b) -> (a b)

cons (cons ‘a ‘b) -> (a.b)

What is happening here?

Page 18: Functional Languages

Symbolic Differentiation ExamplePg 226

Page 19: Functional Languages

Haskell

• Better syntax!

• Named for Haskell Curry – a logician

• 1999

Page 20: Functional Languages

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,1 .. 101]]

– Construction• 8:[] – [8]

Page 21: Functional Languages

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]

Page 22: Functional Languages

Control Flow

• If The Else statements

• Functions:name :: Domain -> Range

name <vars>| <s1>

| <s2>

….

Page 23: Functional Languages

Example Function

max3 :: Int -> Int -> Int -> Int

Max3 x y z

| x >=y && x >= z = x

| y >= x && y >= z = y

| otherwise = z

Page 24: Functional Languages

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]

Page 25: Functional Languages

Implementation

• Lazy evaluation

• Eager evaluation

• Graph reduction

Page 26: Functional Languages

SECD Machine

• Four Data Structures (lists)

• S – stack, expression evaluation

• E – environment list of <id, value>

• C – control string (e.g. program)

• D – dump, the previous state for function return

Page 27: Functional Languages

Data types

• Expr – – ID(identifier)

– Lambda(identifier, expr)

– Application(expr1, expr2)

– @ (apply symbol)

• WHNF –– INT(number)

– Primary(WHNF -> WHNF)

– Closure(expr, identifier, list( (identifier, WHNF)))

Page 28: Functional Languages

Data types (cont.)

• Stack – list (WHNF)

• Environment – list ( <identifier, WHNF>)

• Control – list ( expr)

• Dump – list (<Stack, Environment, Control>)

• State – – <Stack, Environment, Control, Dump>

Page 29: Functional Languages

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)

Page 30: Functional Languages

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)

Page 31: Functional Languages

Evaluate Function (cont.)

• Evaluate(Closure(expr,id,E1)::(arg::S),E, @::C,D) ->

Evaluate(nil,

(id,arg)::E1,

expr,

(S,E,C)::D)

Page 32: Functional Languages

Evaluate Function (cont.)

• Evaluate(Primary(f)::(arg::S),E,@::C,D) -> Evaluate(f(arg)::S,E,C,D)

• Evaluate(S,E,Application(fun,arg)::C,D) -> Evaluate(S,E,arg::(fun::(@::C)),D)

Page 33: Functional Languages

Functional Programming

• Little used in commercial circles

• Used in some research circle

• May hold the future for parallel computation