X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fmaster-project%2Fsupport%2Ftfvec.git;a=blobdiff_plain;f=Data%2FParam%2FTFVec.hs;h=7a0fa1dcc0ab1d1d000bd497097944ebbec64297;hp=673d69b3bb7370394c936e84022b56c0a25875d6;hb=74aad9e23f7b911124ece69b2886f4ec0c88e390;hpb=a0076ff5e9593b4ccfb3fff7637d6338307e09ef diff --git a/Data/Param/TFVec.hs b/Data/Param/TFVec.hs index 673d69b..7a0fa1d 100644 --- a/Data/Param/TFVec.hs +++ b/Data/Param/TFVec.hs @@ -35,7 +35,6 @@ module Data.Param.TFVec , take , drop , select - , group , (<+) , (++) , map @@ -51,8 +50,11 @@ module Data.Param.TFVec , concat , reverse , iterate + , iteraten , generate + , generaten , copy + , copyn ) where @@ -67,7 +69,7 @@ import Prelude hiding ( zipWith, zip, unzip, concat, reverse, iterate ) import qualified Data.Foldable as DF (Foldable, foldr) import qualified Data.Traversable as DT (Traversable(traverse)) -import Language.Haskell.TH +import Language.Haskell.TH hiding (Pred) import Language.Haskell.TH.Syntax (Lift(..)) newtype (NaturalT s) => TFVec s a = TFVec {unTFVec :: [a]} @@ -82,7 +84,6 @@ deriving instance (NaturalT s, Typeable s, Data s, Typeable a, Data a) => Data ( empty :: TFVec D0 a empty = TFVec [] -{-# NOINLINE (+>) #-} (+>) :: a -> TFVec s a -> TFVec (Succ s) a x +> (TFVec xs) = TFVec (x:xs) @@ -94,8 +95,11 @@ singleton x = x +> empty vectorCPS :: [a] -> (forall s . NaturalT s => TFVec s a -> w) -> w vectorCPS xs = unsafeVectorCPS (toInteger (P.length xs)) xs +-- FIXME: Not the most elegant solution... but it works for now in clash vectorTH :: Lift a => [a] -> ExpQ -vectorTH xs = (vectorCPS xs) lift +-- vectorTH xs = (vectorCPS xs) lift +vectorTH [] = [| empty |] +vectorTH (x:xs) = [| x +> $(vectorTH xs) |] unsafeVector :: NaturalT s => s -> [a] -> TFVec s a unsafeVector l xs @@ -175,13 +179,6 @@ select f s n = liftV (select' f' s' n') | otherwise = [] selectFirst0 _ 0 [] = [] -group :: PositiveT n => n -> TFVec s a -> TFVec (Div s n) (TFVec n a) -group n = liftV (group' (fromIntegerT n)) - where group' :: Int -> [a] -> [TFVec s a] - group' n xs = case splitAtM n xs of - Nothing -> [] - Just (ls, rs) -> TFVec ls : group' n rs - (<+) :: TFVec s a -> a -> TFVec (Succ s) a (<+) (TFVec xs) x = TFVec (xs P.++ [x]) @@ -217,12 +214,18 @@ shiftr :: (PositiveT s, NaturalT n, n ~ Pred s, s ~ Succ n) => TFVec s a -> a -> TFVec s a shiftr xs x = tail xs <+ x -rotl :: (PositiveT s, s ~ Succ (Pred s)) => TFVec s a -> TFVec s a -rotl xs = last xs +> init xs - -class Rotr a where rotr :: a -> a -instance Rotr (TFVec D0 a) where rotr xs = xs -instance (PositiveT s, s ~ Succ (Pred s)) => Rotr (TFVec s a) where rotr xs = tail xs <+ head xs +rotl :: forall s a . NaturalT s => TFVec s a -> TFVec s a +rotl = liftV rotl' + where vlen = fromIntegerT (undefined :: s) + rotl' [] = [] + rotl' xs = let (i,[l]) = splitAt (vlen - 1) xs + in l : i + +rotr :: NaturalT s => TFVec s a -> TFVec s a +rotr = liftV rotr' + where + rotr' [] = [] + rotr' (x:xs) = xs P.++ [x] concat :: TFVec s1 (TFVec s2 a) -> TFVec (s1 :*: s2) a concat = liftV (P.foldr ((P.++).unTFVec) []) @@ -230,14 +233,23 @@ concat = liftV (P.foldr ((P.++).unTFVec) []) reverse :: TFVec s a -> TFVec s a reverse = liftV P.reverse -iterate :: NaturalT s => s -> (a -> a) -> a -> TFVec s a -iterate s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.iterate f x) +iterate :: NaturalT s => (a -> a) -> a -> TFVec s a +iterate = iteraten (undefined :: s) + +iteraten :: NaturalT s => s -> (a -> a) -> a -> TFVec s a +iteraten s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.iterate f x) + +generate :: NaturalT s => (a -> a) -> a -> TFVec s a +generate = generaten (undefined :: s) + +generaten :: NaturalT s => s -> (a -> a) -> a -> TFVec s a +generaten s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.tail $ P.iterate f x) -generate :: NaturalT s => s -> (a -> a) -> a -> TFVec s a -generate s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.tail $ P.iterate f x) +copy :: NaturalT s => a -> TFVec s a +copy x = copyn (undefined :: s) x -copy :: NaturalT s => s -> a -> TFVec s a -copy s x = iterate s id x +copyn :: NaturalT s => s -> a -> TFVec s a +copyn s x = iteraten s id x -- ============= -- = Instances =