src/Tools/Code/code_printer.ML
author haftmann
Tue Jun 30 17:33:30 2009 +0200 (2009-06-30)
changeset 31889 fb2c8a687529
parent 31874 f172346ba805
child 32091 30e2ffbba718
permissions -rw-r--r--
all variable names are optional
     1 (*  Title:      Tools/code/code_printer.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Generic operations for pretty printing of target language code.
     5 *)
     6 
     7 signature CODE_PRINTER =
     8 sig
     9   val nerror: thm -> string -> 'a
    10 
    11   val @@ : 'a * 'a -> 'a list
    12   val @| : 'a list * 'a -> 'a list
    13   val str: string -> Pretty.T
    14   val concat: Pretty.T list -> Pretty.T
    15   val brackets: Pretty.T list -> Pretty.T
    16   val semicolon: Pretty.T list -> Pretty.T
    17   val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T
    18 
    19   val first_upper: string -> string
    20   val first_lower: string -> string
    21   type var_ctxt
    22   val make_vars: string list -> var_ctxt
    23   val intro_vars: string list -> var_ctxt -> var_ctxt
    24   val lookup_var: var_ctxt -> string -> string
    25 
    26   type literals
    27   val Literals: { literal_char: string -> string, literal_string: string -> string,
    28         literal_numeral: bool -> int -> string,
    29         literal_list: Pretty.T list -> Pretty.T, infix_cons: int * string }
    30     -> literals
    31   val literal_char: literals -> string -> string
    32   val literal_string: literals -> string -> string
    33   val literal_numeral: literals -> bool -> int -> string
    34   val literal_list: literals -> Pretty.T list -> Pretty.T
    35   val infix_cons: literals -> int * string
    36 
    37   type lrx
    38   val L: lrx
    39   val R: lrx
    40   val X: lrx
    41   type fixity
    42   val BR: fixity
    43   val NOBR: fixity
    44   val INFX: int * lrx -> fixity
    45   val APP: fixity
    46   val brackify: fixity -> Pretty.T list -> Pretty.T
    47   val brackify_infix: int * lrx -> fixity -> Pretty.T list -> Pretty.T
    48   val brackify_block: fixity -> Pretty.T -> Pretty.T list -> Pretty.T -> Pretty.T
    49 
    50   type itype = Code_Thingol.itype
    51   type iterm = Code_Thingol.iterm
    52   type const = Code_Thingol.const
    53   type dict = Code_Thingol.dict
    54   type tyco_syntax
    55   type const_syntax
    56   type proto_const_syntax
    57   val parse_infix: ('a -> 'b) -> lrx * int -> string
    58     -> int * ((fixity -> 'b -> Pretty.T)
    59     -> fixity -> 'a list -> Pretty.T)
    60   val parse_syntax: ('a -> 'b) -> OuterParse.token list
    61     -> (int * ((fixity -> 'b -> Pretty.T)
    62     -> fixity -> 'a list -> Pretty.T)) option * OuterParse.token list
    63   val simple_const_syntax: (int * ((fixity -> iterm -> Pretty.T)
    64     -> fixity -> (iterm * itype) list -> Pretty.T)) option -> proto_const_syntax option
    65   val activate_const_syntax: theory -> literals
    66     -> proto_const_syntax -> Code_Thingol.naming -> const_syntax * Code_Thingol.naming
    67   val gen_pr_app: (thm -> var_ctxt -> const * iterm list -> Pretty.T list)
    68     -> (thm -> var_ctxt -> fixity -> iterm -> Pretty.T)
    69     -> (string -> const_syntax option)
    70     -> thm -> var_ctxt -> fixity -> const * iterm list -> Pretty.T
    71   val gen_pr_bind: (thm -> var_ctxt -> fixity -> iterm -> Pretty.T)
    72     -> thm -> fixity
    73     -> iterm -> var_ctxt -> Pretty.T * var_ctxt
    74 
    75   val mk_name_module: Name.context -> string option -> (string -> string option)
    76     -> 'a Graph.T -> string -> string
    77   val dest_name: string -> string * string
    78 end;
    79 
    80 structure Code_Printer : CODE_PRINTER =
    81 struct
    82 
    83 open Code_Thingol;
    84 
    85 fun nerror thm s = error (s ^ ",\nin equation " ^ Display.string_of_thm thm);
    86 
    87 (** assembling text pieces **)
    88 
    89 infixr 5 @@;
    90 infixr 5 @|;
    91 fun x @@ y = [x, y];
    92 fun xs @| y = xs @ [y];
    93 val str = PrintMode.setmp [] Pretty.str;
    94 val concat = Pretty.block o Pretty.breaks;
    95 val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
    96 fun semicolon ps = Pretty.block [concat ps, str ";"];
    97 fun enum_default default sep opn cls [] = str default
    98   | enum_default default sep opn cls xs = Pretty.enum sep opn cls xs;
    99 
   100 
   101 (** names and variable name contexts **)
   102 
   103 type var_ctxt = string Symtab.table * Name.context;
   104 
   105 fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty,
   106   Name.make_context names);
   107 
   108 fun intro_vars names (namemap, namectxt) =
   109   let
   110     val (names', namectxt') = Name.variants names namectxt;
   111     val namemap' = fold2 (curry Symtab.update) names names' namemap;
   112   in (namemap', namectxt') end;
   113 
   114 fun lookup_var (namemap, _) name = case Symtab.lookup namemap name
   115  of SOME name' => name'
   116   | NONE => error ("Invalid name in context: " ^ quote name);
   117 
   118 val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode;
   119 val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode;
   120 
   121 
   122 (** pretty literals **)
   123 
   124 datatype literals = Literals of {
   125   literal_char: string -> string,
   126   literal_string: string -> string,
   127   literal_numeral: bool -> int -> string,
   128   literal_list: Pretty.T list -> Pretty.T,
   129   infix_cons: int * string
   130 };
   131 
   132 fun dest_Literals (Literals lits) = lits;
   133 
   134 val literal_char = #literal_char o dest_Literals;
   135 val literal_string = #literal_string o dest_Literals;
   136 val literal_numeral = #literal_numeral o dest_Literals;
   137 val literal_list = #literal_list o dest_Literals;
   138 val infix_cons = #infix_cons o dest_Literals;
   139 
   140 
   141 (** syntax printer **)
   142 
   143 (* binding priorities *)
   144 
   145 datatype lrx = L | R | X;
   146 
   147 datatype fixity =
   148     BR
   149   | NOBR
   150   | INFX of (int * lrx);
   151 
   152 val APP = INFX (~1, L);
   153 
   154 fun fixity_lrx L L = false
   155   | fixity_lrx R R = false
   156   | fixity_lrx _ _ = true;
   157 
   158 fun fixity NOBR _ = false
   159   | fixity _ NOBR = false
   160   | fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
   161       pr < pr_ctxt
   162       orelse pr = pr_ctxt
   163         andalso fixity_lrx lr lr_ctxt
   164       orelse pr_ctxt = ~1
   165   | fixity BR (INFX _) = false
   166   | fixity _ _ = true;
   167 
   168 fun gen_brackify _ [p] = p
   169   | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
   170   | gen_brackify false (ps as _::_) = Pretty.block ps;
   171 
   172 fun brackify fxy_ctxt =
   173   gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks;
   174 
   175 fun brackify_infix infx fxy_ctxt =
   176   gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks;
   177 
   178 fun brackify_block fxy_ctxt p1 ps p2 =
   179   let val p = Pretty.block_enclose (p1, p2) ps
   180   in if fixity BR fxy_ctxt
   181     then Pretty.enclose "(" ")" [p]
   182     else p
   183   end;
   184 
   185 
   186 (* generic syntax *)
   187 
   188 type tyco_syntax = int * ((fixity -> itype -> Pretty.T)
   189   -> fixity -> itype list -> Pretty.T);
   190 type const_syntax = int * ((var_ctxt -> fixity -> iterm -> Pretty.T)
   191   -> thm -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
   192 type proto_const_syntax = int * (string list * (literals -> string list
   193   -> (var_ctxt -> fixity -> iterm -> Pretty.T)
   194     -> thm -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T));
   195 
   196 fun simple_const_syntax (SOME (n, f)) = SOME (n,
   197       ([], (fn _ => fn _ => fn pr => fn thm => fn vars => f (pr vars))))
   198   | simple_const_syntax NONE = NONE;
   199 
   200 fun activate_const_syntax thy literals (n, (cs, f)) naming =
   201   fold_map (Code_Thingol.ensure_declared_const thy) cs naming
   202   |-> (fn cs' => pair (n, f literals cs'));
   203 
   204 fun gen_pr_app pr_app pr_term syntax_const thm vars fxy (app as ((c, (_, tys)), ts)) =
   205   case syntax_const c
   206    of NONE => brackify fxy (pr_app thm vars app)
   207     | SOME (k, pr) =>
   208         let
   209           fun pr' fxy ts = pr (pr_term thm) thm vars fxy (ts ~~ curry Library.take k tys);
   210         in if k = length ts
   211           then pr' fxy ts
   212         else if k < length ts
   213           then case chop k ts of (ts1, ts2) =>
   214             brackify fxy (pr' APP ts1 :: map (pr_term thm vars BR) ts2)
   215           else pr_term thm vars fxy (Code_Thingol.eta_expand k app)
   216         end;
   217 
   218 fun gen_pr_bind pr_term thm (fxy : fixity) pat vars =
   219   let
   220     val vs = Code_Thingol.fold_varnames (insert (op =)) pat [];
   221     val vars' = intro_vars vs vars;
   222   in (pr_term thm vars' fxy pat, vars') end;
   223 
   224 
   225 (* mixfix syntax *)
   226 
   227 datatype 'a mixfix =
   228     Arg of fixity
   229   | Pretty of Pretty.T;
   230 
   231 fun mk_mixfix prep_arg (fixity_this, mfx) =
   232   let
   233     fun is_arg (Arg _) = true
   234       | is_arg _ = false;
   235     val i = (length o filter is_arg) mfx;
   236     fun fillin _ [] [] =
   237           []
   238       | fillin pr (Arg fxy :: mfx) (a :: args) =
   239           (pr fxy o prep_arg) a :: fillin pr mfx args
   240       | fillin pr (Pretty p :: mfx) args =
   241           p :: fillin pr mfx args;
   242   in
   243     (i, fn pr => fn fixity_ctxt => fn args =>
   244       gen_brackify (fixity fixity_this fixity_ctxt) (fillin pr mfx args))
   245   end;
   246 
   247 fun parse_infix prep_arg (x, i) s =
   248   let
   249     val l = case x of L => INFX (i, L) | _ => INFX (i, X);
   250     val r = case x of R => INFX (i, R) | _ => INFX (i, X);
   251   in
   252     mk_mixfix prep_arg (INFX (i, x),
   253       [Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r])
   254   end;
   255 
   256 fun parse_mixfix prep_arg s =
   257   let
   258     val sym_any = Scan.one Symbol.is_regular;
   259     val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
   260          ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
   261       || ($$ "_" >> K (Arg BR))
   262       || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
   263       || (Scan.repeat1
   264            (   $$ "'" |-- sym_any
   265             || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
   266                  sym_any) >> (Pretty o str o implode)));
   267   in case Scan.finite Symbol.stopper parse (Symbol.explode s)
   268    of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
   269     | ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p)
   270     | _ => Scan.!!
   271         (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
   272   end;
   273 
   274 val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr");
   275 
   276 fun parse_syntax prep_arg xs =
   277   Scan.option ((
   278       ((OuterParse.$$$ infixK  >> K X)
   279         || (OuterParse.$$$ infixlK >> K L)
   280         || (OuterParse.$$$ infixrK >> K R))
   281         -- OuterParse.nat >> parse_infix prep_arg
   282       || Scan.succeed (parse_mixfix prep_arg))
   283       -- OuterParse.string
   284       >> (fn (parse, s) => parse s)) xs;
   285 
   286 val _ = List.app OuterKeyword.keyword [infixK, infixlK, infixrK];
   287 
   288 
   289 (** module name spaces **)
   290 
   291 val dest_name =
   292   apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
   293 
   294 fun mk_name_module reserved_names module_prefix module_alias program =
   295   let
   296     fun mk_alias name = case module_alias name
   297      of SOME name' => name'
   298       | NONE => name
   299           |> Long_Name.explode
   300           |> map (fn name => (the_single o fst) (Name.variants [name] reserved_names))
   301           |> Long_Name.implode;
   302     fun mk_prefix name = case module_prefix
   303      of SOME module_prefix => Long_Name.append module_prefix name
   304       | NONE => name;
   305     val tab =
   306       Symtab.empty
   307       |> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name))
   308            o fst o dest_name o fst)
   309              program
   310   in the o Symtab.lookup tab end;
   311 
   312 end; (*struct*)