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

Add Distributive and Representable instances to Vec n #2829

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ADDED: `Distributive` and `Representable` instances for `Vec`
4 changes: 3 additions & 1 deletion clash-prelude/clash-prelude.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ Library
TypeFamilies
UndecidableInstances

Build-depends: array >= 0.5.1.0 && < 0.6,
Build-depends: adjunctions >= 4.0 && < 5.0,
array >= 0.5.1.0 && < 0.6,
arrows >= 0.4 && < 0.5,
base >= 4.11 && < 5,
binary >= 0.8.5 && < 0.11,
Expand All @@ -327,6 +328,7 @@ Library
data-binary-ieee754 >= 0.4.4 && < 0.6,
data-default-class >= 0.1.2 && < 0.2,
deepseq >= 1.4.1.0 && < 1.6,
distributive >= 0.1 && < 1.0,
extra >= 1.6.17 && < 1.8,
ghc-prim >= 0.5.1.0 && < 0.12,
ghc-typelits-extra >= 0.4 && < 0.5,
Expand Down
15 changes: 15 additions & 0 deletions clash-prelude/src/Clash/Sized/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ import Data.Constraint.Nat (leZero)
import Data.Data
(Data (..), Constr, DataType, Fixity (..), Typeable, mkConstr, mkDataType)
import Data.Either (isLeft)
import Data.Distributive
import Data.Functor.Rep
#if MIN_VERSION_base(4,18,0)
import qualified Data.Foldable1 as F1
#endif
Expand Down Expand Up @@ -2776,3 +2778,16 @@ type instance Lens.Index (Vec n a) = Index n
type instance Lens.IxValue (Vec n a) = a
instance KnownNat n => Lens.Ixed (Vec n a) where
ix i f xs = replace_int xs (fromEnum i) <$> f (index_int xs (fromEnum i))

instance KnownNat n => Distributive (Vec n) where
distribute fxs = tabulate $ \i -> fmap (!! i) fxs
{-# INLINE distribute #-}

instance KnownNat n => Representable (Vec n) where
type Rep (Vec n) = Index n

tabulate f = map f indicesI
{-# INLINE tabulate #-}

index = (!!)
{-# INLINE index #-}