blarney-lang/blarney

Have `when` work also not within `always`

Opened this issue · 2 comments

I think it would be nice to have when work as a when :: Bit 1 -> Action () -> Module () on top of just when :: Bit 1 -> Action () -> Action (). I'd like to write something like

top = do
  cycleCount :: Reg (Bit 8) <- makeReg 0
  -- Increment cycleCount on every cycle
  always do
    cycleCount <== cycleCount.val + 1
  -- Terminate simulation when count reaches 20
  when (cycleCount.val .==. 20) do
    display "Finished"
    finish

instead of

top = do
  cycleCount :: Reg (Bit 8) <- makeReg 0
  always do
    -- Increment cycleCount on every cycle
    cycleCount <== cycleCount.val + 1
    -- Terminate simulation when count reaches 20
    when (cycleCount.val .==. 20) do
      display "Finished"
      finish

or

top = do
  cycleCount :: Reg (Bit 8) <- makeReg 0
  -- Increment cycleCount on every cycle
  always do
    cycleCount <== cycleCount.val + 1
  -- Terminate simulation when count reaches 20
  always do
    when (cycleCount.val .==. 20) do
      display "Finished"
      finish
mn416 commented

Yeah, library seems happy if we introduce

class When m where
  when :: Bit 1 -> Action () -> m ()

with instances for Module and Action.

Just wondering if there are any annoying cases when the type is no longer inferred. The other option is to have a new function, e.g. on, or whenever.

I think I prefer the same name but I don't know how likely it would be for the type not to be inferred correctly...
<troll>if only Action and Module where the same</troll>