= CIVL-C Language Manual == Types === The Boolean type The boolean type is denoted `_Bool`, as in C. Its values are 0 and 1, which are also denoted by `$false` and `$true`, respectively. One may also include the standard C header `stdbool.h`, which defines `false` and `true` (also as 0 and 1), and defines the type `bool` to be an alias for `_Bool`. === The integer type There is one integer type, corresponding to the mathematical integers. Currently, all of the C integer types `int`, `long`, `unsigned int`, `short`, etc., are mapped to the CIVL integer type. [This is expected to change.] === The real type There is one real type, corresponding to the mathematical real numbers. Currently, all of the C real types `double`, `float`, etc., are mapped to the CIVL real type. [This is expected to change.] === The process type `$proc` This is a primitive object type and functions like any other primitive C type (e.g., `int`). An object of this type refers to a process. It can be thought of as a process ID, but it is not an integer and cannot be cast to one. Certain expressions take an argument of `$proc` type and some return something of `$proc` type. The operators `==` and `!=` may be used with two arguments of type `$proc` to determine whether the two arguments refer to the same process. The constant `$self` has `$proc` type and refers to the process evaluating this expression; constant `$proc_null` has `$proc` type and refers to no process. === The scope type `$scope` An object of this type is a reference to a dynamic scope. Several constants, expressions, and functions dealing with the $scope type are also provided. The `$scope` type is like any other object type. It may be used as the element type of an array, a field in a structure or union, and so on. Expressions of type `$scope` may occur on the left or right-hand sides of assignments and as arguments in function calls just like any other expression. Two different variables of type `$scope` may be aliased, i.e., they may refer to the same dynamic scope. === Domain types: `$domain` and `$domain(n)` A domain type is used to represent a set of tuples of integer values. Every tuple in a domain object has the same arity (i.e., number of components). The arity must be at least 1, and is called the dimension of the domain object. For each integer constant expression n, there is a type `$domain(n)`, representing domains of dimension n. The universal domain type, denoted `$domain`, represents domains of all positive dimensions, i.e., it is the union over all n ≥ 1 of `$domain(n)`. In particular, each `$domain(n)` is a subtype of `$domain`. There are expressions for specifying domain values. Certain statements use domains, such as the “CIVL-for” loop `$for`. === The range type `$range` An object of this type represents an ordered set of integers. Ranges are typically used as a step in constructing domains. They can also be used in quantified expressions to specify the domain of a bound variable (see `$forall` and `$exists`). == Type qualifiers === The `$input` and `$output` type qualifiers The declaration of a variable in the root scope may include the type qualifier `$input`, e.g., {{{ $input int N; }}} This declares the variable to be an input variable, i.e., one which is considered to be an input to the program. Such a variable is initialized with an arbitrary (unconstrained) value of its type. When using symbolic execution to verify a program, such a variable will be assigned a unique symbolic constant of its type. In contrast, variables in the root scope which are not input variables will instead be initialized with the “undefined” value. Reading an undefined value is erroneous. [Note: the model checker attempts to catch such errors but currently does not do so for arrays, which are always initialized with unconstrained values.] In addition, input variables may only be read, never written to; an attempt to write to an input variable is also flagged as an error. Alternatively, it is possible to specify a particular concrete value for an input variable, either on the command line, e.g., {{{ civl verify -inputN=8 ... }}} or by including an initializer in the declaration, e.g. {{{ $input int N=8; }}} The protocol for initializing an input variable is the following: if a command line value is specified, it is used. Otherwise, if an initializer is present, it is used. Otherwise, the variable is assigned an arbitrary value of its type. A variable in the root scope may be declared with `$output` to declare it to be an output variable. Output variables may only be written to, never read. They are used primarily in functional equivalence checking. Input and output variables play a key role when determining whether two programs are functionally equivalent. Two programs are considered functionally equivalent if, whenever they are given the same inputs (i.e., corresponding $input variables are initialized with the same values) they will produce the same outputs (i.e., corresponding $output variables will end up with the same values at termination). === Abstract (uninterpreted) functions An abstract function declares a function without a body. An abstract function is declared using a standard function prototype with the function qualifier `$abstract`, e.g., {{{ $abstract int f(int x); }}} An abstract function must have a non-void return type and take at least one parameter. An invocation of an abstract function is an expression and can be used anywhere an expression is allowed. The interpretation is an "uninterpreted function". A unique symbolic constant of function type will be created, corresponding to the abstract function, and invocations are represented as applications of the uninterpreted function to the arguments. === Atomic functions A function is declared atomic using the function qualifier `$atomic_f`, e.g., {{{ $atomic_f int f(int x) { ... } }}} A call to such a function executes as a single atomic step, i.e., without interleaving from other processes. Hence, this is only relevant for concurrent programs. Declaring a function to be atomic is almost equivalent to placing `$atomic{ ... }` around the function body. The difference is that in the latter case, the call to the function and the execution of the body are executed in two atomic steps, i.e., after activation frame is pushed onto the call stack, another process could execute before the first process obtains the atomic lock and executes its body. For an atomic function, the entire sequence of events happens in one atomic step. An atomic function must have a definition; in particular, neither a system function nor an abstract function may be declared using `$atomic_f`. === System functions A system function is one in which the definition of the function is not provided in CIVL-C code, but is implemented instead in a certain Java class. A system function is declared by adding the function qualifier `$system` to a function prototype. Invocation of a system function always takes place in a single atomic step. A system function may have a guard, which is specified in the function contract using a `$executes_when` clause. Unless constrained by its contract or other qualifiers, a system function may modify the stay in an arbitrary way. === Pure functions A system or atomic function may be declared to be `$pure`, e.g., {{{ $pure $system double sin(double x); $pure $atomic_f double mysin(double x) { return x - x*x*x/6.; } }}} This means that the function is a mathematical function of its arguments only. I.e., an invocation of the function has no side-effects and the return value depends on the arguments only (if called twice with the same arguments, it will return the same value, regardless of any differences in the state). The user is responsible for ensuring that a function declared pure actually is pure. If this is not the case, the model checker may produce incorrect results. == Expressions === Boolean expressions CIVL-C provides the boolean constants`$true` and `$false`, which are simply defined as 1 and 0, respectively. CIVL-C is, after all, an extension of C. A program may also include the standard C library header file `stdbool.h`, which defines `true` and `false` in the exact same way. In addition to the standard logical operators `&&`, `||`, and `!`, CIVL-C provides `=>` (implies). `p => q` is equivalent to `!(p) || q` (and has the same short-circuit semantics). A universally quantified formula has the form `$forall` `(` ''variable-decls'' (`;` ''variable-decls'' )* (`|` ''restriction'')? `)` ''expr'' where ''variable-decls'' has one of the forms ''type'' ''identifier'' (`,` ''identifier'')* ''type'' ''identifier'' `:` ''range'' where ''type'' is a type name (e.g., `int` or `double`), ''identifier'' is the name of the bound variable, ''restriction'' is a formula (boolean expression) which expresses some restriction on the values that the bound variable can take, ''range'' is an expression of `$range` type, and ''expr'' is a formula. The universally quantified formula holds iff for all values assignable to the bound variable for which the restriction holds, the formula expr holds. The syntax for existential quantification is the same, with `$exists` in place of `$forall`. Examples: {{{ int a[3], b[3][2]; int main() { int n=3, m=2; $assert($forall (int i | 0<=i && i s2` is equivalent to `s2 < s1`. * `s1 >= s2` is equivalent to `s2 <= s1`. Each of these expressions is erroneous if `s1` or `s2` is undefined. This error is reported by the model checker. === This process: `$self` This expression of `$proc` type returns a reference to the process which is evaluating this expression. It provides a way for code to obtain the identity of the process executing the code. == Statements === Atomic blocks: `$atomic` An ''atomic block'' is a statement of the form {{{ $atomic { stmt1; stmt2; ... } }}} and indicates that the statements comprising the block should be executed without the intervention of other processes. More precisely, there is a global atomic lock which is initially free. A process attempting to execute an atomic block will wait until the atomic lock becomes free, i.e., no other process is inside an atomic block. The process must also wait until the guard of the atomic block holds; this means that the first statement in the block is enabled. Once the atomic lock is free and the guard holds, the atomic block becomes enabled and the process may enter the atomic block. The process may not necessarily enter the atomic block as soon as these conditions hold, because some other enabled process may be scheduled first. In fact, some other process may obtain the atomic lock. But if the enabling conditions hold and this process is scheduled, it will obtain the atomic lock and begin executing the statements of the atomic block without other processes executing. Upon reaching the end of the atomic block, the process releases the atomic lock and exits the block. There is an exception to atomicity: if the process inside the atomic block executes a `$yield` statement, it releases the atomic lock. This allows other processes to execute, and even obtain the lock. At any point at which the atomic lock is free and the first statement following the `$yield` is enabled, the original process may re-obtain the atomic lock and continue executing its block atomically. If a statement inside an atomic block blocks, so that the process executing the atomic block has no enabled statement, execution deadlocks. The exception to this rule is that the first statement in the atomic block, and the first statement after a `$yield`, as described above, may block, without necessarily causing deadlock. Atomic blocks may be nested. Hence the atomic lock is held with a certain multiplicity. Each time the process enters an atomic block, the multiplicity is incremented. Each time it leaves an atomic block, the multiplicity is decremented. When the multiplicity hits 0, the atomic lock is released. Execution of a `$yield` does not change the multiplicity; the process releases the lock but maintains the multiplicity, so that when the lock is re-obtained, the original multiplicity is still in place. === Nondeterministic selection statement `$choose` A `$choose` statement has the form {{{ $choose { stmt1; stmt2; ... default: stmt } }}} The default clause is optional. The guards of the statements are evaluated and among those that are true, one is chosen nondeterministically and executed. If none are true and the default clause is present, it is chosen. The default clause will only be selected if all guards are false. If no default clause is present and all guards are false, the statement blocks. Hence the implicit guard of the `$choose` statement without a default clause is the disjunction of the guards of its sub-statements. The implicit guard of the `$choose` statement with a default clause is ''true''. Example: this shows how to encode a “low-level” guarded transition system: {{{ l1: $choose { $when (x>0) {x--; goto l2;} $when (x==0) {y=1; goto l3;} default: {z=1; goto l4;} } l2: $choose { ... } l3: $choose { ... } }}} === Domain iteration statement: `$for` A domain statement has the form `$for` `(``int` ''i1''`,` ...`,` ''in'' `:` ''dom''`)` ''S'' where ''i1'', . . . , ''in'' are ''n'' identifiers, ''dom'' is an expression of type `$domain(`''n''`)`, and ''S'' is a statement. The identifiers declare ''n'' variables of integer type. Control iterates over the values of the domain, assigning the integer variables the components of the current tuple in the domain at the start of each iteration. The scope of the variables extends to the end of ''S''. The iterations takes place in the order specified by the domain, e.g., dictionary order for a Caretesian domain. Note that if a range expression can be used as ''dom'' here, it will be automatically converted to a one-dimensional domain. For example, {{{ $for (int i : 0 .. 10) S }}} is equivalent to {{{ $for (int i: ($domain(1)){0 .. 10}) S }}} There is a also a parallel version of this construct, `$parfor`. === `$parfor` === `$spawn` === `$when` == Functions === `$assert` === `$assume` === `$assume_push` === `$assume_pop` === `$choose_int` === `$default_value` === `$elaborate_domain` === `$exit` === `$free` === `$havoc` === `$hidden` === `$hide` === `$is_derefable` === `$is_terminated` === `$local_end` === `$local_start` === `$malloc` === `$pathCondition` === `$pow` `$pow(double, double)` === `$reveal` === `$wait` and `$waitall` === `$yield` == Macros === `$elaborate` == Contract Annotations clauses === The `depends_on` clause `\nothing` === The `executes_when` clause