-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't loop on hidden recursive data type
The recursive occurance was hiding behind a type family. So we should always expand type families when checking whether a data type is recursive. Fixes #1921
- Loading branch information
1 parent
8894dd6
commit a152f39
Showing
4 changed files
with
85 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FIXED: Dont' loop on recursive data types hiding behind type families [#1921](https://github.com/clash-lang/clash-compiler/issues/1921) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{-# LANGUAGE DerivingVia | ||
, DerivingStrategies | ||
, LambdaCase | ||
, AllowAmbiguousTypes | ||
, ApplicativeDo | ||
, StandaloneDeriving | ||
, StandaloneKindSignatures #-} | ||
|
||
module T1921 where | ||
|
||
import Clash.Prelude | ||
import Control.Lens | ||
import Data.Default | ||
import Data.Singletons.Prelude | ||
import Data.Singletons.TypeLits as TL | ||
|
||
topEntity :: Clock System -> Reset System -> Enable System -> Signal System (Unsigned 8) | ||
topEntity = exposeClockResetEnable fibonacciLFSR8 | ||
|
||
-- Straightforward newtype | ||
type LFSRState :: Nat -> Type | ||
newtype LFSRState n = LFSRState { runLFSRState :: BitVector n } | ||
deriving (NFDataX, AutoReg) via (BitVector n) | ||
instance KnownNat n => Default (LFSRState n) where | ||
def = LFSRState (fromIntegral 1) | ||
|
||
fibonacciLFSR8 :: HiddenClockResetEnable dom => Signal dom (Unsigned 8) | ||
fibonacciLFSR8 = fibonacciLFSRType @('[3,4,5,7]) @8 | ||
|
||
fibonacciLFSRType | ||
:: forall (taps :: [Nat]) (n :: Nat) dom | ||
. SingI taps | ||
=> KnownNat n | ||
=> HiddenClockResetEnable dom | ||
=> Signal dom (Unsigned n) | ||
fibonacciLFSRType = | ||
let lfsr = autoReg def (LFSRState <$> lfsr') | ||
lfsr' = do lfsrState <- lfsr | ||
-- shift the bit register by one, and then replace | ||
-- the bit on the end by xor of the taps (via go) | ||
return $ | ||
shiftL (runLFSRState lfsrState) 1 | ||
& ix 0 | ||
.~ go lfsrState (sing :: Sing taps) | ||
in unpack <$> lfsr' | ||
|
||
where | ||
go :: forall (n :: Nat) (indices :: [Nat]) | ||
. SingI indices | ||
=> KnownNat n | ||
=> LFSRState n -> SList indices -> Bit | ||
go b@(LFSRState bs) = \case | ||
-- If there is only one tap left, return that bit | ||
(SCons a SNil) -> withKnownNat a | ||
$ bs ^?! ix (fromIntegral $ TL.natVal a) | ||
-- XOR a tapped bit with the remaining taps | ||
(SCons a as) -> withSingI as $ withKnownNat a | ||
$ xor (bs ^?! ix (fromIntegral $ TL.natVal a)) (go b as) | ||
-- This should never happen (we can't have no taps) | ||
SNil -> error "A no-tap LFSR is ill-defined" |