Docsity

Functions-Software Engineering-Lecture 09 Slides-Computer Science, Slides of Software Engineering

Partial preview of the text

Download Functions-Software Engineering-Lecture 09 Slides-Computer Science and more Slides Software Engineering in PDF only on Docsity!

LECTURE 9: FUNCTIONS

Software Engineering

1 Cartesian Products

  • As defined earlier, a set is an unstructured object: the order in which elements occur in a set is not important.
  • However, many objects in formal system specification require some structure or ordering — otherwise how could we have things like Modula-2 RECORDs or C structures?
  • Cartesian products are one way of making objects which have structure.
  • An ordered pair is an example of an n-tuple; in this case n = 2.
  • We list the components of an n-tuple in parentheses.
  • Cartesian products are not restricted to just 2 sets — we can have as many as we wish.
  • Definition: If

S 1 ,... , Sn are arbitrary sets, then S 1 × · · · × Sn is the set of n-tuples over S 1 ,... , Sn: S 1 × · · · × Sn == {(e 1 ,... , en) | e 1 ∈ S 1 ∧ · · · ∧ en ∈ Sn}.

  • Things to note about cartesian products: - #(S 1 × · · · × Sn) = #S 1 ∗ · · · ∗ #Sn - ¬∀S 1 , S 2 : Set • (S 1 × S 2 ) = (S 2 × S 1 ) (i.e., the cartesian product operation does not commute).
  • Finally, let S = {S 1 ,... , Sn} be an indexed set of sets; then the cartesian product over its component sets is often written: ∏ i∈ 1 ..n S or just ∏ S.
  • Cartesian products are sometimes called cross products.
  • If PN is so defined, then we say PN(mike) = 1531, and PN(eric) = 1489.
  • QUESTION: Can we define the set of functions from T 1 to T 2 as the set of ordered pairs T 1 × T 2?
  • ANSWER: No — this doesn’t work: consider the set PN == {(mike, 1531), (mike, 1455)} what is PN(mike) defined to be here?
  • Functions have a uniqueness property: every possible input to the function must have at most one associated output.
  • Note that it is is possible for two inputs to map to the same output.
  • What happens when we try to put a value into a function when there is no corresponding output listed? If PN == {(mike, 1531)} then PN(eric) =? In this case we say that the function is undefined for that value.
  • EXAMPLE. If

PN == {(eric, 1489), (mike, 1531)} then dom PN = {eric, mike} and ran PN = { 1531 , 1489 }

  • Theorems about domain and range:

dom f ≥ # ran f

dom(f ∪ g) = (dom f ) ∪ (dom g) ran(f ∪ g) = (ran f ) ∪ (ran g) dom(f ∩ g) ⊆ (dom f ) ∩ (dom g) ran(f ∩ g) ⊆ (ran f ) ∩ (ran g) dom ∅ = ∅ ran ∅ = ∅

4 Total and Partial Functions

  • The most general kind of functions we consider are partial functions.
  • Definition: If f is a function from T 1 to T 2 , then f is a partial function. The set of all partial functions from T 1 to T 2 is given by the expression T 1 → 7 T 2.
  • Note that: - ∅ ∈ T 1 → 7 T 2 (i.e, the emptyset is a partial function). - if f ∈ T 1 → 7 T 2 then f may be undefined for some value in T 1.
  • QUESTION: What happens if a function takes more than one argument? ANSWER: Then we say that the function takes just one input, from the cartesian product of the input argument types.
  • EXAMPLE. The function plus takes two integers as inputs, adds them together and returns the result; plus : Z × Z → Z
  • The expression

f : D 1 × · · · × Dm → R 1 × · · · × Rn which specifies the type of the function f is called the signature of f.

5 Properties of Functions

5.1 Injections

  • Definition: A function is one-to-one iff every element in the domain maps to a different element in the range. One-to-one functions are also called injections.
  • EXAMPLES. The following is an injection:

{(mike, 1531), (eric, 1489)} whereas the following is not: {(mike, 1531), (eric, 1531)}

  • Finally, if a function is both an injection and a surjection, then it is called a bijection.
  • There are operators for building combinations of types:

constructor returns → 7 partial functions → (total) functions 7→ partial injections → (total) injections →→ 7 partial surjections →→ (total) surjections →→ bijections

6 The Maplet Notation

  • A more convenient way of writing the function {(mike, 1531), (eric, 1489)} is to write {mike 7 → 1531 , eric 7 → 1489 }
  • The symbol 7 → is called the maplet arrow: the expression mike 7 → 1531 is called a maplet.
  • (The maplet notation is just Z syntactic sugar.)

7.1 Domain Restriction

  • Suppose, given our function PN which maps a person in a department to their phone number, we wanted to extract another function which just contained the details of the logic group.
  • Let LG be the set containing names of logic group members.
  • Then the following expression will do the trick: LG  PN
  •  is the domain restriction operator.
  • Definition: Suppose f is a function

f : T 1 → 7 T 2 and S is a set S : IP T 1 then S  f is an expression which returns the function obtained from f by removing from it all maplets x 7 → y such that x 6 ∈ S.

  • EXAMPLE. Let

PN == {mjw 7 → 1531 , en 7 → 1488 , ajt 7 → 1777 } and S 1 == {mike, en} S 2 == {ajt} then S 1  PN = {mjw 7 → 1531 , en 7 → 1488 } S 2  PN = {ajt 7 → 1777 }

  • EXERCISE. Define, by set comprehension, the  operator. S  f == {x : T 1 ; y : T 2 | (x ∈ T 1 ) ∧ (x 7 → y) ∈ f - x 7 → y}
  • Theorems about domain restriction:

dom(S  f ) = S ∩ dom f S  f ⊆ f ∅  f = ∅