Docsity

Lambda Calculus: Understanding Variables, Functions, and Semantics, Papers of Programming Languages

Partial preview of the text

Download Lambda Calculus: Understanding Variables, Functions, and Semantics and more Papers Programming Languages in PDF only on Docsity!

Language Semantics

Lambda Calculus: Variables and

Functions

Describing Semantics

 Informal definitions

 Tutorials: learn by working examples  Reference Manuals  Natural language explanation for each syntax rule

 Formal definitions (skip)

 Attribute grammars  Associate attributes (values) with each grammar symbol  Associate semantics rules with each grammar rule  Operational semantics  Interpret the language on an abstract machine or using another language  Denotational semantics  Define language constructs as mathematical functions  Axiomatic semantics (proof rules)  Define properties (invariance) of language constructs

 Goal: communication, automation and validation

Pure Lambda Calculus

 Abstract syntax : M ::= x | λ x.M | M M

 x represents variable names  Each expression in Lambda Calculus is called a lambda term or a lambda expression

 Concrete syntax: how to resolve ambiguity?

 ( M M ) has higher precedence than λ x.M ; i.e, λ x.M N <=> λ x. (M N)M M is left associative; i.e. x y z <=> (x y) z  Add parentheses to allow alternative grouping of operations

 Compare: concrete syntax in Lisp/Scheme

M ::= x | (lambda (x) M) | (M M)

The Applied Lambda Calculus

 Can pure lambda calculi express all computation?

 Yes, it is Turing complete. Other values/operations can be represented as function abstractions.  For example, booleans can be expressed as True = λ t. (λ f. t) False = λ t. (λ f. f)  But we are not going to be extreme.

 The applied lambda calculus

M ::= e | x | λ x.M | M M  e represents all regular arithmetic expressions

 Examples of applied lambda calculus

 Expressions: x+y, x+2y+z  Function abstraction/definition: λ x.(x+y), λz.(x+2y+z)  Function application (invocation): (λ x.(x+y)) 3

Lambda Calculus In Real Languages

 Lisp

 Many different dialects  Lisp 1.5, Maclisp, …, Scheme, ...CommonLisp,…  This class uses Scheme  Function abstraction (allow multiple parameters)  λ x. M => (lambda (x) M)  λx. λ y. λ z. M => (lambda (x y z) M)  Function application  M1 M2 => (M1 M2)  (M1 M2) M3 => (M1 M2 M3)

 C (each function must have a name)

 λ x. λ y. λ z. M => int f(int x,int y,int z) { return M; }  (M1 M2) M3 => M1(M2, M3)

Exercises: translate Lambda Calculus  From Lambda calculus ==> C language  λ x. x <=> (lambda (x) x) int dummy(int x) { return x; }What if x is a float or string?  λ f. λ g. λ x. f (g x) <=> (lambda (f) (lambda (g) (lambda (x) (f (g x))))) int dummy(int (f)(...), int (g)(...), int x) { return f(g(x)); }**  C: does not allow functions to return functions  (λ f. λ x. (f x)) y z ==>?  From C ==> Lambda calculus int f1(int (g)(...)) { return g(g); } int f2(int x) { return x + 1; }*  int main() { return f2(3); } ==>?int main() { return f1(f2); } ==>?int main() { return f1(f1); } ==>?

Programming With Lambda Calculus  What happens in evaluation (λ y. y + 1) x = x + 1 (λ f. λ x. f (f x)) g = λ x. g (g x) (λ f. λ x. f (f x)) (λ y. y+1) = λ x. (λ y. y+1) ((λ y. y+1) x) = λ x. (λ y. y+1) (x+1) = λ x. (x+1)+

 Lambda term evaluation => substitute variables

(parameters) with values

 Each variable is a name (or memory store) that can be given different values  When variables are used in expressions, need find the binding location/declaration and get the value

Variable Binding  Bound and Free variables

 Each λ x.M declares a new local variable x

 x is bound (local) in λ x.M  The binding scope of x is M => the occurrences of x in M refers to the λ x declaration  Each variable x in a expression M is free (global) if  there is no λx in the expression M, or x appears outside all λx declarations in M  The binding scope of x is somewhere outside of M  Example: λ x. λ y. (z1*x+z2 y)  Bound variables: x, y; free variables: z1, z  Binding scopes λ x => λ y. (z1x+z2 y) λ y => (z1x+z2 *y)

Equality of Lambda Terms  Idempotence axiom  M = M  Commutativity rule  M = N => N = M  Transitivity rule  M = N N = P M = P  Congruence rule  M=M’ => λ x. M = λ x. M’  M = M’ N = N’ M N = M’ N’  What does the above mean?  Make sure you copy the un-modified terms

Equality of Lambda Terms

 α-axiom

 λx. M = λy. [y/x]M  [y/x]M: substitutes y for free occurrences of x in M  y cannot already appear in M  Example  λ x. (x + y) = λ z. (z + y)  But λ x. (x + y) ≠ λ y. (y + y)

 β-axiom

 (λ x. M) N = [N/x] M  [N/x]M is substitutes N for free occurrences of x in M  Free variables in N cannot be bound in M  Example  (λ x. λ y. (x + y)) z1 = λ y. (z1+y)  But (λ x. λ y. (x + y)) y ≠ λ y. (y + y)

Example: Variable Substitution

 (λ f. λ x. f (f x)) (λ y. y+x)

apply twice add x to argument

 Substitute variables “blindly”

λ x. [(λ y. y+x) ((λ y. y+x) x)] => λ x. x+x+x

 Rename bound variables

(λ f. λ z. f (f z)) (λ y. y+x)

=> λ z. [(λ y. y+x) ((λ y. y+x) z))]

=> λ z. z+x+x

Easy rule: always rename variables to be distinct

Additional Exercises

 Reduce lambda terms

 λ x. (λ y. y x) (λ z. x z)

 (λ x. (λ y. y x) (λ z. x z) ) y

 (λ x. (λ y. y x) (λ z. x z) ) (λ y. y z)

Recursion and Fixed Points

 Recursive functions

 The body of a function invokes the function

 Factorial: f(n) = if n=0 then 1 else n*f(n-1)

 Is it possible to write recursive functions

in Lambda Calculus?

 Yes, using fixed-point combinator

 More advanced topics (not required)