Quantcast

Foldable for (,)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Foldable for (,)

Jonathon Delgado
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?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Francesco Ariis
On Sun, Apr 23, 2017 at 08:21:34AM +0000, Jonathon Delgado wrote:

> 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?

Hello Jonathon,
    the proponents of `Foldable (a,)` see `(2,3)` not as a pair of 'equal'
values, but as a value *and* an annotation, much like some other folks
see Either as having a value (Right a) *or* an annotation (usually an
error in the form of Left e).

So to go back to your examples:

    (2,3)
     ^ ^
     | +------------- I am the value
     |
     +--------------- I am an annotation (and since tuples arguments can
                      be heterogeneous, I could be a String, a Bool,
                      anything).

If you agree with this paradigm, `length`, `sum` and friend become a
bit less icky.

I would prefer tuples to be unbiased, but this intuition helped me
connect with the people on the other side of the line.
Does this help?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Jonathon Delgado
In reply to this post by Jonathon Delgado
If a tuple only has one value, why do functions for operating over sets make sense at all? I can see from your explanations why the answers could be considered correct (if a particular convention is assumed), but why does the operation make sense at all? It seems like we're asking for the length of a single value, its product, etc.

Francesco Ariis wrote:

> 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?
Hello Jonathon,
    the proponents of `Foldable (a,)` see `(2,3)` not as a pair of 'equal'
values, but as a value *and* an annotation, much like some other folks
see Either as having a value (Right a) *or* an annotation (usually an
error in the form of Left e).

So to go back to your examples:

    (2,3)
     ^ ^
     | +------------- I am the value
     |
     +--------------- I am an annotation (and since tuples arguments can
                      be heterogeneous, I could be a String, a Bool,
                      anything).

If you agree with this paradigm, `length`, `sum` and friend become a
bit less icky.

I would prefer tuples to be unbiased, but this intuition helped me
connect with the people on the other side of the line.
Does this help?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Francesco Ariis
On Sun, Apr 23, 2017 at 10:06:00AM +0000, Jonathon Delgado wrote:
> If a tuple only has one value, why do functions for operating over sets
> make sense at all? I can see from your explanations why the answers could
> be considered correct (if a particular convention is assumed), but why
> does the operation make sense at all? It seems like we're asking for the
> length of a single value, its product, etc.

I can only guess: consistency. Once you create an instance of `Foldable`
you instantly get some functions "for free". Among those are foldMap,
foldr etc. *and* sum, length and friends.

I cannot see an occurrence where writing `length (x, y)` instead of 1
makes sense.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Jonathon Delgado
In reply to this post by Jonathon Delgado
So then the question becomes, why do tuples need Foldable if the functions it defines aren't useful?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Doug McIlroy
In reply to this post by Jonathon Delgado
> So to go back to your examples:
>
>     (2,3)
>      ^ ^
>      | +------------- I am the value
>      |
>      +--------------- I am an annotation (and since tuples arguments can
>                       be heterogeneous, I could be a String, a Bool,
>                       anything).
>
> If you agree with this paradigm, `length`, `sum` and friend become a
> bit less icky.

To me, "annotation" connotes auxiliary information--yet it will
be the primary key when pairs are sorted. I would be more
comfortable with the explanation if the field were referred
to as an "identifier", or some similar term.

Doug
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Tony Morris-4
In reply to this post by Jonathon Delgado
A tuple doesn't only have one value. forall a. ((,) a) only has one
value. Fortunately, Haskell has a kind system so we can easily determine
what length does.

> :k Foldable
Foldable :: (* -> *) -> Constraint

Clearly then, if we see a program (length x) where x is a tuple, then we
can easily determine that this a constant value 1.

For the same reason if we see (length [[1,2,3], [4,5,6]]) and ask, is
the length 2 or 6? It's clearly 2; just look at the kind of Foldable.

I like types, and types of types. Join me.

On 23/04/17 20:06, Jonathon Delgado wrote:
> If a tuple only has one value,



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Michael Orlitzky
On 04/23/2017 08:29 PM, Tony Morris wrote:
> A tuple doesn't only have one value. forall a. ((,) a) only has one
> value. Fortunately, Haskell has a kind system so we can easily determine
> what length does.

This all makes sense when you realize that Foldable is just the "Object"
class from Visual Basic. There's only one sensible way to define a
ToString() method on an arbitrary object; therefore it makes sense to
provide that method by default, and to have it do the one thing it can
do (print garbage).

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Jonathon Delgado
In reply to this post by Jonathon Delgado
Tony Morris - please could you give a (practical) example of code where the a tuple could realistically be passed to length, but you don't know what the answer will be at compile time?

Michael Orlitzky - everything in .NET has to descend from Object because of it's OO design. Why does tuple have to implement Foldable if it doesn't provide any useful functions?

Thank you very much everyone in this thread for helping me understand!
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

David Thomas
One thing that's been missed in this discussion is that constraints
can propagate.

