Docsity

Functional Programming Concepts: Abstraction, Binding, and Church Booleans, Study notes of Computer Science

Partial preview of the text

Download Functional Programming Concepts: Abstraction, Binding, and Church Booleans and more Study notes Computer Science in PDF only on Docsity!

CIS 500

Software Foundations

Fall 2003

22 September

Administrivia

need to switch sections, send mail to [email protected] is still some flexibility in recitation assignments; if you find you

The lambda-calculus

simplestnontrivial programming language, then the lambda-calculus is theIf our previous language of arithmetic expressions was the simplest

interesting

programming language...

T uring complete

higher order (functions as data)

main new feature: variable binding and lexical scope

The e. coli of programming language research

(including ML, Haskell, Scheme, Lisp, ...)The foundation of many real-world programming language designs

Intuitions

we pass it. We might write Suppose we want to describe a function that adds three to any number

     =                 

That is, “

is

.”

Intuitions

we pass it. We might write Suppose we want to describe a function that adds three to any number

     =                 

That is, “

is

.”

Q: What is

itself?

A:

is the function that, given

, yields

                .

5-b

Intuitions

we pass it. We might write Suppose we want to describe a function that adds three to any number

     =                 

That is, “

is

.”

Q: What is

itself?

A:

is the function that, given

, yields

                .

    =                   

This function exists independent of the name

   .

5-c

Essentials

We have introduced two primitive syntactic forms:

abstraction

of a term

on some subterm

:

“The function that, when applied to a value

 , yields

with



in

place of

.”

application

of a function to an argument:





“the function



applied to the argument

 ”

Recall that we wrote anonymous functions “

    

” in OCaml.

Abstractions over Functions

Consider the

-abstraction

=            

Note that the parameter variable



is used in the

function

position in the

body of

. T

erms like

are called

higher-order

functions.

If we apply

to an argument like

, the “substitution rule” yields a

nontrivial computation:

                                       

Example

           

The Pure Lambda-Calculus

As the preceding examples suggest, once we have

-abstraction and

In this language — the “pure lambda-calculus”—have left a rich and powerful programming language.application, we can throw away all the other language primitives and still

everything

is a function.

Variables always denote functions

Functions always take other functions as parameters

The result of a function is always a function

Syntax

::=

terms

variable

abstraction

application

T erm

inology:

terms in the pure

-calculus are often called

-terms

terms of the form

are called

-abstractions

or just

abstractions

Scope

The

-abstraction term

binds

the variable

.

The

scope

of this binding is the

body

.

Occurrences of

inside

are said to be

bound

by the abstraction.

Occurrences of

that are

not

within the scope of an abstraction binding

are said to be

free

.      

Values



::=

values

abstraction value

Operational Semantics

Computation rule:

        



 

    

(E-A

PP

A

BS

)

Notation:

 

 

    

is “the term that results from substituting

free occurrences of

in

 

with

   .”