4-functions.ltx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. \ch{Functions}\label{ch:functions}
  2. As promised, this chapter discusses functions.
  3. So, what is a function?
  4. So far, we've been dealing with \xti{values} - like $2$, $\mset{3,2,5}$, and
  5. $90$. They are static. Static things are fine, but they aren't very
  6. interesting. It's much more interesting to examine \xti{changing things} ---
  7. more specifically, things that change \xti{predictably} and \xti{transparently}.
  8. Enter the \termref{function}{d-functions}. It's a mathematical construct. A
  9. function takes some input, and maps it to an output. Functions are sometimes
  10. referred to as \term{mappings} or \term{morphisms}.
  11. Let's look at a simple function, which takes a number and adds $2$ to it
  12. \begin{alignedmath}
  13. f : \Z \to \Z \\
  14. f = \fn{x}{x + 2}
  15. \end{alignedmath}
  16. Pretty simple, right? Okay, so what happens when we send $28$ to $f$?
  17. \begin{alignmath}{rcl}
  18. \evalat{f}{x=28} & = & \fn{x=28}{28 + 2} \\
  19. & = & 30 \\
  20. \end{alignmath}
  21. Alternatively, since it's obvious we're working with $x$:
  22. \begin{alignmath}{rcl}
  23. \evalat{f}{28} & = & 28 + 2 \\
  24. & = & 30 \\
  25. \end{alignmath}
  26. This highlights an important property of functions: \termref{referential
  27. transparency}{d-functions}. If you send a function the same input twice, you
  28. should get the same output both times. That is,
  29. \[ a = b \implies \evalat{f}{a} = \eva{f}{b} \]
  30. Note that the opposite is not always true:
  31. \[ a = b \notimpliedby \evalat{f}{a} = \eva{f}{b} \]
  32. (If that is true, then the function is \term{injective}).
  33. Using the lambda --- $\lambda$ --- is common when I am using a function without
  34. giving it a name. However, usually I will use this notation:
  35. \begin{alignedmath}
  36. f : \Z \to \Z \\
  37. \evalat{f}{x} = x + 2
  38. \end{alignedmath}
  39. The whole $f : \Z \to \Z$ thing should be pretty obvious. If not, it means that
  40. $f$ is a function that takes a member of $\Z$ (the whole numbers, both negative
  41. and positive), and takes it to another member of $\Z$. Other people might use
  42. the notation
  43. \[ \Z \stackrel{f}{\to} \Z \]
  44. That notation is undoubtedly easier to understand. However, as we'll see, that
  45. notation quickly becomes unfeasible.
  46. With this in mind, if $f : A \to B$, then $A$ is the \term{domain} of $f$,
  47. written $\dom{f}$, and $B$ is the \term{codomain} of $f$, written $\codom{f}$.
  48. With regard to the function we were just discussing
  49. \begin{alignedmath}
  50. f : \Z \to \Z \\
  51. \evalat{f}{x} = x + 2
  52. \end{alignedmath}
  53. $\Z$ is both the domain and the codomain. If this is the case, then we say that
  54. $f$ is a \term{closure}.\footnote{There's a programming language called Clojure,
  55. whose name is a pun on this concept.} $f$ is ``closed under $\Z$'', meaning
  56. that things can't use $f$ to escape from $\Z$. $f$ is closed.
  57. If you can't remember all of these terms, don't worry, they are all listed in
  58. \cref{d-functions}.
  59. \ss{Functions with multiple arguments}
  60. Remember my explanation of vectors earlier? If not, vectors are like sets,
  61. but order and repetition matter.
  62. Here's a function that takes two arguments, and adds them to each other.
  63. \begin{alignedmath}
  64. f : \mvec{\Z, \Z} \to \Z \\
  65. \evalat{f}{x,y} = x + y \\
  66. \end{alignedmath}
  67. Pretty easy to understand, right?
  68. If you haven't figured it out from the context, the inputs to the function are
  69. called the \term{arguments}.
  70. Here's a similar function that takes three arguments and adds them to each other
  71. \begin{alignedmath}
  72. f : \mvec{\Z, \Z, \Z} \to \Z \\
  73. \evalat{f}{x,y,z} = x + y + z \\
  74. \end{alignedmath}
  75. You can name your function anything you want, same with the arguments (it
  76. doesn't have to be $f$). It's just a common convention, which you don't have to
  77. follow.
  78. \xtb{What if I want to add a bunch of things together?}
  79. Good idea!
  80. \begin{alignedmath}
  81. f : \mvec{\Z, \Z, \dots, \Z} \to \Z \\
  82. \evalat{f}{x_1, x_2, x_3, \dots, x_n} = x_1 + x_2 + x_3 + \dots + x_n
  83. \end{alignedmath}
  84. That however isn't ideal, because we have no guarantee that the arguments in the
  85. \dots are actually integers. How about we have a \xti{set} of integers, and we
  86. just take the sum? This has the added benefit of less typing
  87. \begin{alignedmath}
  88. f : \evalat{\Set}{\Z} \to \Z \\
  89. \evalat{f}{s} = \sum s
  90. \end{alignedmath}
  91. So,
  92. \begin{alignmath}{rcl}
  93. \evalat{f}{\mset{1,2,3,4,5}} & = & \sum \mset{1,2,3,4,5} \\
  94. & = & 1 + 2 + 3 + 4 + 5\\
  95. & = & 15 \\
  96. \end{alignmath}
  97. \ss{Eta-reductions}
  98. \nocite{eta-conversion}
  99. Mathematicians like to make themselves look smart. One such way is to invent
  100. fancy terms for simple things. One such term is the $\eta$-reduction.
  101. Let's look at that function we just had
  102. \begin{alignedmath}
  103. f : \evalat{\Set}{\Z} \to \Z \\
  104. \evalat{f}{s} = \sum s
  105. \end{alignedmath}
  106. Notice that we are repeating $s$ on both sides of the equation. It would seem
  107. much simpler, and just as clear, to write:
  108. \begin{alignedmath}
  109. f : \evalat{\Set}{\Z} \to \Z \\
  110. f = \sum
  111. \end{alignedmath}
  112. That's all an $\n$-reduction is: if you see an extraneous argument, you remove
  113. it to make things simpler. As long as we have the signature --- the
  114. $f : \evalat{\Set}{\Z} \to \Z$ thing -- it's pretty clear what $f$ does. This is
  115. a prime example of mathematicians being both lazy and pretentious at the same
  116. time: a practice designed to allow us to be lazier, to which mathematicians have
  117. assigned a ridiculous name to make it sound hard.
  118. \xtb{What the hell is $\n$?}
  119. $\n$ is the Greek letter eta; it's pronounced ``eight-uh''.
  120. The ancient Greeks were too dumb to comprehend the concept of ``eight''. Every
  121. time someone brought it up, they said ``uh'' immediately thereafter. The sound
  122. ``eight-uh'' became so common that they decided to make it a letter.
  123. The Greeks' poor comprehension of simple mathematics remains to this day, and
  124. is largely the reason for their current financial
  125. crisis.\cite{w-greek-finance}
  126. If you ever take a physics course, you will undoubtedly notice that Greek
  127. letters are used frequently in physics. This is the physicists way of subtly
  128. hinting that they actually have no idea what they are talking about, and
  129. pleading for help from the mathematicians.
  130. \ss{Other lambda calculi}
  131. This entire idea where you take simple concepts and make them sound really fancy
  132. is called \termref{$\lambda$ calculus}{d-lambda-calculus}. If you hear people
  133. talk about ``calculus'', they are talking about something else, not this. Nobody
  134. is pretentious enough to actually talk about $\lambda$ calculus.
  135. Anyway, here's a brief summary of $\lambda$ calculus. You can find this in
  136. \cref{d-lambda-calculus}, too. You might want to brush up on your Greek
  137. alphabet. I have a nice table of Greek letters in \cref{d-greek-alphabet}.
  138. \begin{description}
  139. \item[$\lambda$ abstraction] A way to write a function: $\ld{x,y} x + y$
  140. \item[$\alpha$ conversion] Changing the names of the arguments. For instance,
  141. you can write the above function as
  142. \[ \ld{a,b} a + b \]
  143. \item[$\beta$ reduction] Partially calculating a result. For instance
  144. \[ \ld{2,y} 2 + y \]
  145. Can be $\beta$ reduced to
  146. \[ \ld{y} 2 + y \]
  147. \item[$\eta$ conversion] Removing or adding extraneous free arguments. The last
  148. function
  149. \[ \ld{2,y} 2 + y \]
  150. Can be $\eta$ \xti{reduced} to
  151. \[ 2 + \]
  152. Which could then be $\eta$ \xti{abstracted} to
  153. \[ \ld{2,\kappa} 2 + \kappa \]
  154. \end{description}
  155. \s{Currying}
  156. We sort of got side-tracked by toying around with sets and making fun of
  157. physicists. Hopefully that introduction introduced you to the basic concept of a
  158. function, and let you know that they can take multiple arguments
  159. Let's look at that function again:
  160. \begin{alignedmath}
  161. f : \mvec{\Z, \Z, \Z} \to \Z \\
  162. \evalat{f}{x,y,z} = x + y + z \\
  163. \end{alignedmath}
  164. What if you wanted to bind $x = 3$, but leave the rest ``free''?
  165. \begin{alignedmath}
  166. f : \mvec{\Z, \Z, \Z} \to \Z \\
  167. \evalat{f}{x=3,y,z} = 3 + y + z \\
  168. \end{alignedmath}
  169. Okay, cool. We now have another function:
  170. \begin{alignedmath}
  171. \evalat{f}{3} : \mvec{\Z, \Z} \to \Z \\
  172. \evalat{f}{3,y,z} = 3 + y + z \\
  173. \end{alignedmath}
  174. So, actually, instead of needing 3 integers to do its job, $f$ only needed
  175. one. However, instead of spitting out another integer, it spit out a
  176. function. So, we could write $f$'s signature as:
  177. \begin{alignedmath}
  178. f : \Z \to \parens{\mvec{\Z, \Z} \to \Z }\\
  179. \evalat{f}{x,y,z} = x + y + z \\
  180. \end{alignedmath}
  181. Okay, that's sort of weird and unintuitive. Let's try writing $f$ differently:
  182. \begin{alignedmath}
  183. f : \Z \to \parens{\mvec{\Z, \Z} \to \Z }\\
  184. f = \ld{x}\parens{\ld{y,z} x + y + z} \\
  185. \end{alignedmath}
  186. Let's look at the second half of that:
  187. \begin{alignedmath}
  188. \ld{y,z} x + y + z : \mvec{\Z,\Z} \to \Z \\
  189. \end{alignedmath}
  190. (This assumes that we know what $x$ is)
  191. Let's try splitting this up again:
  192. \begin{alignedmath}
  193. \ld{y}\parens{\ld{z} x + y + z} : \Z \to \parens{\Z \to \Z} \\
  194. \end{alignedmath}
  195. You give this function a value for $y$, and instead of giving you a value, it
  196. gives you another function, hence the signature \nm{\Z \to \parens{\Z \to \Z}}.
  197. Let's plug this back into $f$:
  198. \begin{alignedmath}
  199. f : \Z \to \parens{\Z \to \parens{\Z \to \Z}}\\
  200. f = \ld{x}\parens{\ld{y}\parens{\ld{z} x + y + z} } \\
  201. \end{alignedmath}
  202. So, instead of $f$ taking three integers, it now only takes one, but spits out a
  203. function, which in turn spits out a function, which spits out an integer.
  204. This idea of making a function into a chain of functions is called
  205. ``Currying''.\cite{w-currying} It's named after a dead mathematician named
  206. Haskell Curry (ca. 1900-1982), who developed the technique. The programming
  207. language Haskell is also named after Mr. Curry.
  208. Getting back to that function, those parentheses are somewhat burdensome, let's
  209. get rid of them
  210. \begin{alignmath}{rcl}
  211. f & : & \Z \to \Z \to \Z \to \Z\\
  212. f & = & \ld{x} \ld{y} \ld{z} x + y + z \\
  213. \evalat{f}{x,y,z} & = & x + y + z \\
  214. \end{alignmath}
  215. That's much easier to read. It should be understood that the parentheses are
  216. right-associative: the parentheses ``associate'' rightward --- i.e. it's
  217. $a \to \parens{b \to \parens{c \to d}}$, not
  218. $\parens{\parens{a \to b} \to c} \to d$.\cite{w-operator-associativity}
  219. That's Currying for you.
  220. \ss{Piecewise functions}
  221. \label{s:piecewise}
  222. As a random aside, I'm going to introduce you to the \term{piecewise
  223. function}. It's a function whose definition changes based on the input.
  224. \begin{rclmath}
  225. q & : & \N \to \Z \\
  226. \eva{q}{x} & \ce &
  227. \left\{
  228. \begin{array}{rcc}
  229. \text{$x$ is even} & \to & \frac{x}{2} \\
  230. \text{$x$ is odd} & \to & \ngp{\frac{x+1}{2}} \\
  231. \end{array}
  232. \right.
  233. \end{rclmath}
  234. Let's look at $\eva{q}{0}$: $0$ is even, so $\eva{q}{0} = \frac{0}{2} = 0$.
  235. Let's make a table:
  236. \begin{displaymath}
  237. \begin{tabu}{|c|c|c|} \hline
  238. x & \eva{q}{x} & \text{$\eva{q}{x}$ reduced} \\ \hline
  239. 0 & \fracil{0}{2} & 0 \\
  240. 1 & \ngfracilpf{1+1}{2} & \ng 1 \\
  241. 2 & \fracil{2}{2} & 1 \\
  242. 3 & \ngfracilpf{3+1}{2} & \ng 2 \\
  243. 4 & \fracil{4}{2} & 2 \\
  244. 5 & \ngfracilpf{5+1}{2} & \ng 3 \\
  245. 6 & \fracil{6}{2} & 3 \\
  246. 7 & \ngfracilpf{7+1}{2} & \ng 4 \\
  247. 8 & \fracil{8}{2} & 4 \\
  248. \hline
  249. \end{tabu}
  250. \end{displaymath}
  251. Hopefully you get this. It's pretty simple.
  252. \ss{Vocabulary}
  253. I've been sort of dropping these vocabulary terms throughout the beginning of
  254. the chapter. That said, I'll list them here, so you know where they
  255. are. (They're also in \cref{d-functions}).
  256. \begin{enumerate}
  257. \item All functions are \term{transparent} ---
  258. $a = b \implies \evalat{f}{a} = \evalat{f}{b}$
  259. \item If $f : A \to B$, then $A$ is the \term{domain} of $f$ and $B$ is the
  260. \term{codomain} of $f$.
  261. \item If $f : A \to B$, and there are no two distinct elements of $A$ that map
  262. to the same thing in $B$, then $f$ is \term{injective}.
  263. \begin{alignedmath}
  264. f : A \to B \\
  265. \notexists \mvec{a,b} \semic a,b \in A \land a \ne b \land \evalat{f}{a} = \evalat{f}{b}
  266. \iff \text{$f$ is injective}
  267. \end{alignedmath}
  268. \item If $f : A \to B$, then the elements in $B$ that can be expressed as
  269. $\evalat{f}{x}\semic x \in A$ form the \term{image}.
  270. \begin{alignedmath}
  271. f : A \to B \\
  272. \im{f} = \mset{\evalat{f}{x} \in B\semic x \in A} \\
  273. \end{alignedmath}
  274. \item If the image of a function is equal to its codomain, then the function is
  275. \term{surjective}.
  276. \begin{alignedmath}
  277. f : A \to B \\
  278. B = \mset{\evalat{f}{x} \in B\semic x \in A} \iff \text{$f$ is surjective} \\
  279. \end{alignedmath}
  280. \item If a function is both injective and surjective, then it is
  281. \term{bijective}.
  282. \item Some functions have \term{inverses}. That is, if
  283. \[ f : A \to B \]
  284. \begin{alignedmath}
  285. \arc{f} : B \to A \\
  286. \arc{f, x} = x \sfall x \in A
  287. \end{alignedmath}
  288. Remember that, because of currying, $\arc{f,x} = \evalat{\arc{f}}{x}$. That is:
  289. \begin{alignmath}{rcl}
  290. f & : & A \to B \\
  291. \mathrm{arc} & : & \parens{A \to B} \to B \to A \\
  292. \arc{f} & : & B \to A \\
  293. \end{alignmath}
  294. If a function has an inverse, it is said to be \term{invertible}.
  295. \item If a function is invertible, then the image of the inverse is called the
  296. \term{preimage}.
  297. \end{enumerate}
  298. \begin{ExcList}
  299. \Exercise{I knew you were going to just gloss over those, so I made a really
  300. hard (i.e. fun) problem: prove that a function is invertible if (and only
  301. if) it is bijective. This is a very difficult proof, but you really need to
  302. understand it.}
  303. \Answer{ Let's look at $f : A \to B$. If $f$ is surjective, then $B = \im{f}$,
  304. so we can write
  305. \[ f : A \to \im{f} \]
  306. In other words
  307. \[ f : \dom{f} \to \im{f} \]
  308. It must be true that $f$ is a surjection for $f$ to be invertible. Else
  309. there would be elements in the codomain of $f$ that were not in the domain
  310. of $\arc{f}$.
  311. We've established
  312. \begin{alignedmath}
  313. \arc{f} : \im{f} \to \dom{f}
  314. \end{alignedmath}
  315. Let's assume $f$ is invertible. Then $\dom{f} = \preim{f}$. Thus
  316. \begin{alignedmath}
  317. \arc{f} : \im{f} \to \dom{f}
  318. \end{alignedmath}
  319. For $\arc{f}$ to be a function --- i.e. for $f$ to be invertible, then it
  320. must be true that
  321. \begin{alignedmath}
  322. \notexists a,b \in \im{f} \semic a = b \semic \arc{f,a} \ne \arc{f,b}
  323. \end{alignedmath}
  324. If we flip this around
  325. \begin{alignedmath}
  326. \notexists a,b \in \preim{f} \semic a \ne b \semic \evalat{f}{a} = \evalat{f}{b}
  327. \end{alignedmath}
  328. That is, the definition of injectivity. Thus we have proven
  329. \[
  330. \text{$f$ is invertible}
  331. \iff
  332. \parens{\text{$f$ is an injection}}
  333. \land
  334. \parens{\text{$f$ is a surjection}}
  335. \iff
  336. \text{$f$ is a bijection}
  337. \]
  338. }
  339. \end{ExcList}