07Implementation
Expose private state through a method
Read the starter
This package explicitly exports Tally(..) and mkTally. A caller can
instantiate mkTally and use the increment and value methods of its Tally
interface. The count register inside mkTally is private, so no caller can
read it directly.
Complete the value method at the TODO so the private register reaches the
outside through the interface. The public behavior must expose the initial
value and each committed increment through value.
Before editing, find the interface declaration, the private register, and the
two method implementations. Predict what Run can display while value is a
don't-care expression. The source may compile, but that does not establish a
stable observation contract.
Repair path
Change only the expression owned by the value method. Do not add public state
or duplicate count; the existing register is already the source of truth.
After the edit, run the public simulation and explain why three accepted
increments become visible through a combinational value method.
Completion contract
- Return the current
countfromvalue. - Leave
countprivate to the module. - Do not change the
incrementmethod. - Preserve the initial observation and each subsequent state transition; a hard-coded final value is not sufficient.
Progressive hints
Hint 1
The public method should observe existing state, not create another register.
Hint 2
A value method may be written as a single expression after =. Find the
private declaration whose committed value must cross the interface.
Concept
A package's public interface and its private state are separate things. State declared inside a module is invisible outside it, so a value method is the window that decides what callers may observe.
Transfer experiment
After the required fix, change the public driver to perform one or five increments. Predict the final observation before running. This is not part of grading; it checks that the method reports state rather than a value tailored to one sample run.