+ All Categories
Home > Documents > Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne Room K308 Based on Chapter...

Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne Room K308 Based on Chapter...

Date post: 14-Jan-2016
Category:
Upload: rylee-colebank
View: 213 times
Download: 0 times
Share this document with a friend
108
Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Room K308 Based on Chapter 14. A Logical approach to Discrete Math By David Gries and Fred B. Schneider
Transcript
Page 1: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Computing Fundamentals 1 Lecture 8Functions

Lecturer: Patrick Brownehttp://www.comp.dit.ie/pbrowne/

Room K308Based on Chapter 14.

A Logical approach to Discrete Math By David Gries and Fred B. Schneider

Page 2: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• Given two countries Aland and Bland. We can travel from Aland to Bland using AtoB airways. Cities in Aland are called ai and cities in Bland are called bi .

a1

a2

a3

Aland

b1

b2

b3

AtoB

Bland

b4

Page 3: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• From any given city ai in Aland AtoB airways provide one and only one flight to one city bi in Bland. In other words, you can travel from every city in Aland but cannot travel to more than one city in Bland from that Aland city. We can consider the flights offered by AtoB as function called FAtoB.

a1

a2

a3

Aland

b1

b2

b3

FAtoB

Bland

b4

Page 4: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• There are some cities in Bland, such as b4, that are not served by AtoB airways, therefore they cannot be reached from Aland. Obviously, if you cannot get to b4 then you cannot come back to Aland from b4 . (no return tickets)

a1

a2

a3

Aland

b1

b2

b3

AtoB

Bland

b4

Page 5: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• If for every city that AtoB airways flies into in Bland they supply a return ticket, then AtoB supply a pair of functions, FAtoB and FBtoA. We call FAtoB an injective function because it has a left inverse (a return ticket).

a1

a2

a3

Aland

b1

b2

b3

FAtoB

Bland

b4

Page 6: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• If AtoB airways flies into every city in Bland, still using the original rule that only one flight can leave an Aland city, then there may be more than one way back to Aland. For example, from b2 you can fly to a1 or a3. Then FAtoB is called a surjective function. It has a right inverse (but you may not get back to where you started).

a1

a2

a3

Aland

b1

b2

FAtoB

Bland

Page 7: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• If a function is both injective and surjective it is then called bijective, it has both a left and right inverse.

a1

a2

a3

Aland

b1

b2

b3

FAtoB

Bland

Page 8: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• We apply function to argument (function application)• Function definition (dot notation) g.x = 3 x + 6• Function application g(5)• Gives the value of 35+6• To reduce brackets we can write function.argument. • We evaluate this function• g.5• = < Apply function>• 3 5 + 6• = < Arithmetic>• 21

Functions

Page 9: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• Functions can be considered as a restricted form of relation. This is useful because the terminology and theory of relations carries over to function.

• In programming languages like C or CafeOBJ a function can have a signature, which includes its name, type of argument and the type of the expected return value.

Functions

Page 10: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Fibonacci Function in C and CafeOBJ

mod* FIBO-NAT { pr(NAT) op fib : Nat -> Nat var N : Nat eq fib(0) = 0 . eq fib(1) = 1 . ceq fib(N) = fib(p(N)) + fib(p(p(N))) if N > 1 .}

int fib(int n) {if (n <= 1) return n;else return

fib(n-1) + fib(n-2);}

Signature of a function consists of a name, argument(s) type, and return type

Function Name Argument type p is a predecessor function

Argument variable

Page 11: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Fibonacci Function in Python and CafeOBJ

mod* FIBO-NAT { pr(NAT) op fib : Nat -> Nat var N : Nat eq fib(0) = 0 . eq fib(1) = 1 . ceq fib(N) = fib(p(N)) + fib(p(p(N))) if N > 1 .}

def fib(n):

a, b = 0, 1

while b < n:

print(b, end=' ')

a, b = b, a+b

print()

Signature of a function consists of a name, argument(s) type, and return type

Function Name Argument type p is a predecessor function

Argument variable

Page 12: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Fibonacci Function in C and CafeOBJ

