src/Pure/General/pretty.ML
author wenzelm
Thu, 07 Apr 2011 17:38:17 +0200
changeset 43144 f87e0be80a3f
parent 40391 7cbebd636e79
child 43258 0ae4ad40d7b5
permissions -rw-r--r--
clarified Pretty.mark;
added Pretty.mark_str, Pretty.marks_str convenience;
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@40391
    24
  val default_indent: string -> int -> Output.output
wenzelm@40391
    25
  val add_mode: string -> (string -> int -> Output.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@43144
    37
  val mark_str: Markup.T * string -> T
wenzelm@43144
    38
  val marks_str: Markup.T list * string -> T
wenzelm@23617
    39
  val keyword: string -> T
wenzelm@23617
    40
  val command: string -> T
wenzelm@23638
    41
  val markup_chunks: Markup.T -> T list -> T
wenzelm@18802
    42
  val chunks: T list -> T
wenzelm@19266
    43
  val chunks2: T list -> T
wenzelm@23617
    44
  val block_enclose: T * T -> T list -> T
wenzelm@18802
    45
  val quote: T -> T
wenzelm@18802
    46
  val backquote: T -> T
wenzelm@18802
    47
  val separate: string -> T list -> T list
wenzelm@18802
    48
  val commas: T list -> T list
wenzelm@6116
    49
  val enclose: string -> string -> T list -> T
wenzelm@18802
    50
  val enum: string -> string -> string -> T list -> T
wenzelm@30623
    51
  val position: Position.T -> T
wenzelm@6116
    52
  val list: string -> string -> T list -> T
wenzelm@6116
    53
  val str_list: string -> string -> string list -> T
wenzelm@6116
    54
  val big_list: string -> T list -> T
wenzelm@9730
    55
  val indent: int -> T -> T
wenzelm@23645
    56
  val unbreakable: T -> T
wenzelm@36754
    57
  val margin_default: int Unsynchronized.ref
wenzelm@23645
    58
  val symbolicN: string
wenzelm@36754
    59
  val output_buffer: int option -> T -> Buffer.T
wenzelm@40391
    60
  val output: int option -> T -> Output.output
wenzelm@36754
    61
  val string_of_margin: int -> T -> string
wenzelm@23645
    62
  val string_of: T -> string
wenzelm@23645
    63
  val str_of: T -> string
wenzelm@6116
    64
  val writeln: T -> unit
wenzelm@36757
    65
  val to_ML: T -> ML_Pretty.pretty
wenzelm@36757
    66
  val from_ML: ML_Pretty.pretty -> T
wenzelm@14832
    67
  type pp
wenzelm@14972
    68
  val pp: (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T) -> pp
wenzelm@14832
    69
  val term: pp -> term -> T
wenzelm@14832
    70
  val typ: pp -> typ -> T
wenzelm@14832
    71
  val sort: pp -> sort -> T
wenzelm@14832
    72
  val classrel: pp -> class list -> T
wenzelm@14832
    73
  val arity: pp -> arity -> T
wenzelm@14832
    74
  val string_of_term: pp -> term -> string
wenzelm@14832
    75
  val string_of_typ: pp -> typ -> string
wenzelm@14832
    76
  val string_of_sort: pp -> sort -> string
wenzelm@14832
    77
  val string_of_classrel: pp -> class list -> string
wenzelm@14832
    78
  val string_of_arity: pp -> arity -> string
wenzelm@14832
    79
end;
wenzelm@6116
    80
wenzelm@23617
    81
structure Pretty: PRETTY =
wenzelm@6116
    82
struct
wenzelm@6116
    83
wenzelm@23617
    84
(** print mode operations **)
wenzelm@23617
    85
wenzelm@23617
    86
fun default_indent (_: string) = Symbol.spaces;
wenzelm@23617
    87
wenzelm@23617
    88
local
wenzelm@23698
    89
  val default = {indent = default_indent};
wenzelm@32738
    90
  val modes = Unsynchronized.ref (Symtab.make [("", default)]);
wenzelm@23617
    91
in
wenzelm@23922
    92
  fun add_mode name indent = CRITICAL (fn () =>
wenzelm@32738
    93
    Unsynchronized.change modes (Symtab.update_new (name, {indent = indent})));
wenzelm@23617
    94
  fun get_mode () =
wenzelm@24612
    95
    the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ()));
