Changeset 949339b for doc/theses/andrew_beach_MMath/intro.tex
- Timestamp:
- Sep 27, 2021, 2:09:55 PM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
- Children:
- cc287800
- Parents:
- 4e28d2e9 (diff), 056cbdb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/intro.tex
r4e28d2e9 r949339b 25 25 All types of exception handling link a raise with a handler. 26 26 Both operations are usually language primitives, although raises can be 27 treated as a primitivefunction that takes an exception argument.28 Handlers are more complex as they are added to and removed from the stack29 during execution, must specify what they can handle and give the code to27 treated as a function that takes an exception argument. 28 Handlers are more complex, as they are added to and removed from the stack 29 during execution, must specify what they can handle and must give the code to 30 30 handle the exception. 31 31 … … 39 39 it returns control to that function. 40 40 \begin{center} 41 \input{termination} 41 %\input{termination} 42 % 43 %\medskip 44 \input{termhandle.pstex_t} 45 % I hate these diagrams, but I can't access xfig to fix them and they are 46 % better than the alternative. 42 47 \end{center} 43 48 … … 46 51 The handler is run on top of the existing stack, often as a new function or 47 52 closure capturing the context in which the handler was defined. 48 After the handler has finished running it returns control to the function53 After the handler has finished running, it returns control to the function 49 54 that preformed the raise, usually starting after the raise. 50 55 \begin{center} 51 \input{resumption} 56 %\input{resumption} 57 % 58 %\medskip 59 \input{resumhandle.pstex_t} 60 % The other one. 52 61 \end{center} 53 62 54 63 Although a powerful feature, exception handling tends to be complex to set up 55 and expensive to use 64 and expensive to use, 56 65 so it is often limited to unusual or ``exceptional" cases. 57 The classic example is error handling ,exceptions can be used to66 The classic example is error handling; exceptions can be used to 58 67 remove error handling logic from the main execution path, and pay 59 68 most of the cost only when the error actually occurs. … … 63 72 The \CFA EHM implements all of the common exception features (or an 64 73 equivalent) found in most other EHMs and adds some features of its own. 65 The design of all the features had to be adapted to \CFA's feature set as74 The design of all the features had to be adapted to \CFA's feature set, as 66 75 some of the underlying tools used to implement and express exception handling 67 76 in other languages are absent in \CFA. 68 Still the resulting syntax resembles that of other languages:77 Still, the resulting syntax resembles that of other languages: 69 78 \begin{cfa} 70 79 try { … … 88 97 covering both changes to the compiler and the run-time. 89 98 In addition, a suite of test cases and performance benchmarks were created 90 along 99 alongside the implementation. 91 100 The implementation techniques are generally applicable in other programming 92 101 languages and much of the design is as well. … … 100 109 \item Implementing stack unwinding and the \CFA EHM, including updating 101 110 the \CFA compiler and the run-time environment. 102 \item Design ed and implementeda prototype virtual system.111 \item Designing and implementing a prototype virtual system. 103 112 % I think the virtual system and per-call site default handlers are the only 104 113 % "new" features, everything else is a matter of implementation. 105 114 \item Creating tests to check the behaviour of the EHM. 106 \item Creating benchmarks to check the performance sof the EHM,115 \item Creating benchmarks to check the performance of the EHM, 107 116 as compared to other languages. 108 117 \end{enumerate} … … 110 119 The rest of this thesis is organized as follows. 111 120 The current state of exceptions is covered in \autoref{s:background}. 112 The existing state of \CFA is alsocovered in \autoref{c:existing}.121 The existing state of \CFA is covered in \autoref{c:existing}. 113 122 New EHM features are introduced in \autoref{c:features}, 114 123 covering their usage and design. … … 129 138 message as a payload\cite{Ada12}. 130 139 131 The modern flag -ship for termination exceptionsis \Cpp,140 The modern flagship for termination exceptions -- if one exists -- is \Cpp, 132 141 which added them in its first major wave of non-object-orientated features 133 142 in 1990.\cite{CppHistory} … … 137 146 inheriting from 138 147 \code{C++}{std::exception}. 139 Although there is a special catch-all syntax (@catch(...)@) there are no148 Although there is a special catch-all syntax (@catch(...)@), there are no 140 149 operations that can be performed on the caught value, not even type inspection. 141 Instead the base exception-type \code{C++}{std::exception} defines common150 Instead, the base exception-type \code{C++}{std::exception} defines common 142 151 functionality (such as 143 152 the ability to describe the reason the exception was raised) and all … … 148 157 149 158 Java was the next popular language to use exceptions.\cite{Java8} 150 Its exception system largely reflects that of \Cpp, except that requires159 Its exception system largely reflects that of \Cpp, except that it requires 151 160 you throw a child type of \code{Java}{java.lang.Throwable} 152 161 and it uses checked exceptions. 153 162 Checked exceptions are part of a function's interface, 154 163 the exception signature of the function. 155 Every function that could be raised from a function, either directly or164 Every exception that could be raised from a function, either directly or 156 165 because it is not handled from a called function, is given. 157 166 Using this information, it is possible to statically verify if any given 158 exception is handled and guarantee that no exception will go unhandled.167 exception is handled, and guarantee that no exception will go unhandled. 159 168 Making exception information explicit improves clarity and safety, 160 169 but can slow down or restrict programming. … … 169 178 recovery or repair. In theory that could be good enough to properly handle 170 179 the exception, but more often is used to ignore an exception that the 171 programmer does not feel is worth the effort of handling it, for instance if180 programmer does not feel is worth the effort of handling, for instance if 172 181 they do not believe it will ever be raised. 173 If they are incorrect the exception will be silenced, while in a similar182 If they are incorrect, the exception will be silenced, while in a similar 174 183 situation with unchecked exceptions the exception would at least activate 175 the language's unhandled exception code (usually program abort with an184 the language's unhandled exception code (usually, a program abort with an 176 185 error message). 177 186 178 187 %\subsection 179 188 Resumption exceptions are less popular, 180 although resumption is as old as termination; hence, few189 although resumption is as old as termination; that is, few 181 190 programming languages have implemented them. 182 191 % http://bitsavers.informatik.uni-stuttgart.de/pdf/xerox/parc/techReports/ … … 186 195 included in the \Cpp standard. 187 196 % https://en.wikipedia.org/wiki/Exception_handling 188 Since then resumptions have been ignored in main-stream programming languages.197 Since then, resumptions have been ignored in mainstream programming languages. 189 198 However, resumption is being revisited in the context of decades of other 190 199 developments in programming languages. 191 200 While rejecting resumption may have been the right decision in the past, 192 201 the situation has changed since then. 193 Some developments, such as the function programming equivalent to resumptions,202 Some developments, such as the functional programming equivalent to resumptions, 194 203 algebraic effects\cite{Zhang19}, are enjoying success. 195 A complete reexamination of resumption sis beyond this thesis,196 but the re reemergence is enoughto try them in \CFA.204 A complete reexamination of resumption is beyond this thesis, 205 but their reemergence is enough reason to try them in \CFA. 197 206 % Especially considering how much easier they are to implement than 198 207 % termination exceptions and how much Peter likes them. … … 208 217 209 218 %\subsection 210 More recently exceptions seem to be vanishing from newer programming219 More recently, exceptions seem to be vanishing from newer programming 211 220 languages, replaced by ``panic". 212 221 In Rust, a panic is just a program level abort that may be implemented by 213 222 unwinding the stack like in termination exception 214 223 handling.\cite{RustPanicMacro}\cite{RustPanicModule} 215 Go's panic th rough is very similar to a termination, except it only supports224 Go's panic though is very similar to a termination, except it only supports 216 225 a catch-all by calling \code{Go}{recover()}, simplifying the interface at 217 226 the cost of flexibility.\cite{Go:2021} 218 227 219 228 %\subsection 220 Whileexception handling's most common use cases are in error handling,229 As exception handling's most common use cases are in error handling, 221 230 here are some other ways to handle errors with comparisons with exceptions. 222 231 \begin{itemize} … … 233 242 is discarded to avoid this problem. 234 243 Checking error codes also bloats the main execution path, 235 especially if the error is not handled immediately hand has to be passed244 especially if the error is not handled immediately and has to be passed 236 245 through multiple functions before it is addressed. 246 247 Here is an example of the pattern in Bash, where commands can only ``return" 248 numbers and most output is done through streams of text. 249 \begin{lstlisting}[language=bash,escapechar={}] 250 # Immediately after running a command: 251 case $? in 252 0) 253 # Success 254 ;; 255 1) 256 # Error Code 1 257 ;; 258 2|3) 259 # Error Code 2 or Error Code 3 260 ;; 261 # Add more cases as needed. 262 asac 263 \end{lstlisting} 237 264 238 265 \item\emph{Special Return with Global Store}: 239 266 Similar to the error codes pattern but the function itself only returns 240 that there was an error 241 and store the reason for the error in a fixed global location.242 For example many routines in the C standard library will only return some267 that there was an error, 268 and stores the reason for the error in a fixed global location. 269 For example, many routines in the C standard library will only return some 243 270 error value (such as -1 or a null pointer) and the error code is written into 244 271 the standard variable @errno@. 245 272 246 273 This approach avoids the multiple results issue encountered with straight 247 error codes but otherwise has the same disadvantages and more. 274 error codes as only a single error value has to be returned, 275 but otherwise has the same disadvantages and more. 248 276 Every function that reads or writes to the global store must agree on all 249 277 possible errors and managing it becomes more complex with concurrency. 278 279 This example shows some of what has to be done to robustly handle a C 280 standard library function that reports errors this way. 281 \begin{lstlisting}[language=C] 282 // Now a library function can set the error. 283 int handle = open(path_name, flags); 284 if (-1 == handle) { 285 switch (errno) { 286 case ENAMETOOLONG: 287 // path_name is a bad argument. 288 break; 289 case ENFILE: 290 // A system resource has been exausted. 291 break; 292 // And many more... 293 } 294 } 295 \end{lstlisting} 296 % cite open man page? 250 297 251 298 \item\emph{Return Union}: … … 253 300 Success is one tag and the errors are another. 254 301 It is also possible to make each possible error its own tag and carry its own 255 additional information, but the two 302 additional information, but the two-branch format is easy to make generic 256 303 so that one type can be used everywhere in error handling code. 257 304 258 305 This pattern is very popular in any functional or semi-functional language 259 306 with primitive support for tagged unions (or algebraic data types). 307 Return unions can also be expressed as monads (evaluation in a context) 308 and often are in languages with special syntax for monadic evaluation, 309 such as Haskell's \code{haskell}{do} blocks. 310 311 The main advantage is that an arbitrary object can be used to represent an 312 error, so it can include a lot more information than a simple error code. 313 The disadvantages include that the it does have to be checked along the main 314 execution, and if there aren't primitive tagged unions proper, usage can be 315 hard to enforce. 260 316 % We need listing Rust/rust to format code snippets from it. 261 317 % Rust's \code{rust}{Result<T, E>} 262 The main advantage is that an arbitrary object can be used to represent an 263 error so it can include a lot more information than a simple error code. 264 The disadvantages include that the it does have to be checked along the main 265 execution and if there aren't primitive tagged unions proper usage can be 266 hard to enforce. 318 319 This is a simple example of examining the result of a failing function in 320 Haskell, using its \code{haskell}{Either} type. 321 Examining \code{haskell}{error} further would likely involve more matching, 322 but the type of \code{haskell}{error} is user defined so there are no 323 general cases. 324 \begin{lstlisting}[language=haskell] 325 case failingFunction argA argB of 326 Right value -> -- Use the successful computed value. 327 Left error -> -- Handle the produced error. 328 \end{lstlisting} 329 330 Return unions as monads will result in the same code, but can hide most 331 of the work to propagate errors in simple cases. The code to actually handle 332 the errors, or to interact with other monads (a common case in these 333 languages) still has to be written by hand. 334 335 If \code{haskell}{failingFunction} is implemented with two helpers that 336 use the same error type, then it can be implemented with a \code{haskell}{do} 337 block. 338 \begin{lstlisting}[language=haskell,literate={}] 339 failingFunction x y = do 340 z <- helperOne x 341 helperTwo y z 342 \end{lstlisting} 267 343 268 344 \item\emph{Handler Functions}: … … 274 350 variable). 275 351 C++ uses this approach as its fallback system if exception handling fails, 276 such as \snake{std::terminate_handler} and, for a time, 277 \snake{std::unexpected_handler}. 352 such as \snake{std::terminate} and, for a time, 353 \snake{std::unexpected}.\footnote{\snake{std::unexpected} was part of the 354 Dynamic Exception Specification, which has been removed from the standard 355 as of C++20.\cite{CppExceptSpec}} 278 356 279 357 Handler functions work a lot like resumption exceptions, … … 283 361 function calls, but cheaper (constant time) to call, 284 362 they are more suited to more frequent (less exceptional) situations. 363 Although, in \Cpp and other languages that do not have checked exceptions, 364 they can actually be enforced by the type system be more reliable. 365 366 This is a more local example in \Cpp, using a function to provide 367 a default value for a mapping. 368 \begin{lstlisting}[language=C++] 369 ValueT Map::key_or_default(KeyT key, ValueT(*make_default)(KeyT)) { 370 ValueT * value = find_value(key); 371 if (nullptr != value) { 372 return *value; 373 } else { 374 return make_default(key); 375 } 376 } 377 \end{lstlisting} 285 378 \end{itemize} 286 379 … … 288 381 Because of their cost, exceptions are rarely used for hot paths of execution. 289 382 Hence, there is an element of self-fulfilling prophecy as implementation 290 techniques have been focused on making them cheap to set -up,383 techniques have been focused on making them cheap to set up, 291 384 happily making them expensive to use in exchange. 292 385 This difference is less important in higher-level scripting languages, 293 where using exception for other tasks is more common.386 where using exceptions for other tasks is more common. 294 387 An iconic example is Python's 295 \code{Python}{StopIteration}\cite{PythonExceptions} exception that388 \code{Python}{StopIteration}\cite{PythonExceptions} exception, that 296 389 is thrown by an iterator to indicate that it is exhausted. 297 When paired with Python's iterator-based for-loop this will be thrown every390 When paired with Python's iterator-based for-loop, this will be thrown every 298 391 time the end of the loop is reached.\cite{PythonForLoop}
Note: See TracChangeset
for help on using the changeset viewer.