159'331 Programming Languages - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

159'331 Programming Languages

Description:

What was our theory for Imperative and OO programs? ... Humpty Dumpty, in Through the Looking Glass by Lewis Carroll. Do not be afraid of all this... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 18
Provided by: KenHa98
Category:

less

Transcript and Presenter's Notes

Title: 159'331 Programming Languages


1
159.331 Programming Languages Algorithms
  • Lecture 21 - Functional Programming Languages -
    Part 5
  • Lambda Calculus Some Theory

2
Some Theory
  • What was our theory for Imperative and OO
    programs?
  • Not much but is in fact grounded out in models
    of machines - memory and automata
  • For Functional languages, there are some very
    useful theoretical ideas and underpinning
    concepts
  • Look at semantics and Lambda calculus - albeit
    somewhat superficially in this paper.

3
What Are Semantics again?
  • Any property of a construct can be defined as its
    semantics or meaning
  • In an expression checker the semantics of an
    expression like 2 3 can be its value
  • Ina type checker the semantics of 2 3 can
    be the type int for integer
  • In an infix to postfix translator the semantics
    of 2 3 can be the string 2 3
  • There are several methods of defining semantics

4
An Example - A Let Expression
  • E let x E1 in E2
  • We can use this in various context with
    different semantics
  • First consideration is for its syntax as a
    production from a grammar for expressions
    (remember EBNF)

5
Informal Semantics
  • E let x E1 in E2
  • Occurrences of x in E2 denote the value E1
  • The value of E2 is the value of the whole
    expression E
  • Example E2 is 1 x
  • E1 might be 2, hence E2 is 1 2 and E is 3
    under some assumptions about arithmetic of
    integers

6
Attribute Grammar
  • Attribute val of E denotes a value. Attribute
    env for environment binds variables to values.
    The operation bind(x,v,env) creates a new
    environment with x bound to v
  • bindings for all other variables are as in env
  • E.val E2.val value of E is the value of E2
  • E1.env E.env variable bindings in E1 are the
    same as in E
  • E2.env bind( x, E1.val, env ) In E2, x is
    bound to the value of E1

7
Operational Semantics
  • E let x E1 in E2
  • The interpreter eval takes two parameters
  • An expression to be evaluates
  • And an expression with variable bindings
  • eval( E, env ) eval( E2, bind( x, eval(E1,env),
    env ) )

8
Denotational Semantics
  • The meaning of the expression E, written as E
    is a function from environments to values. Thus
    E env, the application of E to
    environment env is a value
  • let x E1 in E2 env
  • E2 bind(x, E1 env, env )

9
Natural Semantics
  • Read the logical formula env - E v as
  • In environment env, expression E has value v
  • The rule for let-expressions is
  • env - E1 v1 bind( x, v1, env ) - E2 v2
  • env - let x E1 in E2 v2

10
An Aside on Logic Formulae
  • Suppose we want formal way of defining the
    value of plus E1 E2 as the sum of the values
    E1 and E2
  • We can write this as a logic formula
  • E1 v1 E2 v2
  • plus E1 E2 v1 v2
  • In other words if E1 has the value v1 and
    E2 has the value v2 then plus E1 E2 has the
    value v1 v2

11
The moral of this story
  • There are many ways of saying the same thing
  • Different communities have different syntax
    which help in different ways
  • When I use a word, it means just what I choose
    it to mean--neither more nor less. - Humpty
    Dumpty, in Through the Looking Glass by Lewis
    Carroll
  • Do not be afraid of all this
  • We will return to denotational semantics when we
    discuss Logic later

12
Lambda Calculus
  • Alonzo Churchs notation for studying types
  • Supposedly inspired by Whitehead and Russell
    notation for the class of all xs such that f(x)
    to wit xf(x) - moved the carat down
    and it became a lambda ?
  • ?x.M is used for a function with parameter x and
    body M
  • So ?x.xx is a function that maps 5 to 5 5
  • Functions are written next to their arguments so
    f a is the application of function f to argument
    a
  • (?x.xx) 5 applies our function to 5 yielding
    25

13
  • We call formulas like (?x.xx) 5 terms
  • Chirchs original formulation of ?-calculus was
    for general properties of functions not tied to
    any particular problem area. The integer 5 and
    the multiplication operator below to the problem
    area arithmetic and are not part of the pure
    calculus
  • A grammar for terms in the pure ?-calculus is
  • M x (M1 M2 ) (?x.M)
  • We use letters f, x, y, z for variables and M,
    N,P, Q for terms
  • A term is a variable x, or an application (M N)
    of function M to N or an abstraction (?x.M)

14
  • We are allowed constants such as c which can
    represent values like integers and operations on
    data structures like lists
  • c can stand for basic constants like true and
    nil or constant functions like head and
  • Pure ?-calculus is untyped - Functrions can be
    applied freely and it even makes sense to write
    (x,x) where x is applied to itself
  • A functional programming language is essentially
    a ?-calculus with appropriate constants

15
Going Further
  • ?-calculus is a fascinating topic in its own
    right - we could base a whole course around it.
  • Generally if you learn how it works and the
    various results that arise from setting up
    certain constants and semantics for them we
    can see how to generate a functional language
  • We ascribe meaning to meaning -)

16
Summary
  • Various Semantics - various ways to set up a
    semantics
  • Introduction to ?-Calculus - terms, grammar -
    variables applications and abstractions
  • Some of the older books on programming language
    s (eg in Massey Library) give a treatment
    based on ?-calculus up front. - eg Ravi Sethi
    - Programming Languages- Concepts and Cosntructs

17
Further Reading
  • See Bal Grune Chapter 4
  • See Sebesta Chapter 15
  • See also Hudaks ACM article on Conception,
    Evolution and Application of Functional
    Programming Languages
  • Next - Logic Programming Languages
Write a Comment
User Comments (0)
About PowerShow.com