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