DMFP Soln Selected
DMFP Soln Selected
Functional Programming
Thomas VanDrunen
August 13, 2013
1 Set
1.3 Set notation
1.3.2. False. 1.3.4. True. 1.3.6. False. 1.3.8. False. 1.3.10. True.
1
1.12 Recursive functions
1.12.4
fun multiply(x, 0) = 0
| multiply(x, y) = x + multiply(x, y-1);
]1.12.6
fun arithSum(0) = 0
| arithSum(n) = n + arithSum(n-1) ;
2 List
2.1 Lists
2.1.1
2.1.5
2.1.8
[ [ ( |{z}
2.3 , |{z}
5 ), ( |{z}
8.1 , |{z}
6 )], [] ]
|{z}
real int real int (real * int) list
| {z } | {z }
(real * int) (real * int)
| {z }
(real * int) list
| {z }
(real * int) list list
fun count([]) = 0
| count(a::rest) = 1 + count(rest);
2
2.2.5
fun findNth([], n) = ~1
| findNth(a::rest, 0) = a
| findNth(a::rest, n) = findNth(rest, n-1)
2.2.6
fun reverse([]) = []
| reverse(a::rest) = reverse(rest) @ [a];
2.2.10
fun listify([]) = []
| listify(a::rest) = [a] :: listify(rest);
2.2.12
fun splitList([]) = ( [] , [] )
| splitList( (a, b )::rest) =
let
val (x, y) = splitList(rest);
in
( a::x , b::y)
end;
3 Proposition
3.3 Boolean values
3.3.4
fun turnToInts([]) = []
| turnToInts(true::rest) = 1::turnToInts(rest)
| turnToInts(false::rest) = 0::turnToInts(rest);
3.4.6
3
(q ∧ p)∨ ∼ (p∨ ∼ q)
≡ (q ∧ p) ∨ (∼ p ∧ q) DeMorgan’s and double negative
≡ q ∧ (p∨ ∼ p) Distributive (and commutative)
≡ q∧T Negation
≡ q Identity
3.4.7
((q ∧ (p ∧ (p ∨ q))) ∨ (q∧ ∼ p))∧ ∼ q
≡ ((q ∧ p) ∨ (q∧ ∼ p))∧ ∼ q Absorption
≡ (q ∧ (p∨ ∼ p))∧ ∼ q Distributivity
≡ (q ∧ T )∧ ∼ q Negation
≡ q∧ ∼ q Identity
≡ F Negation
4 Proof
4.2 Subset proofs
4.2.1.
Proof. Suppose a ∈ A. [By generalization, a ∈ A or a ∈ B.] By
definition of union, a ∈ A ∪ B. Therefore, by definition of subset, A ⊆
A ∪ B. 2
4.2.8.
Proof (long version). Suppose x ∈ A × (B − C). By definition of
Cartesian product, x = (a, d) for some a ∈ A and d ∈ B − C. By
definition of difference, d ∈ B and d ∈
/ C.
By definition of Cartesian product, (a, d) ∈ A × B. Also by definition of
Cartesian product, this time used negatively, (a, d) ∈
/ A × C.
[That is, we rewrite d ∈/ C. as ∼ (d ∈ C). By generalization, ∼ (d ∈
C ∧ a ∈ A). By definition of Cartesian product, ∼ ((a, d) ∈ A × C). This
can be rewritten as (a, d) ∈
/ A × C.]
By definition of difference, (a, d) ∈ (A × B) − (A × C). By substitution,
x ∈ (A × B) − (A × C). Therefore, by definition of subset, A × (B − C) ⊆
(A × B) − (A × C). 2
Proof (short version). Suppose (a, d) ∈ A × (B − C). By definition
of Cartesian product, a ∈ A and d ∈ B − C.
By definition of difference, d ∈ B and d ∈
/ C. By definition of Cartesian
product, (a, d) ∈ A × B and (a, d) ∈/ A × C.
By definition of difference, (a, d) ∈ (A × B) − (A × C). Therefore, by
definition of subset, A × (B − C) ⊆ (A × B) − (A × C). 2
4
5 Relation
5.3 Image, inverse, and composition
Ex 5.3.5.
Ex 5.3.7.
Ex 5.3.10.
Ex 5.4.7.
5
Proof. Suppose x, y ∈ Z such that (x, y) ∈ R. By how R is definited,
x − y is even. By definition of even, there exists c ∈ Z such that x − y =
2 · c. By arithmetic, y − x = 2 · -c, which is even by definition. (y, x) ∈ R
by how R is defined. Therefore R is symmetric by definition. 2
Ex 5.4.8.
6
Ex 5.4.23.
Ex 5.4.25.
6 Equivalence relations
Ex 5.5.4.
Ex 5.5.6.
7
6.7 Transitive closure
Ex 5.7.2.
7 Function
7.4 Images and inverse images
Ex 7.4.1.
Suppose A, b ⊆ X. Further suppose y ∈ F (A ∩ B).
By definition of image, there exists x ∈ A ∩ B such that f (x) = y. By
definition of intersection, x ∈ A and x ∈ B. By definition of image,
y ∈ F (A) and y ∈ F (B). By definition of intersection again, y ∈
F (A) ∩ F (B).
Therefore, by definition of subset, F (A ∩ B) ⊆ F (A) ∩ F (B). 2
Ex 7.4.3.
Attempted proof. Suppose A, B ∈ X. Further suppose y ∈ F (A − B).
By definition of image, there exists x ∈ A − B such that f (x) = y. By
definition of difference, x ∈ A and x ∈
/ B.
By definition of image, y ∈ F (A). Also by definition of image, used
negatively, y ∈
/ F (B), right?
8
NO! Just because x ∈ / B and f (x) = y does not mean that there isn’t
some other element in B that also hits y. Thus here is our counterex-
ample:
7.4.4.
Suppose A ⊆ B ⊆ X.
First suppose y ∈ F (B). By definition of image, there exists x ∈ B such
that f (x) = y.
Now, either x ∈ A of x ∈/ A by the negation law. [How did we come up
with that? We looked ahead and noticed that we want either y ∈ F (B−A)
or y ∈ F (A) and saw how that would depend on x ∈ A or x ∈ / A.]
Case 1: Suppose x ∈ A. Then, by definition of image, y ∈ F (A). By
definition of union, y ∈ F (B − A) ∪ F (A).
Case 2: Suppose x ∈ / A. Then, by definition of difference, x ∈ B − A,
and by definition of image, y ∈ F (B − A). By definition of union,
y ∈ F (B − A) ∪ F (A).
Either way, y ∈ F (B − A) ∪ F (A) and hence F (B) ⊆ F (B − A) ∪ F (A)
by definition of subset.
Next suppose y ∈ F (B −A)∪F (A). By definition of union, y ∈ F (B −A)
or y ∈ F (A).
Case 1: Suppose y ∈ F (B − A). By definition of image, there exists
x ∈ B − A such that f (x) = y. By definition of difference, x ∈ B. So
y ∈ F (B) by definition of image.
Case 2: Suppose y ∈ F (A). By definition of image, there exists x ∈ A
such that f (x) = y. By definition of subset, x ∈ B. So y ∈ F (B) by
definition of image.
Either way, y ∈ F (B), and hence F (B − A) ∪ F (A) ⊆ F (B) by definition
of subset.
Therefore, by definition of set equality, F (B) = F (B − A) ∪ F (A). 2
Ex 7.4.6.
9
In the proof above, we could also do without the variable y. It’s simply another
name for that same mathematical object that we call f (x):
Ex 7.4.7.
Proof. Suppose A, B ∈ Y .
First suppose x ∈ F −1 (A ∪ B). By definition of inverse image, f (x) ∈
A ∪ B. By definition of union, f (x) ∈ A and f (x) ∈ B.
Case 1: Suppose f (x)inA. By definition of image, x ∈ F −1 (A). By
definition of union, x ∈ F −1 (A) ∪ F −1 (B).
Case 2: Similarly, if f (x) ∈ B, x ∈ F −1 (A) ∪ F −1 (B).
Either way, x ∈ F −1 (A)∪F −1 (B) and so F −1 (A∪B) ⊆ F −1 (A)∪F −1 (B)
by definition of subset.
Next suppose x ∈ F −1 (A) ∪ F −1 (B). By definition of union, x ∈ F −1 (A)
or x ∈ F −1 (B).
Case 1: Suppose x ∈ F −1 (A). By definition of inverse image, f (x) ∈ A.
By definition of union, f (x) ∈ A ∪ B. By definition of inverse image
again, x ∈ F −1 (A ∪ B).
Case 2: Similarly, if x ∈ F −1 (B), x ∈ F −1 (A ∪ B). Either way, x ∈
F −1 (A ∪ B) and so F −1 (A) ∪ F −1 (B) ⊆ F −1 (A ∪ B).
Therefore, by definition of set equality, F −1 (A ∪ B) = F −1 (A) ∪ F −1 (B).
2
10
Ex 7.6.5.
Proof. Suppose A ⊆ Y and f is onto. Further suppose y ∈ A.
By definition of onto, there exists x ∈ X such that f (x) = y. By
definition of inverse image, x ∈ F −1 (A). By definition of image, y ∈
F (F −1 (A)).
Therefore, by definition of subset, A ⊆ F (F −1 (A)). 2
Ex 7.8.4.
Proof. Suppose f : A → B and g : B → C are both onto.
[Now, we want to prove “ontoness.” Of which function? g ◦ f . How
do we prove ontoness? We pick something from the codomain of the
function we’re proving to be onto and show that it is hit. What is the
codomain of g ◦ f ? C.]
Further suppose c ∈ C. [We need to come up with something in the
domain of g ◦ f that hits c. The domain is A. We will use the fact that
f and g are both onto.] By definition of not, there exists b ∈ B such
that g(b) = c. Similarly there exists a ∈ A such that f (a) = b. Now,
Ex 7.8.8.
Proof. Suppose f : A → B, g : A → B, h : B → C, h is onto, and
h ◦ f = h ◦ g.
[This is a proof of function equality. Remember those? The two functions
we’re showing to be equal are f and g. How do we prove two functions
are equal? We pick an element in their (mutual) domain and show that
the two functions map it to the same thing. What is the domain of f
and g? A.]
Suppose a ∈ A. [We need to show f (a) = g(a).] Then
Since h(f (a)) = h(g(a)), we then have that f (a) = g(a) by definition of
onto (because h is onto). Therefore, by definition of function equality,
f = g. 2
11