src/Pure/Syntax/printer.ML
author paulson
Thu Sep 25 12:09:41 1997 +0200 (1997-09-25)
changeset 3706 e57b5902822f
parent 2913 ce271fa4d8e2
child 3776 38f8ec304b95
permissions -rw-r--r--
Generalized and exported biresolution_from_nets_tac to allow the declaration
of Clarify_tac
     1 (*  Title:      Pure/Syntax/printer.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Pretty printing of asts, terms, types and print (ast) translation.
     6 *)
     7 
     8 signature PRINTER0 =
     9 sig
    10   val show_brackets: bool ref
    11   val show_sorts: bool ref
    12   val show_types: bool ref
    13   val show_no_free_types: bool ref
    14   val print_mode: string list ref
    15 end;
    16 
    17 signature PRINTER =
    18 sig
    19   include PRINTER0
    20   val term_to_ast: (string -> (typ -> term list -> term) option) -> term -> Ast.ast
    21   val typ_to_ast: (string -> (typ -> term list -> term) option) -> typ -> Ast.ast
    22   type prtabs
    23   val empty_prtabs: prtabs
    24   val extend_prtabs: prtabs -> string -> SynExt.xprod list -> prtabs
    25   val merge_prtabs: prtabs -> prtabs -> prtabs
    26   val pretty_term_ast: bool -> prtabs
    27     -> (string -> (Ast.ast list -> Ast.ast) option)
    28     -> (string -> (string -> string * int) option) -> Ast.ast -> Pretty.T
    29   val pretty_typ_ast: bool -> prtabs
    30     -> (string -> (Ast.ast list -> Ast.ast) option)
    31     -> (string -> (string -> string * int) option) -> Ast.ast -> Pretty.T
    32 end;
    33 
    34 structure Printer: PRINTER =
    35 struct
    36 
    37 open Lexicon Ast SynExt TypeExt SynTrans;
    38 
    39 
    40 (** options for printing **)
    41 
    42 val show_types = ref false;
    43 val show_sorts = ref false;
    44 val show_brackets = ref false;
    45 val show_no_free_types = ref false;
    46 val print_mode = ref ([]:string list);
    47 
    48 
    49 
    50 (** misc utils **)
    51 
    52 (* apply print (ast) translation function *)
    53 
    54 fun apply_trans name a f args =
    55   (f args handle
    56     Match => raise Match
    57   | exn => (writeln ("Error in " ^ name ^ " for " ^ quote a); raise exn));
    58 
    59 
    60 (* simple_ast_of *)
    61 
    62 fun simple_ast_of (Const (c, _)) = Constant c
    63   | simple_ast_of (Free (x, _)) = Variable x
    64   | simple_ast_of (Var (xi, _)) = Variable (string_of_vname xi)
    65   | simple_ast_of (t as _ $ _) =
    66       let val (f, args) = strip_comb t in
    67         mk_appl (simple_ast_of f) (map simple_ast_of args)
    68       end
    69   | simple_ast_of (Bound i) = Variable ("B." ^ string_of_int i)
    70   | simple_ast_of (Abs _) = sys_error "simple_ast_of: Abs";
    71 
    72 
    73 
    74 (** typ_to_ast **)
    75 
    76 fun ast_of_termT trf tm =
    77   let
    78     fun ast_of (t as Const ("_class", _) $ Free _) = simple_ast_of t
    79       | ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of t
    80       | ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of t
    81       | ast_of (Const (a, _)) = trans a []
    82       | ast_of (t as _ $ _) =
    83           (case strip_comb t of
    84             (Const (a, _), args) => trans a args
    85           | (f, args) => Appl (map ast_of (f :: args)))
    86       | ast_of t = raise_term "ast_of_termT: bad term encoding of type" [t]
    87     and trans a args =
    88       (case trf a of
    89         Some f => ast_of (apply_trans "print translation" a (uncurry f) (dummyT, args))
    90       | None => raise Match)
    91           handle Match => mk_appl (Constant a) (map ast_of args);
    92   in
    93     ast_of tm
    94   end;
    95 
    96 fun typ_to_ast trf T = ast_of_termT trf (term_of_typ (! show_sorts) T);
    97 
    98 
    99 
   100 (** term_to_ast **)
   101 
   102 fun mark_freevars (t $ u) = mark_freevars t $ mark_freevars u
   103   | mark_freevars (Abs (x, T, t)) = Abs (x, T, mark_freevars t)
   104   | mark_freevars (t as Free _) = const "_free" $ t
   105   | mark_freevars (t as Var _) = const "_var" $ t
   106   | mark_freevars a = a;
   107 
   108 fun ast_of_term trf no_freeTs show_types show_sorts tm =
   109   let
   110     fun prune_typs (t_seen as (Const _, _)) = t_seen
   111       | prune_typs (t as Free (x, ty), seen) =
   112           if ty = dummyT then (t, seen)
   113           else if no_freeTs orelse t mem seen then (free x, seen)
   114           else (t, t :: seen)
   115       | prune_typs (t as Var (xi, ty), seen) =
   116           if ty = dummyT then (t, seen)
   117           else if no_freeTs orelse t mem seen then (var xi, seen)
   118           else (t, t :: seen)
   119       | prune_typs (t_seen as (Bound _, _)) = t_seen
   120       | prune_typs (Abs (x, ty, t), seen) =
   121           let val (t', seen') = prune_typs (t, seen);
   122           in (Abs (x, ty, t'), seen') end
   123       | prune_typs (t1 $ t2, seen) =
   124           let
   125             val (t1', seen') = prune_typs (t1, seen);
   126             val (t2', seen'') = prune_typs (t2, seen');
   127           in
   128             (t1' $ t2', seen'')
   129           end;
   130 
   131     fun ast_of tm =
   132       (case strip_comb tm of
   133         (t as Abs _, ts) => mk_appl (ast_of (abs_tr' t)) (map ast_of ts)
   134       | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
   135           mk_appl (constrain (c $ free x) T) (map ast_of ts)
   136       | ((c as Const ("_bound", _)), Free (x, T) :: ts) =>
   137           mk_appl (constrain (c $ free x) T) (map ast_of ts)
   138       | ((c as Const ("_var", _)), Var (xi, T) :: ts) =>
   139           mk_appl (constrain (c $ var xi) T) (map ast_of ts)
   140       | (Const (c, T), ts) => trans c T ts
   141       | (t, ts) => mk_appl (simple_ast_of t) (map ast_of ts))
   142 
   143     and trans a T args =
   144       (case trf a of
   145         Some f => ast_of (apply_trans "print translation" a (uncurry f) (T, args))
   146       | None => raise Match)
   147           handle Match => mk_appl (Constant a) (map ast_of args)
   148 
   149     and constrain t T =
   150       if show_types andalso T <> dummyT then
   151         Appl [Constant constrainC, simple_ast_of t,
   152           ast_of_termT trf (term_of_typ show_sorts T)]
   153       else simple_ast_of t
   154   in
   155     tm
   156     |> prop_tr' show_sorts
   157     |> (if show_types then #1 o prune_typs o rpair [] else I)
   158     |> mark_freevars
   159     |> ast_of
   160   end;
   161 
   162 fun term_to_ast trf tm =
   163   ast_of_term trf (! show_no_free_types) (! show_types orelse ! show_sorts)
   164     (! show_sorts) tm;
   165 
   166 
   167 
   168 (** type prtabs **)
   169 
   170 datatype symb =
   171   Arg of int |
   172   TypArg of int |
   173   String of string |
   174   Break of int |
   175   Block of int * symb list;
   176 
   177 type prtabs = (string * ((symb list * int * int) list) Symtab.table) list;
   178 
   179 (*find tab for mode*)
   180 fun get_tab prtabs mode =
   181   if_none (assoc (prtabs, mode)) Symtab.null;
   182 
   183 (*collect tabs for mode hierarchy (default "")*)
   184 fun tabs_of prtabs modes =
   185   mapfilter (fn mode => assoc (prtabs, mode)) (modes @ [""]);
   186 
   187 (*find formats in tab hierarchy*)
   188 fun get_fmts [] _ = []
   189   | get_fmts (tab :: tabs) a = Symtab.lookup_multi (tab, a) @ get_fmts tabs a;
   190 
   191 
   192 (* xprods_to_fmts *)
   193 
   194 fun xprod_to_fmt (XProd (_, _, "", _)) = None
   195   | xprod_to_fmt (XProd (_, xsymbs, const, pri)) =
   196       let
   197         fun cons_str s (String s' :: syms) = String (s ^ s') :: syms
   198           | cons_str s syms = String s :: syms;
   199 
   200         fun arg (s, p) =
   201           (if s = "type" then TypArg else Arg)
   202           (if is_terminal s then max_pri else p);
   203 
   204         fun xsyms_to_syms (Delim s :: xsyms) =
   205               apfst (cons_str s) (xsyms_to_syms xsyms)
   206           | xsyms_to_syms (Argument s_p :: xsyms) =
   207               apfst (cons (arg s_p)) (xsyms_to_syms xsyms)
   208           | xsyms_to_syms (Space s :: xsyms) =
   209               apfst (cons_str s) (xsyms_to_syms xsyms)
   210           | xsyms_to_syms (Bg i :: xsyms) =
   211               let
   212                 val (bsyms, xsyms') = xsyms_to_syms xsyms;
   213                 val (syms, xsyms'') = xsyms_to_syms xsyms';
   214               in
   215                 (Block (i, bsyms) :: syms, xsyms'')
   216               end
   217           | xsyms_to_syms (Brk i :: xsyms) =
   218               apfst (cons (Break i)) (xsyms_to_syms xsyms)
   219           | xsyms_to_syms (En :: xsyms) = ([], xsyms)
   220           | xsyms_to_syms [] = ([], []);
   221 
   222         fun nargs (Arg _ :: syms) = nargs syms + 1
   223           | nargs (TypArg _ :: syms) = nargs syms + 1
   224           | nargs (String _ :: syms) = nargs syms
   225           | nargs (Break _ :: syms) = nargs syms
   226           | nargs (Block (_, bsyms) :: syms) = nargs syms + nargs bsyms
   227           | nargs [] = 0;
   228       in
   229         (case xsyms_to_syms xsymbs of
   230           (symbs, []) => Some (const, (symbs, nargs symbs, pri))
   231         | _ => sys_error "xprod_to_fmt: unbalanced blocks")
   232       end;
   233 
   234 fun xprods_to_fmts xprods = mapfilter xprod_to_fmt xprods;
   235 
   236 
   237 (* empty, extend, merge prtabs *)
   238 
   239 val empty_prtabs = [];
   240 
   241 fun extend_prtabs prtabs mode xprods =
   242   let
   243     val fmts = xprods_to_fmts xprods;
   244     val tab = get_tab prtabs mode;
   245     val new_tab = generic_extend (op =) Symtab.dest_multi Symtab.make_multi tab fmts;
   246   in overwrite (prtabs, (mode, new_tab)) end;
   247 
   248 fun merge_prtabs prtabs1 prtabs2 =
   249   let
   250     val modes = distinct (map fst (prtabs1 @ prtabs2));
   251     fun merge mode =
   252       (mode,
   253         generic_merge (op =) Symtab.dest_multi Symtab.make_multi
   254           (get_tab prtabs1 mode) (get_tab prtabs2 mode));
   255   in
   256     map merge modes
   257   end;
   258 
   259 
   260 
   261 (** pretty term or typ asts **)
   262 
   263 fun is_chain [Block (_, pr)] = is_chain pr
   264   | is_chain [Arg _] = true
   265   | is_chain _  = false;
   266 
   267 fun pretty tabs trf tokentrf type_mode curried ast0 p0 =
   268   let
   269     val trans = apply_trans "print ast translation";
   270 
   271     fun token_trans c [Variable x] =
   272           (case tokentrf c of
   273             None => None
   274           | Some f => Some (f x))
   275       | token_trans _ _ = None;
   276 
   277     (*default applications: prefix / postfix*)
   278     val appT =
   279       if type_mode then tappl_ast_tr'
   280       else if curried then applC_ast_tr'
   281       else appl_ast_tr';
   282 
   283     fun synT ([], args) = ([], args)
   284       | synT (Arg p :: symbs, t :: args) =
   285           let val (Ts, args') = synT (symbs, args);
   286           in (astT (t, p) @ Ts, args') end
   287       | synT (TypArg p :: symbs, t :: args) =
   288           let
   289             val (Ts, args') = synT (symbs, args);
   290           in
   291             if type_mode then (astT (t, p) @ Ts, args')
   292             else (pretty tabs trf tokentrf true curried t p @ Ts, args')
   293           end
   294       | synT (String s :: symbs, args) =
   295           let val (Ts, args') = synT (symbs, args);
   296           in (Pretty.str s :: Ts, args') end
   297       | synT (Block (i, bsymbs) :: symbs, args) =
   298           let
   299             val (bTs, args') = synT (bsymbs, args);
   300             val (Ts, args'') = synT (symbs, args');
   301           in (Pretty.blk (i, bTs) :: Ts, args'') end
   302       | synT (Break i :: symbs, args) =
   303           let val (Ts, args') = synT (symbs, args);
   304           in ((if i < 0 then Pretty.fbrk else Pretty.brk i) :: Ts, args') end
   305       | synT (_ :: _, []) = sys_error "synT"
   306 
   307     and parT (pr, args, p, p': int) = #1 (synT
   308           (if p > p' orelse
   309             (! show_brackets andalso p' <> max_pri andalso not (is_chain pr))
   310             then [Block (1, String "(" :: pr @ [String ")"])]
   311             else pr, args))
   312 
   313     and prefixT (_, a, [], _) = [Pretty.str a]
   314       | prefixT (c, _, args, p) =
   315           if c = Constant "_appl" orelse c = Constant "_applC" then
   316             error "Syntax insufficient for printing prefix applications!"
   317           else astT (appT (c, args), p)
   318 
   319     and splitT 0 ([x], ys) = (x, ys)
   320       | splitT 0 (rev_xs, ys) = (Appl (rev rev_xs), ys)
   321       | splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
   322       | splitT _ _ = sys_error "splitT"
   323 
   324     and combT (tup as (c, a, args, p)) =
   325       let
   326         val nargs = length args;
   327 
   328         (*find matching table entry, or print as prefix / postfix*)
   329         fun prnt [] = prefixT tup
   330           | prnt ((pr, n, p') :: prnps) =
   331               if nargs = n then parT (pr, args, p, p')
   332               else if nargs > n andalso not type_mode then
   333                 astT (appT (splitT n ([c], args)), p)
   334               else prnt prnps;
   335       in
   336         (case token_trans a args of     (*try token translation function*)
   337           Some (s, len) => [Pretty.strlen s len]
   338         | None =>
   339             (case trf a of              (*try print translation function*)
   340               Some f => (astT (trans a f args, p) handle Match => prnt (get_fmts tabs a))
   341             | None => prnt (get_fmts tabs a)))
   342       end
   343 
   344     and astT (c as Constant a, p) = combT (c, a, [], p)
   345       | astT (Variable x, _) = [Pretty.str x]
   346       | astT (Appl ((c as Constant a) :: (args as _ :: _)), p) =
   347           combT (c, a, args, p)
   348       | astT (Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
   349       | astT (ast as Appl _, _) = raise_ast "pretty: malformed ast" [ast];
   350   in
   351     astT (ast0, p0)
   352   end;
   353 
   354 
   355 (* pretty_term_ast *)
   356 
   357 fun pretty_term_ast curried prtabs trf tokentrf ast =
   358   Pretty.blk (0, pretty (tabs_of prtabs (! print_mode))
   359     trf tokentrf false curried ast 0);
   360 
   361 
   362 (* pretty_typ_ast *)
   363 
   364 fun pretty_typ_ast _ prtabs trf tokentrf ast =
   365   Pretty.blk (0, pretty (tabs_of prtabs (! print_mode))
   366     trf tokentrf true false ast 0);
   367 
   368 end;