The Ciao assertion package
Author(s): Manuel Hermenegildo, Francisco Bueno, German Puebla.The assertions package adds a number of new declaration definitions and new operator definitions which allow including program assertions in user programs. Such assertions can be used to describe predicates, properties, modules, applications, etc. These descriptions can contain formal specifications (such as sets of preconditions, post-conditions, or descriptions of computations) as well as machine-readable textual comments.
This module is part of the assertions library. It defines the basic code-related assertions, i.e., those intended to be used mainly by compilation-related tools, such as the static analyzer or the run-time test generator.
Giving specifications for predicates and other program elements is the main functionality documented here. The exact syntax of comments is described in the autodocumenter (lpdoc [Knu84,Her99]) manual, although some support for adding machine-readable comments in assertions is also mentioned here.
There are two kinds of assertions: predicate assertions and program point assertions. All predicate assertions are currently placed as directives in the source code, i.e., preceded by “:-”. Program point assertions are placed as goals in clause bodies.
More info
The facilities provided by the library are documented in the description of its component modules. This documentation is intended to provide information only at a “reference manual” level. For a more tutorial introduction to the subject and some more examples please see [PBH00]. The assertion language implemented in this library is modeled after this design document, although, due to implementation issues, it may differ in some details. The purpose of this manual is to document precisely what the implementation of the library supports at any given point in time.
Some attention points
- Formatting commands within text strings: many of the predicates defined in these modules include arguments intended for providing textual information. This includes titles, descriptions, comments, etc. The type of this argument is a character string. In order for the automatic generation of documentation to work correctly, this character string should adhere to certain conventions. See the description of the docstring/1 type/grammar for details.
- Referring to variables: In order for the automatic documentation system to work correctly, variable names (for example, when referring to arguments in the head patterns of pred declarations) must be surrounded by an @var command. For example, @var{VariableName} should be used for referring to the variable “VariableName”, which will appear then formatted as follows: VariableName. See the description of the docstring/1 type/grammar for details.
Usage and interface
- Library usage:
The recommended procedure in order to make use of assertions in user programs is to include the assertions syntax library, using one of the following declarations, as appropriate::- module(...,...,[assertions]). :- use_package([assertions]).
- Exports:
- New operators defined:
=>/2 [975,xfx], ::/2 [978,xfx], decl/1 [1150,fx], decl/2 [1150,xfx], pred/1 [1150,fx], pred/2 [1150,xfx], prop/1 [1150,fx], prop/2 [1150,xfx], modedef/1 [1150,fx], calls/1 [1150,fx], calls/2 [1150,xfx], success/1 [1150,fx], success/2 [1150,xfx], test/1 [1150,fx], test/2 [1150,xfx], texec/1 [1150,fx], texec/2 [1150,xfx], comp/1 [1150,fx], comp/2 [1150,xfx], entry/1 [1150,fx], exit/1 [1150,fx], exit/2 [1150,xfx]. - New declarations defined:
pred/1, pred/2, texec/1, texec/2, calls/1, calls/2, success/1, success/2, test/1, test/2, comp/1, comp/2, prop/1, prop/2, entry/1, exit/1, exit/2, modedef/1, decl/1, decl/2, doc/2, comment/2. - Imports:
- System library modules:
assertions/assertions_props. - Packages:
prelude, nonpure.
- System library modules:
Documentation on new declarations
More than one of these assertions may appear per predicate, in which case each one represents a possible “mode” of use (usage) of the predicate. The exact scope of the usage is defined by the properties given for calls in the body of each assertion (which should thus distinguish the different usages intended). All of them together cover all possible modes of usage.
For example, the following assertions describe (all the and the only) modes of usage of predicate length/2 (see lists):
:- pred length(L,N) : list * var => list * integer # "Computes the length of L.". :- pred length(L,N) : var * integer => list * integer # "Outputs L of length N.". :- pred length(L,N) : list * integer => list * integer # "Checks that L is of length N.".
Usage::- pred AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_body/1)AssertionBody is an assertion body.
Usage::- AssertionStatus pred AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:assrt_body/1)AssertionBody is an assertion body.
Usage::- texec AssertionBody.
- The following properties should hold at call time:
(assertions_props:c_assrt_body/1)AssertionBody is a call assertion body.
Usage::- AssertionStatus texec AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:c_assrt_body/1)AssertionBody is a call assertion body.
For example, the following assertion describes all possible calls to predicate is/2 (see arithmetic):
:- calls is(term,arithexpression).
Usage::- calls AssertionBody.
- The following properties should hold at call time:
(assertions_props:c_assrt_body/1)AssertionBody is a call assertion body.
Usage::- AssertionStatus calls AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:c_assrt_body/1)AssertionBody is a call assertion body.
For example, the following assertion specifies the answers of the length/2 predicate if it is called as in the first mode of usage above (note that the previous pred assertion already conveys such information, however it also compelled the predicate calls, while the success assertion does not):
:- success length(L,N) : list * var => list * integer.
Usage::- success AssertionBody.
- The following properties should hold at call time:
(assertions_props:s_assrt_body/1)AssertionBody is a predicate assertion body.
Usage::- AssertionStatus success AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:s_assrt_body/1)AssertionBody is a predicate assertion body.
:- test length(L,N) : ( L = [1,2,5,2] ) => ( N = 4 ).
Usage::- test AssertionBody.
- The following properties should hold at call time:
(assertions_props:s_assrt_body/1)AssertionBody is a predicate assertion body.
Usage::- AssertionStatus test AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:s_assrt_body/1)AssertionBody is a predicate assertion body.
For example, the following assertion specifies that the computation of append/3 (see lists) will not fail if it is called as described (but does not compel the predicate to be called that way):
:- comp append(Xs,Ys,Zs) : var * var * var + not_fail.
Usage::- comp AssertionBody.
- The following properties should hold at call time:
(assertions_props:g_assrt_body/1)AssertionBody is a comp assertion body.
Usage::- AssertionStatus comp AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:g_assrt_body/1)AssertionBody is a comp assertion body.
Properties are standard predicates, but which are guaranteed to terminate for any possible instantiation state of their argument(s), do not perform side-effects which may interfere with the program behaviour, and do not further instantiate their arguments or add new constraints.
Provided the above holds, properties can thus be safely used as run-time checks. The program transformation used in ciaopp for run-time checking guarantees the third requirement. It also performs some basic checks on properties which in most cases are enough for the second requirement. However, it is the user's responsibility to guarantee termination of the properties defined. (See also Declaring regular types for some considerations applicable to writing properties.)
The set of properties is thus a strict subset of the set of predicates. Note that properties can be used to describe characteristics of arguments in assertions and they can also be executed (called) as any other predicates.
Usage::- prop AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_body/1)AssertionBody is an assertion body.
Usage::- AssertionStatus prop AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:assrt_body/1)AssertionBody is an assertion body.
These assertions are trusted by the compiler. As a result, if their descriptions are erroneous they can introduce bugs in programs. Thus, entry/1 assertions should be written with care.
An important use of these assertions is in providing information to the compiler which it may not be able to infer from the program. The main use is in providing information on the ways in which exported predicates of a module will be called from outside the module. This will greatly improve the precision of the analyzer, which otherwise has to assume that the arguments that exported predicates receive are any arbitrary term.
Usage::- entry AssertionBody.
- The following properties should hold at call time:
(assertions_props:c_assrt_body/1)AssertionBody is a call assertion body.
:- exit length(L,N) : list * var => list * integer.
Usage::- exit AssertionBody.
- The following properties should hold at call time:
(assertions_props:s_assrt_body/1)AssertionBody is a predicate assertion body.
Usage::- AssertionStatus exit AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:s_assrt_body/1)AssertionBody is a predicate assertion body.
The syntax of mode definitions is similar to that of pred declarations. For example, the following set of assertions:
:- modedef +A : nonvar(A) # "A is bound upon predicate entry.".
:- pred p(+A,B) : integer(A) => ground(B).
is equivalent to:
:- pred p(A,B) : (nonvar(A),integer(A)) => ground(B)
# "A is bound upon predicate entry.".
Usage::- modedef AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_body/1)AssertionBody is an assertion body.
Usage::- decl AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_body/1)AssertionBody is an assertion body.
Usage::- AssertionStatus decl AssertionBody.
- The following properties should hold at call time:
(assertions_props:assrt_status/1)AssertionStatus is an acceptable status for an assertion.
(assertions_props:assrt_body/1)AssertionBody is an assertion body.
Usage::- doc(Pred,Comment).
Documentation . This assertion provides a text Comment for a given predicate Pred.
- The following properties should hold at call time:
(assertions_props:head_pattern/1)Pred is a head pattern.
(assertions_props:docstring/1)Comment is a text comment with admissible documentation commands. The usual formatting commands that are applicable in comment strings are defined by stringcommand/1. See the lpdoc manual for documentation on comments.
Usage::- comment(Pred,Comment).
An alias for doc/2 (deprecated, for compatibility with older versions).
- The following properties should hold at call time:
(assertions_props:head_pattern/1)Pred is a head pattern.
(assertions_props:docstring/1)Comment is a text comment with admissible documentation commands. The usual formatting commands that are applicable in comment strings are defined by stringcommand/1. See the lpdoc manual for documentation on comments.
Documentation on exports
Usage:check(PropertyConjunction)
This assertion provides information on a clause program point (position in the body of a clause). Calls to a check/1 assertion can appear in the body of a clause in any place where a literal can normally appear. The property defined by PropertyConjunction should hold in all the run-time stores corresponding to that program point. See also Run-time checking of assertions.
- The following properties should hold at call time:
(assertions_props:property_conjunction/1)PropertyConjunction is either a term or a conjunction of terms. The main functor and arity of each of those terms corresponds to the definition of a property. The first argument of each such term is a variable which appears as a head argument.
Usage:trust(PropertyConjunction)
This assertion also provides information on a clause program point. It is identical syntactically to a check/1 assertion. However, the properties stated are not taken as something to be checked but are instead trusted by the compiler. While the compiler may in some cases detect an inconsistency between a trust/1 assertion and the program, in all other cases the information given in the assertion will be taken to be true. As a result, if these assertions are erroneous they can introduce bugs in programs. Thus, trust/1 assertions should be written with care.
An important use of these assertions is in providing information to the compiler which it may not be able to infer from the program (either because the information is not present or because the analyzer being used is not precise enough). In particular, providing information on external predicates which may not be accessible at the time of compiling the module can greatly improve the precision of the analyzer. This can be easily done with trust assertion.
- The following properties should hold at call time:
(assertions_props:property_conjunction/1)PropertyConjunction is either a term or a conjunction of terms. The main functor and arity of each of those terms corresponds to the definition of a property. The first argument of each such term is a variable which appears as a head argument.
Usage:true(PropertyConjunction)
This assertion is identical syntactically to a check/1 assertion. However, the properties stated have been proved to hold by the analyzer. Thus, these assertions often represent the analyzer output.
- The following properties should hold at call time:
(assertions_props:property_conjunction/1)PropertyConjunction is either a term or a conjunction of terms. The main functor and arity of each of those terms corresponds to the definition of a property. The first argument of each such term is a variable which appears as a head argument.
Usage:false(PropertyConjunction)
This assertion is identical syntactically to a check/1 assertion. However, the properties stated have been proved not to hold by the analyzer. Thus, these assertions often represent the analyzer output.
- The following properties should hold at call time:
(assertions_props:property_conjunction/1)PropertyConjunction is either a term or a conjunction of terms. The main functor and arity of each of those terms corresponds to the definition of a property. The first argument of each such term is a variable which appears as a head argument.