Of course no one is wanting to pass something they know is a tuple
into a function they know is length.  But a function that expects
something Foldable might want to know length or sum, and it might be
reasonable to call that function on a tuple.

On Sun, Apr 23, 2017 at 11:52 PM, Jonathon Delgado
<[hidden email]> wrote:
> Tony Morris - please could you give a (practical) example of code where the a tuple could realistically be passed to length, but you don't know what the answer will be at compile time?
>
> Michael Orlitzky - everything in .NET has to descend from Object because of it's OO design. Why does tuple have to implement Foldable if it doesn't provide any useful functions?
>
> Thank you very much everyone in this thread for helping me understand!
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Tony Morris
In reply to this post by Jonathon Delgado
I only know that the length of any forall a. ((,) a) is going to be one. When it is polymorphic, I don't know that it is ((,) a) although it might be at the call site, and so I don't know the length.

On Mon, Apr 24, 2017 at 4:52 PM, Jonathon Delgado <[hidden email]> wrote:
Tony Morris - please could you give a (practical) example of code where the a tuple could realistically be passed to length, but you don't know what the answer will be at compile time?

Michael Orlitzky - everything in .NET has to descend from Object because of it's OO design. Why does tuple have to implement Foldable if it doesn't provide any useful functions?

Thank you very much everyone in this thread for helping me understand!
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Michael Orlitzky
In reply to this post by Jonathon Delgado
On 04/24/2017 02:52 AM, Jonathon Delgado wrote:
>
> Michael Orlitzky - everything in .NET has to descend from Object
> because of it's OO design. Why does tuple have to implement Foldable
> if it doesn't provide any useful functions?
>

That was sarcasm =P

I agree with you.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

amindfv
In reply to this post by David Thomas


> El 24 abr 2017, a las 03:20, David Thomas <[hidden email]> escribió:
>
> One thing that's been missed in this discussion is that constraints
> can propagate.
>
> Of course no one is wanting to pass something they know is a tuple
> into a function they know is length.  But a function that expects
> something Foldable might want to know length or sum, and it might be
> reasonable to call that function on a tuple.
>

Do you have a real-world example of a case where that's useful, and difficult to achieve in another (non-Foldable) way?

Genuinely asking, so that when we talk about what's gained/lost we have something concrete to talk about.

Tom



> On Sun, Apr 23, 2017 at 11:52 PM, Jonathon Delgado
> <[hidden email]> wrote:
>> Tony Morris - please could you give a (practical) example of code where the a tuple could realistically be passed to length, but you don't know what the answer will be at compile time?
>>
>> Michael Orlitzky - everything in .NET has to descend from Object because of it's OO design. Why does tuple have to implement Foldable if it doesn't provide any useful functions?
>>
>> Thank you very much everyone in this thread for helping me understand!
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Ryan Trinkle-3
I actually think the terminology may be the issue here: it's not tough to think of cases where one might need to use (sum . fmap (const 1) :: (Functor t, Foldable t) => t a -> Int), and it's also not tough to think of cases where the term "length" doesn't fit everyone's expectation for what that function does.  On the other hand, there isn't really any other Foldable-based implementation of 'length' that you can write.

On Mon, Apr 24, 2017 at 8:34 AM, <[hidden email]> wrote:


> El 24 abr 2017, a las 03:20, David Thomas <[hidden email]> escribió:
>
> One thing that's been missed in this discussion is that constraints
> can propagate.
>
> Of course no one is wanting to pass something they know is a tuple
> into a function they know is length.  But a function that expects
> something Foldable might want to know length or sum, and it might be
> reasonable to call that function on a tuple.
>

Do you have a real-world example of a case where that's useful, and difficult to achieve in another (non-Foldable) way?

Genuinely asking, so that when we talk about what's gained/lost we have something concrete to talk about.

Tom



> On Sun, Apr 23, 2017 at 11:52 PM, Jonathon Delgado
> <[hidden email]> wrote:
>> Tony Morris - please could you give a (practical) example of code where the a tuple could realistically be passed to length, but you don't know what the answer will be at compile time?
>>
>> Michael Orlitzky - everything in .NET has to descend from Object because of it's OO design. Why does tuple have to implement Foldable if it doesn't provide any useful functions?
>>
>> Thank you very much everyone in this thread for helping me understand!
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Foldable for (,)

Kim-Ee Yeoh
Administrator
In reply to this post by Jonathon Delgado
Hey Jonathon,

Based on the last email you sent, it seems like you're not quite satisfied with the responses.

And for good reason.

It turns out that there is more to 

length (2,3) = 1
sum (2,3) = product (2,3) = 3

than just the offending Traversable instance of the tuple pair (,).

Would you mind resending your first email of Apr 23 to the haskell-cafe mailing list?

If you do so, I'll contribute by opening a discussion of the Functor instance.


Best,
Kim-Ee


On Sunday, April 23, 2017, Jonathon Delgado <[hidden email]> wrote:
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?
_______________________________________________
Beginners mailing list
<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;Beginners@haskell.org&#39;)">Beginners@...
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


--
-- Kim-Ee

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...