vector-heterogenous 0.1.0 → 0.1.1
raw patch · 2 files changed
+59/−2 lines, 2 files
Files
src/Data/Vector/Heterogenous/HList.hs view
@@ -5,6 +5,7 @@ {-# LANGUAGE TypeOperators #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE TypeFamilies #-}@@ -17,6 +18,9 @@ HList (..) , HLength (..) , List2HList (..)+ , HList2List (..)+ , HTake1 (..)+ , HDrop1 (..) -- * Downcasting , ConstraintBox (..)@@ -31,6 +35,7 @@ -- ** HList , HCons (..) , UnHList (..)+ , HAppend -- ** Type Lists , Distribute (..)@@ -38,13 +43,16 @@ , Map (..) , Reverse (..) , (:!) (..)+ , Index (..) , (++) (..) , ($) (..) , Concat (..) , Length (..)+ , Length1 (..) -- ** Type Nats , Nat1(..)+ , Nat1Box(..) , ToNat1 , FromNat1 )@@ -101,6 +109,15 @@ instance (HLength (HList xs)) => HLength (HList (x ': xs)) where hlength (x:::xs) = 1+hlength xs +-- | For converting into a list++class HList2List xs a | xs -> a where+ hlist2list :: xs -> [a]+instance HList2List (HList '[]) a where+ hlist2list xs = []+instance (HList2List (HList xs) a) => HList2List (HList (a ':xs)) a where+ hlist2list (x:::xs) = x:(hlist2list xs) + -- | For construction from lists class List2HList x xs where@@ -115,6 +132,34 @@ list2hlist [] = error "List2HList x HNil: cannot append empty list" list2hlist (x:xs) = x:::(list2hlist xs) +-- | Equivalent to prelude's "drop"+class HDrop1 n xs1 xs2 | n xs1 -> xs2 where+ hdrop1 :: n -> xs1 -> xs2+ +instance HDrop1 (Nat1Box Zero) (HList xs1) (HList xs1) where+ hdrop1 n xs = xs+ +instance (HDrop1 (Nat1Box n) (HList xs1) (HList xs2)) => + HDrop1 (Nat1Box (Succ n)) (HList (x ': xs1)) (HList xs2) where+ hdrop1 _ (x:::xs) = hdrop1 (Nat1Box :: Nat1Box n) xs++-- | Equivalent to prelude's "take"+class HTake1 n xs1 xs2 | n xs1 -> xs2 where+ htake1 :: n -> xs1 -> xs2++instance HTake1 (Nat1Box Zero) (HList xs1) (HList '[]) where+ htake1 _ _ = HNil+ +instance (HTake1 (Nat1Box n) (HList xs1) (HList xs2)) => HTake1 (Nat1Box (Succ n)) (HList (x ': xs1)) (HList (x ': xs2)) where+ htake1 _ (x:::xs) = x:::(htake1 (Nat1Box :: Nat1Box n) xs)+ +-- instance (HTake1 (Nat1Box (ToNat1 n)) (HList xs1) (HList xs2)) => HTake1 (Sing n) (HList xs1) (HList xs2) where+-- htake1 _ xs = htake1 (Nat1Box :: Nat1Box (ToNat1 n)) xs++-- type family HTake (n::Nat) xs+-- type instance HTake n (HList xs) = HList (Take n xs)++ ------------------------------------------------------------------------------- -- downcasting HList -> [] @@ -161,6 +206,9 @@ type family UnHList (xs :: *) :: [a] type instance UnHList (HList xs) = xs +type family HAppend xs ys :: *+type instance HAppend (HList xs) (HList ys) = HList (xs ++ ys)+ --------------------------------------- type family Distribute (xs::[a->b]) (t::a) :: [b]@@ -210,9 +258,16 @@ type instance Concat '[] = '[] type instance Concat (x ': xs) = x ++ Concat xs +type Take (n::Nat) (xs :: [*]) = Take1 (ToNat1 n) xs++type family Take1 (n::Nat1) (xs::[*]) :: [*]+type instance Take1 Zero xs = '[]+type instance Take1 (Succ n) (x ': xs) = x ': (Take1 n xs)+ --------------------------------------- data Nat1 = Zero | Succ Nat1+data Nat1Box (n::Nat1) = Nat1Box type family FromNat1 (n :: Nat1) :: Nat type instance FromNat1 Zero = 0@@ -220,6 +275,8 @@ type instance FromNat1 (Succ (Succ Zero)) = 2 type instance FromNat1 (Succ (Succ (Succ Zero))) = 3 type instance FromNat1 (Succ (Succ (Succ (Succ Zero)))) = 4+type instance FromNat1 (Succ (Succ (Succ (Succ (Succ Zero))))) = 5+type instance FromNat1 (Succ (Succ (Succ (Succ (Succ (Succ Zero)))))) = 6 type family ToNat1 (n :: Nat) :: Nat1 type instance ToNat1 0 = Zero
vector-heterogenous.cabal view
@@ -1,9 +1,9 @@ Name: vector-heterogenous-Version: 0.1.0+Version: 0.1.1 Synopsis: A type-safe library for vectors whose elements can be of any type, or any type satisfying some constraints Description: Category: Data, Data Structures-License: GPL+License: BSD3 --License-file: LICENSE Author: Mike izbicki Maintainer: mike@izbicki.me