blarney-lang / blarney

Haskell library for hardware description
Other
98 stars 11 forks source link

Packing sum-types #31

Open christiaanb opened 5 years ago

christiaanb commented 5 years ago

More a suggestion than an issue: we recently implemented packing of sum(-of-product) types in Clash by separating the process of packing fields from packing the constructor. See:

  1. The default methods for the Pack class: https://github.com/clash-lang/clash-compiler/blob/6422262599ebf53b4bf97d44a5b063c92d3ff81b/clash-prelude/src/Clash/Class/BitPack.hs#L73-L131

  2. The Generic GBitpack class: https://github.com/clash-lang/clash-compiler/blob/6422262599ebf53b4bf97d44a5b063c92d3ff81b/clash-prelude/src/Clash/Class/BitPack.hs#L273-L300

  3. The generic instances for sum types: https://github.com/clash-lang/clash-compiler/blob/6422262599ebf53b4bf97d44a5b063c92d3ff81b/clash-prelude/src/Clash/Class/BitPack.hs#L309-L338

Perhaps/hopefully these ideas can be easily converted to blarney. Although you would then also need first-class patterns (as in e.g. http://hackage.haskell.org/package/first-class-patterns) for sum-types to be useful in EDSLs (otherwise you can't "eliminate" sum types). However, with GHC 8.8 you'll get source plugins, by which perhaps you can convert Haskell case-statements to first-class patterns.

mn416 commented 5 years ago

Hi @christiaanb,

Many thanks for the links. Nice to see that packing a sum type works with generic deriving, but to be useful I think we'd also need unpack, which is much harder. For example, makeReg :: Bits a => Module (Reg a) in Blarney needs pack (so that the typed value can be fed into a verilog register) and also unpack (so that the output of the verilog register can be viewed in its typed form).

Like you say, some heavier machinery seems to be required. Personally, I'm not sure it's worth it.

Somewhat related, we do have a couple of small libraries for pattern matching on bit strings: Blarney.BitScan and Blarney.BitPat.