Giter VIP home page Giter VIP logo

Comments (1)

sjakobi avatar sjakobi commented on August 16, 2024

On the surface, and without any detailed knowledge of the prettyprinter internals, it seems like an identity instance is trivial to add:

instance Pretty (Doc ann) where
  pretty = id

Unfortunately it's not quite this easy:

ghci> instance Pretty (Doc ann) where pretty = id

<interactive>:4:42: error:
    • Couldn't match type ‘ann1’ with ‘ann’
      Expected: Doc ann -> Doc ann1
        Actual: Doc ann -> Doc ann
      ‘ann1’ is a rigid type variable bound by
        the type signature for:
          pretty :: forall ann1. Doc ann -> Doc ann1
        at <interactive>:4:33-38
      ‘ann’ is a rigid type variable bound by
        the instance declaration
        at <interactive>:4:10-25
    • In the expression: id
      In an equation for ‘pretty’: pretty = id
      In the instance declaration for ‘Pretty (Doc ann)’
    • Relevant bindings include
        pretty :: Doc ann -> Doc ann1 (bound at <interactive>:4:33)

Is there a trick to make the annotation type of the input and output Docs match up?

Looking through the history, I noticed that there was a general instance before, but one that removed all annotations:

instance Pretty (Doc ann) where
  pretty = unAnnotate

This was surprising to me, since it wasn't clear why using pretty should remove pre-existing annotations.

The relevant commit seems to be b653d59. This was long before my involvement in this project, but it seems that the removal was motivated by the (performance) pitfall documented in the instance haddocks:

since this un-annotates its argument, nesting it means multiple, potentially costly, traversals over the 'Doc'.

Maybe @quchen can provide more context.

To get back to the instance, I wonder how to best avoid the unAnnotate pass. Maybe we could consider having an instance for Docs that cannot contain annotations, e.g.

instance Pretty (Doc Void) where
  pretty = unsafeCoerce

(dhall uses a similar trick.)

from prettyprinter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.