[Haskell-cafe] Foldable for (,)
Dmitry Olshansky
olshanskydr at gmail.com
Wed May 3 09:34:30 UTC 2017
Foldable is required tor datatype to be a Traversable:
class (Functor t, Foldable t) => Traversable (t :: * -> *)
((,) a) is Functor. To be Traversable it has to be Foldable.
If ((,) a) is Traversable then we can write:
sequence (1, Just 2) -- == Just (1,2)
sequence (1, Nothing) -- == Nothing
There could be other useful classes or functions which required Foldable.
An other side, a tuple (with parameterized second part) can be a part of
complex datatype and possibly we need Foldable or Traversable instance for
that type.
If someone inhabits to think about tuple as a Functor, he/she can think
about tuple as Foldable and Traversable as well:
fmap (+1) (1,2) == (1,3)
foldMap (+1) (1,2) == 3
There are other datatypes with similar Foldable instances. I mean a least
Identity.
length (Identity [1,2,3]) == 1
2017-05-03 11:41 GMT+03:00 Jonathon Delgado <voldermort at hotmail.com>:
> Thank you for your explanation, but I think I'm missing something basic.
> Lists can have a variable length, so it makes sense to have operations that
> return the length or operate over a set. As ((,) a) can only have one
> value, the Foldable operations appear to be redundant as well as misleading
> (by implying that there could be more than one value).
>
> From: Haskell-Cafe <haskell-cafe-bounces at haskell.org> on behalf of Tony
> Morris <tonymorris at gmail.com>
> Sent: 03 May 2017 08:32
> To: haskell-cafe at haskell.org
> Subject: Re: [Haskell-cafe] Foldable for (,)
>
> It's Foldable for ((,) a).
>
> It is not Foldable for any of these things:
>
> * (,)
> * tuples
> * pairs
>
> In fact, to talk about a Foldable for (,) or "tuples" is itself a kind
> error. There is no good English name for the type constructor ((,) a)
> which I suspect, along with being unfamiliar with utilising the
> practical purpose of types (and types of types) is the root cause of all
> the confusion in this discussion.
>
> Ask yourself what the length of this value is:
>
> [[1,2,3], [4,5,6]]
>
> Is it 6? What about this one:
>
> [(1, 'a'), (undefined, 77)]
>
> Is it 4? No, obviously not, which we can determine by:
>
> :kind Foldable :: (* -> *) -> Constraint
> :kind [] :: * -> *
>
> Therefore, there is no possible way that the Foldable instance for []
> can inspect the elements (and determine that they are pairs in this
> case). By this method, we conclude that the length of the value is 2. It
> cannot be anything else, some assumptions about length itself put aside.
>
> By this ubiquitous and very practical method of reasoning, the length of
> any ((,) a) is not only one, but very obviously so.
>
> On 03/05/17 17:21, Jonathon Delgado wrote:
> > I sent the following post to the Beginners list a couple of weeks ago
> (which failed to furnish an actual concrete example that answered the
> question). Upon request I'm reposting it to Café:
> >
> > I've seen many threads, including the one going on now, about why we
> need to have:
> >
> > length (2,3) = 1
> > product (2,3) = 3
> > sum (2,3) = 3
> > or (True,False) = False
> >
> > but the justifications all go over my head. Is there a beginner-friendly
> explanation for why such seemingly unintuitive operations should be allowed
> by default?
> > _______________________________________________
> > Haskell-Cafe mailing list
> > To (un)subscribe, modify options or view archives go to:
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>
>
> Haskell-Cafe Info Page
> mail.haskell.org
> This mailing list is for the discussion of topics related to Haskell. The
> volume may at times be high, as the scope is broader than the main Haskell
> mailing list.
>
> > Only members subscribed via the mailman list are allowed to post.
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170503/538fad2c/attachment.html>
More information about the Haskell-Cafe
mailing list