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