mod* FIBO-NAT { pr(NAT) op fib : Nat -> Nat var N : Nat eq fib(0) = 0 . eq fib(1) = 1 . ceq fib(N) = fib(p(N)) + fib(p(p(N))) if N > 1 .}

int fib(int n) {if (n <= 1) return n;else return

fib(n-1) + fib(n-2);}

Signature of a function consists of a name, argument(s) type, and return type.

Return type

Argument constraints

Page 13: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions• A function f is a rule for computing a value v

from another value w, so that the application f(w) or f.w denotes a value v:f.w = v. The fundamental property of function application, stated in terms of inference rule Leibniz is:

f.Yf.XYX

• This property allows us to conclude theorems like f(b+b)= f(2b) and f.b + f.b = 2f.b.

Page 14: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions and programming

• In programming functions can have ‘side effects’ i.e. they can change a parameter or a global variable. For example, if b is changed by the function f then

• f.b + f.b = 2 f.b • no longer holds. This makes it difficult to reason

or prove properties about a program. By prohibiting side-effects we can use mathematical laws for reasoning about programs involving function application.

Page 15: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions as relations

• While we can consider functions as a rule we can also think of functions as a binary relation B C, that contains all pairs <b,c> such that f.b=c. A relation can have distinct values c and c’ that satisfy bfc and bfc’, but a function cannot.

b

c

c’

Allowed for relations but

not allowed for functions

Page 16: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• Function application can be defined as textual substitution. If

• g.z:Expression• Defines a function g then function

application g.X for any argument X is defined by

• g.X = Expression[z := X]

Functions Textual Substitution

Page 17: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Function Def. & Types

• A binary relation f on B C, is called a function iff it is determinate.

Determinate (no fan out): (b,c,c’| b f c b f c’ : c=c’)A function f on B C is total ifTotal: B = Dom.f• Otherwise it is partial. We write f:BC for

the type of f if f is total and f:BC if f is partial.

Page 18: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• This close correspondence between function application and textual substitution suggests that Leibniz (1.5) links equality and function application. So, we can reformulate Leibniz for functions.

g.Yg.XYX

Page 19: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions & Types

• In Computer Science many types are needed. – Simple types like integers and natural numbers– Complex types like sets, lists, sequences.

• Each function has a type which describes the types of its parameters and the type of its result:

• (8.1) f: t1 t1 ... t1 r

Page 20: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions & their types

• Are the following functions?• plus: (plus(1,3)or 1+3)• not : (not(true) or true)• less: (less(1,3)or 1<3)

Page 21: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions & Types

• Certain restrictions are need to insure expression are type correct.

• During textual substitution E[x := F], x and F must have the same type.

• Equality b=c is defined only if b and c have the same type. Treating equality as an infix function:

• _=_ :tt • For any type t.

Page 22: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

14.41 Theorem

Total function f:BC is surjective (or onto ) if Ran.f = C.

Total function f:BC is injective (or one-to-one) if

(b,b’:B,c:C| bfc b’fc b=b’)• A function is bijective if it is injective and

surjective.Compare injective with determinate: (b,c,c’| b f c b f c’ : c=c’)

Page 23: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

14.41 Theorem

• Total function f is injective,one-to-one if

(b,b’:B,c:C| bfc b’fc b=b’)• Total function f:BC is surjective, onto if

Ran.f = C.

• A function is bijective if it is injective and surjective.

Page 24: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

An Injective function can : f: A ->B ={f:A ->B | f-1 B ->A : f} (have inverse)

f: A->B ={f:A ->B | dom f = A : f} (be total)

a1

a2

a3

A

b1 b4

b2

b3

f

dom f ran f

B

Source Target

Page 25: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective function in CafeOBJ(*)

