functions. a function between two sets is a relation between those sets that has a special property,...

29
Functions

Post on 22-Dec-2015

222 views

Category:

Documents


3 download

TRANSCRIPT

Functions

Functions

• A function between two sets is a relation between those sets that has a special property, namely that each member of the from-set is related to at most one member of the to-set.

Functions

Ipoh

Kota Bharu

Alor Star

Seremban

Pasir Mas

Kangar

Perak

Kelantan

Kedah

Neg. Sembilan

cities states

isin

Partial Function

• From the diagram the city can only be in one state; there is only one arrow connecting it

• The domain of the function need not be the whole of the from-set

Partial Function : Examples

• Isin : cities ↛ states• Lent_to is a function that allows a book to be borrowed by only one

person at a time can be declared as

Lent_to : Book ↛ Person• The type of Lent_to is p(Book Person)• The predicate Book ↛ Person Book Person formalizes the fact

that every function from Book to Person is also a relation.

Lent_to : Book ↛ Person

Lent_to = {r : Book Person | b : Book; p, q : Person

(( b r p b r q ) p = q ))}

Partial Function : Example

Some notes

• Domain, range, domain restriction, range restriction, domain subtraction, range subtraction, inverse, composition apply to functions

• Inverse of a function may not be a function• Union, intersection, difference can be used for functions• Union of two functions might not be a function

Function definition by lambda abstraction

Function definition by lambda abstraction

• Lambda expression is equivalent to function described by set expression

x : T | pred • term is equivalent to

{x : T | pred • x ↦term}

• Example : function which maps any number of which is less than 5 to its square

n : | n < 5 • n2 can also be written as

{n : | n < 5 • n ↦ n2 }

Function application

• Since a function maps each element of its domain to one range element we use the notation

f x

to denote for any x in the domain of f the unique range element to which f maps x

• Example: let Lent_to : Book ↛ Person

suppose b : Book is in the domain Lent_to then we apply Lent_to to b by writing

Lent_to b

this denote the person to whom the book b has been lent. b is called the argument of the function application. Lent_to b is the value of for argument b

Examples

• Definition by set comprehension

isqrt : { n : | n 0 n2 ↦ n }we would have

isqrt 9 = 3

or we don’t have the function name { n : | n 0 n2 ↦ n } 9 = 3

Total Functions

• A function is total function when its domain is the whole of its from-set

Total Functions - example

• Suppose we define a new relation relating each Potter to their age in years:

ageofPotter : Potters

it is a function since each Potter is related to only one number.

every Potter has an age so the domain of this function is the whole of the from-set. This means the function is total

• We use → to denote total function• Example: we would declare

ageofPotter : Potters →

Toolkit functions

Function Overriding

Another example

• Suppose the present value of Lent_to function is given by the following set enumeration

old == {29 ↦ joe, 567 ↦ sue, 49 ↦joe, 1022 ↦les}

suppose certain amendments are to be incorporated by the following set enumeration

new == {29 ↦ jim, 95 ↦ joe}

if we have old ⊕ new then the result will be

{29 ↦ jim, 95 ↦ joe, 567 ↦ sue, 49 ↦ joe, 1022 ↦ les}

Injective Functions

• If each element of the domain is mapped to a different element of the to-set, then the function is injective (partial injection or 1 to 1 function)

• Second members of the pairs are unique• Denoted by • if the domain of an injection is the whole of the from-set, we call the

injection a total injection• Denoted by ↣

Surjective functions

• If the range of the function is the whole of the to-set, then the function is surjective (onto)

• For each second member of the pairs, there is at least one first member of the pairs

• Notations used

partial surjective

total surjective

Bijection functions

• A function which is both injective and surjective is said to be bijective ( 1 to 1 correspondance)

• Notations used

• When declaring a function in a specification the important questions to consider are as follows: is it total or partial? is it injective? is it surjective? is the source a finite set?

Data type definition: free type definition

• Suppose we have fiction and non-fiction books. The given set for them is [Non_Fiction, Fiction]

let say we want to construct a type Book to cover both kinds: we define

Book ::= fict << Fiction >> | nfict << Non_Fiction >>

the definition introduces:

1) the new type Book

2) two construct functions fict and nfict

<< …. >> is called disjoint union bracket

Data type definition: free type definition

• The above definition is equivalent to:

1) [Book]

2) fict : Fiction ↣Book

3) nfict : Non_Fiction ↣Book

4) ran fict ran nfict = 5) Book = ran fict ran nfict

Given this declaration

f : Fiction; Lent_to : Book ↛Person

The person who has borrowed f is Lent_to (fict f)