Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell-cafe] Is () a 0-length tuple?

0 views
Skip to first unread message

Pasqualino "Titto" Assini

unread,
Nov 6, 2009, 5:09:23 PM11/6/09
to haskell-cafe
Hi,

I just noticed that the json library maps () to a JSON empty array: [].

This makes sense if () is a 0-length tuple, the (2,"hi") tuple for
example maps to [2,"hi"]

But is it so in Haskell?

In what sense () is a 0-length tuple?

Thanks,

titto
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Ross Mellgren

unread,
Nov 6, 2009, 5:14:28 PM11/6/09
to Pasqualino Titto Assini, haskell-cafe
It is kind of a 0-length tuple, if you squint, though it's usually
called "unit". Each tuple type is entirely distinct from each other,
so there's nothing that indicates that something is a tuple except for
the spelling -- (a,b,c). For example, unlike python, there is no way
to determine the length of a tuple at runtime (e.g. length ()) nor
iterate at runtime (as the types are heterogeneous).

-Ross

John Dorsey

unread,
Nov 6, 2009, 5:39:28 PM11/6/09
to haskell-cafe
> In what sense () is a 0-length tuple?

In what sense isn't it?

Data.Tuple is much to narrow to be of any use here. () is in at least most,
if not all, of the type classes that tuples are in. The syntax is
strikingly similar.

If you ask me, it walks/quacks/smells like a duck, so it's a duck.

Regards,
John

Pasqualino "Titto" Assini

unread,
Nov 7, 2009, 2:00:33 PM11/7/09
to haskell-cafe
The syntax is similar, but what else is?

In JavaScript there is a "null" value, that is the only value of the null type.

Isn't () the same thing? �The only value of the unary type?

Best,

� � � � � � � � titto


2009/11/6 John Dorsey <has...@colquitt.org>:

--
Pasqualino "Titto" Assini, Ph.D.
http://quicquid.org/

Eugene Kirpichov

unread,
Nov 7, 2009, 2:09:53 PM11/7/09
to Pasqualino Titto Assini, haskell-cafe
2009/11/7 Pasqualino "Titto" Assini <titto...@gmail.com>:

> The syntax is similar, but what else is?
>
> In JavaScript there is a "null" value, that is the only value of the null type.
>
> Isn't () the same thing? �The only value of the unary type?
>
No, () has two values: () and undefined (t.i., _|_).

> Best,
>
> � � � � � � � � titto
>
>
> 2009/11/6 John Dorsey <has...@colquitt.org>:
>>> In what sense () is a 0-length tuple?
>>
>> In what sense isn't it?
>>
>> Data.Tuple is much to narrow to be of any use here. �() is in at least most,
>> if not all, of the type classes that tuples are in. �The syntax is
>> strikingly similar.
>>
>> If you ask me, it walks/quacks/smells like a duck, so it's a duck.
>>
>> Regards,
>> John
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskel...@haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
>
>
> --
> Pasqualino "Titto" Assini, Ph.D.
> http://quicquid.org/
> _______________________________________________
> Haskell-Cafe mailing list
> Haskel...@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

--
Eugene Kirpichov
Web IR developer, market.yandex.ru

Matthew Gruen

unread,
Nov 7, 2009, 2:21:04 PM11/7/09
to haskell-cafe
Forgot to cc haskell-cafe. Trying again:

---------- Forwarded message ----------
From: Matthew Gruen <wikigra...@gmail.com>
Date: Sat, Nov 7, 2009 at 2:16 PM
Subject: Re: [Haskell-cafe] Is () a 0-length tuple?
To: Pasqualino Titto Assini <titto...@gmail.com>

On Sat, Nov 7, 2009 at 2:00 PM, Pasqualino "Titto" Assini
<titto...@gmail.com> wrote:
> The syntax is similar, but what else is?
>
> In JavaScript there is a "null" value, that is the only value of the null type.
>
> Isn't () the same thing? �The only value of the unary type?
>
> Best,
>
> � � � � � � � � titto
>

> Pasqualino "Titto" Assini, Ph.D.
> http://quicquid.org/

In JavaScript's case, there is not a null type. The null value belongs
to the 'object' type, whereas the undefined value belongs to the
'undefined' type. This is all a lot less useful when you realize that
JavaScript has a dynamic type system. But this is JSON, not
JavaScript.

In JSON, arrays, objects, strings, and numbers can be any number of
values. Booleans can be two values. Null can only be one value.
Personally, I think a better mapping for () would be JSNull, since
both have only one value in normal form. However, there is not
necessarily any natural mapping between Haskell values and JSON
values. The library tries to provide as many as possible, including
(), which it happens to map to JSArray [] instead of JSNull. As long
as the library is internally consistent, though, it should be fine.

Ketil Malde

unread,
Nov 8, 2009, 3:52:19 AM11/8/09
to haskell-cafe
Eugene Kirpichov <ekirp...@gmail.com> writes:

>> In JavaScript there is a "null" value, that is the only value of the null type.
>> Isn't () the same thing?  The only value of the unary type?

> No, () has two values: () and undefined (t.i., _|_).

I'd argue that yes, they're the same thing, since any function returning
"null" in JS might also fail to terminate or terminate with an
exception.

-k
--
If I haven't seen further, it is by standing in the footprints of giants

Svein Ove Aas

unread,
Nov 8, 2009, 4:54:00 AM11/8/09
to Ketil Malde, haskell-cafe
On Sun, Nov 8, 2009 at 9:52 AM, Ketil Malde <ke...@malde.org> wrote:
> Eugene Kirpichov <ekirp...@gmail.com> writes:
>
>>> In JavaScript there is a "null" value, that is the only value of the null type.
>>> Isn't () the same thing?  The only value of the unary type?
>
>> No, () has two values: () and undefined (t.i., _|_).
>
How should I put it..?
undefined is bottom, but bottom is not undefined?

