Skip to content
This repository has been archived by the owner on Sep 7, 2018. It is now read-only.

Suggestions for some utility functions #72

Open
comonoidial opened this issue Sep 2, 2016 · 5 comments
Open

Suggestions for some utility functions #72

comonoidial opened this issue Sep 2, 2016 · 5 comments

Comments

@comonoidial
Copy link

Looking at some clash code I found useful functions missing from the prelude.

  • snatToNum (as Integer is rarely used in designs)
  • bitCast = unpack . pack (implemented with zero cost in hardware)
  • constructing a vector from a index mapping function (Index n -> a) -> Vec n a
  • multiplexor function with n inputs on Signal types so that it be conveniently written on one line (using a list of inputs or variadic function, or whatever synthesizes efficiently) instead the now common:
res = myMux <$> sels <*> xs <*> ys <*> zs
  where myMux A _ _ _ = 0
            myMux B x _ _ = x
            myMux C _ y _ = y
            myMux D _ _ z = z
-- want to write something like:
multiplexor sels (A,0) (B,xs) (C,ys) (D,zs)
@christiaanb
Copy link
Member

christiaanb added a commit that referenced this issue Sep 2, 2016
@christiaanb
Copy link
Member

christiaanb commented Sep 2, 2016

http://hackage.haskell.org/package/clash-prelude-0.10.14/docs/CLaSH-Sized-Vector.html#v:backpermute is isomorphic to (Index n -> a) -> Vec n a.

Edit: perhaps backPermute is too general. A possible implementation for indexMapping could be:

indexMapping :: KnownNat n => (Index n -> a) -> Vec n a
indexMapping f = imap (\i _ -> f i) (repeat ())

@christiaanb
Copy link
Member

christiaanb commented Sep 2, 2016

Would:

multiplexor :: (KnownNat n, Enum sel) => Signal sel -> Vec n (Signal a) -> Signal a
multiplexor sel alts = (!!) <$> bundle alts <*> sel

work for you?

so you can write:

multiplexor sels (0 :> xs :> ys :> zs :> Nil)

Edit: of course you lose the exhaustiveness check you get in the original example

@ggreif
Copy link
Contributor

ggreif commented Sep 3, 2016

multiplexer is the more accepted spelling

@comonoidial
Copy link
Author

The downside of using this vector multiplexer variant is that it depends on the implicit Enum ordering of the selection signal. And I would prefer explicit selectors to keep the code understandable.

Maybe Vector could be made an instance of Representable Functor, with:

tabulate :: (Index n -> a) -> Vec n a
index :: Vec n a ->(Index n -> a)

Also the related type classes in Data.Key have a lot of functions that work on both lists and vectors, so that could be a way to simplify the transition from list based code.

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

No branches or pull requests

3 participants