




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















Com S 541
Overview:
!^
What is Computability? â Churchâs Thesis !^
Primitive Recursive Functions !^
The Lambda Calculus !^
The Church-Rosser Property !^
Modeling basic programming constructs
References:
!^
H.P. Barendregt, âThe Lambda Calculus â Its Syntax and Semanticsâ, North-Holland, 1984 !^
David A. Schmidt, âThe Structure of Typed Programming Languagesâ, MITPress, 1994 !^
Carl A. Gunter, âSemantics of Programming Languagesâ, MIT Press, 1992
Com S 541
!^
Computation is usually modeled as a mapping from inputs to outputs,carried out by a formal âmachineâ, or program, which processes its input ina sequence of steps. !^
An âeffectively computableâ function is one that can be computed in afinite amount of time using finite resources.
Problem input
yes no output
âeffectivelycomputableâ
function
program/machine
Com S 541
!^
A problem that cannot be solved by any Turing machine in finite time (orany equivalent formalism) is called uncomputable.
Assuming Churchâs thesis is true, an uncomputableproblem cannot be solved by any real computer.
The Halting Problem
Given an arbitrary Turing machine and its input tape, will the machineeventually halt?The Halting Problem is provably uncomputable â which means that itcannot be solved in practice.
Com S 541
!^
Primitive recursive functions are a total recursive functions (i.e. Turing computable)and they form the smallest class of functions that:^!
Includes the base functions
!^
c
is introduced as
function (in most cases
c=
)
!^
!^
represented as a function with arity
n
!^
Is closed under the production rules
!^
Composition: Let
h
be a function of arity
k
and
g
,âŠ, 1
gk
functions with arity
n
, then
the function
denotes a function with arity
n
.
!^
Primitive recursion: Let
h
be a function with arity
n+
and
g
be a function with arity
n-
1 , then the following equation set denotes a function
f
with arity
n
:
n m
xm
xn
xm
x nm P^
†â€
=^
1
,
)
,..
.,
,..
., 1 (
cons with
t
, )
,..
., 1 (
=
=^
c
c
xn
x nc F
with
1
) (^
=^
x x f
)]
,..
., 1 (
),.
..,
,..
., 1 ( 1 [ )
,..
., 1 (^
xn
x gk
xn
x g h
xn
x f^
=
)) , 1
,..
., 1 ( , , 1
,..
., 1 ( ) 1
, 1
,..
., 1 (
) 1
,..
., 1 ( ) 0 , 1
,..
. 1 (
y
xn
x f y
xn
x h
y
xn
x f
xn
x g
xn x f
â
â
=
â
â
=
â
Com S 541
-- base functions: prim_null, prim_succ, and prim_projprim_null :: Int -> Intprim_null x = 0prim_succ :: Int -> Intprim_succ x = x + 1-- The operator !! - list index subscript - is defined in Prelude.hsprim_proj :: ([Int], Int) -> Intprim_proj (tuple, index) = tuple!!(index - 1)-- add( x, y ) = x + yprim_add :: (Int, Int) -> Intprim_add (x, 0) = prim_proj([x], 1)prim_add (x, y+1) = h(x, y, prim_add(x, y))
where h(x, y, z) = (prim_succ. prim_proj)( [x, y, z], 3 )
Com S 541
!^
Recursive equations:exp(x, 0) = 1exp(x, y+1) = x * exp(x, y) !^
Primitive recursive functions:
(x) F
exp(x,
1 1
z)) y, (x, P z), y, (x,
mul(P )z y,
h(x,
with
y)
exp(x, y,
h(x,
y
exp(x,
(^33)
(^31)
Com S 541
!^
Recursive equations:pred(0) = 0pred(x+1) = x !^
Primitive recursive functions:
!
pred(0)
pred(x)) (x, P
pred(x
Com S 541
!^
Lambda calculus is a language with clear operational and denotationalsemantics capable of expressing algorithms. Also it forms a compactlanguage to denote mathematical proofs. !^
Logic provides a formal language in which mathematical statements can beformulated and provides deductive power to derive these. Type theory is aformal system, based on lambda calculus and logic, in which statements,computable functions and proofs all can be naturally represented. !^
The lambda calculus is a good medium to represent mathematics on acomputer with the aim to exchange and store reliable mathematicalknowledge.
Com S 541
!^
Beta reduction is the computational engine of the lambda calculus:Define:
λ
x.x
Now consider:
λ
x.x)(
λ
x.x)
λ
x.x)
/x]
x
ÎČ
-reduction
(λ
x.x)
substitution
!^
We can implement most lambda expressions directly in Haskell:i = \x -> x?^
i 5 5?^
i i 5 5
Com S 541
!^
The variable
x
is bound by the enclosing
λ
in the expression
λ
x.e
. A variable
that is not bound, is free:
fv(x)
= { x}
fv(
λ
x.e)
= f
v(e){ x}
fv(e
1
e^2
= f
v(e
f
v(e
2
!^
An expression with no free variables is closed (otherwise it is open). Forexample,
y
is bound and
x
is free in the (open) expression
λ
y.xy
Syntactic substitution will not always work:
(λ
x.
λ
y.xy)y
[y/
x]
(λ
y.xy)
ÎČ
(λ
y.yy)
incorrect substitution!
Since
y
is already bound in
λ
y .x y)
, we cannot directly substitute
y
for
x
Com S 541
!
!
Com S 541
!
!
Com S 541
!^
Most programming languages are
function call are evaluated before control is passed to the function. !^
Most modern functional languages, on the other hand, use
that is, expressions are only evaluated when they are needed.Consider:
square n = n * n
Applicative-order reduction:
square (2 + 5)
square 7
7 * 7
49
Normal-order reduction:
square (2 + 5)
(2 + 5) * (2 + 5)
7 * (2 + 5)
7 * 7
49
Com S 541
!^
Motivation:^!
Modeling call-by-value in programming languages !^
In function calls, evaluate arguments then invoke function
!^
In the lambda-calculus, this means:^!
In (e
1
e^2
), reduce e
2
to normal form using applicative order reduction
!^
Then reduce e
1
to normal form using applicative order reduction
!^
If e
1
is a lambda abstraction, do beta reduction, and reduce the result to normal
form using applicative order reduction
!^
Syntax makes it easy:^!
Write expression using fully parenthesized notation !^
Always perform rightmost beta reduction by !^
Repeatedly scanning for rightmost (left parenthesis) occurrence of ((
λ x. e
) e 1
) 2
!^
Note, this includes reduction of primitives, e.g. ((add 1) 2)