src/Pure/General/pretty.ML
author wenzelm
Thu, 06 May 2010 23:52:20 +0200
changeset 36727 379f5b1e7f91
parent 36725 58020b59baf7
child 36728 97d2780ad6f0
permissions -rw-r--r--
replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm@6118
     1
(*  Title:      Pure/General/pretty.ML
wenzelm@8806
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@10952
     3
    Author:     Markus Wenzel, TU Munich
wenzelm@6116
     4
wenzelm@6116
     5
Generic pretty printing module.
wenzelm@6116
     6
wenzelm@6116
     7
Loosely based on
wenzelm@6116
     8
  D. C. Oppen, "Pretty Printing",
wenzelm@6116
     9
  ACM Transactions on Programming Languages and Systems (1980), 465-483.
wenzelm@6116
    10
wenzelm@6116
    11
The object to be printed is given as a tree with indentation and line
wenzelm@6116
    12
breaking information.  A "break" inserts a newline if the text until
wenzelm@6116
    13
the next break is too long to fit on the current line.  After the newline,
wenzelm@6116
    14
text is indented to the level of the enclosing block.  Normally, if a block
wenzelm@6116
    15
is broken then all enclosing blocks will also be broken.  Only "inconsistent
wenzelm@6116
    16
breaks" are provided.
wenzelm@6116
    17
wenzelm@6116
    18
The stored length of a block is used in breakdist (to treat each inner block as
wenzelm@6116
    19
a unit for breaking).
wenzelm@6116
    20
*)
wenzelm@6116
    21
wenzelm@6116
    22
signature PRETTY =
wenzelm@14832
    23
sig
wenzelm@23660
    24
  val default_indent: string -> int -> output
wenzelm@23698
    25
  val add_mode: string -> (string -> int -> output) -> unit
wenzelm@6116
    26
  type T
wenzelm@6116
    27
  val str: string -> T
wenzelm@6116
    28
  val brk: int -> T
wenzelm@6116
    29
  val fbrk: T
wenzelm@6116
    30
  val breaks: T list -> T list
wenzelm@6116
    31
  val fbreaks: T list -> T list
wenzelm@23645
    32
  val blk: int * T list -> T
wenzelm@6116
    33
  val block: T list -> T
wenzelm@23645
    34
  val strs: string list -> T
wenzelm@23617
    35
  val markup: Markup.T -> T list -> T
wenzelm@26703
    36
  val mark: Markup.T -> T -> T
wenzelm@23617
    37
  val keyword: string -> T
wenzelm@23617
    38
  val command: string -> T
wenzelm@23638
    39
  val markup_chunks: Markup.T -> T list -> T
wenzelm@18802
    40
  val chunks: T list -> T
wenzelm@19266
    41
  val chunks2: T list -> T
wenzelm@23617
    42
  val block_enclose: T * T -> T list -> T
wenzelm@18802
    43
  val quote: T -> T
wenzelm@18802
    44
  val backquote: T -> T
wenzelm@18802
    45
  val separate: string -> T list -> T list
wenzelm@18802
    46
  val commas: T list -> T list
wenzelm@6116
    47
  val enclose: string -> string -> T list -> T
wenzelm@18802
    48
  val enum: string -> string -> string -> T list -> T
wenzelm@30623
    49
  val position: Position.T -> T
wenzelm@6116
    50
  val list: string -> string -> T list -> T
wenzelm@6116
    51
  val str_list: string -> string -> string list -> T
wenzelm@6116
    52
  val big_list: string -> T list -> T
wenzelm@9730
    53
  val indent: int -> T -> T
wenzelm@23645
    54
  val unbreakable: T -> T
wenzelm@23645
    55
  val setmargin: int -> unit
wenzelm@32966
    56
  val setmp_margin_CRITICAL: int -> ('a -> 'b) -> 'a -> 'b
wenzelm@23645
    57
  val setdepth: int -> unit
wenzelm@30623
    58
  val to_ML: T -> ML_Pretty.pretty
