Logo BCDDLogo CSC
BCDDBehavioural Contract-Driven Design
version 1.0-beta
ITEN

Chapter 21 — The step and the first formulas

The guarantees of the staircase light CSC are written in words: they are checked by judgement. A guarantee written as a formula, by contrast, has a meaning that a program can check. This chapter introduces the step, that is the unit with which a formula observes the system. It then introduces the first keywords of formulas and the operators with which conditions are combined.

bcdd recognises the formulas of this chapter and of the two that follow: it checks their form, names and types, and reports their degree. It does not evaluate them on the scenarios: chapter 24 describes what remains to be checked.

The step

A step is the unit of observation of the system: an instant, with the set of events that occur in it and the value of the states immediately after those events. Every instant of a scenario (chapter 9), that is every line that begins with @, is a step, and the trace of a scenario is a sequence of steps. A step respects three rules:

  1. the instant of a step does not precede that of the previous step; two consecutive steps may have the same instant, like the two instants @0 s of scenario s4;
  2. in a step the same event appears at most once;
  3. the value of a state in a step is the value the state takes after the events of the step.

In scenario s1 the first step is @0 s premuto modo = accesa: the event premuto occurs at instant 0 and, after the press, the mode is accesa.

The step is a model of observation, not a description of the implementation: it does not imply that the implementation does everything at the same instant.

The formula

A formula is a guarantee written in a form that a program can check step by step. It is written after the colon of the line, in place of VERBAL and VERIFY:

  GUARANTEE g1: WHENEVER premuto THEN modo <> spenta AT ONCE

A formula has no VERIFY line: the way to check it is established by the language, not by whoever writes the CSC.

Inside a formula appear conditions on the step, that is expressions that in every step are true or false. When a condition is true in a step it is said to hold in that step; for an event it is said instead to occur.

Condition Holds in the step in which
premuto the event premuto occurs
modo = accesa the state modo has the value accesa
modo <> spenta the state modo has a value other than spenta
modo IN {accesa, in_pulizia} the state modo has the value accesa or in_pulizia
NOT p p does not hold
p AND q p and q both hold
p OR q at least one of p and q holds
p IMPLIES q p does not hold, or q holds

In the forms of this chapter and of the two that follow, the lower-case letters p, q and c denote conditions on the step and the upper-case P a formula; from chapter 22 onwards e denotes an event, x a state, X a request and d a duration.

The phases of the lifecycle of a request (chapter 5) are conditions too: requested(Pulizie) holds in the step in which the request Pulizie reaches the phase requested.

A formula cites only elements declared in the CSC (events, states, values of lists of values, requests, named constants), numbers and durations.

Operators and precedence

Besides the logical operators, a condition uses the comparison and calculation operators:

Operator Meaning
=, <> equal, not equal
<, <=, >, >= less than, less than or equal, greater than, greater than or equal; between numbers or between durations
+, -, *, / sum, difference, product, quotient
^ power
- x the opposite of x
x IN {v1, v2, …} x has one of the listed values
(IF c THEN x ELSE y) x in the steps in which c holds, y in the others; always written in parentheses

The mathematical functions ABS, MIN, MAX, LOG10, ROUND, FLOOR and CEIL have their mathematical meaning. The minus sign between two values is written separated from both: a - b. Written without spaces, a-b is a single name, because a name may contain a hyphen between two letters.

Without parentheses the operators apply according to this precedence, from the highest to the lowest:

Precedence Operators Consecutive identical operators
1 x[i], x.campo (chapter 23) left-associative
2 ^ not admitted: a ^ b ^ c is an error
3 - before a value right-associative
4 *, / left-associative
5 +, - left-associative
6 =, <>, <, <=, >, >=, IN not admitted: a <= b <= c is an error
7 NOT right-associative
8 AND, OR the same operator repeats freely; AND and OR mixed are an error
9 IMPLIES not admitted: a IMPLIES b IMPLIES c is an error

Thus NOT a AND b is (NOT a) AND b, a AND b IMPLIES c is (a AND b) IMPLIES c and x + y * 2 is x + (y * 2). For the combinations that the table excludes bcdd reports error E26; the message indicates how to write them with parentheses. a AND b OR c is written (a AND b) OR c or a AND (b OR c), depending on the intended meaning. The examples in the manual use parentheses whenever a condition combines more than two conditions.

The first keywords of formulas

A formula is built with four keywords, which establish in which steps a condition must hold. A fifth, AT ONCE, specifies the step of the consequence of WHENEVER.

“Always”: ALWAYS p. In every step p holds. ALWAYS modo <> in_pulizia states that the light never enters the cleaning mode.

“Sooner or later”: EVENTUALLY p. In at least one step p holds. EVENTUALLY avviso states that in some step a warning occurs. Written as a guarantee on its own, it would require a warning in every trace. The light, however, issues a warning only after a press and is not required to issue one if nobody presses.

