Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:13:02+00:00 2026-06-12T15:13:02+00:00

I’m writing a pretty-printer for a simple white-space sensitive language. I like the Leijen

  • 0

I’m writing a pretty-printer for a simple white-space sensitive language.

I like the Leijen pretty-printer library more than I like the Wadler library, but the Leijen library has one problem in my domain: any line break I insert may be overridden by the group construct, which may compress any line, which might change the semantics of the output.

I don’t think I can implement an ungroupable line in the wl-pprint (although I’d love to be wrong).

Looking a bit at the wl-pprint-extras package, I don’t think that even the exposed internal interface allows me to create a line which will not be squashed by group.

Do I just have to rely on the fact that I never use group, or do I have some better option?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:13:03+00:00Added an answer on June 12, 2026 at 3:13 pm

    Given that you want to be able to group and you also need to be able to ensure some lines aren’t uninserted,
    why don’t we use the fact that the library designers encoded the semantics in the data type,
    instead of in code. This fabulous decision makes it eminently re-engineerable.

    The Doc data type encodes a line break using the constructor Line :: Bool -> Doc.
    The Bool represents whether to omit a space when removing a line. (Lines indent when they’re there.)
    Let’s replace the Bool:

    data LineBehaviour = OmitSpace | AddSpace | Keep
    
    data Doc = ...
        ...
        Line !LineBehaviour   -- not Bool any more
    

    The beautiful thing about the semantics-as-data design is that if we replace
    this Bool data with LineBehaviour data, functions that didn’t use it but
    passed it on unchanged don’t need editing. Functions that look inside at what
    the Bool is break with the change – we’ll rewrite exactly the parts of the code
    that need changing to support the new semantics by changing the data type where
    the old semantics resided. The program won’t compile until we’ve made all the
    changes we should, while we won’t need to touch a line of code that doesn’t
    depend on line break semantics. Hooray!

    For example, renderPretty uses the Line constructor, but in the pattern Line _,
    so we can leave that alone.

    First, we need to replace Line True with Line OmitSpace, and Line False with Line AddSpace,

    line = Line AddSpace
    
    linebreak = Line OmitSpace
    

    but perhaps we should add our own

    hardline :: Doc
    hardline = Line Keep
    

    and we could perhaps do with a binary operator that uses it

    infixr 5 <->
    (<->) :: Doc -> Doc -> Doc
    x <-> y = x <> hardline <> y
    

    and the equvalent of the vertical seperator, which I can’t think of a better name than very vertical separator:

    vvsep,vvcat :: [Doc] -> Doc
    vvsep = fold (<->)
    vvcat = fold (<->)
    

    The actual removing of lines happens in the group function. Everything can stay the same except:

    flatten (Line break)    = if break then Empty else Text 1 " "
    

    should be changed to

    flatten (Line OmitSpace)    = Empty
    flatten (Line AddSpace)     = Text 1 " "
    flatten (Line Keep)         = Line Keep
    

    That’s it: I can’t find anything else to change!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.