wenzelm@23617
    96
end;
wenzelm@23617
    97
wenzelm@23617
    98
fun mode_indent x y = #indent (get_mode ()) x y;
wenzelm@23645
    99
wenzelm@23645
   100
val output_spaces = Output.output o Symbol.spaces;
wenzelm@23645
   101
val add_indent = Buffer.add o output_spaces;
wenzelm@23617
   102
wenzelm@23617
   103
wenzelm@10952
   104
wenzelm@10952
   105
(** printing items: compound phrases, strings, and breaks **)
wenzelm@10952
   106
wenzelm@37530
   107
abstype T =
wenzelm@40391
   108
    Block of (Output.output * Output.output) * T list * int * int
wenzelm@40391
   109
      (*markup output, body, indentation, length*)
wenzelm@40391
   110
  | String of Output.output * int  (*text, length*)
wenzelm@40391
   111
  | Break of bool * int  (*mandatory flag, width if not taken*)
wenzelm@37530
   112
with
wenzelm@6116
   113
wenzelm@23645
   114
fun length (Block (_, _, _, len)) = len
wenzelm@23645
   115
  | length (String (_, len)) = len
wenzelm@23645
   116
  | length (Break (_, wd)) = wd;
wenzelm@6116
   117
wenzelm@9730
   118
wenzelm@9730
   119
wenzelm@23645
   120
(** derived operations to create formatting expressions **)
wenzelm@23645
   121
wenzelm@23645
   122
val str = String o Output.output_width;
wenzelm@23645
   123
wenzelm@23645
   124
fun brk wd = Break (false, wd);
wenzelm@36728
   125
val fbrk = Break (true, 1);
wenzelm@23645
   126
wenzelm@23645
   127
fun breaks prts = Library.separate (brk 1) prts;
wenzelm@23645
   128
fun fbreaks prts = Library.separate fbrk prts;
wenzelm@23645
   129
wenzelm@30667
   130
fun block_markup m (indent, es) =
wenzelm@23645
   131
  let
wenzelm@23645
   132
    fun sum [] k = k
wenzelm@23645
   133
      | sum (e :: es) k = sum es (length e + k);
wenzelm@23645
   134
  in Block (m, es, indent, sum es 0) end;
wenzelm@23645
   135
wenzelm@30667
   136
fun markup_block m arg = block_markup (Markup.output m) arg;
wenzelm@30667
   137
wenzelm@38792
   138
val blk = markup_block Markup.empty;
wenzelm@23645
   139
fun block prts = blk (2, prts);
wenzelm@23645
   140
val strs = block o breaks o map str;
wenzelm@23645
   141
wenzelm@23645
   142
fun markup m prts = markup_block m (0, prts);
wenzelm@43144
   143
fun mark m prt = if m = Markup.empty then prt else markup m [prt];
wenzelm@43144
   144
fun mark_str (m, s) = mark m (str s);
wenzelm@43144
   145
fun marks_str (ms, s) = fold_rev mark ms (str s);
wenzelm@43144
   146
wenzelm@43144
   147
fun keyword name = mark_str (Markup.keyword, name);
wenzelm@43144
   148
fun command name = mark_str (Markup.command, name);
wenzelm@23645
   149
wenzelm@23645
   150
fun markup_chunks m prts = markup m (fbreaks prts);
wenzelm@38792
   151
val chunks = markup_chunks Markup.empty;
wenzelm@23645
   152
fun chunks2 prts = blk (0, flat (Library.separate [fbrk, fbrk] (map single prts)));
wenzelm@23645
   153
wenzelm@36733
   154
fun block_enclose (prt1, prt2) prts = chunks [block (fbreaks (prt1 :: prts)), prt2];
wenzelm@23645
   155
wenzelm@23645
   156
fun quote prt = blk (1, [str "\"", prt, str "\""]);
wenzelm@23645
   157
fun backquote prt = blk (1, [str "`", prt, str "`"]);
wenzelm@23645
   158
wenzelm@23645
   159
