Something went wrong. Try again.
Actually just three programming languages in a trenchcoat
Something went wrong. Try again.
1.8 kB · 51 lines
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152\subsubsection{Assert}
The last statement, of very low importance, is the \kw{assert} statement.In a real program the \kw{assert} statement is likely only to be used toenforce invariants, or otherwise only in tests, which are further discussed in\S\ref{sec:tests}.
\begin{bnf*} \bnfprod{Assert}{ \bnfts{\kw{assert}} \bnfsp \bnfpn{Expression} \bnfor } \\ \bnfmore{ \bnfts{\kw{assert}} \bnfsp \bnfpn{Expression} \bnfsp \bnfts{\kw{as}} \bnfsp \bnfpn{Expression} }\end{bnf*}
The \kw{assert} statement is optionally provided an expression (before \kw{as})which is only evaluated in case of failure to be included in an error message.If no message is provided, the source text of the expression being asserted isextracted and used as the message.
The other (or only) expression is the condition which is used to determinewhether to end the program. If it evaluates to \kw{true}, the assertion wasa success and execution continues.
When \kw{false}, the message expression (if any) is evaluated, an effect is\kw{yield}ed of the form \texttt{'unhandled\_exception(message)}. While it is possibleto handle this effect, it is not recommended for anything but exceptionallyspecific reasons, as assertion errors are typically a sign of programer failure.If the handler for this effect attempts to \kw{resume}, the program will end.
\begin{prooftree} \AxiomC{$\Gamma\vdash C = \kw{true}$} \LeftLabel{Assertion} \UnaryInfC{$\Gamma\vdash \kw{assert}\ M\ \kw{as}\ C : \top$}\end{prooftree}
\begin{prooftree} \AxiomC{$\Gamma\vdash C = \kw{false}$} \LeftLabel{Assertion} \UnaryInfC{$\Gamma\vdash \kw{assert}\ M\ \kw{as}\ C : \bot$}\end{prooftree}