Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Fundamentals 1 Lecture 8 Functions

Similar presentations


Presentation on theme: "Computing Fundamentals 1 Lecture 8 Functions"— Presentation transcript:

1 Computing Fundamentals 1 Lecture 8 Functions
Lecturer: Patrick Browne Room K308 Based on Chapter 14. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

2 Given two countries Aland and Bland
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 . b4 a1 a2 a3 AtoB b1 b2 b3 Aland Bland

3 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. b4 a1 a2 a3 FAtoB b1 b2 b3 A rough analogy is, physically a person can only make one trip from one airport at time. This analogy does not accurately represent the situation because with a real airlines a passenger could fly out of the same airport to different destinations over a period of time. The fact we can travel from every city means we are talking about total functions. Aland Bland

4 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) b4 a1 a2 a3 AtoB b1 b2 b3 Roughly no return tickets corresponds to no inverse for b4 Aland Bland

5 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). b4 a1 a2 a3 FAtoB b1 b2 b3 FAtoB is the inverse of FBtoA. An injective function is one-to-one. If e is an identity element of (S, * ) and a * b = e, then a is called a left inverse of b and b is called a right inverse of a. Aland Bland

6 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 FAtoB b1 b2 FAtoB has a right inverse. If you fly from Bland to Aland you can get back to the same city in Bland. FAtoB FBtoA Aland Bland

7 If a function is both injective and surjective it is then called bijective, it has both a left and right inverse. a1 a2 a3 FAtoB b1 b2 b3 A bijective function is also known as an ‘isomorphism’ or ‘invertible map’ Aland Bland

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

9 Functions 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.

10 Fibonacci Function in C and CafeOBJ
Signature of a function consists of a name, argument(s) type, and return type Function Name Argument type p is a predecessor function 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); } Argument variable

11 Fibonacci Function in Python and CafeOBJ
Signature of a function consists of a name, argument(s) type, and return type Function Name Argument type p is a predecessor function 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() Argument variable

12 Fibonacci Function in C and CafeOBJ
Signature of a function consists of a name, argument(s) type, and return type. Return type 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); } There are other elements above that are closely related to the signature e.g. the a variable name for the argument and return values. But generally a signature of a function is just its name and types The signature of a ‘module’ is a list of function signatures. Fibonacci: After two starting values, each number is the sum of the two preceding numbers. The first Fibonacci numbers, also denoted as Fn, for n = 0, 1, … , are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, , , Argument constraints

13 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: This property allows us to conclude theorems like f(b+b)= f(2b) and f.b + f.b = 2f.b.

14 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.

15 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. c Informally we can say that there is ‘no fan out’ from domain to range. Allowed for relations but not allowed for functions b c’

16 Functions Textual Substitution
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]

17 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 if Total: B = Dom.f Otherwise it is partial. We write f:BC for the type of f if f is total and f:BC if f is partial. Note: partial and total functions differ from ‘partial order’ and ‘total order’ in relations. The determinate property formally defines ‘no fan out’. All functions whether they are total or partial are determinate. Partial means that there are elements in the domain for which the function is not defined. You could say that the partiality is a property of the domain whereas injective, surjective, and bijective (discussed later) properties are properties of the range.

18 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.

19 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

20 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)

21 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: _=_ :tt  For any type t.

22 14.41 Theorem Total function f:BC is surjective (or onto ) if Ran.f = C. Total function f:BC 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’) The definition of injective uses the relational for Domain-RelationName-Co-Domain These definitions are only concerned with total function. Partial functions (elements in the domain for which the function is not defined) are more tricky. Roughly partiality is a property of the domain whereas injective, surjective, and bijective properties are properties of the range.

23 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:BC is surjective, onto if Ran.f = C. A function is bijective if it is injective and surjective. (g f a) should give back the same a:A. (f g b) should give back the same b:B.

