mainland / mainland-pretty

Other
10 stars 12 forks source link

expose alternative #9

Closed RasmusWL closed 9 years ago

RasmusWL commented 9 years ago

I've run into trouble when printing long let bindings in a compiler:

λ> putStrLn $ pretty 20 $ text "let" <+> text "variable" <+> equals <+> 
     text "function" <> encloseSep lparen rparen comma (map ppr [1..10])

gives

let variable = function(1,
                        2,
                        3,
                        4,
                        5,
                        6,
                        7,
                        8,
                        9,
                        10)

By exposing alt I can now do this:

λ> let (<???>) = \x y -> (x <+> y) `alt` (x </> (indent 2 y))
λ> putStrLn $ pretty 20 $ text "let" <+> text "variable" <+> equals <???> 
     text "function" <> encloseSep lparen rparen comma (map ppr [1..10])

which gives

let variable =
  function(1, 2, 3,
           4, 5, 6,
           7, 8, 9,
           10)

I haven't been able to see any other way of doing this, but if you have a better solution I'm all ears. (I don't want to go with always placing the expression on the next line)

I'm not entirely sure that I'm interpreting the invariant listed in the code correctly (all layouts of the two arguments flatten to the same layout), but I haven't been able to see any major problems with exposing alt.

mainland commented 9 years ago

I've exposed Alt as the <|> combinator.