src/Pure/Syntax/printer.ML
author wenzelm
Thu Aug 30 15:04:42 2007 +0200 (2007-08-30 ago)
changeset 24484 013b98b57b86
parent 23937 66e1f24d655d
child 24612 d1b315bdb8d7
permissions -rw-r--r--
maintain mode in context (get/set/restore_mode);
     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 show_all_types: bool ref
    15   val pp_show_brackets: Pretty.pp -> Pretty.pp
    16 end;
    17 
    18 signature PRINTER =
    19 sig
    20   include PRINTER0
    21   val term_to_ast: Proof.context ->
    22     (string -> (Proof.context -> bool -> typ -> term list -> term) list) -> term -> Ast.ast
    23   val typ_to_ast: Proof.context ->
    24     (string -> (Proof.context -> bool -> typ -> term list -> term) list) -> typ -> Ast.ast
    25   val sort_to_ast: Proof.context ->
    26     (string -> (Proof.context -> bool -> typ -> term list -> term) list) -> sort -> Ast.ast
    27   type prtabs
    28   val empty_prtabs: prtabs
    29   val extend_prtabs: string -> SynExt.xprod list -> prtabs -> prtabs
    30   val remove_prtabs: string -> SynExt.xprod list -> prtabs -> prtabs
    31   val merge_prtabs: prtabs -> prtabs -> prtabs
    32   val pretty_term_ast: (string -> xstring) -> Proof.context -> bool -> prtabs
    33     -> (string -> (Proof.context -> Ast.ast list -> Ast.ast) list)
    34     -> (string -> (string -> output * int) option) -> Ast.ast -> Pretty.T list
    35   val pretty_typ_ast: Proof.context -> bool -> prtabs
    36     -> (string -> (Proof.context -> Ast.ast list -> Ast.ast) list)
    37     -> (string -> (string -> output * int) option) -> Ast.ast -> Pretty.T list
    38 end;
    39 
    40 structure Printer: PRINTER =
    41 struct
    42 
    43 
    44 (** options for printing **)
    45 
    46 val show_types = ref false;
    47 val show_sorts = ref false;
    48 val show_brackets = ref false;
    49 val show_no_free_types = ref false;
    50 val show_all_types = ref false;
    51 
    52 fun pp_show_brackets pp = Pretty.pp (setmp show_brackets true (Pretty.term pp),
    53   Pretty.typ pp, Pretty.sort pp, Pretty.classrel pp, Pretty.arity pp);
    54 
    55 
    56 
    57 (** misc utils **)
    58 
    59 (* apply print (ast) translation function *)
    60 
    61 fun apply_trans ctxt fns args =
    62   let
    63     fun app_first [] = raise Match
    64       | app_first (f :: fs) = f ctxt args handle Match => app_first fs;
    65   in app_first fns end;
    66 
    67 fun apply_typed x y fs = map (fn f => fn ctxt => f ctxt x y) fs;
    68 
    69 
    70 (* simple_ast_of *)
    71 
    72 fun simple_ast_of (Const (c, _)) = Ast.Constant c
    73   | simple_ast_of (Free (x, _)) = Ast.Variable x
    74   | simple_ast_of (Var (xi, _)) = Ast.Variable (Term.string_of_vname xi)
    75   | simple_ast_of (t as _ $ _) =
    76       let val (f, args) = strip_comb t in
    77         Ast.mk_appl (simple_ast_of f) (map simple_ast_of args)
    78       end
    79   | simple_ast_of (Bound i) = Ast.Variable ("B." ^ string_of_int i)
    80   | simple_ast_of (Abs _) = sys_error "simple_ast_of: Abs";
    81 
    82 
    83 
    84 (** sort_to_ast, typ_to_ast **)
    85 
    86 fun ast_of_termT ctxt trf tm =
    87   let
    88     fun ast_of (t as Const ("_class", _) $ Free _) = simple_ast_of t
    89       | ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of t
    90       | ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of t
    91       | ast_of (Const (a, _)) = trans a []
    92       | ast_of (t as _ $ _) =
    93           (case strip_comb t of
    94             (Const (a, _), args) => trans a args
    95           | (f, args) => Ast.Appl (map ast_of (f :: args)))
    96       | ast_of t = simple_ast_of t
    97     and trans a args =
    98       ast_of (apply_trans ctxt (apply_typed false dummyT (trf a)) args)
    99         handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args);
   100   in ast_of tm end;
   101 
   102 fun sort_to_ast ctxt trf S = ast_of_termT ctxt trf (TypeExt.term_of_sort S);
   103 fun typ_to_ast ctxt trf T = ast_of_termT ctxt trf (TypeExt.term_of_typ (! show_sorts) T);
   104 
   105 
   106 
   107 (** term_to_ast **)
   108 
   109 fun mark_freevars ((t as Const (c, _)) $ u) =
   110       if member (op =) SynExt.standard_token_markers c then (t $ u)
   111       else t $ mark_freevars u
   112   | mark_freevars (t $ u) = mark_freevars t $ mark_freevars u
   113   | mark_freevars (Abs (x, T, t)) = Abs (x, T, mark_freevars t)
   114   | mark_freevars (t as Free _) = Lexicon.const "_free" $ t
   115   | mark_freevars (t as Var (xi, T)) =
   116       if xi = SynExt.dddot_indexname then Const ("_DDDOT", T)
   117       else Lexicon.const "_var" $ t
   118   | mark_freevars a = a;
   119 
   120 fun ast_of_term ctxt trf show_all_types no_freeTs show_types show_sorts tm =
   121   let
   122     fun prune_typs (t_seen as (Const _, _)) = t_seen
   123       | prune_typs (t as Free (x, ty), seen) =
   124           if ty = dummyT then (t, seen)
   125           else if no_freeTs orelse member (op aconv) seen t then (Lexicon.free x, seen)
   126           else (t, t :: seen)
   127       | prune_typs (t as Var (xi, ty), seen) =
   128           if ty = dummyT then (t, seen)
   129           else if no_freeTs orelse member (op aconv) seen t then (Lexicon.var xi, seen)
   130           else (t, t :: seen)
   131       | prune_typs (t_seen as (Bound _, _)) = t_seen
   132       | prune_typs (Abs (x, ty, t), seen) =
   133           let val (t', seen') = prune_typs (t, seen);
   134           in (Abs (x, ty, t'), seen') end
   135       | prune_typs (t1 $ t2, seen) =
   136           let
   137             val (t1', seen') = prune_typs (t1, seen);
   138             val (t2', seen'') = prune_typs (t2, seen');
   139           in (t1' $ t2', seen'') end;
   140 
   141     fun ast_of tm =
   142       (case strip_comb tm of
   143         (t as Abs _, ts) => Ast.mk_appl (ast_of (SynTrans.abs_tr' t)) (map ast_of ts)
   144       | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
   145           Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts)
   146       | ((c as Const ("_var", _)), Var (xi, T) :: ts) =>
   147           Ast.mk_appl (constrain (c $ Lexicon.var xi) T) (map ast_of ts)
   148       | ((c as Const ("_bound", _)), Free (x, T) :: ts) =>
   149           Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts)
   150       | (Const ("_idtdummy", T), ts) =>
   151           Ast.mk_appl (constrain (Lexicon.const "_idtdummy") T) (map ast_of ts)
   152       | (c' as Const (c, T), ts) =>
   153           if show_all_types
   154           then Ast.mk_appl (constrain c' T) (map ast_of ts)
   155           else trans c T ts
   156       | (t, ts) => Ast.mk_appl (simple_ast_of t) (map ast_of ts))
   157 
   158     and trans a T args =
   159       ast_of (apply_trans ctxt (apply_typed show_sorts T (trf a)) args)
   160         handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args)
   161 
   162     and constrain t T =
   163       if show_types andalso T <> dummyT then
   164         Ast.Appl [Ast.Constant SynExt.constrainC, simple_ast_of t,
   165           ast_of_termT ctxt trf (TypeExt.term_of_typ show_sorts T)]
   166       else simple_ast_of t
   167   in
   168     tm
   169     |> SynTrans.prop_tr'
   170     |> (if show_types then #1 o prune_typs o rpair [] else I)
   171     |> mark_freevars
   172     |> ast_of
   173   end;
   174 
   175 fun term_to_ast ctxt trf tm =
   176   ast_of_term ctxt trf (! show_all_types) (! show_no_free_types)
   177     (! show_types orelse ! show_sorts orelse ! show_all_types) (! show_sorts) tm;
   178 
   179 
   180 
   181 (** type prtabs **)
   182 
   183 datatype symb =
   184   Arg of int |
   185   TypArg of int |
   186   String of string |
   187   Space of string |
   188   Break of int |
   189   Block of int * symb list;
   190 
   191 type prtabs = (string * ((symb list * int * int) list) Symtab.table) list;
   192 
   193 fun mode_tab prtabs mode = the_default Symtab.empty (AList.lookup (op =) prtabs mode);
   194 fun mode_tabs prtabs modes = map_filter (AList.lookup (op =) prtabs) (modes @ [""]);
   195 
   196 
   197 (* xprod_to_fmt *)
   198 
   199 fun xprod_to_fmt (SynExt.XProd (_, _, "", _)) = NONE
   200   | xprod_to_fmt (SynExt.XProd (_, xsymbs, const, pri)) =
   201       let
   202         fun arg (s, p) =
   203           (if s = "type" then TypArg else Arg)
   204           (if Lexicon.is_terminal s then SynExt.max_pri else p);
   205 
   206         fun xsyms_to_syms (SynExt.Delim s :: xsyms) =
   207               apfst (cons (String s)) (xsyms_to_syms xsyms)
   208           | xsyms_to_syms (SynExt.Argument s_p :: xsyms) =
   209               apfst (cons (arg s_p)) (xsyms_to_syms xsyms)
   210           | xsyms_to_syms (SynExt.Space s :: xsyms) =
   211               apfst (cons (Space s)) (xsyms_to_syms xsyms)
   212           | xsyms_to_syms (SynExt.Bg i :: xsyms) =
   213               let
   214                 val (bsyms, xsyms') = xsyms_to_syms xsyms;
   215                 val (syms, xsyms'') = xsyms_to_syms xsyms';
   216               in
   217                 (Block (i, bsyms) :: syms, xsyms'')
   218               end
   219           | xsyms_to_syms (SynExt.Brk i :: xsyms) =
   220               apfst (cons (Break i)) (xsyms_to_syms xsyms)
   221           | xsyms_to_syms (SynExt.En :: xsyms) = ([], xsyms)
   222           | xsyms_to_syms [] = ([], []);
   223 
   224         fun nargs (Arg _ :: syms) = nargs syms + 1
   225           | nargs (TypArg _ :: syms) = nargs syms + 1
   226           | nargs (String _ :: syms) = nargs syms
   227           | nargs (Space _ :: syms) = nargs syms
   228           | nargs (Break _ :: syms) = nargs syms
   229           | nargs (Block (_, bsyms) :: syms) = nargs syms + nargs bsyms
   230           | nargs [] = 0;
   231       in
   232         (case xsyms_to_syms xsymbs of
   233           (symbs, []) => SOME (const, (symbs, nargs symbs, pri))
   234         | _ => sys_error "xprod_to_fmt: unbalanced blocks")
   235       end;
   236 
   237 
   238 (* empty, extend, merge prtabs *)
   239 
   240 val empty_prtabs = [];
   241 
   242 fun change_prtabs f mode xprods prtabs =
   243   let
   244     val fmts = map_filter xprod_to_fmt xprods;
   245     val tab = fold f fmts (mode_tab prtabs mode);
   246   in AList.update (op =) (mode, tab) prtabs end;
   247 
   248 fun extend_prtabs m = change_prtabs Symtab.update_list m;
   249 fun remove_prtabs m = change_prtabs (Symtab.remove_list (op =)) m;
   250 
   251 fun merge_prtabs prtabs1 prtabs2 =
   252   let
   253     val modes = distinct (op =) (map fst (prtabs1 @ prtabs2));
   254     fun merge m = (m, Symtab.merge_list (op =) (mode_tab prtabs1 m, mode_tab prtabs2 m));
   255   in map merge modes end;
   256 
   257 
   258 
   259 (** pretty term or typ asts **)
   260 
   261 fun is_chain [Block (_, pr)] = is_chain pr
   262   | is_chain [Arg _] = true
   263   | is_chain _  = false;
   264 
   265 fun const_markup c = Pretty.markup (Markup.const c) o single;
   266 
   267 fun pretty extern_const ctxt tabs trf tokentrf type_mode curried ast0 p0 =
   268   let
   269     fun token_trans c [Ast.Variable x] =
   270           (case tokentrf c of
   271             NONE => NONE
   272           | SOME f => SOME (f x))
   273       | token_trans _ _ = NONE;
   274 
   275     (*default applications: prefix / postfix*)
   276     val appT =
   277       if type_mode then TypeExt.tappl_ast_tr'
   278       else if curried then SynTrans.applC_ast_tr'
   279       else SynTrans.appl_ast_tr';
   280 
   281     fun synT _ ([], args) = ([], args)
   282       | synT markup (Arg p :: symbs, t :: args) =
   283           let val (Ts, args') = synT markup (symbs, args);
   284           in (astT (t, p) @ Ts, args') end
   285       | synT markup (TypArg p :: symbs, t :: args) =
   286           let
   287             val (Ts, args') = synT markup (symbs, args);
   288           in
   289             if type_mode then (astT (t, p) @ Ts, args')
   290             else (pretty I ctxt tabs trf tokentrf true curried t p @ Ts, args')
   291           end
   292       | synT markup (String s :: symbs, args) =
   293           let val (Ts, args') = synT markup (symbs, args);
   294           in (markup (Pretty.str s) :: Ts, args') end
   295       | synT markup (Space s :: symbs, args) =
   296           let val (Ts, args') = synT markup (symbs, args);
   297           in (Pretty.str s :: Ts, args') end
   298       | synT markup (Block (i, bsymbs) :: symbs, args) =
   299           let
   300             val (bTs, args') = synT markup (bsymbs, args);
   301             val (Ts, args'') = synT markup (symbs, args');
   302             val T =
   303               if i < 0 then Pretty.unbreakable (Pretty.block bTs)
   304               else Pretty.blk (i, bTs);
   305           in (T :: Ts, args'') end
   306       | synT markup (Break i :: symbs, args) =
   307           let
   308             val (Ts, args') = synT markup (symbs, args);
   309             val T = if i < 0 then Pretty.fbrk else Pretty.brk i;
   310           in (T :: Ts, args') end
   311       | synT _ (_ :: _, []) = sys_error "synT"
   312 
   313     and parT markup (pr, args, p, p': int) = #1 (synT markup
   314           (if p > p' orelse
   315             (! show_brackets andalso p' <> SynExt.max_pri andalso not (is_chain pr))
   316             then [Block (1, Space "(" :: pr @ [Space ")"])]
   317             else pr, args))
   318 
   319     and atomT a =
   320       (case try (unprefix Lexicon.constN) a of
   321         SOME c => const_markup c (Pretty.str (extern_const c))
   322       | NONE =>
   323           (case try (unprefix Lexicon.fixedN) a of
   324             SOME x =>
   325               (case tokentrf "_free" of
   326                 SOME f => Pretty.raw_str (f x)
   327               | NONE => Pretty.str x)
   328           | NONE => Pretty.str a))
   329 
   330     and prefixT (_, a, [], _) = [atomT a]
   331       | prefixT (c, _, args, p) = astT (appT (c, args), p)
   332 
   333     and splitT 0 ([x], ys) = (x, ys)
   334       | splitT 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys)
   335       | splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
   336       | splitT _ _ = sys_error "splitT"
   337 
   338     and combT (tup as (c, a, args, p)) =
   339       let
   340         val nargs = length args;
   341         val markup = const_markup (unprefix Lexicon.constN a)
   342           handle Fail _ => I;
   343 
   344         (*find matching table entry, or print as prefix / postfix*)
   345         fun prnt ([], []) = prefixT tup
   346           | prnt ([], tb :: tbs) = prnt (Symtab.lookup_list tb a, tbs)
   347           | prnt ((pr, n, p') :: prnps, tbs) =
   348               if nargs = n then parT markup (pr, args, p, p')
   349               else if nargs > n andalso not type_mode then
   350                 astT (appT (splitT n ([c], args)), p)
   351               else prnt (prnps, tbs);
   352       in
   353         (case token_trans a args of     (*try token translation function*)
   354           SOME s => [Pretty.raw_str s]
   355         | NONE =>                       (*try print translation functions*)
   356             astT (apply_trans ctxt (trf a) args, p) handle Match => prnt ([], tabs))
   357       end
   358 
   359     and astT (c as Ast.Constant a, p) = combT (c, a, [], p)
   360       | astT (Ast.Variable x, _) = [Pretty.str x]
   361       | astT (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combT (c, a, args, p)
   362       | astT (Ast.Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
   363       | astT (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]);
   364   in astT (ast0, p0) end;
   365 
   366 
   367 (* pretty_term_ast *)
   368 
   369 fun pretty_term_ast extern_const ctxt curried prtabs trf tokentrf ast =
   370   pretty extern_const ctxt (mode_tabs prtabs (! print_mode))
   371     trf tokentrf false curried ast 0;
   372 
   373 
   374 (* pretty_typ_ast *)
   375 
   376 fun pretty_typ_ast ctxt _ prtabs trf tokentrf ast =
   377   pretty I ctxt (mode_tabs prtabs (! print_mode))
   378     trf tokentrf true false ast 0;
   379 
   380 end;