As others have stated, as given, this problem has no solution. But your real problem is that your data is not described by a list of tuples - by definition, a list is homogenous (all elements contain the same type) while your data is heterogenous.
If you want write a function depending on the type, you must store the type on the type level somehow.
Your example data is actually described by the type Prod ((,) Integer) '[String, Bool, Integer], where Prod is the following type:
data Prod f (xs :: [a]) where
P0 :: Prod f '[]
(:>) :: f x -> Prod f xs -> Prod f (x ': xs)
infixr 5 :>
or more generally Prod ((,) Integer) xs for some list of types xs.
Your example value is then
a = (1, "uno") :> (2, True) :> (3, 5) :> P0
You can branch on these types using the normal methods - i.e. a type class. Supposing one has such a class:
class HasFoo x where
foo :: x -> Integer
instance HasFoo String where foo = fromIntegral . length
instance HasFoo Bool where foo = fromIntegral . fromEnum
instance HasFoo Integer where foo = id
you can apply such a foo function to every element of your product
type family All (c :: k -> Constraint) (xs :: [k]) :: Constraint where
All c '[] = ()
All c (x ': xs) = (c x, All c xs)
-- example operation: add everything
fooProd :: All HasFoo xs
=> Prod ((,) Integer) xs
-> Integer
fooProd P0 = 0
fooProd ((i, x) :> ps) = i + foo x + fooProd ps
This requires some GHC extensions, at least TypeFamilies, GADTs, ConstraintKinds, DataKinds, PolyKinds.
ImpredicativeTypesextension, which currently doesn’t really work. - Alexis KingImpredicativeTypeswon't help you here. Impredicativity allows you to parameterise types byforall-quantified types, eg to make a homogeneous list of polymorphic functions. OP wants to build a heterogeneous list of monomorphic tuples. - Benjamin Hodgsona :: [(Int, Either Bool String)]- Benjamin Hodgsonforallquantification usingImpredicativeTypes. - Alexis King