“Whenever… then”: WHENEVER p THEN q. In every step in which p holds, the condition q must hold in the same step or in a later step. The condition p is the trigger, the condition q is the consequence. With AT ONCE the consequence must hold in the same step as the trigger, and not later: WHENEVER p THEN q AT ONCE.

“While”: WHILE c: P. The formula P must hold in the steps in which the condition c holds, called the scope; the other steps are not considered. WHILE modo = spenta: ALWAYS NOT avviso states that in no step in which the light is off does a warning occur. A formula without WHILE concerns all the steps of the trace.

These keywords do not nest: after ALWAYS, EVENTUALLY, WHENEVER and THEN a condition on the step is written, not another formula. A formula has at most one scope, written at the beginning. For a nested keyword or a second scope bcdd reports error E27.

A formula on several lines

A long formula can be split over several lines. A line continues on the next line when it has been left open: it has an unclosed parenthesis, or it ends with an operator or a keyword that requires a continuation, such as THEN, AND, OR, IMPLIES, WITHIN, a comma, a comparison or calculation operator. For example:

  GUARANTEE g1: WHENEVER premuto THEN
                  modo <> spenta AT ONCE

The two lines are a single declaration. A line that begins with a keyword that opens a declaration (GUARANTEE, SCENARIO, END, …) or with @ never continues the previous one: a forgotten parenthesis does not absorb the END line of the block into the declaration.

The staircase light CSC with the first formulas

Guarantee g1, “when someone presses the button, the light switches on”, becomes:

  GUARANTEE g1: WHENEVER premuto THEN modo <> spenta AT ONCE

It reads: whenever premuto occurs, in the same step the mode is other than spenta. The consequence is not modo = accesa: during cleaning a press leaves the mode in_pulizia (guarantee g7), and the light is on all the same. The same consequence can also be written modo IN {accesa, in_pulizia}.

Guarantee g2, “the light switches off by itself after a while”, becomes:

  GUARANTEE g2: WHENEVER modo = accesa THEN modo = spenta

In every step in which the light is on, in a later step the light must be off. The time remains indeterminate, as in the text in words; the time limit is in the detail g2a (chapter 22).

A new guarantee, g8, which uses WHILE, is also added:

  GUARANTEE g8: WHILE modo = spenta: ALWAYS NOT avviso

A guarantee written as a formula changes form, but keeps its name: g1 remains g1, and the details g2a and g2b remain details of g2. The change of form modifies the text of the approved CSC, so it requires a new version (chapter 10): 0.3, pending approval. The text in words can be kept in a comment above the formula. Lines 1-2 and 15-18 of version 0.3 are:

-- The staircase light, as seen by the person on the stairs and by the caretaker.
-- VERSION 0.3: g1 and g2 written as formulas; g8 added. Pending approval.
  -- g1: «When someone presses the button, the light switches on»
  GUARANTEE g1: WHENEVER premuto THEN modo <> spenta AT ONCE
  -- g2: «The light switches off by itself after a while»
  GUARANTEE g2: WHENEVER modo = accesa THEN modo = spenta

The line of g8 is line 33, immediately before the assumption a1.

The degree of formulas and the report

The degree of a guarantee (chapter 2) is derived from the form of the line: no line declares it. A guarantee written as a formula, which bcdd recognises in full, has formal degree:

$ bcdd check UsoLuce.csc
bcdd check UsoLuce.csc
lines: 50 (recognised 41, comments 4, blank 5, unrecognised 0)
blocks: 1
  line 4: CSC UsoLuce VERSION 0.3, STATUS HYPOTHESIS

1. ERRORS: 0

2. DEGREE OF THE CONTRACTUAL LINES
  verbal: 8
  formal: 3
  declared extension: 0
  unrecognised: 0
  line 16: GUARANTEE g1 formal
  line 18: GUARANTEE g2 formal
  line 19: GUARANTEE g2a verbal
  line 21: GUARANTEE g2b verbal
  line 23: GUARANTEE g3 verbal
  line 25: GUARANTEE g4 verbal
  line 27: GUARANTEE g5 verbal
  line 29: GUARANTEE g6 verbal
  line 31: GUARANTEE g7 verbal
  line 33: GUARANTEE g8 formal
  line 34: ASSUME a1 verbal

3. UNDECIDED: 0

4. PENDING APPROVAL, NOT CONTRACT: 9
  line 16: GUARANTEE g1
  line 18: GUARANTEE g2
  line 23: GUARANTEE g3
  line 25: GUARANTEE g4
  line 27: GUARANTEE g5
  line 29: GUARANTEE g6
  line 31: GUARANTEE g7
  line 33: GUARANTEE g8
  line 34: ASSUME a1

5. UNRECOGNISED LINES: 0

6. UNDECLARED TYPES: 0

7. CSCs NAMED BUT NOT CHECKED: 0

