Skip to main content

Classes

A class describes a part of the system you model. It has four kinds of members: variables (its state), features (its parts), transitions (how its state changes), and properties (values computed from its state).

Declaring a Class​

Declare a class with the class keyword, followed by its members in braces:

class Counter {
var value: int := 0

tran increment() {
value := value + 1
}

prop isPositive(): bool {
return value > 0
}
}

A class without members can leave out the braces:

class Marker

Inheritance​

A class extends at most one other class, its superclass. Write the superclass after a colon:

class Counter {
var value: int := 0
}

class LimitedCounter : Counter {
var limit: int := 10

prop atLimit(): bool {
return value >= limit // value is inherited from Counter
}
}

The subclass inherits all members of its superclass. A class without a superclass extends Anything.

Abstract Classes​

Mark a class abstract when it is only meant to be extended. An abstract class can declare abstract transitions and properties, which have no body:

abstract class Shape {
abstract prop area(): int
}

class Square : Shape {
var side: int := 1

redefine prop area(): int {
return side * side
}
}

A class that is not abstract must redefine every abstract member it inherits. An abstract class cannot be a verification case.

Only an abstract class can have abstract members:

class Shape {
abstract prop area(): int // an error
}

Redefining Members​

To replace an inherited member, declare it again with redefine:

class Counter {
var value: int := 0

tran increment() {
value := value + 1
}
}

class DoubleCounter : Counter {
redefine tran increment() {
value := value + 2
}
}

A redefinition takes the same number of parameters as the member it replaces. The type of a redefined property or feature must be the inherited type or a more specific one. Variables cannot be redefined.

Without redefine, a member with the same name does not replace the inherited one. It only hides its name, and you get a warning:

class Counter {
tran increment() {
}
}

class DoubleCounter : Counter {
tran increment() { // a warning
}
}

Inherited members that use the name still use the inherited member. A verification case without redefine on its init, tran, or prop still runs and checks the inherited ones.

The Anything Base Class​

Every class extends Anything from the standard library. Anything has an empty init and tran, and a prop that always holds:

class Anything {
tran {
}

init {
}

prop {
return AG true
}
}

A verification case is a class marked @VerificationCase. Verifying it runs its init once, then its tran again and again, and checks its prop. Redefine them to give the case its own behaviour and property.

Accessing Members​

Inside a class, use a member by its name, as in value. Use a period to reach a member of another instance, as in counter.value. self is the current instance, so self.value reaches the member even when a parameter has the same name:

class Counter {
var value: int := 0

tran set(value: int) {
self.value := value // self.value is the variable, value is the parameter
}
}

class Holder {
contains counter: Counter

prop current(): int {
return counter.value
}
}

Reading a member through a variable that holds nothing has no value, and the operation blocks.