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