14. TO BE JUDGED: 2
  line 19: GUARANTEE g2a DETAILS g2: consistency between the line and the line that details it, one of the two in words
  line 21: GUARANTEE g2b DETAILS g2: consistency between the line and the line that details it, one of the two in words

15. CHECKS NOT EXECUTED: 3
  scenarios: lines 37, 42, 45, 47
  coverage of the triggers: lines 16, 18, 33
  consistency of assumptions and guarantees: lines 16, 18, 33

outcome: no errors

The three formulas have formal degree. In section 4 they appear as lines pending approval, because version 0.3 is pending approval; the details g2a and g2b, by contrast, have their own STATUS RATIFIED BY D-12.

Section 14, TO BE JUDGED (chapter 8), lists the two details: they are written in words and make a formula precise, and their consistency with g2 has to be judged by a person.

Section 15, CHECKS NOT EXECUTED, lists the checks on formulas that bcdd does not execute, with the lines to which they would apply:

A line of section 15 is neither satisfied nor violated: the check has not been executed. The report shows it so that no formula appears to have undergone checks that were not executed. Chapter 24 describes these checks. The section appears only when it has at least one entry.

Summary

Special cases

A line that begins with THEN. If g1 breaks the line before THEN, the first line does not require a continuation, and the line that begins with THEN is a declaration on its own. Neither of the two lines is recognised, and g1 has the degree unrecognised:

5. UNRECOGNISED LINES: 2
  line 16: GUARANTEE g1: WHENEVER premuto
  line 17: THEN modo <> spenta AT ONCE
…
outcome: no errors; unrecognised lines: 2 (see section 5)

The line must be broken after THEN, as in the example of the section “A formula on several lines”.

Undeclared name in a formula. With premuta in place of premuto, bcdd reports error E24, undeclared name:

1. ERRORS: 1
  line 16: [E24] name not declared: premuta

The line remains recognised and formal: the form is correct, the name is not. Each position in a formula requires an element of a certain kind (an event, a state, a request, a value). For a name of a different kind, too, bcdd reports error E24.

AND and OR without parentheses. The consequence modo = accesa OR modo = in_pulizia AND NOT avviso mixes AND and OR:

1. ERRORS: 1
  line 16: [E26] AND and OR together without parentheses: write (a AND b) OR c, or a AND (b OR c)

A formula inside another. ALWAYS (modo = spenta IMPLIES ALWAYS NOT avviso) contains a formula in place of a condition:

1. ERRORS: 1
  line 33: [E27] ALWAYS inside another form: the language does not nest ALWAYS, EVENTUALLY, WHENEVER and their shorthands

The correct form is that of g8, with the scope WHILE.

Approval of a CSC with formulas. The file passes bcdd check --strict, and bcdd ratify records its approval as for a CSC with guarantees in words only. On a copy of version 0.3:

$ bcdd ratify UsoLuce.csc --all D-15
approved with D-15 in CSC UsoLuce: 1 line
  line 5: CSC UsoLuce
contract: 11

In this chapter version 0.3 remains pending approval, because chapters 22 and 23 add further formulas to it.

The formula as a detail. As an alternative to replacement, the guarantee in words remains and the formula is added as its detail (chapter 8):

  GUARANTEE g1: VERBAL "When someone presses the button, the light switches on"
                VERIFY BY JUDGEMENT "a person who presses the button and watches"
  GUARANTEE g1a DETAILS g1: WHENEVER premuto THEN modo <> spenta AT ONCE

Both forms are admitted. With the detail the contract contains both lines, and the detail is pending approval. In the report g1a is formal, and section 14 lists its consistency with g1 among the lines to be judged:

14. TO BE JUDGED: 3
  line 17: GUARANTEE g1a DETAILS g1: consistency between the line and the line that details it, one of the two in words
  line 20: GUARANTEE g2a DETAILS g2: consistency between the line and the line that details it, one of the two in words
  line 22: GUARANTEE g2b DETAILS g2: consistency between the line and the line that details it, one of the two in words

Formula in an entity. An entity guarantee can be written as a formula. The elements are named with the qualified name Csc.elemento, as in entity scenarios (chapter 18). For example, in the entity LuceScale the following guarantee

  GUARANTEE ls1: WHENEVER Pressione.premuto THEN Luce.modo <> spenta WITHIN 1 s

has formal degree:

2. DEGREE OF THE CONTRACTUAL LINES
  verbal: 2
  formal: 1
  declared extension: 0
  unrecognised: 0
  line 15: GUARANTEE ls1 formal
  line 16: GUARANTEE ls2 verbal
  line 18: GUARANTEE ls3 verbal

Pressione and Luce must be CSCs that the entity provides or uses (otherwise: error E20), and the element must be declared in that CSC (otherwise: error E24). For a simple name, without the CSC in front, bcdd also reports error E24. In this version bcdd does not check the type of the elements in entity formulas, but only the name and the kind.