src/Pure/General/pretty.ML
author wenzelm
Fri, 17 Dec 2010 20:21:35 +0100
changeset 41253 42f24340ae53
parent 40131 7cbebd636e79
child 42266 f87e0be80a3f
permissions -rw-r--r--
more explicit references to structure Raw_Simplifier;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 6116
diff changeset
     1
(*  Title:      Pure/General/pretty.ML
8806
wenzelm
parents: 8456
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
     3
    Author:     Markus Wenzel, TU Munich
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     4
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     5
Generic pretty printing module.
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     6
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     7
Loosely based on
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     8
  D. C. Oppen, "Pretty Printing",
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
     9
  ACM Transactions on Programming Languages and Systems (1980), 465-483.
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    10
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    11
The object to be printed is given as a tree with indentation and line
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    12
breaking information.  A "break" inserts a newline if the text until
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    13
the next break is too long to fit on the current line.  After the newline,
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    14
text is indented to the level of the enclosing block.  Normally, if a block
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    15
is broken then all enclosing blocks will also be broken.  Only "inconsistent
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    16
breaks" are provided.
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    17
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    18
The stored length of a block is used in breakdist (to treat each inner block as
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    19
a unit for breaking).
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    20
*)
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    21
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    22
signature PRETTY =
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    23
sig
40131
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 38474
diff changeset
    24
  val default_indent: string -> int -> Output.output
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 38474
diff changeset
    25
  val add_mode: string -> (string -> int -> Output.output) -> unit
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    26
  type T
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    27
  val str: string -> T
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    28
  val brk: int -> T
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    29
  val fbrk: T
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    30
  val breaks: T list -> T list
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    31
  val fbreaks: T list -> T list
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    32
  val blk: int * T list -> T
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    33
  val block: T list -> T
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    34
  val strs: string list -> T
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    35
  val markup: Markup.T -> T list -> T
26703
c07b1a90600c removed obsolete raw_str;
wenzelm
parents: 24612
diff changeset
    36
  val mark: Markup.T -> T -> T
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    37
  val keyword: string -> T
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    38
  val command: string -> T
23638
09120c2dd71f added markup_chunks;
wenzelm
parents: 23628
diff changeset
    39
  val markup_chunks: Markup.T -> T list -> T
18802
f449d516f36b renamed gen_list to enum;
wenzelm
parents: 18603
diff changeset
    40
  val chunks: T list -> T
19266
2e8ad3f2cd66 added command, keyword;
wenzelm
parents: 18802
diff changeset
    41
  val chunks2: T list -> T
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    42
  val block_enclose: T * T -> T list -> T
18802
f449d516f36b renamed gen_list to enum;
wenzelm
parents: 18603
diff changeset
    43
  val quote: T -> T
f449d516f36b renamed gen_list to enum;
wenzelm
parents: 18603
diff changeset
    44
  val backquote: T -> T
f449d516f36b renamed gen_list to enum;
wenzelm
parents: 18603
diff changeset
    45
  val separate: string -> T list -> T list
f449d516f36b renamed gen_list to enum;
wenzelm
parents: 18603
diff changeset
    46
  val commas: T list -> T list
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    47
  val enclose: string -> string -> T list -> T
18802
f449d516f36b renamed gen_list to enum;
wenzelm
parents: 18603
diff changeset
    48
  val enum: string -> string -> string -> T list -> T
30620
16b7ecc183e5 added position;
wenzelm
parents: 29606
diff changeset
    49
  val position: Position.T -> T
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    50
  val list: string -> string -> T list -> T
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    51
  val str_list: string -> string -> string list -> T
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    52
  val big_list: string -> T list -> T
9730
11d137b25555 added indent;
wenzelm
parents: 9121
diff changeset
    53
  val indent: int -> T -> T
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    54
  val unbreakable: T -> T
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
    55
  val margin_default: int Unsynchronized.ref
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    56
  val symbolicN: string
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
    57
  val output_buffer: int option -> T -> Buffer.T
