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 Protocol instances for tuples up to arity 8 #71

Merged
merged 3 commits into from
Apr 3, 2024
Merged
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
1 change: 1 addition & 0 deletions clash-protocols.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ library
Protocols.Internal
Protocols.Internal.TaggedBundle
Protocols.Internal.TaggedBundle.TH
Protocols.Internal.TH
Protocols.Internal.Units
Protocols.Internal.Units.TH

Expand Down
15 changes: 9 additions & 6 deletions src/Protocols/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import qualified Clash.Prelude as C
import qualified Clash.Explicit.Prelude as CE

import Protocols.Internal.TH (protocolTupleInstances)
import Protocols.Cpp (maxTupleSize)

import Control.Arrow ((***))
import Data.Coerce (coerce)
import Data.Default (Default(def))
Expand Down Expand Up @@ -156,7 +159,7 @@

-- | Protocol-agnostic acknowledgement
newtype Ack = Ack Bool
deriving (Generic, C.NFDataX, Show, C.Bundle)

Check warning on line 162 in src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.2.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 162 in src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.2.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 162 in src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.4.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 162 in src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.4.8 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 162 in src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.6.4 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

Check warning on line 162 in src/Protocols/Internal.hs

View workflow job for this annotation

GitHub Actions / Cabal tests - ghc 9.6.4 / clash 1.8.1

• Both DeriveAnyClass and GeneralizedNewtypeDeriving are enabled

-- | Acknowledge. Used in circuit-notation plugin to drive ignore components.
instance Default Ack where
Expand Down Expand Up @@ -185,17 +188,17 @@
type Fwd () = ()
type Bwd () = ()

-- | __NB__: The documentation only shows instances up to /3/-tuples. By
-- default, instances up to and including /12/-tuples will exist. If the flag
-- @large-tuples@ is set instances up to the GHC imposed limit will exist. The
-- GHC imposed limit is either 62 or 64 depending on the GHC version.
instance Protocol (a, b) where
type Fwd (a, b) = (Fwd a, Fwd b)
type Bwd (a, b) = (Bwd a, Bwd b)

instance Protocol (a, b, c) where
type Fwd (a, b, c) = (Fwd a, Fwd b, Fwd c)
type Bwd (a, b, c) = (Bwd a, Bwd b, Bwd c)

instance Protocol (a, b, c, d) where
type Fwd (a, b, c, d) = (Fwd a, Fwd b, Fwd c, Fwd d)
type Bwd (a, b, c, d) = (Bwd a, Bwd b, Bwd c, Bwd d)
-- Generate n-tuple instances, where n > 2
protocolTupleInstances maxTupleSize

instance C.KnownNat n => Protocol (C.Vec n a) where
type Fwd (C.Vec n a) = C.Vec n (Fwd a)
Expand Down
37 changes: 37 additions & 0 deletions src/Protocols/Internal/TH.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{-# OPTIONS_HADDOCK hide #-}

module Protocols.Internal.TH (protocolTupleInstances) where

import Language.Haskell.TH

appTs :: Q Type -> [Q Type] -> Q Type
appTs = foldl appT

protocolTupleInstances :: Int -> Q [Dec]
protocolTupleInstances n = mapM protocolTupleInstance [3..n]

protocolTupleInstance :: Int -> Q Dec
protocolTupleInstance n =
instanceD
(pure []) -- context
(protocolConT `appT` tup) -- head
[mkTyInst fwdConName, mkTyInst bwdConName] -- body

where
fwdConName = mkName "Fwd"
bwdConName = mkName "Bwd"
protocolConT = conT (mkName "Protocol")

tyVars :: [TypeQ]
tyVars = map (varT . mkName . ('a':) . show) [1..n]

tup = tupleT n `appTs` tyVars

mkTyInst :: Name -> DecQ
mkTyInst con =
tySynInstD $ tySynEqn Nothing lhs rhs
where
lhs, rhs :: TypeQ
lhs = conT con `appT` tup
rhs = tupleT n `appTs` map (conT con `appT`) tyVars

2 changes: 1 addition & 1 deletion src/Protocols/Internal/Units/TH.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# OPTIONS_HADDOCK hide #-}

module Protocols.Internal.Units.TH where
module Protocols.Internal.Units.TH (unitsTupleInstances) where

import Language.Haskell.TH

Expand Down
Loading