caml-list - the Caml user's mailing list
 help / color / Atom feed
* [Caml-list] Printer for lists in toplevel is different when opening List
@ 2020-02-26 15:30 Christophe Garion
  2020-02-26 15:51 ` Nicolás Ojeda Bär
  2020-02-26 18:36 ` Florian Angeletti
  0 siblings, 2 replies; 5+ messages in thread
From: Christophe Garion @ 2020-02-26 15:30 UTC (permalink / raw)
  To: caml-list

Hello,

I am using OCaml 4.09.0 from OPAM and I have just noticed something
peculiar using the toplevel.

When evaluating 1 :: 2 :: [], the list is printed as usual:

# 1 :: 2 :: 3 :: [] ;;
- : int list = [1; 2; 3]

If I open first List, then the printing is different:

# open List ;;
# 1 :: 2 :: 3 :: [] ;;
- : int List.t = (::) (1, [2; 3])

The list is now typed with the type t used in the List module signature
and the list representation is different. It seems that the "classic"
printing function is used for the tail of the list, but not for the
first Cons of the list.

Is it normal?

Best regards,

Christophe

--
Christophe Garion
GPG: 1982 15B2 64AC 3C34 532D  BF19 6CD6 246C 62DA 5A7F

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Printer for lists in toplevel is different when opening List
  2020-02-26 15:30 [Caml-list] Printer for lists in toplevel is different when opening List Christophe Garion
@ 2020-02-26 15:51 ` Nicolás Ojeda Bär
  2020-02-26 20:55   ` Christophe Garion
  2020-02-26 18:36 ` Florian Angeletti
  1 sibling, 1 reply; 5+ messages in thread
From: Nicolás Ojeda Bär @ 2020-02-26 15:51 UTC (permalink / raw)
  To: Christophe Garion; +Cc: OCaml Mailing List

Dear Christophe,

Looks like a bug in the toplevel printer to me (and it is still
present in 4.10).
Could you please open a bug report at

    https://github.com/ocaml/ocaml/issues

with the description of the issue as in your message? Thanks!

Best wishes,
--
Nicolás OJEDA BÄR
nicolas.ojeda.bar@lexifi.com
https://www.lexifi.com

Le mer. 26 févr. 2020 à 16:30, Christophe Garion
<tofgarion@runbox.com> a écrit :
>
> Hello,
>
> I am using OCaml 4.09.0 from OPAM and I have just noticed something
> peculiar using the toplevel.
>
> When evaluating 1 :: 2 :: [], the list is printed as usual:
>
> # 1 :: 2 :: 3 :: [] ;;
> - : int list = [1; 2; 3]
>
> If I open first List, then the printing is different:
>
> # open List ;;
> # 1 :: 2 :: 3 :: [] ;;
> - : int List.t = (::) (1, [2; 3])
>
> The list is now typed with the type t used in the List module signature
> and the list representation is different. It seems that the "classic"
> printing function is used for the tail of the list, but not for the
> first Cons of the list.
>
> Is it normal?
>
> Best regards,
>
> Christophe
>
> --
> Christophe Garion
> GPG: 1982 15B2 64AC 3C34 532D  BF19 6CD6 246C 62DA 5A7F

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Printer for lists in toplevel is different when opening List
  2020-02-26 15:30 [Caml-list] Printer for lists in toplevel is different when opening List Christophe Garion
  2020-02-26 15:51 ` Nicolás Ojeda Bär
@ 2020-02-26 18:36 ` Florian Angeletti
  2020-02-26 21:00   ` Christophe Garion
  1 sibling, 1 reply; 5+ messages in thread
From: Florian Angeletti @ 2020-02-26 18:36 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1062 bytes --]

The printer for lists is indeed quite sensitive to the exact path of the 
type being printed.

Any manipulation that muddles this path makes the printer revert to the 
generic sum type printer.

Moreover, in the case of `List`, the list type is re-exported as

type |'a| t  =|'a list|  =
| []
| (::) of 'a * 'a list

Thus after opening the List module, the toplevel printer sees

1 :: (2 :: ...)

as

