CA215 Languages and Computability - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CA215 Languages and Computability

Description:

Alan Turing: (1936/7) invented a class of machines (later to be called Turing ... If t is an abstraction ?x.s, then by the inductive hypothesis FV (s) is finite. ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 31
Provided by: saramor
Category:

less

Transcript and Presenter's Notes

Title: CA215 Languages and Computability


1
CA215 Languages and Computability
  • Lambda Calculus

lt Lecture 10 gt
2
Reminder Lab
  • Week 10
  • Mock exam
  • Week 11
  • Lab exam 1
  • Week 12
  • Lab exam 2

3
Instructions for Lab Exam
  • You must not open email or internet or any other
    windows not relating to your exam. No books, or
    slides are permitted. Anyone found with the above
    will be asked to leave.
  • You may have your cheat sheet on your desk for
    personal reference
  • Students are not permitted to communicate during
    the exam or to use each others cheat sheets.
  • Reboot your computer into Linux and log in as
    usual.
  • Open a Hugs session.
  • Take your exam paper and wait to be told to turn
    it over.
  • Once you are told to commence you will have 1
    hour 45 mins to complete 3 questions.
  • You can ask for clarification from any of the
    tutors if you have problems understanding the
    question.
  • If you wish to have your mock exam marked, email
    your solutions in three separate files marked
    q1.hs, q2.hs and q3.hs to smorri_at_computing.dcu.ie
    by 11am. Any solutions received after 11am will
    not be marked.

4
Suggestions on Lab Exam
  • Remember your lectures on program design break
    a problem down into smaller component parts
  • Remember abstraction each function does not
    have to be concerned with another function it is
    used in, so long as it does what it is meant to
    do. Take each function individually.
  • Be careful with time management. Divide your time
    equally into the three questions, the 40 mark
    question should require some more time than the
    30 mark questions.

5
About the Cheat Sheet
  • Anything (including code snippets and syntax
    rules) that you may find useful for the exam
  • It can be hand-written or typed
  • 1 page  (A4 size) (both sides)

6
-Calculus
  • Introduction
  • Syntax of ?-Terms
  • Substitution
  • Conversion
  • Evaluation
  • Church Rosser Theorem

7
-Calculus Some History
  • Leibniz had as ideal the following
  • Create a universal language in which all
    possible problems can be stated.
  • Find a decision method to solve all the problems
    stated in the universal language.

Gottfried Wilhelm Leibniz (1646 -1716)
8
-Calculus Some History
  • In 1936 the Entscheidungsproblem was solved in
    the negative independently by Alonzo Church and
    Alan Turing.
  • Alonzo Church (1936) invented a formal system
    called the lambda calculus and defined the notion
    of computable function via this system.
  • Alan Turing (1936/7) invented a class of
    machines (later to be called Turing machines) and
    defined the notion of computable function via
    these machines.

9
-Calculus Introduction
  • ?-calculus has subsequently been studied by many
    people (and still is) since it was invented.
  • It is considered to be a foundational basis for
    functional and sequential programming languages.
  • It can be used within Haskell, where a
    ?-abstraction ?x.t is represented by \x -gt t.
  • It is simple, powerful and easy to extend with
    features of interest.
  • Landin pointed out how ?-calculus could be used
    to explain features of some existing programming
    languages
  • Whatever the next 700 languages turn out to
    be, they will
  • surely be variants of ?-calculus.
  • -- Landin 1966

10
Syntax of ?-Terms
  • The language of lambda calculus is simply defined
    as (BNF Form)
  • ltfunctiongt (?ltvariablesgt.
    ltexpressiongt)
  • ltapplicationgt (ltexpressiongt
    ltexpressiongt)
  • ltexpressiongt ltvariablesgt
    ltapplicationgt ltfunctiongt
  • ltvariablesgt x y ...

11
Syntax of ?-Terms
  • Variables e.g. x, u and v.
  • Applications
  • of the form F A where F and A are ?-terms, e.g. f
    x.
  • the data F considered as algorithm applied to the
    data A considered as input
  • This can be viewed in two ways either as the
    process of computation F A or as the output of
    this process.

12
Syntax of ?-Terms
  • Abstractions (functions)
  • of the form ?x.s, e.g. ?x.x, ?x.?y.x, ?x.?y.y.
  • If M Mx is an expression containing x, then
    ?x.Mx denotes the function
  • For example
  • denotes the function
    applied to the argument 3 giving
    231 which is 7

13
Notation
  • To keep the notation of lambda expressions
    uncluttered, the following conventions are
    usually applied
  • Outermost parentheses are dropped
  • M N instead of (M N).
  • Applications are assumed to be left associative
  • M N P means (M N) P.
  • The body of an abstraction extends as far right
    as possible
  • ?x.M N means ?x.(M N) and not (?x.M) N.
  • A sequence of abstractions are contracted
  • ?x.?y.?z.N is abbreviated as ?xyz.N

14
Free and Bound Variables
  • A free variable in a ?-term is one which is not
    bound to any corresponding formal parameter.
  • Variables that fall within the scope of a lambda
    are said to be bound
  • For example in ?y.x y, x is free while y is
    bound
  • The set of free variables of a ?-term t, notation
    FV(t), can be defined inductively over the syntax
    of t as follows
  • A ?-term is said to be closed if it does not
    contain any free variables, notation FV(M) Ø.

15
Structural InductionFV is Finite
  • As an illustration of structural induction, we
    will prove that for any ?-term t, the set FV (t)
    is always finite
  • If t is a variable v, then FV (t) v by
    definition, and this is certainly finite.
  • If t is an application s1 s2, then by the
    inductive hypothesis FV (s1) and FV (s2) are both
    finite. But FV (t) FV (s1) ? FV (s2), and the
    union of two finite sets is finite.
  • If t is an abstraction ?x.s, then by the
    inductive hypothesis FV (s) is finite. But FV (t)
    FV (s) \ x, which must also be finite as it
    is no larger.
  • Q.E.D.

16
Substitution
  • In order to express certain rules, we need to
    formalize the notion of substituting a term for a
    variable in another term
  • We write ts/x for the result of substituting a
    term s for a variable x in another term t.
  • This can be remembered by thinking of
    multiplication of fractions xt/x t
  • For example (?z.x z x)y/x ?z.y z y
  • Of course we only substitute for free variables,
    so (?x.x)y/x ?x.x

17
Naive Substitution
  • We could naively define substitution by recursion
    as follows
  • xt/x t
  • yt/x y, if x ? y
  • (s1 s2)t/x s1t/x s2t/x
  • (?x.s)t/x ?x.s
  • (?y.s)t/x ?y.(st/x), if x ? y
  • However this isnt right. We get (?y.x y)y/x
    ?y.y y - variable capture (name conflict).
  • We should rename first to ?w.x w, and only then
    perform the naive substitution
  • (?w.x w)y/x ?w.y w

18
Renaming Substitution
  • Consider two cases when defining (?y.s)t/x for
    x ? y.
  • If either x FV (s) or y FV (t), then we can
    proceed as before variable capture will not
    occur.
  • Otherwise, we pick a new variable z (FV (s) ?
    FV (t)) and form ?z.(sz/yt/x). That is, first
    we rename the bound variable y to z, then proceed
    with the substitution as before.
  • For definiteness, we can define z to be the
    lexicographically earliest variable not occurring
    free in s or t.
  • The above definition ensures that substitution
    always respects the intuitive interpretation. Now
    we can use this operation freely.

19
Conversion Rules (Reductions)
  • ?-calculus is based on three fundamental
    conversions which transform one term into
    another one, intuitively equivalent to it.
  • These are traditionally denoted by the Greek
    letters a (alpha), ß (beta) and ? (eta).
  • a-conversion changing bound variables
  • ß-conversion applying functions to their
    arguments
  • ?-conversion which captures a notion of
    extensionality
  • The most important to us is ß.

20
a-conversion
  • An alpha reduction is hardly a reduction at all
  • It could be otherwise called, "renaming," but
    must be done with some care to make sure that one
    does not alter the scope of a variable when
    renaming it
  • Definition ?x.s ? ?y.sy/x provided y FV (s).
  • Example
  • ?u.u v ? ?w.w v, but ?u.u v ? ?v.v v. This
    restriction avoids another instance of name
    capture (name conflict).
  • ? read as a-reduces to

21
ß-conversion
  • beta conversion is perhaps the most intuitive
    reduction
  • also the most important in analyzing lambda
    expressions
  • It is, in essence, a direct substitution
  • Definition (?x.s) t ? st/x
  • Examples
  • (?z. f z) b ? (f z)b/z (f b)
  • (?z.y) c ? y c/z y

22
?-conversion
  • It is used to eliminate useless variables in
    abstractions
  • Definition ?x.t x ? t, provided x FV(t).
  • For example
  • ?u.v u ? v
  • but ?u.u u ? u.

23
Normal Form
  • A lambda expression that cannot be reduced
    further (by beta-reduction rule) is called a
    normal form
  • If a lambda expression E can be reduced to a
    normal form, we then say that E has a normal
    form.
  • Such lambda expressions are useful (actually
    indespensible) in encoding recursive functions.

24
?-Equality
  • Using these conversion rules we can define
    formally when two lambda terms are to be
    considered equal
  • We say that two ?-terms s and t are equal, and
    write s t, if there is a finite sequence of a,
    ß or ? conversions, forwards or backwards, at any
    depth, which connects them.
  • Note that this equality is a defined notion and
    is not the same as equality at the syntactic
    level.
  • We call syntactic level equality identity, and
    use the symbol .
  • For example
  • ?x.x ?y.y, but it is not the case that ?x.x
    ?y.y
  • The equality relation is extensional, i.e. if two
    functions f and g give equal results for all
    arguments, then they are themselves equal.

25
Evaluation
  • The basic evaluation strategy in the ?-calculus
    is reduction.
  • Any sub-expression of the form (?x.e1) e2 is a
    suitable candidate for reduction (by
    ß-conversion).
  • Such an expression is called a reducible
    expression or redex for short.
  • At any point in a reduction, there might be a
    choice of which expression to reduce next.

26
Evaluation
  • One evaluation strategy we could use is to always
    select the leftmost innermost redex. This is
    called applicative-order reduction and
    corresponds to call-by-value evaluation. This is
    used in most programming languages.
  • Another strategy is to always select the leftmost
    outermost redex. This is called normal-order
    reduction and corresponds to call-by-name
    evaluation. This is used by many functional
    programming languages such as Haskell.
  • An evaluation is said to have completed when the
    expression contains no more redexes. The
    expression is then said to be in normal form.

27
Applicative Order Reduction VS. Normal Order
Reduction
  • suppose we have two functions which are defined
    as
  • f(x) x x
  • g(x) x 1
  • Now consider the function application f(g(2))
  • Normal Order Reduction
  • f(g(2)) --gt g(2) g(2) --gt 3 g(2) --gt 3 3
    --gt 6
  • we apply the "outer most" function.
  • Applicative Order Reduction
  • f(g(2)) --gt f(3) --gt 3 3 --gt 6
  • we apply the "inner most" function.

28
Example 1
  • Consider the following ?-term
  • ?f.f(f(f(a))) (?x.g x)
  • The normal-order reduction of this is as follows
  • The applicative-order reduction of this is as
    follows
  • The applicative-order reduction is therefore more
    efficient in this instance.

29
Example 2
  • Consider the following ?-term
  • (?x.y) ((?x.xx)(?x.xx))
  • The applicative-order reduction of this is as
    follows
  • The normal-order reduction of this is as follows
  • The normal-order reduction therefore terminates
    while the applicative-order reduction does not.

30
The Church Rosser Theorem
  • If A -gt B and A -gt C then there exists an
    expression D such that B -gt D and C -gt D
  • If A has a normal form E, then there is a normal
    order reduction A -gt E.
  • That is
  • no matter what reduction strategy is used
    initially, there is always a way to converge into
    the same expression.
  • In addition, normal order reduction guarantees
    termination if the given expression has a normal
    form.
Write a Comment
User Comments (0)
About PowerShow.com