Ignore:
Timestamp:
Sep 27, 2021, 10:16:57 AM (2 months ago)
Author:
Thierry Delisle <tdelisle@…>
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
Added
Removed
  • doc/theses/andrew_beach_MMath/intro.tex

    r716b62c r056cbdb  
    3939it returns control to that function.
    4040\begin{center}
    41 \input{termination}
    42 
    43 \medskip
     41%\input{termination}
     42%
     43%\medskip
    4444\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.
    4547\end{center}
    46 \todo*{Can I make the new diagrams fit the old style?}
    4748
    4849Resumption exception handling searches the stack for a handler and then calls
     
    5354that preformed the raise, usually starting after the raise.
    5455\begin{center}
    55 \input{resumption}
    56 
    57 \medskip
     56%\input{resumption}
     57%
     58%\medskip
    5859\input{resumhandle.pstex_t}
     60% The other one.
    5961\end{center}
    6062
     
    136138message as a payload\cite{Ada12}.
    137139
    138 The modern flag-ship for termination exceptions is \Cpp,
     140The modern flagship for termination exceptions -- if one exists -- is \Cpp,
    139141which added them in its first major wave of non-object-orientated features
    140142in 1990.\cite{CppHistory}
     
    193195included in the \Cpp standard.
    194196% https://en.wikipedia.org/wiki/Exception_handling
    195 Since then, resumptions have been ignored in main-stream programming languages.
     197Since then, resumptions have been ignored in mainstream programming languages.
    196198However, resumption is being revisited in the context of decades of other
    197199developments in programming languages.
     
    215217
    216218%\subsection
    217 More recently exceptions, seem to be vanishing from newer programming
     219More recently, exceptions seem to be vanishing from newer programming
    218220languages, replaced by ``panic".
    219221In Rust, a panic is just a program level abort that may be implemented by
    220222unwinding the stack like in termination exception
    221223handling.\cite{RustPanicMacro}\cite{RustPanicModule}
    222 Go's panic through is very similar to a termination, except it only supports
     224Go's panic though is very similar to a termination, except it only supports
    223225a catch-all by calling \code{Go}{recover()}, simplifying the interface at
    224226the cost of flexibility.\cite{Go:2021}
     
    243245through multiple functions before it is addressed.
    244246
     247Here is an example of the pattern in Bash, where commands can only  ``return"
     248numbers and most output is done through streams of text.
     249\begin{lstlisting}[language=bash,escapechar={}]
     250# Immediately after running a command:
     251case $? in
     2520)
     253        # Success
     254        ;;
     2551)
     256        # Error Code 1
     257        ;;
     2582|3)
     259        # Error Code 2 or Error Code 3
     260        ;;
     261# Add more cases as needed.
     262asac
     263\end{lstlisting}
     264
    245265\item\emph{Special Return with Global Store}:
    246266Similar to the error codes pattern but the function itself only returns
     
    252272
    253273This approach avoids the multiple results issue encountered with straight
    254 error codes but otherwise has the same disadvantages and more.
     274error codes as only a single error value has to be returned,
     275but otherwise has the same disadvantages and more.
    255276Every function that reads or writes to the global store must agree on all
    256277possible errors and managing it becomes more complex with concurrency.
     278
     279This example shows some of what has to be done to robustly handle a C
     280standard library function that reports errors this way.
     281\begin{lstlisting}[language=C]
     282// Now a library function can set the error.
     283int handle = open(path_name, flags);
     284if (-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?
    257297
    258298\item\emph{Return Union}:
     
    265305This pattern is very popular in any functional or semi-functional language
    266306with primitive support for tagged unions (or algebraic data types).
    267 % We need listing Rust/rust to format code snippets from it.
    268 % Rust's \code{rust}{Result<T, E>}
     307Return unions can also be expressed as monads (evaluation in a context)
     308and often are in languages with special syntax for monadic evaluation,
     309such as Haskell's \code{haskell}{do} blocks.
     310
    269311The main advantage is that an arbitrary object can be used to represent an
    270312error, so it can include a lot more information than a simple error code.
     
    272314execution, and if there aren't primitive tagged unions proper, usage can be
    273315hard to enforce.
     316% We need listing Rust/rust to format code snippets from it.
     317% Rust's \code{rust}{Result<T, E>}
     318
     319This is a simple example of examining the result of a failing function in
     320Haskell, using its \code{haskell}{Either} type.
     321Examining \code{haskell}{error} further would likely involve more matching,
     322but the type of \code{haskell}{error} is user defined so there are no
     323general cases.
     324\begin{lstlisting}[language=haskell]
     325case failingFunction argA argB of
     326    Right value -> -- Use the successful computed value.
     327    Left error -> -- Handle the produced error.
     328\end{lstlisting}
     329
     330Return unions as monads will result in the same code, but can hide most
     331of the work to propagate errors in simple cases. The code to actually handle
     332the errors, or to interact with other monads (a common case in these
     333languages) still has to be written by hand.
     334
     335If \code{haskell}{failingFunction} is implemented with two helpers that
     336use the same error type, then it can be implemented with a \code{haskell}{do}
     337block.
     338\begin{lstlisting}[language=haskell,literate={}]
     339failingFunction x y = do
     340        z <- helperOne x
     341        helperTwo y z
     342\end{lstlisting}
    274343
    275344\item\emph{Handler Functions}:
     
    292361function calls, but cheaper (constant time) to call,
    293362they are more suited to more frequent (less exceptional) situations.
     363Although, in \Cpp and other languages that do not have checked exceptions,
     364they can actually be enforced by the type system be more reliable.
     365
     366This is a more local example in \Cpp, using a function to provide
     367a default value for a mapping.
     368\begin{lstlisting}[language=C++]
     369ValueT 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}
    294378\end{itemize}
    295379
     
    297381Because of their cost, exceptions are rarely used for hot paths of execution.
    298382Hence, there is an element of self-fulfilling prophecy as implementation
    299 techniques have been focused on making them cheap to set-up,
     383techniques have been focused on making them cheap to set up,
    300384happily making them expensive to use in exchange.
    301385This difference is less important in higher-level scripting languages,
Note: See TracChangeset for help on using the changeset viewer.