wenzelm@30623
    59
  val from_ML: ML_Pretty.pretty -> T
wenzelm@23645
    60
  val symbolicN: string
wenzelm@16714
    61
  val output_buffer: T -> Buffer.T
wenzelm@23660
    62
  val output: T -> output
wenzelm@23645
    63
  val string_of: T -> string
wenzelm@23645
    64
  val str_of: T -> string
wenzelm@6116
    65
  val writeln: T -> unit
wenzelm@14832
    66
  type pp
wenzelm@14972
    67
  val pp: (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T) -> pp
wenzelm@14832
    68
  val term: pp -> term -> T
wenzelm@14832
    69
  val typ: pp -> typ -> T
wenzelm@14832
    70
  val sort: pp -> sort -> T
wenzelm@14832
    71
  val classrel: pp -> class list -> T
wenzelm@14832
    72
  val arity: pp -> arity -> T
wenzelm@14832
    73
  val string_of_term: pp -> term -> string
wenzelm@14832
    74
  val string_of_typ: pp -> typ -> string
wenzelm@14832
    75
  val string_of_sort: pp -> sort -> string
wenzelm@14832
    76
  val string_of_classrel: pp -> class list -> string
wenzelm@14832
    77
  val string_of_arity: pp -> arity -> string
wenzelm@14832
    78
end;
wenzelm@6116
    79
wenzelm@23617
    80
structure Pretty: PRETTY =
wenzelm@6116
    81
struct
wenzelm@6116
    82
wenzelm@23617
    83
(** print mode operations **)
wenzelm@23617
    84
wenzelm@23617
    85
fun default_indent (_: string) = Symbol.spaces;
wenzelm@23617
    86
wenzelm@23617
    87
local
wenzelm@23698
    88
  val default = {indent = default_indent};
wenzelm@32738
    89
  val modes = Unsynchronized.ref (Symtab.make [("", default)]);
wenzelm@23617
    90
in
wenzelm@23922
    91
  fun add_mode name indent = CRITICAL (fn () =>
wenzelm@32738
    92
    Unsynchronized.change modes (Symtab.update_new (name, {indent = indent})));
wenzelm@23617
    93
  fun get_mode () =
wenzelm@24612
    94
    the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ()));
wenzelm@23617
    95
end;
wenzelm@23617
    96
wenzelm@23617
    97
fun mode_indent x y = #indent (get_mode ()) x y;
wenzelm@23645
    98
wenzelm@23645
    99
val output_spaces = Output.output o Symbol.spaces;
wenzelm@23645
   100
val add_indent = Buffer.add o output_spaces;
wenzelm@23617
   101
wenzelm@23617
   102
wenzelm@10952
   103
wenzelm@10952
   104
(** printing items: compound phrases, strings, and breaks **)
wenzelm@10952
   105
wenzelm@6116
   106
datatype T =
wenzelm@30667
   107
  Block of (output * output) * T list * int * int |  (*markup output, body, indentation, length*)
wenzelm@30667
   108
  String of output * int |                           (*text, length*)
wenzelm@30667
   109
  Break of bool * int;                               (*mandatory flag, width if not taken*)
wenzelm@6116
   110
wenzelm@23645
   111
fun length (Block (_, _, _, len)) = len
wenzelm@23645
   112
  | length (String (_, len)) = len
wenzelm@23645
   113
  | length (Break (_, wd)) = wd;
wenzelm@6116
   114
wenzelm@9730
   115
wenzelm@9730
   116
wenzelm@23645
   117
(** derived operations to create formatting expressions **)
wenzelm@23645
   118
wenzelm@23645
   119
val str = String o Output.output_width;
wenzelm@23645
   120
wenzelm@23645
   121
fun brk wd = Break (false, wd);
wenzelm@23645
   122
val fbrk = Break (true, 2);
wenzelm@23645
   123
wenzelm@23645
   124
fun breaks prts = Library.separate (brk 1) prts;
wenzelm@23645
   125