module INJ {[ A B ]op f_ : A -> B op g_ : B -> A var a : A vars a b' : B eq [linv] : g f a = a .ceq [inj] : b = b' if g b == g b' .}

Page 26: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective function in CafeOBJ(*)

• eq [linv] : g f A = A .• [linv] represents an axiom for a left inverse

taking an A to a B and back to the same A. The [linv] equation says that g is a left inverse of f (i.e. g(f(a)) = a).

• ceq [inj] : B = B' if g B == g B' .• The conditional equation [inj] represents the

injective property, that two B’s are the same if they map to the same A. The [inj] equation expresses the injective (or one-to-one) property.

Page 27: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

CafeOBJ Injective Function left inverse mod* INJ { [ A B ]• op f_ : A -> B op g_ : B -> A• var a : A • vars b0 b1 : B • eq [linv] : g f a = a .• ceq [inj] : b0 = b1 if g b0 == g b1 .}

a1

a2

a3

A

b1

b2

b3

f

dom f ran f

B

Not in range of f.

b4

Page 28: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Surjective Function• Total function f:AB is surjective, onto if Ran.f = B.

• Total function f is injective,one-to-one if

(b,b’:B,c:C| bfc b’fc b=b’).

a1

a2

a3

b1

b2

f

a1

a2

a3

A

b1 b4

b2

b3

fB

Page 29: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.
Page 30: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

14.42 Theorem

• Let f:BC be a total function, and let f-1 is its relational inverse. If f is not injective (one-to-one) then f-1 is not a determinate function.

Function f not injective The inverse (f-1) is not determinate

BCC B

Page 31: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

14.42 Theorem

• Let f:BC be a total function, and let f-1 be its relational inverse. Then f-1 is a (i.e. determinate) function iff f is injective (one-to-one). And, f-1 is total if f is surjective (onto).

Function not total Inverse not surjective (onto)

?

B CCB

Page 32: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

14.42 Theorem

• Let f:BC be a total function, and let f-1 be its relational inverse. Then f-1 is total iff f is surjective (onto).

Function not total Inverse not surjective (onto)

?

B CCB

Page 33: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Total & Partial functions

• Dealing with partial functions can be difficult. What’s the value of (f.b=f.b) if bDom.f ?

• The choice of value must be such that the rules of manipulations that we use in the propositional and predicate calculus hold even in the presence of undefined values, and this is not easy to achieve. However, for partial function f:BC one can always restrict attention to its total counterpart, f:Dom.f C

Page 34: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• The binary relation < is not a function because 1 < 2 and 1 < 3 both hold.

• Identity relation iB over B is a total function, iB:BB ; ib=b for all b in B.

• Total function f: is defined by f(n)=n+1 is the relation {<0,1>,<1,2>,…}.

• Partial function f: is defined by f(n) = 1/n is the relation {<1,1/1>,<2,1/2>,<3,1/3>…}. It is partial because f.0 is not defined.

• Note is a natural number, is a rational number.

Page 35: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

is an integer, + is a positive integer, - is a negative integer.

• Function f:+ is defined by f(b)=1/b is total, since f.b is defined for all elements of +. However, g: is defined by g.b =1/b is partial because g.0 is not defined.

Page 36: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• The partial function f takes each lower case character to the next character can be defined by a finite number of pairs {<‘a’,’b’>,<‘b’,’c’>,..,<‘y’,’z’>}

• It is partial because there is no component whose first component is ‘z’.

Page 37: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• When partial and total functions are viewed as binary relations, functions can inherit operations and properties of binary relations.

• Two functions are equal when their sets of pairs are equal.

• Similar to relations, we can have the product and powers (or composition see later) of functions.

Page 38: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Inverses of Total Functions

• Every relation has an inverse relation.

• However, the inverse of a function does not have to be a function. For example:

• f: defined as f(b)=b2.• f(-2)=4 and f(2)=4• f-1(4)=2, two values (inverse is sq. rt).

Page 39: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Inverse of Total Functions

• Partial functions

• Total functions

• Injective or on-to-one

• Surjective or onto

• Bijective has inverse

Page 40: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions Products• We can have the product of two relations.• We now look at the product (f g) of two total functions.• (f g).b = d• = < viewing as relation >• b(f g)b• = < product of relation 14.20>• ( |: bfc cgd)• = < relation as function application >• ( |: f.b=c g.c=d)• = <Trading 9.19>• ( |: c = f.b : g.c=d)• = < one point rule 8.14>• g(f.b) = d

Page 41: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions Products• (f g).b = d• = < viewing as relation >• b(f g)b• = < product of relation 14.20>• ( |: bfc cgd)• = < relation as function application >• ( |: f.b=c g.c=d)• = <Trading 9.19>• ( |: c = f.b : g.c=d)• = < one point rule 8.14>• g(f.b) = d• Hence (f g).b = g(f.b). This illustrates the

difference between relational notation and functional notation.

Page 42: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions Products

• Hence (fg).b = g(f.b). This illustrates the difference between relational notation and functional notation, particularly for products. Functions possess an asymmetry between input and output. Expression f(arg) stands for the value of the function for the argument. On the other hand a relational expression r(a,b) is a statement that may be true or false. The product operation for relations is legal when the range of the first relation is the same as the domain of the second relation.

Page 43: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions Products

• We would prefer f(g.b) instead of g(f.b). So we have the new symbol for composition of functions, defined as:

f g g f

Page 44: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Composition of Functions1

g o f, the composition of f and g

Page 45: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Composition of Functions1

• The functions f:X→Y and g:Y→Z can be composed by first applying f to an argument x and then applying g to the result. Thus one obtains a function:

• gof: X → Z • defined by (gof)(x) = g(f(x)) for all x in X.• The notation gof is read as "g circle f" or "g composed

with f“. • The composition of functions is always associative. That

is, if f, g, and h are three functions with suitably chosen domains and codomains, then f o (g o h) = (f o g) o h.

Page 46: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

A function in Cint addTwo(int arg) Example of usage{return (arg+2);} y = addTwo(7);

arg1

arg2

Argument:int

return1

return2

Return value:int

addTwo(arg1)

addTwo(arg2)

Page 47: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

A function is a relation• A function is a special case of a relation in which there

is at most one value in the range for each value in the domain. A function with a finite domain is also known as a mapping. Note in diagram no diverging lines from left to right.

x1

x2

x4

x6

X

y1

y2

f

Dom.f Ran.f

Y

x5

x3

Page 48: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

A function is a relation• The function f:XY ‘the function f, from X to Y’

• Is equivalent to relation XfY with the restriction that for each x in the domain of f, f relates x to at most one y;

x1

x2

x4

x6

X

y1

y2

f

Dom.f Ran.f

Y

x5

x3

Page 49: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions• The relation between persons and their identity

numbers – identityNo < Person, > or – Person identityNo

• is a function if there is a rule that a person may only have one identity number . It could be defined:

• identityNo: Person • Perhaps there should also be a rule that only one

identity number may be associated with any one person, but that is not indicated here.

Page 50: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• A functions source and target can be the same ‘domain’ or type:

• hasMother: Person Person

• any person can have only one mother and several people could have the same mother.

Page 51: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Function Application

• All the concepts which pertain to relations apply to functions.

• In addition a function may be applied.

• Since there will be at most only one value in the range for a given x it is possible to designate that value directly.

• The value of f applied to x is the value in the range of function of the function f corresponding to the value of x in its domain.

Page 52: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Function Application

• The application is undefined if the value of x is not in the domain of f.

• It is important to check that the value of x is in the domain of the function f before attempting to apply f to x.

• The application of the function to the value x (the argument) is written: f.x or f(x)

Page 53: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Function Application

• We can check the applicability of f by writing: • x dom f • f.x = y• These predicates must be true if f(x)=y is a

function.

Page 54: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Partial and Total Functions• If the identyNo function is a partial function there

may be values in the source that are not in the domain (some people may have no identity number).

• A total function is one where there is a is a value for every possible value of x, so f.x is always defined. It is written

• f: X Y • Because: • Dom f = X (or source)

• function application can always be used with a total function.

Page 55: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Total Functions

• The function age, from Person to natural number, is total since every person has an age:

• age: Person • The function hasMother, is total since every

person has one mother (including a mother!): • hasMother : Person Person

Page 56: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective Function

• An injective function may be partial or total.

• The functions identityNo and MonogamousMarriage are injective.

Page 57: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Isomorphism

• Given two functions: • f:A->B, g:B->A • f is isomorphism or invertible map iif gf=IA and fg=IB .

• Where IA, IB are the identity functions of A and B respectively.

• A left inverse of f is a function g such that – g(f(x)) = x. – or using alternative notation – g f x = x

Page 58: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Isomorphism

• Let f:A->A and g:B->B be two functions on A and B respectively. If there is a mapping t that converts all x in A to some u=t(x) in B such that g(t(x))=t(f(x)), then the two functions f and g are homomorphic and t is a homomorphism

• If also if t is bijective, then the two functions are isomorphic and t is an isomorphism with respect to f and g.

Page 59: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Isomorphism

x t u

y t z

Arabic f g Roman

Page 60: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

An Injective functions1.

Two injective functions A non-injective function.

Page 61: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective Function f

• Using the INJ module and the CafeOBJ apply command we will prove that an injective function with a right inverse is an isomorphism.

• For f:A->B, g:B->A is isomorphism or invertible map iif gf=IA and fg=IB

• A function f has at least one left inverse if it is an injection.

• A left inverse is a function g such that • g f a = a or• g(f(a)) = a • See lab 6 for details

Page 62: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective Function fmodule INJ { -- module name[ A B ] -- two sortsop f_ : A -> B -- a function from A to Bop g_ : B -> A -- a function from B to Avar a : A -- variablesvars b b' : B -- equation for left inverse equation of feq [linv] : g f a = a .-- conditional equation for injective propertyceq [inj] : b = b' if g b == g b' .}

The [Linv] equation says that g(f(x)) = x, i.e. g is a left inverse of f.The [inj] equation expresses the injective (or one-to-one) property.A function f has at least one "left inverse" if and only if it is an injection.

Page 63: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective Function f

open INJ . op b : > B . -- make a constant b

-- make a right inverse as the term to which the equation(s) will be applied to.

start f g b .

-- substitute B = f g b (our term) and B' = b in [inj]

apply .inj with B' = b at term . -- normal evaluation will execute [linv]

apply red at term .

result b : B

Page 64: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective Function f

• In CafeOBJ equations are written declaratively and interpreted operationally as rewrite rules, which replace substitution instances of the LHS by the corresponding substitutions instances of the RHS. The operational semantics normally require that the least sort of the LHS is greater that or equal to the least sort of the RHS. The [Linv] equation says that g(f(x)) = x (or g is a left inverse of f). The [inj] equation expresses the injective (or one-to-one) property.

Page 65: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Right Inverse

• module* GROUP {• [ G ] • op 0 : -> G • op _+_ : G G -> G { assoc } • op -_ : G -> G • var X : G • eq [EQ1] : 0 + X = X . • eq [EQ2] : (-X) + X = 0 . }

Page 66: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Right Inverse manual Proof

• We want to prove that –X is a right inverse of +.• A standard proof goes as follows:

1. X + ( X) =

2. 0 + X + ( X) =

3. ( ( X)) + ( X) + X + ( X) =

4. ( ( X)) + 0 + ( X) =

5. ( ( X)) + ( X) =

6. 0

Page 67: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Right inverse CafeOBJ proof

• open GROUP • op a : > G . -- a constant• start a + ( a) . – the term• apply .1 at (1) . • apply .2 with X = a at [1] . • apply reduce at term .

Page 68: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

• Injective composition.

Page 69: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

An Surjective functions1.

Two surjective functions A non-surjective function

Page 70: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

An Surjective Composition1.

•Surjective composition: the first function need not be surjective

Page 71: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

A bijection 1.

Composition gof of two functions is bijective, we can only say that f is injective and g is surjective.

A bijective function

Page 72: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

A function is called a bijection , if it is onto and one-to-one. A bijective function has an inverse

Function Application

Some examples, range on the left domain on the right

A function f from X to Y is a relation from X to Y having the properties:1. The domain of f is in X.2. If (x,y),(x,y’)f, then y=y’ (no fan out from domain).A total function from X to Y is denoted f: X Y.

Page 73: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Injective Surjective

Page 74: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• Which of the following sets P, Q, and R represents a partial, surjective, injective or bijective function from

• X={1,2,3,4} to Y={a,b,c,d}?

Page 75: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• X={1,2,3,4} to Y={a,b,c,d}

• P = {<1,a>,<2,a>,<3,c>,<4,b>}

• It is a function from X to Y.

• Domain = X, range = {a,b,c}.

• The function neither injective (both 1 and 2 map to a) nor surjective(nothing maps to d)

Page 76: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Functions

• X={1,2,3,4} to Y={a,b,c,d}• P = {<1,a>,<2,a>,<3,c>,<4,b>}

• Q = {<1,c>,<2,a>,<3,b>,<4,c>,<2,d> }• It is not a function from X to Y, because 2 maps to both a

and d.

• R = {<1,c> ,<2,d>,<3,a>,<4,b>}• It is a function from X to Y.• Domain = X, range =Y. The function both injective and

surjective.

Page 77: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

CafeOBJ Error sorts

• The idea of error sorts is to add new elements to the models of a specification and to let the result of a function be one of the error elements, whenever the function is not defined for an input.

• Error sorts can be included in the definition of the function, specifying for which inputs the result will be an error.

Page 78: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

CafeOBJ Error sorts

• From CafeOBJ Manual• An error sort is automatically declared for each

connected component of the sort graph, and has an internal name that starts with ?.

• 2 rules for error sorts.• (1) If a term is ill-formed but potentially well-formed, it is

regarded as a well-formed term of questionable sort.• (2) On evaluation, the potential is investigated. If the

result is a term which is unquestionably well-formed, it acquires a full citizenship. Otherwise it remains an outcast (i.e. an error sort).

Page 79: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

CafeOBJ parse and errors

• Factorial (!) and division (/) are def defined in the FACT and RAT modules respectively. Division is not defined for zero.

parse in FACT + RAT : (4 / 2) .parse in FACT + RAT : (4 / 0) .parse in FACT + RAT : (4 / 2) ! .A \/ B \/ C The principle of contradiction – that a thing cannot be and not be at the same time and in the same respect- is an axiom of thought, a law of reason of greater certitude than any law of science.

Page 80: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

CafeOBJ Error sorts

Above the error super-sort ?Elt for the Elt.

Every sort has an error super-sort (or super set).

Page 81: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

A Function

GivenSide = left rightthe functiondriveON Country Side • Representing the side of the road vehicles drive

on is a surjection, because the range includes all the values of the type Side. It would usually be considered total because driveOn should be defined in all countries.

Page 82: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

What is a Hash Function?• A hash function takes a data item to be stored

or retrieved (say to/from disk) and computes the first choice for a storage location for the item. For example assume StudentNumber is the key field of a student record, and one record is stored on one disk block. To store or retrieve the record for student number n in a choice of 11 disk locations numbered 0 to 10 we might use the hash function.

• h(n) = n mod 11

Page 83: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

0 1 2 3 4 5 6 7 8 9 10

Describe the steps needed to store a record for student number

257 using in locations disk 0-10 using h(n) = n mod 11

132 515102 558 32

Steps to store 257 using h(n)=n mod 11 into locations 0-10

1. Calculate h(257) giving 4.

2. Note location is already occupied, a collision has occurred.

3. Search for next free space , with 0 assumed to follow 10.

4. If no free space then array is full, otherwise store 257 in next free space

0 1 2 3 4 5 6 7 8 9 10

132 515102 558 32257

Page 84: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Exercise 1

• Only one person can book a room. A system records the booking of hotel rooms on one night. Given the domain of discourse consists of:

• Rooms the set of all rooms in hotel• Person the set of all possible persons• The state of the hotel’s bookings can be

represented by the following:• bookedTo: Room Person

Page 85: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Exercise 1 Solution

(a) Explain why bookedTo is a function.• bookedTo is a function since it maps rooms to

persons and for any given room(domain) at most one person (range) can book it. A person can book any number of rooms.

b) Explain why bookedTo is partial.

The function is partial since not all rooms have been booked.

Page 86: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Example

• Which of the following sets P, Q, and R represents a partial, surjective, injective or bijective function from X={1,2,3,4} to Y={a,b,c,d}? In each case explain your reasoning.

• P = {<1,a>,<2,a>,<3,c>,<4,b>} • Q = {<1,c>,<2,a>,<3,b>,<4,c>,<2,d> }• R = {<1,c> ,<2,d>,<3,a>,<4,b>}

Page 87: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Example

• Which of the following sets P, Q, and R represents a partial, surjective, injective or bijective function from X={1,2,3,4} to Y={a,b,c,d}? In each case explain your reasoning.

• P = {<1,a>,<2,a>,<3,c>,<4,b>} • P is a total function from X to Y.• Domain = X, range={a,b,c}. The function

neither injective (both 1 and 2 map to a) nor surjective (nothing maps to d).

Page 88: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Example

• Which of the following sets P, Q, and R represents a partial, surjective, injective or bijective function from X={1,2,3,4} to Y={a,b,c,d}? In each case explain your reasoning.

• Q = {<1,c>,<2,a>,<3,b>,<4,c>,<2,d> }• Q is not a function from X to Y, because 2 maps

to both a and d.

Page 89: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Example

• Which of the following sets P, Q, and R represents a partial, surjective, injective or bijective function from X={1,2,3,4} to Y={a,b,c,d}? In each case explain your reasoning.

• R = {<1,c> ,<2,d>,<3,a>,<4,b>}• R is a total function from X to Y.• Domain = X, range =Y. The function both

injective and surjective.

Page 90: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Exercise

• Which of the following sets represents a functions from {1,2,3} to {4,5,6}? If it is a functions, what type of function is it?.

• P = {<1, 4> ,<2,5>, <3, 4>} • Q = {<1, 4> ,<2, 6>}• R = {<1, 4> ,<2, 1>,<3,3>}

Page 91: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Exercise

• Which of the following sets represents a functions from X = {1,2,3,4} to Y={a,b,c,d}? If it is a functions, what type of function is it?

• P = {<1,a>,<2,a>,<3,c>,<4,b>} • Q = {<1,c>,<2,a>,<3,b>,<4,c>,<2,d> }• R = {<1,c> ,<2,d>,<3,a>,<4,b>}• R = {<1,d> ,<2,d>,<4,a>} • R = {<1,b> ,<2,b>,<3,b>,<4,b>}

Page 92: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Exercise-Solution 1

• Does P represents a functions from X = {1,2,3,4} to Y={a,b,c,d}? If it is a functions, what type of function is it?

• P = {<1,a>,<2,a>,<3,c>,<4,b>} • It is a function from X to Y.

• Domain = X, range = {a,b,c}. The function neither injective nor surjective.

Page 93: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Exercise-Solution 2

• Does Q represents a functions from X = {1,2,3,4} to Y={a,b,c,d}? If it is a functions, what type of function is it?

• Q = {<1,c>,<2,a>,<3,b>,<4,c>,<2,d> }

• It is not a function from X to Y.

Page 94: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Exercise-Solution 3

• Does R represents a functions from X = {1,2,3,4} to Y={a,b,c,d}? If it is a functions, what type of function is it?

• R = {<1,c> ,<2,d>,<3,a>,<4,b>}• It is a function from X to Y.• Domain = X, range =Y. The function both

injective and surjective, therefore bijective.

Page 95: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Function on Sets

• Determine whether the set P represents a function from X={e,f,g,h} to Y={a,b,c,d}.

• P = {<e,a>,<f,a>,<g,c>,<h,b> }• It is a function from X to Y.

• Domain = X, range = {a,b,c}.

• The function neither injective nor surjective. Why?

Page 96: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Function on Sets

• Determine whether the set Q represents a function from X={e,f,g,h} to Y={a,b,c,d}.

• Q={<e,c>,<f,a>,<g,b>,<h,c>,<f,d>}

• It is not a function from X to Y.

• Why?

Page 97: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Function on Sets

• Determine whether the set R represents a function from X={e,f,g,h} to Y={a,b,c,d}.

• R = {<e,a> ,<f,b>,<g,c>,<h,d>}• It is a function from X to Y.

• Domain = X, range =Y. The function both injective and surjective, therefore bijective.

Page 98: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Identifying Functions

• Which of the following are functions? • Explain why they are or are not functions?1. parent(child:Person)->parent:Person

2. mother(child:Person) -> parent:Person

3. oldestChild(parent:Person) -> child:Person

4. allChilderenOf(parent:Person) -> (Person)

Page 99: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Identifying Functions(*)

• Let the sort Person represent the set of all people and the sort PPerson represent the power set of Person. [Person < PPerson ]

• Which of the following are total functions:1) Returns the parents of the argument• op parentOf : Person -> Person