24 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) Target Source f a1 a2 a3 b1 b4 b2 b3 (g f a) should give back the same a:A. (f g b) should give back the same b:B. mod* INJ { -- A function f has at least one "left inverse" if and only if it is an injection. -- A left inverse is a function g such that g(f(x)) = x. -- Page 173 of Gries and Schneider has the following definitions of one-to-one -- A function is one-to-one if for different arguments it yields different arguments -- (forAll x,y | x =/= y : f.x =/= f.y) -- alternatively -- (forAll x,y | x == y : f.x == f.y) [ 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 . } eof -- here is an annotated proof open INJ . op b2 : ­> B . start f g b this term should return b2 show term . (f (g b2)) : B – this is the term that will be used in the .inj equation apply .inj with b1=b2 at term . -- response shifting focus to condition -- condition(1) g (f (g b2)) == g b2 : Bool show pending . [pending actions] 1| in f (g b2) at top | rule ceq [inj] : b0 = b2 if g b0 == g b2 | condition g (f (g b2)) == g b2 replacement b2 apply red at term . close . A B dom f ran f

25 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' . }

26 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.

27 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 .} Not in range of f. b4 a1 a2 a3 f b1 b2 b3 -- here is an annotated proof open INJ . op b2 : ­> B . start f g b this term should return b2 show term . (f (g b2)) : B – this is the term that will be used in the .inj equation apply .inj with b1=b2 at term . -- response shifting focus to condition -- condition(1) g (f (g b2)) == g b2 : Bool show pending . [pending actions] 1| in f (g b2) at top | rule ceq [inj] : b0 = b2 if g b0 == g b2 | condition g (f (g b2)) == g b2 replacement b2 apply red at term . close . A B dom f ran f

28 f f Surjective Function a1 a2 a3 b1 b2 a1 a2 a3 b1 b4 b2 b3 A B
Total function f:AB 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 f b1 b2 a1 a2 a3 b1 b4 b2 b3 f A B

29

30 14.42 Theorem Let f:BC 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. B C A functions ‘relational inverse’ does not have to be a function. The above shows an element in C mapping to two elements in B hence fan out. B C Function f not injective The inverse (f-1) is not determinate

31 14.42 Theorem Let f:BC 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). f-1 is total iff f is surjective onto. The left diagram above shows that there are some elements in B that are not mapped to C. B C B C ? Inverse not surjective (onto) Function not total

32 14.42 Theorem Let f:BC be a total function, and let f-1 be its relational inverse. Then f-1 is total iff f is surjective (onto). B C B C ? Inverse not surjective (onto) Function not total

33 Total & Partial functions
Dealing with partial functions can be difficult. What’s the value of (f.b=f.b) if bDom.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:BC one can always restrict attention to its total counterpart, f:Dom.f C

34 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:BB ; 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.

35 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.

36 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’.

37 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.

38 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).

39 Inverse of Total Functions
Partial functions Total functions Injective or on-to-one Surjective or onto Bijective has inverse

40 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

41 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.

42 Functions Products Hence (fg).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.

43 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

44 Composition of Functions1
g o f, the composition of f and g 1 From

45 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.

46 A function in C arg1 arg2 return1 return2 Return value:int
int addTwo(int arg) Example of usage {return (arg+2);} y = addTwo(7); arg1 arg2 addTwo(arg1) return1 return2 addTwo(arg2) Argument:int Return value:int

47 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. f x5 x1 x2 x4 x6 y1 y2 X x3 Y Dom.f Ran.f

48 A function is a relation
The function f:XY ‘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; f x5 x1 x2 x4 x6 y1 y2 X x3 Y Dom.f Ran.f

49 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.

50 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.

51 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.

52 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)

53 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.

54 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.

55 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

56 Injective Function An injective function may be partial or total.
The functions identityNo and MonogamousMarriage are injective.

57 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

58 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. f(x)=x+2 for Arabic numerals g(u) = u + ii for Roman numerals t(a) maps every Arabic numeral to every Roman numeral. The functions f and g are isomorphic and t is an isomorphism with respect to f and g.

59 Isomorphism x t u Arabic f g Roman y t z f(x)=x+2 for Arabic numerals
g(u) = u + ii for Roman numerals t(a) maps every Arabic numeral to every Roman numeral. The functions f and g are isomorphic and t is an isomorphism with respect to f and g. y t z

