Git Product home page Git Product logo

elm-pretty-printer's People

Contributors

pithub avatar pwentz avatar rupertlssmith avatar timjs avatar yetanotherminion avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

elm-pretty-printer's Issues

Make tail-recursion faster by removing the thunks.

Using a thunk \() -> ... may result in slow javascript code.

Instead do:

type Doc
    = Empty
    | Concatenate Doc Doc
    | Nest Int Doc
    | Flatten Doc -- Flatten the doc later.
    ... -- More constructors

flatten : Doc -> Doc
flatten doc =
    case doc of
        Concatenate doc1 doc2 ->
            Concatenate (Flatten doc1) (Flatten doc2)

        Nest i doc1 ->
            Nest i (Flatten doc1)

        ... -- More cases but they were already tail recursive.

someFunOverDocs doc =
    case someDoc of
        Flatten doc -> someFunOverDocs (flatten doc) -- lazily apply the flatten.

        .... -- Other cases as normal.

Fail line fitting sooner.

Now that flatten has been made lazy, the best function could be optimised to incrementally check the line fit without completely flattening the doc.

Should be able to reject large expressions fitted onto a single line sooner that way.

It doesn't look trivial to implement this optimization though, so I'll just leave it in this issue for now until it seems worth trying to implement it.

Long lists are blowing the stack.

When laying out a long list and choosing between putting it on a single line versus splitting into multiple lines, the flatten function is used to do the flattening onto a single line. This function is not tail-recursive so will overflow the stack when too many docs are being Concatenated together.

The part that is not TR is:

flatten : Doc -> Doc
flatten doc =
    case doc of
        Concatenate doc1 doc2 ->
            Concatenate (flatten doc1) (flatten doc2)

        Nest i doc1 ->
            Nest i (flatten doc1)

Tag a Document

Hi @rupertlssmith, for my port of the Elm compiler from Haskell to Elm I need a function like

setTag : t -> Doc t -> Doc t

which sets the tag for all the strings in an already assembled document.

If you like, I could create a pull request.

Instead of or additionally to this function I could implement a function like

updateTag : (Maybe t -> Maybe t) -> Doc t -> Doc t

or even

updateTag : (String -> Maybe t -> Maybe t) -> Doc t -> Doc t

which would give more control over the tag replacement, but I'm not sure whether something like this would be needed.

What do you think?

Give deeply nested softlines less precedent

I am making a pretty printer for a GHC core AST and thought that making the space between terms in an application a softline would be a good idea. However, it seems that the deepest nested softline is favoured to become a newline.

$wtest :: ByteArray# -> Int# -> Int# -> Int#
$wtest ww ww ww = let end = +# ww ww
in case $wcaseConvert upperMapping type (\i -> case i of
            { I# ipv -> case >=# ipv end of
                  { 1 -> Done type type
                    lwild -> case indexWord16Array# ww ipv of
                        { r# -> case geWord16# r# 55296 of
                              { 1 -> case leWord16# r# 56319 of
                                    { 1 -> case indexWord16Array# ww (+# ipv 1) of
                                          { r#1 -> Yield type type (C# (chr# (+# 9216 (+#
                                                                                       (uncheckedIShiftL#
                                                                                        (-#
                                                                                         (word2Int#
                                                                                          (word16ToWord#
                                                                                           r#))
                                                                                         55296) 10)
                                                                                       (word2Int#
                                                                                        (word16ToWord#
                                                                                         r#1))))))
                                                   (I# (+# ipv 2))
                                          }
                                      lwild2 -> Yield type type (C# (chr# (word2Int# (word16ToWord#
                                                                                      r#)))) (I# (+#
                                                                                                  ipv
                                                                                                  1))
                                    }
                                lwild1 -> Yield type type (C# (chr# (word2Int# (word16ToWord# r#))))
                                          (I# (+# ipv 1))
                              }
                        }

Is there a way to give the first softline precedent for considering as a newline?

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.