Operators, Functions, and Properties – part 18

In the latest post in this series, we looked at modeling a simple four-function calculator as a state machine where key presses invoke operators that operate on the current state of the machine, producing a new state.

Missing from this diagram is the screen, as well as detail as what happens in each of the operators.  And though we included in the state the three components listed, we don’t know for sure if this will account for all the stuff that needs to be kept around in some way for the calculator to work.

Filling in some details about the screen is not that hard; we pretty much saw already that the number displayed on the screen matches the component we’ve called the RightValue.  When you enter a number on the calculator, say 314, that number is displayed; when you then enter “+”, the number 314 still shows, but will disappear as soon as you start entering the second number.  As soon as you enter the second number, the first number feels like it got squirreled away somewhere; and this component we’ve called the LeftValue.  The fact that the “+” key was pressed itself – which is not visible on the screen either, must have been stored somewhere also, and this is the component we’ve called PendingOp.  So it appears that part of what happens when the “+” is pressed is that the value currently in RightValue is stored into LeftValue and that RightValue is made available for the next number to be entered.  “Made available” is a bit of a cop-out, in that the window doesn’t reset to zero at the moment the “+” is pressed, but the effect is delayed till a digit key is pressed, and the truth is that right now we don’t have a way to model that.  To do so, we need more state.  It seems like a new state component NumberStarted would deal with that, a component with two possible values “yes” and “no”.

Since we will often use an operator that selects a component from the overall state, it is convenient to introduce some way to indicate this in a diagram.  Similarly, it will be useful to have an easy way to show that a particular component of the state has been updated to a new value.

Compared to the diagram at the beginning of the post, you can see that we have added a component to the state.  On the right, we have made a partial attempt to show what happens if the “2” key gets pressed.  The entire state is shown as input to the “.RightValue” selection operator.  This operator selects the indicated component of the state, and this component is now the input to the “× 10” box.  For convenience, we also show the state, unaltered, being passed through the selector operator, coming out at the bottom.  The “.RightValue” selection operator at the top looks a lot like the “.RightValue” update operator at the bottom, and they only differ in the direction of the arrows.  For the update operator at the bottom, the new value of the .RightValue component is shown coming in from the right, and being combined with the old value of the state to form the new value of the state.  The idea of the update operator is that all components are left alone, while the named component is being replaced by the new value.

To be specific, if we assume that the current state has the value { LeftValue=13, PendingOp=”+”, RightValue=”43″, NumberStarted=”yes” }, and that the “2” key is pressed, then the .RightValue selector would be invoked, and it would produce “43” as its value on the right; this value then goes into the “× 10” box, producing “430” as its output; this value goes into the “+ 2” box, producing “432” as its output.  This output, “432”, then goes into the .RightValue update operator, which takes the current state and replaces the RightValue component with “432”, resulting in the new state with the value { LeftValue=13, PendingOp=”+”, RightValue=”432″, NumberStarted=”yes” }.  And indeed, if the RightValue was 43 showing on the sceen, and the “2” key was pressed, we would then expect to see “432” on the screen.

The diagram above shows a variant of the component update operator.  The simplest way to think of it is as a chain of two update operators, each dealing with a single component.  In this view, the top update operator changes the value of the RightValue component of the state, which then feeds into the bottom update operator, which in turn changes the value of the NumberStarted component.  In this view, we merely dispense with the arrow between the .RightValue component update operater and the .NumberStarted component update operator.   An alternate way of thinking about it is as a single update operator that updates multiple components.

These selection operators and update operators are useful and interesting in their own right, and in addition they are useful in chaining together the various operators that model each of the key presses in our four-function calculator.

We will continue this in the next post.

Advertisements
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

3 Responses to Operators, Functions, and Properties – part 18

  1. Pingback: Operators, Functions, and Properties – part 19 « Learning and Unlearning Math

  2. Pingback: Operators, Functions, and Properties – part 22 « Learning and Unlearning Math

  3. Pingback: Operators, Functions, and Properties: The Series « Learning and Unlearning Math

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s