Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007770OCamltoplevelpublic2018-04-11 12:042019-02-06 13:58
Reporterdrup 
Assigned To 
PrioritynormalSeverityfeatureReproducibilityN/A
StatusacknowledgedResolutionopen 
PlatformOSOS Version
Product Version 
Target VersionFixed in Version 
Summary0007770: Improved API for toplevel printers
DescriptionThe current API for toplevel printers is not particularly convenient. It either require manual work from the user or ocamlfind predicate tricks.
One potential solution would be to annotate type declarations:

  type foo = ...
  [@@printer pp_foo]

When the toplevel tries to print an object of type foo, we lookup the definition, find the annotation, and that's it.

This ticket aims to discus the design of this before I start implementing it. There are various potential choices. `pp_foo` will be almost always declared *after* the current module, so we can't really typecheck at declaration time.
Either:
1. We assume `pp_foo` is local to the current module and we check its type once we are done with the rest of the module.
2. We don't typecheck anything statically and instead check dynamically, just like `#install_printer`. This solution also allows printers that are not local to the current scope (as long as they are in scope for printing, it's fine).
3. We wait for modular implicit, which provide a similar solution to the problem.

Opinions ?
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0019010)
dbuenzli (reporter)
2018-04-11 14:20

Nice to look at this Garbiel ! Can be related to https://caml.inria.fr/mantis/view.php?id=7589 [^] point 5.

I guess these annotations will go in cmis ? Didn't think it through but what about annotating the pretty printer with the type instead ?

If two annotations declare the same type the last one loaded takes over. This also has the advantage of being able to override or provide alternate pretty printers by simply loading a cmi.
(0019012)
dbuenzli (reporter)
2018-04-11 14:28
edited on: 2018-04-11 14:31

In fact I guess you don't even need to specify the type. You'd just have an [@@toploop] annotation and any function that sports this annotation when a cmi is loaded automatically given to #install_printer. Not sure if that's feasible though.

EDIT That's actually what Jérémie suggested here:

https://github.com/ocaml/dune/issues/114#issuecomment-315790361 [^]

(0019013)
drup (reporter)
2018-04-11 14:42

I didn't know jeremy got the same idea. Yes, the annotations would go in the cmis.

I'm not very convinced by the idea of annotating the printers since module types are loaded lazily in OCaml, in particular with module aliases.

Basically, if you have a Foo_printers module, or even Foo.Printers where Printers is a module alias, unless the cmi is loaded by other means (like calling Foo_printers.pp explicitly), the printer annotation will never be seen.
Annotating on the type avoids the issue.
Having your printers depend on which cmi the typechecker decided to load seem very unfriendly to me.

As far as order of printer is concerned, I would go like this:
- Starts from the type proposed by -short-alias
- For each type alias, try to find a printer at the type definition, if unsuccessful, resolve *one* layer of alias, and try again.
(0019014)
frisch (developer)
2018-04-11 18:15

An alternative approach would be to use some naming convention (deriving-like). For a value of a type [Foo.Bar.my_type], the toplevel would look e.g. for a printer
called [print_my_type] of type [Format.formatter -> Foo.Bar.my_type -> unit] in module [Foo.Bar].

This is a bit ad hoc but for the toplevel this might be ok.
(0019033)
drup (reporter)
2018-04-15 14:21

firsch: That's strictly less powerful than 1, and not much easier to implement. It's also far less flexible/retro-compatible. It doesn't even really help all that much for a future migration to a modular implicit based approach.
(0019038)
dim (developer)
2018-04-16 13:58

The reason I suggested annotating the printer rather than the type is that if you annotate the type, you end up typing the printer identifier twice. It seems to me that toplevel printers are generally defined in the same module as the types themselves, so the typer should see them.
(0019039)
drup (reporter)
2018-04-16 15:43

dim: This means you have one reasonable case (printer is local) and the other cases which might or might not work depending on the whims of the typechecker. I think it makes more sense to either support that one good case (proposition 1) or all the cases (proposition 2).
(0019117)
dim (developer)
2018-05-17 12:11

Personally, I prefer proposition 1 as we get errors immediately. When things are not checked, they tend to quickly get out of sync.
(0019548)
dim (developer)
2019-01-16 12:41
edited on: 2019-01-16 12:41

Drup, unless you are planning to look at this in the near future, I'm going to give a shot at implementing this feature.

(0019549)
drup (reporter)
2019-01-16 13:04

My time is very limited lately, so go ahead! I'll be happy to review and/or help when needed.
(0019574)
dim (developer)
2019-01-30 14:21

I had a first look at this, and thought a bit more about how we could adapt it so that we could get maps and sets printed in the toplevel.

I get the feeling that this is going to turn into a less good implementation of implicits. So I'm leaning towards simply abandoning this feature and waiting for implicits instead. In the meantime, the hack like the one I did in utop should be enough.
(0019579)
dim (developer)
2019-02-04 09:15

Just to clarify, it doesn't seem worth to me to invest too much time trying to solve this problem in a general way given than implicits will do exactly that in a more principled way.

However, we could import the same patch as I wrote for utop:

https://github.com/ocaml-community/utop/commit/fa3880d2a29ef1c33a67b686878c7215c9a725a3 [^]

It doesn't solve the problem in a general way, but is an improvement over the current situation until we get the proper solution.
(0019582)
dbuenzli (reporter)
2019-02-05 21:58
edited on: 2019-02-05 22:51

Having this only in `utop` is kind of useless since there are many places where it's not used (e.g. on the web). This means we still need to keep our `lib_top_init.ml` files around for these places (see for example [1] where this came up).

It may not solve the problem in a general way but then we don't even have an ETA on implicits. So if we can ensure this design will not collide with a future better one I'd rather have this in now even if that may solve only 95% of the cases. It seems the patch is rather light.

If you have time to upstream it @dim that would be very much appreciated.

[1]: https://github.com/owlbarn/owl/pull/360 [^]

(0019583)
dim (developer)
2019-02-06 13:58

Yh, I agree. TBH the patch itself is trivial, it's more everything around that always takes time: doc, tests, PR description, etc...


I have a few more urgent things to look at, but I'll come back to this once my stack clears up a bit.

- Issue History
Date Modified Username Field Change
2018-04-11 12:04 drup New Issue
2018-04-11 14:20 dbuenzli Note Added: 0019010
2018-04-11 14:28 dbuenzli Note Added: 0019012
2018-04-11 14:31 dbuenzli Note Edited: 0019012 View Revisions
2018-04-11 14:42 drup Note Added: 0019013
2018-04-11 18:15 frisch Note Added: 0019014
2018-04-15 14:21 drup Note Added: 0019033
2018-04-16 13:58 dim Note Added: 0019038
2018-04-16 15:43 drup Note Added: 0019039
2018-05-17 12:11 dim Note Added: 0019117
2018-05-23 20:13 xleroy Status new => acknowledged
2019-01-16 12:41 dim Note Added: 0019548
2019-01-16 12:41 dim Note Edited: 0019548 View Revisions
2019-01-16 13:04 drup Note Added: 0019549
2019-01-30 14:21 dim Note Added: 0019574
2019-02-04 09:15 dim Note Added: 0019579
2019-02-05 21:58 dbuenzli Note Added: 0019582
2019-02-05 22:51 dbuenzli Note Edited: 0019582 View Revisions
2019-02-06 13:58 dim Note Added: 0019583


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker