| | 45 | |
| | 46 | \begin{Grammar} |
| | 47 | \emph{FnDecl} |
| | 48 | &::=& \option{\emph{FnMods}} \emph{FnHeaderFront} \emph{FnHeaderClause} |
| | 49 | \EXP{=} \emph{Expr} \\ |
| | 50 | &$|$& \emph{FnSig} \\ |
| | 51 | |
| | 52 | \emph{FnSig} &::=& \emph{SimpleName} \EXP{\mathrel{\mathtt{:}}} \emph{Type}\\ |
| | 53 | |
| | 54 | \emph{FnMods} &::=& \emph{FnMod}$^+$\\ |
| | 55 | |
| | 56 | \emph{FnMod} &::=& \emph{AbsFnMod} $|$ \KWD{private}\\ |
| | 57 | |
| | 58 | \emph{AbsFnMod} &::=& \emph{LocalFnMod} $|$ |
| | 59 | \KWD{test}\\ |
| | 60 | |
| | 61 | \emph{LocalFnMod} &::=& \KWD{atomic} $|$ \KWD{io}\\ |
| | 62 | |
| | 63 | \emph{FnHeaderFront} |
| | 64 | &::=& \emph{NamedFnHeaderFront}\\ |
| | 65 | &$|$& \emph{OpHeaderFront} \\ |
| | 66 | |
| | 67 | \emph{NamedFnHeaderFront} |
| | 68 | &::=& \emph{Id} \option{\emph{StaticParams}} \emph{ValParam} \\ |
| | 69 | |
| | 70 | \emph{ValParam} &::=& \emph{BindId}\\ |
| | 71 | &$|$& \texttt(\option{\emph{Params}}\texttt)\\ |
| | 72 | |
| | 73 | \emph{Params} |
| | 74 | &::=& |
| | 75 | (\emph{Param}\EXP{,})$^*$ \options{\emph{Varargs}\EXP{,}} \emph{Keyword}(\EXP{,}\emph{Keyword})$^*$\\ |
| | 76 | &$|$& |
| | 77 | (\emph{Param}\EXP{,})$^*$ \emph{Varargs}\\ |
| | 78 | &$|$& \emph{Param}(\EXP{,} \emph{Param})$^*$\\ |
| | 79 | |
| | 80 | \emph{VarargsParam} &::=& \emph{BindId}\EXP{\COLONOP}\emph{Type}\EXP{...} \\ |
| | 81 | |
| | 82 | \emph{Varargs} &::=& \emph{VarargsParam}\\ |
| | 83 | |
| | 84 | \emph{Keyword} &::=& \emph{Param}\EXP{=}\emph{Expr} \\ |
| | 85 | |
| | 86 | \emph{PlainParam} &::=& \emph{BindId} \option{\emph{IsType}} \\ |
| | 87 | &$|$& \emph{Type} \\ |
| | 88 | |
| | 89 | \emph{Param} &::=& \emph{PlainParam}\\ |
| | 90 | |
| | 91 | \emph{FnHeaderClause} &::=& \option{\emph{IsType}} \emph{FnClauses} \\ |
| | 92 | |
| | 93 | \emph{FnClauses} &::=& \option{\emph{Throws}} \option{\emph{Where}} |
| | 94 | \emph{Contract} \\ |
| | 95 | |
| | 96 | \emph{Throws} &::=& \KWD{throws} \emph{MayTraitTypes}\\ |
| | 97 | |
| | 98 | \end{Grammar} |
| | 99 | |
| | 100 | Syntactically, a function declaration consists of |
| | 101 | an optional sequence of modifiers followed by |
| | 102 | the name of the function, optional static parameters |
| | 103 | (described in \chapref{trait-parameters}), |
| | 104 | the value parameter with its (optionally) declared type, |
| | 105 | an optional type of a return value, |
| | 106 | an optional declaration of thrown checked exceptions |
| | 107 | (discussed in \chapref{exceptions}), |
| | 108 | an optional \KWD{where} clause (discussed in \secref{where-clauses}), |
| | 109 | a contract for the function (discussed in \secref{contracts}), |
| | 110 | and finally an optional body expression preceded by the token \EXP{=}. |
| | 111 | A \KWD{throws} clause does not include naked type variables. |
| | 112 | Every element in a \KWD{throws} clause is a subtype of |
| | 113 | \TYP{CheckedException}. |
| | 114 | When a function declaration includes a body expression, it is called a |
| | 115 | \emph{function definition}. |
| | 116 | Function declarations can be mutually recursive. |
| | 117 | |
| | 118 | Function declarations can include the following special modifiers: |
| | 119 | \paragraph{\KWD{atomic}:} |
| | 120 | A function with the modifier \KWD{atomic} acts as if its entire |
| | 121 | body were surrounded in an \KWD{atomic} expression discussed in |
| | 122 | \secref{atomic}. |
| | 123 | |
| | 124 | \paragraph{\KWD{io}:} |
| | 125 | Functions that perform externally visible input/output actions |
| | 126 | are said to be \KWD{io} functions. |
| | 127 | An \KWD{io} function must not be invoked from a non-\KWD{io} function. |
| | 128 | |
| | 129 | A function takes exactly one argument, which may be a tuple. |
| | 130 | When a function takes a tuple argument, we abuse terminology by saying that |
| | 131 | the function takes multiple arguments. |
| | 132 | Value parameters cannot be mutated inside the function body. |
| | 133 | |
| | 134 | A function's value parameter consists of a parenthesized, |
| | 135 | comma-separated list of bindings where each binding is one of: |
| | 136 | \begin{itemize} |
| | 137 | \item A plain binding ``\VAR{identifier}'' |
| | 138 | or ``\EXP{\VAR{identifier}\COLONOP{}T}'' |
| | 139 | \item A varargs binding ``\EXP{\VAR{identifier}\COLONOP{}T\ldots}'' |
| | 140 | \item A keyword binding ``\EXP{\VAR{identifier}=\exp}'' |
| | 141 | or ``\EXP{\VAR{identifier}\COLONOP{}T=\exp}'' |
| | 142 | \end{itemize} |
| | 143 | When the parameter is a single plain binding without a declared type, |
| | 144 | enclosing parentheses may be elided. |
| | 145 | The following restrictions apply: No two bindings may have the same |
| | 146 | identifier. |
| | 147 | No keyword binding may precede a plain binding. |
| | 148 | No varargs binding may follow a keyword binding or precede a plain binding. |
| | 149 | Note that it is permitted to have a single plain binding, |
| | 150 | or to have no bindings. The latter case, ``()'', is considered equivalent |
| | 151 | to a single plain binding of the ignored identifier ``\_'' of type (), that |
| | 152 | is, ``\EXP{(\_\COLONOP())}''. |
| | 153 | Also, there can be at most one varargs binding. |
| | 154 | |
| | 155 | A parameter declared by keyword binding is called |
| | 156 | a \emph{keyword parameter}; |
| | 157 | a keyword parameter must be declared with |
| | 158 | a \emph{default} expression, |
| | 159 | which is used when no argument is bound to the parameter explicitly. |
| | 160 | Syntactically, the default expression is specified |
| | 161 | after an \EXP{=} sign. |
| | 162 | The default expression of a parameter $x$ of function $f$ |
| | 163 | is evaluated each time the function is called |
| | 164 | without a value provided for $x$ at the call site. |
| | 165 | All parameters occurring to the left of $x$ |
| | 166 | are in scope of its default expression. |
| | 167 | All parameters following $x$ must include default expressions as well; |
| | 168 | $x$ is in scope of their default expressions and the body of the function. |
| | 169 | When an argument is passed explicitly for a keyword parameter, |
| | 170 | that argument must be passed as a \emph{keyword argument}. |
| | 171 | (See \secref{function-app}.) |
| | 172 | If no type is declared for a keyword parameter, |
| | 173 | the type is inferred from the static type of its default expression. |
| | 174 | |
| | 175 | A parameter declared by varargs binding is called |
| | 176 | a \emph{varargs parameter}; |
| | 177 | it is used to pass a variable number of arguments to a function |
| | 178 | as a single heap sequence. |
| | 179 | The type of a varargs parameter |
| | 180 | is \EXP{\TYP{HeapSequence}\llbracket{}T\rrbracket} where \VAR{T} is |
| | 181 | the type mentioned in (or inferred for) that binding. |
| | 182 | \note{See the library section for a discussion of |
| | 183 | \TYP{HeapSequence}.} |
| | 184 | Note that the type of a varargs parameter cannot be omitted. |
| | 185 | If a function does not have a varargs parameter |
| | 186 | then the number of arguments is fixed by the function's type. |
| | 187 | Note that a varargs parameter is not allowed to have a default expression. |
| | 188 | |
| | 189 | For example, here is a simple |
| | 190 | polymorphic function for creating lists: |
| | 191 | \input{\home/basic/examples/Fun.Decl.tex} |
| | 192 | |
| | 195 | |
| | 196 | Fortress allows functions to be overloaded; there may be multiple function |
| | 197 | declarations with the same function name in a single lexical scope. |
| | 198 | Thus, we need to determine which functional declarations are applicable to |
| | 199 | a function application. |
| | 200 | |
| | 201 | An overloaded function has multiple arrow types in its function type, |
| | 202 | and it associates a declaration with each constituent arrow type. |
| | 203 | When an overloaded function of type \VAR{T} is called with an argument of type |
| | 204 | \VAR{A}, the call is ``dispatched'' to the declaration associated with |
| | 205 | the most specific type of \VAR{T} applicable to \VAR{A} |
| | 206 | (if no such type exists, then the function is not applicable to \VAR{A}). |
| | 207 | This is well defined because the overloading rules guarantee that |
| | 208 | the type of any function value is a well-formed function type. |
| | 209 | |
| | 210 | If a function's argument type is \EXP{()}, |
| | 211 | then function declarations with the following forms of parameter lists are |
| | 212 | considered to be applicable: |
| | 213 | \begin{itemize} |
| | 214 | \item \EXP{()} |
| | 215 | which means the same thing as \EXP{(\_\COLONOP())} |
| | 216 | \item \EXP{(x\COLONOP())} |
| | 217 | which is something programmers don't ordinarily write |
| | 218 | \item \EXP{(x\COLONOP{}T\ldots)} |
| | 219 | \end{itemize} |
| | 220 | In the last case, \VAR{x} is bound to an empty |
| | 221 | \EXP{\TYP{HeapSequence}\llbracket{}T\rrbracket}. |
| | 222 | |
| | 223 | If a function's argument type \VAR{A} is |
| | 224 | neither \EXP{()} nor a tuple type, then function declarations with the following forms of parameter lists are considered |
| | 225 | to be applicable: |
| | 226 | \begin{itemize} |
| | 227 | \item |
| | 228 | \EXP{(x\COLONOP{}T)} where \VAR{A} is a subtype of \VAR{T} |
| | 229 | \item |
| | 230 | \EXP{(x\COLONOP{}T\ldots)} where \VAR{A} is a subtype of \VAR{T} |
| | 231 | \end{itemize} |
| | 232 | In the last case, \VAR{x} is bound to a |
| | 233 | \EXP{\TYP{HeapSequence}\llbracket{}T\rrbracket} of length \EXP{1}, |
| | 234 | containing the actual argument value. |
| | 235 | |
| | 236 | If a function's argument type \VAR{A} is |
| | 237 | a tuple type, then function declarations with the following forms of |
| | 238 | parameter lists are considered to be applicable: |
| | 239 | \begin{itemize} |
| | 240 | \item |
| | 241 | \EXP{(x\COLONOP{}T)} where \VAR{A} is a subtype of \VAR{T} |
| | 242 | \item |
| | 243 | \EXP{(x\COLONOP{}T\ldots)} where \VAR{A} is a subtype of \VAR{T} |
| | 244 | \item |
| | 245 | a parameter list with no varargs binding, provided that |
| | 246 | \begin{itemize} |
| | 247 | \item type \VAR{A} has exactly as many plain types as the parameter |
| | 248 | list has plain bindings, and |
| | 249 | \item for every keyword-type pair (described in |
| | 250 | \secref{tuple-types}) in \VAR{A}, the parameter list has a |
| | 251 | binding with the same keyword, and |
| | 252 | \item for every element type in \VAR{A}, the type in the element type |
| | 253 | is a subtype of the type of the corresponding binding in the parameter list. |
| | 254 | \end{itemize} |
| | 255 | \item |
| | 256 | a parameter list with a varargs binding, provided that |
| | 257 | \begin{itemize} |
| | 258 | \item type \VAR{A} has at least as many plain types as the parameter |
| | 259 | list has plain bindings, and |
| | 260 | \item for every keyword-type pair in |
| | 261 | \VAR{A}, the parameter list has a binding with the same keyword, and |
| | 262 | \item for every element type in \VAR{A}, the type in the element type |
| | 263 | is a subtype of the type of the corresponding binding in the parameter |
| | 264 | list---but if there is no corresponding binding, then the type in the |
| | 265 | element type must be a subtype of the type in the varargs binding. |
| | 266 | \end{itemize} |
| | 267 | \end{itemize} |
| | 268 | In the latter case, the parameter named by the identifier in the varargs |
| | 269 | binding is bound to a \EXP{\TYP{HeapSequence}\llbracket{}T\rrbracket} |
| | 270 | that contains, in order, all the |
| | 271 | values of the tuple that did not correspond to plain |
| | 272 | bindings, followed by all the values in the varargs \TYP{HeapSequence} |
| | 273 | of the tuple, if any. |
| | 274 | |
| | 275 | When an argument is passed explicitly for a keyword parameter, that |
| | 276 | argument must be passed as a \emph{keyword argument}. |
| | 277 | Syntactically, a keyword argument is a keyword-value pair |
| | 278 | ``\EXP{\VAR{identifier} = e}''. |
| | 279 | Keyword parameters not explicitly bound are bound to their default values. |
| | 280 | If a parameter that has no default value is not explicitly bound to an |
| | 281 | argument, it is a static error. |
| | 282 | |
| | 283 | When a function is called (See \secref{function-calls} for a discussion |
| | 284 | of function call expressions), function arguments are evaluated in parallel, |
| | 285 | keyword parameters not explicitly bound are bound to their |
| | 286 | default values sequentially, |
| | 287 | and the body of the function is evaluated in |
| | 288 | a new environment, extending the environment in which |
| | 289 | it is defined with all parameters bound to their arguments. |
| | 290 | |
| | 291 | If the application of a function \VAR{f} ends by calling another |
| | 292 | function \VAR{g}, tail-call |
| | 293 | optimization must be applied. Storage used by the new environments |
| | 294 | constructed for the application of \VAR{f} must be reclaimed. |
| | 295 | |
| | 296 | Here are some examples: |
| | 297 | %sin(pi) |
| | 298 | %arctan(y, x) |
| | 299 | %makeColor(red=5, green=3, blue=43) |
| | 300 | %processString(s, start=5, finish=43) |
| | 301 | \input{\home/basic/examples/Fun.App.a.tex} |
| | 302 | If the function's argument is not a tuple, then the argument need not be |
| | 303 | parenthesized: |
| | 304 | %sin x |
| | 305 | %log log n |
| | 306 | \input{\home/basic/examples/Fun.App.b.tex} |
| | 307 | |
| | 308 | Here are a few varargs and keyword examples: |
| | 309 | %f(x:ZZ, y:ZZ..., z:ZZ = 0) = <| x, <| q | q<-y |>, z |> |
| | 310 | % |
| | 311 | %f(1) returns <| 1, <| |>, 0 |> |
| | 312 | %f(1, 2, 3) returns <| 1, <| 2, 3 |>, 0 |> |
| | 313 | %f(1, [2 3]...) returns <| 1, <| 2, 3 |>, 0 |> |
| | 314 | %f(1, 2, 3, [4 5]...) returns <| 1, <| 2, 3, 4, 5 |>, 0 |> |
| | 315 | %f(1, 2, 3, 17#3...) returns <| 1, <| 2, 3, 17, 18, 19 |>, 0 |> |
| | 316 | %f(1, 2, 3, z=8) returns <| 1, <| 2, 3 |>, 8 |> |
| | 317 | %f([2 3]...) declaration not applicable |
| | 318 | \begin{Fortress} |
| | 319 | \(f(x\COLONOP\mathbb{Z}, y\COLONOP\mathbb{Z}\ldots, z\COLONOP\mathbb{Z} = 0) = |
| | 320 | \langle x, \langle q \mid q\leftarrow{}y \rangle, z \rangle\)\\ |
| | 321 | \end{Fortress} |
| | 322 | |
| | 323 | \begin{tabular}{lcl} |
| | 324 | \EXP{f(1)} & \emph{returns} & |
| | 325 | \EXP{\langle 1, \langle \rangle, 0 \rangle} |
| | 326 | \\ |
| | 327 | \EXP{f(1, 2, 3)} & \emph{returns} & |
| | 328 | \EXP{\langle 1, \langle 2, 3 \rangle, 0 \rangle} |
| | 329 | \\ |
| | 330 | \EXP{f(1, [2\;3]\ldots)} & \emph{returns} & |
| | 331 | \EXP{\langle 1, \langle 2, 3 \rangle, 0 \rangle} |
| | 332 | \\ |
| | 333 | \EXP{f(1, 2, 3, [4\;5]\ldots)} & \emph{returns} & |
| | 334 | \EXP{\langle 1, \langle 2, 3, 4, 5 \rangle, 0 \rangle} |
| | 335 | \\ |
| | 336 | \EXP{f(1, 2, 3, 17\mathinner{\hbox{\tt\char'43}}3\ldots)} & \emph{returns} & |
| | 337 | \EXP{\langle 1, \langle 2, 3, 17, 18, 19 \rangle, 0 \rangle} |
| | 338 | \\ |
| | 339 | \EXP{f(1, 2, 3, z=8)} & \emph{returns} & |
| | 340 | \EXP{\langle 1, \langle 2, 3 \rangle, 8 \rangle} |
| | 341 | \\ |
| | 342 | \EXP{f([2\;3]\ldots)} & \emph{declaration not applicable} |
| | 343 | \end{tabular} |
| | 344 | |
| | 347 | \begin{Grammar} |
| | 348 | \emph{AbsFnDecl} &::=& \option{\emph{AbsFnMods}} \emph{FnHeaderFront} |
| | 349 | \emph{FnHeaderClause}\\ |
| | 350 | &$|$& \emph{FnSig} \\ |
| | 351 | |
| | 352 | \emph{AbsFnMods} &::=& \emph{AbsFnMod}$^+$\\ |
| | 353 | |
| | 354 | \emph{AbsFnMod} &::=& \emph{LocalFnMod} $|$ \KWD{test}\\ |
| | 355 | |
| | 356 | \emph{LocalFnMod} &::=& \KWD{atomic} $|$ \KWD{io}\\ |
| | 357 | |
| | 358 | \emph{FnSig} &::=& \emph{Name} \EXP{\mathrel{\mathtt{:}}} \emph{ArrowType}\\ |
| | 359 | \end{Grammar} |
| | 360 | |
| | 361 | In a component, a function with a single principal type may be declared separately from its |
| | 362 | definitions using an \emph{abstract function declaration}. It is a static |
| | 363 | error if there are two abstract function declarations with the same name. |
| | 364 | For an abstract function declaration with name \VAR{f}, argument type |
| | 365 | \VAR{T}, and return type \VAR{U} (\VAR{T} and \VAR{U} may be tuple types), |
| | 366 | any concrete declaration for \VAR{f} must be for a function whose argument |
| | 367 | type is a subtype of \VAR{T} and whose return type is a subtype of \VAR{U}. |
| | 368 | Furthermore, the union of the argument types of the concrete declarations for |
| | 369 | \VAR{f} must be equal to \VAR{T}. |
| | 370 | Concrete function declarations must satisfy the overloading rules. |
| | 371 | Unless \VAR{T} has a \KWD{comprises} clause (or is a tuple type, at least |
| | 372 | one of whose entries has a \KWD{comprises} clause), this implies that some |
| | 373 | concrete declaration for \VAR{f} must have argument type \VAR{T}. |
| | 374 | |
| | 375 | Syntactically, an abstract function declaration is a function declaration |
| | 376 | without a body. Parameter names may be elided but parameter types cannot |
| | 377 | be omitted. Additionally, when a function's type is not parameterized, |
| | 378 | Fortress provides an alternative mathematical notation for |
| | 379 | an abstract function declaration: function name followed by the token |
| | 380 | `\EXP{\COLONOP}', followed by an arrow type. |
| | 381 | |
| | 382 | For example, after the following abstract function declaration: |
| | 383 | %printMolecule(Molecule): () |
| | 384 | \begin{Fortress} |
| | 385 | \(\VAR{printMolecule}(\TYP{Molecule})\COLON ()\) |
| | 386 | \end{Fortress} |
| | 387 | where trait \TYP{Molecule} is defined as follows: |
| | 388 | %trait Molecule comprises {OrganicMolecule, InorganicMolecule} end |
| | 389 | \begin{Fortress} |
| | 390 | \(\KWD{trait} \TYP{Molecule} \KWD{comprises} \{\TYP{OrganicMolecule}, \TYP{InorganicMolecule}\} \KWD{end}\) |
| | 391 | \end{Fortress} |
| | 392 | the programmer could write: |
| | 393 | %printMolecule(molecule: Molecule) = ... |
| | 394 | \begin{Fortress} |
| | 395 | \(\VAR{printMolecule}(\VAR{molecule}\COLON \TYP{Molecule}) = \ldots\) |
| | 396 | \end{Fortress} |
| | 397 | or could write: |
| | 398 | %printMolecule(molecule: OrganicMolecule) = ... |
| | 399 | %printMolecule(molecule: InorganicMolecule) = ... |
| | 400 | \begin{Fortress} |
| | 401 | \(\VAR{printMolecule}(\VAR{molecule}\COLON \TYP{OrganicMolecule}) = \ldots\)\\ |
| | 402 | \(\VAR{printMolecule}(\VAR{molecule}\COLON \TYP{InorganicMolecule}) = \ldots\) |
| | 403 | \end{Fortress} |
| | 404 | For the latter, the programmer must provide a |
| | 405 | definition for every immediate subtype of \TYP{Molecule}, or |
| | 406 | it is a static error. |
| | 407 | |
| | 410 | |
| | 411 | \begin{Grammar} |
| | 412 | \emph{Contract} &::=& \option{\emph{Requires}} \option{\emph{Ensures}} \option{\emph{Invariant}}\\ |
| | 413 | |
| | 414 | \emph{Requires} &::=& |
| | 415 | \KWD{requires} \{ \option{\emph{ExprList}} \} \\ |
| | 416 | |
| | 417 | \emph{Ensures} &::=& |
| | 418 | \KWD{ensures} \{ \option{\emph{EnsuresClauseList}} \} \\ |
| | 419 | |
| | 420 | \emph{EnsuresClauseList} &::=& |
| | 421 | \emph{EnsuresClause}(\EXP{,} \emph{EnsuresClause})$^*$ \\ |
| | 422 | |
| | 423 | \emph{EnsuresClause} &::=& \emph{Expr} \options{\KWD{provided} \emph{Expr}} \\ |
| | 424 | |
| | 425 | \emph{Invariant}&::=& |
| | 426 | \KWD{invariant} \{ \option{\emph{ExprList}} \} \\ |
| | 427 | |
| | 428 | \end{Grammar} |
| | 429 | |
| | 430 | Function contracts consist of three optional clauses: a \KWD{requires} |
| | 431 | clause, an \KWD{ensures} clause, and an \KWD{invariant} clause. |
| | 432 | All three clauses are evaluated in the scope of the function body. |
| | 433 | \note{ |
| | 434 | extended with a special variable \emph{arg}, bound to an immutable |
| | 435 | array of all function arguments. (This array is useful for describing |
| | 436 | contracts in the presence of higher-order functions; |
| | 437 | see \secref{arrow-types}). |
| | 438 | } |
| | 439 | |
| | 440 | \note{ |
| | 441 | Jan: Which of the following must be pure? Presumably all of them. |
| | 442 | We should say so. |
| | 443 | |
| | 444 | Eric: Requiring declared purity on all called functions in a contract is |
| | 445 | too restrictive. |
| | 446 | } |
| | 447 | |
| | 448 | The \KWD{requires} clause consists of a sequence of |
| | 449 | expressions of type \TYP{Boolean} separated by commas and |
| | 450 | enclosed in curly braces. |
| | 451 | The \KWD{requires} clause is evaluated during a function call before |
| | 452 | the body of the function. If any expression in a \KWD{requires} clause |
| | 453 | does not evaluate to \VAR{true}, a \TYP{CallerViolation} exception is |
| | 454 | thrown. |
| | 455 | |
| | 456 | The \KWD{ensures} clause consists of a sequence of |
| | 457 | \KWD{ensures} subclauses. Each such subclause consists of an |
| | 458 | expression of type \TYP{Boolean}, |
| | 459 | optionally followed by |
| | 460 | a \KWD{provided} subclause. A \KWD{provided} subclause begins with |
| | 461 | \KWD{provided} followed by an expression of type |
| | 462 | \TYP{Boolean}. For each subclause in the \KWD{ensures} clause of a |
| | 463 | contract, the \KWD{provided} |
| | 464 | subclause is evaluated immediately after the \KWD{requires} clause during a |
| | 465 | function call (before the function body is evaluated). |
| | 466 | If a \KWD{provided} subclause evaluates to \VAR{true}, |
| | 467 | then the expression preceding this \KWD{provided} subclause is evaluated |
| | 468 | after the function body is evaluated. |
| | 469 | If the expression evaluated after function evaluation does |
| | 470 | not evaluate to \VAR{true}, a \TYP{CalleeViolation} exception is thrown. |
| | 471 | The expression preceding the \KWD{provided} subclause can refer to |
| | 472 | the return value of the function. |
| | 473 | A \KWD{outcome} variable is implicitly bound to a return value of the |
| | 474 | function and is in scope of |
| | 475 | the expression preceding the \KWD{provided} subclause. |
| | 476 | The implicitly declared \KWD{outcome} shadows any other declaration with the |
| | 477 | same name in scope. |
| | 478 | |
| | 479 | The \KWD{invariant} clause consists of a sequence of expressions of |
| | 480 | \emph{any type} enclosed by curly braces. |
| | 481 | These expressions are evaluated before and after a function call. For |
| | 482 | each expression $e$ in this sequence, if the value of $e$ when evaluated |
| | 483 | before the function call is not equal to the value of $e$ after |
| | 484 | the function call, a \TYP{CalleeViolation} exception is thrown. |
| | 485 | |
| | 486 | Here are some examples: |
| | 487 | \input{\home/basic/examples/Fun.Contract.tex} |
| | 488 | |
| | 489 | Overloaded function contracts are handled similarly with method contracts |
| | 490 | described in \secref{method-contracts}. In particular, substitutability is |
| | 491 | preserved: the statically most applicable function to a call should be |
| | 492 | substitutable with the dynamically most applicable function to the call. |
| | 493 | For a call of function $f$, |
| | 494 | we use the term \emph{static contract} of $f$ to refer to |
| | 495 | a contract declared in the statically most applicable function declaration |
| | 496 | and the term \emph{dynamic contract} of $f$ to refer to a contract declared |
| | 497 | in the dynamically most applicable function declaration. |
| | 498 | Three exceptions may be thrown due to an overloaded function contract |
| | 499 | violation: \TYP{CallerViolation} is thrown when the \KWD{requires} clause |
| | 500 | of the static contract fails, \TYP{CalleeViolation} is thrown when the |
| | 501 | \KWD{ensures} or \KWD{invariant} clause of the dynamic contract fails, |
| | 502 | and \TYP{ContractOverloadingViolation} is thrown |
| | 503 | when the \KWD{requires} clause of the dynamic contract |
| | 504 | or the \KWD{ensures} or \KWD{invariant} clause of the static contract fails. |
| | 505 | |
| | 506 | |
| | 507 | Evaluation of a call of function $f$ proceeds as follows. |
| | 508 | Let $C$ and $C'$ be the static and dynamic contracts of $f$, respectively. |
| | 509 | If the \KWD{requires} clause of $C$ fails, |
| | 510 | a \TYP{CallerViolation} exception is thrown. |
| | 511 | % |
| | 512 | Otherwise, if the \KWD{requires} clause of $C'$ fails, |
| | 513 | a \TYP{ContractOverloadingViolation} exception is thrown. |
| | 514 | % |
| | 515 | Otherwise, the \KWD{provided} subclauses of $C$ and $C'$ are evaluated. For |
| | 516 | every \KWD{provided} subclause that evaluates to \VAR{true}, the |
| | 517 | corresponding \KWD{ensures} subclause is recorded in a table |
| | 518 | $E$ for later comparison. Similarly, the \KWD{invariant} clauses of $C$ |
| | 519 | and $C'$ are evaluated and the results are stored in $E$ for later |
| | 520 | comparison. |
| | 521 | % |
| | 522 | Then the body of the dynamically most applicable function declaration of $f$ |
| | 523 | is evaluated. After evaluation of the body, |
| | 524 | % |
| | 525 | all \KWD{ensures} subclauses of the dynamic contract recorded in $E$ |
| | 526 | are checked to ensure that they evaluate to \VAR{true}, and |
| | 527 | all \KWD{invariant} clauses of the dynamic contract recorded in $E$ |
| | 528 | are checked to ensure that they evaluate to values equal to the |
| | 529 | values they evaluated to before evaluation of the body. |
| | 530 | % |
| | 531 | If any such check fails, a \TYP{CalleeViolation} |
| | 532 | exception is thrown. Otherwise, all \KWD{ensures} subclauses and |
| | 533 | \KWD{invariant} clauses of the static contract in $E$ are checked. |
| | 534 | If any of these checks fails, a \TYP{ContractOverloadingViolation} exception |
| | 535 | is thrown. |
| | 536 | |