170 likes | 338 Views
159.331 Programming Languages & Algorithms. Lecture 23 - Functional Programming Languages - Part 5 Lambda Calculus & Some Theory. 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
E N D
159.331 Programming Languages & Algorithms Lecture 23 - Functional Programming Languages - Part 5 Lambda Calculus & Some Theory Prog Lang & Alg
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. Prog Lang & Alg
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 Prog Lang & Alg
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) Prog Lang & Alg
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 Prog Lang & Alg
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 Prog Lang & Alg
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 ) ) Prog Lang & Alg
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 ) Prog Lang & Alg
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 Prog Lang & Alg
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 Prog Lang & Alg
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 Prog Lang & Alg
Lambda Calculus • Alonzo Church’s notation for studying types • Supposedly inspired by Whitehead and Russell notation for the class of all x’s 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.x*x 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.x*x) 5 applies our function to 5 yielding 25 Prog Lang & Alg
We call formulas like (x.x*x) 5 terms • Church’s 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) Prog Lang & Alg
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 - Functions 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 Prog Lang & Alg
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 :-) Prog Lang & Alg
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 Constructs Summary Prog Lang & Alg
Further Reading • See Bal & Grune Chapter 4 • See Sebesta Chapter 15 • See also Hudak’s ACM article on Conception, Evolution and Application of Functional Programming Languages • Next - Logic Programming Languages… Prog Lang & Alg