40131
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 38474
diff changeset
    58
  val output: int option -> T -> Output.output
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
    59
  val string_of_margin: int -> T -> string
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    60
  val string_of: T -> string
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    61
  val str_of: T -> string
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    62
  val writeln: T -> unit
36748
wenzelm
parents: 36747
diff changeset
    63
  val to_ML: T -> ML_Pretty.pretty
wenzelm
parents: 36747
diff changeset
    64
  val from_ML: ML_Pretty.pretty -> T
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    65
  type pp
14972
wenzelm
parents: 14832
diff changeset
    66
  val pp: (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T) -> pp
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    67
  val term: pp -> term -> T
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    68
  val typ: pp -> typ -> T
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    69
  val sort: pp -> sort -> T
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    70
  val classrel: pp -> class list -> T
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    71
  val arity: pp -> arity -> T
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    72
  val string_of_term: pp -> term -> string
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    73
  val string_of_typ: pp -> typ -> string
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    74
  val string_of_sort: pp -> sort -> string
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    75
  val string_of_classrel: pp -> class list -> string
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    76
  val string_of_arity: pp -> arity -> string
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
    77
end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    78
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    79
structure Pretty: PRETTY =
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    80
struct
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
    81
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    82
(** print mode operations **)
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    83
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    84
fun default_indent (_: string) = Symbol.spaces;
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    85
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    86
local
23698
af84f2f13d4d moved print_mode setup for markup to markup.ML;
wenzelm
parents: 23660
diff changeset
    87
  val default = {indent = default_indent};
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30667
diff changeset
    88
  val modes = Unsynchronized.ref (Symtab.make [("", default)]);
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    89
in
23922
707639e9497d marked some CRITICAL sections (for multithreading);
wenzelm
parents: 23787
diff changeset
    90
  fun add_mode name indent = CRITICAL (fn () =>
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 30667
diff changeset
    91
    Unsynchronized.change modes (Symtab.update_new (name, {indent = indent})));
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    92
  fun get_mode () =
24612
d1b315bdb8d7 avoid direct access to print_mode;
wenzelm
parents: 23922
diff changeset
    93
    the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ()));
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    94
end;
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    95
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
    96
fun mode_indent x y = #indent (get_mode ()) x y;
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    97
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    98
val output_spaces = Output.output o Symbol.spaces;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
    99
val add_indent = Buffer.add o output_spaces;
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
   100
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
   101
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   102
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   103
(** printing items: compound phrases, strings, and breaks **)
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   104
37529
a23e8aa853eb treat Pretty.T as strictly abstract type;
wenzelm
parents: 37193
diff changeset
   105
abstype T =
40131
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 38474
diff changeset
   106
    Block of (Output.output * Output.output) * T list * int * int
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 38474
diff changeset
   107
      (*markup output, body, indentation, length*)
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 38474
diff changeset
   108
  | String of Output.output * int  (*text, length*)
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 38474
diff changeset
   109
  | Break of bool * int  (*mandatory flag, width if not taken*)
37529
a23e8aa853eb treat Pretty.T as strictly abstract type;
wenzelm
parents: 37193
diff changeset
   110
with
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   111
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   112
fun length (Block (_, _, _, len)) = len
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   113
  | length (String (_, len)) = len
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   114
  | length (Break (_, wd)) = wd;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   115
9730
11d137b25555 added indent;
wenzelm
parents: 9121
diff changeset
   116
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   117
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   118
(** derived operations to create formatting expressions **)
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   119
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   120
val str = String o Output.output_width;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   121
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   122
fun brk wd = Break (false, wd);
36690
97d2780ad6f0 uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents: 36689
diff changeset
   123
val fbrk = Break (true, 1);
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   124
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   125
fun breaks prts = Library.separate (brk 1) prts;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   126
fun fbreaks prts = Library.separate fbrk prts;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   127
30667
53fbf7c679b0 Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents: 30624
diff changeset
   128
