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