2) Returns the oldest child of the argument• op oldestChildOf : Person -> Person

3) Returns all the children of the argument• op allChildrenOf : Person -> PPerson

Page 100: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

INJ Explained

1. eq [linv] : g f a = a .2. ceq [inj]: b0 = b1 if g b0 == g b1 .

3. open INJ . 4. op b : > B . 5. start f g b . – right inverse6. apply .inj with b1 = b at term7. apply red at term .Print out notes section slides 90-95, run

slide show 90-95. Try to relate notes to slide show.

Page 101: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

INJ Explained

1. eq [linv] : g f a = a2. ceq [inj]: b0 = b1 if g b0 == g b1

3. open INJ . 4. op b : > B .

5. start f g b . – right inverse6. apply .inj with b1 = b at term7. apply red at term .

Substitutions are : f g b b f g b b

Page 102: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

INJ Explained

1. eq [linv] : g f a = a2. ceq [inj]: b0 = b1 if g b0 == g b1

3. open INJ . 4. op b : > B .

5. start f g b . – right inverse6. apply .inj with b1 = b at term7. apply red at term .

giving : ceq [injNew] : f g b = b if g f g b == g b

Page 103: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

INJ Explained

1. eq [linv] : g f a = a2. ceq [inj]: b0 = b1 if g b0 == g b1

