[m-users.] Tuples as defined by the Book

Mark Brown mark at mercurylang.org
Tue Feb 22 10:13:08 AEDT 2022


Hi Fabrice,

On Tue, Feb 22, 2022 at 9:33 AM Fabrice Nicol <fabrnicol at gmail.com> wrote:
>
> Hi everyone,
>
> I confess to being a bit befuddled at the somewhat terse and elusive definition given by the reference manual on Type types (3.1.2.2):
>
> "The tuple types are {}, {T}, {T1, T2}, . . .
> A tuple type is equivalent to a discriminated union type (see Section 3.2.1 [Discriminated
> unions], page 33) with declaration
>     :- type {Arg1, Arg2, ..., ArgN}
>         --->     { {Arg1, Arg2, ..., ArgN} }."
>
> The equivalence with discriminated union types is clear; the type declaration is a bit less so.

I agree this is confusing. It would be better to just give examples of
tuple types and corresponding values, in my opinion.

The trick with this example is that the outer braces are a kind of
escape that prevents the compiler from interpreting the constructor as
being part of the syntax of the declaration itself, so they allow you
to define ';'/2 as a constructor, for example. In this case, the
escape is needed to avoid the '{}' constructor being interpreted as an
escape, I guess.

>
> My understanding of tuples would rather be as follows:
>
> :- type {Arg1, Arg2, ..., ArgN}
>     --->     {} (Arg1, Arg2, ..., ArgN).
>
> In other words, tuple terms look to me as (instantiated) discriminated union types with anonymous constructors '{}' of arity N consisting of the two joined tokens '{' and '}'.
>
> This is evidenced among others by tests/hard_coded/tuple_test.m:30.
>
> I think the reference manual could perhaps rephrase its explanation in a more convincing and clear-cut way as above.
>
> I honestly do not understand the manual definition; to  me it irresistibly smacks of infinite recursion. Something vaguely Chomskian in me, against all odds, keeps rewriting this declaration as follows:
>
>     :- type {Arg1, Arg2, ..., ArgN}
>         --->     { { { Arg1, Arg2, ..., ArgN} } }.
>
>     :- type {Arg1, Arg2, ..., ArgN}
>         --->     { { { { Arg1, Arg2, ..., ArgN} } } }.
>
>     :- type {Arg1, Arg2, ..., ArgN}
>         --->     { { { { { Arg1, Arg2, ..., ArgN} } } } }.
>
> (... to Infinity ...)
>
> somewhat hellishly.
>
> But there must be something I missed there; I would be grateful for any principled denial of these misconceptions of mine (while we are all still alive).

Hopefully a little escapology helps :-)

Mark

>
> Fabrice
>
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users


More information about the users mailing list