# CPSC 333: Lab Exercise #12: Simple Schemas

Location: [CPSC 333] [Lab Exercise] [Previous Exercise] Exercise #12

The following exercise will be taken up in labs during the week of April 14-17. If at all possible, you should try to solve these problems ahead of time, so that you can compare your own solutions to the ones presented during labs, or have questions ready to ask in case you had trouble solving them.

This is based on material presented in class on April 11, 1997.

1. Extend the ``Counter'' example used in lectures by writing schemas for the following additional functions.

1. A function ``Increment_Counter.'' This should increase the counter's value by one, unless that would take outside the counter outside the allowed range (by making it larger than max_value). If the change would cause the value to be too large then the value should not be changed at all. The function will not require any inputs, and it should use an output ``new_value!'' to report the new value for the counter.

2. A function ``Decrement_Counter.'' This is the same as ``Increment_Counter, except that it decreases the counter's value by one if it is legal to do so, and makes no change otherwise.

3. A function ``Report_Counter_Sign,'' which inspects the counter's value, without changing it, and returns an output variable ``sign!'' value positive if the counter's value is currently strictly greater than zero, the value zero if the counter value is equal to zero, and the value negative otherwise.

One way (in Z) to declare that the variable sign! should have one of the above three values is to define its type using the declaration

sign! : { positive, zero, negative }

2. Now, write a set of schemas that define a traffic light controller, similar to one that has previously been described, and whose behaviour has been specified using a state-transition diagram, an augmented data flow diagram, and process specifications.

Like the traffic light included in previous examples, this should have four possible states: off, red, green, or yellow. (You can use the same kind of declaration to define of a light, as was used at the end of the first question to define the the type of the output value ``sign!''.)

Three functions that access or modify the state of the traffic light:

• ``Turn_On_or_Off'' should cause the state to change from off to red or from any other state (red, green, or yellow) to off. No inputs are required, but an output will be used: This function should return the traffic light's new state as the value of an output variable, ``new_state!''.
• ``Change Colour'' should cause the state to change from red to green, from green to yellow, or from yellow to red, and should have no effect (that is, make no change) if the light is off. This function will also require no inputs and will use an output variable, ``new_state!'', to report the new state of the light.
• ``Report State'' should cause no change, but should report the current state of the light, using an output value ``current_state!''

Write the schemas required to define a ``traffic light'' object and the above functions, that modify or access it. Try to use the `conventions for schema decoration, naming, and inclusion, that were defined in class, to avoid having to write the same definitions in schemas over and over again.

Location: [CPSC333] [Lab Exercises] [Previous Exercise] [Exercise #12]

Department of Computer Science
University of Calgary

Office: (403) 220-5073
Fax: (403) 284-4707

eberly@cpsc.ucalgary.ca