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