3. open INJ . 4. op b : > B .

5. start f g b . – right inverse6. apply .inj with b1 = b at term7. apply red at term .

Eval :ceq [injNew] : f g b = b if g f g b == g b

evaluate

Page 104: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

1. eq [linv] : g f a = a2. ceq [inj]: b0 = b1 if g b0 == g b1

3. open INJ . 4. op b : > B .

5. start f g b . – right inverse6. apply .inj with b1 = b at term7. apply red at term .

giving :ceq [injNew] : f g b = b if g f g b == g b

Substitutions g b g b

INJ Explained

Page 105: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

1. eq [linv] : g f a = a2. ceq [inj]: b0 = b1 if g b0 == g b1

3. open INJ . 4. op b : > B .

5. start f g b . – right inverse6. apply .inj with b1 = b at term7. apply red at term .

giving :ceq [injNew] : f g b = b if g f g b == g b

Giving new equation eq [linvNew] : g f g b = g b

INJ Explained

Page 106: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.
Page 107: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.
Page 108: Computing Fundamentals 1 Lecture 8 Functions Lecturer: Patrick Browne  Room K308 Based on Chapter 14. A Logical approach.

Algebra with two sorts

mod! TWOSORTS {[ T V ]

op f : T -> Top g : T -> V

-- Specific elements of Tops t1 t2 t3 t4 t5 : -> T

-- Specific elements of Vops v1 v2 v3 v4 : -> V

-- equations for feq f(t1) = t3 .eq f(t3) = t4 .eq f(t4) = t5 .-- equations for geq g(t1) = v1 .eq g(t2) = v2 .eq g(t4) = v4 .eq g(t5) = v4 .}

open TWOSORTS**> Some reductionsred f(t1) .red f(f(f(t1))) .red g(t4) == g(t5) .


Recommended