forked from adamschoenemann/dmuac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter8.lhs
840 lines (663 loc) · 24.4 KB
/
Chapter8.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
8. Set Theory
=============
> {-# LANGUAGE FlexibleInstances #-}
> module Chapter8 where
> import Test.QuickCheck
Definition 18 (Subset)
----------------------
Let $A$ and $B$ be sets. Then $A ⊆ B$ if and only if
$$
∀x. (x ∈ A → x ∈ B)
$$
Definition 19 (Set equality)
----------------------------
Let $A$ and $B$ be sets. Then $A = B$ if and only if $A ⊆ B ∧ B ⊆ A$
Definition 20 (Proper subset)
-----------------------------
Let $A$ and $B$ be sets. Then $A ⊂ B$ if and only if $A ⊆ B ∧ A ≠ B$
Definition 21. Union, Intersection and Difference
----------------------------------
$$
\begin{align}
A ∪ B & = \\{x\ |\ x ∈ A ∨ x ∈ B\\} \\\
A ∩ B & = \\{x\ |\ x ∈ A ∧ x ∈ B\\} \\\
A - B & = \\{x\ |\ x ∈ A ∧ x ∉ B\\}
\end{align}
$$
Example 11
----------
Let $A = \\{1,2,3\\}, B = \\{3,4,5\\}$ and $C = \\{4,5,6\\}$. Then
$$
\begin{align}
A ∪ B & = \\{1,2,3,4,5\\} \\\
A ∩ B & = \\{3\\} \\\
A - B & = \\{1,2\\}
\end{align}
$$
$$
\begin{align}
A ∪ C & = \\{1,2,3,4,5,6\\} \\\
A ∩ C & = ∅ \\\
A - C & = \\{1,2,3\\}
\end{align}
$$
Example 12
----------
Let
$$
\begin{align}
I & = \\{...,-2,-1,0,1,2,...\\} \\\
N & = \\{0,1,2,...\\} \\\
H & = \\{-2^15,...,-2,-1,0,1,2,...,2^15 - 1\\} \\\
W & = \\{-2^31,...,-2,-1,0,1,2,...,2^31 - 1\\}
\end{align}
$$
This $I$ is the set of integers, $N$ is the set of natural numbers,
$H$ is the set of integers that are representable on a computer with
a 16-bit word using 2's complement number representation, and $W$
is the set of integers that are representable in a 32-bit word.
We can use these definitions to create new sets. For example,
$I - W$ is the set of integers that are not representable in a word.
Union and Intersection are associative.
Definition 22
-------------
Let $C$ be a non-empty collection (set) of subsets of the universe
$U$. Let $I$ be a non-empty set, and for each $i ∈ I$ let
$A_i ⊆ C$. Then
$$
\begin{align}
\bigcup_{i ∈ I} A_i & = \\{x\ |\ ∃i ∈ I . x ∈ A_i\\}, \\\ \\\
\bigcap_{i ∈ I} A_i & = \\{x\ |\ ∀i ∈ I . x ∈ A_i\\}
\end{align}
$$
Definition 23
-------------
For ant two sets $A$ and $B$, if $A ∩ B = ∅$ then $A$ and $B$ are disjoint sets.
Exercise 1
----------
Given the sets $A = \\{1,2,3,4,5\\}$ and $B = \\{2,4,6\\}$, calculate the
following sets:
(a): $A ∪ (B ∩ c) = A ∪ \\{2,4\\} = A$
(b): $(A ∩ B) ∪ B = \\{2,4\\} ∪ B = B$
(c): $A - B = \\{1,3,5\\}$
(d): $(B - A) ∩ B = \\{6\\} ∩ B = \\{6\\}$
(e): $A ∪ (B - A) = A ∪ \\{6\\} = \\{1,2,3,4,5,6\\}$
8.2.3 Complement and Power
==========================
Definition 24
-------------
Let $U$ be the universe of discourse, and $A$ be a set.
The *complement* of $A$, written $A'$, is the set $U - A$.
Example 13
----------
Given the universe of alphanumeric characters, the complement
of the set of digits is the set of letters.
Example 14
----------
If the universe is $\\{1,2,4,5\\}$, then $\\{1,2\\}'$ = $\\{3,4,5\\}$
A set that contains a lot of elements will have an even larger number of subsets.
These subsets are themselves objects, and it is often useful to define a new
set containg all of them. The set of all subsets of $A$ is called a *powerset*
of $A$. (In contrast, the set of all *elements* of $A$ is just $A$ itself).
Definition 25
-------------
Let $A$ be a set. The *powerset* of $A$, written $P(A)$, is the set of all
subsets of $A$:
$$
P(A) = \\{S\ |\ S ⊆ A\\}
$$
Example 15 (Powersets)
----------------------
- $P(Ø) = \\{\\{\\}\\}$
- $P(\\{a\\}) = \\{ Ø, \\{a\\}\\}$
- $P(\\{a,b\\}) = \\{Ø, \\{a\\}, \\{b\\}, \\{a,b\\}\\}$
- $P(\\{a,b,c\\}) = \\{Ø,\\{a\\},\\{b\\},\\{c\\},\\{a,b\\},\\{a,c\\},\\{b,c\\},\\{a,b,c\\}\\}$
Notice that if $A$ contains $n$ elements, then its powerset contains $2^n$ elements.
These examples show that fact, as $2^0 = 1, 2^1 = 2, 2^2 = 4$ and $2^3 = 8$
8.3 Finite Sets with Equality
=============================
In computing, finite sets with equality are very useful. Finite means no infinite loops,
and equality is important in order to compare and work with elements of sets, and to
determine if a given value is a member of a set.
In Haskell, things that exhibit equality are instances of the `Eq` typeclass.
class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
(/=) = not . (==)
We can implement sets using lists in Haskell, but lists and sets differ
in two important aspecs: 1) lists can contain duplicates and 2) lists
are ordered, while sets are unordered.
8.3.1 Computing with Sets
First an import, to use `sort`
> import Data.List (sort)
Now create the constructor. This constructor should be *private*.
Sets should be constructed from lists using the `fromList` smart constructor
defined below.
> newtype Set a = Set [a]
A Show instance
> instance Show a => Show (Set a) where
> show (Set []) = "{}"
> show (Set xs) = "{" ++ show' xs ++ "}" where
> show' [] = ""
> show' [x] = show x
> show' (x:xs) = show x ++ ", " ++ show' xs
A Eq instance
> instance (Eq a, Ord a) => Eq (Set a) where
> (Set x) == (Set y) = (sort . normalizeSet) x == (sort . normalizeSet) y
An Ord instance
> instance (Ord a) => Ord (Set a) where
> compare (Set xs) (Set ys) = compare xs ys
Define a universe we will use for this chapter's purposes
> _U = Set [-100..100]
An Arbitrary instance for testing (Ints only)
> instance Arbitrary (Set Int) where
> arbitrary = do
> n <- choose (0, 100)
> xs <- vectorOf n (choose (-100,100))
> return $ fromList xs
Some helpers to ensure that any set is de-duplicated.
> normalizeSet :: Eq a => [a] -> [a]
> normalizeSet xs = dedup xs [] where
> dedup [] res = res
> dedup (x:xs) res = if x `elem` res
> then dedup xs res
> else dedup xs (res ++ [x])
> normalForm :: Eq a => [a] -> Bool
> normalForm xs = length (normalizeSet xs) == length xs
And constructors
> fromList :: (Ord a, Eq a) => [a] -> Set a
> fromList xs = Set ((sort . normalizeSet) xs)
And a converter
> toList :: Set a -> [a]
> toList (Set xs) = xs
And some operators
> union :: (Eq a, Ord a) => Set a -> Set a -> Set a
> union (Set xs) (Set ys) = fromList (xs ++ ys)
> (+++) :: (Eq a, Ord a) => Set a -> Set a -> Set a
> (+++) = union
> intersection :: (Eq a, Ord a) => Set a -> Set a -> Set a
> intersection (Set xs) (Set ys) = fromList $ filter pred (xs ++ ys)
> where pred x = x `elem` xs && x `elem` ys
> (***) :: (Eq a, Ord a) => Set a -> Set a -> Set a
> (***) = intersection
> difference :: (Eq a, Ord a) => Set a -> Set a -> Set a
> difference (Set xs) (Set ys) = fromList [x | x <- xs, not $ x `elem` ys]
> (~~~) :: (Eq a, Ord a) => Set a -> Set a -> Set a
> (~~~) = difference
cmpl requires the universe to be known, so is quite restricted
> cmpl :: Set Int -> Set Int
> cmpl x = _U ~~~ x
And some queries
> subset :: (Eq a) => Set a -> Set a -> Bool
> subset (Set xs) (Set ys) = all (`elem` ys) xs
> properSubset :: (Eq a) => Set a -> Set a -> Bool
> properSubset a@(Set xs) b@(Set ys) = not (xs == ys) && subset a b
And the powerset function
> -- first a helper on lists
> powerlist :: [a] -> [[a]]
> powerlist [] = [[]]
> powerlist [x] = [[], [x]]
> powerlist [x,y] = [[], [x], [y], [x, y]]
> powerlist (x:xs) = concat $ map (\z -> [z, x:z]) (powerlist xs)
An example of applying the powerlist function by hand
powerlist [1,2,3]
= concat $ map (\z -> [z, 1:z]) (powerlist [2,3])
= concat $ map (\z -> [z, 1:z]) [[], [2], [3], [2,3]]
= concat $ (\z -> [z, 1:z]) [] : map (\z -> [z, 1:z]) [[2], [3], [2,3]]
= concat $ [[], [1]] : (\z -> [z, 1:z]) [2] : map (\z -> [z, 1:z]) [[3], [2,3]]
= concat $ [[], [1]] : [[2], [1,2]] : [[3], [1,3]] : [[2,3], [1,2,3]] : []
= concat $ [[], [1]] : [[2], [1,2]] : [[3], [1,3]] : [[[2,3], [1,2,3]]]
= concat $ [[], [1]] : [[2], [1,2]] : [[[3], [1,3]], [[2,3], [1,2,3]]]
= concat $ [[], [1]] : [[[2], [1,2]], [[3], [1,3]], [[2,3], [1,2,3]]]
= concat $ [[[], [1]], [[2], [1,2]], [[3], [1,3]], [[2,3], [1,2,3]]]
= [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]]
> -- just uses powerlist under the hood
> powerset :: (Eq a, Ord a) => Set a -> Set (Set a)
> powerset (Set xs) = Set $ map (Set) $ powerlist xs
Finally, the crossproduct of a set
> crossproduct :: Set a -> Set b -> Set (a,b)
> crossproduct (Set a) (Set b) = Set [(x,y) | x <- a, y <- b]
Exercise 2
----------
Work out the values of the following set expressions, and then
check your answer using the Haskell expression that folows.
a. `[1,2,3] +++ [3] == [1,2,3]`
b. `[4,2] +++ [2,4] == [4,2]`
c. `[1,2,3] *** [3] == [3]`
d. `[] *** [1,3,5] == []`
e. `[1,2,3] ~~~ [3] == [1,2]`
f. `[2,3] ~~~ [1,2,3] == []`
g. `[1,2,3] *** [1,2] == [1,2]`
h. `[1,2,3] +++ [4,5,6] == [1,2,3,4,5,6]`
i. `([4,3] ~~~ [4,5]) *** [1,2] == [3] *** [1,2] == []`
j. `([3,2,4] +++ [4,2]) ~~~ [2,3]`
k. `subset [3,4] [4,5,6] == False`
l. `subset [1,3] [4,1,3,6] == True`
m. `subset [] [1,2,3] == True`
n. `setEq [1,2] [2,1] == True`
o. `setEq [3,4,6] [2,3,5] == False`
p. `[1,2,3] ~~~ [1] == [2,3]`
q. `[] ~~~ [1,2] == []`
Exercise 3
----------
The `powerset` function takes a set and returns its power set. Work out the
values of the following expressions:
powerset [3,2,4] == [[], [3], [2], [4], [3,2], [3,4], [2,4], [3,2,4]]
powerset [2] == [[], [2]]
Exercise 4
----------
The *cross product* of two sts $A$ and $B$ is defiend as
$$
A × B = \\{(a,b)\ |\ a ∈ A, b ∈ B \\}
$$
Evaluate these expressions:
crossproduct [1,2,3] ['a', 'b'] = [(1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b')]
crossproduct [1] ['a', 'b'] = [(1, 'a'), (1, 'b')]
Exercise 5
----------
In the following exercise, let `u` be `[1,2,3,4,5,6,7,8,9,10]`,
`a` be `[2,3,4]`, `b` be `[5,6,7]` and `c` be `[1,2]`. Give the elements of each set:
a +++ b == [2,3,4,5,6,7]
u ~~~ a *** (b +++ c) == [1,5,6,7,8,9,10] *** [5,6,7,1,2] = [1,5,6,7]
c ~~~ b == [1,2]
(a +++ b) +++ c == [2,3,4,5,6,7] +++ [1,2] = [1,2,3,4,5,6,7]
u ~~~ a = [1,5,6,7,8,9,10]
u ~~~ (b *** c) = u ~~~ [] = u
Exercise 6
----------
What are the elements of the set $\\{ x + y\ |\ x ∈ \\{1,2,3\\} ∧ y ∈ \\{4,5\\}\\}$
$$
\\{ 1+4,1+5,2+4,2+5,3+4,3+5 \\} = \\{ 5,6,6,7,7,8 \\}
$$
Exercise 7
----------
Write and evaluate a list comprehension that expresses the set
$\\{ x\ |\ x ∈ \\{1,2,3,4,5\\} ∧ x < 0\\}$
[x | x <- [1,2,3,4,5], x < 0] = []
Exercise 8
----------
Write and evaluate a list comprehension that expresses the set
$\\{ x + y\ |\ x ∈ \\{1,2,3\\} ∧ y ∈ \\{4,5\\} \\}$
[x + y | x <- [1,2,3], y <- [4,5]] = [1+4,1+5,2+4,2+5,3+4,3+5] = [5,6,6,7,7,8]
Exercise 9
----------
Write and evaluate a list comprehension that expresses the set
$\\{ x \ |\ x ∈ \\{1,2,3,4,5,6,7,8,9,10\\} ∧ \textit{even } x \\}$
[x | x <- [1,2,3,4,5,6,7,8,9,10], even x] = [2,4,6,8,10]
Exercise 10
-----------
What is the value of each of the following expressions?
subset [1,3,4] [4,3] == False
subset [] [2,3,4] == True
setEq [2,3] [4,5,6] == False
setEq [1,2] [1,2,3] == False
8.4 Set Laws
============
Theorem 68
----------
Let $A$, $B$ and $C$ be sets. If $A ⊆ B$ and $B ⊆ C$, then $A ⊆ C$.
*Proof*
$$
\begin{align}
1. & A ⊆ B & \\{\text{Premise}\\} \\\
2. & x ∈ A → x ∈ B & \\{\text{Def.} ⊆ \\} \\\
3. & B ⊆ C & \\{\text{Premise}\\} \\\
4. & x ∈ B → x ∈ C & \\{\text{Def.} ⊆ \\} \\\
5. & x ∈ A → x ∈ C & \\{\text{Implication}\\} \\\
6. & ∀x. (x ∈ A → x ∈ C) & \\{ ∀I \\} \\\
7. & A ⊆ C & \\{\text{Def.} ⊆ \\}
\end{align}
$$
Exercise 11
-----------
Let $A,B$ and $C$ be sets. Prove that if $A ⊂ B$ and $B ⊂ C$ then $A ⊂ C$
First we prove that $A ⊆ C$ and then that $A ≠ C$
$$
\begin{align}
1. & A ⊆ B ∧ A ≠ B & \\{\text{Premise}\\} \\\
2. & A ⊆ B & \\{ ∧ E_L \\} \\\
3. & B ⊆ C ∧ B ≠ C & \\{\text{Premise}\\} \\\
4. & B ⊆ C & \\{ ∧ E_L \\} \\\
5. & x ∈ A → x ∈ B & \\{\text{Def.} ⊆ \\} \\\
6. & x ∈ B → x ∈ C & \\{\text{Def.} ⊆ \\} \\\
7. & x ∈ A → x ∈ C & \\{\text{Implication}\\} \\\
8. & ∀x. (x ∈ A → x ∈ C) & \\{ ∀I \\} \\\
9. & A ⊆ C & \\{\text{Def.} ⊆ \\}
\end{align}
$$
We know that $A ≠ B ↔ ¬(A ⊆ B ∧ B ⊆ A)$
and $B ≠ C ↔ ¬(B ⊆ C ∧ C ⊆ B)$.
Since we also know that $a ⊂ b → a ⊆ b$, we know that
$A ⊆ B$ and $B ⊆ C$.
Furthermore, we know that $a, ¬(a ∧ b) ⊢ ¬b$, so we can infer
that $¬(C ⊆ B)$ and $¬(B ⊆ A)$.
If $C$ is not subset-or-equal to $B$, and $B$ is not subset-or-equal
to $A$, then $A$ cannot be subset-or-equal-to $C$.
And thus, we know that $A ≠ C$, and $A ⊆ C$, so $A ⊂ C$. Q.E.D.
Exercise 12
-----------
Consider the following two claims. For each one, if it is true
give a proof, but if it is false give a counter-example.
(a). If $A ⊆ B$ and $B ⊆ C$, then $A ⊂ C$.
Not true. Counterexample:
If $A = \\{ 1, 2\\}$ and $B = \\{1,2\\}$ and $C = \\{1,2\\}$,
then $A ⊆ B$ and $B ⊆ C$ but $A ⊄ B$
(b). if $A ⊂ B$ and $B ⊂ C$, then $A ⊆ C$
True. Proof:
$$
\begin{align}
1. & A ⊆ B ∧ A ≠ B & \\{\text{Premise}\\} \\\
2. & A ⊆ B & \\{ ∧ E_L \\} \\\
3. & B ⊆ C ∧ B ≠ C & \\{\text{Premise}\\} \\\
4. & B ⊆ C & \\{ ∧ E_L \\} \\\
5. & x ∈ A → x ∈ B & \\{\text{Def.} ⊆ \\} \\\
6. & x ∈ B → x ∈ C & \\{\text{Def.} ⊆ \\} \\\
7. & x ∈ A → x ∈ C & \\{\text{Implication}\\} \\\
8. & ∀x. (x ∈ A → x ∈ C) & \\{ ∀I \\} \\\
9. & A ⊆ C & \\{\text{Def.} ⊆ \\}
\end{align}
$$
8.4.1 Associative and Commutative Set Operations
================================================
The set union and intersection operators and commutative and associative.
Theorem 69
----------
For all sets $A,B$ and $C$,
1. $A ∪ B = B ∪ A$
2. $A ∩ B = B ∩ A$
3. $A ∪ (B ∪ C) = (A ∪ B) ∪ C$
4. $A ∩ (B ∩ C) = (A ∩ B) ∩ C$
5. $A - B = A ∩ B'$
*Proof*. We prove the second equation. Let $x$ be any element of
$U$, Then each line i nthe proof below is logically equivalent
$↔$ to the following line:
$$
\begin{align}
1. & x ∈ A ∩ B & \\{ Premise \\} \\\
2. & x ∈ A ∧ x ∈ B & \\{ \text{Def.} ∩ \\} \\\
3. & x ∈ B ∧ x ∈ A & \\{ \text{Comm.} ∩ \\} \\\
4. & x ∈ B ∩ A & \\{ \text{Def.} ∩ \\} \\\
5. & ∀x ∈ U. x ∈ A ∩ B ↔ x ∈ B ∩ A & \\{ ∀I \\} \\\
6. & A ∩ B = B ∩ A & \\{ \text{Def. set equality} \\}
\end{align}
$$
8.4.2 Distributive Laws
=======================
The following theorem states that the union and intersection
operators distribute over each other.
Theorem 70
----------
$A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)$
*Proof*. Let $x$ be an arbitrary element of the universe $U$.
Then the following expressions are equivalent ($↔$):
$$
\begin{align}
1. & A ∩ (B ∪ C) & \\{\text{Premise}\\} \\\
2. & x ∈ A ∧ (x ∈ B ∨ x ∈ C) & \\{\text{Def.} ∪, ∩\\} \\\
3. & (x ∈ A ∧ x ∈ B) ∨ (x ∈ A ∨ x ∈ C) & \\{ ∧ \text{ over } ∨ \\} \\\
4. & x ∈ (A ∩ B) ∪ (A ∩ C) & \\{\text{Def.} ∪, ∩\\} \\\
5. & ∀x ∈ U. x ∈ A ∩ (B ∪ C) ↔ x ∈ (A ∩ B) ∪ (A ∩ C) & \\{ ∀ I \\} \\\
6. & A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) & \\{\text{Def.} set equality \\}
\end{align}
$$
Theorem 71
----------
$A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)$
*Proof*.
$$
\begin{align}
1. & A ∪ (B ∩ C) & \\{\text{Premise}\\} \\\
2. & x ∈ A ∨ (x ∈ B ∧ x ∈ C) & \\{\text{Def.} ∩, ∪\\} \\\
3. & (x ∈ A ∨ x ∈ B) ∧ (x ∈ A ∧ x ∈ C) & \\{ ∨ \text{ over } ∧ \\} \\\
4. & x ∈ (A ∪ B) ∩ (A ∪ C) & \\{\text{Def.} ∩, ∪\\} \\\
5. & ∀x ∈ U. x ∈ A ∪ (B ∩ C) ↔ x ∈ (A ∪ B) ∩ (A ∪ C) & \\{ ∀ I \\} \\\
6. & A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) & \\{\text{Def.} set equality \\}
\end{align}
$$
8.4.3 DeMorgan's Laws for Sets
==============================
Theorem 72
----------
Let $A$ and $B$ be arbitrary sets. Then
$$
(A ∪ B)' = A' ∩ B'
$$
and
$$
(A ∩ B)' = A' ∪ B'
$$
*Proof*. We prove that $(A ∪ B)' = A' ∩ B'$. Let $x$ be any element
of $U$. Then the following lines are equivalent:
$$
\begin{align}
1. & x ∈ (A ∪ B)' & \\{\text{Premise} \\} \\\
2. & x ∈ U ∧ ¬(x ∈ A ∪ B) & \\{\text{Def. comp} \\} \\\
3. & x ∈ U ∧ ¬(x ∈ A ∨ x ∈ B) & \\{\text{Def.} ∪ \\} \\\
4. & x ∈ U ∧ (¬(x ∈ A) ∧ ¬(x ∈ B)) & \\{\text{DeMorgan} \\} \\\
5. & x ∈ U ∧ x ∈ U ∧ (¬(x ∈ A) ∧ ¬(x ∈ B)) & \\{\text{Idemp. of ∧}\\} \\\
6. & (x ∈ U ∧ ¬(x ∈ A)) ∧ (x ∈ U ∧ ¬(x ∈ B)) & \\{\text{Comm. of ∧}\\} \\\
7. & x ∈ U - A ∧ x ∈ U - B & \\{\text{Def. of diff.} \\} \\\
8. & x ∈ (U - A) ∩ (U - B) & \\{\text{Def.} ∪ \\} \\\
9. & (x ∈ A' ∧ B') & \\{\text{Def. of comp.} \\} \\\
10. & ∀x. x ∈ (A ∪ B)' ↔ x ∈ (A' ∧ B') & \\{ ∀ \text{ introduction} \\} \\\
11. & (A ∪ B)' = A' ∩ B'
\end{align}
$$
8.7 Review Exercises
====================
Exercise 13
-----------
For the following questions, give a proof using set laws,
or find a counterexample.
(a) $(A' ∪ B)' ∩ C' = A ∩ (B ∪ C)'$
$$
\begin{align}
1. & (A' ∪ B)' ∩ C' & \\\
2. & A'' ∩ B' ∩ C' & \\{ \text{DeMorgan} \\} \\\
3. & A ∩ B' ∩ C' & \\{ \text{Double complement} \\} \\\
4. & A ∩ (B' ∩ C') & \\{ ∩ \text{ assoc} \\} \\\
5. & A ∩ (B ∪ C)' & \\{ \text{DeMorgan}\\}
\end{align}
$$
(b) $A − (B ∪ C)' = A ∩ (B ∪ C)$
$$
\begin{align}
1. & A - (B ∪ C)' & \\\
2. & A ∩ ((B ∪ C)')' & \\{\text{Theorem 69.5}\\} \\\
3. & A ∩ (B ∪ C) & \\{\text{Double complement}\\}
\end{align}
$$
(c) $(A ∩ B) ∪ (A ∩ B') = A$
$$
\begin{align}
1. & (A ∩ B) ∪ (A ∩ B') & \\\
2. & A ∩ (B ∪ B') & \\{ ∩ \text{ over } ∪ \\} \\\
3. & A ∩ U & \\{ a ∪ a' = U \\} \\\
4. & A & \\{ ∩ \text{ identity } \\} \\\
\end{align}
$$
(d) $A ∪ (B − A) = A ∪ B$
$$
\begin{align}
1. & A ∪ (B - A) & \\\
2. & A ∪ (B ∩ A') & \\{\text{Theorem 69.5}\\} \\\
4. & (A ∪ B) ∩ (A ∪ A') & \\{ ∪ \text{ over } ∩\\} \\\
5. & (A ∪ B) ∩ U & \\{a ∪ a' = U \\} \\\
6. & A ∪ B & \\{ ∩ \text{domination} \\}
\end{align}
$$
(e) $A − B = B' − A'$
$$
\begin{align}
1. & A - B & \\\
2. & A ∩ B' & \\{\text{Theorem 69.5}\\} \\\
3. & ((A ∩ B')')' & \\{\text{Double neg} \\} \\\
4. & (A' ∪ B'')' & \\{\text{DeMorgan}\\} \\\
5. & (B'' ∪ A')' & \\{ ∪ \text{ commutes}\\} \\\
6. & B''' ∩ A'' & \\{\text{DeMorgan}\\} \\\
7. & B''' - A' & \\{\text{Theorem 69.5}\\} \\\
8. & B' - A' & \\{\text{Double neg} \\}
\end{align}
$$
(f) $A ∩ (B − C) = (A ∩ B) − (A ∩ C)$
$$
\begin{align}
& A ∩ (B - C) & \\\
= & A ∩ (B ∩ C') & \text{Def. minus} \\\
= & Ø ∪ A ∩ (B ∩ C') & \text{∪ identity} \\\
= & (Ø ∩ B) ∪ (A ∩ B ∩ C') & \text{∩ domination} \\\
= & (A ∩ A' ∩ B) ∪ (A ∩ B ∩ C') & \text{a ∩ a = Ø} \\\
= & ((A ∩ B) ∩ A') ∪ ((A ∩ B) ∩ C') & \text{∩ commute and assoc} \\\
= & (A ∩ B) ∩ (A' ∪ C') & \text{∩ over ∪} \\\
= & (A ∩ B) ∩ (A' ∪ C')'' & \text{Double neg.} \\\
= & (A ∩ B) ∩ (A'' ∩ C'')' & \text{DeMorgan} \\\
= & (A ∩ B) ∩ (A ∩ C)' & \text{Double neg.} \\\
= & (A ∩ B) - (A ∩ C) & \text{Def. minus}
\end{align}
$$
(g) $A − (B ∪ C) = (A − B) ∩ (A − C)$
$$
\begin{align}
& (A - B) ∩ (A - C) & \\\
= & (A ∩ B') ∩ (A ∩ C') & \text{Def. -} \\\
= & (A ∩ A) ∩ (B' ∩ C') & \text{∩ assoc} \\\
= & A ∩ (B' ∩ C') & \text{∩ idempotent} \\\
= & A ∩ (B' ∩ C')'' & \text{Double neg.} \\\
= & A ∩ (B ∪ C)' & \text{DeMorgan} \\\
= & A - (B ∪ C) & \text{Def. -}
\end{align}
$$
(h) $A ∩ (A' ∪ B) = A ∩ B$
$$
\begin{align}
& A ∩ (A' ∪ B) & \\\
= & (A ∩ A') ∪ (A ∩ B) & \text{∩ over ∪} \\\
= & Ø ∪ (A ∪ B) & a ∩ a' = Ø \\\
= & A ∪ B & \text{∪ identity}
\end{align}
$$
(i) $(A − B') ∪ (A − C') = A ∩ (B ∩ C)$
$$
\begin{align}
& (A - B') ∪ (A - C') & \\\
= & (A ∩ B'') ∪ (A - C'') & \text{Def. -} \\\
= & (A ∩ B) ∪ (A ∩ C) & \text{Double neg.}\\\
= & A ∩ (B ∪ C) & \text{∩ over ∪}
\end{align}
$$
Exercise 14
-----------
The function
> smaller :: Ord a => a -> [a] -> Bool
> smaller x [] = True
> smaller x (y:ys) = x < y
takes a value and a list of values and returns `True` if the
value is smaller than the first element in the list. Using this
function, write a function that takes a set and returns its
powerset. Use `foldr`.
> powerlist' :: (Ord a, Eq a) => [a] -> [[a]]
> powerlist' xs = normalizeSet $ foldr g [[]] xs where
> g x acc =
> [x : epset | epset <- acc
> , not (elem x epset) && smaller x epset]
> ++ acc
Exercise 15
-----------
Prove that $(A ∪ B)' = ((A ∪ A') ∩ A') ∩ ((B ∩ B') ∩ B')$
$$
\begin{align}
& (A ∪ B)' & \\\
= & A' ∩ B' & \text{DeMorgan} \\\
= & (U ∩ A') ∩ (U ∩ B') & \text{∩ identity} \\\
= & ((A ∪ A') ∩ A') ∩ ((B ∩ B') ∩ B') & \text{a ∪ a' = U}
\end{align}
$$
Exercise 16
-----------
Using a list comprehension, write a function that takes two sets
and returns `True` if the first is a subset of the other.
> isSubset' xs ys = [x | x <- xs, x `elem` ys] == xs
Exercise 17
-----------
What is wrong with this definition of `diff`, a function that
takes two sets and returns their difference?
diff :: Eq a => [a] -> [a] -> [a]
diff set1 set2 [e | e <- set2, not (elem e set1)]
The arguments are switched - set1 is subtracted from set2
Exercise 18
-----------
What is wrong with this definition of `intersection`,
a function that takes two sets and returns their intersection?
intersection :: [a] -> [a] -> [a]
intersection set1 set2 = [e | e <- set1, e <- set2]
This will duplicate all elements on set 1, (length set2) times.
It should be
intersection :: [a] -> [a] -> [a]
intersection set1 set2 = [e | e <- set1, e `elem` set2]
Exercise 19
-----------
Write a function using a list comprehension that takes
two sets and returns their union.
> union' xs ys = [x | x <- normalizeSet $ xs ++ ys]
Exercise 20
-----------
Is it ever the case that $A ∪ (B - C) = B$
If $A = Ø, C = Ø$ then $A ∪ (B - C) = A ∪ (B - Ø) = A ∪ B = Ø ∪ B = B$
In any case, if $A ⊆ B$ and $B ⊆ C$
Exercise 21
-----------
Give an example in which $(A ∪ C) ∩ (B ∪ C) = Ø$
If $A$ and $B$ are disjoint and $C = Ø$
$A = \\{1\\}, B = \\{2\\}, C = Ø, (A ∪ C) ∩ (B ∪ C) = \\{1\\} ∩ \\{2\\} = Ø$
Exercise 22
-----------
Prove the commutative law of set-intersection, $A ∩ B = B ∩ A$.
$$
\begin{align}
& A ∩ B & \\\
= & x ∈ A ∧ x ∈ B & \text{Def. ∩} \\\
= & x ∈ B ∧ x ∈ A & \text{∧ commutes} \\\
= & ∀x. x ∈ B ∧ x ∈ A & \text{∀ I} \\\
= & B ∩ A & \text{Def. ∩}
\end{align}
$$
Exercise 23
-----------
Express the commutative law of set-intersection in terms of the
set operations and Boolean operations defiend in the `Stdm` module.
> intersectionCommutes a b = a *** b == b *** a
> -- quickCheck (intersectionCommutes :: (Set Int -> Set Int -> Bool))
Exercise 24
-----------
Prove the associative law of set-union, $(A ∪ B) ∪ C = A ∪ (B ∪ C)$
$$
\begin{align}
& (A ∪ B) ∪ C & \\\
= & (x ∈ A ∨ x ∈ B) ∨ x ∈ C & \text{Def. ∪} \\\
= & x ∈ A ∨ (x ∈ B ∨ x ∈ C) & \text{∨ commutes} \\\
= & A ∪ (B ∪ C) & \text{Def. ∪} \\\
\end{align}
$$
Exercise 25
-----------
Prove that the difference between two sets is the intersection of
one with the complement of the other, which can be written as
$A - B = A ∩ B'$.
$$
\begin{align}
& A - B & \\\
= & [x | x ∈ A ∧ x ∉ B] & \text{Def. -} \\\
= & [x | x ∈ A ∧ x ∈ (U - B)] & \text{x ∉ A = x ∈ A'}
= & A ∩ B' & \text{Def. ∩}
\end{align}
$$
Exercise 26
-----------
Prove that union distributs over intersection.
$$
A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
$$
Exercise 27
-----------
Prove DeMorgan's law for the set intersection,
$(A ∩ B)' = A' ∪ B'$