| author | wenzelm | 
| Sun, 01 Oct 2017 12:28:52 +0200 | |
| changeset 66736 | 148891036469 | 
| parent 45778 | df6e210fb44c | 
| child 72004 | 913162a47d9f | 
| permissions | -rw-r--r-- | 
| 39348 | 1 | (* ========================================================================= *) | 
| 2 | (* PRETTY-PRINTING *) | |
| 39502 | 3 | (* Copyright (c) 2008 Joe Hurd, distributed under the BSD License *) | 
| 39348 | 4 | (* ========================================================================= *) | 
| 5 | ||
| 6 | structure Print :> Print = | |
| 7 | struct | |
| 8 | ||
| 9 | open Useful; | |
| 10 | ||
| 11 | (* ------------------------------------------------------------------------- *) | |
| 12 | (* Constants. *) | |
| 13 | (* ------------------------------------------------------------------------- *) | |
| 14 | ||
| 15 | val initialLineLength = 75; | |
| 16 | ||
| 17 | (* ------------------------------------------------------------------------- *) | |
| 18 | (* Helper functions. *) | |
| 19 | (* ------------------------------------------------------------------------- *) | |
| 20 | ||
| 21 | fun revAppend xs s = | |
| 22 | case xs of | |
| 23 | [] => s () | |
| 24 | | x :: xs => revAppend xs (K (Stream.Cons (x,s))); | |
| 25 | ||
| 26 | fun revConcat strm = | |
| 27 | case strm of | |
| 28 | Stream.Nil => Stream.Nil | |
| 45778 | 29 | | Stream.Cons (h,t) => revAppend h (fn () => revConcat (t ())); | 
| 39348 | 30 | |
| 31 | local | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 32 | fun calcSpaces n = nChars #" " n; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 33 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 34 | val cacheSize = 2 * initialLineLength; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 35 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 36 | val cachedSpaces = Vector.tabulate (cacheSize,calcSpaces); | 
| 39348 | 37 | in | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 38 | fun nSpaces n = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
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: 
39349diff
changeset | 40 | else calcSpaces n; | 
| 39348 | 41 | end; | 
| 42 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 43 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 44 | (* Escaping strings. *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 45 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 46 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 47 | fun escapeString {escape} =
 | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 48 | let | 
| 42102 | 49 | val escapeMap = List.map (fn c => (c, "\\" ^ str c)) escape | 
| 39348 | 50 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 51 | fun escapeChar c = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 52 | case c of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 53 | #"\\" => "\\\\" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 54 | | #"\n" => "\\n" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 55 | | #"\t" => "\\t" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 56 | | _ => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
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: 
39349diff
changeset | 58 | SOME (_,s) => s | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 59 | | NONE => str c | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 60 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 61 | String.translate escapeChar | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 62 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 63 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 64 | (* ------------------------------------------------------------------------- *) | 
| 45778 | 65 | (* Pretty-printing blocks. *) | 
| 66 | (* ------------------------------------------------------------------------- *) | |
| 67 | ||
| 68 | datatype style = Consistent | Inconsistent; | |
| 69 | ||
| 70 | datatype block = | |
| 71 | Block of | |
| 72 |       {style : style,
 | |
| 73 | indent : int}; | |
| 74 | ||
| 75 | fun toStringStyle style = | |
| 76 | case style of | |
| 77 | Consistent => "Consistent" | |
| 78 | | Inconsistent => "Inconsistent"; | |
| 79 | ||
| 80 | fun isConsistentStyle style = | |
| 81 | case style of | |
| 82 | Consistent => true | |
| 83 | | Inconsistent => false; | |
| 84 | ||
| 85 | fun isInconsistentStyle style = | |
| 86 | case style of | |
| 87 | Inconsistent => true | |
| 88 | | Consistent => false; | |
| 89 | ||
| 90 | fun mkBlock style indent = | |
| 91 | Block | |
| 92 |       {style = style,
 | |
| 93 | indent = indent}; | |
| 94 | ||
| 95 | val mkConsistentBlock = mkBlock Consistent; | |
| 96 | ||
| 97 | val mkInconsistentBlock = mkBlock Inconsistent; | |
| 98 | ||
| 99 | fun styleBlock (Block {style = x, ...}) = x;
 | |
| 100 | ||
| 101 | fun indentBlock (Block {indent = x, ...}) = x;
 | |
| 102 | ||
| 103 | fun isConsistentBlock block = isConsistentStyle (styleBlock block); | |
| 104 | ||
| 105 | fun isInconsistentBlock block = isInconsistentStyle (styleBlock block); | |
| 106 | ||
| 107 | (* ------------------------------------------------------------------------- *) | |
| 108 | (* Words are unbreakable strings annotated with their effective size. *) | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 109 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 110 | |
| 45778 | 111 | datatype word = Word of {word : string, size : int};
 | 
| 112 | ||
| 113 | fun mkWord s = Word {word = s, size = String.size s};
 | |
| 114 | ||
| 115 | val emptyWord = mkWord ""; | |
| 116 | ||
| 117 | fun charWord c = mkWord (str c); | |
| 118 | ||
| 119 | fun spacesWord i = Word {word = nSpaces i, size = i};
 | |
| 120 | ||
| 121 | fun sizeWord (Word {size = x, ...}) = x;
 | |
| 122 | ||
| 123 | fun renderWord (Word {word = x, ...}) = x;
 | |
| 124 | ||
| 125 | (* ------------------------------------------------------------------------- *) | |
| 126 | (* Possible line breaks. *) | |
| 127 | (* ------------------------------------------------------------------------- *) | |
| 128 | ||
| 129 | datatype break = Break of {size : int, extraIndent : int};
 | |
| 130 | ||
| 131 | fun mkBreak n = Break {size = n, extraIndent = 0};
 | |
| 132 | ||
| 133 | fun sizeBreak (Break {size = x, ...}) = x;
 | |
| 134 | ||
| 135 | fun extraIndentBreak (Break {extraIndent = x, ...}) = x;
 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 136 | |
| 45778 | 137 | fun renderBreak b = nSpaces (sizeBreak b); | 
| 138 | ||
| 139 | fun updateSizeBreak size break = | |
| 140 | let | |
| 141 |       val Break {size = _, extraIndent} = break
 | |
| 142 | in | |
| 143 | Break | |
| 144 |         {size = size,
 | |
| 145 | extraIndent = extraIndent} | |
| 146 | end; | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 147 | |
| 45778 | 148 | fun appendBreak break1 break2 = | 
| 149 | let | |
| 150 | (*BasicDebug | |
| 151 | val () = warn "merging consecutive pretty-printing breaks" | |
| 152 | *) | |
| 153 |       val Break {size = size1, extraIndent = extraIndent1} = break1
 | |
| 154 |       and Break {size = size2, extraIndent = extraIndent2} = break2
 | |
| 155 | ||
| 156 | val size = size1 + size2 | |
| 157 | and extraIndent = Int.max (extraIndent1,extraIndent2) | |
| 158 | in | |
| 159 | Break | |
| 160 |         {size = size,
 | |
| 161 | extraIndent = extraIndent} | |
| 162 | end; | |
| 39348 | 163 | |
| 164 | (* ------------------------------------------------------------------------- *) | |
| 165 | (* A type of pretty-printers. *) | |
| 166 | (* ------------------------------------------------------------------------- *) | |
| 167 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 168 | datatype step = | 
| 45778 | 169 | BeginBlock of block | 
| 39348 | 170 | | EndBlock | 
| 45778 | 171 | | AddWord of word | 
| 172 | | AddBreak of break | |
| 39348 | 173 | | AddNewline; | 
| 174 | ||
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 175 | type ppstream = step Stream.stream; | 
| 39348 | 176 | |
| 45778 | 177 | type 'a pp = 'a -> ppstream; | 
| 39348 | 178 | |
| 45778 | 179 | fun toStringStep step = | 
| 39348 | 180 | case step of | 
| 181 | BeginBlock _ => "BeginBlock" | |
| 182 | | EndBlock => "EndBlock" | |
| 45778 | 183 | | AddWord _ => "Word" | 
| 184 | | AddBreak _ => "Break" | |
| 185 | | AddNewline => "Newline"; | |
| 39348 | 186 | |
| 187 | val skip : ppstream = Stream.Nil; | |
| 188 | ||
| 189 | fun sequence pp1 pp2 : ppstream = Stream.append pp1 (K pp2); | |
| 190 | ||
| 191 | local | |
| 192 | fun dup pp n () = if n = 1 then pp else Stream.append pp (dup pp (n - 1)); | |
| 193 | in | |
| 45778 | 194 | fun duplicate n pp : ppstream = | 
| 195 | let | |
| 196 | (*BasicDebug | |
| 197 | val () = if 0 <= n then () else raise Bug "Print.duplicate" | |
| 198 | *) | |
| 199 | in | |
| 200 | if n = 0 then skip else dup pp n () | |
| 201 | end; | |
| 39348 | 202 | end; | 
| 203 | ||
| 204 | val program : ppstream list -> ppstream = Stream.concatList; | |
| 205 | ||
| 206 | val stream : ppstream Stream.stream -> ppstream = Stream.concat; | |
| 207 | ||
| 208 | (* ------------------------------------------------------------------------- *) | |
| 45778 | 209 | (* Pretty-printing blocks. *) | 
| 210 | (* ------------------------------------------------------------------------- *) | |
| 39348 | 211 | |
| 212 | local | |
| 45778 | 213 | fun beginBlock b = Stream.singleton (BeginBlock b); | 
| 39348 | 214 | |
| 45778 | 215 | val endBlock = Stream.singleton EndBlock; | 
| 216 | in | |
| 217 | fun block b pp = program [beginBlock b, pp, endBlock]; | |
| 218 | end; | |
| 39348 | 219 | |
| 45778 | 220 | fun consistentBlock i pps = block (mkConsistentBlock i) (program pps); | 
| 39348 | 221 | |
| 45778 | 222 | fun inconsistentBlock i pps = block (mkInconsistentBlock i) (program pps); | 
| 39348 | 223 | |
| 45778 | 224 | (* ------------------------------------------------------------------------- *) | 
| 225 | (* Words are unbreakable strings annotated with their effective size. *) | |
| 226 | (* ------------------------------------------------------------------------- *) | |
| 39348 | 227 | |
| 45778 | 228 | fun ppWord w = Stream.singleton (AddWord w); | 
| 39348 | 229 | |
| 45778 | 230 | val space = ppWord (mkWord " "); | 
| 39348 | 231 | |
| 45778 | 232 | fun spaces i = ppWord (spacesWord i); | 
| 39348 | 233 | |
| 45778 | 234 | (* ------------------------------------------------------------------------- *) | 
| 235 | (* Possible line breaks. *) | |
| 236 | (* ------------------------------------------------------------------------- *) | |
| 39348 | 237 | |
| 45778 | 238 | fun ppBreak b = Stream.singleton (AddBreak b); | 
| 39348 | 239 | |
| 45778 | 240 | fun breaks i = ppBreak (mkBreak i); | 
| 39348 | 241 | |
| 45778 | 242 | val break = breaks 1; | 
| 39348 | 243 | |
| 45778 | 244 | (* ------------------------------------------------------------------------- *) | 
| 245 | (* Forced line breaks. *) | |
| 246 | (* ------------------------------------------------------------------------- *) | |
| 39348 | 247 | |
| 45778 | 248 | val newline = Stream.singleton AddNewline; | 
| 39348 | 249 | |
| 45778 | 250 | fun newlines i = duplicate i newline; | 
| 39348 | 251 | |
| 252 | (* ------------------------------------------------------------------------- *) | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 253 | (* Pretty-printer combinators. *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 254 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 255 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 256 | 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: 
39349diff
changeset | 257 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 258 | fun ppBracket' l r ppA a = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 259 | let | 
| 45778 | 260 | val n = sizeWord l | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 261 | in | 
| 45778 | 262 | inconsistentBlock n | 
| 263 | [ppWord l, | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 264 | ppA a, | 
| 45778 | 265 | ppWord r] | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 266 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 267 | |
| 45778 | 268 | fun ppOp' w = sequence (ppWord w) break; | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 269 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 270 | fun ppOp2' ab ppA ppB (a,b) = | 
| 45778 | 271 | inconsistentBlock 0 | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 272 | [ppA a, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 273 | ppOp' ab, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 274 | ppB b]; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 275 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 276 | fun ppOp3' ab bc ppA ppB ppC (a,b,c) = | 
| 45778 | 277 | inconsistentBlock 0 | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 278 | [ppA a, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 279 | ppOp' ab, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 280 | ppB b, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 281 | ppOp' bc, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 282 | ppC c]; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 283 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 284 | fun ppOpList' s ppA = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 285 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 286 | fun ppOpA a = sequence (ppOp' s) (ppA a) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 287 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 288 | fn [] => skip | 
| 45778 | 289 | | h :: t => inconsistentBlock 0 (ppA h :: List.map ppOpA t) | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 290 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 291 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 292 | fun ppOpStream' s ppA = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 293 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 294 | fun ppOpA a = sequence (ppOp' s) (ppA a) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 295 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 296 | fn Stream.Nil => skip | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 297 | | Stream.Cons (h,t) => | 
| 45778 | 298 | inconsistentBlock 0 | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 299 | [ppA h, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 300 | Stream.concat (Stream.map ppOpA (t ()))] | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 301 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 302 | |
| 45778 | 303 | fun ppBracket l r = ppBracket' (mkWord l) (mkWord r); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 304 | |
| 45778 | 305 | fun ppOp s = ppOp' (mkWord s); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 306 | |
| 45778 | 307 | fun ppOp2 ab = ppOp2' (mkWord ab); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 308 | |
| 45778 | 309 | fun ppOp3 ab bc = ppOp3' (mkWord ab) (mkWord bc); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 310 | |
| 45778 | 311 | fun ppOpList s = ppOpList' (mkWord s); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 312 | |
| 45778 | 313 | fun ppOpStream s = ppOpStream' (mkWord s); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 314 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 315 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 316 | (* Pretty-printers for common types. *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 317 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 318 | |
| 45778 | 319 | fun ppChar c = ppWord (charWord c); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 320 | |
| 45778 | 321 | fun ppString s = ppWord (mkWord s); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 322 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 323 | fun ppEscapeString escape = ppMap (escapeString escape) ppString; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 324 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 325 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 326 | val pp = ppString "()"; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 327 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 328 | fun ppUnit () = pp; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 329 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 330 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 331 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 332 | val ppTrue = ppString "true" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 333 | and ppFalse = ppString "false"; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 334 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 335 | 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: 
39349diff
changeset | 336 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 337 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 338 | val ppInt = ppMap Int.toString ppString; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 339 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 340 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 341 | val ppNeg = ppString "~" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 342 | and ppSep = ppString "," | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 343 | and ppZero = ppString "0" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 344 | and ppZeroZero = ppString "00"; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 345 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 346 | fun ppIntBlock i = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 347 | if i < 10 then sequence ppZeroZero (ppInt i) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 348 | 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: 
39349diff
changeset | 349 | else ppInt i; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 350 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 351 | fun ppIntBlocks i = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 352 | if i < 1000 then ppInt i | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 353 | else sequence (ppIntBlocks (i div 1000)) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 354 | (sequence ppSep (ppIntBlock (i mod 1000))); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 355 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 356 | fun ppPrettyInt i = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 357 | if i < 0 then sequence ppNeg (ppIntBlocks (~i)) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 358 | else ppIntBlocks i; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 359 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 360 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 361 | val ppReal = ppMap Real.toString ppString; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 362 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 363 | val ppPercent = ppMap percentToString ppString; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 364 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 365 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 366 | val ppLess = ppString "Less" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 367 | and ppEqual = ppString "Equal" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 368 | and ppGreater = ppString "Greater"; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 369 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 370 | fun ppOrder ord = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 371 | case ord of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 372 | LESS => ppLess | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 373 | | EQUAL => ppEqual | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 374 | | GREATER => ppGreater; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 375 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 376 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 377 | local | 
| 45778 | 378 | val left = mkWord "[" | 
| 379 | and right = mkWord "]" | |
| 380 | and sep = mkWord ","; | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 381 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 382 | 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: 
39349diff
changeset | 383 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 384 | 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: 
39349diff
changeset | 385 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 386 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 387 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 388 | val ppNone = ppString "-"; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 389 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 390 | fun ppOption ppX xo = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 391 | case xo of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 392 | SOME x => ppX x | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 393 | | NONE => ppNone; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 394 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 395 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 396 | local | 
| 45778 | 397 |   val left = mkWord "("
 | 
| 398 | and right = mkWord ")" | |
| 399 | and sep = mkWord ","; | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 400 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 401 | fun ppPair ppA ppB = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 402 | ppBracket' left right (ppOp2' sep ppA ppB); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 403 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 404 | fun ppTriple ppA ppB ppC = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 405 | 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: 
39349diff
changeset | 406 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 407 | |
| 45778 | 408 | fun ppException e = ppString (exnMessage e); | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 409 | |
| 45778 | 410 | (* ------------------------------------------------------------------------- *) | 
| 411 | (* A type of pretty-printers. *) | |
| 412 | (* ------------------------------------------------------------------------- *) | |
| 413 | ||
| 414 | local | |
| 415 | val ppStepType = ppMap toStringStep ppString; | |
| 416 | ||
| 417 | val ppStyle = ppMap toStringStyle ppString; | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 418 | |
| 45778 | 419 | val ppBlockInfo = | 
| 420 | let | |
| 421 | val sep = mkWord " " | |
| 422 | in | |
| 423 |         fn Block {style = s, indent = i} =>
 | |
