In general objects may have state that may change over time during the program execution. The data-items representing the state of an object are constants and/or variables. A constant denotes the same object during the life-time of the enclosing object - the state of a constant object may, however, change over time. A variable may refer to different objects during the life-time of the enclosing object - and again, the state of these object may vary over time.
At the basic level, the state of an object is represented by the values of the constants and variable data-items. A value is a timeless concept that does not change over time. The number 7 is an example of a value.
There is thus a fundamental difference between objects that may change state and values that are timeless concepts. The support for objects and values in programming languages differ between languages. The rationale behind objects and values in Beta and qBeta is further described in the section on Objects and Values. In a program execution, values must be represented by objects or other data-structures. In qBeta, values are represented by immutable objects that are objects that does not change state over time.
The qBeta library contains a pattern Value that is a subpattern of Object. Patterns that subpattern from Value describes immutable objects.
As an example of a pattern describing immutable objects, consider the following definition of Point:
Point(x: ?integer, y: ?integer): Value
= p1: ? Point -> p2: ? Point
p2 := (x = p1.x) and (y = p2.y)
move(dx,dy: ?integer) -> p: ?Point
p := Point(x + dx,y + dy)
Point is a subpattern of Value. Point has two arguments, x, and y that may be supplied when a Point objects is generated. In the code snippets below, the constant object a is initialized to the Point(2,3), and the variable Point b is assigned the new object Point(3,4). Both of the objects Point(2,3) and Point(3,4) cannot change state after they have been generated. Finally the assignment b := a implies that a and b now refer to the same Point object -- as would be expected.
a: = Point(2,3)
b: ? Point
b := Point(3,4)
b := a
Point has two local patterns (methods), pattern = that defines equality for a Point, and pattern move that generates a new Point moved a certain distance from the Point. Examples of use is shown below:
%if a = b %then b := a.move(2,4)
The basic values such as Integer, Char, and Real are all subpatterns of Value and thus immutable.The pattern Integer is defined in the following way:
+ V: ?Integer -> res: ? Integer
= V: ?Integer -> res: ? Boolean
The argument V of Integer is thus the value of the Integer object being integrated. An expression Integer(7) thus generates an immutable object representing the value 7.
x: = Integer(7)
y: ? Integer
y := Integer(12)
y := x
The value x is a constant object representing the value 7. The variable y may refer to (immutable) Integer objects and may be assigned new values as in y := Integer(12), and y := x
In general numbers like 7 and 12 correspond to Integer(7) and Integer(12) respectively - the example above may thus be written as:
x: = 7
y: ? Integer
y := 12
x := y
this is of course a self-recurring definition that cannot be described in qBeta without special handling of the compiler. This is also the case for other basic values such as Char and Real.
The pattern Value is defined using the subpattern restriction immutable - for a precise description of immutable, see the section on Subpattern Restrictions.