fun block_markup m (indent, es) =
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   129
  let
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   130
    fun sum [] k = k
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   131
      | sum (e :: es) k = sum es (length e + k);
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   132
  in Block (m, es, indent, sum es 0) end;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   133
30667
53fbf7c679b0 Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents: 30624
diff changeset
   134
fun markup_block m arg = block_markup (Markup.output m) arg;
53fbf7c679b0 Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents: 30624
diff changeset
   135
38474
e498dc2eb576 uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents: 37529
diff changeset
   136
val blk = markup_block Markup.empty;
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   137
fun block prts = blk (2, prts);
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   138
val strs = block o breaks o map str;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   139
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   140
fun markup m prts = markup_block m (0, prts);
26703
c07b1a90600c removed obsolete raw_str;
wenzelm
parents: 24612
diff changeset
   141
fun mark m prt = markup m [prt];
37193
a4b2bb0dab08 simplified command/keyword markup;
wenzelm
parents: 36748
diff changeset
   142
fun keyword name = mark Markup.keyword (str name);
a4b2bb0dab08 simplified command/keyword markup;
wenzelm
parents: 36748
diff changeset
   143
fun command name = mark Markup.command (str name);
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   144
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   145
fun markup_chunks m prts = markup m (fbreaks prts);
38474
e498dc2eb576 uniform Markup.empty/Markup.Empty in ML and Scala;
wenzelm
parents: 37529
diff changeset
   146
val chunks = markup_chunks Markup.empty;
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   147
fun chunks2 prts = blk (0, flat (Library.separate [fbrk, fbrk] (map single prts)));
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   148
36733
wenzelm
parents: 36690
diff changeset
   149
fun block_enclose (prt1, prt2) prts = chunks [block (fbreaks (prt1 :: prts)), prt2];
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   150
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   151
fun quote prt = blk (1, [str "\"", prt, str "\""]);
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   152
fun backquote prt = blk (1, [str "`", prt, str "`"]);
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   153
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   154
fun separate sep prts =
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   155
  flat (Library.separate [str sep, brk 1] (map single prts));
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   156
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   157
val commas = separate ",";
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   158
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   159
fun enclose lpar rpar prts =
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   160
  block (str lpar :: (prts @ [str rpar]));
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   161
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   162
fun enum sep lpar rpar prts = enclose lpar rpar (separate sep prts);
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   163
30620
16b7ecc183e5 added position;
wenzelm
parents: 29606
diff changeset
   164
val position =
16b7ecc183e5 added position;
wenzelm
parents: 29606
diff changeset
   165
  enum "," "{" "}" o map (fn (x, y) => str (x ^ "=" ^ y)) o Position.properties_of;
16b7ecc183e5 added position;
wenzelm
parents: 29606
diff changeset
   166
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   167
val list = enum ",";
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   168
fun str_list lpar rpar strs = list lpar rpar (map str strs);
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   169
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   170
fun big_list name prts = block (fbreaks (str name :: prts));
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   171
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   172
fun indent 0 prt = prt
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   173
  | indent n prt = blk (0, [str (Symbol.spaces n), prt]);
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   174
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   175
fun unbreakable (Break (_, wd)) = String (output_spaces wd, wd)
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   176
  | unbreakable (Block (m, es, indent, wd)) = Block (m, map unbreakable es, indent, wd)
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   177
  | unbreakable (e as String _) = e;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   178
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   179
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   180
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   181
(** formatting **)
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   182
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   183
(* formatted output *)
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   184
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   185
local
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   186
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17542
diff changeset
   187
type text = {tx: Buffer.T, ind: Buffer.T, pos: int, nl: int};
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17542
diff changeset
   188
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17542
diff changeset
   189
val empty: text =
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   190
 {tx = Buffer.empty,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   191
  ind = Buffer.empty,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   192
  pos = 0,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   193
  nl = 0};
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   194
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32738
diff changeset
   195
