Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packing sum-types #31

Open
christiaanb opened this issue Jul 22, 2019 · 1 comment
Open

Packing sum-types #31

christiaanb opened this issue Jul 22, 2019 · 1 comment

Comments

@christiaanb
Copy link

christiaanb commented Jul 22, 2019

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
Copy link
Collaborator

mn416 commented Jul 26, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants