Skip to content

Improve BitPack instances with undefined bits #353

@basile-henry

Description

@basile-henry

Now that BitVectors contain a bitmask for which bits are defined, we should be able to improve some of the BitPack instances (mainly Vec).

For example, we could expect these two values to pack the same way:

Clash.Prelude> pack (Nothing :: Maybe Bit)
0.
Clash.Prelude> pack (0 :> undefined :> Nil :: Vec 2 Bit)
*** Exception: X: undefined
CallStack (from HasCallStack):
  errorX, called at <interactive>:5:12 in interactive:Ghci1

Which would allow us to do things like:

Clash.Prelude> bitCoerce (0 :> 1 :> Nil :: Vec 2 Bit) :: Maybe Bit
Nothing
Clash.Prelude> bitCoerce (0 :> undefined :> Nil :: Vec 2 Bit) :: Maybe Bit
*** Exception: X: undefined

Similarly we could get expressions like bv2v . v2bv to be closer to id even in the presence of undefined bits:

Clash.Prelude> head $ id (1 :> undefined :> Nil :: Vec 2 Bit)
1
Clash.Prelude> head $ (bv2v . v2bv) (1 :> undefined :> Nil :: Vec 2 Bit)
*** Exception: X: undefined
CallStack (from HasCallStack):
  errorX, called at src/Clash/Explicit/Prelude/Safe.hs:237:13 in clash-prelude-0.99-5tD8YyKlskCDMkI1EAcLIV:Clash.Explicit.Prelude.Safe
  undefined, called at <interactive>:15:28 in interactive:Ghci2

@christiaanb suggested to change the following:

pack# :: Bit -> BitVector 1
pack# (Bit m b) = BV m b

To:

pack# :: Bit -> BitVector 1
pack# b = case maybeX b of
  Just (Bit m b) -> BV m b
  Nothing -> undefined#

One thing to note here, even if we manage to fix bv2v . v2bv == id, we wouldn't be able to recover the msg from a errorX msg through that expression. The errors would be represented as single bits, so all error messages would be "forgotten".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions