Download Lambda Calculus Background, λ calculus Models of computation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C Sharp (programming language) wikipedia , lookup

Currying wikipedia , lookup

Anonymous function wikipedia , lookup

Falcon (programming language) wikipedia , lookup

Curry–Howard correspondence wikipedia , lookup

Closure (computer programming) wikipedia , lookup

Standard ML wikipedia , lookup

Lambda lifting wikipedia , lookup

Lambda calculus wikipedia , lookup

Combinatory logic wikipedia , lookup

Lambda calculus definition wikipedia , lookup

Transcript
Lecture 7: Lambda Calculus
• Introduction to λ calculus
•
•
•
•
•
Syntax
Semantics, computations
Programming in λ calculus
The SECD machine
Operational semantics for the λ calculus
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
1
Background, λ calculus
During the 1930:ies some fundamental questions were answered:
• What are the limitations of formal proof systems and possible
deductions / proofs (Gödel 1936)?
• How can we formalize the concept of algorithms (Turing 1939)?
• Where is the limit between algorithmic computability and
uncomputability (Turing 1939)?
Not only Turing suggested a model for computability.
The same year (1939) Alonzo Church defined the λcalculus – a simple system in which computable
functions can be defined and evaluated.
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
2
Models of computation
•
The abstract machine of Turing reminds of imperative languages.
The λ-calculus is an abstract way of defining and combining
functions, and is similar to functional languages.
• Surprisingly enough both models are exactly as powerful
→ Church-Turing-thesis
λ-calculus
↔
↔
Turing machine ↔
imperative
2 0 0 6 -0 9 -1 9
↔
functional
Lennart Ed b lo m , Inst . f.
d at av et enskap
3
1
Syntax of λ calculus
Let 〈var〉 stand for an infinite set of variables (usually
denoted by small letters).
The set of all λ-expressions is defined by the following
context free grammar rules:
〈expr〉 → 〈var〉
| (〈expr〉 〈expr〉)
| λ〈var〉.〈expr〉
(λx.(y x) y)
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
4
Syntax of λ calculus (2)
Often a revised, simplified(?) notation is used.
- omit parentheses around applications
- put parentheses around abstractions if they are part of an
application
- an abstraction body extends as far as possible to the right
- function application is left associative
x
λx.y x
(λx.y x) y
(λx.λy.((λx.x) x)) (λx.x y)
2 0 0 6 -0 9 -1 9
λy.(λx.y x) y
Lennart Ed b lo m , Inst . f.
d at av et enskap
5
Function application and
function definition
The informal semantics of the syntax is…
• E1 E2 denotes function application: E 1 is applied on E 2
• λx.E
describes a function: x is the (only) formal
parameter and E is the body.
The expression λx.E is called an λ-abstraction
Normal programmming language syntax
for λx.E would be e.g fun f(x) = E. The
most important difference is that the
functions of the λ-calculus are
anonymous – they have no names!
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
6
2
Bound and free variables
• In λx.E the λx. is the ”header” that ”declares” a formal
parameter x. Every occurrence of x in E denotes this formal
parameter – x is called a bound variable.
a) v is bound in λx.E ⇔ v=x and v is free in E or v is bound in E
b) v is bound in (E1 E2) ⇔ v is bound in E1 or v is bound in E2.
• Variables that occurs in expression and are not bound are
free variables.
E= ((λx.λy.(λx.x) (x x)) (λx.x y))
⇒ bound(E) = {x,y} and free(E) = {y} in this case
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
7
α-conversion
Evaluation of lambda expressions
• α-conversion
Formal parameters may be renamed, i.e every
occurence of x in λx.E may be changed to y.
Notation E1 →α E2.
• Problem: free occurences of identifiers in E1 must not be
bound.
Ex: (λy.+ x y)), change y to x
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
8
Substitution
To replace all free occurences of x in E1 with E2 is written E1 [ E 2/x].
• If E1 is a variable
 x[E’/x] = E’
 y[E’/x] = y
,y≠x
• If E1 is an application
 (E1 E2)[E’/x]
= (E1 [E’/x])(E2 [E’/x])
•
If E1 is an abstraction
 (λx.E)[E’/x]
