Programs as Functions - Lecture Slides | CSE 452, Study notes of Programming Languages

Material Type: Notes; Professor: Dillon; Class: Organization Programming Lang; Subject: Computer Science & Engineering; University: Michigan State University; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 07/29/2009

koofers-user-1z5
koofers-user-1z5 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
L. Dillon, CSE 452, Fall 2008 1
Fundamentally, a program computes a function
:PTWSPZ[PJI\[\ZLM\S]PL^!PNUVYLZJH]LH[ZK\L[VÄUP[L<
WYLJPZPVU\UX\HSPÄHISL\UJVU[YVSSHISLPUW\[ZZPKLLMMLJ[Z
concurrency etc.)
Two reasons why a program might not compute an
output for all inputs
An1. operation could produce an error on some inputs
LNHYP[OTL[PJV]LYÅV^KP]PKLI`aLYV06LYYVYL[J½
Computation2. might not terminate on some inputs
e.g., int f (int n) { if n = 0 then return 1 else return (n * f(n-1)); }½
These two cases are “mathematically equivalent” but
“operationally different”
A language implementation can reliably detect when (1) occurs<
But not when (2) occurs<
Programs as functions
L. Dillon, CSE 452, Fall 2008 2
A partial function, f:AAB, is a subset fA=B
such that
6<xDAandy, z DB,(x, y)Df and (x, z)Dfimplies y = z
A total function is a partial function f:AABsuch that
6<xDA7yDBsuch that (x, y)Df
Notation:  (x, y)Dfis also written y = f (x)
Programs KLÄULWHY[PHSM\UJ[PVUZMVY[^VYLHZVUZ
partial operations<
nontermination<
Partial functions
L. Dillon, CSE 452, Fall 2008 3
Computable functions
+LÄUP[PVU!
A partial function f:AAB is computable if there is a program P
that computes f,
i.e., for any xDA, if there exists yDBsuch that y = f(x), then
computation P(x) halts with output y
Church's thesis:
All general computing devices compute the same functions—
ZWLJPÄJHSS`[OLM\UJ[PVUZJVTW\[HISLI`HTuring machine
Terminology :
The set of computable functions on the natural numbers is called
the set of partial recursive functions
L. Dillon, CSE 452, Fall 2008 4
Noncomputable functions
Halting function: 
Given a program P and an input x, decides whether P halts on x
Fact :
There is no program that computes Halt
0TWSPJH[PVUZMVYWYVNYHTHUHS`ZPZ!
There are useful program properties that cannot be decided
automatically
Will a program run forever?½
Will a program eventually cause an error?½
>PSSHWYVNYHTL]LY[V\JOHZWLJPÄJTLTVY`SVJH[PVUHNHPU&½
(ButJVUZLY]H[P]LHUHS`ZPZTH`Z\MÄJLLN[`WLJOLJRPUN
1, if P(x) halts
0, otherwise
Z
[
\
Halt(P, x) =
pf2

Partial preview of the text

Download Programs as Functions - Lecture Slides | CSE 452 and more Study notes Programming Languages in PDF only on Docsity!

L. Dillon, CSE 452, Fall 2008 1

‹Fundamentally, a program computes a function

<:PTWSPZ[PJI[\ZLM\S]PL^!PNUVYLZJH]LH[ZK\L[VÄUP[L

WYLJPZPVU\UX\HSPÄHISL\UJVU[YVSSHISLPUW[ZZPKLLMMLJ[Z

concurrency etc.)

‹Two reasons why a program might not compute an

output for all inputs

  1. Anoperation could produce an error on some inputs ½ LNHYP[OTL[PJV]LYÅV^KP]PKLI`aLYV06LYYVYL[J
  2. Computationmight not terminate on some inputs ½e.g., int f (int n) { if n = 0 then return 1 else return (n * f(n-1)); }

‹ These two cases are “mathematically equivalent” but

“operationally different”

<A language implementation can reliably detect when (1) occurs < But not when (2) occurs

Programs as functions

L. Dillon, CSE 452, Fall 2008 2

‹A partial function, f : A A B, is a subset f

 A = B

such that

< 6 x D A and y, z D B , ( x, y ) D f and ( x, z ) D f implies y = z

‹ A total function is a partial function f : A A B such that

< 6 x D A  7 y D B such that ( x, y ) D f

‹ Notation: ( x, y ) D f is also written y = f ( x )

‹ Programs KLÄULWHY[PHSM\UJ[PVUZMVY[^VYLHZVUZ

< partial operations < nontermination

Partial functions

L. Dillon, CSE 452, Fall 2008 3

Computable functions

‹ +LÄUP[PVU!

A partial function f : A A B is computable if there is a program P that computes f , i.e., for any x D A , if there exists y D B such that y = f ( x ), then computation P ( x ) halts with output y

‹ Church's thesis:

All general computing devices compute the same functions— ZWLJPÄJHSS[OLM\UJ[PVUZJVTW\[HISLIH Turing machine

‹ Terminology :

The set of computable functions on the natural numbers is called the set of partial recursive functions

L. Dillon, CSE 452, Fall 2008 4

Noncomputable functions

‹ Halting function:

Given a program P and an input x , decides whether P halts on x

‹ Fact :

There is no program that computes Halt

‹ 0TWSPJH[PVUZMVYWYVNYHTHUHS`ZPZ!

There are useful program properties that cannot be decided automatically ½Will a program run forever? ½ Will a program eventually cause an error? ½>PSSHWYVNYHTL]LY[V\JOHZWLJPÄJTLTVYSVJH[PVUHNHPU& (ButJVUZLY]H[P]LHUHSZPZTHZ\MÄJLLN[WLJOLJRPUN

1, if P(x) halts 0, otherwise

Z

[

\

Halt ( P, x ) =

L. Dillon, CSE 452, Fall 2008 5

Complexity

‹ *VTW[HIPSP[ZHZUV[OPUNHIV[WYHJ[PJHSP[LMÄJPLUJ

‹ Computational complexity theory:

< Provides estimates of the resources (e.g., time, space) needed by a program that computes a given function <Different complexity classes: linear, polynomial, exponential, ...

Time complexity function

n = 10 n = 20 n = 30 n = 40 n = 50 n = 60

n .00001 s .00002 s .00003 s .00004 s .00005 s .00006 s n (^2) .0001 s .0004 s .0009 s .0016 s .0025 s .0036 s n^5 .1 s 3.2 s 24.3 s 1.7 m 5.2 m 13.0 m 2 n^ .001 s^ 1.0 s^ 17.9 m^ 12.7 d^ 35.7 y^ 366 c 3 n^ .059 s^ 58 m^ 6.5 y^ 3855 c^2 =^10 8 c^ 1.3=^ c s = sec., m = min., d = days, y = years, c = centuries; n PZZPaLVMWYVISLTPUZ[HUJL L. Dillon, CSE 452, Fall 2008 6

Complexity

‹ Effect of improved technology

:PaLVM3HYNLZ[7YVISLT0UZ[HUJL Solvable in 1 Hour Time complexity function

With present computer

With computer 100 times faster

With computer 1000 times faster n N 1 100 N 1 1000 N 1 n^5 N 2 2.5 N 2 3.98 N 2 3 n^ N 3 N 3 + 4.19 N 3 + 6.

Source: Computers and Intractability: A Guide to the Theory of NP-Completeness , by M. Garey and D. Johnson, W. H. Freeman & Co., 1979.

L. Dillon, CSE 452, Fall 2008 7

Complexity

‹ Some decidable problems are provably intractable

< i.e., no polynomial time program can possibly solve them

‹ Many others are NP-Complete

< e.g., SAT: "solving" a Boolean logic formula < widely believed to be intractable

‹ Some implications:

< Thereare practical limits to what programs can do regardless VMLMÄJPLUJVM[OLSHUN\HNLZ[OLHYL^YP[[LUPU < Thereare program properties for which automated analysis, al- though possible in theory, may be impractical < Complexityof a problem to be solved may affect choice of programming language