Skip to content

Commit e953122

Browse files
committed
Add repeat and replicate to Protocols.Vec
1 parent d16ec53 commit e953122

File tree

2 files changed

+54
-1
lines changed
  • clash-protocols

2 files changed

+54
-1
lines changed

clash-protocols/src/Protocols/Vec.hs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@ module Protocols.Vec (
1111
unzip3,
1212
concat,
1313
unconcat,
14+
repeat,
15+
replicate,
1416
) where
1517

1618
-- base
1719
import Data.Tuple
1820
import Prelude ()
1921

2022
-- clash-prelude
21-
import Clash.Prelude hiding (concat, split, unconcat, unzip, unzip3, zip, zip3)
23+
import Clash.Prelude hiding (
24+
concat,
25+
repeat,
26+
replicate,
27+
split,
28+
unconcat,
29+
unzip,
30+
unzip3,
31+
zip,
32+
zip3,
33+
)
2234
import Clash.Prelude qualified as C
2335

2436
-- clash-protocols-base
@@ -131,3 +143,15 @@ split3Vec ::
131143
split3Vec v = (v0, v1, v2)
132144
where
133145
(v0, splitAtI -> (v1, v2)) = splitAtI v
146+
147+
{- | repeat a circuit for a number of times, the number of times the circuit is repeated
148+
is determined by the type-level natural number `n`.
149+
-}
150+
repeat :: (C.KnownNat n) => Circuit a b -> Circuit (Vec n a) (Vec n b)
151+
repeat (Circuit function) = Circuit (C.unzip . uncurry (zipWith (curry function)))
152+
153+
{- | replicate a circuit for a given number of times, the number of times the circuit is replicated
154+
is given by the supplied `SNat n`.
155+
-}
156+
replicate :: SNat n -> Circuit a b -> Circuit (Vec n a) (Vec n b)
157+
replicate SNat = repeat

clash-protocols/tests/Tests/Protocols/Vec.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Test.Tasty.TH (testGroupGenerator)
1818

1919
-- clash-protocols (me!)
2020
import Protocols
21+
import Protocols.Df qualified as Df
2122
import Protocols.Vec qualified as Vec
2223

2324
import Clash.Hedgehog.Sized.Vector (genVec)
@@ -176,6 +177,34 @@ prop_unconcat =
176177
dut = Vec.unconcat C.d2
177178
model = C.unconcat C.d2
178179

180+
prop_repeat :: Property
181+
prop_repeat =
182+
idWithModel
183+
@(C.Vec 3 (Df System Int))
184+
@(C.Vec 3 (Df System Int))
185+
defExpectOptions
186+
gen
187+
model
188+
dut
189+
where
190+
gen = genVecData genSmallInt
191+
dut = Vec.repeat (Df.map succ)
192+
model = fmap (fmap succ)
193+
194+
prop_replicate :: Property
195+
prop_replicate =
196+
idWithModel
197+
@(C.Vec 3 (Df System Int))
198+
@(C.Vec 3 (Df System Int))
199+
defExpectOptions
200+
gen
201+
model
202+
dut
203+
where
204+
gen = genVecData genSmallInt
205+
dut = Vec.replicate C.SNat (Df.map succ)
206+
model = fmap (fmap succ)
207+
179208
tests :: TestTree
180209
tests =
181210
-- TODO: Move timeout option to hedgehog for better error messages.

0 commit comments

Comments
 (0)