Data.Foldable.all and empty list

Hello, Is there any particular reason why the 'all' function returns True when supplied with an empty list (or any other foldable)? I'm aware that it is implemented via All monoid instance, my question is whether this behaviour is intentional or it can be considered a bug.

Completely intentional. 'all' tests whether the predicate is true for all elements; if there are no elements, it's vacuously true (cf. forall quantifier in logic). On 9/29/2015 11:33 PM, Lana Black wrote:
Hello,
Is there any particular reason why the 'all' function returns True when supplied with an empty list (or any other foldable)? I'm aware that it is implemented via All monoid instance, my question is whether this behaviour is intentional or it can be considered a bug. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

It's intentional. The behavior makes sense (although it's a bit unintuitive at first) and follows common mathematical convention. For one, it makes sense logically: it is vacuously true that every element of an empty list satisfies any predicate. That's how forall works normally. Think about it by analogy to implication: false implies anything (ie False -> a is true no matter what a is). It also makes sense when looking at these as a fold: the folding operation is && and True is the identity element for &&. In a broad sense, True is naturally the "empty" case for && so that's where we start from. It's the same for say sum (an empty sum is 0) and product (an empty product is 1).
From a practical standpoint, this default neatly avoids a bunch of special cases, meaning we don't have to check if a list is empty before aggregating it. It's something that's worth understanding and relying in because it makes your code simpler and more regular. On Sep 29, 2015 14:33, "Lana Black"
wrote:
Hello,
Is there any particular reason why the 'all' function returns True when supplied with an empty list (or any other foldable)? I'm aware that it is implemented via All monoid instance, my question is whether this behaviour is intentional or it can be considered a bug. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

All the spiders in my hand are purple. There are no spiders in my hand. On 30/09/15 07:33, Lana Black wrote:
Hello,
Is there any particular reason why the 'all' function returns True when supplied with an empty list (or any other foldable)? I'm aware that it is implemented via All monoid instance, my question is whether this behaviour is intentional or it can be considered a bug. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

On Wed, Sep 30, 2015 at 4:33 AM, Lana Black
Is there any particular reason why the 'all' function returns True when supplied with an empty list (or any other foldable)? I'm aware that it is implemented via All monoid instance, my question is whether this behaviour is intentional or it can be considered a bug.
Hi Lana, this is a good question, variants of which are actually asked quite often. The function 'all' has type signature (a -> Bool) -> [a] -> Bool. Given a list of as that's split arbitrarily into bs and cs such that as == bs ++ cs, 'all' has the property that: all p as == all p bs && all p cs, for any boolean predicate p. So if bs or cs is the empty list, the property forces all f [] == True. For a similar Q&A on related functions, see https://groups.google.com/forum/#!msg/elm-discuss/sPS98RnV0Og/dSddTp8VXYcJ -- Kim-Ee
participants (5)
-
David Kraeutmann
-
Kim-Ee Yeoh
-
Lana Black
-
Tikhon Jelvis
-
Tony Morris