Outline CPSC 121: Models of Computation 2012 Summer Term 2

snick
∨
snack
Outline
•
•
•
•
•
•
•
CPSC 121: Models of Computation
2012 Summer Term 2
Functions
Steve Wolfman, based on notes by
Patrice Belleville and others
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
2
1
Learning Goals: Pre-Class
Learning Goals: In-Class
By the start of class, you should be able to:
By the end of this unit, you should be able
to:
– Define the terms domain, co-domain, range,
image, and pre-image
– Use appropriate function syntax to relate
these terms (e.g., f : A → B indicates that
f is a function mapping domain A to codomain B).
– Determine whether f : A → B is a function
given a definition for f as an equation or
arrow diagram.
– Define the terms injective (one-to-one),
surjective (onto), bijective (one-to-one
correspondence), and inverse.
– Determine whether a given function is
injective, surjective, and/or bijective.
– Apply your proof skills to proofs about the
properties (e.g., injectiveness, surjectiveness,
bijectiveness, and function-ness) of functions
and their inverses.
3
4
Outline
•
•
•
•
•
•
•
What is this?
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
data1
output
data2
5
control
6
What is this?
What is this?
data1
data1
output
output
data2
data2
7
control
8
control
Functions, Abstraction,
and CPSC 121
What is this?
In the lab, you implemented a multiplexer and then
used it as a piece in larger circuits.
You abstracted from the concrete implementation
to a description of its function:
f(control, data1, data2) = output
= (~control ∧ data1) ∨
(control ∧ data2)
= data1 if control is 0,
data1
but data2 otherwise
output
data2
Computer scientists use many abstraction levels,
with reasoning and execution tools at each level.
In 121, we encounter abstraction levels like: wiring
physical gates, computer-based hardware
design techniques, propositional logic, predicate
logic, sets, D
Functions are a model that let us talk about how
computations work and fit together.
Bonus: functions alone are enough for10
all of
computation, using the “λ calculus”.
9
control
Functions We’ve Used
• Any combinational logic circuit
• Lists, as long as they’re immutable
(unchangeable): A(i) = the ith value in A.
• The Power Set operation (mapping a set
to another set)
• Addition, subtraction, multiplication, ...
Sequential circuits and mutable lists aren’t functions. Why 11not?
(Actually we can model them as functions by making time explicit)
Outline
•
•
•
•
•
•
•
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
12
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
What is a Function?
What is a Function?
Mostly, a function is what you learned it was
all through K-12 mathematics, with strange
vocabulary to make it more interestingD
A function f:A → B maps values from its
domain A to its co-domain B.
Domain
Mostly, a function is what you learned it was
all through K-12 mathematics, with strange
vocabulary to make it more interestingD
A function f:A → B maps values from its
domain A to its co-domain B.
Co-domain
f(x) = x3
f(x) = x mod 4
f(x) = x
13
f(x) = x3
f(x) = x mod 4
f(x) = x
Domain
Co-domain
R or Z or...
R or Z or...
Z or Z+ or...
Z or Z+ or...
R or R+ or...
Z or Z+ or...
14
Look, sets!
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
Plotting Functions
Plotting Functions
f(x) = x3
f(x) = x mod 4
f(x) = x
f(x) = x3
f(x) = x mod 4
f(x) = x
15
Not every function is easy to plot!
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
What is a Function?
What is a Function?
Not every function has to do with numbersD
A function f:A → B maps values from its
domain A to its co-domain B.
Domain
16
Not every function is easy to plot!
Co-domain
f(x) = ~x
f(x,y) = x ∨ y
f(x) = x’s phone #
Not every function has to do with numbersD
A function f:A → B maps values from its
domain A to its co-domain B.
Domain
Co-domain
f(x) = ~x
{T, F}
f(x,y) = x ∨ y
{T,F}×
×{T,F}
f(x) = x’s phone # Set of people?
17
{T, F}
{T, F}
10-dig #s?
18
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
What is a Function?
What is a Function?
A function f:A → B maps values from its
domain A to its co-domain B.
A function f:A → B maps values from its
domain A to its co-domain B.
f(control, data1, data2)
= (~control ∧ data1) ∨
(control ∧ data2)
f(control, data1, data2)
= (~control ∧ data1) ∨
(control ∧ data2)
Domain?
Co-domain?
Let B = {T,F}:
Domain? B × B × B
Co-domain? B
19
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
20
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
What is a Function?
What is a Function?
A function f:A → B maps values from its domain
A to its co-domain B.
Alan
A function f:A → B maps values from its domain
A to its co-domain B.
Alan
Steve
111
Steve
111
Paul
121
Paul
121
Patrice
211
Patrice
211
Karon
Karon
George
George
Domain?
Co-domain?
21
Other examples?
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
Domain? {Alan, Steve, Paul, Patrice, Karon, George}
22
Co-domain? {111, 121, 211}
Other examples?
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
What is a Function?
What is a Function?
A function f:A → B maps values from its
domain A to its co-domain B.
f can’t map one element of its domain to more
than one element of its co-domain:
A function f:A → B maps values from its
domain A to its co-domain B.
f can’t map one element of its domain to more
than one element of its co-domain:
∀x ∈ A, ∀y1,y2 ∈ B,
[(f(x) = y1) ∧ (f(x) = y2)] → (y1 = y2).
∀x ∈ A, ∀y1,y2 ∈ B,
[(f(x) = y1) ∧ (f(x) = y2)] → (y1 = y2).
f
f
Why insist on this?
Why insist on this?
A
B
23
Why not? We give a different
name (relation) to things that do.
A
B
24
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
Not a Function
Not a Function
Why isn’t this a function?
Why isn’t this a function?
There is some
single value x such
that f(x) has more
than one value.
It may also be true
that there is a value
y such that f(y) has
no value (depending
on f’s domain!).
x
25
(The Laffer Curve: a non-functional tax policy.)
26
(The Laffer Curve: a non-functional tax policy.)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
Function Terminology
Function Terminology
A function f:A → B maps values from its
domain A to its co-domain B.
For f to be a function, it must map every
element in its domain:
A function f:A → B maps values from its
domain A to its co-domain B.
For f to be a function, it must map every
element in its domain:
∀x ∈ A, ∃y
∃ ∈ B, f(x) = y.
∀x ∈ A, ∃y
∃ ∈ B, f(x) = y.
f
Why insist on this?
f
Why insist on this?
This proves handy in
some later definitions.
B
A
Warning: some mathematicians
would say that makes f “total”.
B
A
27
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
28
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
Not a Function
Not a Function
Anne
Anne
Alan
Alan
Steve
111
Steve
111
Paul
121
Paul
121
Patrice
211
Patrice
211
Karon
Karon
George
George
29
Foiled by sabbatical.
30
Anne does not map to anything: not a function.
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
Function Terminology
Function Terminology
A function f:A → B maps values from its
domain A to its co-domain B.
f(x) is called the image of x (under f).
x is called the pre-image of f(x) (under f).
f
A function f:A → B maps values from its
domain A to its co-domain B.
The range of f is the set of all images of
elements of f’s domain. In other words:
{ f(x) | x ∈ A }
f
x
x
A
B
y
31
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
B
A
Which is clearer, the
English or the logic?
y
32
CORRESPONDS TO TEXTBOOK READING
(NOT COVERED IN CLASS)
Trying out Terminology
Trying out Terminology
f(x) = x2
What is the image of 16?
What is the range of f?
f(x) = x2
What is the image of 16? f(16) = 162 = 256
What is the range of f? R0 (assuming the domain is R)
f(x)
f(x)
x
x
33
•
•
•
•
•
•
•
34
Outline
Function Properties: Injective
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
A function f:A → B is injective (also oneto-one) if each image is associated with at
most one pre-image:
∀x∈
∈A, ∀y∈
∈A, x≠
≠y → f(x) ≠ f(y).
Injective?
35
Alan
Steve
Paul
Patrice
Karon
George
111
121/202
121/203
121/BCS
211/201
211/202
211/BCS
Injective?
Alan
Steve
Paul
Patrice
Karon
George
111
121
211
36
Trying out Terminology
Trying out Terminology
f(x) = x2
Injective?
What if f:R+ → R+?
f(x) = |x| (the absolute value of x)
Injective?
a. Yes, if f:RR → R+
b. Yes, if f:RR+ → R
c. Yes, for some other domain/co-domain
d. No, not for any domain/co-domain
e. None of these is correct
f(x)
f(x)
x
x
37
Trying out Terminology
38
Trying out Terminology
f:{s|s is a 121 student} → {A+, A, …, D, F}
f(s) = s’s mark in 121
f:{s|s is a 121 student} → {A+, A, …, D, F}
f(s) = s’s mark in 121
Is f injective?
a. Yes
b. No
c. Not enough information
What if we didn’t know what f represented, only its “type”
and the fact that there are lots of 121 students:
f:{s|s is a 121 student} → {A+, A, …, D, F}
Is f injective?
a. Yes
b. No
c. Not enough information
39
40
Nifty Injective Function:
Error-Detecting Codes
Outline
•
•
•
•
•
•
•
41
Error-detecting codes must be injective. Why?
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
42
Function Properties: Surjective
Trying out Terminology
f:A → B is surjective (also onto) if every
element of the co-domain has a pre-image:
∀y ∈ B, ∃x ∈ A, y = f(x).
Surjective?
Alan
Steve
Paul
Patrice
Karon
George
111
121/202
121/203
121/BCS
211/201
211/202
211/BCS
Surjective?
Alan
Steve
Paul
Patrice
Karon
George
f(x) = x2
f:R → R0?
Surjective?
What if f:R → R?
What if f:Z → Z0?
f(x)
121
211
x
44
43
Can we define “surjective” in terms of “range” and “co-domain”?
Trying out Terminology
Trying out Terminology
f(x) = x
Is f surjective?
a. Yes, for f:RR → R0?
b. Yes, for f:RR0 → R?
c. Yes, for f:RR → Z?
d. No, not for any
domain/co-domain
e. None of these
is correct
f:{s|s is a 121 student} → {A+, A, …, D, F}
f(s) = s’s mark in 121
f(x)
Is f surjective?
a. Yes
b. No
c. Not enough information
x
Could we ever know that f was surjective just by knowing f’s
domain and co-domain?
46
45
Nifty Surjective Function:
Cryptographic Hash
Trying out Terminology
Could we ever know that f was surjective just by knowing f’s
domain and co-domain (and their cardinalities)?
a. Yes
b. No
c. Not enough information
47
“Good” cryptographic hashes should be but don’t have 48
to be
surjectiveD but they must not be injective.
Outline
Surjective Functions So Far
Which combinational circuits with one output
are surjective?
a. Every such circuit.
b. Any such circuit that represents a
contingency (neither a tautology nor
contradiction).
c. Only the ones equivalent to an inverter.
d. No such circuit is surjective.
e. None of these is correct.
49
•
•
•
•
•
•
•
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
50
Function Properties: Bijective
Function Properties: Bijective
A function f:A → B is bijective (also oneto-one correspondence) if it is both one-toone and onto (both injective and
surjective).
Every element in the domain has exactly
one unique image. Every element in the
co-domain has exactly one unique preimage.
A function f:A → B is bijective (also oneto-one correspondence) if it is both one-toone and onto.
Bijective?
Alan
Steve
Paul
Patrice
Karon
George
Bijective?
121/202
121/203
121/BCS
211/201
211/202
211/BCS
Alan
Steve
Paul
Patrice
Karon
George
111
121
211
51
52
Nifty Bijective Function:
Encryption/Lossless Compression
Trying out Terminology
f(x) = x2
f:? → ?
Bijective for what
domain/co-domain?
f(x)
x
53
Two sets have the same cardinality if we can put them54in a
bijection. What does that say about lossless compression?
•
•
•
•
•
•
•
Outline
Function Operations: Inverse
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
The inverse of a function f:A → B is
f-1:B → A. f(x) = y ↔ f-1(y) = x.
How can we tell whether f-1 is a function?
(Hint: what would make f-1 not a function?)
55
Trying out Terminology
56
Can we prove it?
Trying out Terminology
f(x) = x2
What’s the inverse of each of these fs?
What’s the inverse of f?
Alan
Steve
Paul
Patrice
Karon
George
121/202
121/203
121/BCS
211/201
211/202
211/BCS
Alan
Steve
Paul
Patrice
Karon
George
f(x)
What should the
domain/co-domain be?
111
121
211
x
58
57
Outline
•
•
•
•
•
•
•
Proving a function injective
Recall: A function f : A → B is injective
exactly when: ∀x∈
∈A, ∀y∈
∈A, x≠
≠y →
f(x) ≠ f(y).
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
A typical approach is to prove the
contrapositive by antecedent assumption:
assume f(x) = f(y) and show that x
= y.
59
60
Worked Problem:
x3 + 5 is injective
Problem: x3 + 5 is injective
Theorem: f(x) = x3+5 is injective, where f : Z→
→Z.
Recall: A function f : A → B is injective exactly
when: ∀x,y ∈ A, x≠
≠y → f(x) ≠ f(y).
We proceed by establishing the contrapositive of this
definition.
Then,
x3 + 5 = y3 + 5
by antecedent assumption
x3 = y3
subtracting 5 from both sides
x=y
cube root of both sides
QED
Theorem: f(x) = x3 + 5 is injective,
where f : Z → Z.
61
Be careful! Would the same proof work for x2 + 5?
62
No: the square root of x2 may be +x or -x!
Proving a function surjective
Problem: w + 2z is surjective
Recall: A function f : A → B is surjective
exactly when:
∀y ∈ B, ∃x ∈ A, y = f(x).
That existential gives us a lot of freedom to
pick a witness!
A typical approach is to “solve for” the
necessary x given a y.
Theorem: f(w,z) = w + 2z is surjective,
where f : (Z × Z) → Z.
64
63
Worked Problem:
w + 2z is surjective
Proving a function bijective
Theorem: f(w,z) = w + 2z is surjective,
where f : (Z × Z) → Z.
Recall: A function f : A → B is surjective
exactly when:
∀y ∈ B, ∃x ∈ A, y = f(x).
WLOG, let y be an arbitrary integer.
Let w = y and z = 0. Then,
w + 2z = y + 2(0) = y.
QED
65
Prove that it’s injective.
Prove that it’s surjective.
Done.
66
Proving a function has an
inverse
An Inverse Proof
Theorem: If f:A → B is bijective, then
f-1 : B → A is a function.
Recall that f-1(f(x)) = x.
Prove that it’s bijective.
Done.
68
67
An Inverse Proof, Worked
Aside: Functions are Just Sets
Theorem: If f : A → B is bijective, then
f-1 : B → A is a function.
We proceed by antecedent assumption.
Assume f : A → B is bijective.
Consider an arbitrary element y of B. Because
f is surjective, there is some x in A such that
f(x) = y. Because f is injective, that is the
only such x.
f-1(y) = x by definition; so, f-1 maps every
element of B to exactly one element of A.
QED
We can define functions in terms of sets and
tuples (and we can define tuples in terms
of sets!).
The function f:A → B is the set
{(x, f(x)) | x ∈ A}.
f is a subset of A × B!
70
69
Outline
•
•
•
•
•
•
•
Learning Goals: In-Class
Prereqs, Learning Goals, and Quiz Notes
Functions as “Computation Abstractions”
Terminology (out-of-class)
Properties: Injective, Surjective, Bijective
Inverse Operation
Function Proofs
Next Lecture Notes
By the end of this unit, you should be able
to:
– Define the terms injective (one-to-one),
surjective (onto), bijective (one-to-one
correspondence), and inverse.
– Determine whether a given function is
injective, surjective, and/or bijective.
– Apply your proof skills to proofs about the
properties (e.g., injectiveness, surjectiveness,
bijectiveness, and function-ness) of functions
and their inverses.
71
72
Learning Goals: “Pre-Class”
(REPEAT)
snick
∨
snack
The pre-class goals are to be able to:
– Trace the operation of a deterministic finitestate automaton (represented as a diagram)
on an input, including indicating whether the
DFA accepts or rejects the input.
– Deduce the language accepted by a simple
DFA after working through multiple example
inputs.
Extra SlidesD
73
Dropped from learning goals and exam.
Dropped from learning goals and exam.
Function Operations: Composition
The composition of two functions f:B → C
and g:A → B written f o g is the
function h:A → C, where:
∀x ∈ A, h(x) = f(g(x)).
Alan
Steve
Paul
Patrice
Karon
George
121/202
121/203
121/BCS
211/201
211/202
211/BCS
121/202
121/203
121/BCS
211/201
211/202
211/BCS
74
A Composition Proof
Theorem: If g:A → B and f:B → C are
functions, then f o g is a function.
Recall that (f o g)(x) = f(g(x)).
111
121
211
What is f? What is g? What is f o g?
75
Dropped from learning goals and exam.
76
Dropped from learning goals and exam.
A Composition Proof, Worked
A Composition Proof, Worked
Theorem: If g:A → B and f:B → C are
functions, then f o g is a function.
We proceed by antecedent assumption.
Assume g and f are functions. We must
show that fog is a function: every element
of the domain of f o g (A), is mapped to
one and only one element of its co-domain
(C).
(Continued on next slide.)
77
WLOG, let x be an arbitrary element of A.
By the definition of f o g, (f o g)(x) =
f(g(x)). We know g(x) is an (i.e., “one
and only one”) element of B, since g is a
function, x ∈ A, and g : A → B.
By similar reasoning, since g(x) ∈ B, we
know f(g(x)) ∈ C because f is a
function.
QED
78