fun newline {tx, ind = _, pos = _, nl} : text =
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   196
 {tx = Buffer.add (Output.output "\n") tx,
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   197
  ind = Buffer.empty,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   198
  pos = 0,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   199
  nl = nl + 1};
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   200
23628
41cdbfb9f77b markup: emit as control information -- no indent text;
wenzelm
parents: 23617
diff changeset
   201
fun control s {tx, ind, pos: int, nl} : text =
41cdbfb9f77b markup: emit as control information -- no indent text;
wenzelm
parents: 23617
diff changeset
   202
 {tx = Buffer.add s tx,
41cdbfb9f77b markup: emit as control information -- no indent text;
wenzelm
parents: 23617
diff changeset
   203
  ind = ind,
41cdbfb9f77b markup: emit as control information -- no indent text;
wenzelm
parents: 23617
diff changeset
   204
  pos = pos,
41cdbfb9f77b markup: emit as control information -- no indent text;
wenzelm
parents: 23617
diff changeset
   205
  nl = nl};
41cdbfb9f77b markup: emit as control information -- no indent text;
wenzelm
parents: 23617
diff changeset
   206
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17542
diff changeset
   207
fun string (s, len) {tx, ind, pos: int, nl} : text =
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   208
 {tx = Buffer.add s tx,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   209
  ind = Buffer.add s ind,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   210
  pos = pos + len,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   211
  nl = nl};
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   212
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   213
fun blanks wd = string (output_spaces wd, wd);
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   214
17756
d4a35f82fbb4 minor tweaks for Poplog/ML;
wenzelm
parents: 17542
diff changeset
   215
fun indentation (buf, len) {tx, ind, pos, nl} : text =
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   216
  let val s = Buffer.content buf in
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
   217
   {tx = Buffer.add (mode_indent s len) tx,
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   218
    ind = Buffer.add s ind,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   219
    pos = pos + len,
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   220
    nl = nl}
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   221
  end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   222
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   223
(*Add the lengths of the expressions until the next Break; if no Break then
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   224
  include "after", to account for text following this block.*)
36687
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 32966
diff changeset
   225
fun breakdist (Break _ :: _, _) = 0
58020b59baf7 basic formatting of pretty trees;
wenzelm
parents: 32966
diff changeset
   226
  | breakdist (Block (_, _, _, len) :: es, after) = len + breakdist (es, after)
32784
1a5dde5079ac eliminated redundant bindings;
wenzelm
parents: 32738
diff changeset
   227
  | breakdist (String (_, len) :: es, after) = len + breakdist (es, after)
10952
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   228
  | breakdist ([], after) = after;
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   229
b520e4f1313b support general indentation (e.g. for non-tt latex output);
wenzelm
parents: 9730
diff changeset
   230
