• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Finally, you can manage your Google Docs, uploads, and email attachments (plus Dropbox and Slack files) in one convenient place. Claim a free account, and in less than 2 minutes, Dokkio (from the makers of PBworks) can automatically organize your content for you.



Page history last edited by Banned User 10 years, 10 months ago

Generating indented output with the Format module


This page should go to http://www.ocaml-tutorial.org once it is complete, i.e. has some kind of introduction and explains the basic concepts of the Format module.


The Format module of the standard library allows powerful pretty-printing, but can be difficult to master. Since indentation rules that we want to apply are very often the same, here is a small cookbook.


Useful links:



C-style functions


The goal is to indent according to these patterns:




function_name {

or, if it fits on one line:



function_name { instr1; instr2; }


What we don't want:





function_name { instr1;
                instr2; }

function_name { instr1; instr2;


The solution given by Xavier Leroy consists in printing "function_name" and the function body using only one box. They are not printed independently using a function that prints the function_name and another one that prints the body or any list of instructions enclosed in curly brackets.


The box starts just before "function_name" and ends after the closing bracket. It is defined with some indentation, for example 4 spaces. It means that normally, everything which doesn't fit on the first line will be printed with an additional indentation of 4 spaces relative to the beginning of the box. In addition to that default indentation, additional indentation can be specified when break hints are added. The special thing here is that the closing bracket must be in the box and at the same time should not be indented like the preceding lines. The solution is to set a break hint with negative indentation, so that the closing bracket gets aligned with the start of the box.


The solution is:



open Format

let pretty_decl ppf name l =
  fprintf ppf "@[<hv 4>%s {" name;
  List.iter (fun instr -> fprintf ppf "@ %s;" instr) l;
  fprintf ppf "@;<1 -4>}@]";;


Test in the toplevel:



# open Format;;
# let pretty_decl ppf name l =
  fprintf ppf "@[<hv 4>%s {" name;
  List.iter (f(fun instr -> fprintf ppf "@ %s;" instr) l;
  fprintf ppf "@;<1 -4>}@]";;  
val pretty_decl : Format.formatter -> string -> string list -> unit = <fun>
# pretty_decl std_formatter "the_name" ["a";"b"];;
the_name { a; b; }- : unit = ()
# pretty_decl std_formatter "the_name" ["aaaaaaaaaaaaaaaaaaaaa";"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"];;
the_name {
}- : unit = ()


The same trick can be used to ensure that the opening and the closing bracket are vertically aligned, the only difference is that the box would begin just before the opening bracket, unless everything fits on one line.


Comments (0)

You don't have permission to comment on this page.