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