Skip to content

Commit

Permalink
Add Distributive and Representable instances to Vec n (#2829)
Browse files Browse the repository at this point in the history
  • Loading branch information
gergoerdi authored Nov 24, 2024
1 parent 10f26ff commit f13b853
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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 #-}

0 comments on commit f13b853

Please sign in to comment.