60 An Injective functions1.
Two injective functions A non-injective function. 1

61 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

62 Injective Function f module INJ { -- module name [ A B ] -- two sorts op f_ : A -> B -- a function from A to B op g_ : B -> A -- a function from B to A var a : A -- variables vars b b' : B -- equation for left inverse equation of f eq [linv] : g f a = a . -- conditional equation for injective property ceq [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. -- From Lab 6 -- Task 1 theorem proving with 'apply' command. -- We will use apply to prove a theorem. -- For more detail see course notes. -- Using the INJ module(below) and the CafeOBJ 'apply' command -- we will prove that an injective function with a right inverse is an isomorphism. -- We need to understand the following terms: -- A (left) <-> B (right) -- right inverse: f:A->B, g:B->A, fg=IB -- left inverse: f:A->B, g:B->A, gf=IA -- isomorphism: f:A->B, g:B->A f is isomorphism or invertible map iif gf=IA and fg=IB -- injective function: every element in the domain is used (total) and every element in codomain is used. -- In the module INJ we are given two equations which represent the fact that f is injective (has a left inverse) ) The left inverse (linv) says g f a = a, gives the identity of a. ) The injective equation (inj) says two b's are equal only if g brings them to the same a (no fan-out for g). This equation asserts that two variables are equal, the apply command is used to evaluate it.

63 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 -- From Lab 6 -- Paste in each of the non-comment lines into CafeOBJ -- Try to understand the output. set trace on open INJ . op b : ­> B . -- make a constant b start f g b . -- make a right inverse as the term to which the equation(s) will be applied to. apply .inj with B' = b at term . -- put B = f g b(our term) and B' = b in [inj] -- this will cause the focus to shift to the condition. show term . -- ((g (f (g b))) == (g b)) : Bool show tree . apply red at term . -- normal evaluation will execute [linv] -- here is full trace -- 1>[7] rule: eq [linv] : g (f A:A) = A -- { A:A |-> g b } -- 1<[7] g (f (g b)) --> g b -- [7]: g (f (g b)) == g b -- -> g b == g b -- 1>[8] rule: eq CXU == CYU = #{ CXU |-> g b, CYU |-> g b } -- 1<[8] g b == g b --> true -- [8]: g b == g b ---> true -- condition(1) true : Bool -- condition is satisfied, applying rule -- shifting focus back to previous context -- result b : B -- So given an injective function with a left inverse -- we applied a 'right inverse'(our term) and an arbitrary 'b' which returned that 'b' -- This proves that f (or g) is an isomorphism. close

64 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.

65 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 . }

66 Right Inverse manual Proof
We want to prove that –X is a right inverse of +. A standard proof goes as follows: X + (­ X) = 0 + X + (­ X) = (­ (­ X)) + (­ X) + X + (­ X) = (­ (­ X)) (­ X) = (­ (­ X)) + (­ X) = CafeOBJ does substitution using rewrite rules converting the LHS to the RHS (where it can) But in two lines of the above proof requires two reverse applications of rewrite rules i.e. converting from RHS to LHS. Moving from line 1 to line 2 requires the reverse of EQ1 Moving from line 2 to line 3 requires the reverse of EQ2

67 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 . Note difference between show rules and show rule show rules Two versions of show rule show rule .1 show rule .EQ1 You can use the name (EQ1) or the number (.1) of rewrite rules with show command. of reversed rules, reduction yields the desired result, as follows. open GROUP op a : -> G -- a is used to distinguish it from X in the equations start a + (- a) -- this is the term to be proved (try show term). apply ­.1 at (1) apply the first rule (EQ1) in reverse to the current term (at (1)) *result 0 + a + ­ a : G apply ­.2 with X = ­ a at [1] . – apply the reverse of second rule (EQ2) substituting –a for X in the first argument of EQ2. result ­ (­ a) + ­ a + a + ­ a : G apply reduce at term . – Finally evaluate the current term result 0 : G Look in more detail what happened at the second apply command. This command applied eq (­ X) + X = 0 . to the subterm 0. Without saying anything, a variable X would be introduced. This situation often occurs when a rule is reversely applied. The substitution ``X = ­ a'' avoids that. You can continue, and prove another theorem that 0 is a right identity (so the identity). Since we already showed ``a + (­ a)'' equals 0, it is sound to add the equation. That leads to the command sequence eq a + (­ a) = 0 . start a + 0 . apply ­.2 with X = a at (2) .

