30 -> ast -> Pretty.T |
30 -> ast -> Pretty.T |
31 end |
31 end |
32 end; |
32 end; |
33 |
33 |
34 functor PrinterFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT |
34 functor PrinterFun(structure Symtab: SYMTAB and TypeExt: TYPE_EXT |
35 and SExtension: SEXTENSION sharing TypeExt.SynExt = SExtension.Parser.SynExt) |
35 and SynTrans: SYN_TRANS sharing TypeExt.SynExt = SynTrans.Parser.SynExt) |
36 : PRINTER = |
36 : PRINTER = |
37 struct |
37 struct |
38 |
38 |
39 structure Symtab = Symtab; |
39 structure Symtab = Symtab; |
40 structure SynExt = TypeExt.SynExt; |
40 structure SynExt = TypeExt.SynExt; |
41 open SExtension.Parser.Lexicon SynExt.Ast SynExt TypeExt SExtension; |
41 open SynTrans.Parser.Lexicon SynExt.Ast SynExt TypeExt SynTrans; |
42 |
42 |
43 |
43 |
44 (** options for printing **) |
44 (** options for printing **) |
45 |
45 |
46 val show_types = ref false; |
46 val show_types = ref false; |
59 fun ast_of_term trf show_types show_sorts tm = |
59 fun ast_of_term trf show_types show_sorts tm = |
60 let |
60 let |
61 fun prune_typs (t_seen as (Const _, _)) = t_seen |
61 fun prune_typs (t_seen as (Const _, _)) = t_seen |
62 | prune_typs (t as Free (x, ty), seen) = |
62 | prune_typs (t as Free (x, ty), seen) = |
63 if ty = dummyT then (t, seen) |
63 if ty = dummyT then (t, seen) |
64 else if t mem seen then (Free (x, dummyT), seen) |
64 else if t mem seen then (free x, seen) |
65 else (t, t :: seen) |
65 else (t, t :: seen) |
66 | prune_typs (t as Var (xi, ty), seen) = |
66 | prune_typs (t as Var (xi, ty), seen) = |
67 if ty = dummyT then (t, seen) |
67 if ty = dummyT then (t, seen) |
68 else if t mem seen then (Var (xi, dummyT), seen) |
68 else if t mem seen then (var xi, seen) |
69 else (t, t :: seen) |
69 else (t, t :: seen) |
70 | prune_typs (t_seen as (Bound _, _)) = t_seen |
70 | prune_typs (t_seen as (Bound _, _)) = t_seen |
71 | prune_typs (Abs (x, ty, t), seen) = |
71 | prune_typs (Abs (x, ty, t), seen) = |
72 let val (t', seen') = prune_typs (t, seen); |
72 let val (t', seen') = prune_typs (t, seen); |
73 in (Abs (x, ty, t'), seen') end |
73 in (Abs (x, ty, t'), seen') end |
79 (t1' $ t2', seen'') |
79 (t1' $ t2', seen'') |
80 end; |
80 end; |
81 |
81 |
82 |
82 |
83 fun ast_of (Const (a, _)) = trans a [] |
83 fun ast_of (Const (a, _)) = trans a [] |
84 | ast_of (Free (x, ty)) = constrain x (Free (x, dummyT)) ty |
84 | ast_of (Free (x, ty)) = constrain x (free x) ty |
85 | ast_of (Var (xi, ty)) = constrain (string_of_vname xi) (Var (xi, dummyT)) ty |
85 | ast_of (Var (xi, ty)) = constrain (string_of_vname xi) (var xi) ty |
86 | ast_of (Bound i) = Variable ("B." ^ string_of_int i) |
86 | ast_of (Bound i) = Variable ("B." ^ string_of_int i) |
87 | ast_of (t as Abs _) = ast_of (abs_tr' t) |
87 | ast_of (t as Abs _) = ast_of (abs_tr' t) |
88 | ast_of (t as _ $ _) = |
88 | ast_of (t as _ $ _) = |
89 (case strip_comb t of |
89 (case strip_comb t of |
90 (Const (a, _), args) => trans a args |
90 (Const (a, _), args) => trans a args |
96 | None => raise Match) |
96 | None => raise Match) |
97 handle Match => mk_appl (Constant a) (map ast_of args) |
97 handle Match => mk_appl (Constant a) (map ast_of args) |
98 |
98 |
99 and constrain x t ty = |
99 and constrain x t ty = |
100 if show_types andalso ty <> dummyT then |
100 if show_types andalso ty <> dummyT then |
101 ast_of (Const (constrainC, dummyT) $ t $ term_of_typ show_sorts ty) |
101 ast_of (const constrainC $ t $ term_of_typ show_sorts ty) |
102 else Variable x; |
102 else Variable x; |
103 in |
103 in |
104 if show_types then |
104 if show_types then |
105 ast_of (#1 (prune_typs (prop_tr' show_sorts tm, []))) |
105 ast_of (#1 (prune_typs (prop_tr' show_sorts tm, []))) |
106 else ast_of (prop_tr' show_sorts tm) |
106 else ast_of (prop_tr' show_sorts tm) |
195 |
195 |
196 |
196 |
197 |
197 |
198 (** pretty term or typ asts **) |
198 (** pretty term or typ asts **) |
199 |
199 |
200 fun chain[Block(_,pr)] = chain(pr) |
200 fun chain [Block (_, pr)] = chain pr |
201 | chain[Arg _] = true |
201 | chain [Arg _] = true |
202 | chain _ = false; |
202 | chain _ = false; |
203 |
203 |
204 fun pretty prtab trf type_mode ast0 p0 = |
204 fun pretty prtab trf type_mode ast0 p0 = |
205 let |
205 let |
206 val trans = apply_trans "print ast translation"; |
206 val trans = apply_trans "print ast translation"; |
229 | synT (Break i :: symbs, args) = |
229 | synT (Break i :: symbs, args) = |
230 let val (Ts, args') = synT (symbs, args); |
230 let val (Ts, args') = synT (symbs, args); |
231 in ((if i < 0 then Pretty.fbrk else Pretty.brk i) :: Ts, args') end |
231 in ((if i < 0 then Pretty.fbrk else Pretty.brk i) :: Ts, args') end |
232 | synT (_ :: _, []) = sys_error "synT" |
232 | synT (_ :: _, []) = sys_error "synT" |
233 |
233 |
234 and parT (pr, args, p, p': int) = |
234 and parT (pr, args, p, p': int) = #1 (synT |
235 #1 (synT(if p > p' orelse |
235 (if p > p' orelse |
236 (!show_brackets andalso p' <> max_pri andalso |
236 (! show_brackets andalso p' <> max_pri andalso not (chain pr)) |
237 not(chain pr)) |
237 then [Block (1, String "(" :: pr @ [String ")"])] |
238 then [Block (1, String "(" :: pr @ [String ")"])] |
238 else pr, args)) |
239 else pr, |
|
240 args)) |
|
241 |
239 |
242 and prefixT (_, a, [], _) = [Pretty.str a] |
240 and prefixT (_, a, [], _) = [Pretty.str a] |
243 | prefixT (c, _, args, p) = astT (appT (c, args), p) |
241 | prefixT (c, _, args, p) = astT (appT (c, args), p) |
244 |
242 |
245 and splitT 0 ([x], ys) = (x, ys) |
243 and splitT 0 ([x], ys) = (x, ys) |