( 1 :: (2 :: ... : 'a list) : 'a t)

And since t is different that list, it prints the value as

(::)(1, ...)

then starting from the second element, we are back to 'a list and the 
pretty list printer.

This means that we can make the situation worse by re-exporting the type 
list as

type |'a| t  =|'a list|  =
| []
| (::) of 'a * 'a t

With this definition, we completely confuse the pretty printer for lists 
and are rewarded with:

# [1;2];;

- : int t = (::) (1, (::) (2, []))

If you are interested, I have a small patch that makes the toplevel 
printer identify the list type more reliably: 
https://github.com/ocaml/ocaml/pull/9336 .

— octachron.


[-- Attachment #2: Type: text/html, Size: 1751 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Printer for lists in toplevel is different when opening List
  2020-02-26 15:51 ` Nicolás Ojeda Bär
@ 2020-02-26 20:55   ` Christophe Garion
  0 siblings, 0 replies; 5+ messages in thread
From: Christophe Garion @ 2020-02-26 20:55 UTC (permalink / raw)
  To: Nicolás Ojeda Bär; +Cc: OCaml Mailing List


On Wed, Feb 26 2020 at 04:51:19 PM, Nicolás Ojeda Bär <nicolas.ojeda.bar@lexifi.com> wrote:

> Dear Christophe,

Hi Nicolás,

> Looks like a bug in the toplevel printer to me (and it is still
> present in 4.10).
> Could you please open a bug report at
>
>     https://github.com/ocaml/ocaml/issues
>
> with the description of the issue as in your message? Thanks!

No problem, I will open a bug report. I will also reference Florian PR
#9336 that solve the problem.

Best,

Christophe

--
Christophe Garion
GPG: 1982 15B2 64AC 3C34 532D  BF19 6CD6 246C 62DA 5A7F

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] Printer for lists in toplevel is different when opening List
  2020-02-26 18:36 ` Florian Angeletti
@ 2020-02-26 21:00   ` Christophe Garion
  0 siblings, 0 replies; 5+ messages in thread
From: Christophe Garion @ 2020-02-26 21:00 UTC (permalink / raw)
  To: Florian Angeletti; +Cc: caml-list


On Wed, Feb 26 2020 at 07:36:48 PM, Florian Angeletti <octa@polychoron.fr> wrote:

> The printer for lists is indeed quite sensitive to the exact path of
> the type being printed.
>
> Any manipulation that muddles this path makes the printer revert to
> the generic sum type printer.
>
> Moreover, in the case of `List`, the list type is re-exported as
>
> type |'a| t  =|'a list|  =
> | []
> | (::) of 'a * 'a list
>
> Thus after opening the List module, the toplevel printer sees
>
> 1 :: (2 :: ...)
>
> as
>
> ( 1 :: (2 :: ... : 'a list) : 'a t)
>
> And since t is different that list, it prints the value as
>
> (::)(1, ...)
>
> then starting from the second element, we are back to 'a list and the
> pretty list printer.

OK, thanks for the explanation!

> This means that we can make the situation worse by re-exporting the
> type list as
>
> type |'a| t  =|'a list|  =
> | []
> | (::) of 'a * 'a t
>
> With this definition, we completely confuse the pretty printer for
> lists and are rewarded with:
>
> # [1;2];;
>
> - : int t = (::) (1, (::) (2, []))
>
> If you are interested, I have a small patch that makes the toplevel
> printer identify the list type more reliably:
> https://github.com/ocaml/ocaml/pull/9336 .

Thanks, as Nicolás asked me to open an issue, I will point to your PR.

Best,

Christophe

--
Christophe Garion
GPG: 1982 15B2 64AC 3C34 532D  BF19 6CD6 246C 62DA 5A7F

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-26 15:30 [Caml-list] Printer for lists in toplevel is different when opening List Christophe Garion
2020-02-26 15:51 ` Nicolás Ojeda Bär
2020-02-26 20:55   ` Christophe Garion
2020-02-26 18:36 ` Florian Angeletti
2020-02-26 21:00   ` Christophe Garion

caml-list - the Caml user's mailing list

Archives are clonable:
	git clone --mirror http://inbox.vuxu.org/caml-list
	git clone --mirror https://inbox.ocaml.org/caml-list

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.caml-list


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git