src/Pure/Syntax/printer.ML
author wenzelm
Mon, 04 Oct 1993 15:30:49 +0100
changeset 18 c9ec452ff08f
parent 0 a5a9c433f639
child 42 d981488bda7b
permissions -rw-r--r--
lots of internal cleaning and tuning;
removed {parse,print}_{pre,post}_proc;
new lexer: now human readable due to scanner combinators;
new parser installed, but still inactive (due to grammar ambiguities);
added Syntax.test_read;
typ_of_term: sorts now made distinct and sorted;
mixfix: added forced line breaks (//);
PROP now printed before subterm of type prop with non-const head;
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 =
clasohm@0
     9
sig
clasohm@0
    10
  val show_types: bool ref
clasohm@0
    11
  val show_sorts: bool ref
clasohm@0
    12
end;
clasohm@0
    13
clasohm@0
    14
signature PRINTER =
clasohm@0
    15
sig
clasohm@0
    16
  include PRINTER0
clasohm@0
    17
  structure Symtab: SYMTAB
clasohm@0
    18
  structure XGram: XGRAM
clasohm@0
    19
  structure Pretty: PRETTY
clasohm@0
    20
  local open XGram XGram.Ast in
clasohm@0
    21
    val term_to_ast: (string -> (term list -> term) option) -> term -> ast
clasohm@0
    22
    val typ_to_ast: (string -> (term list -> term) option) -> typ -> ast
wenzelm@18
    23
    type prtab
wenzelm@18
    24
    val empty_prtab: prtab
wenzelm@18
    25
    val extend_prtab: prtab -> (string prod list) -> (string * (ast list -> ast)) list
wenzelm@18
    26
      -> prtab
wenzelm@18
    27
    val mk_prtab: (string prod list) -> (string * (ast list -> ast)) list -> prtab
wenzelm@18
    28
    val pretty_term_ast: prtab -> ast -> Pretty.T
wenzelm@18
    29
    val pretty_typ_ast: prtab -> ast -> Pretty.T
clasohm@0
    30
  end
clasohm@0
    31
end;
clasohm@0
    32
clasohm@0
    33
functor PrinterFun(structure Symtab: SYMTAB and Lexicon: LEXICON
clasohm@0
    34
  and TypeExt: TYPE_EXT and SExtension: SEXTENSION and Pretty: PRETTY
clasohm@0
    35
  sharing TypeExt.Extension = SExtension.Extension) (*: PRINTER *) =  (* FIXME *)
clasohm@0
    36
struct
clasohm@0
    37
clasohm@0
    38
structure Symtab = Symtab;
clasohm@0
    39
structure Extension = TypeExt.Extension;
clasohm@0
    40
structure XGram = Extension.XGram;
clasohm@0
    41
structure Pretty = Pretty;
clasohm@0
    42
open XGram XGram.Ast Lexicon TypeExt Extension SExtension;
clasohm@0
    43
clasohm@0
    44
clasohm@0
    45
(** options for printing **)
clasohm@0
    46
clasohm@0
    47
val show_types = ref false;
clasohm@0
    48
val show_sorts = ref false;
clasohm@0
    49
clasohm@0
    50
clasohm@0
    51
wenzelm@18
    52
(** convert term or typ to ast **)
clasohm@0
    53
wenzelm@18
    54
fun apply_trans name a f args =
wenzelm@18
    55
  (f args handle
clasohm@0
    56
    Match => raise Match
wenzelm@18
    57
  | exn => (writeln ("Error in " ^ name ^ " for " ^ quote a); raise exn));
clasohm@0
    58
clasohm@0
    59
clasohm@0
    60
fun ast_of_term trf show_types show_sorts tm =
clasohm@0
    61
  let
wenzelm@18
    62
(*(* FIXME old - remove *)
wenzelm@18
    63
    fun fix_aprop tys (Abs (x, ty, t)) = Abs (x, ty, fix_aprop (ty :: tys) t)
wenzelm@18
    64
      | fix_aprop tys t =
wenzelm@18
    65
          let
wenzelm@18
    66
            val (f, args) = strip_comb t;
wenzelm@18
    67
            val t' = list_comb (f, map (fix_aprop tys) args);
wenzelm@18
    68
          in
wenzelm@18
    69
            if not (is_Const f) andalso fastype_of (tys, t) = propT
wenzelm@18
    70
            then Const (apropC, dummyT) $ t' else t'
wenzelm@18
    71
          end;
wenzelm@18
    72
clasohm@0
    73
    val aprop_const = Const (apropC, dummyT);
clasohm@0
    74
clasohm@0
    75
    fun fix_aprop (tm as Const _) = tm
clasohm@0
    76
      | fix_aprop (tm as Free (x, ty)) =
clasohm@0
    77
          if ty = propT then aprop_const $ Free (x, dummyT) else tm
clasohm@0
    78
      | fix_aprop (tm as Var (xi, ty)) =
clasohm@0
    79
          if ty = propT then aprop_const $ Var (xi, dummyT) else tm
clasohm@0
    80
      | fix_aprop (tm as Bound _) = tm
clasohm@0
    81
      | fix_aprop (Abs (x, ty, t)) = Abs (x, ty, fix_aprop t)
clasohm@0
    82
      | fix_aprop (t1 $ t2) = fix_aprop t1 $ fix_aprop t2;
clasohm@0
    83
wenzelm@18
    84
    val fix_aprop = fn _ => fn tm => fix_aprop tm;
wenzelm@18
    85
*)
wenzelm@18
    86
wenzelm@18
    87
    (* FIXME check *)
wenzelm@18
    88
wenzelm@18
    89
    fun aprop t = Const (apropC, dummyT) $ t;
wenzelm@18
    90
wenzelm@18
    91
    fun is_prop tys tm =
wenzelm@18
    92
      fastype_of (tys, tm) = propT handle TERM _ => false;
wenzelm@18
    93
wenzelm@18
    94
    fun fix_aprop _ (tm as Const _) = tm
wenzelm@18
    95
      | fix_aprop _ (tm as Free (x, ty)) =
wenzelm@18
    96
          if ty = propT then aprop (Free (x, dummyT)) else tm
wenzelm@18
    97
      | fix_aprop _ (tm as Var (xi, ty)) =
wenzelm@18
    98
          if ty = propT then aprop (Var (xi, dummyT)) else tm
wenzelm@18
    99
      | fix_aprop tys (tm as Bound _) =
wenzelm@18
   100
          if is_prop tys tm then aprop tm else tm
wenzelm@18
   101
      | fix_aprop tys (Abs (x, ty, t)) = Abs (x, ty, fix_aprop (ty :: tys) t)
wenzelm@18
   102
      | fix_aprop tys (tm as t1 $ t2) =
wenzelm@18
   103
          (if is_Const (head_of tm) orelse not (is_prop tys tm)
wenzelm@18
   104
            then I else aprop) (fix_aprop tys t1 $ fix_aprop tys t2);
wenzelm@18
   105
clasohm@0
   106
clasohm@0
   107
    fun prune_typs (t_seen as (Const _, _)) = t_seen
clasohm@0
   108
      | prune_typs (t as Free (x, ty), seen) =
clasohm@0
   109
          if ty = dummyT then (t, seen)
clasohm@0
   110
          else if t mem seen then (Free (x, dummyT), seen)
clasohm@0
   111
          else (t, t :: seen)
clasohm@0
   112
      | prune_typs (t as Var (xi, ty), seen) =
clasohm@0
   113
          if ty = dummyT then (t, seen)
clasohm@0
   114
          else if t mem seen then (Var (xi, dummyT), seen)
clasohm@0
   115
          else (t, t :: seen)
clasohm@0
   116
      | prune_typs (t_seen as (Bound _, _)) = t_seen
clasohm@0
   117
      | prune_typs (Abs (x, ty, t), seen) =
clasohm@0
   118
          let val (t', seen') = prune_typs (t, seen);
clasohm@0
   119
          in (Abs (x, ty, t'), seen') end
clasohm@0
   120
      | prune_typs (t1 $ t2, seen) =
clasohm@0
   121
          let
clasohm@0
   122
            val (t1', seen') = prune_typs (t1, seen);
clasohm@0
   123
            val (t2', seen'') = prune_typs (t2, seen');
clasohm@0
   124
          in
clasohm@0
   125
            (t1' $ t2', seen'')
clasohm@0
   126
          end;
clasohm@0
   127
clasohm@0
   128
clasohm@0
   129
    fun ast_of (Const (a, _)) = trans a []
clasohm@0
   130
      | ast_of (Free (x, ty)) = constrain x (Free (x, dummyT)) ty
clasohm@0
   131
      | ast_of (Var (xi, ty)) = constrain (string_of_vname xi) (Var (xi, dummyT)) ty
clasohm@0
   132
      | ast_of (Bound i) = Variable ("B." ^ string_of_int i)
clasohm@0
   133
      | ast_of (t as Abs _) = ast_of (abs_tr' t)
clasohm@0
   134
      | ast_of (t as _ $ _) =
clasohm@0
   135
          (case strip_comb t of
clasohm@0
   136
            (Const (a, _), args) => trans a args
clasohm@0
   137
          | (f, args) => Appl (map ast_of (f :: args)))
clasohm@0
   138
clasohm@0
   139
    and trans a args =
clasohm@0
   140
      (case trf a of
wenzelm@18
   141
        Some f => ast_of (apply_trans "print translation" a f args)
clasohm@0
   142
      | None => raise Match)
clasohm@0
   143
          handle Match => mk_appl (Constant a) (map ast_of args)
clasohm@0
   144
clasohm@0
   145
    and constrain x t ty =
clasohm@0
   146
      if show_types andalso ty <> dummyT then
clasohm@0
   147
        ast_of (Const (constrainC, dummyT) $ t $ term_of_typ show_sorts ty)
clasohm@0
   148
      else Variable x;
clasohm@0
   149
  in
wenzelm@18
   150
    if show_types then ast_of (#1 (prune_typs (fix_aprop [] tm, [])))
wenzelm@18
   151
    else ast_of (fix_aprop [] tm)
clasohm@0
   152
  end;
clasohm@0
   153
clasohm@0
   154
clasohm@0
   155
(* term_to_ast *)
clasohm@0
   156
clasohm@0
   157
fun term_to_ast trf tm =
clasohm@0
   158
  ast_of_term trf (! show_types) (! show_sorts) tm;
clasohm@0
   159
clasohm@0
   160
clasohm@0
   161
(* typ_to_ast *)
clasohm@0
   162
clasohm@0
   163
fun typ_to_ast trf ty =
clasohm@0
   164
  ast_of_term trf false false (term_of_typ (! show_sorts) ty);
clasohm@0
   165
clasohm@0
   166
clasohm@0
   167
wenzelm@18
   168
(** type prtab **)
clasohm@0
   169
clasohm@0
   170
datatype symb =
clasohm@0
   171
  Arg of int |
clasohm@0
   172
  TypArg of int |
clasohm@0
   173
  String of string |
clasohm@0
   174
  Break of int |
clasohm@0
   175
  Block of int * symb list;
clasohm@0
   176
clasohm@0
   177
datatype format =
clasohm@0
   178
  Prnt of symb list * int * int |
clasohm@0
   179
  Trns of ast list -> ast |
clasohm@0
   180
  TorP of (ast list -> ast) * (symb list * int * int);
clasohm@0
   181
wenzelm@18
   182
type prtab = format Symtab.table;
clasohm@0
   183
clasohm@0
   184
wenzelm@18
   185
(* empty_prtab *)
clasohm@0
   186
wenzelm@18
   187
val empty_prtab = Symtab.null;
clasohm@0
   188
wenzelm@18
   189
wenzelm@18
   190
wenzelm@18
   191
(** extend_prtab **)
wenzelm@18
   192
wenzelm@18
   193
fun extend_prtab prtab prods trfuns =
clasohm@0
   194
  let
clasohm@0
   195
    fun nargs (Arg _ :: symbs) = nargs symbs + 1
clasohm@0
   196
      | nargs (TypArg _ :: symbs) = nargs symbs + 1
clasohm@0
   197
      | nargs (String _ :: symbs) = nargs symbs
clasohm@0
   198
      | nargs (Break _ :: symbs) = nargs symbs
clasohm@0
   199
      | nargs (Block (_, bsymbs) :: symbs) = nargs symbs + nargs bsymbs
clasohm@0
   200
      | nargs [] = 0;
clasohm@0
   201
clasohm@0
   202
    fun merge (s, String s' :: l) = String (s ^ s') :: l
clasohm@0
   203
      | merge (s, l) = String s :: l;
clasohm@0
   204
wenzelm@18
   205
    fun mk_prnp sy c p =
clasohm@0
   206
      let
wenzelm@18
   207
        val constr = (c = constrainC orelse c = constrainIdtC);
clasohm@0
   208
clasohm@0
   209
        fun syn2pr (Terminal s :: sy) =
wenzelm@18
   210
              let val (symbs, sy') = syn2pr sy;
clasohm@0
   211
              in (merge (s, symbs), sy') end
clasohm@0
   212
          | syn2pr (Space s :: sy) =
wenzelm@18
   213
              let val (symbs, sy') = syn2pr sy;
clasohm@0
   214
              in (merge (s, symbs), sy') end
clasohm@0
   215
          | syn2pr (Nonterminal (s, p) :: sy) =
clasohm@0
   216
              let
clasohm@0
   217
                val (symbs, sy') = syn2pr sy;
clasohm@0
   218
                val symb =
clasohm@0
   219
                  (if constr andalso s = "type" then TypArg else Arg)
clasohm@0
   220
                    (if is_terminal s then 0 else p);
clasohm@0
   221
              in (symb :: symbs, sy') end
clasohm@0
   222
          | syn2pr (Bg i :: sy) =
clasohm@0
   223
              let
clasohm@0
   224
                val (bsymbs, sy') = syn2pr sy;
clasohm@0
   225
                val (symbs, sy'') = syn2pr sy';
clasohm@0
   226
              in (Block (i, bsymbs) :: symbs, sy'') end
clasohm@0
   227
          | syn2pr (Brk i :: sy) =
wenzelm@18
   228
              let val (symbs, sy') = syn2pr sy;
clasohm@0
   229
              in (Break i :: symbs, sy') end
clasohm@0
   230
          | syn2pr (En :: sy) = ([], sy)
clasohm@0
   231
          | syn2pr [] = ([], []);
clasohm@0
   232
clasohm@0
   233
        val (pr, _) = syn2pr sy;
clasohm@0
   234
      in
clasohm@0
   235
        (pr, nargs pr, p)
clasohm@0
   236
      end;
clasohm@0
   237
wenzelm@18
   238
    fun trns_err c = error ("More than one parse ast translation for " ^ quote c);
clasohm@0
   239
wenzelm@18
   240
    fun add_fmt tab c fmt x = Symtab.update ((c, fmt x), tab);
wenzelm@18
   241
wenzelm@18
   242
    fun add_prod (tab, Prod (_, _, "", _)) = tab
wenzelm@18
   243
      | add_prod (tab, Prod (_, sy, c, p)) =
wenzelm@18
   244
          (case Symtab.lookup (tab, c) of
wenzelm@18
   245
            None => add_fmt tab c Prnt (mk_prnp sy c p)
wenzelm@18
   246
          | Some (Prnt _) => add_fmt tab c Prnt (mk_prnp sy c p)
wenzelm@18
   247
          | Some (Trns f) => add_fmt tab c TorP (f, mk_prnp sy c p)
wenzelm@18
   248
          | Some (TorP (f, _)) => add_fmt tab c TorP (f, mk_prnp sy c p));
wenzelm@18
   249
wenzelm@18
   250
    fun add_tr (tab, (c, f)) =
wenzelm@18
   251
      (case Symtab.lookup (tab, c) of
wenzelm@18
   252
        None => add_fmt tab c Trns f
wenzelm@18
   253
      | Some (Prnt pr) => add_fmt tab c TorP (f, pr)
wenzelm@18
   254
      | Some (Trns _) => trns_err c
wenzelm@18
   255
      | Some (TorP _) => trns_err c);
clasohm@0
   256
  in
wenzelm@18
   257
    Symtab.balance (foldl add_prod (foldl add_tr (prtab, trfuns), prods))
clasohm@0
   258
  end;
clasohm@0
   259
clasohm@0
   260
wenzelm@18
   261
(* mk_prtab *)
clasohm@0
   262
wenzelm@18
   263
val mk_prtab = extend_prtab empty_prtab;
clasohm@0
   264
clasohm@0
   265
wenzelm@18
   266
wenzelm@18
   267
(** pretty term/typ asts **)
wenzelm@18
   268
wenzelm@18
   269
(*pretty assumes a syntax derived from Pure, otherwise it may loop forever*)
wenzelm@18
   270
wenzelm@18
   271
fun pretty tab type_mode ast0 p0 =
clasohm@0
   272
  let
wenzelm@18
   273
    val trans = apply_trans "print ast translation";
wenzelm@18
   274
wenzelm@18
   275
    val appT = if type_mode then tappl_ast_tr' else appl_ast_tr';
wenzelm@18
   276
clasohm@0
   277
    fun synT ([], args) = ([], args)
clasohm@0
   278
      | synT (Arg p :: symbs, t :: args) =
wenzelm@18
   279
          let val (Ts, args') = synT (symbs, args);
clasohm@0
   280
          in (astT (t, p) @ Ts, args') end
clasohm@0
   281
      | synT (TypArg p :: symbs, t :: args) =
wenzelm@18
   282
          let val (Ts, args') = synT (symbs, args);
wenzelm@18
   283
          in (pretty tab true t p @ Ts, args') end
clasohm@0
   284
      | synT (String s :: symbs, args) =
wenzelm@18
   285
          let val (Ts, args') = synT (symbs, args);
clasohm@0
   286
          in (Pretty.str s :: Ts, args') end
clasohm@0
   287
      | synT (Block (i, bsymbs) :: symbs, args) =
clasohm@0
   288
          let
clasohm@0
   289
            val (bTs, args') = synT (bsymbs, args);
clasohm@0
   290
            val (Ts, args'') = synT (symbs, args');
clasohm@0
   291
          in (Pretty.blk (i, bTs) :: Ts, args'') end
clasohm@0
   292
      | synT (Break i :: symbs, args) =
wenzelm@18
   293
          let val (Ts, args') = synT (symbs, args);
wenzelm@18
   294
          in ((if i < 0 then Pretty.fbrk else Pretty.brk i) :: Ts, args') end
clasohm@0
   295
      | synT (_ :: _, []) = error "synT"
clasohm@0
   296
clasohm@0
   297
    and parT (pr, args, p, p': int) =
wenzelm@18
   298
      if p > p' then
wenzelm@18
   299
        #1 (synT ([Block (1, String "(" :: pr @ [String ")"])], args))
wenzelm@18
   300
      else #1 (synT (pr, args))
clasohm@0
   301
clasohm@0
   302
    and prefixT (_, a, [], _) = [Pretty.str a]
clasohm@0
   303
      | prefixT (c, _, args, p) = astT (appT (c, args), p)
clasohm@0
   304
wenzelm@18
   305
    and splitT 0 ([x], ys) = (x, ys)
wenzelm@18
   306
      | splitT 0 (rev_xs, ys) = (Appl (rev rev_xs), ys)
wenzelm@18
   307
      | splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
wenzelm@18
   308
      | splitT _ _ = error "splitT"
wenzelm@18
   309
clasohm@0
   310
    and combT (tup as (c, a, args, p)) =
clasohm@0
   311
      let
clasohm@0
   312
        val nargs = length args;
wenzelm@18
   313
clasohm@0
   314
        fun prnt (pr, n, p') =
wenzelm@18
   315
          if nargs = n then parT (pr, args, p, p')
wenzelm@18
   316
          else if nargs < n orelse type_mode then prefixT tup
wenzelm@18
   317
          else astT (appT (splitT n ([c], args)), p);
clasohm@0
   318
      in
wenzelm@18
   319
        (case Symtab.lookup (tab, a) of
clasohm@0
   320
          None => prefixT tup
clasohm@0
   321
        | Some (Prnt prnp) => prnt prnp
clasohm@0
   322
        | Some (Trns f) =>
wenzelm@18
   323
            (astT (trans a f args, p) handle Match => prefixT tup)
clasohm@0
   324
        | Some (TorP (f, prnp)) =>
wenzelm@18
   325
            (astT (trans a f args, p) handle Match => prnt prnp))
clasohm@0
   326
      end
clasohm@0
   327
clasohm@0
   328
    and astT (c as Constant a, p) = combT (c, a, [], p)
clasohm@0
   329
      | astT (Variable x, _) = [Pretty.str x]
wenzelm@18
   330
      | astT (Appl ((c as Constant a) :: (args as _ :: _)), p) =
wenzelm@18
   331
          combT (c, a, args, p)
clasohm@0
   332
      | astT (Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
wenzelm@18
   333
      | astT (ast as Appl _, _) = raise_ast "pretty: malformed ast" [ast];
clasohm@0
   334
  in
clasohm@0
   335
    astT (ast0, p0)
clasohm@0
   336
  end;
clasohm@0
   337
clasohm@0
   338
clasohm@0
   339
(* pretty_term_ast *)
clasohm@0
   340
clasohm@0
   341
fun pretty_term_ast tab ast =
wenzelm@18
   342
  Pretty.blk (0, pretty tab false ast 0);
clasohm@0
   343
clasohm@0
   344
clasohm@0
   345
(* pretty_typ_ast *)
clasohm@0
   346
clasohm@0
   347
fun pretty_typ_ast tab ast =
wenzelm@18
   348
  Pretty.blk (0, pretty tab true ast 0);
clasohm@0
   349
clasohm@0
   350
clasohm@0
   351
end;
clasohm@0
   352