Skip to content

Use (:>) in favor of Cons #2914

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

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
4 changes: 2 additions & 2 deletions clash-lib/src/Clash/Core/TermLiteral.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import Clash.Core.Term (Term(Literal, Data), collectAr
import Clash.Promoted.Nat
import Clash.Promoted.Nat.Unsafe
import Clash.Sized.Index (Index)
import Clash.Sized.Vector (Vec (Nil, Cons), fromList)
import Clash.Sized.Vector (Vec (Nil, (:>)), fromList)
import qualified Clash.Util.Interpolate as I
import qualified Clash.Verification.Internal as Cv

Expand Down Expand Up @@ -193,7 +193,7 @@ instance (TermLiteral a, KnownNat n) => TermLiteral (Vec n a) where
case constr of
Data (MkData{dcName=Name{nameOcc}})
| nameOcc == showt 'Nil -> Right []
| nameOcc == showt 'Cons ->
| nameOcc == showt '(:>) ->
case lefts args of
[_gadtProof, c0, cs0] -> do
c1 <- termToData @a c0
Expand Down
10 changes: 5 additions & 5 deletions clash-lib/src/Clash/Core/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ unsafeCoerceTy =

-- | Create a vector of supplied elements
mkVec :: DataCon -- ^ The Nil constructor
-> DataCon -- ^ The Cons (:>) constructor
-> DataCon -- ^ The (:>) constructor
-> Type -- ^ Element type
-> Integer -- ^ Length of the vector
-> [Term] -- ^ Elements to put in the vector
Expand Down Expand Up @@ -128,7 +128,7 @@ mkVec nilCon consCon resTy = go
_ -> error "impossible"

-- | Append elements to the supplied vector
appendToVec :: DataCon -- ^ The Cons (:>) constructor
appendToVec :: DataCon -- ^ The (:>) constructor
-> Type -- ^ Element type
-> Term -- ^ The vector to append the elements to
-> Integer -- ^ Length of the vector
Expand Down Expand Up @@ -161,7 +161,7 @@ extractElems
-> InScopeSet
-- ^ (Superset of) in scope variables
-> DataCon
-- ^ The Cons (:>) constructor
-- ^ The (:>) constructor
-> Type
-- ^ The element type
-> Char
Expand Down Expand Up @@ -609,10 +609,10 @@ shouldSplit0 tcm (TyConApp tcNm tyArgs)
-- Project the n'th value out of a vector
--
-- >>> mkVecSelector subj 0
-- case subj of Cons x xs -> x
-- case subj of x :> xs -> x
--
-- >>> mkVecSelector subj 2
-- case (case (case subj of Cons x xs -> xs) of Cons x xs -> xs) of Cons x xs -> x
-- case (case (case subj of x :> xs -> xs) of x :> xs -> xs) of x :> xs -> x
mkVecSelector :: forall m . MonadUnique m => InScopeSet -> Term -> Integer -> m Term
mkVecSelector is0 subj 0 =
mkSelectorCase ($(curLoc) ++ "mkVecSelector") is0 tcm subj 2 1
Expand Down
4 changes: 2 additions & 2 deletions clash-lib/src/Clash/Netlist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,10 +1056,10 @@ mkDcApplication declType [dstHType] bndr dc args = do
Vector 0 _ -> return (HW.DataCon dstHType VecAppend [])
Vector 1 _ -> case argExprsFiltered of
[e] -> return (HW.DataCon dstHType VecAppend [e])
_ -> error $ $(curLoc) ++ "Unexpected number of arguments for `Cons`: " ++ showPpr args
_ -> error $ $(curLoc) ++ "Unexpected number of arguments for `(:>)`: " ++ showPpr args
Vector _ _ -> case argExprsFiltered of
[e1,e2] -> return (HW.DataCon dstHType VecAppend [e1,e2])
_ -> error $ $(curLoc) ++ "Unexpected number of arguments for `Cons`: " ++ showPpr args
_ -> error $ $(curLoc) ++ "Unexpected number of arguments for `(:>)`: " ++ showPpr args
MemBlob _ _ ->
case compare 6 (length argExprsFiltered) of
EQ -> return (HW.DataCon dstHType (DC (dstHType,0)) argExprsFiltered)
Expand Down
2 changes: 1 addition & 1 deletion clash-lib/src/Clash/Netlist/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ mkADT builtInTranslation reprs m tyString tc args = case tyConDataCons (UniqMap.
-- data Vec :: Nat -> Type -> Type
-- where
-- Nil :: Vec 0 a
-- Cons :: forall m . (n ~ m + 1) => a -> Vec m a -> Vec n a
-- (:>) :: forall m . (n ~ m + 1) => a -> Vec m a -> Vec n a
--
-- where we can generate a type for `m` when we know `n` (by doing `n-1`).
--
Expand Down
12 changes: 6 additions & 6 deletions clash-lib/src/Clash/Normalize/PrimitiveReductions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ vecTailTy vecNm =
-- vector, the latter the tail.
extractHeadTail
:: DataCon
-- ^ The Cons (:>) constructor
-- ^ The (:>) constructor
-> Type
-- ^ Element type
-> Integer
Expand All @@ -206,15 +206,15 @@ extractHeadTail consCon elTy n vec =
in
( Case vec elTy [(pat, Var el)]
, Case vec restTy [(pat, Var rest)] )
_ -> error "extractHeadTail: failed to instantiate Cons DC"
_ -> error "extractHeadTail: failed to instantiate (:>) DC"
where
tys = [(LitTy (NumTy n)), elTy, (LitTy (NumTy (n-1)))]

-- | Create a vector of supplied elements
mkVecCons
:: HasCallStack
=> DataCon
-- ^ The Cons (:>) constructor
-- ^ The (:>) constructor
-> Type
-- ^ Element type
-> Integer
Expand All @@ -235,7 +235,7 @@ mkVecCons consCon resTy n h t
, Left (primCo consCoTy)
, Left h
, Left t ]
_ -> error "mkVecCons: failed to instantiate Cons DC"
_ -> error "mkVecCons: failed to instantiate (:>) DC"

-- | Create an empty vector
mkVecNil
Expand Down Expand Up @@ -568,7 +568,7 @@ reduceTraverse n aTy fTy bTy dict fun arg (TransformContext is0 ctx) = do
-- > (:>) <$> x0 <*> ((:>) <$> x1 <*> pure Nil)
mkTravVec :: TyConName -- ^ Vec tcon
-> DataCon -- ^ Nil con
-> DataCon -- ^ Cons con
-> DataCon -- ^ (:>) con
-> Term -- ^ 'pure' term
-> Term -- ^ '<*>' term
-> Term -- ^ 'fmap' term
Expand Down Expand Up @@ -983,7 +983,7 @@ reduceUnconcat unconcatPrimInfo n m aTy _kn sm arg (TransformContext inScope _ct
, Left (snd nextVec)
]
-- let (mvec,nextVec) = splitAt sm arg
-- in Cons mvec (unconcat sm nextVec)
-- in mvec :> (unconcat sm nextVec)
lBody = mkVecCons consCon innerVecTy n mvec nextUnconcat
lb = Letrec lbs lBody

Expand Down
8 changes: 4 additions & 4 deletions clash-lib/src/Clash/Normalize/Transformations/Case.hs
Original file line number Diff line number Diff line change
Expand Up @@ -670,24 +670,24 @@ caseOneAlt e = return e
-- existential should be. For example, consider Vec:
--
-- data Vec :: Nat -> Type -> Type where
-- Nil :: Vec 0 a
-- Cons x xs :: a -> Vec n a -> Vec (n + 1) a
-- Nil :: Vec 0 a
-- x :> xs :: a -> Vec n a -> Vec (n + 1) a
--
-- Thus, 'null' (annotated with existentials) could look like:
--
-- null :: forall n . Vec n Bool -> Bool
-- null v =
-- case v of
-- Nil {n ~ 0} -> True
-- Cons {n1:Nat} {n~n1+1} (x :: a) (xs :: Vec n1 a) -> False
-- (:>) {n1:Nat} {n~n1+1} (x :: a) (xs :: Vec n1 a) -> False
--
-- When it's applied to a vector of length 5, this becomes:
--
-- null :: Vec 5 Bool -> Bool
-- null v =
-- case v of
-- Nil {5 ~ 0} -> True
-- Cons {n1:Nat} {5~n1+1} (x :: a) (xs :: Vec n1 a) -> False
-- (:>) {n1:Nat} {5~n1+1} (x :: a) (xs :: Vec n1 a) -> False
--
-- This function solves 'n1' and replaces every occurrence with its solution. A
-- very limited number of solutions are currently recognized: only adds (such
Expand Down
6 changes: 3 additions & 3 deletions clash-lib/src/Clash/Normalize/Transformations/Letrec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import qualified Data.Text.Extra as Text
import GHC.Stack (HasCallStack)

import Clash.Annotations.BitRepresentation.Deriving (dontApplyInHDL)
import Clash.Sized.Vector as Vec (Vec(Cons), splitAt)
import Clash.Sized.Vector as Vec (Vec((:>)), splitAt)

import Clash.Annotations.Primitive (extractPrim)
import Clash.Core.DataCon (DataCon(..))
Expand Down Expand Up @@ -151,9 +151,9 @@ removeUnusedExpr _ e@(Case _ _ [(DataPat _ [] xs,altExpr)]) =
else return e

-- Replace any expression that creates a Vector of size 0 within the application
-- of the Cons constructor, by the Nil constructor.
-- of the (:>) constructor, by the Nil constructor.
removeUnusedExpr _ e@(collectArgsTicks -> (Data dc, [_,Right aTy,Right nTy,_,Left a,Left nil],ticks))
| nameOcc (dcName dc) == Text.showt 'Vec.Cons
| nameOcc (dcName dc) == Text.showt '(Vec.:>)
= do
tcm <- Lens.view tcCache
case runExcept (tyNatSize tcm nTy) of
Expand Down
6 changes: 3 additions & 3 deletions clash-prelude/src/Clash/Explicit/SimIO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ getLine (File fp) (Reg r) = SimIO $ do
return 0
where
rep :: String -> Vec m (Unsigned 8) -> Vec m (Unsigned 8)
rep [] vs = vs
rep (x:xs) (Cons _ vs) = Cons (toEnum (fromEnum x)) (rep xs vs)
rep _ Nil = Nil
rep [] vs = vs
rep (x:xs) (_ :> vs) = toEnum (fromEnum x) :> rep xs vs
rep _ Nil = Nil
-- See: https://github.com/clash-lang/clash-compiler/pull/2511
{-# CLASH_OPAQUE getLine #-}
{-# ANN getLine hasBlackBox #-}
Expand Down
Loading