There are plenty of other constructions that are bottom. Infinite
loops, throws, errors.. common for all of them, of course, is that you
can't pattern-match on them or otherwise use them in pure code, and
they generally don't act like values.

So, can't we just say that () has a single value, namely ()? It'd make
this much simpler, and we won't have to deal with the Nihil monoid.

==

data Nihil

instance Monoid Nihil where
mappend _ _ = undefined

--
Svein Ove Aas

Conor McBride

unread,
Nov 8, 2009, 5:04:08 AM11/8/09
to haskell-cafe Cafe
How about this?

{-# LANGUAGE ThinkTotal #-}

On 8 Nov 2009, at 09:53, Svein Ove Aas wrote:

> On Sun, Nov 8, 2009 at 9:52 AM, Ketil Malde <ke...@malde.org> wrote:
>> Eugene Kirpichov <ekirp...@gmail.com> writes:
>>
>>>> In JavaScript there is a "null" value, that is the only value of
>>>> the null type.
>>>> Isn't () the same thing? The only value of the unary type?
>>
>>> No, () has two values: () and undefined (t.i., _|_).

() is the only value of ().

If we could agree a standard set of email pragmas, we could save
ourselves a lot of violent agreement.

Cheers

Conor

Jon Fairbairn

unread,
Nov 8, 2009, 5:14:50 AM11/8/09
to haskel...@haskell.org
"Pasqualino \"Titto\" Assini" <titto...@gmail.com> writes:

> The syntax is similar, but what else is?

What would you expect from an empty tuple?

(a,b,c) has a constructor function p3 a b c = (a,b,c) and three
destructor functions s3_1 (a,b,c) = a, s3_2 (a,b,c) = b and s3_3 (a,b,c)=c

(a,b) has a constructor function p2 a b = (a,b) and two destructor functions
s2_1 (a,b) = a and s2_2 (a,b) = b

(a) has a constructor function p1 a = (a) and one destructor function
s1_1 a = a

() has a constructor function p0 = () and zero destructor functions.

> In JavaScript there is a "null" value, that is the only value of the
> null type.

I'm not sure that Javascript is a suitable place to get intuitions for
Haskell (null type would seem more like empty than single), but anyway,
the thing is that the sole (non-bottom) value of the /unit/ type is the
same as the empty tuple¹.

> Isn't () the same thing?  The only value of the unary type?

The "empty" type in Haskell would be (forall a.a) which has no
non-bottom values.


[1] Aside: I wanted haskell to share the same type for all empty values
(ie lists would have been symmetric unions of pairs with the unit type
List t = (t,List t) || ()), but that didn't fit with algebraic datatypes
so the design went a different way.
--
Jón Fairbairn Jon.Fa...@cl.cam.ac.uk

Deniz Dogan

unread,
Nov 8, 2009, 6:22:07 AM11/8/09
to Matthew Gruen, haskell-cafe
2009/11/7 Matthew Gruen <wikigra...@gmail.com>:

What point are you trying to make by distinguishing JSON from
JavaScript? JSON is a subset of JavaScript, they share the same type
system. "Null can be only one value." This doesn't make sense to me,
since as you say null is not a type, but a value.

--
Deniz Dogan

Ketil Malde

unread,
Nov 8, 2009, 6:44:21 AM11/8/09
to haskel...@haskell.org
Jon Fairbairn <jon.fa...@cl.cam.ac.uk> writes:

> The "empty" type in Haskell would be (forall a.a) which has no
> non-bottom values.

With an extension, you can also define:

data Void -- without any constructors

which is perhaps closer to null types in other languages?

-k
--
If I haven't seen further, it is by standing in the footprints of giants

Matthew Gruen

unread,
Nov 8, 2009, 4:10:31 PM11/8/09
to Deniz Dogan, haskell-cafe
On Sun, Nov 8, 2009 at 6:21 AM, Deniz Dogan <deniz.a...@gmail.com>
wrote:

> What point are you trying to make by distinguishing JSON from
> JavaScript? JSON is a subset of JavaScript, they share the same type
> system. "Null can be only one value." This doesn't make sense to me,
> since as you say null is not a type, but a value.
>
> --
> Deniz Dogan
>

It seems I underestimated the typedness of null in JavaScript :) I checked
the ECMAScript specification, and it does refer to a "null type".. so titto
was right.[1] My opinion is that JSON's 'type system' should be analyzed
orthogonal to JavaScript's regardless. If JSON is a subset of JavaScript, it
is primarily a syntactic one. When I said "Null can be only one value",
implying that null is a type, I was referring to JSON's null, not
JavaScript's null. In JSON, null *is* definitely a unit type. When
considering mappings between Haskell and JSON in the case of (), we should
see that () is a unit type in Haskell, null is a unit type in JSON
(regardless of its role in JavaScript), and maybe try to associate them.

�Matt

[1] I was misled by the fact that typeof null = 'object'. The logic behind
this, I think, is that null is meant to be bound to a variable that would
otherwise be a reference to an actual object value. Many have criticized
this result, e.g. Douglas Crockford (
http://javascript.crockford.com/remedial.html)

Deniz Dogan

unread,
Nov 8, 2009, 4:49:32 PM11/8/09
to Matthew Gruen, haskell-cafe
2009/11/8 Matthew Gruen <wikigra...@gmail.com>:

Let's keep in mind when reading the ECMAScript specification that
JavaScript is merely based on it and breaks it on several different
points. :)

0 new messages