1 {-# LANGUAGE DeriveDataTypeable,
9 module Data.HList.Prelude
36 import Types.Data.Num hiding ((:*:))
45 deriving (Show, Eq, Ord, Read, Typeable)
55 deriving (Show, Eq, Ord, Read, Typeable)
57 instance HList l => HList (HCons e l)
59 hCons :: HList l => e -> l -> HCons e l
66 class HExtendable e l where
68 (.*.) :: e -> l -> e :*: l
70 instance HExtendable e HNil where
71 type e :*: HNil = HCons e HNil
72 e .*. nil = hCons e nil
74 instance HList l => HExtendable e (HCons e' l) where
75 type e :*: HCons e' l = HCons e (HCons e' l)
76 e .*. HCons e' l = hCons e (hCons e' l)
82 class HAppendable l l' where
84 (.++.) :: l -> l' -> l :++: l'
86 instance HList l => HAppendable HNil l where
90 instance ( HList (l :++: l')
92 ) => HAppendable (HCons e l) l' where
93 type HCons e l :++: l' = HCons e (l :++: l')
94 (HCons e l) .++. l' = hCons e (l .++. l')
97 class Applyable f a where
99 apply :: f -> a -> Apply f a
102 class Applyable2 f a b where
104 apply2 :: f -> a -> b -> Apply2 f a b
109 instance Applyable Id a where
114 data ApplyHAppend = ApplyHAppend
116 instance HAppendable a b => Applyable2 ApplyHAppend a b where
117 type Apply2 ApplyHAppend a b = a :++: b
118 apply2 _ a b = a .++. b
121 class HFoldrable f v l where
123 hFoldr :: f -> v -> l -> HFoldr f v l
125 instance HFoldrable f v HNil where
126 type HFoldr f v HNil = v
129 instance ( HFoldrable f v l
130 , Applyable2 f e (HFoldr f v l)
131 ) => HFoldrable f v (HCons e l) where
132 type HFoldr f v (HCons e l) = Apply2 f e (HFoldr f v l)
133 hFoldr f v (HCons e l) = apply2 f e (hFoldr f v l)
136 class HConcatable ls where
138 hConcat :: ls -> HConcat ls
140 instance HFoldrable ApplyHAppend HNil ls => HConcatable ls where
141 type HConcat ls = HFoldr ApplyHAppend HNil ls
142 hConcat ls = hFoldr ApplyHAppend hNil ls
145 class HMappable f l where
147 hMap :: f -> l -> HMap f l
149 instance HMappable f HNil where
150 type HMap f HNil = HNil
153 instance ( Applyable f x
156 ) => HMappable f (HCons x xs) where
157 type HMap f (HCons x xs) = HCons (Apply f x) (HMap f xs)
158 hMap f (HCons x xs) = hCons (apply f x) (hMap f xs)
161 type family HLength l
162 type instance HLength HNil = D0
163 type instance HLength (HCons e l) = Succ (HLength l)