Explanation of elem data type

According to this http://zvon.org/other/haskell/Outputprelude/elem_f.html elem is Eq a => a -> [a] -> Bool but according to my ghci :t it's this elem :: (Foldable t, Eq a) => a -> t a -> Bool I understand the first, but not the second, especially with the t. What is this saying extra, different from the first one? ⨽ Lawrence Bottorff Grand Marais, MN, USA borgauf@gmail.com

Hello Lawrence, Il 03 agosto 2021 alle 17:14 Galaxy Being ha scritto:
According to this http://zvon.org/other/haskell/Outputprelude/elem_f.html elem is
Eq a => a -> [a] -> Bool
but according to my ghci :t it's this
elem :: (Foldable t, Eq a) => a -> t a -> Bool
I understand the first, but not the second, especially with the t. What is this saying extra, different from the first one?
Yup, some years ago there was a shift of some functions — after a big discussion, as there were a few controversial changes — from «working on lists only» to «working on all instances of some class» (in this case: Foldable, alias «data structures that can be folded») [1]. What is there for you? You gain the ability to use `elem` on other structures than lists (e.g. Trees). If you — like me — do not fancy reading signatures with too many typeclasses, you can always use +d in ghci to default to concrete types: λ> :t foldr foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b λ> :t +d foldr foldr :: (a -> b -> b) -> b -> [a] -> b [1] https://wiki.haskell.org/Foldable_Traversable_In_Prelude

Thanks for the insights. So what then does the t a part mean? In the
simpler version t a is [a]. Is t some container? It's not meant to be a
function, is it?
On Tue, Aug 3, 2021 at 5:39 PM Francesco Ariis
Hello Lawrence,
Il 03 agosto 2021 alle 17:14 Galaxy Being ha scritto:
According to this < http://zvon.org/other/haskell/Outputprelude/elem_f.html> elem is
Eq a => a -> [a] -> Bool
but according to my ghci :t it's this
elem :: (Foldable t, Eq a) => a -> t a -> Bool
I understand the first, but not the second, especially with the t. What is this saying extra, different from the first one?
Yup, some years ago there was a shift of some functions — after a big discussion, as there were a few controversial changes — from «working on lists only» to «working on all instances of some class» (in this case: Foldable, alias «data structures that can be folded») [1].
What is there for you? You gain the ability to use `elem` on other structures than lists (e.g. Trees). If you — like me — do not fancy reading signatures with too many typeclasses, you can always use +d in ghci to default to concrete types:
λ> :t foldr foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b λ> :t +d foldr foldr :: (a -> b -> b) -> b -> [a] -> b
[1] https://wiki.haskell.org/Foldable_Traversable_In_Prelude _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- ⨽ Lawrence Bottorff Grand Marais, MN, USA borgauf@gmail.com

Il 03 agosto 2021 alle 19:36 Galaxy Being ha scritto:
Thanks for the insights. So what then does the t a part mean? In the simpler version t a is [a]. Is t some container? It's not meant to be a function, is it?
`t` is any unary type constructor. Maybe, Tree, etc. all take one parameter (Maybe a, Tree a, etc.); in a similar fashion [] takes one parameter ([] a). [a] is just syntactic sugar λ> [7] :: [Int] [7] λ> [7] :: [] Int [7]

To add, t is typically some kind of container although it doesn’t have to
be. For instance, “t a” could be a “list (of) cat(s)”, except it doesn’t
*have* to be a list (can be anything that’s an instance of Foldable), and
it doesn’t *have* to be cats (could be a giraffe or a piano or any other
type).
In “list (of) cat(s)”, list can be thought of as a type-level function that
takes a type (cat) to construct a concrete type (list cat), hence the
notation.
This shows you commonly defined instances of Foldable (notice that the very
first one is a list, denoted by [ ]).
https://hackage.haskell.org/package/Cabal-3.4.0.0/docs/Distribution-Compat-P...
—
RRI
On Tue, Aug 3, 2021 at 18:06 Francesco Ariis
Il 03 agosto 2021 alle 19:36 Galaxy Being ha scritto:
Thanks for the insights. So what then does the t a part mean? In the simpler version t a is [a]. Is t some container? It's not meant to be a function, is it?
`t` is any unary type constructor. Maybe, Tree, etc. all take one parameter (Maybe a, Tree a, etc.); in a similar fashion [] takes one parameter ([] a). [a] is just syntactic sugar
λ> [7] :: [Int] [7] λ> [7] :: [] Int [7]
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- -- Ramnath R Iyer
participants (3)
-
Francesco Ariis
-
Galaxy Being
-
Ramnath R Iyer