fun separate sep prts =
wenzelm@23645
   160
  flat (Library.separate [str sep, brk 1] (map single prts));
wenzelm@23645
   161
wenzelm@23645
   162
val commas = separate ",";
wenzelm@23645
   163
wenzelm@23645
   164
fun enclose lpar rpar prts =
wenzelm@23645
   165
  block (str lpar :: (prts @ [str rpar]));
wenzelm@23645
   166
wenzelm@23645
   167
fun enum sep lpar rpar prts = enclose lpar rpar (separate sep prts);
wenzelm@23645
   168
wenzelm@30623
   169
val position =
wenzelm@30623
   170
  enum "," "{" "}" o map (fn (x, y) => str (x ^ "=" ^ y)) o Position.properties_of;
wenzelm@30623
   171
wenzelm@23645
   172
val list = enum ",";
wenzelm@23645
   173
fun str_list lpar rpar strs = list lpar rpar (map str strs);
wenzelm@23645
   174
wenzelm@23645
   175
fun big_list name prts = block (fbreaks (str name :: prts));
wenzelm@23645
   176
wenzelm@23645
   177
fun indent 0 prt = prt
wenzelm@23645
   178
  | indent n prt = blk (0, [str (Symbol.spaces n), prt]);
wenzelm@23645
   179
wenzelm@23645
   180
fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd)
wenzelm@23645
   181
  | unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd)
wenzelm@23645
   182
  | unbreakable (e as String _) = e;
wenzelm@23645
   183
wenzelm@23645
   184
wenzelm@23645
   185
wenzelm@23645
   186
(** formatting **)
wenzelm@23645
   187
wenzelm@23645
   188
(* formatted output *)
wenzelm@23645
   189
wenzelm@23645
   190
local
wenzelm@6116
   191
wenzelm@17756
   192
type text = {tx: Buffer.T, ind: Buffer.T, pos: int, nl: int};
wenzelm@17756
   193
wenzelm@17756
   194
val empty: text =
wenzelm@10952
   195
 {tx = Buffer.empty,
wenzelm@10952
   196
  ind = Buffer.empty,
wenzelm@10952
   197
  pos = 0,
wenzelm@10952
   198
  nl = 0};
wenzelm@6116
   199
wenzelm@32791
   200
fun newline {tx, ind = _, pos = _, nl} : text =
wenzelm@14832
   201
 {tx = Buffer.add (Output.output "\n") tx,
wenzelm@10952
   202
  ind = Buffer.empty,
wenzelm@10952
   203
  pos = 0,
wenzelm@10952
   204
  nl = nl + 1};
wenzelm@6116
   205
wenzelm@23628
   206
fun control s {tx, ind, pos: int, nl} : text =
wenzelm@23628
   207
 {tx = Buffer.add s tx,
wenzelm@23628
   208
  ind = ind,
wenzelm@23628
   209
  pos = pos,
wenzelm@23628
   210
  nl = nl};
wenzelm@23628
   211
wenzelm@17756
   212
fun string (s, len) {tx, ind, pos: int, nl} : text =
wenzelm@10952
   213
 {tx = Buffer.add s tx,
wenzelm@10952
   214
  ind = Buffer.add s ind,
wenzelm@10952
   215
  pos = pos + len,
wenzelm@10952
   216
  nl = nl};
wenzelm@6116
   217
wenzelm@10952
   218
fun blanks wd = string (output_spaces wd, wd);
wenzelm@6116
   219
wenzelm@17756
   220
fun indentation (buf, len) {tx, ind, pos, nl} : text =
wenzelm@10952
   221
  let val s = Buffer.content buf in
wenzelm@23617
   222
   {tx = Buffer.add (mode_indent s len) tx,
wenzelm@10952
   223
    ind = Buffer.add s ind,
wenzelm@10952
   224
    pos = pos + len,
wenzelm@10952
   225
    nl = nl}
wenzelm@10952
   226
  end;
wenzelm@6116
   227
wenzelm@10952
   228
(*Add the lengths of the expressions until the next Break; if no Break then
wenzelm@10952
   229
  include "after", to account for text following this block.*)
wenzelm@36725
   230
