Exercise · Volume 1 · 2.2.5-11
Exercise 2.2.5-11
not stated yet in Lean solution written
| Rating | 21 |
|---|---|
| Kind | K5, design an algorithm: give a method for a task, often with a cost bound from the written solution |
| Section | Section 2.2.5 of Volume 1 |
The task
Design a method for synchronous simulation in which the variables change together at the end of a step, using the tables and , and give an error stop when a variable gets two values in the same step.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Remember in a list which variables were changed in the step.
Hint 2
Use to mean that the variable has not been changed.
Solution
Show the solution
Initially for all and . Values are .
Assignment of a new value to during a step. If , then already has a new value in this step, so stop with an error. Otherwise set , and . The old value of stays in , so the other assignments of the step still see the old value.
End of the step. Set and . While : set , , , .
The list through holds exactly the variables changed in the step. The first assignment sets a link that is not , since it is either or a variable number, so the test of the second assignment works. The time of a step is proportional to the number of variables that change, and not to .
If the variables are scattered in memory, the same method works with a field and a field in the node of each variable, and with pointing at a node.
Check
I ran this method on 2000 random steps with 8 variables. The result matched a copy of the old values updated by the same assignments, the number of list nodes equalled the number of assigned variables, and all links were after the step.
Remarks
The error for a double assignment is only raised when two different assignments are made. If the same value is given twice, the method still reports it, and one may weaken the test to compare the values.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.