Skip to content
code0.HDL

25Implementation

Implement a typeclass instance

Problem

Priority declares the overloaded operation priorityOf. The starter has working Priority#(Bool) behavior, but Priority#(RequestKind) returns zero for every label. Implement the semantic order from lowest to highest: Refresh, Read, Write, then Reset.

Edit only priorityOf inside Priority#(RequestKind). Preserve the class, Bool instance, polymorphic requestPriority function and proviso, and module structure.

Read and predict before editing

Read the starter in four passes:

  1. Note the enum order: Reset, Read, Refresh, Write.
  2. Read the class member's input and two-bit result.
  3. Compare the complete Bool instance with the unfinished instance.
  4. Trace the Write call through requestPriority and its proviso.

Before editing, predict the public run. Write selects Priority#(RequestKind) at compile time, but the placeholder produces 0. The code compiles because the instance exists with the right member type; its behavior is wrong. The repaired run must show 2.

Make the minimum implementation

Implement a total mapping for all four labels inside the existing member. Do not use pack(value): semantic order differs from enum declaration order. A case makes each label explicit, but completion depends on behavior rather than one syntax form.

Reason through these observations:

  • Refresh, Read, Write, and Reset produce 0, 1, 2, and 3;
  • False and True still produce 0 and 3 through Priority#(Bool); and
  • both types still use the unchanged requestPriority body.

Completion requires every label's priority and existing Bool dispatch to remain correct. Handling only the displayed Write is incomplete. Do not add a runtime selector: the argument type resolves the instance before simulation.

Progressive hints

Hint 1: locate the defect

The proviso and instance header are valid. Repair the concrete member, not generic dispatch or instance resolution.

Hint 2: choose the discriminant

Branch on RequestKind and account for every label. Use the semantic priority table rather than each label's encoded position.

Hint 3: check totality

There are four labels and four required priorities. Check each once and confirm the two-bit result represents 0 through 3.

Transfer experiment

Temporarily change the displayed argument in mkTop from Write to True. Predict 3, run once, and restore it. The same generic function should select the existing Bool instance without proviso changes or runtime dispatch.

Review this concept
Ready
Main.bsv