68 Injective composition.

69 An Surjective functions1.
Two surjective functions A non-surjective function 1

70 An Surjective Composition1.
Surjective composition: the first function need not be surjective 1

71 A bijection 1. A bijective function
Composition gof of two functions is bijective, we can only say that f is injective and g is surjective. 1

72 A function f from X to Y is a relation from X to Y having the
Some examples, range on the left domain on the right A function is called a bijection , if it is onto and one-to-one. A bijective function has an inverse Function Application 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.

73 Injective Surjective For each map state whether or not it represents a function. If it is a function state whether or not it represents a surjective, injective or bijective function. In each case explain your reasoning.

74 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}?

75 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)

76 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.

77 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.

78 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).

79 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. parse in FACT + RAT : (3 / 0) . (3 / 0):?Rat parse in FACT + RAT : (4 / 0) . (4 / 0):?Rat parse in FACT + RAT : (4 / 0) ! . ((4 / 0) !):?Rat parse in FACT + RAT : (4 / 2) ! . ((4 / 2) !):?Rat

80 CafeOBJ Error sorts Above the error super-sort ?Elt for the Elt.
Every sort has an error super-sort (or super set).

81 A Function Given Side = left  right the function
driveON 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.

82 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

83 Describe the steps needed to store a record for student number 257 using in locations disk 0-10 using h(n) = n mod 11 558 32 132 102 15 5 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 558 32 132 102 15 5 257

84 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

85 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. A partial function is a binary relation that associates each element of a domain set with at most one element of another codomain set. It is not defined for some inputs of the right type, that is, for some of the domain. Another way of saying this is, that not every element of the domain has to be associated with an element of the codomain.

86 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>}

87 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).

88 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.

89 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.

90 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>}

91 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>}

92 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.

93 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.

94 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.

95 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?

96 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?

97 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.

98 Identifying Functions
Which of the following are functions? Explain why they are or are not functions? parent(child:Person)->parent:Person mother(child:Person) -> parent:Person oldestChild(parent:Person) -> child:Person allChilderenOf(parent:Person) -> (Person) 1: Not a function, because people have two parents, which contradicts definition of function. 2: Is a total function, because everyone has exactly one biological mother. 3: Not a total function, because some people have no children, therefore function is undefined for some of the domain. 4: Is a function, because for those with children a set is returned, for those people without children the empty set is returned. In either case the domain and range are defined.

99 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

100 INJ Explained open INJ . op b : ­> B .
eq [linv] : g f a = a . ceq [inj]: b0 = b1 if g b0 == g b1 . open INJ . op b : ­> B . start f g b . – right inverse apply .inj with b1 = b at term apply red at term . Print out notes section slides 90-95, run slide show Try to relate notes to slide show. The first two lines in the above slide contains the two relevant equations from INJ. The final five lines represent the placing of a right inverse equation (f g b) into INJ. Line 3 just opens the module INJ for processing this always has to be done to work on a CafeOBJ module. Line 4 sets up an arbitrary ‘b’ which exists in B (also known as the range or the co-domain). Line 5 represents a right inverse ‘f g b’. It is really lines 6 and 7 that do the work, which can be stated as follows: “The 6 and 7 lines prove that if you add a ‘right inverse’ to INJ the you get a bijective function.” INJ already represents an injective function which by definition already has a ‘left inverse’. The right inverse, ‘f g b’, works line this; it takes a ‘b’ brings it to an ‘a’ in A (Aland) and then takes that ‘a’ back to a ‘b’ in B (Bland). We will show that the starting and finishing ‘b’ are the same.