| 424 | program [ppStyle s, ppWord sep, ppInt i] | |
| 425 | end; | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 426 | |
| 45778 | 427 | val ppWordInfo = | 
| 428 | let | |
| 429 | val left = mkWord "\"" | |
| 430 | and right = mkWord "\"" | |
| 431 |         and escape = {escape = [#"\""]}
 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 432 | |
| 45778 | 433 | val pp = ppBracket' left right (ppEscapeString escape) | 
| 434 | in | |
| 435 |         fn Word {word = s, size = n} =>
 | |
| 436 | if size s = n then pp s else ppPair pp ppInt (s,n) | |
| 437 | end; | |
| 438 | ||
| 439 | val ppBreakInfo = | |
| 440 | let | |
| 441 | val sep = mkWord "+" | |
| 442 | in | |
| 443 |         fn Break {size = n, extraIndent = k} =>
 | |
| 444 | if k = 0 then ppInt n else program [ppInt n, ppWord sep, ppInt k] | |
| 445 | end; | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 446 | |
| 45778 | 447 | fun ppStep step = | 
| 448 | inconsistentBlock 2 | |
| 449 | (ppStepType step :: | |
| 450 | (case step of | |
| 451 | BeginBlock b => | |
| 452 | [break, | |
| 453 | ppBlockInfo b] | |
| 454 | | EndBlock => [] | |
| 455 | | AddWord w => | |
| 456 | [break, | |
| 457 | ppWordInfo w] | |
| 458 | | AddBreak b => | |
| 459 | [break, | |
| 460 | ppBreakInfo b] | |
| 461 | | AddNewline => [])); | |
| 462 | in | |
| 463 | val ppPpstream = ppStream ppStep; | |
| 464 | end; | |
| 42102 | 465 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 466 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 467 | (* Pretty-printing infix operators. *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 468 | (* ------------------------------------------------------------------------- *) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 469 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 470 | type token = string; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 471 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 472 | datatype assoc = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 473 | LeftAssoc | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 474 | | NonAssoc | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 475 | | RightAssoc; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 476 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 477 | datatype infixes = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 478 | Infixes of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 479 |       {token : token,
 | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 480 | precedence : int, | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 481 | assoc : assoc} list; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 482 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 483 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 484 | fun comparePrecedence (io1,io2) = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 485 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 486 |         val {token = _, precedence = p1, assoc = _} = io1
 | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 487 |         and {token = _, precedence = p2, assoc = _} = io2
 | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 488 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 489 | Int.compare (p2,p1) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 490 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 491 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 492 | fun equalAssoc a a' = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 493 | case a of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 494 | LeftAssoc => (case a' of LeftAssoc => true | _ => false) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 495 | | NonAssoc => (case a' of NonAssoc => true | _ => false) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 496 | | RightAssoc => (case a' of RightAssoc => true | _ => false); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 497 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 498 |   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: 
39349diff
changeset | 499 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 500 | fun add t a' acc = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 501 | case acc of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 502 | [] => raise Bug "Print.layerInfixes: null" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 503 |       | {tokens = ts, assoc = a} :: acc =>
 | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 504 |         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: 
39349diff
changeset | 505 | else raise Bug "Print.layerInfixes: mixed assocs"; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 506 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 507 |   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: 
39349diff
changeset | 508 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 509 | 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: 
39349diff
changeset | 510 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 511 | (acc,p) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 512 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 513 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 514 | fun layerInfixes (Infixes ios) = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 515 | case sort comparePrecedence ios of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 516 | [] => [] | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 517 |       | {token = t, precedence = p, assoc = a} :: ios =>
 | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 518 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 519 | val acc = new t a [] | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 520 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 521 | val (acc,_) = List.foldl layer (acc,p) ios | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 522 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 523 | acc | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 524 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 525 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 526 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 527 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 528 |   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: 
39349diff
changeset | 529 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 530 | 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: 
39349diff
changeset | 531 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 532 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 533 | fun tokensInfixes ios = tokensLayeredInfixes (layerInfixes ios); | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 534 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 535 | fun destInfixOp dest tokens tm = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 536 | case dest tm of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 537 | NONE => NONE | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 538 | | 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: 
39349diff
changeset | 539 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 540 | fun ppLayeredInfixes dest ppTok {tokens,assoc} ppLower ppSub =
 | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 541 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 542 | fun isLayer t = StringSet.member t tokens | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 543 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 544 | fun ppTerm aligned (tm,r) = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 545 | case dest tm of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 546 | NONE => ppSub (tm,r) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 547 | | SOME (t,a,b) => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 548 | 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: 
39349diff
changeset | 549 | else ppLower (tm,t,a,b,r) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 550 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 551 | and ppLeft tm_r = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 552 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 553 | val aligned = case assoc of LeftAssoc => true | _ => false | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 554 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 555 | ppTerm aligned tm_r | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 556 | end | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 557 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 558 | and ppLayer (tm,t,a,b,r) = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 559 | program | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 560 | [ppLeft (a,true), | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 561 | ppTok (tm,t), | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 562 | ppRight (b,r)] | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 563 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 564 | and ppRight tm_r = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 565 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 566 | val aligned = case assoc of RightAssoc => true | _ => false | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 567 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 568 | ppTerm aligned tm_r | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 569 | end | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 570 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 571 | fn tm_t_a_b_r as (_,t,_,_,_) => | 
| 45778 | 572 | if isLayer t then inconsistentBlock 0 [ppLayer tm_t_a_b_r] | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 573 | else ppLower tm_t_a_b_r | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 574 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 575 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 576 | local | 
| 45778 | 577 |   val leftBrack = mkWord "("
 | 
| 578 | and rightBrack = mkWord ")"; | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 579 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 580 | fun ppInfixes ops = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 581 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 582 | val layers = layerInfixes ops | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 583 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 584 | val toks = tokensLayeredInfixes layers | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 585 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 586 | fn dest => fn ppTok => fn ppSub => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 587 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 588 | fun destOp tm = destInfixOp dest toks tm | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 589 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 590 | 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: 
39349diff
changeset | 591 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 592 | and ppLayers ls (tm,t,a,b,r) = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 593 | case ls of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 594 | [] => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 595 | 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: 
39349diff
changeset | 596 | | l :: ls => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 597 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 598 | val ppLower = ppLayers ls | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 599 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 600 | 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: 
39349diff
changeset | 601 | end | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 602 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 603 | fn (tm,r) => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 604 | case destOp tm of | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 605 | 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: 
39349diff
changeset | 606 | | NONE => ppSub (tm,r) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 607 | end | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 608 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 609 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 610 | |
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 611 | (* ------------------------------------------------------------------------- *) | 
| 45778 | 612 | (* A type of output lines. *) | 
| 613 | (* ------------------------------------------------------------------------- *) | |
| 614 | ||
| 615 | type line = {indent : int, line : string};
 | |
| 616 | ||
| 617 | val emptyLine = | |
| 618 | let | |
| 619 | val indent = 0 | |
| 620 | and line = "" | |
| 621 | in | |
| 622 |       {indent = indent,
 | |
| 623 | line = line} | |
| 624 | end; | |
| 625 | ||
| 626 | fun addEmptyLine lines = emptyLine :: lines; | |
| 627 | ||
| 628 | fun addLine lines indent line = {indent = indent, line = line} :: lines;
 | |
| 629 | ||
| 630 | (* ------------------------------------------------------------------------- *) | |
| 631 | (* Pretty-printer rendering. *) | |
| 39348 | 632 | (* ------------------------------------------------------------------------- *) | 
| 633 | ||
| 45778 | 634 | datatype chunk = | 
| 635 | WordChunk of word | |
| 636 | | BreakChunk of break | |
| 637 | | FrameChunk of frame | |
| 638 | ||
| 639 | and frame = | |
| 640 | Frame of | |
| 641 |       {block : block,
 | |
| 642 | broken : bool, | |
| 643 | indent : int, | |
| 644 | size : int, | |
| 645 | chunks : chunk list}; | |
| 646 | ||
| 647 | datatype state = | |
| 648 | State of | |
| 649 |       {lineIndent : int,
 | |
| 650 | lineSize : int, | |
| 651 | stack : frame list}; | |
| 652 | ||
| 653 | fun blockFrame (Frame {block = x, ...}) = x;
 | |
| 654 | ||
| 655 | fun brokenFrame (Frame {broken = x, ...}) = x;
 | |
| 656 | ||
| 657 | fun indentFrame (Frame {indent = x, ...}) = x;
 | |
| 658 | ||
| 659 | fun sizeFrame (Frame {size = x, ...}) = x;
 | |
| 660 | ||
| 661 | fun chunksFrame (Frame {chunks = x, ...}) = x;
 | |
| 662 | ||
| 663 | fun styleFrame frame = styleBlock (blockFrame frame); | |
| 664 | ||
| 665 | fun isConsistentFrame frame = isConsistentBlock (blockFrame frame); | |
| 666 | ||
| 667 | fun breakingFrame frame = isConsistentFrame frame andalso brokenFrame frame; | |
| 668 | ||
| 669 | fun sizeChunk chunk = | |
| 670 | case chunk of | |
| 671 | WordChunk w => sizeWord w | |
| 672 | | BreakChunk b => sizeBreak b | |
| 673 | | FrameChunk f => sizeFrame f; | |
| 674 | ||
| 675 | local | |
| 676 | fun add (c,acc) = sizeChunk c + acc; | |
| 677 | in | |
| 678 | fun sizeChunks cs = List.foldl add 0 cs; | |
| 679 | end; | |
| 680 | ||
| 681 | local | |
| 682 | fun flattenChunks acc chunks = | |
| 683 | case chunks of | |
| 684 | [] => acc | |
| 685 | | chunk :: chunks => flattenChunk acc chunk chunks | |
| 686 | ||
| 687 | and flattenChunk acc chunk chunks = | |
| 688 | case chunk of | |
| 689 | WordChunk w => flattenChunks (renderWord w :: acc) chunks | |
| 690 | | BreakChunk b => flattenChunks (renderBreak b :: acc) chunks | |
| 691 | | FrameChunk f => flattenFrame acc f chunks | |
| 692 | ||
| 693 | and flattenFrame acc frame chunks = | |
| 694 | flattenChunks acc (chunksFrame frame @ chunks); | |
| 695 | in | |
| 696 | fun renderChunks chunks = String.concat (flattenChunks [] chunks); | |
| 697 | end; | |
| 698 | ||
| 699 | fun addChunksLine lines indent chunks = | |
| 700 | addLine lines indent (renderChunks chunks); | |
| 701 | ||
| 702 | local | |
| 703 | fun add baseIndent ((extraIndent,chunks),lines) = | |
| 704 | addChunksLine lines (baseIndent + extraIndent) chunks; | |
| 705 | in | |
| 706 | fun addIndentChunksLines lines baseIndent indent_chunks = | |
| 707 | List.foldl (add baseIndent) lines indent_chunks; | |
| 708 | end; | |
| 709 | ||
| 710 | fun isEmptyFrame frame = | |
| 711 | let | |
| 712 | val chunks = chunksFrame frame | |
| 713 | ||
| 714 | val empty = List.null chunks | |
| 715 | ||
| 716 | (*BasicDebug | |
| 717 | val () = | |
| 718 | if not empty orelse sizeFrame frame = 0 then () | |
| 719 | else raise Bug "Print.isEmptyFrame: bad size" | |
| 720 | *) | |
| 721 | in | |
| 722 | empty | |
| 723 | end; | |
| 724 | ||
| 725 | local | |
| 726 | fun breakInconsistent blockIndent = | |
| 727 | let | |
| 728 | fun break chunks = | |
| 729 | case chunks of | |
| 730 | [] => NONE | |
| 731 | | chunk :: chunks => | |
| 732 | case chunk of | |
| 733 | BreakChunk b => | |
| 734 | let | |
| 735 | val pre = chunks | |
| 736 | and indent = blockIndent + extraIndentBreak b | |
| 737 | and post = [] | |
| 738 | in | |
| 739 | SOME (pre,indent,post) | |
| 740 | end | |
| 741 | | _ => | |
| 742 | case break chunks of | |
| 743 | SOME (pre,indent,post) => | |
| 744 | let | |
| 745 | val post = chunk :: post | |
| 746 | in | |
| 747 | SOME (pre,indent,post) | |
| 748 | end | |
| 749 | | NONE => NONE | |
| 750 | in | |
| 751 | break | |
| 752 | end; | |
| 753 | ||
| 754 | fun breakConsistent blockIndent = | |
| 755 | let | |
| 756 | fun break indent_chunks chunks = | |
| 757 | case breakInconsistent blockIndent chunks of | |
| 758 | NONE => (chunks,indent_chunks) | |
| 759 | | SOME (pre,indent,post) => | |
| 760 | break ((indent,post) :: indent_chunks) pre | |
| 761 | in | |
| 762 | break [] | |
| 763 | end; | |
| 764 | in | |
| 765 | fun breakFrame frame = | |
| 766 | let | |
| 767 | val Frame | |
| 768 |               {block,
 | |
| 769 | broken = _, | |
| 770 | indent = _, | |
| 771 | size = _, | |
| 772 | chunks} = frame | |
| 773 | ||
| 774 | val blockIndent = indentBlock block | |
| 775 | in | |
| 776 | case breakInconsistent blockIndent chunks of | |
| 777 | NONE => NONE | |
| 778 | | SOME (pre,indent,post) => | |
| 779 | let | |
| 780 | val broken = true | |
| 781 | and size = sizeChunks post | |
| 782 | ||
| 783 | val frame = | |
| 784 | Frame | |
| 785 |                   {block = block,
 | |
| 786 | broken = broken, | |
| 787 | indent = indent, | |
| 788 | size = size, | |
| 789 | chunks = post} | |
| 790 | in | |
| 791 | case styleBlock block of | |
| 792 | Inconsistent => | |
| 793 | let | |
| 794 | val indent_chunks = [] | |
| 795 | in | |
| 796 | SOME (pre,indent_chunks,frame) | |
| 797 | end | |
| 798 | | Consistent => | |
| 799 | let | |
| 800 | val (pre,indent_chunks) = breakConsistent blockIndent pre | |
| 801 | in | |
| 802 | SOME (pre,indent_chunks,frame) | |
| 803 | end | |
| 804 | end | |
| 805 | end; | |
| 806 | end; | |
| 807 | ||
| 808 | fun removeChunksFrame frame = | |
| 809 | let | |
| 810 | val Frame | |
| 811 |             {block,
 | |
| 812 | broken, | |
| 813 | indent, | |
| 814 | size = _, | |
| 815 | chunks} = frame | |
| 816 | in | |
| 817 | if broken andalso List.null chunks then NONE | |
| 818 | else | |
| 819 | let | |
| 820 | val frame = | |
| 821 | Frame | |
| 822 |                 {block = block,
 | |
| 823 | broken = true, | |
| 824 | indent = indent, | |
| 825 | size = 0, | |
| 826 | chunks = []} | |
| 827 | in | |
| 828 | SOME (chunks,frame) | |
| 829 | end | |
| 830 | end; | |
| 831 | ||
| 832 | val removeChunksFrames = | |
| 833 | let | |
| 834 | fun remove frames = | |
| 835 | case frames of | |
| 836 | [] => | |
| 837 | let | |
| 838 | val chunks = [] | |
| 839 | and frames = NONE | |
| 840 | and indent = 0 | |
| 841 | in | |
| 842 | (chunks,frames,indent) | |
| 843 | end | |
| 844 | | top :: rest => | |
| 845 | let | |
| 846 | val (chunks,rest',indent) = remove rest | |
| 847 | ||
| 848 | val indent = indent + indentFrame top | |
| 849 | ||
| 850 | val (chunks,top') = | |
| 851 | case removeChunksFrame top of | |
| 852 | NONE => (chunks,NONE) | |
| 853 | | SOME (topChunks,top) => (topChunks @ chunks, SOME top) | |
| 854 | ||
| 855 | val frames' = | |
| 856 | case (top',rest') of | |
| 857 | (NONE,NONE) => NONE | |
| 858 | | (SOME top, NONE) => SOME (top :: rest) | |
| 859 | | (NONE, SOME rest) => SOME (top :: rest) | |
| 860 | | (SOME top, SOME rest) => SOME (top :: rest) | |
| 861 | in | |
| 862 | (chunks,frames',indent) | |
| 863 | end | |
| 864 | in | |
| 865 | fn frames => | |
| 866 | let | |
| 867 | val (chunks,frames',indent) = remove frames | |
| 868 | ||
| 869 | val frames = Option.getOpt (frames',frames) | |
| 870 | in | |
| 871 | (chunks,frames,indent) | |
| 872 | end | |
| 873 | end; | |
| 874 | ||
| 875 | local | |
| 876 | fun breakUp lines lineIndent frames = | |
| 877 | case frames of | |
| 878 | [] => NONE | |
| 879 | | frame :: frames => | |
| 880 | case breakUp lines lineIndent frames of | |
| 881 | SOME (lines_indent,lineSize,frames) => | |
| 882 | let | |
| 883 | val lineSize = lineSize + sizeFrame frame | |
| 884 | and frames = frame :: frames | |
| 885 | in | |
| 886 | SOME (lines_indent,lineSize,frames) | |
| 887 | end | |
| 888 | | NONE => | |
| 889 | case breakFrame frame of | |
| 890 | NONE => NONE | |
| 891 | | SOME (frameChunks,indent_chunks,frame) => | |
| 892 | let | |
| 893 | val (chunks,frames,indent) = removeChunksFrames frames | |
| 894 | ||
| 895 | val chunks = frameChunks @ chunks | |
| 896 | ||
| 897 | val lines = addChunksLine lines lineIndent chunks | |
| 898 | ||
| 899 | val lines = addIndentChunksLines lines indent indent_chunks | |
| 900 | ||
| 901 | val lineIndent = indent + indentFrame frame | |
| 902 | ||
| 903 | val lineSize = sizeFrame frame | |
| 904 | ||
| 905 | val frames = frame :: frames | |
| 906 | in | |
| 907 | SOME ((lines,lineIndent),lineSize,frames) | |
| 908 | end; | |
| 909 | ||
| 910 | fun breakInsideChunk chunk = | |
| 911 | case chunk of | |
| 912 | WordChunk _ => NONE | |
| 913 | | BreakChunk _ => raise Bug "Print.breakInsideChunk" | |
| 914 | | FrameChunk frame => | |
| 915 | case breakFrame frame of | |
| 916 | SOME (pathChunks,indent_chunks,frame) => | |
| 917 | let | |
| 918 | val pathIndent = 0 | |
| 919 | and breakIndent = indentFrame frame | |
| 920 | in | |
| 921 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,frame) | |
| 922 | end | |
| 923 | | NONE => breakInsideFrame frame | |
| 924 | ||
| 925 | and breakInsideChunks chunks = | |
| 926 | case chunks of | |
| 927 | [] => NONE | |
| 928 | | chunk :: chunks => | |
| 929 | case breakInsideChunk chunk of | |
| 930 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,frame) => | |
| 931 | let | |
| 932 | val pathChunks = pathChunks @ chunks | |
| 933 | and chunks = [FrameChunk frame] | |
| 934 | in | |
| 935 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,chunks) | |
| 936 | end | |
| 937 | | NONE => | |
| 938 | case breakInsideChunks chunks of | |
| 939 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,chunks) => | |
| 940 | let | |
| 941 | val chunks = chunk :: chunks | |
| 942 | in | |
| 943 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,chunks) | |
| 944 | end | |
| 945 | | NONE => NONE | |
| 946 | ||
| 947 | and breakInsideFrame frame = | |
| 948 | let | |
| 949 | val Frame | |
| 950 |               {block,
 | |
| 951 | broken = _, | |
| 952 | indent, | |
| 953 | size = _, | |
| 954 | chunks} = frame | |
| 955 | in | |
| 956 | case breakInsideChunks chunks of | |
| 957 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,chunks) => | |
| 958 | let | |
| 959 | val pathIndent = pathIndent + indent | |
| 960 | and broken = true | |
| 961 | and size = sizeChunks chunks | |
| 962 | ||
| 963 | val frame = | |
| 964 | Frame | |
| 965 |                   {block = block,
 | |
| 966 | broken = broken, | |
| 967 | indent = indent, | |
| 968 | size = size, | |
| 969 | chunks = chunks} | |
| 970 | in | |
| 971 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,frame) | |
| 972 | end | |
| 973 | | NONE => NONE | |
| 974 | end; | |
| 975 | ||
| 976 | fun breakInside lines lineIndent frames = | |
| 977 | case frames of | |
| 978 | [] => NONE | |
| 979 | | frame :: frames => | |
| 980 | case breakInsideFrame frame of | |
| 981 | SOME (pathChunks,pathIndent,indent_chunks,breakIndent,frame) => | |
| 982 | let | |
| 983 | val (chunks,frames,indent) = removeChunksFrames frames | |
| 984 | ||
| 985 | val chunks = pathChunks @ chunks | |
| 986 | and indent = indent + pathIndent | |
| 987 | ||
| 988 | val lines = addChunksLine lines lineIndent chunks | |
| 989 | ||
| 990 | val lines = addIndentChunksLines lines indent indent_chunks | |
| 991 | ||
| 992 | val lineIndent = indent + breakIndent | |
| 993 | ||
| 994 | val lineSize = sizeFrame frame | |
| 995 | ||
| 996 | val frames = frame :: frames | |
| 997 | in | |
| 998 | SOME ((lines,lineIndent),lineSize,frames) | |
| 999 | end | |
| 1000 | | NONE => | |
| 1001 | case breakInside lines lineIndent frames of | |
| 1002 | SOME (lines_indent,lineSize,frames) => | |
| 1003 | let | |
| 1004 | val lineSize = lineSize + sizeFrame frame | |
| 1005 | and frames = frame :: frames | |
| 1006 | in | |
| 1007 | SOME (lines_indent,lineSize,frames) | |
| 1008 | end | |
| 1009 | | NONE => NONE; | |
| 1010 | in | |
| 1011 | fun breakFrames lines lineIndent frames = | |
| 1012 | case breakUp lines lineIndent frames of | |
| 1013 | SOME ((lines,lineIndent),lineSize,frames) => | |
| 1014 | SOME (lines,lineIndent,lineSize,frames) | |
| 1015 | | NONE => | |
| 1016 | case breakInside lines lineIndent frames of | |
| 1017 | SOME ((lines,lineIndent),lineSize,frames) => | |
| 1018 | SOME (lines,lineIndent,lineSize,frames) | |
| 1019 | | NONE => NONE; | |
| 1020 | end; | |
| 1021 | ||
| 1022 | (*BasicDebug | |
| 1023 | fun checkChunk chunk = | |
| 1024 | case chunk of | |
| 1025 | WordChunk t => (false, sizeWord t) | |
| 1026 | | BreakChunk b => (false, sizeBreak b) | |
| 1027 | | FrameChunk b => checkFrame b | |
| 1028 | ||
| 1029 | and checkChunks chunks = | |
| 1030 | case chunks of | |
| 1031 | [] => (false,0) | |
| 1032 | | chunk :: chunks => | |
| 1033 | let | |
| 1034 | val (bk,sz) = checkChunk chunk | |
| 1035 | ||
| 1036 | val (bk',sz') = checkChunks chunks | |
| 1037 | in | |
| 1038 | (bk orelse bk', sz + sz') | |
| 1039 | end | |
| 1040 | ||
| 1041 | and checkFrame frame = | |
| 1042 | let | |
| 1043 | val Frame | |
| 1044 |             {block = _,
 | |
| 1045 | broken, | |
| 1046 | indent = _, | |
| 1047 | size, | |
| 1048 | chunks} = frame | |
| 1049 | ||
| 1050 | val (bk,sz) = checkChunks chunks | |
| 1051 | ||
| 1052 | val () = | |
| 1053 | if size = sz then () | |
| 1054 | else raise Bug "Print.checkFrame: wrong size" | |
| 1055 | ||
| 1056 | val () = | |
| 1057 | if broken orelse not bk then () | |
| 1058 | else raise Bug "Print.checkFrame: deep broken frame" | |
| 1059 | in | |
| 1060 | (broken,size) | |
| 1061 | end; | |
| 1062 | ||
| 1063 | fun checkFrames frames = | |
| 1064 | case frames of | |
| 1065 | [] => (true,0) | |
| 1066 | | frame :: frames => | |
| 1067 | let | |
| 1068 | val (bk,sz) = checkFrame frame | |
| 1069 | ||
| 1070 | val (bk',sz') = checkFrames frames | |
| 1071 | ||
| 1072 | val () = | |
| 1073 | if bk' orelse not bk then () | |
| 1074 | else raise Bug "Print.checkFrame: broken stack frame" | |
| 1075 | in | |
| 1076 | (bk, sz + sz') | |
| 1077 | end; | |
| 1078 | *) | |
| 1079 | ||
| 1080 | (*BasicDebug | |
| 1081 | fun checkState state = | |
| 1082 | (let | |
| 1083 |        val State {lineIndent,lineSize,stack} = state
 | |
| 1084 | ||
| 1085 | val () = | |
| 1086 | if not (List.null stack) then () | |
| 1087 | else raise Error "no stack" | |
| 1088 | ||
| 1089 | val (_,sz) = checkFrames stack | |
| 1090 | ||
| 1091 | val () = | |
| 1092 | if lineSize = sz then () | |
| 1093 | else raise Error "wrong lineSize" | |
| 1094 | in | |
| 1095 | () | |
| 1096 | end | |
| 1097 | handle Error err => raise Bug err) | |
| 1098 |     handle Bug bug => raise Bug ("Print.checkState: " ^ bug);
 | |
| 1099 | *) | |
| 1100 | ||
| 1101 | fun isEmptyState state = | |
| 1102 | let | |
| 1103 |       val State {lineSize,stack,...} = state
 | |
| 1104 | in | |
| 1105 | lineSize = 0 andalso List.all isEmptyFrame stack | |
| 1106 | end; | |
| 1107 | ||
| 1108 | fun isFinalState state = | |
| 1109 | let | |
| 1110 |       val State {stack,...} = state
 | |
| 1111 | in | |
| 1112 | case stack of | |
| 1113 | [] => raise Bug "Print.isFinalState: empty stack" | |
| 1114 | | [frame] => isEmptyFrame frame | |
| 1115 | | _ :: _ :: _ => false | |
| 1116 | end; | |
| 1117 | ||
| 1118 | local | |
| 1119 | val initialBlock = | |
| 1120 | let | |
| 1121 | val indent = 0 | |
| 1122 | and style = Inconsistent | |
| 1123 | in | |
| 1124 | Block | |
| 1125 |           {indent = indent,
 | |
| 1126 | style = style} | |
| 1127 | end; | |
| 1128 | ||
| 1129 | val initialFrame = | |
| 1130 | let | |
| 1131 | val block = initialBlock | |
| 1132 | and broken = false | |
| 1133 | and indent = 0 | |
| 1134 | and size = 0 | |
| 1135 | and chunks = [] | |
| 1136 | in | |
| 1137 | Frame | |
| 1138 |           {block = block,
 | |
| 1139 | broken = broken, | |
| 1140 | indent = indent, | |
| 1141 | size = size, | |
| 1142 | chunks = chunks} | |
| 1143 | end; | |
| 1144 | in | |
| 1145 | val initialState = | |
| 1146 | let | |
| 1147 | val lineIndent = 0 | |
| 1148 | and lineSize = 0 | |
| 1149 | and stack = [initialFrame] | |
| 1150 | in | |
| 1151 | State | |
| 1152 |           {lineIndent = lineIndent,
 | |
| 1153 | lineSize = lineSize, | |
| 1154 | stack = stack} | |
| 1155 | end; | |
| 1156 | end; | |
| 1157 | ||
| 1158 | fun normalizeState lineLength lines state = | |
| 1159 | let | |
| 1160 |       val State {lineIndent,lineSize,stack} = state
 | |
| 1161 | ||
| 1162 | val within = | |
| 1163 | case lineLength of | |
| 1164 | NONE => true | |
| 1165 | | SOME len => lineIndent + lineSize <= len | |
| 1166 | in | |
| 1167 | if within then (lines,state) | |
| 1168 | else | |
| 1169 | case breakFrames lines lineIndent stack of | |
| 1170 | NONE => (lines,state) | |
| 1171 | | SOME (lines,lineIndent,lineSize,stack) => | |
| 1172 | let | |
| 1173 | (*BasicDebug | |
| 1174 | val () = checkState state | |
| 1175 | *) | |
| 1176 | val state = | |
| 1177 | State | |
| 1178 |                   {lineIndent = lineIndent,
 | |
| 1179 | lineSize = lineSize, | |
| 1180 | stack = stack} | |
| 1181 | in | |
| 1182 | normalizeState lineLength lines state | |
| 1183 | end | |
| 1184 | end | |
| 1185 | (*BasicDebug | |
| 1186 |     handle Bug bug => raise Bug ("Print.normalizeState:\n" ^ bug)
 | |
| 1187 | *) | |
| 1188 | ||
| 1189 | local | |
| 1190 | fun executeBeginBlock block lines state = | |
| 1191 | let | |
| 1192 |         val State {lineIndent,lineSize,stack} = state
 | |
| 1193 | ||
| 1194 | val broken = false | |
| 1195 | and indent = indentBlock block | |
| 1196 | and size = 0 | |
| 1197 | and chunks = [] | |
| 1198 | ||
| 1199 | val frame = | |
| 1200 | Frame | |
| 1201 |               {block = block,
 | |
| 1202 | broken = broken, | |
| 1203 | indent = indent, | |
| 1204 | size = size, | |
| 1205 | chunks = chunks} | |
| 1206 | ||
| 1207 | val stack = frame :: stack | |
| 1208 | ||
| 1209 | val state = | |
| 1210 | State | |
| 1211 |               {lineIndent = lineIndent,
 | |
| 1212 | lineSize = lineSize, | |
| 1213 | stack = stack} | |
| 1214 | in | |
| 1215 | (lines,state) | |
| 1216 | end; | |
| 1217 | ||
| 1218 | fun executeEndBlock lines state = | |
| 1219 | let | |
| 1220 |         val State {lineIndent,lineSize,stack} = state
 | |
| 1221 | ||
| 1222 | val (lineSize,stack) = | |
| 1223 | case stack of | |
| 1224 | [] => raise Bug "Print.executeEndBlock: empty stack" | |
| 1225 | | topFrame :: stack => | |
| 1226 | let | |
| 1227 | val Frame | |
| 1228 |                       {block = topBlock,
 | |
| 1229 | broken = topBroken, | |
| 1230 | indent = topIndent, | |
| 1231 | size = topSize, | |
| 1232 | chunks = topChunks} = topFrame | |
| 1233 | ||
| 1234 | val (lineSize,topSize,topChunks,topFrame) = | |
| 1235 | case topChunks of | |
| 1236 | BreakChunk break :: chunks => | |
| 1237 | let | |
| 1238 | (*BasicDebug | |
| 1239 | val () = | |
| 1240 | let | |
| 1241 | val mesg = | |
| 1242 | "ignoring a break at the end of a " ^ | |
| 1243 | "pretty-printing block" | |
| 1244 | in | |
| 1245 | warn mesg | |
| 1246 | end | |
| 1247 | *) | |
| 1248 | val n = sizeBreak break | |
| 1249 | ||
| 1250 | val lineSize = lineSize - n | |
| 1251 | and topSize = topSize - n | |
| 1252 | and topChunks = chunks | |
| 1253 | ||
| 1254 | val topFrame = | |
| 1255 | Frame | |
| 1256 |                               {block = topBlock,
 | |
| 1257 | broken = topBroken, | |
| 1258 | indent = topIndent, | |
| 1259 | size = topSize, | |
| 1260 | chunks = topChunks} | |
| 1261 | in | |
| 1262 | (lineSize,topSize,topChunks,topFrame) | |
| 1263 | end | |
| 1264 | | _ => (lineSize,topSize,topChunks,topFrame) | |
| 1265 | in | |
| 1266 | if List.null topChunks then (lineSize,stack) | |
| 1267 | else | |
| 1268 | case stack of | |
| 1269 | [] => raise Error "Print.execute: too many end blocks" | |
| 1270 | | frame :: stack => | |
| 1271 | let | |
| 1272 | val Frame | |
| 1273 |                             {block,
 | |
| 1274 | broken, | |
| 1275 | indent, | |
| 1276 | size, | |
| 1277 | chunks} = frame | |
| 1278 | ||
| 1279 | val size = size + topSize | |
| 1280 | ||
| 1281 | val chunk = FrameChunk topFrame | |
| 1282 | ||
| 1283 | val chunks = chunk :: chunks | |
| 1284 | ||
| 1285 | val frame = | |
| 1286 | Frame | |
| 1287 |                             {block = block,
 | |
| 1288 | broken = broken, | |
| 1289 | indent = indent, | |
| 1290 | size = size, | |
| 1291 | chunks = chunks} | |
| 1292 | ||
| 1293 | val stack = frame :: stack | |
| 1294 | in | |
| 1295 | (lineSize,stack) | |
| 1296 | end | |
| 1297 | end | |
| 1298 | ||
| 1299 | val state = | |
| 1300 | State | |
| 1301 |               {lineIndent = lineIndent,
 | |
| 1302 | lineSize = lineSize, | |
| 1303 | stack = stack} | |
| 1304 | in | |
| 1305 | (lines,state) | |
| 1306 | end; | |
| 1307 | ||
| 1308 | fun executeAddWord lineLength word lines state = | |
| 1309 | let | |
| 1310 |         val State {lineIndent,lineSize,stack} = state
 | |
| 1311 | ||
| 1312 | val n = sizeWord word | |
| 1313 | ||
| 1314 | val lineSize = lineSize + n | |
| 1315 | ||
| 1316 | val stack = | |
| 1317 | case stack of | |
| 1318 | [] => raise Bug "Print.executeAddWord: empty stack" | |
| 1319 | | frame :: stack => | |
| 1320 | let | |
| 1321 | val Frame | |
| 1322 |                       {block,
 | |
| 1323 | broken, | |
| 1324 | indent, | |
| 1325 | size, | |
| 1326 | chunks} = frame | |
| 1327 | ||
| 1328 | val size = size + n | |
| 1329 | ||
| 1330 | val chunk = WordChunk word | |
| 1331 | ||
| 1332 | val chunks = chunk :: chunks | |
| 1333 | ||
| 1334 | val frame = | |
| 1335 | Frame | |
| 1336 |                       {block = block,
 | |
| 1337 | broken = broken, | |
| 1338 | indent = indent, | |
| 1339 | size = size, | |
| 1340 | chunks = chunks} | |
| 1341 | ||
| 1342 | val stack = frame :: stack | |
| 1343 | in | |
| 1344 | stack | |
| 1345 | end | |
| 1346 | ||
| 1347 | val state = | |
| 1348 | State | |
| 1349 |               {lineIndent = lineIndent,
 | |
| 1350 | lineSize = lineSize, | |
| 1351 | stack = stack} | |
| 1352 | in | |
| 1353 | normalizeState lineLength lines state | |
| 1354 | end; | |
| 1355 | ||
| 1356 | fun executeAddBreak lineLength break lines state = | |
| 1357 | let | |
| 1358 |         val State {lineIndent,lineSize,stack} = state
 | |
| 1359 | ||
| 1360 | val (topFrame,restFrames) = | |
| 1361 | case stack of | |
| 1362 | [] => raise Bug "Print.executeAddBreak: empty stack" | |
| 1363 | | topFrame :: restFrames => (topFrame,restFrames) | |
| 1364 | ||
| 1365 | val Frame | |
| 1366 |               {block = topBlock,
 | |
| 1367 | broken = topBroken, | |
| 1368 | indent = topIndent, | |
| 1369 | size = topSize, | |
| 1370 | chunks = topChunks} = topFrame | |
| 1371 | in | |
| 1372 | case topChunks of | |
| 1373 | [] => (lines,state) | |
| 1374 | | topChunk :: restTopChunks => | |
| 1375 | let | |
| 1376 | val (topChunks,n) = | |
| 1377 | case topChunk of | |
| 1378 | BreakChunk break' => | |
| 1379 | let | |
| 1380 | val break = appendBreak break' break | |
| 1381 | ||
| 1382 | val chunk = BreakChunk break | |
| 1383 | ||
| 1384 | val topChunks = chunk :: restTopChunks | |
| 1385 | and n = sizeBreak break - sizeBreak break' | |
| 1386 | in | |
| 1387 | (topChunks,n) | |
| 1388 | end | |
| 1389 | | _ => | |
| 1390 | let | |
| 1391 | val chunk = BreakChunk break | |
| 1392 | ||
| 1393 | val topChunks = chunk :: topChunks | |
| 1394 | and n = sizeBreak break | |
| 1395 | in | |
| 1396 | (topChunks,n) | |
| 1397 | end | |
| 1398 | ||
| 1399 | val lineSize = lineSize + n | |
| 1400 | ||
| 1401 | val topSize = topSize + n | |
| 1402 | ||
| 1403 | val topFrame = | |
| 1404 | Frame | |
| 1405 |                   {block = topBlock,
 | |
| 1406 | broken = topBroken, | |
| 1407 | indent = topIndent, | |
| 1408 | size = topSize, | |
| 1409 | chunks = topChunks} | |
| 1410 | ||
| 1411 | val stack = topFrame :: restFrames | |
| 1412 | ||
| 1413 | val state = | |
| 1414 | State | |
| 1415 |                   {lineIndent = lineIndent,
 | |
| 1416 | lineSize = lineSize, | |
| 1417 | stack = stack} | |
| 1418 | ||
| 1419 | val lineLength = | |
| 1420 | if breakingFrame topFrame then SOME ~1 else lineLength | |
| 1421 | in | |
| 1422 | normalizeState lineLength lines state | |
| 1423 | end | |
| 1424 | end; | |
| 1425 | ||
| 1426 | fun executeBigBreak lines state = | |
| 1427 | let | |
| 1428 | val lineLength = SOME ~1 | |
| 1429 | and break = mkBreak 0 | |
| 1430 | in | |
| 1431 | executeAddBreak lineLength break lines state | |
| 1432 | end; | |
| 1433 | ||
| 1434 | fun executeAddNewline lineLength lines state = | |
| 1435 | if isEmptyState state then (addEmptyLine lines, state) | |
| 1436 | else executeBigBreak lines state; | |
| 1437 | ||
| 1438 | fun executeEof lineLength lines state = | |
| 1439 | if isFinalState state then (lines,state) | |
| 1440 | else | |
| 1441 | let | |
| 1442 | val (lines,state) = executeBigBreak lines state | |
| 1443 | ||
| 1444 | (*BasicDebug | |
| 1445 | val () = | |
| 1446 | if isFinalState state then () | |
| 1447 | else raise Bug "Print.executeEof: not a final state" | |
| 1448 | *) | |
| 1449 | in | |
| 1450 | (lines,state) | |
| 1451 | end; | |
| 1452 | in | |
| 1453 |   fun render {lineLength} =
 | |
| 1454 | let | |
| 1455 | fun execute step state = | |
| 1456 | let | |
| 1457 | val lines = [] | |
| 1458 | in | |
| 1459 | case step of | |
| 1460 | BeginBlock block => executeBeginBlock block lines state | |
| 1461 | | EndBlock => executeEndBlock lines state | |
| 1462 | | AddWord word => executeAddWord lineLength word lines state | |
| 1463 | | AddBreak break => executeAddBreak lineLength break lines state | |
| 1464 | | AddNewline => executeAddNewline lineLength lines state | |
| 1465 | end | |
| 1466 | ||
| 1467 | (*BasicDebug | |
| 1468 | val execute = fn step => fn state => | |
| 1469 | let | |
| 1470 | val (lines,state) = execute step state | |
| 1471 | ||
| 1472 | val () = checkState state | |
| 1473 | in | |
| 1474 | (lines,state) | |
| 1475 | end | |
| 1476 | handle Bug bug => | |
| 1477 |               raise Bug ("Print.execute: after " ^ toStringStep step ^
 | |
| 1478 | " command:\n" ^ bug) | |
| 1479 | *) | |
| 1480 | ||
| 1481 | fun final state = | |
| 1482 | let | |
| 1483 | val lines = [] | |
| 1484 | ||
| 1485 | val (lines,state) = executeEof lineLength lines state | |
| 1486 | ||
| 1487 | (*BasicDebug | |
| 1488 | val () = checkState state | |
| 1489 | *) | |
| 1490 | in | |
| 1491 | if List.null lines then Stream.Nil else Stream.singleton lines | |
| 1492 | end | |
| 1493 | (*BasicDebug | |
| 1494 |             handle Bug bug => raise Bug ("Print.final: " ^ bug)
 | |
| 1495 | *) | |
| 1496 | in | |
| 1497 | fn pps => | |
| 1498 | let | |
| 1499 | val lines = Stream.maps execute final initialState pps | |
| 1500 | in | |
| 1501 | revConcat lines | |
| 1502 | end | |
| 1503 | end; | |
| 1504 | end; | |
| 39348 | 1505 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1506 | local | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1507 |   fun inc {indent,line} acc = line :: nSpaces indent :: acc;
 | 
| 39348 | 1508 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1509 |   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: 
39349diff
changeset | 1510 | in | 
| 45778 | 1511 | fun toStringWithLineLength len ppA a = | 
| 1512 | case render len (ppA a) of | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1513 | Stream.Nil => "" | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1514 | | Stream.Cons (h,t) => | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1515 | let | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1516 | val lines = Stream.foldl incn (inc h []) (t ()) | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1517 | in | 
| 45778 | 1518 | String.concat (List.rev lines) | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1519 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1520 | end; | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1521 | |
| 45778 | 1522 | local | 
| 1523 |   fun renderLine {indent,line} = nSpaces indent ^ line ^ "\n";
 | |
| 1524 | in | |
| 1525 | fun toStreamWithLineLength len ppA a = | |
| 1526 | Stream.map renderLine (render len (ppA a)); | |
| 1527 | end; | |
| 1528 | ||
| 1529 | fun toLine ppA a = toStringWithLineLength {lineLength = NONE} ppA a;
 | |
| 43269 | 1530 | |
| 45778 | 1531 | (* ------------------------------------------------------------------------- *) | 
| 1532 | (* Pretty-printer rendering with a global line length. *) | |
| 1533 | (* ------------------------------------------------------------------------- *) | |
| 1534 | ||
| 1535 | val lineLength = ref initialLineLength; | |
| 1536 | ||
| 1537 | fun toString ppA a = | |
| 1538 | let | |
| 1539 |       val len = {lineLength = SOME (!lineLength)}
 | |
| 1540 | in | |
| 1541 | toStringWithLineLength len ppA a | |
| 1542 | end; | |
| 43269 | 1543 | |
| 1544 | fun toStream ppA a = | |
| 45778 | 1545 | let | 
| 1546 |       val len = {lineLength = SOME (!lineLength)}
 | |
| 1547 | in | |
| 1548 | toStreamWithLineLength len ppA a | |
| 1549 | end; | |
| 43269 | 1550 | |
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1551 | local | 
| 45778 | 1552 | val sep = mkWord " ="; | 
| 39443 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1553 | in | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1554 | fun trace ppX nameX x = | 
| 
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
 blanchet parents: 
39349diff
changeset | 1555 | 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: 
39349diff
changeset | 1556 | end; | 
| 39348 | 1557 | |
| 1558 | end |