fun fbreaks prts = Library.separate fbrk prts;
wenzelm@23645
   126
wenzelm@30667
   127
fun block_markup m (indent, es) =
wenzelm@23645
   128
  let
wenzelm@23645
   129
    fun sum [] k = k
wenzelm@23645
   130
      | sum (e :: es) k = sum es (length e + k);
wenzelm@23645
   131
  in Block (m, es, indent, sum es 0) end;
wenzelm@23645
   132
wenzelm@30667
   133
fun markup_block m arg = block_markup (Markup.output m) arg;
wenzelm@30667
   134
wenzelm@23645
   135
val blk = markup_block Markup.none;
wenzelm@23645
   136
fun block prts = blk (2, prts);
wenzelm@23645
   137
val strs = block o breaks o map str;
wenzelm@23645
   138
wenzelm@23645
   139
fun markup m prts = markup_block m (0, prts);
wenzelm@26703
   140
fun mark m prt = markup m [prt];
wenzelm@26703
   141
fun keyword name = mark (Markup.keyword name) (str name);
wenzelm@26703
   142
fun command name = mark (Markup.command name) (str name);
wenzelm@23645
   143
wenzelm@23645
   144
fun markup_chunks m prts = markup m (fbreaks prts);
wenzelm@23645
   145
val chunks = markup_chunks Markup.none;
wenzelm@23645
   146
fun chunks2 prts = blk (0, flat (Library.separate [fbrk, fbrk] (map single prts)));
wenzelm@23645
   147
wenzelm@23645
   148
fun block_enclose (p1, p2) ps = chunks [(block  o fbreaks) (p1 :: ps), p2];
wenzelm@23645
   149
wenzelm@23645
   150
fun quote prt = blk (1, [str "\"", prt, str "\""]);
wenzelm@23645
   151
fun backquote prt = blk (1, [str "`", prt, str "`"]);
wenzelm@23645
   152
wenzelm@23645
   153
fun separate sep prts =
wenzelm@23645
   154
  flat (Library.separate [str sep, brk 1] (map single prts));
wenzelm@23645
   155
wenzelm@23645
   156
val commas = separate ",";
wenzelm@23645
   157
wenzelm@23645
   158
fun enclose lpar rpar prts =
wenzelm@23645
   159
  block (str lpar :: (prts @ [str rpar]));
wenzelm@23645
   160
wenzelm@23645
   161
fun enum sep lpar rpar prts = enclose lpar rpar (separate sep prts);
wenzelm@23645
   162
wenzelm@30623
   163
val position =
wenzelm@30623
   164
  enum "," "{" "}" o map (fn (x, y) => str (x ^ "=" ^ y)) o Position.properties_of;
wenzelm@30623
   165
wenzelm@23645
   166
val list = enum ",";
wenzelm@23645
   167
fun str_list lpar rpar strs = list lpar rpar (map str strs);
wenzelm@23645
   168
wenzelm@23645
   169
fun big_list name prts = block (fbreaks (str name :: prts));
wenzelm@23645
   170
wenzelm@23645
   171
fun indent 0 prt = prt
wenzelm@23645
   172
  | indent n prt = blk (0, [str (Symbol.spaces n), prt]);
wenzelm@23645
   173
wenzelm@23645
   174
fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd)
wenzelm@23645
   175
  | unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd)
wenzelm@23645
   176
  | unbreakable (e as String _) = e;
wenzelm@23645
   177
wenzelm@23645
   178
wenzelm@23645
   179
wenzelm@23645
   180
(** formatting **)
wenzelm@23645
   181
wenzelm@23645
   182
(* margin *)
wenzelm@23645
   183
wenzelm@23645
   184
fun make_margin_info m =
wenzelm@23645
   185
 {margin = m,                   (*right margin, or page width*)
wenzelm@23645
   186
  breakgain = m div 20,         (*minimum added space required of a break*)
wenzelm@23645
   187
  emergencypos = m div 2};      (*position too far to right*)
