Fixed Points in Functional Programming: Recursion and Fixed-point Combinators, Papers of Programming Languages

The concept of fixed points in functional programming, focusing on recursion and the use of fixed-point combinators. It discusses the fixed-point semantics of objects and the typed and polymorphic lambda calculus. References are provided for further study.

Typology: Papers

Pre 2010

Uploaded on 09/02/2009

koofers-user-ibc
koofers-user-ibc 🇺🇸

10 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Com S 541
Fixed Points
Overview:
!Recursion and the fixed-point combinator
!Fixed-point semantics of objects
!The typed lambda-calculus
!The polymorpic lambda calculus
References:
!H.P. Barendregt, “The Lambda Calculus – Its Syntax and Semantics”, North-
Holland, 1984
!David A. Schmidt, “Denotational Semantics – A Methodology for Language
Development”, Allyn and Bacon Inc., 1986
!Joseph E. Stoy, “Denotational Semantics – The Scott-Strachey Approach to
Programming Language Theory”, MIT Press, 1992
!William R. Cook, “A Denotational Semantics of Inheritance”, Ph.D., Brown
University, 1989
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Fixed Points in Functional Programming: Recursion and Fixed-point Combinators and more Papers Programming Languages in PDF only on Docsity!

Com S 541

Fixed Points

Overview:^!^

Recursion and the fixed-point combinator! Fixed-point semantics of objects! The typed lambda-calculus! The polymorpic lambda calculus References:^!^

H.P. Barendregt, “The Lambda Calculus – Its Syntax and Semantics”, North-Holland, 1984! David A. Schmidt, “Denotational Semantics – A Methodology for LanguageDevelopment”, Allyn and Bacon Inc., 1986! Joseph E. Stoy, “Denotational Semantics – The Scott-Strachey Approach toProgramming Language Theory”, MIT Press, 1992! William R. Cook, “A Denotational Semantics of Inheritance”, Ph.D., BrownUniversity, 1989

Com S 541

Recursion

!^ Suppose we want to define arithmetic operations on our lambda-encodednumbers (see “Representing Numbers”).In Haskell we can program:

plus n m

| n == 0 = m| otherwise = plus (n-1) (m+1) so we might try to define:plus

≡ λ^ n m. iszero n m (plus (pred n) (succ m)) !^ Unfortunately this is not a definition, since we are trying to use “plus” beforeit is defined. Although recursion is fundamental to functional programming, itis not primitive in the lambda calculus, so we must find a way to “program” it!

Com S 541

Fixed Points

!^ In gener

al,a fixed poi

ntofa f

unction i

s a value i

n the funct

ion’s dom ai

n,

w hich i

s m apped t

o itselfby t

he funct

ion.Ther

efore,a

fixed point of a

function

fis a value

p^ such that

f p = p

!^ Examples:

fact 1 = 1fact 2 = 2fib 0 = 0fib 1 = 1 λx. 27 = 27 λx. 6 – x = 3 !^ However, not all functions have exactly one fixed point: “succ n = n + 1”has none, while “id” has infinitely many.

Com S 541

Fixed Point Theorem

Fixed point Theorem:^ For every

F^ there exists a fixed point

X^ such that

F X^ ↔

X.

Proof:^ Let

Y^ ≡ λf. (

λx. f (x x)) (

λx. f (x x))

Now consider:X^

≡^ Y F^ "

(λx. F (x x)) (

λx. F (x x)) "^ F ((λ

x. F (x x)) (

λx. F (x x)))

"^ F X Therefore, the “Y combinator” can always be used to find a fixed point ofan arbitrary lambda expression, if such a fixed point exists.

Com S 541

Recursive Functions Are Fixed Points

!^ We cannot write:

plus^ ≡ λ

n m. iszero n m (plus (pred n) (succ m)) because plus is unbound in the “definition”.! We can, however, abstract over plus:rplus

≡ λ^ plus n m. iszero n m (plus (pred n) (succ m)) !^ Now we seek a lambda expression plus, such that:

rplus plus

↔^ plus !^ I. e., plus is a fixed point of rplus. By the fixed point theorem, we can take:

plus^ ≡^

Y rplus

Com S 541

Unfolding Recursive Lambda Expressions

!^ Consider:plus 1 1

= (Y rplus) 1 1^ "

rplus plus 1 1 " iszero 1 1 (plus (pred 1) (succ 1)) " False 1 (plus (pred 1) (succ 1)) " plus (pred 1) (succ 1) " rplus plus (pred 1) (succ 1) " iszero (pred 1) (succ 1) (plus (pred (pred 1)) (succ (succ 1))) " iszero 0 (succ 1) (...) " True (succ 1) (...) " succ 1 " 2

Com S 541

Fixed-point Semantics

!^ Fixed-point semantics of recursive programs provides the mathematicalsetting for an inheritance model for an object-oriented language.!^ Consider the following definition of the factorial function:

fact =^

λn.^ if^

n = 1^ then

^1 else

n *fact (n – 1)

The identifier fact is equated with a function definition, in which “fact”appears. Thus the definition is self-referential. The use of the name fact torepresent the self-reference is however just a syntactic convention. Inobject-oriented languages we commonly use the special symbol

self :

fact =^

λn.^ if^

n = 1^ then

^1 else

n * self (n – 1)

Com S 541

Function Transformation

!^ By using fixed-point technique, recursive definitions can by transformedinto a non-recursive form:

FACT =

λself.

λn.^ if

n = 1^

then^^1

else^ n * self (n – 1)

!^ FACT is a functional, or a mapping from functions to functions. The formalparameter “self” represents the function to call in order to compute thefactorial function. The original definition of

fact can now be given in terms

ofFACT:

fact =

FACT(fact)

But now

fact is defined as a value that is unchanged when

FACT is applied.

Such value is called a “fixed point” of

FACT. Under certain conditions, it is

possible to compute a unique fixed point, the least fixed point, of anyfunction by using the fixed-point function “fix” that has the followingproperty: if

f= fix(

F), then

F(f)= f

Com S 541

Generators

Definition:A function intended to specify a fixed point whose formal parameterrepresents self-reference is called generator. Thus a generator has theform

G =^ λ^ self. body

where self may occur free in body. Intuitively, self-reference is “unbound”in a generator, while self-reference in its fixed point is connected back tothe generator.

client

O Fixed point

O Generator

self

Com S 541

A Record Generator

!^ Consider the following record generator representing a pair ofvalues where the second depends upon the first:G =

λ^ self. [ base

"^ 7, square

"^ self.base * self.base ]

The fixed point of this generator is the recordm = fix(G) = [ base

"^ 7, square

"^ 49 ]

Com S 541

Wrappers

Definition:A wrapper is a function designed to modify a self-referential structure in aself-referential way; it has two parameters, one representing self-referenceand the other representing the superstructure being modified. Thus awrapper is a function of the form

W =^ λ^

self.^ λ

super. Body

where self and super may occur free in body.! The application of a wrapper to a generator involves binding together self-reference in the wrapper and the generator, and then applying thewrapper modification to the value of the generator:W

⊕^ G =

λ^ self. W(self)(G(self)), where

⊕^ =^ λ

a.^ λb.^

λs. a(s)(b(s))

Com S 541

Applying Wrappers

!^ The result of applying a wrapper to a generator is a generator.Self-references in in the wrapper W and the generator G arebound together though the variable self, signified by thejoining of the arrows out of W and G. The arrow form W to Grepresents the application of W to G:

client^

W^

G super self

self