diku-dk / futhark

:boom::computer::boom: A data-parallel functional programming language
http://futhark-lang.org
ISC License
2.41k stars 166 forks source link

Switch to a better prettyprinting library #1156

Closed athas closed 2 years ago

athas commented 4 years ago

We currently use mainland-pretty for formatting type errors, warnings, the IR, and so on. This is because language-c-quote, which we use in the C backend, uses it, and it appeared simpler at the time to use the same library everywhere. Mainland-pretty is deficient in various areas, notably it is not very good for printing nice user-visible text with effects like colours and boldface, which we want for error messages.

I think that prettyprinter is the best alternative. It has a very similar API, so it's not particularly difficult to switch. One advantage of this library is that there are then libraries like prettyprinter-ansi-terminal which can be used to use terminal control codes, which can be automatically stripped when printing to something that doesn't support them (e.g. when piping compiler output to a file).

Munksgaard commented 4 years ago

I just spent a bit of time trying this out (https://github.com/diku-dk/futhark/commit/808c70b477482ca7822362d8e9eedbba6542b21a) and it turns out that prettyprinter doesn't support precedence handling (pprPrec).

It seems like we use pprPrec quite a lot in Futhark.Language.Pretty, I guess for handling infix operators and such.

The current advice from the creator of prettyprinter is to use annotations and add a custom renderer. However, this seems like a lot of work and I should probably be spending my time on my memory optimizations instead, so I'll put it to rest for now.

athas commented 4 years ago

We can easily just do our own parenthesis handling by using ordinary functions for prettyprinting subexpressions. Type classes are never a requirement.

But I agree that this is not something you should spend your time on.