wenzelm@23645
   188
wenzelm@32738
   189
val margin_info = Unsynchronized.ref (make_margin_info 76);
wenzelm@23645
   190
fun setmargin m = margin_info := make_margin_info m;
wenzelm@32966
   191
fun setmp_margin_CRITICAL m f = setmp_CRITICAL margin_info (make_margin_info m) f;
wenzelm@23645
   192
wenzelm@23645
   193
wenzelm@23645
   194
(* depth limitation *)
wenzelm@23645
   195
wenzelm@32738
   196
val depth = Unsynchronized.ref 0;   (*maximum depth; 0 means no limit*)
wenzelm@23645
   197
fun setdepth dp = (depth := dp);
wenzelm@23645
   198
wenzelm@23645
   199
local
wenzelm@32791
   200
  fun pruning dp (Block (m, bes, indent, _)) =
wenzelm@23645
   201
        if dp > 0
wenzelm@30667
   202
        then block_markup m (indent, map (pruning (dp - 1)) bes)
wenzelm@23645
   203
        else str "..."
wenzelm@32791
   204
    | pruning _ e = e;
wenzelm@23645
   205
in
wenzelm@23645
   206
  fun prune e = if ! depth > 0 then pruning (! depth) e else e;
wenzelm@23645
   207
end;
wenzelm@23645
   208
wenzelm@23645
   209
wenzelm@23645
   210
(* formatted output *)
wenzelm@23645
   211
wenzelm@23645
   212
local
wenzelm@6116
   213
wenzelm@17756
   214
type text = {tx: Buffer.T, ind: Buffer.T, pos: int, nl: int};
wenzelm@17756
   215
wenzelm@17756
   216
val empty: text =
wenzelm@10952
   217
 {tx = Buffer.empty,
wenzelm@10952
   218
  ind = Buffer.empty,
wenzelm@10952
   219
  pos = 0,
wenzelm@10952
   220
  nl = 0};
wenzelm@6116
   221
wenzelm@32791
   222
fun newline {tx, ind = _, pos = _, nl} : text =
wenzelm@14832
   223
 {tx = Buffer.add (Output.output "\n") tx,
wenzelm@10952
   224
  ind = Buffer.empty,
wenzelm@10952
   225
  pos = 0,
wenzelm@10952
   226
  nl = nl + 1};
wenzelm@6116
   227
wenzelm@23628
   228
fun control s {tx, ind, pos: int, nl} : text =
wenzelm@23628
   229
 {tx = Buffer.add s tx,
wenzelm@23628
   230
  ind = ind,
wenzelm@23628
   231
  pos = pos,
wenzelm@23628
   232
  nl = nl};
wenzelm@23628
   233
wenzelm@17756
   234
fun string (s, len) {tx, ind, pos: int, nl} : text =
wenzelm@10952
   235
 {tx = Buffer.add s tx,
wenzelm@10952
   236
  ind = Buffer.add s ind,
wenzelm@10952
   237
  pos = pos + len,
wenzelm@10952
   238
  nl = nl};
wenzelm@6116
   239
wenzelm@10952
   240
fun blanks wd = string (output_spaces wd, wd);
wenzelm@6116
   241
wenzelm@17756
   242
fun indentation (buf, len) {tx, ind, pos, nl} : text =
wenzelm@10952
   243
  let val s = Buffer.content buf in
wenzelm@23617
   244
   {tx = Buffer.add (mode_indent s len) tx,
wenzelm@10952
   245
    ind = Buffer.add s ind,
wenzelm@10952
   246
    pos = pos + len,
wenzelm@10952
   247
    nl = nl}
wenzelm@10952
   248
  end;
wenzelm@6116
   249
wenzelm@10952
   250
(*Add the lengths of the expressions until the next Break; if no Break then
wenzelm@10952
   251
  include "after", to account for text following this block.*)
wenzelm@36725
   252
