I want to define a function that takes an arbitrarily sized HList of values behind some type constructor, e.g. Maybe
:
foo :: HList '[t a, t b, t c, ...] -> Bar
-- For example:
foo :: HList '[Maybe a, Maybe b, Maybe c, ...] -> Bar
Is there some way to do that in Haskell? Thanks!
You could add a constraint using a custom type class, requiring the types to involve the same f
type constructor.
import Data.HList.HList
class HSame (t :: [Type]) where
instance HSame '[] where
instance HSame '[f x] where
instance HSame (f u : xs) => HSame (f t ': f u ': xs) where
foo :: HSame xs => HList xs -> Bool
foo _ = True
Alternatively, define a Map f xs
type family and define
foo :: HList (Map f xs) -> Bool
foo _ = True
but this won't work well with type inference, since there's no easy way for the compiler to infer f
and xs
.