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