fun breakdist (Break _ :: _, _) = 0
wenzelm@36725
   253
  | breakdist (Block (_, _, _, len) :: es, after) = len + breakdist (es, after)
wenzelm@32791
   254
  | breakdist (String (_, len) :: es, after) = len + breakdist (es, after)
wenzelm@10952
   255
  | breakdist ([], after) = after;
wenzelm@10952
   256
wenzelm@10952
   257
(*Search for the next break (at this or higher levels) and force it to occur.*)
wenzelm@6116
   258
fun forcenext [] = []
wenzelm@32791
   259
  | forcenext (Break _ :: es) = Break (true, 0) :: es
wenzelm@6116
   260
  | forcenext (e :: es) = e :: forcenext es;
wenzelm@6116
   261
wenzelm@6116
   262
(*es is list of expressions to print;
wenzelm@6116
   263
  blockin is the indentation of the current block;
wenzelm@10952
   264
  after is the width of the following context until next break.*)
wenzelm@6116
   265
fun format ([], _, _) text = text
wenzelm@32791
   266
  | format (e :: es, block as (_, blockin), after) (text as {ind, pos, nl, ...}) =
wenzelm@10952
   267
      (case e of
wenzelm@32791
   268
        Block ((bg, en), bes, indent, _) =>
wenzelm@10952
   269
          let
wenzelm@10952
   270
            val {emergencypos, ...} = ! margin_info;
wenzelm@10952
   271
            val pos' = pos + indent;
wenzelm@10952
   272
            val pos'' = pos' mod emergencypos;
wenzelm@10952
   273
            val block' =
wenzelm@10952
   274
              if pos' < emergencypos then (ind |> add_indent indent, pos')
wenzelm@23645
   275
              else (add_indent pos'' Buffer.empty, pos'');
wenzelm@23617
   276
            val btext: text = text
wenzelm@23628
   277
              |> control bg
wenzelm@23617
   278
              |> format (bes, block', breakdist (es, after))
wenzelm@23628
   279
              |> control en;
wenzelm@10952
   280
            (*if this block was broken then force the next break*)
wenzelm@23617
   281
            val es' = if nl < #nl btext then forcenext es else es;
wenzelm@23617
   282
          in format (es', block, after) btext end
wenzelm@10952
   283
      | Break (force, wd) =>
wenzelm@10952
   284
          let val {margin, breakgain, ...} = ! margin_info in
wenzelm@10952
   285
            (*no break if text to next break fits on this line
wenzelm@10952
   286
              or if breaking would add only breakgain to space*)
wenzelm@10952
   287
            format (es, block, after)
wenzelm@10952
   288
              (if not force andalso
wenzelm@10952
   289
                  pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain)
wenzelm@10952
   290
                then text |> blanks wd  (*just insert wd blanks*)
wenzelm@10952
   291
                else text |> newline |> indentation block)
wenzelm@36725
   292
          end
wenzelm@36725
   293
      | String str => format (es, block, after) (string str text));
wenzelm@6116
   294
wenzelm@23645
   295
in
wenzelm@6116
   296
wenzelm@23645
   297
fun formatted e = #tx (format ([prune e], (Buffer.empty, 0), 0) empty);
wenzelm@6116
   298
wenzelm@23645
   299
end;
wenzelm@6116
   300
wenzelm@19266
   301
wenzelm@23645
   302
(* special output *)
wenzelm@6116
   303
wenzelm@23645
   304
(*symbolic markup -- no formatting*)
wenzelm@23645
   305
fun symbolic prt =
wenzelm@6116
   306
  let
wenzelm@30667
   307
    fun out (Block ((bg, en), [], _, _)) = Buffer.add bg #> Buffer.add en
wenzelm@30667
   308
      | out (Block ((bg, en), prts, indent, _)) =
wenzelm@30667
   309
          Buffer.add bg #> Buffer.markup (Markup.block indent) (fold out prts) #> Buffer.add en
wenzelm@23645
   310
      | out (String (s, _)) = Buffer.add s
