1 {-# LANGUAGE DeriveDataTypeable,
9 module Data.HList.Prelude
37 import Types.Data.Bool
38 import Types.Data.Num hiding ((:*:))
47 deriving (Show, Eq, Ord, Read, Typeable)
57 deriving (Show, Eq, Ord, Read, Typeable)
59 instance HList l => HList (HCons e l)
61 hCons :: HList l => e -> l -> HCons e l
68 class HExtendable e l where
70 (.*.) :: e -> l -> e :*: l
72 instance HExtendable e HNil where
73 type e :*: HNil = HCons e HNil
74 e .*. nil = hCons e nil
76 instance HList l => HExtendable e (HCons e' l) where
77 type e :*: HCons e' l = HCons e (HCons e' l)
78 e .*. HCons e' l = hCons e (hCons e' l)
84 class HAppendable l l' where
86 (.++.) :: l -> l' -> l :++: l'
88 instance HList l => HAppendable HNil l where
92 instance ( HList (l :++: l')
94 ) => HAppendable (HCons e l) l' where
95 type HCons e l :++: l' = HCons e (l :++: l')
96 (HCons e l) .++. l' = hCons e (l .++. l')
99 class Applyable f a where
101 apply :: f -> a -> Apply f a
104 class Applyable2 f a b where
106 apply2 :: f -> a -> b -> Apply2 f a b
111 instance Applyable Id a where
116 data ApplyHAppend = ApplyHAppend
118 instance HAppendable a b => Applyable2 ApplyHAppend a b where
119 type Apply2 ApplyHAppend a b = a :++: b
120 apply2 _ a b = a .++. b
123 class HFoldrable f v l where
125 hFoldr :: f -> v -> l -> HFoldr f v l
127 instance HFoldrable f v HNil where
128 type HFoldr f v HNil = v
131 instance ( HFoldrable f v l
132 , Applyable2 f e (HFoldr f v l)
133 ) => HFoldrable f v (HCons e l) where
134 type HFoldr f v (HCons e l) = Apply2 f e (HFoldr f v l)
135 hFoldr f v (HCons e l) = apply2 f e (hFoldr f v l)
138 class HConcatable ls where
140 hConcat :: ls -> HConcat ls
142 instance HFoldrable ApplyHAppend HNil ls => HConcatable ls where
143 type HConcat ls = HFoldr ApplyHAppend HNil ls
144 hConcat ls = hFoldr ApplyHAppend hNil ls
147 class HMappable f l where
149 hMap :: f -> l -> HMap f l
151 instance HMappable f HNil where
152 type HMap f HNil = HNil
155 instance ( Applyable f x
158 ) => HMappable f (HCons x xs) where
159 type HMap f (HCons x xs) = HCons (Apply f x) (HMap f xs)
160 hMap f (HCons x xs) = hCons (apply f x) (hMap f xs)
163 class HAllable f l where
165 hAll :: f -> l -> HAll f l
167 instance HAllable f HNil where
168 type HAll f HNil = True
169 hAll _ _ = undefined :: True
171 instance ( IfT (Apply f x) (HAll f xs) False
174 ) => HAllable f (HCons x xs) where
175 type HAll f (HCons x xs) = If (Apply f x)
178 hAll f (HCons x xs) = ifT (apply f x)
183 type family HLength l
184 type instance HLength HNil = D0
185 type instance HLength (HCons e l) = Succ (HLength l)