fun breakdist (Break _ :: _, _) = 0
wenzelm@36725
   231
  | breakdist (Block (_, _, _, len) :: es, after) = len + breakdist (es, after)
wenzelm@32791
   232
  | breakdist (String (_, len) :: es, after) = len + breakdist (es, after)
wenzelm@10952
   233
  | breakdist ([], after) = after;
wenzelm@10952
   234
wenzelm@10952
   235
(*Search for the next break (at this or higher levels) and force it to occur.*)
wenzelm@6116
   236
fun forcenext [] = []
wenzelm@36728
   237
  | forcenext (Break _ :: es) = fbrk :: es
wenzelm@6116
   238
  | forcenext (e :: es) = e :: forcenext es;
wenzelm@6116
   239
wenzelm@23645
   240
in
wenzelm@6116
   241
wenzelm@36754
   242
fun formatted margin input =
wenzelm@36754
   243
  let
wenzelm@36754
   244
    val breakgain = margin div 20;     (*minimum added space required of a break*)
wenzelm@36754
   245
    val emergencypos = margin div 2;   (*position too far to right*)
wenzelm@36754
   246
wenzelm@36754
   247
    (*es is list of expressions to print;
wenzelm@36754
   248
      blockin is the indentation of the current block;
wenzelm@36754
   249
      after is the width of the following context until next break.*)
wenzelm@36754
   250
    fun format ([], _, _) text = text
wenzelm@36754
   251
      | format (e :: es, block as (_, blockin), after) (text as {ind, pos, nl, ...}) =
wenzelm@36754
   252
          (case e of
wenzelm@36754
   253
            Block ((bg, en), bes, indent, _) =>
wenzelm@36754
   254
              let
wenzelm@36754
   255
                val pos' = pos + indent;
wenzelm@36754
   256
                val pos'' = pos' mod emergencypos;
wenzelm@36754
   257
                val block' =
wenzelm@36754
   258
                  if pos' < emergencypos then (ind |> add_indent indent, pos')
wenzelm@36754
   259
                  else (add_indent pos'' Buffer.empty, pos'');
wenzelm@36754
   260
                val btext: text = text
wenzelm@36754
   261
                  |> control bg
wenzelm@36754
   262
                  |> format (bes, block', breakdist (es, after))
wenzelm@36754
   263
                  |> control en;
wenzelm@36754
   264
                (*if this block was broken then force the next break*)
wenzelm@36754
   265
                val es' = if nl < #nl btext then forcenext es else es;
wenzelm@36754
   266
              in format (es', block, after) btext end
wenzelm@36754
   267
          | Break (force, wd) =>
wenzelm@36754
   268
              (*no break if text to next break fits on this line
wenzelm@36754
   269
                or if breaking would add only breakgain to space*)
wenzelm@36754
   270
              format (es, block, after)
wenzelm@36754
   271
                (if not force andalso
wenzelm@36754
   272
                    pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain)
wenzelm@36754
   273
                  then text |> blanks wd  (*just insert wd blanks*)
wenzelm@36754
   274
                  else text |> newline |> indentation block)
wenzelm@36754
   275
          | String str => format (es, block, after) (string str text));
wenzelm@36754
   276
  in
wenzelm@36756
   277
    #tx (format ([input], (Buffer.empty, 0), 0) empty)
wenzelm@36754
   278
  end;
wenzelm@6116
   279
wenzelm@23645
   280
end;
wenzelm@6116
   281
wenzelm@19266
   282
wenzelm@23645
   283
(* special output *)
wenzelm@6116
   284
wenzelm@23645
   285
(*symbolic markup -- no formatting*)
wenzelm@23645
   286
fun symbolic prt =
wenzelm@6116
   287
  let
wenzelm@30667
   288
    fun out (Block ((bg, en), [], _, _)) = Buffer.add bg #> Buffer.add en
wenzelm@30667
   289
      | out (Block ((bg, en), prts, indent, _)) =
wenzelm@30667
   290
          Buffer.add bg #> Buffer.markup (Markup.block indent) (fold out prts) #> Buffer.add en
wenzelm@23645
   291
      | out (String (s, _)) = Buffer.add s
