src/Pure/Syntax/printer.ML
author wenzelm
Wed Oct 31 21:59:07 2001 +0100 (2001-10-31)
changeset 12004 1703de633aaf
parent 11795 12a0fb3ac366
child 12252 835fef0fac51
permissions -rw-r--r--
IsarThy.theorem_i: no locale;
     1 (*  Title:      Pure/Syntax/printer.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow and Markus Wenzel, TU Muenchen
     4 
     5 Pretty printing of asts, terms, types and print (ast) translation.
     6 *)
     7 
     8 signature PRINTER0 =
     9 sig
    10   val show_brackets: bool ref
    11   val show_sorts: bool ref
    12   val show_types: bool ref
    13   val show_no_free_types: bool ref
    14 end;
    15 
    16 signature PRINTER =
    17 sig
    18   include PRINTER0
    19   val term_to_ast: (string -> (bool -> typ -> term list -> term) list) -> term -> Ast.ast
    20   val typ_to_ast: (string -> (bool -> typ -> term list -> term) list) -> typ -> Ast.ast
    21   val sort_to_ast: (string -> (bool -> typ -> term list -> term) list) -> sort -> Ast.ast
    22   type prtabs
    23   val empty_prtabs: prtabs
    24   val extend_prtabs: prtabs -> string -> SynExt.xprod list -> prtabs
    25   val merge_prtabs: prtabs -> prtabs -> prtabs
    26   val pretty_term_ast: bool -> prtabs
    27     -> (string -> (Ast.ast list -> Ast.ast) list)
    28     -> (string -> (string -> string * real) option) -> Ast.ast -> Pretty.T
    29   val pretty_typ_ast: bool -> prtabs
    30     -> (string -> (Ast.ast list -> Ast.ast) list)
    31     -> (string -> (string -> string * real) option) -> Ast.ast -> Pretty.T
    32 end;
    33 
    34 structure Printer: PRINTER =
    35 struct
    36 
    37 
    38 (** options for printing **)
    39 
    40 val show_types = ref false;
    41 val show_sorts = ref false;
    42 val show_brackets = ref false;
    43 val show_no_free_types = ref false;
    44 
    45 
    46 
    47 (** misc utils **)
    48 
    49 (* apply print (ast) translation function *)
    50 
    51 fun apply_first [] x = raise Match
    52   | apply_first (f :: fs) x = f x handle Match => apply_first fs x;
    53 
    54 fun apply_trans name a fs args =
    55   (apply_first fs args handle
    56     Match => raise Match
    57   | exn => (priority ("Error in " ^ name ^ " for " ^ quote a); raise exn));
    58 
    59 fun apply_typed x y fs = map (fn f => f x y) fs;
    60 
    61 
    62 (* simple_ast_of *)
    63 
    64 fun simple_ast_of (Const (c, _)) = Ast.Constant c
    65   | simple_ast_of (Free (x, _)) = Ast.Variable x
    66   | simple_ast_of (Var (xi, _)) = Ast.Variable (Lexicon.string_of_vname xi)
    67   | simple_ast_of (t as _ $ _) =
    68       let val (f, args) = strip_comb t in
    69         Ast.mk_appl (simple_ast_of f) (map simple_ast_of args)
    70       end
    71   | simple_ast_of (Bound i) = Ast.Variable ("B." ^ string_of_int i)
    72   | simple_ast_of (Abs _) = sys_error "simple_ast_of: Abs";
    73 
    74 
    75 
    76 (** sort_to_ast, typ_to_ast **)
    77 
    78 fun ast_of_termT trf tm =
    79   let
    80     (* FIXME improve: lookup token classes *)
    81     fun ast_of (t as Const ("_class", _) $ Free _) = simple_ast_of t
    82       | ast_of (t as Const ("_tfree", _) $ Free _) = simple_ast_of t
    83       | ast_of (t as Const ("_tvar", _) $ Var _) = simple_ast_of t
    84       | ast_of (Const (a, _)) = trans a []
    85       | ast_of (t as _ $ _) =
    86           (case strip_comb t of
    87             (Const (a, _), args) => trans a args
    88           | (f, args) => Ast.Appl (map ast_of (f :: args)))
    89       | ast_of t = simple_ast_of t
    90     and trans a args =
    91       ast_of (apply_trans "print translation" a (apply_typed false dummyT (trf a)) args)
    92         handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args);
    93   in ast_of tm end;
    94 
    95 fun sort_to_ast trf S = ast_of_termT trf (TypeExt.term_of_sort S);
    96 fun typ_to_ast trf T = ast_of_termT trf (TypeExt.term_of_typ (! show_sorts) T);
    97 
    98 
    99 
   100 (** term_to_ast **)
   101 
   102 fun mark_freevars ((t as Const (c, _)) $ u) =
   103       if c mem_string TokenTrans.standard_token_markers then (t $ u)
   104       else t $ mark_freevars u
   105   | mark_freevars (t $ u) = mark_freevars t $ mark_freevars u
   106   | mark_freevars (Abs (x, T, t)) = Abs (x, T, mark_freevars t)
   107   | mark_freevars (t as Free _) = Lexicon.const "_free" $ t
   108   | mark_freevars (t as Var (xi, T)) =
   109       if xi = SynExt.dddot_indexname then Const ("_DDDOT", T)
   110       else Lexicon.const "_var" $ t
   111   | mark_freevars a = a;
   112 
   113 fun ast_of_term trf no_freeTs show_types show_sorts tm =
   114   let
   115     fun prune_typs (t_seen as (Const _, _)) = t_seen
   116       | prune_typs (t as Free (x, ty), seen) =
   117           if ty = dummyT then (t, seen)
   118           else if no_freeTs orelse t mem seen then (Lexicon.free x, seen)
   119           else (t, t :: seen)
   120       | prune_typs (t as Var (xi, ty), seen) =
   121           if ty = dummyT then (t, seen)
   122           else if no_freeTs orelse t mem seen then (Lexicon.var xi, seen)
   123           else (t, t :: seen)
   124       | prune_typs (t_seen as (Bound _, _)) = t_seen
   125       | prune_typs (Abs (x, ty, t), seen) =
   126           let val (t', seen') = prune_typs (t, seen);
   127           in (Abs (x, ty, t'), seen') end
   128       | prune_typs (t1 $ t2, seen) =
   129           let
   130             val (t1', seen') = prune_typs (t1, seen);
   131             val (t2', seen'') = prune_typs (t2, seen');
   132           in (t1' $ t2', seen'') end;
   133 
   134     fun ast_of tm =
   135       (case strip_comb tm of
   136         (t as Abs _, ts) => Ast.mk_appl (ast_of (SynTrans.abs_tr' t)) (map ast_of ts)
   137       | ((c as Const ("_free", _)), Free (x, T) :: ts) =>
   138           Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts)
   139       | ((c as Const ("_bound", _)), Free (x, T) :: ts) =>
   140           Ast.mk_appl (constrain (c $ Lexicon.free x) T) (map ast_of ts)
   141       | ((c as Const ("_var", _)), Var (xi, T) :: ts) =>
   142           Ast.mk_appl (constrain (c $ Lexicon.var xi) T) (map ast_of ts)
   143       | (Const (c, T), ts) => trans c T ts
   144       | (t, ts) => Ast.mk_appl (simple_ast_of t) (map ast_of ts))
   145 
   146     and trans a T args =
   147       ast_of (apply_trans "print translation" a (apply_typed show_sorts T (trf a)) args)
   148         handle Match => Ast.mk_appl (Ast.Constant a) (map ast_of args)
   149 
   150     and constrain t T =
   151       if show_types andalso T <> dummyT then
   152         Ast.Appl [Ast.Constant SynExt.constrainC, simple_ast_of t,
   153           ast_of_termT trf (TypeExt.term_of_typ show_sorts T)]
   154       else simple_ast_of t
   155   in
   156     tm
   157     |> SynTrans.prop_tr'
   158     |> (if show_types then #1 o prune_typs o rpair [] else I)
   159     |> mark_freevars
   160     |> ast_of
   161   end;
   162 
   163 fun term_to_ast trf tm =
   164   ast_of_term trf (! show_no_free_types) (! show_types orelse ! show_sorts)
   165     (! show_sorts) tm;
   166 
   167 
   168 
   169 (** type prtabs **)
   170 
   171 datatype symb =
   172   Arg of int |
   173   TypArg of int |
   174   String of string |
   175   Break of int |
   176   Block of int * symb list;
   177 
   178 type prtabs = (string * ((symb list * int * int) list) Symtab.table) list;
   179 
   180 
   181 (*find tab for mode*)
   182 fun get_tab prtabs mode =
   183   if_none (assoc (prtabs, mode)) Symtab.empty;
   184 
   185 (*collect tabs for mode hierarchy (default "")*)
   186 fun tabs_of prtabs modes =
   187   mapfilter (fn mode => assoc (prtabs, mode)) (modes @ [""]);
   188 
   189 
   190 (* xprod_to_fmt *)
   191 
   192 fun xprod_to_fmt (SynExt.XProd (_, _, "", _)) = None
   193   | xprod_to_fmt (SynExt.XProd (_, xsymbs, const, pri)) =
   194       let
   195         fun cons_str s (String s' :: syms) = String (s ^ s') :: syms
   196           | cons_str s syms = String s :: syms;
   197 
   198         fun arg (s, p) =
   199           (if s = "type" then TypArg else Arg)
   200           (if Lexicon.is_terminal s then SynExt.max_pri else p);
   201 
   202         fun xsyms_to_syms (SynExt.Delim s :: xsyms) =
   203               apfst (cons_str s) (xsyms_to_syms xsyms)
   204           | xsyms_to_syms (SynExt.Argument s_p :: xsyms) =
   205               apfst (cons (arg s_p)) (xsyms_to_syms xsyms)
   206           | xsyms_to_syms (SynExt.Space s :: xsyms) =
   207               apfst (cons_str s) (xsyms_to_syms xsyms)
   208           | xsyms_to_syms (SynExt.Bg i :: xsyms) =
   209               let
   210                 val (bsyms, xsyms') = xsyms_to_syms xsyms;
   211                 val (syms, xsyms'') = xsyms_to_syms xsyms';
   212               in
   213                 (Block (i, bsyms) :: syms, xsyms'')
   214               end
   215           | xsyms_to_syms (SynExt.Brk i :: xsyms) =
   216               apfst (cons (Break i)) (xsyms_to_syms xsyms)
   217           | xsyms_to_syms (SynExt.En :: xsyms) = ([], xsyms)
   218           | xsyms_to_syms [] = ([], []);
   219 
   220         fun nargs (Arg _ :: syms) = nargs syms + 1
   221           | nargs (TypArg _ :: syms) = nargs syms + 1
   222           | nargs (String _ :: syms) = nargs syms
   223           | nargs (Break _ :: syms) = nargs syms
   224           | nargs (Block (_, bsyms) :: syms) = nargs syms + nargs bsyms
   225           | nargs [] = 0;
   226       in
   227         (case xsyms_to_syms xsymbs of
   228           (symbs, []) => Some (const, (symbs, nargs symbs, pri))
   229         | _ => sys_error "xprod_to_fmt: unbalanced blocks")
   230       end;
   231 
   232 
   233 (* empty, extend, merge prtabs *)
   234 
   235 val empty_prtabs = [];
   236 
   237 fun extend_prtabs prtabs mode xprods =
   238   let
   239     val fmts = rev (mapfilter xprod_to_fmt xprods);
   240     val tab = get_tab prtabs mode;
   241     val new_tab =
   242       if null fmts then tab
   243       else Symtab.make_multi (fmts @ Symtab.dest_multi tab)     (*prefer new entries*)
   244   in overwrite (prtabs, (mode, new_tab)) end;
   245 
   246 fun merge_prtabs prtabs1 prtabs2 =
   247   let
   248     val modes = distinct (map fst (prtabs1 @ prtabs2));
   249     fun merge mode =
   250       (mode,
   251         generic_merge (op =) Symtab.dest_multi Symtab.make_multi
   252           (get_tab prtabs2 mode) (get_tab prtabs1 mode));       (*prefer 2nd over 1st*)
   253   in
   254     map merge modes
   255   end;
   256 
   257 
   258 
   259 (** pretty term or typ asts **)
   260 
   261 fun is_chain [Block (_, pr)] = is_chain pr
   262   | is_chain [Arg _] = true
   263   | is_chain _  = false;
   264 
   265 fun pretty tabs trf tokentrf type_mode curried ast0 p0 =
   266   let
   267     val trans = apply_trans "print ast translation";
   268 
   269     fun token_trans c [Ast.Variable x] =
   270           (case tokentrf c of
   271             None => None
   272           | Some f => Some (f x))
   273       | token_trans _ _ = None;
   274 
   275     (*default applications: prefix / postfix*)
   276     val appT =
   277       if type_mode then TypeExt.tappl_ast_tr'
   278       else if curried then SynTrans.applC_ast_tr'
   279       else SynTrans.appl_ast_tr';
   280 
   281     fun synT ([], args) = ([], args)
   282       | synT (Arg p :: symbs, t :: args) =
   283           let val (Ts, args') = synT (symbs, args);
   284           in (astT (t, p) @ Ts, args') end
   285       | synT (TypArg p :: symbs, t :: args) =
   286           let
   287             val (Ts, args') = synT (symbs, args);
   288           in
   289             if type_mode then (astT (t, p) @ Ts, args')
   290             else (pretty tabs trf tokentrf true curried t p @ Ts, args')
   291           end
   292       | synT (String s :: symbs, args) =
   293           let val (Ts, args') = synT (symbs, args);
   294           in (Pretty.str s :: Ts, args') end
   295       | synT (Block (i, bsymbs) :: symbs, args) =
   296           let
   297             val (bTs, args') = synT (bsymbs, args);
   298             val (Ts, args'') = synT (symbs, args');
   299           in (Pretty.blk (i, bTs) :: Ts, args'') end
   300       | synT (Break i :: symbs, args) =
   301           let val (Ts, args') = synT (symbs, args);
   302           in ((if i < 0 then Pretty.fbrk else Pretty.brk i) :: Ts, args') end
   303       | synT (_ :: _, []) = sys_error "synT"
   304 
   305     and parT (pr, args, p, p': int) = #1 (synT
   306           (if p > p' orelse
   307             (! show_brackets andalso p' <> SynExt.max_pri andalso not (is_chain pr))
   308             then [Block (1, String "(" :: pr @ [String ")"])]
   309             else pr, args))
   310 
   311     and prefixT (_, a, [], _) = [Pretty.str a]
   312       | prefixT (c, _, args, p) =
   313           if c = Ast.Constant "_appl" orelse c = Ast.Constant "_applC" then
   314             [Pretty.str "*** INSUFFICIENT SYNTAX FOR PREFIX APPLICATION ***"]
   315           else astT (appT (c, args), p)
   316 
   317     and splitT 0 ([x], ys) = (x, ys)
   318       | splitT 0 (rev_xs, ys) = (Ast.Appl (rev rev_xs), ys)
   319       | splitT n (rev_xs, y :: ys) = splitT (n - 1) (y :: rev_xs, ys)
   320       | splitT _ _ = sys_error "splitT"
   321 
   322     and combT (tup as (c, a, args, p)) =
   323       let
   324         val nargs = length args;
   325 
   326         (*find matching table entry, or print as prefix / postfix*)
   327         fun prnt ([], []) = prefixT tup
   328           | prnt ([], tb :: tbs) = prnt (Symtab.lookup_multi (tb, a), tbs)
   329           | prnt ((pr, n, p') :: prnps, tbs) =
   330               if nargs = n then parT (pr, args, p, p')
   331               else if nargs > n andalso not type_mode then
   332                 astT (appT (splitT n ([c], args)), p)
   333               else prnt (prnps, tbs);
   334       in
   335         (case token_trans a args of     (*try token translation function*)
   336           Some s_len => [Pretty.raw_str s_len]
   337         | None =>			(*try print translation functions*)
   338             astT (trans a (trf a) args, p) handle Match => prnt ([], tabs))
   339       end
   340 
   341     and astT (c as Ast.Constant a, p) = combT (c, a, [], p)
   342       | astT (Ast.Variable x, _) = [Pretty.str x]
   343       | astT (Ast.Appl ((c as Ast.Constant a) :: (args as _ :: _)), p) = combT (c, a, args, p)
   344       | astT (Ast.Appl (f :: (args as _ :: _)), p) = astT (appT (f, args), p)
   345       | astT (ast as Ast.Appl _, _) = raise Ast.AST ("pretty: malformed ast", [ast]);
   346   in astT (ast0, p0) end;
   347 
   348 
   349 (* pretty_term_ast *)
   350 
   351 fun pretty_term_ast curried prtabs trf tokentrf ast =
   352   Pretty.blk (0, pretty (tabs_of prtabs (! print_mode))
   353     trf tokentrf false curried ast 0);
   354 
   355 
   356 (* pretty_typ_ast *)
   357 
   358 fun pretty_typ_ast _ prtabs trf tokentrf ast =
   359   Pretty.blk (0, pretty (tabs_of prtabs (! print_mode))
   360     trf tokentrf true false ast 0);
   361 
   362 end;