[Haskell-cafe] Foldable for (,)

Olaf Klinke olf at aatal-apotheke.de
Thu May 4 19:20:42 UTC 2017


> Am 03.05.2017 um 23:20 schrieb M Farkas-Dyck <m.farkasdyck at gmail.com>:
> 
> On 03/05/2017, Olaf Klinke <olf at aatal-apotheke.de> wrote:
>> Streams can be given a Traversable instance easily:
>> 
>> instance Traversable Stream where
>>  sequenceA (Cons fa fas) = liftA2 Cons fa (sequenceA fas)
>> 
>> For some applicatives this will not terminate. For streams of Maybes,
>> sequenceA will either yield Just an infinite stream or return Nothing.
> 
> I believe this will never return `Just` an infinite stream — rather,
> it will not terminate.
Of course you are right, I did not test - the program can only be certain of the Just constructor after knowing that no Nothing occurs in the infinite stream. So perhaps the applicatives where sequenceA terminates and the case where foldMap terminates are the same? 

Anyways, Data.Traversable has foldMapDefault. Thus it seems the original argument that ((,) a) must have a Foldable instance because we want the Traversable instance is valid. For the same reason we made Applicative a superclass of Monad. One might hence wonder whether the brevity and convenience bought by the Traversable instance is worth the confusion brought about by the Foldable instance. 
There are similar cases in the mathematical world. Most mathematicians are happy to use the Axiom of Choice, knowing that accepting it leads to things like the Banach-Tarski Paradox. Then there are constructivists who deny the existence of non-measurable sets (because you need the Axiom of Choice to prove their existence), and the Banach-Tarski Paradox goes away. By this analogy, the Foldable instance of ((,) a) is the Banach-Tarski paradox which we accept and avoid to obtain other nice things we need. 

Olaf


More information about the Haskell-Cafe mailing list