src/Tools/Metis/src/Print.sml
author wenzelm
Sat, 09 Jul 2011 21:53:27 +0200
changeset 43721 fad8634cee62
parent 43269 3535f16d9714
child 45778 df6e210fb44c
permissions -rw-r--r--
echo prover input via raw_messages, for improved protocol tracing;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     1
(* ========================================================================= *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     2
(* PRETTY-PRINTING                                                           *)
39502
cffceed8e7fa fix license
blanchet
parents: 39501
diff changeset
     3
(* Copyright (c) 2008 Joe Hurd, distributed under the BSD License            *)
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     4
(* ========================================================================= *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     5
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     6
structure Print :> Print =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     7
struct
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     8
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     9
open Useful;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    10
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    11
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    12
(* Constants.                                                                *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    13
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    14
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    15
val initialLineLength = 75;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    16
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    17
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    18
(* Helper functions.                                                         *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    19
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    20
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    21
fun revAppend xs s =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    22
    case xs of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    23
      [] => s ()
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    24
    | x :: xs => revAppend xs (K (Stream.Cons (x,s)));
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    25
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    26
fun revConcat strm =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    27
    case strm of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    28
      Stream.Nil => Stream.Nil
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    29
    | Stream.Cons (h,t) => revAppend h (revConcat o t);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    30
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    31
local
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    32
  fun calcSpaces n = nChars #" " n;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    33
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    34
  val cacheSize = 2 * initialLineLength;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    35
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    36
  val cachedSpaces = Vector.tabulate (cacheSize,calcSpaces);
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    37
in
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    38
  fun nSpaces n =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    39
      if n < cacheSize then Vector.sub (cachedSpaces,n)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    40
      else calcSpaces n;
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    41
end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    42
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    43
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    44
(* Escaping strings.                                                         *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    45
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    46
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    47
fun escapeString {escape} =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    48
    let
42102
fcfd07f122d4 new version of Metis 2.3 (29 Dec. 2010)
blanchet
parents: 39502
diff changeset
    49
      val escapeMap = List.map (fn c => (c, "\\" ^ str c)) escape
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    50
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    51
      fun escapeChar c =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    52
          case c of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    53
            #"\\" => "\\\\"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    54
          | #"\n" => "\\n"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    55
          | #"\t" => "\\t"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    56
          | _ =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    57
            case List.find (equal c o fst) escapeMap of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    58
              SOME (_,s) => s
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    59
            | NONE => str c
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    60
    in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    61
      String.translate escapeChar
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    62
    end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    63
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    64
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    65
(* A type of strings annotated with their size.                              *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    66
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    67
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    68
type stringSize = string * int;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    69
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    70
fun mkStringSize s = (s, size s);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    71
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    72
val emptyStringSize = mkStringSize "";
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    73
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    74
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    75
(* A type of pretty-printers.                                                *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    76
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    77
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    78
datatype breakStyle = Consistent | Inconsistent;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    79
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    80
datatype step =
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    81
    BeginBlock of breakStyle * int
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    82
  | EndBlock
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    83
  | AddString of stringSize
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    84
  | AddBreak of int
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    85
  | AddNewline;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    86
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    87
type ppstream = step Stream.stream;
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    88
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    89
fun breakStyleToString style =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    90
    case style of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    91
      Consistent => "Consistent"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    92
    | Inconsistent => "Inconsistent";
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    93
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
    94
fun stepToString step =
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    95
    case step of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    96
      BeginBlock _ => "BeginBlock"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    97
    | EndBlock => "EndBlock"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    98
    | AddString _ => "AddString"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    99
    | AddBreak _ => "AddBreak"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   100
    | AddNewline => "AddNewline";
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   101
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   102
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   103
(* Pretty-printer primitives.                                                *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   104
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   105
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   106
fun beginBlock style indent = Stream.singleton (BeginBlock (style,indent));
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   107
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   108
val endBlock = Stream.singleton EndBlock;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   109
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   110
fun addString s = Stream.singleton (AddString s);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   111
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   112
fun addBreak spaces = Stream.singleton (AddBreak spaces);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   113
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   114
val addNewline = Stream.singleton AddNewline;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   115
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   116
val skip : ppstream = Stream.Nil;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   117
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   118
fun sequence pp1 pp2 : ppstream = Stream.append pp1 (K pp2);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   119
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   120
local
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   121
  fun dup pp n () = if n = 1 then pp else Stream.append pp (dup pp (n - 1));
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   122
in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   123
  fun duplicate n pp = if n = 0 then skip else dup pp n ();
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   124
end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   125
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   126
val program : ppstream list -> ppstream = Stream.concatList;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   127
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   128
val stream : ppstream Stream.stream -> ppstream = Stream.concat;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   129
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   130
fun block style indent pp = program [beginBlock style indent, pp, endBlock];
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   131
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   132
fun blockProgram style indent pps = block style indent (program pps);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   133
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   134
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   135
(* Executing pretty-printers to generate lines.                              *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   136
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   137
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   138
datatype blockBreakStyle =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   139
    InconsistentBlock
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   140
  | ConsistentBlock
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   141
  | BreakingBlock;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   142
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   143
datatype block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   144
    Block of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   145
      {indent : int,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   146
       style : blockBreakStyle,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   147
       size : int,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   148
       chunks : chunk list}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   149
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   150
and chunk =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   151
    StringChunk of {size : int, string : string}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   152
  | BreakChunk of int
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   153
  | BlockChunk of block;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   154
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   155
datatype state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   156
    State of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   157
      {blocks : block list,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   158
       lineIndent : int,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   159
       lineSize : int};
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   160
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   161
val initialIndent = 0;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   162
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   163
val initialStyle = Inconsistent;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   164
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   165
fun liftStyle style =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   166
    case style of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   167
      Inconsistent => InconsistentBlock
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   168
    | Consistent => ConsistentBlock;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   169
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   170
fun breakStyle style =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   171
    case style of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   172
      ConsistentBlock => BreakingBlock
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   173
    | _ => style;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   174
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   175
fun sizeBlock (Block {size,...}) = size;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   176
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   177
fun sizeChunk chunk =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   178
    case chunk of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   179
      StringChunk {size,...} => size
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   180
    | BreakChunk spaces => spaces
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   181
    | BlockChunk block => sizeBlock block;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   182
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   183
val splitChunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   184
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   185
      fun split _ [] = NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   186
        | split acc (chunk :: chunks) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   187
          case chunk of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   188
            BreakChunk _ => SOME (rev acc, chunks)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   189
          | _ => split (chunk :: acc) chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   190
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   191
      split []
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   192
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   193
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   194
val sizeChunks = List.foldl (fn (c,z) => sizeChunk c + z) 0;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   195
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   196
local
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   197
  fun flatten acc chunks =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   198
      case chunks of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   199
        [] => rev acc
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   200
      | chunk :: chunks =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   201
        case chunk of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   202
          StringChunk {string = s, ...} => flatten (s :: acc) chunks
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   203
        | BreakChunk n => flatten (nSpaces n :: acc) chunks
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   204
        | BlockChunk (Block {chunks = l, ...}) =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   205
          flatten acc (List.revAppend (l,chunks));
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   206
in
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   207
  fun renderChunks indent chunks =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   208
      {indent = indent,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   209
       line = String.concat (flatten [] (rev chunks))};
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   210
end;
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   211
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   212
fun renderChunk indent chunk = renderChunks indent [chunk];
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   213
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   214
fun isEmptyBlock block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   215
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   216
      val Block {indent = _, style = _, size, chunks} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   217
42102
fcfd07f122d4 new version of Metis 2.3 (29 Dec. 2010)
blanchet
parents: 39502
diff changeset
   218
      val empty = List.null chunks
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   219
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   220
(*BasicDebug
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   221
      val _ = not empty orelse size = 0 orelse
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   222
              raise Bug "Print.isEmptyBlock: bad size"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   223
*)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   224
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   225
      empty
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   226
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   227
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   228
(*BasicDebug
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   229
fun checkBlock ind block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   230
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   231
      val Block {indent, style = _, size, chunks} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   232
      val _ = indent >= ind orelse raise Bug "Print.checkBlock: bad indents"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   233
      val size' = checkChunks indent chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   234
      val _ = size = size' orelse raise Bug "Print.checkBlock: wrong size"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   235
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   236
      size
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   237
    end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   238
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   239
and checkChunks ind chunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   240
    case chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   241
      [] => 0
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   242
    | chunk :: chunks => checkChunk ind chunk + checkChunks ind chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   243
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   244
and checkChunk ind chunk =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   245
    case chunk of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   246
      StringChunk {size,...} => size
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   247
    | BreakChunk spaces => spaces
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   248
    | BlockChunk block => checkBlock ind block;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   249
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   250
val checkBlocks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   251
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   252
      fun check ind blocks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   253
          case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   254
            [] => 0
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   255
          | block :: blocks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   256
            let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   257
              val Block {indent,...} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   258
            in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   259
              checkBlock ind block + check indent blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   260
            end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   261
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   262
      check initialIndent o rev
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   263
    end;
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   264
*)
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   265
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   266
val initialBlock =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   267
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   268
      val indent = initialIndent
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   269
      val style = liftStyle initialStyle
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   270
      val size = 0
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   271
      val chunks = []
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   272
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   273
      Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   274
        {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   275
         style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   276
         size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   277
         chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   278
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   279
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   280
val initialState =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   281
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   282
      val blocks = [initialBlock]
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   283
      val lineIndent = initialIndent
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   284
      val lineSize = 0
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   285
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   286
      State
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   287
        {blocks = blocks,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   288
         lineIndent = lineIndent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   289
         lineSize = lineSize}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   290
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   291
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   292
(*BasicDebug
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   293
fun checkState state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   294
    (let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   295
       val State {blocks, lineIndent = _, lineSize} = state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   296
       val lineSize' = checkBlocks blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   297
       val _ = lineSize = lineSize' orelse
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   298
               raise Error "wrong lineSize"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   299
     in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   300
       ()
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   301
     end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   302
     handle Error err => raise Bug err)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   303
    handle Bug bug => raise Bug ("Print.checkState: " ^ bug);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   304
*)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   305
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   306
fun isFinalState state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   307
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   308
      val State {blocks,lineIndent,lineSize} = state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   309
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   310
      case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   311
        [] => raise Bug "Print.isFinalState: no block"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   312
      | [block] => isEmptyBlock block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   313
      | _ :: _ :: _ => false
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   314
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   315
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   316
local
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   317
  fun renderBreak lineIndent (chunks,lines) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   318
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   319
        val line = renderChunks lineIndent chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   320
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   321
        val lines = line :: lines
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   322
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   323
        lines
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   324
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   325
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   326
  fun renderBreaks lineIndent lineIndent' breaks lines =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   327
      case rev breaks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   328
        [] => raise Bug "Print.renderBreaks"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   329
      | c :: cs =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   330
        let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   331
          val lines = renderBreak lineIndent (c,lines)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   332
        in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   333
          List.foldl (renderBreak lineIndent') lines cs
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   334
        end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   335
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   336
  fun splitAllChunks cumulatingChunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   337
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   338
        fun split chunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   339
            case splitChunks chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   340
              SOME (prefix,chunks) => prefix :: split chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   341
            | NONE => [List.concat (chunks :: cumulatingChunks)]
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   342
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   343
        split
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   344
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   345
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   346
  fun mkBreak style cumulatingChunks chunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   347
      case splitChunks chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   348
        NONE => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   349
      | SOME (chunks,broken) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   350
        let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   351
          val breaks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   352
              case style of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   353
                InconsistentBlock =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   354
                [List.concat (broken :: cumulatingChunks)]
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   355
              | _ => splitAllChunks cumulatingChunks broken
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   356
        in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   357
          SOME (breaks,chunks)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   358
        end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   359
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   360
  fun naturalBreak blocks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   361
      case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   362
        [] => Right ([],[])
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   363
      | block :: blocks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   364
        case naturalBreak blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   365
          Left (breaks,blocks,lineIndent,lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   366
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   367
            val Block {size,...} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   368
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   369
            val blocks = block :: blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   370
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   371
            val lineSize = lineSize + size
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   372
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   373
            Left (breaks,blocks,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   374
          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   375
        | Right (cumulatingChunks,blocks) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   376
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   377
            val Block {indent,style,size,chunks} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   378
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   379
            val style = breakStyle style
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   380
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   381
            case mkBreak style cumulatingChunks chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   382
              SOME (breaks,chunks) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   383
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   384
                val size = sizeChunks chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   385
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   386
                val block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   387
                    Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   388
                      {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   389
                       style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   390
                       size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   391
                       chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   392
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   393
                val blocks = block :: blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   394
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   395
                val lineIndent = indent
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   396
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   397
                val lineSize = size
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   398
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   399
                Left (breaks,blocks,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   400
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   401
            | NONE =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   402
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   403
                val cumulatingChunks = chunks :: cumulatingChunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   404
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   405
                val size = 0
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   406
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   407
                val chunks = []
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   408
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   409
                val block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   410
                    Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   411
                      {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   412
                       style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   413
                       size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   414
                       chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   415
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   416
                val blocks = block :: blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   417
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   418
                Right (cumulatingChunks,blocks)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   419
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   420
          end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   421
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   422
  fun forceBreakBlock cumulatingChunks block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   423
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   424
        val Block {indent, style, size = _, chunks} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   425
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   426
        val style = breakStyle style
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   427
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   428
        val break =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   429
            case mkBreak style cumulatingChunks chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   430
              SOME (breaks,chunks) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   431
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   432
                val lineSize = sizeChunks chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   433
                val lineIndent = indent
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   434
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   435
                SOME (breaks,chunks,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   436
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   437
            | NONE => forceBreakChunks cumulatingChunks chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   438
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   439
        case break of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   440
          SOME (breaks,chunks,lineIndent,lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   441
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   442
            val size = lineSize
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   443
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   444
            val block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   445
                Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   446
                  {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   447
                   style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   448
                   size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   449
                   chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   450
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   451
            SOME (breaks,block,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   452
          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   453
        | NONE => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   454
      end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   455
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   456
  and forceBreakChunks cumulatingChunks chunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   457
      case chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   458
        [] => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   459
      | chunk :: chunks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   460
        case forceBreakChunk (chunks :: cumulatingChunks) chunk of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   461
          SOME (breaks,chunk,lineIndent,lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   462
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   463
            val chunks = [chunk]
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   464
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   465
            SOME (breaks,chunks,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   466
          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   467
        | NONE =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   468
          case forceBreakChunks cumulatingChunks chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   469
            SOME (breaks,chunks,lineIndent,lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   470
            let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   471
              val chunks = chunk :: chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   472
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   473
              val lineSize = lineSize + sizeChunk chunk
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   474
            in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   475
              SOME (breaks,chunks,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   476
            end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   477
          | NONE => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   478
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   479
  and forceBreakChunk cumulatingChunks chunk =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   480
      case chunk of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   481
        StringChunk _ => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   482
      | BreakChunk _ => raise Bug "Print.forceBreakChunk: BreakChunk"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   483
      | BlockChunk block =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   484
        case forceBreakBlock cumulatingChunks block of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   485
          SOME (breaks,block,lineIndent,lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   486
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   487
            val chunk = BlockChunk block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   488
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   489
            SOME (breaks,chunk,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   490
          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   491
        | NONE => NONE;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   492
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   493
  fun forceBreak cumulatingChunks blocks' blocks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   494
      case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   495
        [] => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   496
      | block :: blocks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   497
        let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   498
          val cumulatingChunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   499
              case cumulatingChunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   500
                [] => raise Bug "Print.forceBreak: null cumulatingChunks"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   501
              | _ :: cumulatingChunks => cumulatingChunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   502
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   503
          val blocks' =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   504
              case blocks' of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   505
                [] => raise Bug "Print.forceBreak: null blocks'"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   506
              | _ :: blocks' => blocks'
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   507
        in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   508
          case forceBreakBlock cumulatingChunks block of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   509
            SOME (breaks,block,lineIndent,lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   510
            let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   511
              val blocks = block :: blocks'
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   512
            in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   513
              SOME (breaks,blocks,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   514
            end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   515
          | NONE =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   516
            case forceBreak cumulatingChunks blocks' blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   517
              SOME (breaks,blocks,lineIndent,lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   518
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   519
                val blocks = block :: blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   520
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   521
                val Block {size,...} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   522
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   523
                val lineSize = lineSize + size
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   524
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   525
                SOME (breaks,blocks,lineIndent,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   526
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   527
            | NONE => NONE
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   528
        end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   529
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   530
  fun normalize lineLength lines state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   531
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   532
        val State {blocks,lineIndent,lineSize} = state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   533
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   534
        if lineIndent + lineSize <= lineLength then (lines,state)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   535
        else
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   536
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   537
            val break =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   538
                case naturalBreak blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   539
                  Left break => SOME break
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   540
                | Right (c,b) => forceBreak c b blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   541
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   542
            case break of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   543
              SOME (breaks,blocks,lineIndent',lineSize) =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   544
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   545
                val lines = renderBreaks lineIndent lineIndent' breaks lines
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   546
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   547
                val state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   548
                    State
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   549
                      {blocks = blocks,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   550
                       lineIndent = lineIndent',
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   551
                       lineSize = lineSize}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   552
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   553
                normalize lineLength lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   554
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   555
            | NONE => (lines,state)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   556
          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   557
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   558
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   559
(*BasicDebug
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   560
  val normalize = fn lineLength => fn lines => fn state =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   561
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   562
        val () = checkState state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   563
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   564
        normalize lineLength lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   565
      end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   566
      handle Bug bug =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   567
        raise Bug ("Print.normalize: before normalize:\n" ^ bug)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   568
*)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   569
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   570
  fun executeBeginBlock (style,ind) lines state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   571
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   572
        val State {blocks,lineIndent,lineSize} = state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   573
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   574
        val Block {indent,...} =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   575
            case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   576
              [] => raise Bug "Print.executeBeginBlock: no block"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   577
            | block :: _ => block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   578
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   579
        val indent = indent + ind
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   580
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   581
        val style = liftStyle style
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   582
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   583
        val size = 0
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   584
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   585
        val chunks = []
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   586
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   587
        val block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   588
            Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   589
              {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   590
               style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   591
               size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   592
               chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   593
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   594
        val blocks = block :: blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   595
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   596
        val state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   597
            State
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   598
              {blocks = blocks,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   599
               lineIndent = lineIndent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   600
               lineSize = lineSize}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   601
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   602
        (lines,state)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   603
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   604
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   605
  fun executeEndBlock lines state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   606
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   607
        val State {blocks,lineIndent,lineSize} = state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   608
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   609
        val (lineSize,blocks) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   610
            case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   611
              [] => raise Bug "Print.executeEndBlock: no block"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   612
            | topBlock :: blocks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   613
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   614
                val Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   615
                      {indent = topIndent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   616
                       style = topStyle,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   617
                       size = topSize,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   618
                       chunks = topChunks} = topBlock
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   619
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   620
                case topChunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   621
                  [] => (lineSize,blocks)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   622
                | headTopChunks :: tailTopChunks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   623
                  let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   624
                    val (lineSize,topSize,topChunks) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   625
                        case headTopChunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   626
                          BreakChunk spaces =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   627
                          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   628
                            val lineSize = lineSize - spaces
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   629
                            and topSize = topSize - spaces
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   630
                            and topChunks = tailTopChunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   631
                          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   632
                            (lineSize,topSize,topChunks)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   633
                          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   634
                        | _ => (lineSize,topSize,topChunks)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   635
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   636
                    val topBlock =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   637
                        Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   638
                          {indent = topIndent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   639
                           style = topStyle,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   640
                           size = topSize,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   641
                           chunks = topChunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   642
                  in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   643
                    case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   644
                      [] => raise Error "Print.executeEndBlock: no block"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   645
                    | block :: blocks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   646
                      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   647
                        val Block {indent,style,size,chunks} = block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   648
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   649
                        val size = size + topSize
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   650
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   651
                        val chunks = BlockChunk topBlock :: chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   652
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   653
                        val block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   654
                            Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   655
                              {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   656
                               style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   657
                               size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   658
                               chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   659
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   660
                        val blocks = block :: blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   661
                      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   662
                        (lineSize,blocks)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   663
                      end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   664
                  end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   665
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   666
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   667
        val state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   668
            State
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   669
              {blocks = blocks,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   670
               lineIndent = lineIndent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   671
               lineSize = lineSize}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   672
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   673
        (lines,state)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   674
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   675
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   676
  fun executeAddString lineLength (s,n) lines state =
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   677
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   678
        val State {blocks,lineIndent,lineSize} = state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   679
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   680
        val blocks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   681
            case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   682
              [] => raise Bug "Print.executeAddString: no block"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   683
            | Block {indent,style,size,chunks} :: blocks =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   684
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   685
                val size = size + n
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   686
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   687
                val chunk = StringChunk {size = n, string = s}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   688
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   689
                val chunks = chunk :: chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   690
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   691
                val block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   692
                    Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   693
                      {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   694
                       style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   695
                       size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   696
                       chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   697
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   698
                val blocks = block :: blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   699
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   700
                blocks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   701
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   702
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   703
        val lineSize = lineSize + n
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   704
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   705
        val state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   706
            State
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   707
              {blocks = blocks,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   708
               lineIndent = lineIndent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   709
               lineSize = lineSize}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   710
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   711
        normalize lineLength lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   712
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   713
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   714
  fun executeAddBreak lineLength spaces lines state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   715
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   716
        val State {blocks,lineIndent,lineSize} = state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   717
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   718
        val (blocks,lineSize) =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   719
            case blocks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   720
              [] => raise Bug "Print.executeAddBreak: no block"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   721
            | Block {indent,style,size,chunks} :: blocks' =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   722
              case chunks of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   723
                [] => (blocks,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   724
              | chunk :: chunks' =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   725
                let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   726
                  val spaces =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   727
                      case style of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   728
                        BreakingBlock => lineLength + 1
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   729
                      | _ => spaces
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   730
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   731
                  val size = size + spaces
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   732
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   733
                  val chunks =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   734
                      case chunk of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   735
                        BreakChunk k => BreakChunk (k + spaces) :: chunks'
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   736
                      | _ => BreakChunk spaces :: chunks
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   737
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   738
                  val block =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   739
                      Block
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   740
                        {indent = indent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   741
                         style = style,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   742
                         size = size,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   743
                         chunks = chunks}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   744
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   745
                  val blocks = block :: blocks'
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   746
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   747
                  val lineSize = lineSize + spaces
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   748
                in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   749
                  (blocks,lineSize)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   750
                end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   751
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   752
        val state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   753
            State
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   754
              {blocks = blocks,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   755
               lineIndent = lineIndent,
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   756
               lineSize = lineSize}
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   757
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   758
        normalize lineLength lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   759
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   760
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   761
  fun executeBigBreak lineLength lines state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   762
      executeAddBreak lineLength (lineLength + 1) lines state;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   763
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   764
  fun executeAddNewline lineLength lines state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   765
      let
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   766
        val (lines,state) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   767
            executeAddString lineLength emptyStringSize lines state
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   768
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   769
        val (lines,state) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   770
            executeBigBreak lineLength lines state
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   771
      in
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   772
        executeAddString lineLength emptyStringSize lines state
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   773
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   774
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   775
  fun final lineLength lines state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   776
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   777
        val lines =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   778
            if isFinalState state then lines
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   779
            else
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   780
              let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   781
                val (lines,state) = executeBigBreak lineLength lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   782
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   783
(*BasicDebug
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   784
                val _ = isFinalState state orelse raise Bug "Print.final"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   785
*)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   786
              in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   787
                lines
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   788
              end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   789
      in
42102
fcfd07f122d4 new version of Metis 2.3 (29 Dec. 2010)
blanchet
parents: 39502
diff changeset
   790
        if List.null lines then Stream.Nil else Stream.singleton lines
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   791
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   792
in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   793
  fun execute {lineLength} =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   794
      let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   795
        fun advance step state =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   796
            let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   797
              val lines = []
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   798
            in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   799
              case step of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   800
                BeginBlock style_ind => executeBeginBlock style_ind lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   801
              | EndBlock => executeEndBlock lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   802
              | AddString s => executeAddString lineLength s lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   803
              | AddBreak spaces => executeAddBreak lineLength spaces lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   804
              | AddNewline => executeAddNewline lineLength lines state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   805
            end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   806
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   807
(*BasicDebug
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   808
        val advance = fn step => fn state =>
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   809
            let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   810
              val (lines,state) = advance step state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   811
              val () = checkState state
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   812
            in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   813
              (lines,state)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   814
            end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   815
            handle Bug bug =>
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   816
              raise Bug ("Print.advance: after " ^ stepToString step ^
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   817
                         " command:\n" ^ bug)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   818
*)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   819
      in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   820
        revConcat o Stream.maps advance (final lineLength []) initialState
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   821
      end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   822
end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   823
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
   824
(* ------------------------------------------------------------------------- *)
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   825
(* Pretty-printer combinators.                                               *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   826
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   827
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   828
type 'a pp = 'a -> ppstream;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   829
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   830
fun ppMap f ppB a : ppstream = ppB (f a);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   831
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   832
fun ppBracket' l r ppA a =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   833
    let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   834
      val (_,n) = l
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   835
    in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   836
      blockProgram Inconsistent n
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   837
        [addString l,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   838
         ppA a,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   839
         addString r]
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   840
    end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   841
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   842
fun ppOp' s = sequence (addString s) (addBreak 1);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   843
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   844
fun ppOp2' ab ppA ppB (a,b) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   845
    blockProgram Inconsistent 0
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   846
      [ppA a,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   847
       ppOp' ab,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   848
       ppB b];
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   849
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   850
fun ppOp3' ab bc ppA ppB ppC (a,b,c) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   851
    blockProgram Inconsistent 0
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   852
      [ppA a,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   853
       ppOp' ab,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   854
       ppB b,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   855
       ppOp' bc,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   856
       ppC c];
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   857
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   858
fun ppOpList' s ppA =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   859
    let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   860
      fun ppOpA a = sequence (ppOp' s) (ppA a)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   861
    in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   862
      fn [] => skip
42102
fcfd07f122d4 new version of Metis 2.3 (29 Dec. 2010)
blanchet
parents: 39502
diff changeset
   863
       | h :: t => blockProgram Inconsistent 0 (ppA h :: List.map ppOpA t)
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   864
    end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   865
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   866
fun ppOpStream' s ppA =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   867
    let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   868
      fun ppOpA a = sequence (ppOp' s) (ppA a)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   869
    in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   870
      fn Stream.Nil => skip
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   871
       | Stream.Cons (h,t) =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   872
         blockProgram Inconsistent 0
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   873
           [ppA h,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   874
            Stream.concat (Stream.map ppOpA (t ()))]
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   875
    end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   876
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   877
fun ppBracket l r = ppBracket' (mkStringSize l) (mkStringSize r);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   878
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   879
fun ppOp s = ppOp' (mkStringSize s);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   880
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   881
fun ppOp2 ab = ppOp2' (mkStringSize ab);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   882
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   883
fun ppOp3 ab bc = ppOp3' (mkStringSize ab) (mkStringSize bc);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   884
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   885
fun ppOpList s = ppOpList' (mkStringSize s);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   886
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   887
fun ppOpStream s = ppOpStream' (mkStringSize s);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   888
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   889
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   890
(* Pretty-printers for common types.                                         *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   891
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   892
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   893
fun ppChar c = addString (str c, 1);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   894
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   895
fun ppString s = addString (mkStringSize s);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   896
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   897
fun ppEscapeString escape = ppMap (escapeString escape) ppString;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   898
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   899
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   900
  val pp = ppString "()";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   901
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   902
  fun ppUnit () = pp;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   903
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   904
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   905
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   906
  val ppTrue = ppString "true"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   907
  and ppFalse = ppString "false";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   908
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   909
  fun ppBool b = if b then ppTrue else ppFalse;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   910
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   911
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   912
val ppInt = ppMap Int.toString ppString;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   913
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   914
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   915
  val ppNeg = ppString "~"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   916
  and ppSep = ppString ","
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   917
  and ppZero = ppString "0"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   918
  and ppZeroZero = ppString "00";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   919
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   920
  fun ppIntBlock i =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   921
      if i < 10 then sequence ppZeroZero (ppInt i)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   922
      else if i < 100 then sequence ppZero (ppInt i)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   923
      else ppInt i;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   924
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   925
  fun ppIntBlocks i =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   926
      if i < 1000 then ppInt i
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   927
      else sequence (ppIntBlocks (i div 1000))
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   928
             (sequence ppSep (ppIntBlock (i mod 1000)));
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   929
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   930
  fun ppPrettyInt i =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   931
      if i < 0 then sequence ppNeg (ppIntBlocks (~i))
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   932
      else ppIntBlocks i;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   933
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   934
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   935
val ppReal = ppMap Real.toString ppString;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   936
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   937
val ppPercent = ppMap percentToString ppString;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   938
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   939
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   940
  val ppLess = ppString "Less"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   941
  and ppEqual = ppString "Equal"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   942
  and ppGreater = ppString "Greater";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   943
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   944
  fun ppOrder ord =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   945
      case ord of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   946
        LESS => ppLess
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   947
      | EQUAL => ppEqual
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   948
      | GREATER => ppGreater;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   949
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   950
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   951
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   952
  val left = mkStringSize "["
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   953
  and right = mkStringSize "]"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   954
  and sep = mkStringSize ",";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   955
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   956
  fun ppList ppX xs = ppBracket' left right (ppOpList' sep ppX) xs;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   957
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   958
  fun ppStream ppX xs = ppBracket' left right (ppOpStream' sep ppX) xs;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   959
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   960
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   961
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   962
  val ppNone = ppString "-";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   963
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   964
  fun ppOption ppX xo =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   965
      case xo of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   966
        SOME x => ppX x
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   967
      | NONE => ppNone;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   968
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   969
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   970
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   971
  val left = mkStringSize "("
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   972
  and right = mkStringSize ")"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   973
  and sep = mkStringSize ",";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   974
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   975
  fun ppPair ppA ppB =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   976
      ppBracket' left right (ppOp2' sep ppA ppB);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   977
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   978
  fun ppTriple ppA ppB ppC =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   979
      ppBracket' left right (ppOp3' sep sep ppA ppB ppC);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   980
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   981
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   982
val ppBreakStyle = ppMap breakStyleToString ppString;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   983
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   984
val ppStep = ppMap stepToString ppString;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   985
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   986
val ppStringSize =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   987
    let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   988
      val left = mkStringSize "\""
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   989
      and right = mkStringSize "\""
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   990
      and escape = {escape = [#"\""]}
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   991
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   992
      val pp = ppBracket' left right (ppEscapeString escape)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   993
    in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   994
      fn (s,n) => if size s = n then pp s else ppPair pp ppInt (s,n)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   995
    end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   996
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   997
fun ppStep step =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   998
    blockProgram Inconsistent 2
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
   999
      (ppStep step ::
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1000
       (case step of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1001
          BeginBlock style_indent =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1002
            [addBreak 1,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1003
             ppPair ppBreakStyle ppInt style_indent]
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1004
        | EndBlock => []
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1005
        | AddString s =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1006
            [addBreak 1,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1007
             ppStringSize s]
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1008
        | AddBreak n =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1009
            [addBreak 1,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1010
             ppInt n]
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1011
        | AddNewline => []));
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1012
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1013
val ppPpstream = ppStream ppStep;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1014
42102
fcfd07f122d4 new version of Metis 2.3 (29 Dec. 2010)
blanchet
parents: 39502
diff changeset
  1015
fun ppException e = ppString (exnMessage e);
fcfd07f122d4 new version of Metis 2.3 (29 Dec. 2010)
blanchet
parents: 39502
diff changeset
  1016
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1017
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1018
(* Pretty-printing infix operators.                                          *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1019
(* ------------------------------------------------------------------------- *)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1020
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1021
type token = string;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1022
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1023
datatype assoc =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1024
    LeftAssoc
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1025
  | NonAssoc
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1026
  | RightAssoc;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1027
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1028
datatype infixes =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1029
    Infixes of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1030
      {token : token,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1031
       precedence : int,
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1032
       assoc : assoc} list;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1033
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1034
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1035
  fun comparePrecedence (io1,io2) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1036
      let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1037
        val {token = _, precedence = p1, assoc = _} = io1
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1038
        and {token = _, precedence = p2, assoc = _} = io2
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1039
      in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1040
        Int.compare (p2,p1)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1041
      end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1042
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1043
  fun equalAssoc a a' =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1044
      case a of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1045
        LeftAssoc => (case a' of LeftAssoc => true | _ => false)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1046
      | NonAssoc => (case a' of NonAssoc => true | _ => false)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1047
      | RightAssoc => (case a' of RightAssoc => true | _ => false);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1048
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1049
  fun new t a acc = {tokens = StringSet.singleton t, assoc = a} :: acc;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1050
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1051
  fun add t a' acc =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1052
      case acc of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1053
        [] => raise Bug "Print.layerInfixes: null"
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1054
      | {tokens = ts, assoc = a} :: acc =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1055
        if equalAssoc a a' then {tokens = StringSet.add ts t, assoc = a} :: acc
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1056
        else raise Bug "Print.layerInfixes: mixed assocs";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1057
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1058
  fun layer ({token = t, precedence = p, assoc = a}, (acc,p')) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1059
      let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1060
        val acc = if p = p' then add t a acc else new t a acc
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1061
      in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1062
        (acc,p)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1063
      end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1064
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1065
  fun layerInfixes (Infixes ios) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1066
      case sort comparePrecedence ios of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1067
        [] => []
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1068
      | {token = t, precedence = p, assoc = a} :: ios =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1069
        let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1070
          val acc = new t a []
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1071
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1072
          val (acc,_) = List.foldl layer (acc,p) ios
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1073
        in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1074
          acc
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1075
        end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1076
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1077
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1078
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1079
  fun add ({tokens = ts, assoc = _}, tokens) = StringSet.union ts tokens;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1080
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1081
  fun tokensLayeredInfixes l = List.foldl add StringSet.empty l;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1082
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1083
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1084
fun tokensInfixes ios = tokensLayeredInfixes (layerInfixes ios);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1085
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1086
fun destInfixOp dest tokens tm =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1087
    case dest tm of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1088
      NONE => NONE
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1089
    | s as SOME (t,a,b) => if StringSet.member t tokens then s else NONE;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1090
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1091
fun ppLayeredInfixes dest ppTok {tokens,assoc} ppLower ppSub =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1092
    let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1093
      fun isLayer t = StringSet.member t tokens
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1094
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1095
      fun ppTerm aligned (tm,r) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1096
          case dest tm of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1097
            NONE => ppSub (tm,r)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1098
          | SOME (t,a,b) =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1099
            if aligned andalso isLayer t then ppLayer (tm,t,a,b,r)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1100
            else ppLower (tm,t,a,b,r)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1101
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1102
      and ppLeft tm_r =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1103
          let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1104
            val aligned = case assoc of LeftAssoc => true | _ => false
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1105
          in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1106
            ppTerm aligned tm_r
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1107
          end
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1108
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1109
      and ppLayer (tm,t,a,b,r) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1110
          program
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1111
            [ppLeft (a,true),
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1112
             ppTok (tm,t),
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1113
             ppRight (b,r)]
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1114
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1115
      and ppRight tm_r =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1116
          let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1117
            val aligned = case assoc of RightAssoc => true | _ => false
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1118
          in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1119
            ppTerm aligned tm_r
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1120
          end
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1121
    in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1122
      fn tm_t_a_b_r as (_,t,_,_,_) =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1123
         if isLayer t then block Inconsistent 0 (ppLayer tm_t_a_b_r)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1124
         else ppLower tm_t_a_b_r
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1125
    end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1126
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1127
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1128
  val leftBrack = mkStringSize "("
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1129
  and rightBrack = mkStringSize ")";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1130
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1131
  fun ppInfixes ops =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1132
      let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1133
        val layers = layerInfixes ops
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1134
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1135
        val toks = tokensLayeredInfixes layers
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1136
      in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1137
        fn dest => fn ppTok => fn ppSub =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1138
           let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1139
             fun destOp tm = destInfixOp dest toks tm
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1140
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1141
             fun ppInfix tm_t_a_b_r = ppLayers layers tm_t_a_b_r
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1142
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1143
             and ppLayers ls (tm,t,a,b,r) =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1144
                 case ls of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1145
                   [] =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1146
                   ppBracket' leftBrack rightBrack ppInfix (tm,t,a,b,false)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1147
                 | l :: ls =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1148
                   let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1149
                     val ppLower = ppLayers ls
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1150
                   in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1151
                     ppLayeredInfixes destOp ppTok l ppLower ppSub (tm,t,a,b,r)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1152
                   end
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1153
           in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1154
             fn (tm,r) =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1155
                case destOp tm of
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1156
                  SOME (t,a,b) => ppInfix (tm,t,a,b,r)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1157
                | NONE => ppSub (tm,r)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1158
           end
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1159
      end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1160
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1161
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1162
(* ------------------------------------------------------------------------- *)
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1163
(* Executing pretty-printers with a global line length.                      *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1164
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1165
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1166
val lineLength = ref initialLineLength;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1167
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1168
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1169
  fun inc {indent,line} acc = line :: nSpaces indent :: acc;
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1170
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1171
  fun incn (indent_line,acc) = inc indent_line ("\n" :: acc);
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1172
in
43269
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1173
  fun toLines len ppA a =
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1174
      case execute {lineLength = len} (ppA a) of
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1175
        Stream.Nil => ""
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1176
      | Stream.Cons (h,t) =>
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1177
        let
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1178
          val lines = Stream.foldl incn (inc h []) (t ())
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1179
        in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1180
          String.concat (rev lines)
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1181
        end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1182
end;
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1183
43269
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1184
fun toString ppA a = toLines (!lineLength) ppA a;
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1185
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1186
fun toLine ppA a = toLines 100000 ppA a;
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1187
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1188
fun toStream ppA a =
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1189
    Stream.map (fn {indent,line} => nSpaces indent ^ line ^ "\n")
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1190
      (execute {lineLength = !lineLength} (ppA a));
3535f16d9714 new Metis version
blanchet
parents: 42102
diff changeset
  1191
39443
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1192
local
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1193
  val sep = mkStringSize " =";
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1194
in
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1195
  fun trace ppX nameX x =
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1196
      Useful.trace (toString (ppOp2' sep ppString ppX) (nameX,x) ^ "\n");
e330437cd22a copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents: 39349
diff changeset
  1197
end;
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1198
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
  1199
end