Tải bản đầy đủ (.pptx) (33 trang)

functional programming languages

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (270.38 KB, 33 trang )




 !"#
COMS W4115 - PLT Columbia University 1 April 24, 2013
Functional Programming Languages
and the Influence of Lambda Calculus
Why Functional Programming?
COMS W4115 - PLT Columbia University 2 April 24, 2013
You may have heard that:
(1)
functional programs are very succinct and
elegant,
but…
(2) they take forever to
write and understand
$%& '%
(')*+$, $
/)+$, $00
$%& '%
(')*+$, $
/)+$, $00
Simple XOR program:
Outline
COMS W4115 - PLT Columbia University 3 April 24, 2013
I. What is Functional Programming?
II. Lambda Calculus' Influence on Functional Programming
III. Benefits of Functional Programming
IV. OCaml in Action: An Entire Interpreter in
3 slides
What is Functional Programming?


COMS W4115 - PLT Columbia University 4 April 24, 2013

A "programming paradigm" that focuses on the evaluation of functions

Programs are defined as recursive functions, where each takes a single input and outputs a
single result.

Recall lambda calculus nests functions in this manner. Ex: (+ (* 1 2) (- 4 3))
*&
 &1
 !&-2
3!00
*&
 &1
 !&-2
3!00
In OCaml:
What is Functional Programming?
COMS W4115 - PLT Columbia University 5 April 24, 2013

One important trademark of Functional Programming:
It avoids both changes in state and mutable data
These expressions are
not equal.
Conceptually, they
are more like this.
4456 446
&0 &0
4456 446
&0 7 ),8

0
9
/>What is Functional Programming?
COMS W4115 - PLT Columbia University 6 April 24, 2013
Some Common (more
or less) FP Languages:

LISP

OCaml

Haskell

Erlang

F# (multi-paradigm)
So what's the value in functional
programming?
Are its benefits worth taking the time to
learn it and overcoming the (possibly) steep
learning curve?
Outline
COMS W4115 - PLT Columbia University 7 April 24, 2013
I.
What is Functional Programming?
II.
Lambda Calculus' Influence on Functional Programming
III.
Benefits of Functional Programming
IV.

OCaml in Action: An Entire Interpreter in
3 slides
Lambda Calculus' Influence
COMS W4115 - PLT Columbia University 8 April 24, 2013

The syntax and theory of Functional Programming is highly
influenced by lambda calculus.

Knowledge of lambda calculus means that FP is easier to learn,
understand, and use efficiently.
You might recognize some of the following FP syntax traits from
lambda calculus…
Lambda Calculus' Influence
Function Abstraction
COMS W4115 - PLT Columbia University 9 April 24, 2013
Lambda Calculus Recap
A function abstraction consists of four parts:
1.
a lambda followed by
2.
a single variable,
3.
a period, and then
4.
an expression

:$"$%
Lambda Calculus' Influence
Function Abstraction
COMS W4115 - PLT Columbia University 10 April 24, 2013

:$"$%
:$"$%
Lambda Calculus:
*$ $%00
*$ $%00
OCaml:
*)$,&$%
*)$,&$%
Algebra:
Lambda Calculus' Influence
Function Abstraction
COMS W4115 - PLT Columbia University 11 April 24, 2013
is equivalent to

Named Functions: OCaml strays from
pure functional programming at times.

The "let" command can be used to allow
one to create named functions
*$ $%00
*$ $%00
5 $&$%00
5 $&$%00
The Let Command
Lambda Calculus' Influence
Function Abstraction
COMS W4115 - PLT Columbia University 12 April 24, 2013
6* %&
)%-"2";,4""00
6* <"=00

->?&2="<<<<<<<<
6* %&
)%-"2";,4""00
6* <"=00
->?&2="<<<<<<<<
#!* )#!%,8
)%-2,4"0
9
* )<"=,0
#!* )#!%,8
)%-2,4"0
9
* )<"=,0
An OCaml "let" example
Similar code
in Java
Lambda Calculus' Influence
Beta Reductions
COMS W4115 - PLT Columbia University 13 April 24, 2013
Lambda Calculus Recap
A function application is evaluated via a beta
reduction
Which occurs when an actual value is
substituted for a variable
Examples:

):x"xzx,y@Ay4xBxzx &yzy
):$"3$,C@3C&
Layman's Terms: (λx.expr1)expr2 means "replace every instance of x in expr1 with expr2"
Lambda Calculus' Influence

