Pigeonhole principle, pointers.

main
Joshua Potter 2024-11-25 09:28:11 -07:00
parent a01123d579
commit 677877e4ce
6 changed files with 474 additions and 7 deletions

View File

@ -265,7 +265,7 @@
"algorithms/loop-invariants.md": "cbefc346842c21a6cce5c5edce451eb2",
"algorithms/loop-invariant.md": "3b390e720f3b2a98e611b49a0bb1f5a9",
"algorithms/running-time.md": "5efc0791097d2c996f931c9046c95f65",
"algorithms/order-growth.md": "434073572a080e423c1bf464806c6d65",
"algorithms/order-growth.md": "b50d02ccb785d9e50210f4c29836af70",
"_journal/2024-02-08.md": "19092bdfe378f31e2774f20d6afbfbac",
"algorithms/sorting/selection-sort.md": "73415c44d6f4429f43c366078fd4bf98",
"algorithms/index 1.md": "6fada1f3d5d3af64687719eb465a5b97",
@ -800,7 +800,7 @@
"c17/basic-types.md": "7c6653bf6dc24c2f2aa72fc95c4f7875",
"c17/types/simple.md": "44d56a2998f3bba578f14672c8b0ee3c",
"c17/types/enumerated.md": "e1f70a30677c776b7b44ac3e0ff4e76d",
"c17/types/derived.md": "3776627765547e91086735ab834c2de1",
"c17/types/derived.md": "9d3a800fb7c944a9391f495c6978c99e",
"c17/types/basic.md": "5064e21e683c0218890058882e06b6f3",
"c17/types/index.md": "14b651bcfc8b2d62ffd200a74a9a2a6b",
"_journal/2024-08-25.md": "e73a8edbd027d0f1a39289540eb512f2",
@ -950,10 +950,14 @@
"_journal/2024-11/2024-11-21.md": "951b6034d60a40dbd8201c50abf0dbb9",
"_journal/2024-11/2024-11-20.md": "951b6034d60a40dbd8201c50abf0dbb9",
"_journal/2024-11/2024-11-19.md": "d879f57154cb27cb168eb1f1f430e312",
"set/cardinality.md": "499e758bc0929af06736fa2974aade60",
"set/cardinality.md": "c334d969afe9248c691cb6d3623c958a",
"geometry/area.md": "3c6e53a64ad3150d8f81f6e4a63da61a",
"_journal/2024-11-23.md": "911f82ab8aede5ecdb96493aef64b0b9",
"_journal/2024-11/2024-11-22.md": "51117030e2364dbce3a8d507dead86ae"
"_journal/2024-11/2024-11-22.md": "51117030e2364dbce3a8d507dead86ae",
"_journal/2024-11-24.md": "225661114ad1b605132873e83acdf777",
"_journal/2024-11/2024-11-23.md": "1649e2f050cfb45a0c8cf52be29f625f",
"_journal/2024-11-25.md": "7f06ceccab35f6119f71d2e26174f142",
"_journal/2024-11/2024-11-24.md": "894b021e6335d4e6947448c8d362c083"
},
"fields_dict": {
"Basic": [

View File

@ -0,0 +1,12 @@
---
title: "2024-11-25"
---
- [ ] Anki Flashcards
- [x] KoL
- [ ] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)
* Notes on the [[cardinality#Pigeonhole Principle|pigeonhole principle]].
* Notes on pointers and function pointers.

View File

@ -0,0 +1,11 @@
---
title: "2024-11-24"
---
- [x] Anki Flashcards
- [x] KoL
- [x] OGS
- [ ] Sheet Music (10 min.)
- [ ] Korean (Read 1 Story)
* Notes on the [[derived#NULL|NULL]] macro.

View File

@ -528,6 +528,47 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
<!--ID: 1727957576041-->
END%%
Evaluation of an array `A` returns `&A[0]`, i.e. a [[#Pointers|pointer]] to the first array element. This is called **array decay**.
%%ANKI
Basic
What is the effect of array decay?
Back: Evaluation of an array `A` returns `&A[0]`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953228-->
END%%
%%ANKI
Basic
What name is given to the implicit conversion of an array to a pointer?
Back: Array decay.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953231-->
END%%
%%ANKI
Basic
According to Gustedt, what C feature explains why are there no "array values"?
Back: Array-to-pointer decay.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953234-->
END%%
%%ANKI
Basic
Why can't arrays directly be made arguments to functions?
Back: Because array arguments decay to pointers.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953237-->
END%%
%%ANKI
Cloze
In a function declaration, any array parameter rewrites to {a pointer}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953240-->
END%%
#### Fixed-Length
A fixed-length array (FLA) has a predetermined size. Their stack allocations can be computed at compilation time.
@ -1270,9 +1311,220 @@ Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co
<!--ID: 1732397726962-->
END%%
### NULL
The `NULL` macro refers to a **null pointer constant**, an ICE with value `0` or such an expression cast to type `void*`. The following table lists some valid values `NULL` can take on:
| Expansion | Type |
| -------------------------- | -------------------- |
| `0U` | `unsigned` |
| `0` | `signed` |
| `\0` | `signed` |
| Enum constant of value `0` | `signed` |
| `0UL` | `unsigned long` |
| `0L` | `signed long` |
| `0ULL` | `unsigned long long` |
| `0LL` | `signed long long` |
| `(void*)0` | `void*` |
%%ANKI
Basic
How are null pointer constants defined in terms of ICEs?
Back: As any ICE with value `0` or such an expression cast to type `void*`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644395-->
END%%
%%ANKI
Basic
What *must* the `NULL` macro expand to?
Back: Any null pointer constant.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644434-->
END%%
%%ANKI
Basic
Which of the following members of the list are ICEs?
```c
0U, '\0', 0UL, (void*)0, 5LL
```
Back: `0U`, `\0`, and `0UL`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644440-->
END%%
%%ANKI
Basic
Which of the following members of the list are null pointer constants?
```c
0U, '\0', 0UL, (void*)0, 5LL
```
Back: `0U`, `\0`, `0UL`, and `(void*)0`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644446-->
END%%
%%ANKI
Basic
Which of the following members of the list could `NULL` be identical to?
```c
0U, '\0', 0UL, (void*)0, 5LL
```
Back: `0U`, `\0`, `0UL`, and `(void*)0`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644454-->
END%%
%%ANKI
Basic
Which of the following members of the list are pointer constants?
```c
0U, '\0', 0UL, (void*)0, 5LL
```
Back: Just `(void*)0`.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644461-->
END%%
%%ANKI
Basic
Why does Gustedt discourage use of `NULL`?
Back: The type of value it expands to is implementation-specific.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644469-->
END%%
%%ANKI
Basic
What is wrong with the following invocation?
```c
printf("%d, %p", 1, NULL);
```
Back: `NULL` may not refer to a pointer type.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644475-->
END%%
%%ANKI
Basic
What value must `NULL` have for the following to be correct?
```c
printf("%d, %p", 1, NULL);
```
Back: `(void*)0`
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732456644482-->
END%%
## Functions
A function `f` without a following opening `(` is converted to a pointer to its start. This is called **function decay**.
%%ANKI
Basic
What is the effect of function decay?
Back: Evaluation of a function `f` without a following opening `(` is converted to a pointer to its start.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953243-->
END%%
%%ANKI
Basic
What name is given to the implicit conversion of a function to a pointer?
Back: Function decay.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953247-->
END%%
%%ANKI
Basic
According to Gustedt, what C feature explains why are there no "function values"?
Back: Function-to-pointer decay.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953250-->
END%%
%%ANKI
Basic
Why can't functions directly be made arguments to functions?
Back: Because function arguments decay to pointers.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953255-->
END%%
%%ANKI
Cloze
{1:Function pointers} are to {2:`(...)`} whereas {2:pointers} are to {1:`[...]`}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953260-->
END%%
%%ANKI
Basic
In what order are decays, dereferences, address ofs, and calls performed in the following?
```c
f(3);
```
Back: Decay, call.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953264-->
END%%
%%ANKI
Basic
In what order are decays, dereferences, address ofs, and calls performed in the following?
```c
(&f)(3);
```
Back: Address of, call.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953269-->
END%%
%%ANKI
Basic
In what order are decays, dereferences, address ofs, and calls performed in the following?
```c
(*f)(3);
```
Back: Decay, dereference, decay, call.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953273-->
END%%
%%ANKI
Basic
In what order are decays, dereferences, address ofs, and calls performed in the following?
```c
(*&f)(3);
```
Back: Address of, dereference, decay, call.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953277-->
END%%
%%ANKI
Basic
In what order are decays, dereferences, address ofs, and calls performed in the following?
```c
(&*f)(3);
```
Back: Decay, dereference, address of, call.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953281-->
END%%
%%ANKI
Cloze
{1:Pointers} refer to {2:arrays} whereas {2:function pointers} refer to {1:functions}.
Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
<!--ID: 1732551953285-->
END%%
## Bibliography
* Bryant, Randal E., and David O'Hallaron. *Computer Systems: A Programmer's Perspective*. Third edition, Global edition. Always Learning. Pearson, 2016.
* “ISO: Programming Languages - C,” April 12, 2011, [https://port70.net/~nsz/c/c11/n1570.pdf](https://port70.net/~nsz/c/c11/n1570.pdf).
* Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).
* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.
* Van der Linden, Peter. _Expert C Programming: Deep C Secrets_. Programming Languages / C. Mountain View, Cal.: SunSoft Pr, 1994.

View File

@ -6,7 +6,7 @@ tags:
- set
---
## Overview
## Equinumerosity
We say set $A$ is **equinumerous** to set $B$, written ($A \approx B$) if and only if there exists a [[functions#Injections|one-to-one]] function from $A$ [[functions#Surjections|onto]] $B$.
@ -58,7 +58,51 @@ Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Pre
<!--ID: 1732295060366-->
END%%
## Equivalence Concept
### Power Sets
No set is equinumerous to its [[set/index#Power Set Axiom|power set]]. This is typically shown using a diagonalization argument.
%%ANKI
Basic
What basic set operation produces a new set the original isn't equinumerous to?
Back: The power set operation.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732541309202-->
END%%
%%ANKI
Basic
What kind of argument is typically made to prove no set is equinumerous to its power set?
Back: A diagonalization argument.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732541309208-->
END%%
%%ANKI
Basic
Who is attributed the discovery of the diagonalization argument?
Back: Georg Cantor.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732541309212-->
END%%
%%ANKI
Basic
Let $g \colon A \rightarrow \mathscr{P}A$. Using a diagonalization argument, what set is *not* in $\mathop{\text{ran}}(g)$?
Back: $\{ x \in A \mid x \not\in g(x) \}$
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732541309216-->
END%%
%%ANKI
Basic
Let $g \colon A \rightarrow \mathscr{P}A$. *Why* isn't $B = \{x \in A \mid x \not\in g(x) \}$ in $\mathop{\text{ran}}(g)$?
Back: For all $x \in A$, $x \in B \Leftrightarrow x \not\in g(x)$.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732541309221-->
END%%
### Equivalence Concept
For any sets $A$, $B$, and $C$:
@ -143,6 +187,150 @@ Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Pre
<!--ID: 1732295060403-->
END%%
## Finiteness
A set is **finite** if and only if it is equinumerous to some [[natural-numbers|natural number]]. Otherwise it is **infinite**.
%%ANKI
Basic
How does Enderton define a finite set?
Back: As a set equinumerous to some natural number.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231320-->
END%%
%%ANKI
Basic
How does Enderton define an infinite set?
Back: As a set not equinumerous to any natural number.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231330-->
END%%
%%ANKI
Basic
Is $n \in \omega$ a finite set?
Back: Yes.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231336-->
END%%
%%ANKI
Basic
*Why* isn't $n \in \omega$ a finite set?
Back: N/A. It is.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231342-->
END%%
%%ANKI
Basic
Is $\omega$ a finite set?
Back: No.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231347-->
END%%
%%ANKI
Basic
*Why* isn't $\omega$ a finite set?
Back: There is no natural number equinumerous to $\omega$.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231353-->
END%%
### Pigeonhole Principle
No natural number is equinumerous to a proper subset of itself. More generally, no finite set is equinumerous to a proper subset of itself.
%%ANKI
Basic
How does Enderton state the pigeonhole principle for $\omega$?
Back: No natural number is equinumerous to a proper subset of itself.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231358-->
END%%
%%ANKI
Basic
How does Enderton state the pigeonhole principle for finite sets?
Back: No finite set is equinumerous to a proper subset of itself.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231364-->
END%%
%%ANKI
Basic
Let $m \in n \in \omega$. What principle precludes $m \approx n$?
Back: The pigeonhole principle.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231369-->
END%%
%%ANKI
Basic
Let $S$ be a set and $n \in \omega$ such that $S \approx n$. For $m \in \omega$, when might $S \approx m$?
Back: *Only* if $m = n$.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231374-->
END%%
%%ANKI
Basic
What is the generalization of the pigeonhole principle for $\omega$?
Back: The pigeonhole principle for finite sets.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231379-->
END%%
%%ANKI
Basic
What is the specialization of the pigeonhole principle for finite sets?
Back: The pigeonhole principle for $\omega$.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231385-->
END%%
%%ANKI
Basic
What name is given to the following theorem? $$\text{No finite set is equinumerous to a proper subset of itself.}$$
Back: The pigeonhole principle.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231391-->
END%%
%%ANKI
Basic
Let $S$ be a finite set and $f \colon S \rightarrow S$ be injective. Is $f$ a bijection?
Back: Yes.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231396-->
END%%
%%ANKI
Basic
Let $S$ be a finite set and $f \colon S \rightarrow S$ be injective. *Why* must $f$ be surjective?
Back: Otherwise $f$ is a bijection between $S$ and a proper subset of $S$, a contradiction.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231401-->
END%%
%%ANKI
Basic
Let $S$ be a finite set and $f \colon S \rightarrow S$ be surjective. Is $f$ a bijection?
Back: Yes.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231407-->
END%%
%%ANKI
Basic
Let $S$ be a finite set and $f \colon S \rightarrow S$ be surjective. *Why* must $f$ be injective?
Back: Otherwise $f$ is a bijection between a proper subset of $S$ and $S$, a contradiction.
Reference: Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).
<!--ID: 1732545231412-->
END%%
## Bibliography
* Herbert B. Enderton, *Elements of Set Theory* (New York: Academic Press, 1977).