X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fmaster-project%2Fsupport%2Ftfvec.git;a=blobdiff_plain;f=Data%2FParam%2FTFVec.hs;h=529c52c12a163dfb3b31397ddfbd987189e78ca4;hp=7a0fa1dcc0ab1d1d000bd497097944ebbec64297;hb=92cb4e3a487e88220aba94d7fc6003384e971e5c;hpb=74aad9e23f7b911124ece69b2886f4ec0c88e390 diff --git a/Data/Param/TFVec.hs b/Data/Param/TFVec.hs index 7a0fa1d..529c52c 100644 --- a/Data/Param/TFVec.hs +++ b/Data/Param/TFVec.hs @@ -57,12 +57,13 @@ module Data.Param.TFVec , copyn ) where - import Types +import Types.Data.Num import Types.Data.Num.Decimal.Literals.TH import Data.RangedWord -import Data.Generics (Data, Typeable) +import Data.Generics (Data) +import Data.Typeable import qualified Prelude as P import Prelude hiding ( null, length, head, tail, last, init, take, drop, (++), map, foldl, foldr, @@ -72,6 +73,8 @@ import qualified Data.Traversable as DT (Traversable(traverse)) import Language.Haskell.TH hiding (Pred) import Language.Haskell.TH.Syntax (Lift(..)) +import Language.Haskell.TH.TypeLib + newtype (NaturalT s) => TFVec s a = TFVec {unTFVec :: [a]} deriving (Eq, Typeable) @@ -96,10 +99,10 @@ 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 [] = [| empty |] -vectorTH (x:xs) = [| x +> $(vectorTH xs) |] +vectorTH :: (Lift a, Typeable a) => [a] -> ExpQ +vectorTH xs = sigE [| (TFVec xs) |] (decTFVecT (toInteger (P.length xs)) xs) +-- vectorTH [] = [| empty |] +-- vectorTH (x:xs) = [| x +> $(vectorTH xs) |] unsafeVector :: NaturalT s => s -> [a] -> TFVec s a unsafeVector l xs @@ -282,10 +285,20 @@ instance NaturalT s => Functor (TFVec s) where instance NaturalT s => DT.Traversable (TFVec s) where traverse f = (fmap TFVec).(DT.traverse f).unTFVec -instance (Lift a, NaturalT nT) => Lift (TFVec nT a) where - lift (TFVec xs) = [| unsafeTFVecCoerse - $(decLiteralV (fromIntegerT (undefined :: nT))) - (TFVec xs) |] +-- instance (Lift a, NaturalT nT) => Lift (TFVec nT a) where +-- lift (TFVec xs) = [| unsafeTFVecCoerse +-- $(decLiteralV (fromIntegerT (undefined :: nT))) +-- (TFVec xs) |] + +instance (Lift a, Typeable a, NaturalT nT) => Lift (TFVec nT a) where + lift (TFVec xs) = sigE [| (TFVec xs) |] (decTFVecT (fromIntegerT (undefined :: nT)) xs) + +decTFVecT :: Typeable x => Integer -> x -> Q Type +decTFVecT n a = appT (appT (conT (''TFVec)) (decLiteralT n)) elemT + where + (con,reps) = splitTyConApp (typeOf a) + elemT = typeRep2Type (P.head reps) + -- ====================== -- = Internal Functions =