wenzelm@23787
   311
      | out (Break (false, wd)) = Buffer.markup (Markup.break wd) (Buffer.add (output_spaces wd))
wenzelm@36727
   312
      | out (Break (true, _)) = Buffer.add (Output.output "\n");
wenzelm@23645
   313
  in out prt Buffer.empty end;
wenzelm@23617
   314
wenzelm@23645
   315
(*unformatted output*)
wenzelm@23645
   316
fun unformatted prt =
wenzelm@6116
   317
  let
wenzelm@30667
   318
    fun fmt (Block ((bg, en), prts, _, _)) = Buffer.add bg #> fold fmt prts #> Buffer.add en
wenzelm@23617
   319
      | fmt (String (s, _)) = Buffer.add s
wenzelm@23617
   320
      | fmt (Break (false, wd)) = Buffer.add (output_spaces wd)
wenzelm@23617
   321
      | fmt (Break (true, _)) = Buffer.add (output_spaces 1);
wenzelm@23645
   322
  in fmt (prune prt) Buffer.empty end;
wenzelm@6116
   323
wenzelm@23645
   324
wenzelm@30627
   325
(* ML toplevel pretty printing *)
wenzelm@30623
   326
wenzelm@30667
   327
fun to_ML (Block (m, prts, ind, _)) = ML_Pretty.Block (m, map to_ML prts, ind)
wenzelm@30627
   328
  | to_ML (String s) = ML_Pretty.String s
wenzelm@30627
   329
  | to_ML (Break b) = ML_Pretty.Break b;
wenzelm@30627
   330
wenzelm@30667
   331
fun from_ML (ML_Pretty.Block (m, prts, ind)) = block_markup m (ind, map from_ML prts)
wenzelm@30627
   332
  | from_ML (ML_Pretty.String s) = String s
wenzelm@30627
   333
  | from_ML (ML_Pretty.Break b) = Break b;
wenzelm@30623
   334
wenzelm@23645
   335
wenzelm@23645
   336
(* output interfaces *)
wenzelm@23645
   337
wenzelm@23645
   338
val symbolicN = "pretty_symbolic";
wenzelm@23645
   339
wenzelm@23645
   340
fun output_buffer prt =
wenzelm@23645
   341
  if print_mode_active symbolicN then symbolic prt
wenzelm@23645
   342
  else formatted prt;
wenzelm@23645
   343
wenzelm@23645
   344
val output = Buffer.content o output_buffer;
wenzelm@23645
   345
val string_of = Output.escape o output;
wenzelm@23645
   346
val str_of = Output.escape o Buffer.content o unformatted;
wenzelm@23645
   347
val writeln = Output.writeln o string_of;
wenzelm@6116
   348
wenzelm@6116
   349
wenzelm@14832
   350
wenzelm@14832
   351
(** abstract pretty printing context **)
wenzelm@14832
   352
wenzelm@14832
   353
datatype pp =
wenzelm@14832
   354
  PP of (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T);
wenzelm@14832
   355
wenzelm@14972
   356
val pp = PP;
wenzelm@14832
   357
wenzelm@14832
   358
fun pp_fun f (PP x) = f x;
wenzelm@14832
   359
wenzelm@14832
   360
val term     = pp_fun #1;
wenzelm@14832
   361
val typ      = pp_fun #2;
wenzelm@14832
   362
val sort     = pp_fun #3;
wenzelm@14832
   363
val classrel = pp_fun #4;
wenzelm@14832
   364
val arity    = pp_fun #5;
wenzelm@14832
   365
wenzelm@14832
   366
val string_of_term     = string_of oo term;
wenzelm@14832
   367
val string_of_typ      = string_of oo typ;
wenzelm@14832
   368
val string_of_sort     = string_of oo sort;
wenzelm@14832
   369
val string_of_classrel = string_of oo classrel;
wenzelm@14832
   370
val string_of_arity    = string_of oo arity;
wenzelm@14832
   371
wenzelm@6116
   372
end;