Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml Format and structural boxes: Why does my output not match the example?

Tags:

io

ocaml

There is an example on the Using the Format module page on the OCaml web site that gives an example of the differences between structural and packing hov boxes. I am trying to replicate this example. (I am using OCaml 3.12.1.)

With the following input:

Format.printf "@[<hov 2>(---@\n@[<hov 2>(---@\n@[<hov 2>(---@,)@]@,)@]@,)@]@\n"

I get the expected output:

(---
  (---
    (---)))

What I can not figure out is how to get the output the page ascribes to "structural boxes":

(---
  (---
    (---
    )
  )
)

I was uncertain as to which Format.printf identifier maps to these boxes, so I tried several variations:

Format.printf "@[<hv 2>(---@\n@[<hv 2>(---@\n@[<hv 2>(---@,)@]@,)@]@,)@]@\n"
Format.printf "@[<2>(---@\n@[<2>(---@\n@[<2>(---@,)@]@,)@]@,)@]@\n"
Format.printf "@[<b 2>(---@\n@[<b 2>(---@\n@[<b 2>(---@,)@]@,)@]@,)@]@\n"

But all of the above give the same output as with the <hov 2> initial example. Would anyone have any ideas how I can get output similar to the second example from web page?

like image 716
md5i Avatar asked Sep 23 '12 03:09

md5i


1 Answers

I would have thought that your second example (with hv boxes) should work. But it seems that @\n does not have the behavior we expect.

Solution 1: force the line break before the right parenthesis, i.e. replace @, by @\n.

let () =
  Format.printf "@.";
  Format.printf "@[<hov 2>(---@\n@[<hov 2>(---@\n@[<hov 2>(---@\n)@]@\n)@]@\n)@]@\n";
  Format.printf "@."

Result:

(---
  (---
    (---
      )
    )
  )

Problem: there will always be a line break, it will never print:

(---(---(---)))

even if it has the room for it. If that is an issue for you, see solution 2.

Solution 2: Use longer lines :) If the break is caused by a long line then an hv box does break the line before the closing parenthesis.

let () =
  Format.printf "@.";
  Format.printf "@[<hv 2>(------------------------------------------------------------------------@,@[<hv 2>(---@,@[<hv 2>(---@,)@]@,)@]@,)@]";
  Format.printf "@."

Result:

(------------------------------------------------------------------------
  (---(---))
  )

However, the closing parenthesis is not aligned with the opening one.

Solution 3: If you want the right parenthesis to be aligned with the left one, you need two boxes.

let () =
  Format.printf "@.";
  Format.printf
    "@[<hv>@[<hv 2>(------------------------------------------------------------------------\
     @[<hv>@[<hv 2>(------------------------------------------------------------------------\
     @[<hv>@[<hv 2>(------------------------------------------------------------------------\
     @]@,)@]@]@,)@]@]@,)@]";
  Format.printf "@."

Result:

(------------------------------------------------------------------------
  (------------------------------------------------------------------------
    (------------------------------------------------------------------------
    )
  )
)
like image 124
Bardou Avatar answered Nov 15 '22 12:11

Bardou