# Changeset 056cbdb for doc/theses/andrew_beach_MMath/intro.tex

Ignore:
Timestamp:
Sep 27, 2021, 10:16:57 AM (2 months ago)
Branches:
master
Children:
172a88d, 1db1454, 7ce2483, 949339b, a51a02d
Parents:
716b62c (diff), 86bd8538 (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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

### Legend:

Unmodified
 r716b62c it returns control to that function. \begin{center} \input{termination} \medskip %\input{termination} % %\medskip \input{termhandle.pstex_t} % I hate these diagrams, but I can't access xfig to fix them and they are % better than the alternative. \end{center} \todo*{Can I make the new diagrams fit the old style?} Resumption exception handling searches the stack for a handler and then calls that preformed the raise, usually starting after the raise. \begin{center} \input{resumption} \medskip %\input{resumption} % %\medskip \input{resumhandle.pstex_t} % The other one. \end{center} message as a payload\cite{Ada12}. The modern flag-ship for termination exceptions is \Cpp, The modern flagship for termination exceptions -- if one exists -- is \Cpp, which added them in its first major wave of non-object-orientated features in 1990.\cite{CppHistory} included in the \Cpp standard. % https://en.wikipedia.org/wiki/Exception_handling Since then, resumptions have been ignored in main-stream programming languages. Since then, resumptions have been ignored in mainstream programming languages. However, resumption is being revisited in the context of decades of other developments in programming languages. %\subsection More recently exceptions, seem to be vanishing from newer programming More recently, exceptions seem to be vanishing from newer programming languages, replaced by panic". In Rust, a panic is just a program level abort that may be implemented by unwinding the stack like in termination exception handling.\cite{RustPanicMacro}\cite{RustPanicModule} Go's panic through is very similar to a termination, except it only supports Go's panic though is very similar to a termination, except it only supports a catch-all by calling \code{Go}{recover()}, simplifying the interface at the cost of flexibility.\cite{Go:2021} through multiple functions before it is addressed. Here is an example of the pattern in Bash, where commands can only  return" numbers and most output is done through streams of text. \begin{lstlisting}[language=bash,escapechar={}] # Immediately after running a command: case \$? in 0) # Success ;; 1) # Error Code 1 ;; 2|3) # Error Code 2 or Error Code 3 ;; # Add more cases as needed. asac \end{lstlisting} \item\emph{Special Return with Global Store}: Similar to the error codes pattern but the function itself only returns This approach avoids the multiple results issue encountered with straight error codes but otherwise has the same disadvantages and more. error codes as only a single error value has to be returned, but otherwise has the same disadvantages and more. Every function that reads or writes to the global store must agree on all possible errors and managing it becomes more complex with concurrency. This example shows some of what has to be done to robustly handle a C standard library function that reports errors this way. \begin{lstlisting}[language=C] // Now a library function can set the error. int handle = open(path_name, flags); if (-1 == handle) { switch (errno) { case ENAMETOOLONG: // path_name is a bad argument. break; case ENFILE: // A system resource has been exausted. break; // And many more... } } \end{lstlisting} % cite open man page? \item\emph{Return Union}: This pattern is very popular in any functional or semi-functional language with primitive support for tagged unions (or algebraic data types). % We need listing Rust/rust to format code snippets from it. % Rust's \code{rust}{Result} Return unions can also be expressed as monads (evaluation in a context) and often are in languages with special syntax for monadic evaluation, such as Haskell's \code{haskell}{do} blocks. The main advantage is that an arbitrary object can be used to represent an error, so it can include a lot more information than a simple error code. execution, and if there aren't primitive tagged unions proper, usage can be hard to enforce. % We need listing Rust/rust to format code snippets from it. % Rust's \code{rust}{Result} This is a simple example of examining the result of a failing function in Haskell, using its \code{haskell}{Either} type. Examining \code{haskell}{error} further would likely involve more matching, but the type of \code{haskell}{error} is user defined so there are no general cases. \begin{lstlisting}[language=haskell] case failingFunction argA argB of Right value -> -- Use the successful computed value. Left error -> -- Handle the produced error. \end{lstlisting} Return unions as monads will result in the same code, but can hide most of the work to propagate errors in simple cases. The code to actually handle the errors, or to interact with other monads (a common case in these languages) still has to be written by hand. If \code{haskell}{failingFunction} is implemented with two helpers that use the same error type, then it can be implemented with a \code{haskell}{do} block. \begin{lstlisting}[language=haskell,literate={}] failingFunction x y = do z <- helperOne x helperTwo y z \end{lstlisting} \item\emph{Handler Functions}: function calls, but cheaper (constant time) to call, they are more suited to more frequent (less exceptional) situations. Although, in \Cpp and other languages that do not have checked exceptions, they can actually be enforced by the type system be more reliable. This is a more local example in \Cpp, using a function to provide a default value for a mapping. \begin{lstlisting}[language=C++] ValueT Map::key_or_default(KeyT key, ValueT(*make_default)(KeyT)) { ValueT * value = find_value(key); if (nullptr != value) { return *value; } else { return make_default(key); } } \end{lstlisting} \end{itemize} Because of their cost, exceptions are rarely used for hot paths of execution. Hence, there is an element of self-fulfilling prophecy as implementation techniques have been focused on making them cheap to set-up, techniques have been focused on making them cheap to set up, happily making them expensive to use in exchange. This difference is less important in higher-level scripting languages,