2. Modeling text editor in Z
We'll model a simple text editor. All you can do with this editor is type in text,
move the cursor backwards and forwards through the text, and delete the
character in front of the cursor
3. Basic type and abbreviation type
We declare a basic type: the set of all characters. Then we make an
abbreviation definition to say that a text is a sequence of characters. [CHAR]
TEXT = seq CHAR
CHAR is a full-fledged Z data type. In Z we can introduce a new data type just by
writing its name inside brackets . From now on we can use CHAR in declarations,
just like N and all the other predefined data types. The abbreviation definition
introduces another new data type, TEXT, a sequence of characters. We can use
seq to define a sequence of any type. Sequences and their operators are defined
in the mathematical tool-kit
5. State schemas
The contents of a system's memory are called its state. Schemas model states as collections of
state variables and their values.
A text editor does have memory: It stores the text you type and the changes you make. The
state of our text editor is very simple: We have a document with a cursor. The document is a
text (a sequence of characters) that is no larger than our upper limit. We model the document
as two texts: left is the text before the cursor, and right is text following it.
7. Initializing schemas
In Z this state is described by a schema conventionally named Init. For
example, we might say that our editor always starts up with an empty
document:
8. The Init schema includes the Editor schema in its declaration section. This
indicates that all the declarations and predicates in Editor apply to Init as
well, so Init can use the local state variables right and left from Editor.
9. Operation schema
We need to model another aspect of state: change. Our editor starts up
empty, but it fills with text as the user types, and its contents change as the
user edits. To model this kind of activity, Z provides the operation schema.
the Insert operation that puts a single character in the document to the left
of the cursor.`
11. Operation schema cont…
Delta Editor (pronounced delta editor) tells us that Insert is an operation
schema that changes the state of Editor.
operation schema declares the input variable ch?
The unprimed variables left and right denote the texts to the left and right of
the cursor before the Insert operation, and the primed variables left’ and
right’ denote those texts after the operation.
12. Operation schema cont…
The first line of the predicate, ch?E printing, is a precondition: It describes
what must be true before the operation can occur. Sometimes preconditions
are called entry conditions. This precondition says that the Insert operation
can only occur when the input is a printing character; it uses the set
membership operator e (pronounced in). The rest of the predicate is a
postcondition: It describes the state of the editor after the operation. The
line left' = left^ (ch?) says that the new character is appended to the end of
the text preceding the cursor (in other words, it is inserted to the left of the
cursor). Here again we use the concatenation operator. The next line right' =
right says that the text following the cursor does not change. In Z it is
necessary to say when things remain the same.
13. Implicit precondition
The next operation moves the cursor forward one character. In many editors,
the user invokes this operation by pressing the right arrow key | -> | on the
workstation keyboard. We use an axiomatic definition to declare this right-
arrow character. We have to include a predicate that uses the set
nonmembership operator £ to say that rightuarrow is not a printing character;
otherwise our editor would try to insert the arrow character into the file,
instead of interpreting it as a command.
The implicit precondition of Forward is that the cursor is not at the end of the
document: right =/= ().
14.
15. Schema calculus
We will define a total version of Forward that works in all situations. We'll
define it in pieces, where each piece is a schema. Then we'll use the schema
calculus to put the pieces together. This is the usual way to define complex
operations in Z.
First we define a state schema to describe the end of file condition where the
cursor is at the end of the document.
16. Schema calculus cont…
In Z it is necessary to say when nothing happens. "^Editor is the operation on
Editor that does not change the value of any state variable. We make
rightarrow into a schema by defining RightArrow
17. Schema calculus cont…
This formula says that T.Forward behaves as the Forward operation when the
cursor is not at the end of the file, but pressing the right arrow key when the
editor is in the EOF state has no effect.