wenzelm@18
|
1 |
(* Title: Pure/Syntax/printer.ML
|
clasohm@0
|
2 |
ID: $Id$
|
clasohm@0
|
3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen
|
wenzelm@18
|
4 |
|
wenzelm@18
|
5 |
Pretty printing of asts, terms, types and print (ast) translation.
|
clasohm@0
|
6 |
*)
|
clasohm@0
|
7 |
|
clasohm@0
|
8 |
signature PRINTER0 =
|
clasohm@0
|
9 |
sig
|
nipkow@504
|
10 |
val show_brackets: bool ref
|
nipkow@504
|
11 |
val show_sorts: bool ref
|
clasohm@0
|
12 |
val show_types: bool ref
|
clasohm@0
|
13 |
end;
|
clasohm@0
|
14 |
|
clasohm@0
|
15 |
signature PRINTER =
|
clasohm@0
|
16 |
sig
|
clasohm@0
|
17 |
include PRINTER0
|
clasohm@0
|
18 |
structure Symtab: SYMTAB
|
wenzelm@237
|
19 |
structure SynExt: SYN_EXT
|
wenzelm@381
|
20 |
local open SynExt SynExt.Ast in
|
clasohm@0
|
21 |
val term_to_ast: (string -> (term list -> term) option) -> term -> ast
|
clasohm@0
|
22 |
val typ_to_ast: (string -> (term list -> term) option) -> typ -> ast
|
wenzelm@18
|
23 |
type prtab
|
wenzelm@18
|
24 |
val empty_prtab: prtab
|
wenzelm@237
|
25 |
val extend_prtab: prtab -> xprod list -> prtab
|
wenzelm@237
|
26 |
val merge_prtabs: prtab -> prtab -> prtab
|
wenzelm@237
|
27 |
val pretty_term_ast: prtab -> (string -> (ast list -> ast) option)
|
wenzelm@237
|
28 |
-> ast -> Pretty.T
|
wenzelm@237
|
29 |
val pretty_typ_ast: prtab -> (string -> (ast list -> ast) option)
|
wenzelm@237
|
30 |
-> ast -> Pretty.T
|
clasohm@0
|
31 |
end
|
clasohm@0
|
32 |
end;
|
clasohm@0
|
33 |
|
wenzelm@237
|
34 |
functor PrinterFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT
|
wenzelm@237
|
35 |
and SExtension: SEXTENSION sharing TypeExt.SynExt = SExtension.Parser.SynExt)
|
wenzelm@258
|
36 |
: PRINTER =
|
clasohm@0
|
37 |
struct
|
clasohm@0
|
38 |
|
clasohm@0
|
39 |
structure Symtab = Symtab;
|
wenzelm@237
|
40 |
structure SynExt = TypeExt.SynExt;
|
wenzelm@237
|
41 |
open SExtension.Parser.Lexicon SynExt.Ast SynExt TypeExt SExtension;
|
clasohm@0
|
42 |
|
clasohm@0
|
43 |
|
clasohm@0
|
44 |
(** options for printing **)
|
clasohm@0
|
45 |
|
clasohm@0
|
46 |
val show_types = ref false;
|
clasohm@0
|
47 |
val show_sorts = ref false;
|
nipkow@504
|
48 |
val show_brackets = ref false;
|
clasohm@0
|
49 |
|
clasohm@0
|
50 |
|
wenzelm@18
|
51 |
(** convert term or typ to ast **)
|
clasohm@0
|
52 |
|
wenzelm@18
|
53 |
fun apply_trans name a f args =
|
wenzelm@18
|
54 |
(f args handle
|
clasohm@0
|
55 |
Match => raise Match
|
wenzelm@18
|
56 |
| exn => (writeln ("Error in " ^ name ^ " for " ^ quote a); raise exn));
|
clasohm@0
|
57 |
|
clasohm@0
|
58 |
|
clasohm@0
|
59 |
fun ast_of_term trf show_types show_sorts tm =
|
clasohm@0
|
60 |
let
|
clasohm@0
|
61 |
fun prune_typs (t_seen as (Const _, _)) = t_seen
|
clasohm@0
|
62 |
| prune_typs (t as Free (x, ty), seen) =
|
clasohm@0
|
63 |
if ty = dummyT then (t, seen)
|
clasohm@0
|
64 |
else if t mem seen then (Free (x, dummyT), seen)
|
clasohm@0
|
65 |
else (t, t :: seen)
|
clasohm@0
|
66 |
| prune_typs (t as Var (xi, ty), seen) =
|
clasohm@0
|
67 |
if ty = dummyT then (t, seen)
|
clasohm@0
|
68 |
else if t mem seen then (Var (xi, dummyT), seen)
|
clasohm@0
|
69 |
else (t, t :: seen)
|
clasohm@0
|
70 |
| prune_typs (t_seen as (Bound _, _)) = t_seen
|
clasohm@0
|
71 |
| prune_typs (Abs (x, ty, t), seen) =
|
clasohm@0
|
72 |
let val (t', seen') = prune_typs (t, seen);
|
clasohm@0
|
73 |
in (Abs (x, ty, t'), seen') end
|
clasohm@0
|
74 |
| prune_typs (t1 $ t2, seen) =
|
clasohm@0
|
75 |
let
|
clasohm@0
|
76 |
val (t1', seen') = prune_typs (t1, seen);
|
clasohm@0
|
77 |
val (t2', seen'') = prune_typs (t2, seen');
|
clasohm@0
|
78 |
in
|
clasohm@0
|
79 |
(t1' $ t2', seen'')
|
clasohm@0
|
80 |
end;
|
clasohm@0
|
81 |
|
clasohm@0
|
82 |
|
clasohm@0
|
83 |
fun ast_of (Const (a, _)) = trans a []
|
clasohm@0
|
84 |
| ast_of (Free (x, ty)) = constrain x (Free (x, dummyT)) ty
|
clasohm@0
|
85 |
| ast_of (Var (xi, ty)) = constrain (string_of_vname xi) (Var (xi, dummyT)) ty
|
clasohm@0
|
86 |
| ast_of (Bound i) = Variable ("B." ^ string_of_int i)
|
clasohm@0
|
87 |
| ast_of (t as Abs _) = ast_of (abs_tr' t)
|
clasohm@0
|
88 |
| ast_of (t as _ $ _) =
|
clasohm@0
|
89 |
(case strip_comb t of
|
clasohm@0
|
90 |
(Const (a, _), args) => trans a args
|
clasohm@0
|
91 |
| (f, args) => Appl (map ast_of (f :: args)))
|
clasohm@0
|
92 |
|
clasohm@0
|
93 |
and trans a args =
|
clasohm@0
|
94 |
(case trf a of
|
wenzelm@18
|
95 |
Some f => ast_of (apply_trans "print translation" a f args)
|
clasohm@0
|
96 |
| None => raise Match)
|
clasohm@0
|
97 |
handle Match => mk_appl (Constant a) (map ast_of args)
|
clasohm@0
|
98 |
|
clasohm@0
|
99 |
and constrain x t ty =
|
clasohm@0
|
100 |
if show_types andalso ty <> dummyT then
|
clasohm@0
|
101 |
ast_of (Const (constrainC, dummyT) $ t $ term_of_typ show_sorts ty)
|
clasohm@0
|
102 |
else Variable x;
|
clasohm@0
|
103 |
in
|
wenzelm@381
|
104 |
if show_types then
|
wenzelm@381
|
105 |
ast_of (#1 (prune_typs (prop_tr' show_sorts tm, [])))
|
wenzelm@381
|
106 |
else ast_of (prop_tr' show_sorts tm)
|
clasohm@0
|
107 |
end;
|
clasohm@0
|
108 |
|
clasohm@0
|
109 |
|
clasohm@0
|
110 |
(* term_to_ast *)
|
clasohm@0
|
111 |
|
clasohm@0
|
112 |
fun term_to_ast trf tm =
|
clasohm@0
|
113 |
ast_of_term trf (! show_types) (! show_sorts) tm;
|
clasohm@0
|
114 |
|
clasohm@0
|
115 |
|
clasohm@0
|
116 |
(* typ_to_ast *)
|
clasohm@0
|
117 |
|
clasohm@0
|
118 |
fun typ_to_ast trf ty =
|
clasohm@0
|
119 |
ast_of_term trf false false (term_of_typ (! show_sorts) ty);
|
clasohm@0
|
120 |
|
clasohm@0
|
121 |
|
clasohm@0
|
122 |
|
wenzelm@18
|
123 |
(** type prtab **)
|
clasohm@0
|
124 |
|
clasohm@0
|
125 |
datatype symb =
|
clasohm@0
|
126 |
Arg of int |
|
clasohm@0
|
127 |
TypArg of int |
|
clasohm@0
|
128 |
String of string |
|
clasohm@0
|
129 |
Break of int |
|
clasohm@0
|
130 |
Block of int * symb list;
|
clasohm@0
|
131 |
|
wenzelm@237
|
132 |
type prtab = (symb list * int * int) Symtab.table;
|
clasohm@0
|
133 |
|
clasohm@0
|
134 |
|
wenzelm@237
|
135 |
(* xprods_to_fmts *)
|
clasohm@0
|
136 |
|
wenzelm@237
|
137 |
fun xprod_to_fmt (XProd (_, _, "", _)) = None
|
wenzelm@237
|
138 |
| xprod_to_fmt (XProd (_, xsymbs, const, pri)) =
|
wenzelm@237
|
139 |
let
|
wenzelm@237
|
140 |
fun cons_str s (String s' :: syms) = String (s ^ s') :: syms
|
wenzelm@237
|
141 |
| cons_str s syms = String s :: syms;
|
wenzelm@237
|
142 |
|
wenzelm@237
|
143 |
fun arg (s, p) =
|
wenzelm@237
|
144 |
(if s = "type" then TypArg else Arg)
|
nipkow@506
|
145 |
(if is_terminal s then max_pri else p);
|
wenzelm@237
|
146 |
|
wenzelm@237
|
147 |
fun xsyms_to_syms (Delim s :: xsyms) =
|
wenzelm@237
|
148 |
apfst (cons_str s) (xsyms_to_syms xsyms)
|
wenzelm@237
|
149 |
| xsyms_to_syms (Argument s_p :: xsyms) =
|
wenzelm@237
|
150 |
apfst (cons (arg s_p)) (xsyms_to_syms xsyms)
|
wenzelm@237
|
151 |
| xsyms_to_syms (Space s :: xsyms) =
|
wenzelm@237
|
152 |
apfst (cons_str s) (xsyms_to_syms xsyms)
|
wenzelm@237
|
153 |
| xsyms_to_syms (Bg i :: xsyms) =
|
wenzelm@237
|
154 |
let
|
wenzelm@237
|
155 |
val (bsyms, xsyms') = xsyms_to_syms xsyms;
|
wenzelm@237
|
156 |
val (syms, xsyms'') = xsyms_to_syms xsyms';
|
wenzelm@237
|
157 |
in
|
wenzelm@237
|
158 |
(Block (i, bsyms) :: syms, xsyms'')
|
wenzelm@237
|
159 |
end
|
wenzelm@237
|
160 |
| xsyms_to_syms (Brk i :: xsyms) =
|
wenzelm@237
|
161 |
apfst (cons (Break i)) (xsyms_to_syms xsyms)
|
wenzelm@237
|
162 |
| xsyms_to_syms (En :: xsyms) = ([], xsyms)
|
wenzelm@237
|
163 |
| xsyms_to_syms [] = ([], []);
|
wenzelm@237
|
164 |
|
wenzelm@237
|
165 |
fun nargs (Arg _ :: syms) = nargs syms + 1
|
wenzelm@237
|
166 |
| nargs (TypArg _ :: syms) = nargs syms + 1
|
wenzelm@237
|
167 |
| nargs (String _ :: syms) = nargs syms
|
wenzelm@237
|
168 |
| nargs (Break _ :: syms) = nargs syms
|
wenzelm@237
|
169 |
| nargs (Block (_, bsyms) :: syms) = nargs syms + nargs bsyms
|
wenzelm@237
|
170 |
| nargs [] = 0;
|
wenzelm@237
|
171 |
in
|
wenzelm@237
|
172 |
(case xsyms_to_syms xsymbs of
|
wenzelm@237
|
173 |
(symbs, []) => Some (const, (symbs, nargs symbs, pri))
|
wenzelm@237
|
174 |
| _ => sys_error "xprod_to_fmt: unbalanced blocks")
|
wenzelm@237
|
175 |
end;
|
wenzelm@237
|
176 |
|
wenzelm@237
|
177 |
fun xprods_to_fmts xprods =
|
wenzelm@237
|
178 |
gen_distinct eq_fst (mapfilter xprod_to_fmt xprods);
|
wenzelm@237
|
179 |
|
wenzelm@237
|
180 |
|
wenzelm@237
|
181 |
(* empty, extend, merge prtabs *)
|
wenzelm@237
|
182 |
|
wenzelm@381
|
183 |
fun err_dup_fmts cs =
|
wenzelm@381
|
184 |
error ("Duplicate formats in printer table for " ^ commas_quote cs);
|
clasohm@0
|
185 |
|
wenzelm@18
|
186 |
val empty_prtab = Symtab.null;
|
clasohm@0
|
187 |
|
wenzelm@237
|
188 |
fun extend_prtab tab xprods =
|
wenzelm@237
|
189 |
Symtab.extend (op =) (tab, xprods_to_fmts xprods)
|
wenzelm@381
|
190 |
handle Symtab.DUPS cs => err_dup_fmts cs;
|
wenzelm@18
|
191 |
|
wenzelm@237
|
192 |
fun merge_prtabs tab1 tab2 =
|
wenzelm@237
|
193 |
Symtab.merge (op =) (tab1, tab2)
|
wenzelm@381
|
194 |
handle Symtab.DUPS cs => err_dup_fmts cs;
|
wenzelm@18
|
195 |
|
wenzelm@18
|
196 |
|
clasohm@0
|
197 |
|
wenzelm@237
|
198 |
(** pretty term or typ asts **)
|
clasohm@0
|
199 |
|
nipkow@506
|
200 |
fun chain[Block(_,pr)] = chain(pr)
|
nipkow@506
|
201 |
| chain[Arg _] = true
|
nipkow@506
|
202 |
| chain _ = false;
|
nipkow@506
|
203 |
|
wenzelm@237
|
204 |
fun pretty prtab trf type_mode ast0 p0 =
|
clasohm@0
|
205 |
let
|
wenzelm@18
|
206 |
val trans = apply_trans "print ast translation";
|
wenzelm@18
|
207 |
|
wenzelm@18
|
208 |
val appT = if type_mode then tappl_ast_tr' else appl_ast_tr';
|
wenzelm@18
|
209 |
|
clasohm@0
|
210 |
fun synT ([], args) = ([], args)
|
clasohm@0
|
211 |
| synT (Arg p :: symbs, t :: args) =
|
wenzelm@18
|
212 |
let val (Ts, args') = synT (symbs, args);
|
clasohm@0
|
213 |
in (astT (t, p) @ Ts, args') end
|
clasohm@0
|
214 |
| synT (TypArg p :: symbs, t :: args) =
|
wenzelm@237
|
215 |
let
|
wenzelm@237
|
216 |
val (Ts, args') = synT (symbs, args);
|
wenzelm@237
|
217 |
in
|
wenzelm@237
|
218 |
if type_mode then (astT (t, p) @ Ts, args')
|
wenzelm@237
|
219 |
else (pretty prtab trf true t p @ Ts, args')
|
wenzelm@237
|
220 |
end
|
clasohm@0
|
221 |
| synT (String s :: symbs, args) =
|
wenzelm@18
|
222 |
let val (Ts, args') = synT (symbs, args);
|
clasohm@0
|
223 |
in (Pretty.str s :: Ts, args') end
|
clasohm@0
|
224 |
| synT (Block (i, bsymbs) :: symbs, args) =
|
clasohm@0
|
225 |
let
|
clasohm@0
|
226 |
val (bTs, args') = synT (bsymbs, args);
|
clasohm@0
|
227 |
val (Ts, args'') = synT (symbs, args');
|
clasohm@0
|
228 |
in (Pretty.blk (i, bTs) :: Ts, args'') end
|
clasohm@0
|
229 |
| synT (Break i :: symbs, args) =
|
wenzelm@18
|
230 |
let val (Ts, args') = synT (symbs, args);
|
wenzelm@18
|
231 |
in ((if i < 0 then Pretty.fbrk else Pretty.brk i) :: Ts, args') end
|
wenzelm@237
|
232 |
| synT (_ :: _, []) = sys_error "synT"
|
clasohm@0
|
233 |
|
clasohm@0
|
234 |
and parT (pr, args, p, p': int) =
|
nipkow@506
|
235 |
#1 (synT(if p > p' orelse
|
nipkow@506
|
236 |
(!show_brackets andalso p' <> max_pri andalso
|
nipkow@506
|
237 |
not(chain pr))
|
nipkow@506
|
238 |
then [Block (1, String "(" :: pr @ [String ")"])]
|
nipkow@506
|
239 |
else pr,
|
nipkow@506
|
240 |
args))
|
clasohm@0
|
241 |
|
clasohm@0
|
242 |
and prefixT (_, a, [], _) = [Pretty.str a]
|
clasohm@0
|
243 |
| prefixT (c, _, args, p) = astT (appT (c, args), p)
|
clasohm@0
|
244 |
|
wenzelm@18
|
245 |
and splitT 0 ([x], ys) = (x, ys)
|
wenzelm@18
|
246 |
| splitT 0 (rev_xs, ys) = (Appl (rev rev_xs), ys)
|
wenzelm@18
|
247 |
| splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
|
wenzelm@237
|
248 |
| splitT _ _ = sys_error "splitT"
|
wenzelm@18
|
249 |
|
clasohm@0
|
250 |
and combT (tup as (c, a, args, p)) =
|
clasohm@0
|
251 |
let
|
clasohm@0
|
252 |
val nargs = length args;
|
wenzelm@18
|
253 |
|
clasohm@0
|
254 |
fun prnt (pr, n, p') =
|
wenzelm@18
|
255 |
if nargs = n then parT (pr, args, p, p')
|
wenzelm@18
|
256 |
else if nargs < n orelse type_mode then prefixT tup
|
wenzelm@18
|
257 |
else astT (appT (splitT n ([c], args)), p);
|
clasohm@0
|
258 |
in
|
wenzelm@237
|
259 |
(case (trf a, Symtab.lookup (prtab, a)) of
|
wenzelm@237
|
260 |
(None, None) => prefixT tup
|
wenzelm@237
|
261 |
| (None, Some prnp) => prnt prnp
|
wenzelm@237
|
262 |
| (Some f, None) =>
|
wenzelm@18
|
263 |
(astT (trans a f args, p) handle Match => prefixT tup)
|
wenzelm@237
|
264 |
| (Some f, Some prnp) =>
|
wenzelm@18
|
265 |
(astT (trans a f args, p) handle Match => prnt prnp))
|
clasohm@0
|
266 |
end
|
clasohm@0
|
267 |
|
clasohm@0
|
268 |
and astT (c as Constant a, p) = combT (c, a, [], p)
|
clasohm@0
|
269 |
| astT (Variable x, _) = [Pretty.str x]
|
wenzelm@18
|
270 |
| astT (Appl ((c as Constant a) :: (args as _ :: _)), p) =
|
wenzelm@18
|
271 |
combT (c, a, args, p)
|
clasohm@0
|
272 |
| astT (Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
|
wenzelm@18
|
273 |
| astT (ast as Appl _, _) = raise_ast "pretty: malformed ast" [ast];
|
clasohm@0
|
274 |
in
|
clasohm@0
|
275 |
astT (ast0, p0)
|
clasohm@0
|
276 |
end;
|
clasohm@0
|
277 |
|
clasohm@0
|
278 |
|
clasohm@0
|
279 |
(* pretty_term_ast *)
|
clasohm@0
|
280 |
|
wenzelm@237
|
281 |
fun pretty_term_ast prtab trf ast =
|
wenzelm@237
|
282 |
Pretty.blk (0, pretty prtab trf false ast 0);
|
clasohm@0
|
283 |
|
clasohm@0
|
284 |
|
clasohm@0
|
285 |
(* pretty_typ_ast *)
|
clasohm@0
|
286 |
|
wenzelm@237
|
287 |
fun pretty_typ_ast prtab trf ast =
|
wenzelm@237
|
288 |
Pretty.blk (0, pretty prtab trf true ast 0);
|
clasohm@0
|
289 |
|
clasohm@0
|
290 |
|
clasohm@0
|
291 |
end;
|
clasohm@0
|
292 |
|