talk.tex 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. % Copyright (C) 2017 Koz Ross <koz.ross@retro-freedom.nz>
  2. % This work is licensed under the Creative Commons Attribution-ShareAlike 4.0
  3. % International License. To view a copy of this license, visit
  4. % http://creativecommons.org/licenses/by-sa/4.0/ or send a letter to Creative
  5. % Commons, PO Box 1866, Mountain View, CA 94042, USA.
  6. \documentclass{beamer}
  7. \usepackage[utf8]{inputenc}
  8. \usecolortheme{seahorse}
  9. \usefonttheme{professionalfonts}
  10. \usepackage{fontspec}
  11. \setmainfont{DejaVu Sans}
  12. \setbeamertemplate{navigation symbols}{}
  13. \usepackage{caption}
  14. \usepackage[font=itshape, begintext=``, endtext='']{quoting}
  15. \title{Type parameterization}
  16. \subtitle{Or: reason \#213 why Java is terrible at everything}
  17. \titlegraphic{\includegraphics[scale=0.5]{logo.eps}}
  18. \author{Koz Ross}
  19. \date{4th May, 2017}
  20. \begin{document}
  21. \begin{frame}[c]
  22. \titlepage{}
  23. \end{frame}
  24. \begin{frame}[c]
  25. \frametitle{Outline}
  26. \tableofcontents
  27. \end{frame}
  28. \section{What is this, and why should I care?}
  29. \begin{frame}[fragile,c]
  30. \frametitle{Example 1}
  31. Consider the following function:\pause{}
  32. \bigskip
  33. \begin{verbatim}
  34. int add () {
  35. return 2 + 3;
  36. }
  37. \end{verbatim}\pause{}
  38. \bigskip
  39. This function is \alert{very} inflexible --- it might as well be a constant!
  40. \end{frame}
  41. \begin{frame}[fragile,c]
  42. \frametitle{Example 1}
  43. We can make the function more flexible (and thus, do more work) by {\em
  44. parameterizing\/} its arguments.\pause{} That way, the {\em user\/} can decide
  45. what numbers it gets to add instead of us:\pause{}
  46. \bigskip
  47. \begin{verbatim}
  48. int add (int x, int y) {
  49. return x + y;
  50. }
  51. \end{verbatim}\pause{}
  52. We call this {\em argument parameterization}, and it is a very useful thing
  53. to have (in fact, programming would be pretty pointless without it).\pause{}
  54. But then, what if our user wants to add two {\tt float}s instead?
  55. \end{frame}
  56. \begin{frame}[fragile,c]
  57. \frametitle{Example 2}
  58. Consider this (partial) definition of a (singly) linked list:\pause{}
  59. \begin{verbatim}
  60. struct node {
  61. int data;
  62. node* next;
  63. };
  64. struct list {
  65. node* first;
  66. };
  67. list* list_new ();
  68. \end{verbatim}\pause{}
  69. What if our user wanted a list of {\tt float}s instead?\pause{} Does it really
  70. matter for list operations what kind of data we're storing?
  71. \end{frame}
  72. \begin{frame}[fragile,c]
  73. \frametitle{The problem}
  74. These are both examples of {\em type restrictions}.\pause{} Normally, these
  75. are a {\em good\/} thing, as we wouldn't want something like this to
  76. compile:\pause{}
  77. \begin{verbatim}
  78. /* this won't compile */
  79. sqrt("10");
  80. /* but will *run* in JavaScript, sigh... */
  81. \end{verbatim}\pause{}
  82. However, in our two examples, type restrictions get in our way.\pause{}
  83. Wouldn't it be nice if we could parameterize over {\em types\/} as well?
  84. \end{frame}
  85. \begin{frame}[fragile,c]
  86. \frametitle{What would this look like?}
  87. \pause{}
  88. \begin{verbatim}
  89. struct node <T> {
  90. T data;
  91. node <T>* next;
  92. };
  93. struct list <T> {
  94. node <T>* first;
  95. };
  96. list <T>* list_new ();
  97. \end{verbatim}\pause{}
  98. So now, if the user wants a list of {\tt int}s, they will write
  99. \verb!list <int>* foo = list_new();!.\linebreak
  100. If they prefer a list of {\tt float}s, they can write
  101. \verb!list <float>* bar = list_new();!.
  102. \end{frame}
  103. \begin{frame}[fragile,c]
  104. \frametitle{Why do we care?}
  105. \pause{}
  106. \begin{itemize}
  107. \item Write the code for a list {\em once}, and it will be able to store
  108. anything a user could want.\pause{}
  109. \item Still have protection against things like this:
  110. \begin{verbatim}
  111. list <int>* foo = list_new();
  112. /* won't compile */
  113. list_insert(foo, "bar");
  114. \end{verbatim}\pause{}
  115. \item Can write very useful things:
  116. \begin{verbatim}
  117. struct pair <T,U> {
  118. T left;
  119. U right;
  120. }; /* a pair of anything! */
  121. \end{verbatim}
  122. \end{itemize}\pause{}
  123. \alert{In short:} Type parameterization makes our code more flexible, more
  124. concise, and generally better.
  125. \end{frame}
  126. \section{How do we implement this?}
  127. \begin{frame}[c,fragile]
  128. \frametitle{Some terminology}
  129. A {\em type parameter\/} is a placeholder type.\pause{} When we mention it in
  130. a definition (like a structure or a function), we call this a {\em
  131. declaration\/}:\pause{}
  132. \begin{verbatim}
  133. struct pair <T,U> { /* declaration */
  134. T left;
  135. U right;
  136. }
  137. \end{verbatim}\pause{}
  138. Later, when we actually use the structure of function, we have to provide an
  139. actual type for the type parameter ({\em instantiation\/}):\pause{}
  140. \begin{verbatim}
  141. pair <float, int> foo; /* instantiation */
  142. \end{verbatim}
  143. \end{frame}
  144. \begin{frame}[fragile,c]
  145. \frametitle{Homogenous translation}
  146. \pause{}
  147. \begin{itemize}
  148. \item Type parameter {\em declarations\/} (and anything using them) get
  149. promoted to some specific type and compiled on the spot.\pause{}
  150. \item Type parameter {\em instantiations\/} are first checked for
  151. consistency; if no problems are found, their types are simply ignored
  152. where appropriate.\pause{}
  153. \end{itemize}
  154. Thus, at {\em runtime}, a \verb!pair <int, float>! is no different to
  155. a \verb!pair <float, char*>! --- might as well be
  156. \verb!pair <wtf, wtf>! for all we care.
  157. \end{frame}
  158. \begin{frame}[fragile,c]
  159. \frametitle{Heterogenous translation}
  160. \pause{}
  161. \begin{itemize}
  162. \item Type parameter {\em declarations\/} (and anything using them) get
  163. turned into a `template', with gaps where the type parameters should
  164. go. Nothing gets compiled yet.\pause{}
  165. \item When the compiler sees a type parameter {\em instantiation}, it
  166. copy-pastes the types into the template, compiles the result, and uses
  167. the result for all identical future cases.\pause{}
  168. \end{itemize}
  169. This, when the compiler sees \verb!pair<int, float>! for the first
  170. time, it will compile a special version for just those types; if it later sees
  171. \verb!pair<float, char*>!, it'll compile a special version for those
  172. types; and so on.
  173. \end{frame}
  174. \section{How good are these?}
  175. \begin{frame}[fragile,c]
  176. \frametitle{Tradeoffs for homogenous translation}
  177. \pause{}
  178. \begin{columns}[t]
  179. \column{.5\textwidth}
  180. \begin{block}{Advantages}
  181. \begin{itemize}
  182. \item Simple:\pause{}
  183. \begin{itemize}
  184. \item Compiles faster\pause{}
  185. \item Simpler compiler logic\pause{}
  186. \end{itemize}
  187. \item Smaller binaries:\pause{}
  188. \begin{itemize}
  189. \item Less space required\pause{}
  190. \item Can use instruction cache effectively\pause{}
  191. \end{itemize}
  192. \end{itemize}
  193. \end{block}
  194. \column{.5\textwidth}
  195. \begin{block}{Disadvantages}
  196. \begin{itemize}
  197. \item No type information at runtime\pause{}
  198. \item Indirection:\pause{}
  199. \begin{itemize}
  200. \item Overhead for a pointer to the data\pause{}
  201. \item Extra pointer chasing
  202. \end{itemize}
  203. \end{itemize}
  204. \end{block}
  205. \end{columns}
  206. \end{frame}
  207. \begin{frame}[fragile,c]
  208. \frametitle{Tradeoffs for heterogenous translation}
  209. \pause{}
  210. \begin{columns}[t]
  211. \column{.5\textwidth}
  212. \begin{block}{Advantages}
  213. \begin{itemize}
  214. \item Type information available at runtime\pause{}
  215. \item No extra indirection\pause{}
  216. \end{itemize}
  217. \end{block}
  218. \column{.5\textwidth}
  219. \begin{block}{Disadvantages}
  220. \begin{itemize}
  221. \item Bigger binaries:\pause{}
  222. \begin{itemize}
  223. \item More space needed\pause{}
  224. \item No hope for instruction cache\pause{}
  225. \end{itemize}
  226. \item Complex:\pause{}
  227. \begin{itemize}
  228. \item Compiles slower\pause{}
  229. \item More complex compiler logic
  230. \end{itemize}
  231. \end{itemize}
  232. \end{block}
  233. \end{columns}
  234. \end{frame}
  235. \begin{frame}[fragile,c]
  236. \frametitle{So which is better?}
  237. \pause{}
  238. \begin{itemize}
  239. \item No single answer --- both have various tradeoffs in general\pause{}
  240. \item Need to be viewed in the context of a particular language\pause{}
  241. \item Let's see some examples!\pause{}
  242. \end{itemize}
  243. We will have rating indicators:\pause{}
  244. \begin{columns}[t]
  245. \column{.5\textwidth}
  246. \begin{center}
  247. \begin{figure}
  248. \includegraphics[scale=0.1]{yes.eps}
  249. \caption*{`This is good (or not a problem)!'}\pause{}
  250. \end{figure}
  251. \end{center}
  252. \column{.5\textwidth}
  253. \begin{center}
  254. \begin{figure}
  255. \includegraphics[scale=0.1]{no.eps}
  256. \caption*{`This is bad (or a {\em real\/} problem)!'}
  257. \end{figure}
  258. \end{center}
  259. \end{columns}
  260. \end{frame}
  261. \begin{frame}[fragile,c]
  262. \frametitle{Homogenous translation done well: C}
  263. \pause{}
  264. Honesty note: C doesn't technically have homogenous translation built-in. We
  265. have to fake it with \verb!void*!.\pause{}
  266. \begin{columns}[t]
  267. \column{.5\textwidth}
  268. \begin{block}{Advantages}
  269. \begin{itemize}
  270. \item Simple\pause{} \includegraphics[scale=0.03]{yes.eps} \pause{}
  271. \item Smaller binaries\pause{} \includegraphics[scale=0.03]{yes.eps}
  272. \pause{}
  273. \end{itemize}
  274. \end{block}
  275. \column{.5\textwidth}
  276. \begin{block}{Disadvantages}
  277. \begin{itemize}
  278. \item No type information at runtime\pause{}
  279. \includegraphics[scale=0.03]{yes.eps}\pause{}
  280. \item Indirection\pause{} \includegraphics[scale=0.03]{no.eps}\pause{}
  281. \end{itemize}
  282. \end{block}
  283. \end{columns}
  284. \begin{center}
  285. \alert{Overall verdict:} $\frac{3}{4}$\pause{} (a B).
  286. \end{center}
  287. \end{frame}
  288. \begin{frame}[fragile,c]
  289. \frametitle{Homogenous translation done badly: Java}
  290. \pause{}
  291. \begin{columns}[t]
  292. \column{.5\textwidth}
  293. \begin{block}{Advantages}
  294. \begin{itemize}
  295. \item Simple\pause{} \includegraphics[scale=0.03]{no.eps} \pause{}
  296. \item Smaller binaries\pause{} \includegraphics[scale=0.03]{no.eps}
  297. \pause{}
  298. \end{itemize}
  299. \end{block}
  300. \column{.5\textwidth}
  301. \begin{block}{Disadvantages}
  302. \begin{itemize}
  303. \item No type information at runtime\pause{}
  304. \includegraphics[scale=0.03]{no.eps}\pause{}
  305. \item Indirection\pause{} \includegraphics[scale=0.03]{yes.eps}\pause{}
  306. \end{itemize}
  307. \end{block}
  308. \end{columns}
  309. \begin{center}
  310. \alert{Overall verdict:} $\frac{1}{4}$\pause{} (drop out of university Java,
  311. you're drunk)
  312. \end{center}
  313. \end{frame}
  314. \begin{frame}[fragile,c]
  315. \frametitle{Heterogenous translation done well: C\#}
  316. \pause{}
  317. \begin{columns}[t]
  318. \column{.5\textwidth}
  319. \begin{block}{Advantages}
  320. \begin{itemize}
  321. \item Type information available at runtime\pause{}
  322. \includegraphics[scale=0.03]{yes.eps}\pause{}
  323. \item No extra indirection\pause{}
  324. \includegraphics[scale=0.03]{yes.eps}\pause{}
  325. \end{itemize}
  326. \end{block}
  327. \column{.5\textwidth}
  328. \begin{block}{Disadvantages}
  329. \begin{itemize}
  330. \item Bigger binaries\pause{}
  331. \includegraphics[scale=0.03]{yes.eps}\pause{}
  332. \item Complex\pause{} \includegraphics[scale=0.03]{yes.eps}\pause{}
  333. \end{itemize}
  334. \end{block}
  335. \end{columns}
  336. \begin{center}
  337. \alert{Overall verdict:} $\frac{4}{4}$\pause{} (I swear that Microsoft
  338. didn't pay me!)
  339. \end{center}
  340. \end{frame}
  341. \begin{frame}[fragile,c]
  342. \frametitle{Heterogenous translation done badly: C++}
  343. \pause{}
  344. \begin{columns}[t]
  345. \column{.5\textwidth}
  346. \begin{block}{Advantages}
  347. \begin{itemize}
  348. \item Type information available at runtime\pause{}
  349. \includegraphics[scale=0.03]{no.eps}\pause{}
  350. \item No extra indirection\pause{}
  351. \includegraphics[scale=0.03]{yes.eps}\pause{}
  352. \end{itemize}
  353. \end{block}
  354. \column{.5\textwidth}
  355. \begin{block}{Disadvantages}
  356. \begin{itemize}
  357. \item Bigger binaries\pause{}
  358. \includegraphics[scale=0.03]{no.eps}\pause{}
  359. \item Complex\pause{} \includegraphics[scale=0.03]{no.eps}\pause{}
  360. \end{itemize}
  361. \end{block}
  362. \end{columns}
  363. \begin{center}
  364. \alert{Overall verdict:} $\frac{1}{4}$\pause{} (bad idea in the 80s, bad
  365. idea now)
  366. \end{center}
  367. \end{frame}
  368. \begin{frame}[c]
  369. \frametitle{Conclusion}
  370. \pause{}
  371. \begin{itemize}
  372. \item Type parametrization is something we want (and language designers have
  373. obliged)\pause{}
  374. \item There's more than one way to do it, and it must be viewed in the
  375. context of the language they inhabit\pause{}
  376. \item More work is still being done on this!\pause{}
  377. \item Important to understand how something works (don't just blindly follow
  378. hype and buzzwords)\pause{}
  379. \end{itemize}
  380. \begin{quoting}
  381. In software development, abstraction is often used as a synonym for
  382. indirection. Not so in mathematics.
  383. \end{quoting}
  384. Susan Potter ({\tt @SusanPotter})
  385. \end{frame}
  386. \section{There's this one thing I don't get\ldots}
  387. \begin{frame}
  388. \frametitle{Question time!}
  389. \begin{center}
  390. \includegraphics[scale=0.5]{question.eps}
  391. \end{center}
  392. \end{frame}
  393. \end{document}