Functional Programming - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Functional Programming

Description:

Definition: Two lists have the same fringe if they contain the same ... Lambdas can only describe functions of a single variable (?n.(?m. ...)) A wrong example: ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 19
Provided by: cmsDt
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming


1
CHAPTER 11
  • Functional Programming
  • Part 3

2
The Mathematics of Functional Programming II
Lambda Calculus
  • Turing machines
  • Express computation by a computer
  • A model for procedural programming languages
  • Lambda calculus
  • Express computation by functions
  • A model for functional programming languages

3
Lambda Calculus
  • The lambda calculus is an abstraction and
    simplification of a functional programming
    language, much as a Turing machine is an
    abstraction and simplification of a computer.
  • Lambda calculus is Turing-complete, so it it can
    be used as a model for computation instead of
    TMs.
  • Issues such as delayed evaluation, recursion, and
    scope can be studied with mathematical precision
    in the lambda calculus.

4
Lambda abstraction and Application
  • Lambda abstraction (like lambda expressions in
    Scheme - anonymous function creation)
  • (?x. 1 x)
  • lambda expression in Scheme
  • (lambda (x) ( 1 x))
  • Applications of expression (like function call)
  • (?x. 1 x) 2
  • Reduction rule
  • (?x. 1 x) 2 gt ( 1 2) gt 3

5
Syntax of lambda calculus
  • lexpr ? constant // e.g., 0, 1, ,
  • variable // e.g., x, y
  • lexpr lexpr // function application
  • (lexpr)
  • (? variable . lexpr // lambda abstraction
  • Variables do not occupy memory
  • The set of constants and the set of variables
    differ among different lambda calculi
  • Lambda calculus without constants is pure lambda
    calculus

6
Notes
  • Parentheses can be left out if no ambiguity
    results
  • (?x.(( 1) x)) ltgt (?x. 1 x)
  • (?x.(( 1) x) 2) ltgt (?x. 1 x) 2
  • Lambda calculus is fully Curried.
  • Application works left to right, abstraction
    right to left.
  • Application has higher precedence than
    abstraction.
  • The set of variables is unspecified, but doesn't
    matter very much, as long as it is (countably)
    infinite.
  • The set of constants isn't specified either, and
    this can make a difference in terms of what you
    want to express. This set may be infinite (all
    integers) or finite or even empty (pure lambda
    calculus).

7
Bound and free variables
  • (?x.E)
  • The scope of the binding of the lambda is
    expression E
  • All occurrences of x in E are bound
  • All occurrences of x outside E are not bound by
    this lambda
  • e.g., in (?x. y x)
  • x is bound, like a local variable
  • y is free, like a nonlocal reference in the
    function

8
Another example
1
2
3
  • (?x. ((?y.((?x. x y) 2)) x) y)

free
Bound by 3
Bound by 2
Bound by 1
9
Application of the above lambda
  • (?x. ((?y.((?x. x y) 2)) x) y) gt
  • (?x. ((?y.( 2 y)) x) y) gt
  • (?x. ( 2 x) y)

10
Currying
  • Lambdas can only describe functions of a single
    variable
  • (?n.(?m. ))
  • A wrong example
  • (?n m. if m 0 then n else ))
  • Currying the process of splitting multiple
    parameters into single parameters to higher-order
    functions
  • The general principle
  • fname (type1, type2) -gt type3 gt
  • fname type1 -gt type2 -gt type3
  • e.g.,
  • ( 2 3) gt (( 2) 3)

11
Beta-abstraction and beta-reduction
  • beta-reduction
  • ((?x. x 1) 2) gt ( 2 1)
  • beta-abstraction
  • ( 2 1) gt ((?x. x 1) 2)
  • beta-conversion (?-conversion) - beta-abstraction
    or beta reduction
  • ((?x. E) F) ltgt EF/x
  • where EF/x is E with all free occurrences of x
    replaced by F

12
Alpha-conversion (?-conversion)
  • The name capture problem if you substitute an
    expression with free variables into a lambda, one
    or more of those free variables may conflict with
    names bound by lambdas (and be captured by them),
    giving incorrect results (since variable
    bound/free status should not change)
  • ((?x.(?y. x y)) y) gt (?y. y y)
  • alpha-conversion
  • General form (?x. E) ltgt (?y. Ey/x) and y does
    not occur in E
  • ((?x.(?y. x y)) y) gt
  • ((?x.(?z. x z)) y) gt
  • (?z. y z)

13
Eta-conversion (?-conversion)
  • The process of eliminating redundant lambda
    abstractions
  • (?x. (E x)) ltgt E if E contains no free
    occurrences of x
  • e.g.,
  • (?x. ( 1 x)) gt ( 1)
  • (?x.(?y.( x y))) gt (?x. ( x)) gt

14
Semantics
  • An expression is a normal form if there are no
    ?-reductions that can be performed on it.
  • The semantics of an expression in lambda calculus
    is expressed by converting it to a normal form,
    if possible. Such a normal form, if it exists, is
    unique, by the famous Church-Rosser Theorem.
  • Not all reduction sequences lead to a normal
    form (? y. 2) ((? x. x x)(? x. x x))

15
Normal vs. Applicative Order
  • A ??-reduction (? x. E) F gt EF/x that
    substitutes F for x before reducing F is called a
    normal-order reduction. It corresponds to the
    lazy evaluation of Haskell (without the
    memoization).
  • If on the other hand, the ?-reduction (? x. E) F
    gt EF/x is performed only after reducing F, it
    is called an applicative order reduction. This
    corresponds to the evaluation rule of Scheme,
    where all arguments are evaluated before a call.

16
Examples
  • Applicative order evaluation (pass by value)
  • ((?x. x x) ( 2 3)) gt
  • ((?x. x x) 5) gt
  • ( 5 5) gt
  • 25
  • Normal order evaluation (pass by name)
  • ((?x. x x) ( 2 3)) gt
  • ( ( 2 3) ( 2 3)) gt
  • ( 5 5) gt
  • 25

17
Strict and nonstrict functions
  • Main result (also a consequence of the
    Church-Rosser Theorem) if a normal form exists,
    a normal-order sequence of conversions will find
    it.
  • If the value of an expression does not depend on
    the value of the parameter, then
  • normal order will compute the correct value
  • Applicative order may give an undefined value
  • An example
  • ((?y. 2) ((?x. x x) (?x. x x)))
  • Normal order 2
  • Applicative order infinite loop

18
The End of Chapter 11 Part 3
Write a Comment
User Comments (0)
About PowerShow.com