(*Search for the next break (at this or higher levels) and force it to occur.*)
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   231
fun forcenext [] = []
36690
97d2780ad6f0 uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents: 36689
diff changeset
   232
  | forcenext (Break _ :: es) = fbrk :: es
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   233
  | forcenext (e :: es) = e :: forcenext es;
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   234
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   235
in
19266
2e8ad3f2cd66 added command, keyword;
wenzelm
parents: 18802
diff changeset
   236
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   237
fun formatted margin input =
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   238
  let
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   239
    val breakgain = margin div 20;     (*minimum added space required of a break*)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   240
    val emergencypos = margin div 2;   (*position too far to right*)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   241
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   242
    (*es is list of expressions to print;
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   243
      blockin is the indentation of the current block;
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   244
      after is the width of the following context until next break.*)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   245
    fun format ([], _, _) text = text
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   246
      | format (e :: es, block as (_, blockin), after) (text as {ind, pos, nl, ...}) =
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   247
          (case e of
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   248
            Block ((bg, en), bes, indent, _) =>
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   249
              let
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   250
                val pos' = pos + indent;
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   251
                val pos'' = pos' mod emergencypos;
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   252
                val block' =
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   253
                  if pos' < emergencypos then (ind |> add_indent indent, pos')
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   254
                  else (add_indent pos'' Buffer.empty, pos'');
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   255
                val btext: text = text
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   256
                  |> control bg
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   257
                  |> format (bes, block', breakdist (es, after))
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   258
                  |> control en;
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   259
                (*if this block was broken then force the next break*)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   260
                val es' = if nl < #nl btext then forcenext es else es;
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   261
              in format (es', block, after) btext end
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   262
          | Break (force, wd) =>
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   263
              (*no break if text to next break fits on this line
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   264
                or if breaking would add only breakgain to space*)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   265
              format (es, block, after)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   266
                (if not force andalso
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   267
                    pos + wd <= Int.max (margin - breakdist (es, after), blockin + breakgain)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   268
                  then text |> blanks wd  (*just insert wd blanks*)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   269
                  else text |> newline |> indentation block)
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   270
          | String str => format (es, block, after) (string str text));
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   271
  in
36747
7361d5dde9ce discontinued Pretty.setdepth, which appears to be largely unused, but can disrupt important markup if enabled accidentally;
wenzelm
parents: 36745
diff changeset
   272
    #tx (format ([input], (Buffer.empty, 0), 0) empty)
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   273
  end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   274
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   275
end;
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   276
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   277
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   278
(* special output *)
18802
f449d516f36b renamed gen_list to enum;
wenzelm
parents: 18603
diff changeset
   279
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   280
(*symbolic markup -- no formatting*)
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   281
fun symbolic prt =
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   282
  let
30667
53fbf7c679b0 Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents: 30624
diff changeset
   283
    fun out (Block ((bg, en), [], _, _)) = Buffer.add bg #> Buffer.add en
53fbf7c679b0 Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents: 30624
diff changeset
   284
      | out (Block ((bg, en), prts, indent, _)) =
53fbf7c679b0 Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents: 30624
diff changeset
   285
          Buffer.add bg #> Buffer.markup (Markup.block indent) (fold out prts) #> Buffer.add en
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   286
      | out (String (s, _)) = Buffer.add s
23787
4868d3913961 Buffer.markup;
wenzelm
parents: 23698
diff changeset
   287
      | out (Break (false, wd)) = Buffer.markup (Markup.break wd) (Buffer.add (output_spaces wd))
36689
379f5b1e7f91 replaced slightly odd fbreak markup by plain "\n", which also coincides with regular linebreaks produced outside the ML pretty engine;
wenzelm
parents: 36687
diff changeset
   288
      | out (Break (true, _)) = Buffer.add (Output.output "\n");
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   289
  in out prt Buffer.empty end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   290
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   291
(*unformatted output*)
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   292
fun unformatted prt =
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   293
  let
30667
53fbf7c679b0 Block markup: maintain output version within tree values (in accordance with String) -- changes operational behaviour wrt. print_mode;
wenzelm
parents: 30624
diff changeset
   294
    fun fmt (Block ((bg, en), prts, _, _)) = Buffer.add bg #> fold fmt prts #> Buffer.add en
23617
840904fc1eb1 added print_mode setup: indent and markup;
wenzelm
parents: 22019
diff changeset
   295
      | fmt (String (s, _)) = Buffer.add s
36690
97d2780ad6f0 uniform treatment of length = 1 for forced breaks, also makes ML/Pretty.length coincide with Scala/XML.content_length;
wenzelm
parents: 36689
diff changeset
   296
      | fmt (Break (_, wd)) = Buffer.add (output_spaces wd);
36747
7361d5dde9ce discontinued Pretty.setdepth, which appears to be largely unused, but can disrupt important markup if enabled accidentally;
wenzelm
parents: 36745
diff changeset
   297
  in fmt prt Buffer.empty end;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   298
30624
e755b8b76365 simplified datatype ML_Pretty.pretty: model Isabelle not Poly/ML;
wenzelm
parents: 30620
diff changeset
   299
36748
wenzelm
parents: 36747
diff changeset
   300
(* output interfaces *)
30620
16b7ecc183e5 added position;
wenzelm
parents: 29606
diff changeset
   301
36748
wenzelm
parents: 36747
diff changeset
   302
val margin_default = Unsynchronized.ref 76;  (*right margin, or page width*)
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   303
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   304
val symbolicN = "pretty_symbolic";
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   305
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   306
fun output_buffer margin prt =
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   307
  if print_mode_active symbolicN then symbolic prt
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   308
  else formatted (the_default (! margin_default) margin) prt;
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   309
36745
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   310
val output = Buffer.content oo output_buffer;
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   311
fun string_of_margin margin = Output.escape o output (SOME margin);
403585a89772 unified/simplified Pretty.margin_default;
wenzelm
parents: 36733
diff changeset
   312
val string_of = Output.escape o output NONE;
23645
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   313
val str_of = Output.escape o Buffer.content o unformatted;
d220d12bd45e export mode_markup;
wenzelm
parents: 23638
diff changeset
   314
val writeln = Output.writeln o string_of;
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   315
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   316
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   317
36748
wenzelm
parents: 36747
diff changeset
   318
(** ML toplevel pretty printing **)
wenzelm
parents: 36747
diff changeset
   319
wenzelm
parents: 36747
diff changeset
   320
fun to_ML (Block (m, prts, ind, _)) = ML_Pretty.Block (m, map to_ML prts, ind)
wenzelm
parents: 36747
diff changeset
   321
  | to_ML (String s) = ML_Pretty.String s
wenzelm
parents: 36747
diff changeset
   322
  | to_ML (Break b) = ML_Pretty.Break b;
wenzelm
parents: 36747
diff changeset
   323
wenzelm
parents: 36747
diff changeset
   324
fun from_ML (ML_Pretty.Block (m, prts, ind)) = block_markup m (ind, map from_ML prts)
wenzelm
parents: 36747
diff changeset
   325
  | from_ML (ML_Pretty.String s) = String s
wenzelm
parents: 36747
diff changeset
   326
  | from_ML (ML_Pretty.Break b) = Break b;
wenzelm
parents: 36747
diff changeset
   327
37529
a23e8aa853eb treat Pretty.T as strictly abstract type;
wenzelm
parents: 37193
diff changeset
   328
end;
a23e8aa853eb treat Pretty.T as strictly abstract type;
wenzelm
parents: 37193
diff changeset
   329
36748
wenzelm
parents: 36747
diff changeset
   330
wenzelm
parents: 36747
diff changeset
   331
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   332
(** abstract pretty printing context **)
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   333
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   334
datatype pp =
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   335
  PP of (term -> T) * (typ -> T) * (sort -> T) * (class list -> T) * (arity -> T);
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   336
14972
wenzelm
parents: 14832
diff changeset
   337
val pp = PP;
14832
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   338
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   339
fun pp_fun f (PP x) = f x;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   340
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   341
val term     = pp_fun #1;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   342
val typ      = pp_fun #2;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   343
val sort     = pp_fun #3;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   344
val classrel = pp_fun #4;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   345
val arity    = pp_fun #5;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   346
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   347
val string_of_term     = string_of oo term;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   348
val string_of_typ      = string_of oo typ;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   349
val string_of_sort     = string_of oo sort;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   350
val string_of_classrel = string_of oo classrel;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   351
val string_of_arity    = string_of oo arity;
6589a58f57cb pp: abstract pretty printing context; string_of/str_of: mark result as raw output; added Pretty.unbreakable;
wenzelm
parents: 12421
diff changeset
   352
6116
8ba2f25610f7 files scan.ML, source.ML, symbol.ML, pretty.ML moved to Pure/General;
wenzelm
parents:
diff changeset
   353
end;