Beta Reductions
COMS W4115 - PLT Columbia University 14 April 24, 2013
):$"$%,$%
):$"$%,$%
Lambda Calculus:
)*$ $%,$%00
)*$ $%,$%00
OCaml:
)*$ $1$,00
)1DE1&1,
)*$ $1$,00
)1DE1&1,
Example Usage:
Lambda Calculus' Influence
Beta Reductions
COMS W4115 - PLT Columbia University 15 April 24, 2013
)*$ $%,$%00
)*$ $%,$%00
$&$1$00
)1DE1&1,
$&$1$00
)1DE1&1,
Example Usage:
The Let Command
$&$%$%00
$&$%$%00
Is semantically equivalent to:
The "let" command can once again be used
to perform similar functionality to the
"fun" command.

Outline
COMS W4115 - PLT Columbia University 16 April 24, 2013
I.
What is Functional Programming?
II.
Lambda Calculus' Influence on Functional Programming
III.
Benefits of Functional Programming
IV.
OCaml in Action: An Entire Interpreter in
3 slides
Benefits of Functional Programming
COMS W4115 - PLT Columbia University 17 April 24, 2013
A few benefits in a nutshell:
1.
Succinct Code
2.
Encourages disciplined
and logical thinking
3.
Lazy Evaluation
4.
Higher Level Functions

Example:
"*#FG)* 3,;A0C0;B00

5.
No Side Effects & Referential Transparency
Benefits of Functional Programming

No Side Effects
COMS W4115 - PLT Columbia University 18 April 24, 2013
Side Effect:
a function or expression has a side effect if it
modifies state or has an observable
interaction with the "outside world."
Examples:

Modify global/static variables

Modify arguments

Write data to a display or file
Functional programs contain no assignment statements. So variables, once given a value, never
change.
Benefits of Functional Programming
No Side Effects
COMS W4115 - PLT Columbia University 19 April 24, 2013
Simple Side Effect Example in Java
7 $&0
7 E# 'H),8
$&0
9
'H),0 44'* 7''#I 
44* ''E*!E$
7 $&0
7 E# 'H),8
$&0
9
'H),0 44'* 7''#I 

44* ''E*!E$
Benefits of Functional Programming
Referential Transparency
COMS W4115 - PLT Columbia University 20 April 24, 2013
Referential Transparency: An expression
(e.g., function) is referentially transparent if
it always produces the same output for the
same input.
Lack of side effects results in referential
transparency.
Because of this behavior, a variable/expression
can always be replaced with its value without
changing the behavior of a program if that
language has Referential Transparency.
Benefits of Functional Programming
Referential Transparency
COMS W4115 - PLT Columbia University 21 April 24, 2013
Lack of Referential Transparency in Java
7 $&;0
7 ##),8
3$0
9
##),0 44
$&;0
##),044;+E''%"JKL
7 $&;0
7 ##),8
3$0
9
##),0 44

$&;0
##),044;+E''%"JKL
Why are Referential Transparency and Lack of Side Effects
Good?
COMS W4115 - PLT Columbia University 22 April 24, 2013

Elimination of bugs

Debugging in general is easier

Independence of evaluation order

Safe reuse of subprograms

Safe multithreading
Why are Referential Transparency and Lack of Side Effects
Good?
COMS W4115 - PLT Columbia University 23 April 24, 2013
Easier to Compile – It's not only people that benefit from being able to better predict and
understand a program's behavior
Better Compiling – optimization via:

Memoization

Parallelization

Common Subexpression Elimination
Easier and Better Compiling
Outline
COMS W4115 - PLT Columbia University 24 April 24, 2013

I.
What is Functional Programming?
II.
Lambda Calculus' Influence on Functional Programming
III.
Benefits of Functional Programming
IV.
OCaml in Action: An Entire Interpreter in
3 slides
Implementing an Interpreter in OCaml
COMS W4115 - PLT Columbia University 25 April 24, 2013

We will build a simple desk calculator to perform
integer arithmetic.

You recall the basic block diagram of a simple interpreter:
Lexer
Lexer
Parser
Parser
AST Walker
AST Walker
tokens AST
Input
(arithmetic expression)
Output
(evaluated arithmetic
expression)

×