1 / 108

Computing Fundamentals 1 Lecture 8 Functions

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.

hei
Download Presentation

Computing Fundamentals 1 Lecture 8 Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computing Fundamentals 1Lecture 8Functions 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

  2. 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 AlandAtoB 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 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 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 Aland Bland

  6. IfAtoB 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 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 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); } 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 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 applicationg.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.

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

  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.

  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 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. Not in range of f. 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 .} b4 a1 a2 a3 f b1 b2 b3 A B dom f ran f

  28. a1 a2 a3 f b1 b2 Surjective Function • 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 b1 b4 b2 b3 f A B

  29. 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 B  C   Function f not injective The inverse (f-1) is not determinate

  30. 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).     B C B C  ? Inverse not surjective (onto) Function not total

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

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

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

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

  35. Functions • The partial function ftakes 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’.

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

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

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

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

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

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

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

  43. Composition of Functions1 g o f, the composition of f and g

  44. Composition of Functions1 • The functions f:X→Y and g:Y→Z can be composed by first applying fto 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.

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

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

  47. A function is a relation • The function f:XY ‘the function f, from X to Y’ • Is equivalent to relation XfYwith the restriction that for each xin 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

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

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

More Related