wenzelm@23787
   292
      | out (Break (false, wd)) = Buffer.markup (Markup.break wd) (Buffer.add (output_spaces wd))
wenzelm@36727
   293
      | out (Break (true, _)) = Buffer.add (Output.output "\n");
wenzelm@23645
   294
  in out prt Buffer.empty end;
wenzelm@23617
   295
wenzelm@23645
   296
(*unformatted output*)
wenzelm@23645
   297
fun unformatted prt =
wenzelm@6116
   298
  let
wenzelm@30667
   299
    fun fmt (Block ((bg, en), prts, _, _)) = Buffer.add bg #> fold fmt prts #> Buffer.add en
wenzelm@23617
   300
      | fmt (String (s, _)) = Buffer.add s
wenzelm@36728
   301
      | fmt (Break (_, wd)) = Buffer.add (output_spaces wd);
wenzelm@36756
   302
  in fmt prt Buffer.empty end;
wenzelm@6116
   303
wenzelm@23645
   304
wenzelm@36757
   305
(* output interfaces *)
wenzelm@30623
   306
wenzelm@36757
   307
val margin_default = Unsynchronized.ref 76;  (*right margin, or page width*)
wenzelm@23645
   308
wenzelm@23645
   309
val symbolicN = "pretty_symbolic";
wenzelm@23645
   310
wenzelm@36754
   311
fun output_buffer margin prt =
wenzelm@23645
   312
  if print_mode_active symbolicN then symbolic prt
wenzelm@36754
   313
  else formatted (the_default (! margin_default) margin) prt;
wenzelm@23645
   314
wenzelm@36754
   315
val output = Buffer.content oo output_buffer;
wenzelm@36754
   316
fun string_of_margin margin = Output.escape o output (SOME margin);
wenzelm@36754
   317
val string_of = Output.escape o output NONE;
wenzelm@23645
   318
val str_of = Output.escape o Buffer.content o unformatted;
wenzelm@23645
   319
val writeln = Output.writeln o string_of;
wenzelm@6116
   320
wenzelm@6116
   321
wenzelm@14832
   322
wenzelm@36757
   323
(** ML toplevel pretty printing **)
wenzelm@36757
   324
wenzelm@36757
   325
fun to_ML (Block (m, prts, ind, _)) = ML_Pretty.Block (m, map to_ML prts, ind)
wenzelm@36757
   326
  | to_ML (String s) = ML_Pretty.String s
wenzelm@36757
   327
  | to_ML (Break b) = ML_Pretty.Break b;
wenzelm@36757
   328
wenzelm@36757
   329
fun from_ML (ML_Pretty.Block (m, prts, ind)) = block_markup m (ind, map from_ML prts)
wenzelm@36757
   330
  | from_ML (ML_Pretty.String s) = String s
wenzelm@36757
   331
  | from_ML (ML_Pretty.Break b) = Break b;
wenzelm@36757
   332
wenzelm@37530
   333
end;
wenzelm@37530
   334
wenzelm@36757
   335
wenzelm@36757
   336
wenzelm@14832
   337
(** abstract pretty printing context **)
wenzelm@14832
   338
wenzelm@14832
   339
datatype pp =
wenzelm@14832
   340
  PP of (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T);
wenzelm@14832
   341
wenzelm@14972
   342
val pp = PP;
wenzelm@14832
   343
wenzelm@14832
   344
fun pp_fun f (PP x) = f x;
wenzelm@14832
   345
wenzelm@14832
   346
val term     = pp_fun #1;
wenzelm@14832
   347
val typ      = pp_fun #2;
wenzelm@14832
   348
val sort     = pp_fun #3;
wenzelm@14832
   349
val classrel = pp_fun #4;
wenzelm@14832
   350
val arity    = pp_fun #5;
wenzelm@14832
   351
wenzelm@14832
   352
val string_of_term     = string_of oo term;
wenzelm@14832
   353
val string_of_typ      = string_of oo typ;
wenzelm@14832
   354
val string_of_sort     = string_of oo sort;
wenzelm@14832
   355
val string_of_classrel = string_of oo classrel;
wenzelm@14832
   356
val string_of_arity    = string_of oo arity;
wenzelm@14832
   357
wenzelm@6116
   358
end;