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