101 INJ Explained open INJ . op b : ­> B .
eq [linv] : g f a = a ceq [inj]: b0 = b1 if g b0 == g b1 open INJ . op b : ­> B . start f g b . – right inverse apply .inj with b1 = b at term apply red at term . Substitutions are : f g b b f g b b Line 6 sets ‘bo’ equal to the right inverse ‘f g b’. It also sets ‘b1’ to b. Looking at line 6 in more detail. The first bit ‘apply .inj’ says that we are interested in evaluating the equation called [inj]. The second bit ‘with b1 = b’ says let the ‘b’ that we declared in line 4 be substituted into ‘b1’ in the equation [inj]

102 INJ Explained open INJ . op b : ­> B .
eq [linv] : g f a = a ceq [inj]: b0 = b1 if g b0 == g b1 open INJ . op b : ­> B . start f g b . – right inverse apply .inj with b1 = b at term apply red at term . giving : ceq [injNew] : f g b = b if g f g b == g b Line 6 sets ‘bo’ equal to the right inverse ‘f g b’. It also sets ‘b1’ to b. Looking at line 6 in more detail. The first bit ‘apply .inj’ says that we are interested in evaluating the equation called [inj]. The second bit ‘with b1 = b’ says let the ‘b’ that we declared in line 4 be substituted for ‘b1’ in the equation [inj] We will call this new equation constructed in line 6 ‘injNew’.

103 INJ Explained open INJ . op b : ­> B .
eq [linv] : g f a = a ceq [inj]: b0 = b1 if g b0 == g b1 open INJ . op b : ­> B . start f g b . – right inverse apply .inj with b1 = b at term apply red at term . Eval :ceq [injNew] : f g b = b if g f g b == g b evaluate Line 7 reduces (or executes) the term that constructed in line 6 as [injNew]. [injNew] is a conditional equation, which if true will establish ‘f g b = b’ for an arbitrary b. We now focus on the evaluation of [injNew]: Because [injNew] is a ‘conditional equation’ its condition must be check first. Which switches evaluation to the conditional part (g f g b == g b) CafeOBJ finds this to be the similar in form to the equation [linv] on line 1. So CafeOBJ switches focus to [linv].

104 start f g b . – right inverse apply .inj with b1 = b at term
INJ Explained Substitutions g b g b eq [linv] : g f a = a ceq [inj]: b0 = b1 if g b0 == g b1 open INJ . op b : ­> B . start f g b . – right inverse apply .inj with b1 = b at term apply red at term . giving :ceq [injNew] : f g b = b if g f g b == g b Above are the substations for [linvNew].

105 start f g b . – right inverse apply .inj with b1 = b at term
INJ Explained Giving new equation eq [linvNew] : g f g b = g b eq [linv] : g f a = a ceq [inj]: b0 = b1 if g b0 == g b1 open INJ . op b : ­> B . start f g b . – right inverse apply .inj with b1 = b at term apply red at term . giving :ceq [injNew] : f g b = b if g f g b == g b When the substitutions are done [linvNew] can be evaluated eq [linvNew] : g f g b = g b Which gives ‘true’. Which means the condition of [injNew] is also true. Which in turn means that ‘f g b’ does give ‘b’ (same ‘b’) Which means adding a right inverse to INJ makes the fg pair an isomorphism (or bijection or invertible map)

106

107

108 Algebra with two sorts mod! TWOSORTS { [ T V ] op f : T -> T
op g : T -> V -- Specific elements of T ops t1 t2 t3 t4 t5 : -> T -- Specific elements of V ops v1 v2 v3 v4 : -> V -- equations for f eq f(t1) = t3 . eq f(t3) = t4 . eq f(t4) = t5 . -- equations for g eq g(t1) = v1 . eq g(t2) = v2 . eq g(t4) = v4 . eq g(t5) = v4 . } open TWOSORTS **> Some reductions red f(t1) . red f(f(f(t1))) . red g(t4) == g(t5) .


Download ppt "Computing Fundamentals 1 Lecture 8 Functions"

Similar presentations


Ads by Google