src/Pure/Syntax/printer.ML
author wenzelm
Fri Jun 13 21:04:42 2008 +0200 (2008-06-13)
changeset 27195 bbf4cbc69243
parent 26707 ddf6bab64b96
child 29565 3f8b24fcfbd6
permissions -rw-r--r--
map_const: soft version, no failure here;
     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 update_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 -> (Proof.context -> string -> Pretty.T) 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 -> (Proof.context -> string -> Pretty.T) 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 update_prtabs mode xprods prtabs =
   243   let
   244     val fmts = map_filter xprod_to_fmt xprods;
   245     val tab' = fold (Symtab.update_list (op =)) fmts (mode_tab prtabs mode);
   246   in AList.update (op =) (mode, tab') prtabs end;
   247 
   248 fun remove_prtabs mode xprods prtabs =
   249   let
   250     val tab = mode_tab prtabs mode;
   251     val fmts = map_filter (fn xprod as SynExt.XProd (_, _, c, _) =>
   252       if null (Symtab.lookup_list tab c) then NONE
   253       else xprod_to_fmt xprod) xprods;
   254     val tab' = fold (Symtab.remove_list (op =)) fmts tab;
   255   in AList.update (op =) (mode, tab') prtabs end;
   256 
   257 fun merge_prtabs prtabs1 prtabs2 =
   258   let
   259     val modes = distinct (op =) (map fst (prtabs1 @ prtabs2));
   260     fun merge m = (m, Symtab.merge_list (op =) (mode_tab prtabs1 m, mode_tab prtabs2 m));
   261   in map merge modes end;
   262 
   263 
   264 
   265 (** pretty term or typ asts **)
   266 
   267 fun is_chain [Block (_, pr)] = is_chain pr
   268   | is_chain [Arg _] = true
   269   | is_chain _  = false;
   270 
   271 fun pretty extern_const ctxt tabs trf tokentrf type_mode curried ast0 p0 =
   272   let
   273     fun token_trans a x =
   274       (case tokentrf a of
   275         NONE =>
   276           if member (op =) SynExt.standard_token_classes a
   277           then SOME (Pretty.str x) else NONE
   278       | SOME f => SOME (f ctxt x));
   279 
   280     (*default applications: prefix / postfix*)
   281     val appT =
   282       if type_mode then TypeExt.tappl_ast_tr'
   283       else if curried then SynTrans.applC_ast_tr'
   284       else SynTrans.appl_ast_tr';
   285 
   286     fun synT _ ([], args) = ([], args)
   287       | synT markup (Arg p :: symbs, t :: args) =
   288           let val (Ts, args') = synT markup (symbs, args);
   289           in (astT (t, p) @ Ts, args') end
   290       | synT markup (TypArg p :: symbs, t :: args) =
   291           let
   292             val (Ts, args') = synT markup (symbs, args);
   293           in
   294             if type_mode then (astT (t, p) @ Ts, args')
   295             else (pretty I ctxt tabs trf tokentrf true curried t p @ Ts, args')
   296           end
   297       | synT markup (String s :: symbs, args) =
   298           let val (Ts, args') = synT markup (symbs, args);
   299           in (markup (Pretty.str s) :: Ts, args') end
   300       | synT markup (Space s :: symbs, args) =
   301           let val (Ts, args') = synT markup (symbs, args);
   302           in (Pretty.str s :: Ts, args') end
   303       | synT markup (Block (i, bsymbs) :: symbs, args) =
   304           let
   305             val (bTs, args') = synT markup (bsymbs, args);
   306             val (Ts, args'') = synT markup (symbs, args');
   307             val T =
   308               if i < 0 then Pretty.unbreakable (Pretty.block bTs)
   309               else Pretty.blk (i, bTs);
   310           in (T :: Ts, args'') end
   311       | synT markup (Break i :: symbs, args) =
   312           let
   313             val (Ts, args') = synT markup (symbs, args);
   314             val T = if i < 0 then Pretty.fbrk else Pretty.brk i;
   315           in (T :: Ts, args') end
   316       | synT _ (_ :: _, []) = sys_error "synT"
   317 
   318     and parT markup (pr, args, p, p': int) = #1 (synT markup
   319           (if p > p' orelse
   320             (! show_brackets andalso p' <> SynExt.max_pri andalso not (is_chain pr))
   321             then [Block (1, Space "(" :: pr @ [Space ")"])]
   322             else pr, args))
   323 
   324     and atomT a =
   325       (case try (unprefix Lexicon.constN) a of
   326         SOME c => Pretty.mark (Markup.const c) (Pretty.str (extern_const c))
   327       | NONE =>
   328           (case try (unprefix Lexicon.fixedN) a of
   329             SOME x => the (token_trans "_free" x)
   330           | NONE => Pretty.str a))
   331 
   332     and prefixT (_, a, [], _) = [atomT a]
   333       | prefixT (c, _, args, p) = astT (appT (c, args), p)
   334 
   335     and splitT 0 ([x], ys) = (x, ys)
   336       | splitT 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys)
   337       | splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
   338       | splitT _ _ = sys_error "splitT"
   339 
   340     and combT (tup as (c, a, args, p)) =
   341       let
   342         val nargs = length args;
   343         val markup = Pretty.mark
   344           (Markup.const (unprefix Lexicon.constN a) handle Fail _ =>
   345             (Markup.fixed (unprefix Lexicon.fixedN a)))
   346           handle Fail _ => I;
   347 
   348         (*find matching table entry, or print as prefix / postfix*)
   349         fun prnt ([], []) = prefixT tup
   350           | prnt ([], tb :: tbs) = prnt (Symtab.lookup_list tb a, tbs)
   351           | prnt ((pr, n, p') :: prnps, tbs) =
   352               if nargs = n then parT markup (pr, args, p, p')
   353               else if nargs > n andalso not type_mode then
   354                 astT (appT (splitT n ([c], args)), p)
   355               else prnt (prnps, tbs);
   356       in
   357         (case tokentrT a args of
   358           SOME prt => [prt]
   359         | NONE => astT (apply_trans ctxt (trf a) args, p) handle Match => prnt ([], tabs))
   360       end
   361 
   362     and tokentrT a [Ast.Variable x] = token_trans a x
   363       | tokentrT _ _ = NONE
   364 
   365     and astT (c as Ast.Constant a, p) = combT (c, a, [], p)
   366       | astT (Ast.Variable x, _) = [Pretty.str x]
   367       | astT (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combT (c, a, args, p)
   368       | astT (Ast.Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
   369       | astT (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]);
   370   in astT (ast0, p0) end;
   371 
   372 
   373 (* pretty_term_ast *)
   374 
   375 fun pretty_term_ast extern_const ctxt curried prtabs trf tokentrf ast =
   376   pretty extern_const ctxt (mode_tabs prtabs (print_mode_value ()))
   377     trf tokentrf false curried ast 0;
   378 
   379 
   380 (* pretty_typ_ast *)
   381 
   382 fun pretty_typ_ast ctxt _ prtabs trf tokentrf ast =
   383   pretty I ctxt (mode_tabs prtabs (print_mode_value ()))
   384     trf tokentrf true false ast 0;
   385 
   386 end;