= λx.E
 (λy.E)[E’/x]
= λy.E[E’/x]
if y not free in E' (no possible name clashes)
or x not free in E (no substitution)
 (λy.E)[E’/x]= λz.E[z/y] [E’/x]
otherwise, where z (new name) is not free in E or E'
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
9
3
β-reduction
•
•
•
•
•
β-reduction
λ-expressions are evaluated using β-reduction. An
expression (λx.E) E' may be replaced by E[E’/x].
Notation E1 →β E2.
The definition of substitution assures that if bound(E)∩
free(E') ≠ ∅ no name captures will occur.
A subexpression which may be reduced is called a
redex. M may be reduced to N if we can go from M to N
doing zero or more reductions.
This is function application.
Example
1) (λx.(λy.y x)) a
2) (λx.(λy.y x)) y
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
10
The Church-Rosser theorem
∗
• A computation in the λ-calculus is a sequence E1 →β E 2
of β reductions (and possibly α conversions).
E2 is in normal form if no β reductions are possible (E 2 is
then the result of the computation, i.e the value of E 1)
• Can an expression have several values???
Church-Rosser-theorem If E →β∗ E1 and E →β∗ E2
there is an expression E' such that both E 1 ∗→β E'
and E∗ 2 →β E'.
E ∗
∗
E1
E2
∗
2 0 0 6 -0 9 -1 9
E'
∗
Lennart Ed b lo m , Inst . f.
d at av et enskap
11
Order of evaluation
• Sometimes there are several redexes in a λ-expression
=> possible to choose different reduction orders
• Normal order = reduce leftmost-outermost redex first =
substitute the argumentet literally into the body of the
function = call-by-name ≈ lazy evaluation
• Applicative order = Leftmost-innermost = evaluate the
argument first = call-by-value = eager evaluation
• There are expressions with no normal form
• If there is a normal form then normal order evaluation will
lead to it.
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
12
4
Programming in λ calculus
We can define common data types in λ calculus!
• operations are λ expressions
• data elements are also λ expressions
Data type boolean
TRUE = λx.λy.x
FALSE = λx.λy.y
IF B THEN E ELSE E' = (B E E')
B AND B' = (B B' FALSE)
..
.
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
13
Programming in λ calculus (2)
Data type pair
<E ,E'> = λx.IF x THEN E ELSE E'
FIRST P = (P TRUE)
SECOND P = (P FALSE)
Data type integer
0 = <TRUE, TRUE>
SUCC N = <FALSE, N>
IS_ZERO N = FIRST N
PRED N = IF IS_ZERO N THEN 0 ELSE SECOND N
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
14
Recursion
•
•
•
•
•
How can we define recursion without naming functions? How can
they call themselves?
Suppose that we have defined λ expressions PLUS and MULT.
Let us define our beloved faculty function!
fac N = if iszero N then (succ 0) else MULT (N (fac(pred N)))
We want to find a λ expression that satisfies this ”equation”, that
defines fac
Make fac a parameter
H=λf.λN. if iszero N then (succ 0) else MULT (N (f (pred N)))
We want a λ expression that defines fac such that
H fac = fac
fac is a fix point of H
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
15
5
Recursion, cont.
• Define the Y combinator, Y = λf.(λx.f (x x)) (λx.(f x x))
(sometimes called fix )
• Y generates a fixpoint for any function F, i.e
(Y F) = (F (Y F)) !!
• Define
fac = Y H
• It follows that
fac = Y H = H (Y H) = H fac o.s.v
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
16
The SECD-machine
• Lambda expressions can be compiled to machine code
for an abstract machine, the SECD-machine
• The instructions of the SECD machine are simple and
easy to understand(?), and can thus be used to give an
operational semantics for the lambda calculus
• Since functional languages basically are ”sugared”
lambda calculus we have also got a semantics for
functional languages (which also indicates a cerain
implementation)
• It is also easy to give a denotational semantics for
functional languages
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
17
Definitions
• type Variable = string
type FuncSymbol =string
• datatype LambdaExp =
Var of Variable
|Fun of FuncSymbol
|Abs of (Variable * LambdaExp)
|App of (LambdaExp * LambdaExp);
• Var denotes (only) bound variables.
Fun denotes (built-in) functions and constants,
remember that all constant in principle are lambda
abstractions
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
18
6
Definitions (2)
•
A ”closure” is a pair <fn, environment >. fn is a function (abstraction),
environment the bindings in effect at its definition.
• type Location = int;
• datatype Command =
ldv of Location
(* the value of a bound var *)
|ldc of FuncSymbol
(* functions incl constants *)
|ldf of Command list
(* SECD-code for a function *)
|app
(* application *)
|rtn
(* return)
• datatype Result =
(* values that can be the result of *)
(* evaluation of a λ-expression *)
Closure of (Command list * Result list)
|Result of LambdaExp
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
19
Definitions (3)
•
•
•
•
S(tack), temporary results, Result list
E(nvironment), values of bound var, Result list
C(ontrol), list containing SECD code, Command list
D(ump), saved machine states,
(Result list*Result list*Command list)list
• Main loop of the machine
fun machine (s,e,c,d) =
let val (s’,e’,c’,d’) = exec (s,e,c,d)
in
if null c’ then hd s’
else machine (s’,e’,c’,d’)
end
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
20
Translation of λ-expressions to SECD-code
• fun position (v,env)= if v=hd env then 0
else 1+position (v,tl env);
• fun
Compile (Var v,env) = [ldv (position(v,env))]
Compile (Fun f,env) = [ldc f]
Compile (Abs(v,b),env) =
let val bdy = Compile (b,v::env)
in [ldf (bdy@[rtn])] end
Compile (App(f,a),env) =
Compile(a,env)@ Compile(f,env)@ [app];
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
21
7
Translation of λ expressions, example
• Compile ((λx.λy.y) a b,[]) =
Compile (App (App (Abs…. ), Fun ”a”), Fun ”b”),[]) =
[LDC b]@Compile (App (Abs… ),Fun ”a”),[])@[APP] =
[LDC b, LDC a]@Compile (Abs (”x”, Abs (”y”, Var ”y”)),[])@[APP,APP]=
[LDC b, LDC a]@[LDF(Compile(Abs
(”y”,Var ”y”)),[x])@[RTN])]@ [APP,APP]=
[LDC b, LDC a, LDF(LDF(Compile(Var
”y”),[y,x])@[RTN])@RTN), APP,APP]=
[LDC b, LDC a, LDF (LDF [LDV 0] @[RTN]),RTN), APP,APP]=
[LDC b, LDC a, LDF (LDF (LDV 0, RTN),RTN), APP,APP]
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
22
The SECD-machine
fun
exec(s,e,ldv(l)::c,d) =
(nth(e,l)::s,e,c,d)
|exec(s,e,ldc(f)::c,d) =
(Result(Fun f)::s,e,c,d)
|exec(s,e,ldf(c’)::c,d) =
(Closure(c’,e)::s,e,c,d)
|exec(Closure(c’,e’)::a::s,e,app::c,d)=
([],a::e’,c’,(s,e,c)::d
|exec([a],e’,[rtn],(s,e,c)::d) =
(a::s,e,c,d)
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
23
SECD, exemple of execution
S
E
C
D
LDC b, LDC a, LDF(..),..
b
LDC a, LDF(..), APP,APP
a, b
LDF1 (LDF2(..)RTN),APP,APP
{LDF2(..); []}, a, b
{LDV 0,RTN;[a]}
APP1 ,APP2
a
LDF2 (LDV 0, RTN),RTN
(b,[ ],APP2)
a
RTN
(b,[ ],APP2)
{LDV 0,RTN;[a]},b
bvalue
APP2
b, a
LDV 0,RTN
([ ], [ ], [ ])
a
RTN
([ ], [ ], [ ])
bvalue
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
24
8
SECD, final remarks
• To make the machine more realistic commands like
arithmetic operations, choice e.g. are added. Rekursion
kan hanteras med speciella instruktioner
• The machine implements ”call-by-value semantics”
Could be modified to implement lazy evaluation
2 0 0 6 -0 9 -1 9
Lennart Ed b lo m , Inst . f.
d at av et enskap
25
9