src/Tools/code/code_printer.ML
author haftmann
Wed Oct 22 14:15:45 2008 +0200 (2008-10-22)
changeset 28663 bd8438543bf2
parent 28064 d4a6460c53d1
child 28690 fc51fa5efea1
permissions -rw-r--r--
code identifier namings are no longer imperative
haftmann@28060
     1
(*  Title:      Tools/code/code_printer.ML
haftmann@28060
     2
    ID:         $Id$
haftmann@28060
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@28060
     4
haftmann@28060
     5
Generic operations for pretty printing of target language code.
haftmann@28060
     6
*)
haftmann@28060
     7
haftmann@28060
     8
signature CODE_PRINTER =
haftmann@28060
     9
sig
haftmann@28663
    10
  val nerror: thm -> string -> 'a
haftmann@28060
    11
haftmann@28060
    12
  val @@ : 'a * 'a -> 'a list
haftmann@28060
    13
  val @| : 'a list * 'a -> 'a list
haftmann@28060
    14
  val str: string -> Pretty.T
haftmann@28060
    15
  val concat: Pretty.T list -> Pretty.T
haftmann@28060
    16
  val brackets: Pretty.T list -> Pretty.T
haftmann@28060
    17
  val semicolon: Pretty.T list -> Pretty.T
haftmann@28060
    18
  val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T
haftmann@28060
    19
haftmann@28060
    20
  type lrx
haftmann@28060
    21
  val L: lrx
haftmann@28060
    22
  val R: lrx
haftmann@28060
    23
  val X: lrx
haftmann@28060
    24
  type fixity
haftmann@28060
    25
  val BR: fixity
haftmann@28060
    26
  val NOBR: fixity
haftmann@28060
    27
  val INFX: int * lrx -> fixity
haftmann@28060
    28
  val APP: fixity
haftmann@28060
    29
  val brackify: fixity -> Pretty.T list -> Pretty.T
haftmann@28060
    30
  val brackify_infix: int * lrx -> fixity -> Pretty.T list -> Pretty.T
haftmann@28060
    31
haftmann@28060
    32
  type itype = Code_Thingol.itype
haftmann@28060
    33
  type iterm = Code_Thingol.iterm
haftmann@28060
    34
  type const = Code_Thingol.const
haftmann@28060
    35
  type dict = Code_Thingol.dict
haftmann@28060
    36
  type class_syntax
haftmann@28060
    37
  type tyco_syntax
haftmann@28060
    38
  type const_syntax
haftmann@28060
    39
  val parse_infix: ('a -> 'b) -> lrx * int -> string
haftmann@28663
    40
    -> int * ((fixity -> 'b -> Pretty.T)
haftmann@28663
    41
    -> fixity -> 'a list -> Pretty.T)
haftmann@28060
    42
  val parse_syntax: ('a -> 'b) -> OuterParse.token list
haftmann@28663
    43
    -> (int * ((fixity -> 'b -> Pretty.T)
haftmann@28663
    44
    -> fixity -> 'a list -> Pretty.T)) option * OuterParse.token list
haftmann@28663
    45
  val simple_const_syntax: (int * ((fixity -> iterm -> Pretty.T)
haftmann@28663
    46
    -> fixity -> (iterm * itype) list -> Pretty.T)) option -> const_syntax option
haftmann@28663
    47
  val gen_pr_app: (thm -> bool -> Code_Name.var_ctxt -> const * iterm list -> Pretty.T list)
haftmann@28663
    48
    -> (thm -> bool -> Code_Name.var_ctxt -> fixity -> iterm -> Pretty.T)
haftmann@28663
    49
    -> (string -> const_syntax option) -> (string -> bool) -> Code_Thingol.naming
haftmann@28663
    50
    -> thm -> bool -> Code_Name.var_ctxt -> fixity -> const * iterm list -> Pretty.T
haftmann@28060
    51
  val gen_pr_bind: ((string option * Pretty.T option) * itype -> Pretty.T)
haftmann@28663
    52
    -> (thm -> bool -> Code_Name.var_ctxt -> fixity -> iterm -> Pretty.T)
haftmann@28663
    53
    -> thm -> fixity
haftmann@28663
    54
    -> (string option * iterm option) * itype -> Code_Name.var_ctxt -> Pretty.T * Code_Name.var_ctxt
haftmann@28060
    55
haftmann@28064
    56
  type literals
haftmann@28064
    57
  val Literals: { literal_char: string -> string, literal_string: string -> string,
haftmann@28064
    58
        literal_numeral: bool -> int -> string, literal_list: Pretty.T list -> Pretty.T, infix_cons: int * string }
haftmann@28064
    59
    -> literals
haftmann@28064
    60
  val literal_char: literals -> string -> string
haftmann@28064
    61
  val literal_string: literals -> string -> string
haftmann@28064
    62
  val literal_numeral: literals -> bool -> int -> string
haftmann@28064
    63
  val literal_list: literals -> Pretty.T list -> Pretty.T
haftmann@28064
    64
  val infix_cons: literals -> int * string
haftmann@28060
    65
end;
haftmann@28060
    66
haftmann@28060
    67
structure Code_Printer : CODE_PRINTER =
haftmann@28060
    68
struct
haftmann@28060
    69
haftmann@28060
    70
open Code_Thingol;
haftmann@28060
    71
haftmann@28060
    72
fun nerror thm s = error (s ^ ",\nin equation " ^ Display.string_of_thm thm);
haftmann@28060
    73
haftmann@28060
    74
(** assembling text pieces **)
haftmann@28060
    75
haftmann@28060
    76
infixr 5 @@;
haftmann@28060
    77
infixr 5 @|;
haftmann@28060
    78
fun x @@ y = [x, y];
haftmann@28060
    79
fun xs @| y = xs @ [y];
haftmann@28060
    80
val str = PrintMode.setmp [] Pretty.str;
haftmann@28060
    81
val concat = Pretty.block o Pretty.breaks;
haftmann@28060
    82
val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
haftmann@28060
    83
fun semicolon ps = Pretty.block [concat ps, str ";"];
haftmann@28060
    84
fun enum_default default sep opn cls [] = str default
haftmann@28060
    85
  | enum_default default sep opn cls xs = Pretty.enum sep opn cls xs;
haftmann@28060
    86
haftmann@28060
    87
haftmann@28060
    88
(** syntax printer **)
haftmann@28060
    89
haftmann@28060
    90
(* binding priorities *)
haftmann@28060
    91
haftmann@28060
    92
datatype lrx = L | R | X;
haftmann@28060
    93
haftmann@28060
    94
datatype fixity =
haftmann@28060
    95
    BR
haftmann@28060
    96
  | NOBR
haftmann@28060
    97
  | INFX of (int * lrx);
haftmann@28060
    98
haftmann@28060
    99
val APP = INFX (~1, L);
haftmann@28060
   100
haftmann@28060
   101
fun fixity_lrx L L = false
haftmann@28060
   102
  | fixity_lrx R R = false
haftmann@28060
   103
  | fixity_lrx _ _ = true;
haftmann@28060
   104
haftmann@28060
   105
fun fixity NOBR _ = false
haftmann@28060
   106
  | fixity _ NOBR = false
haftmann@28060
   107
  | fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
haftmann@28060
   108
      pr < pr_ctxt
haftmann@28060
   109
      orelse pr = pr_ctxt
haftmann@28060
   110
        andalso fixity_lrx lr lr_ctxt
haftmann@28060
   111
      orelse pr_ctxt = ~1
haftmann@28060
   112
  | fixity BR (INFX _) = false
haftmann@28060
   113
  | fixity _ _ = true;
haftmann@28060
   114
haftmann@28060
   115
fun gen_brackify _ [p] = p
haftmann@28060
   116
  | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
haftmann@28060
   117
  | gen_brackify false (ps as _::_) = Pretty.block ps;
haftmann@28060
   118
haftmann@28060
   119
fun brackify fxy_ctxt =
haftmann@28060
   120
  gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks;
haftmann@28060
   121
haftmann@28060
   122
fun brackify_infix infx fxy_ctxt =
haftmann@28060
   123
  gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks;
haftmann@28060
   124
haftmann@28060
   125
haftmann@28060
   126
(* generic syntax *)
haftmann@28060
   127
haftmann@28060
   128
type class_syntax = string * (string -> string option);
haftmann@28060
   129
type tyco_syntax = int * ((fixity -> itype -> Pretty.T)
haftmann@28060
   130
  -> fixity -> itype list -> Pretty.T);
haftmann@28663
   131
type const_syntax = int * ((Code_Name.var_ctxt -> fixity -> iterm -> Pretty.T)
haftmann@28663
   132
  -> Code_Thingol.naming -> thm -> bool -> Code_Name.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
haftmann@28060
   133
haftmann@28060
   134
fun simple_const_syntax x = (Option.map o apsnd)
haftmann@28663
   135
  (fn pretty => fn pr => fn naming => fn thm => fn pat => fn vars => pretty (pr vars)) x;
haftmann@28060
   136
haftmann@28663
   137
fun gen_pr_app pr_app pr_term syntax_const is_cons naming thm pat
haftmann@28060
   138
    vars fxy (app as ((c, (_, tys)), ts)) =
haftmann@28060
   139
  case syntax_const c
haftmann@28060
   140
   of NONE => if pat andalso not (is_cons c) then
haftmann@28060
   141
        nerror thm "Non-constructor in pattern"
haftmann@28060
   142
        else brackify fxy (pr_app thm pat vars app)
haftmann@28060
   143
    | SOME (k, pr) =>
haftmann@28060
   144
        let
haftmann@28663
   145
          fun pr' fxy ts = pr (pr_term thm pat) naming thm pat vars fxy (ts ~~ curry Library.take k tys);
haftmann@28060
   146
        in if k = length ts
haftmann@28060
   147
          then pr' fxy ts
haftmann@28060
   148
        else if k < length ts
haftmann@28060
   149
          then case chop k ts of (ts1, ts2) =>
haftmann@28060
   150
            brackify fxy (pr' APP ts1 :: map (pr_term thm pat vars BR) ts2)
haftmann@28060
   151
          else pr_term thm pat vars fxy (Code_Thingol.eta_expand k app)
haftmann@28060
   152
        end;
haftmann@28060
   153
haftmann@28060
   154
fun gen_pr_bind pr_bind pr_term thm (fxy : fixity) ((v, pat), ty : itype) vars =
haftmann@28060
   155
  let
haftmann@28060
   156
    val vs = case pat
haftmann@28060
   157
     of SOME pat => Code_Thingol.fold_varnames (insert (op =)) pat []
haftmann@28060
   158
      | NONE => [];
haftmann@28663
   159
    val vars' = Code_Name.intro_vars (the_list v) vars;
haftmann@28663
   160
    val vars'' = Code_Name.intro_vars vs vars';
haftmann@28663
   161
    val v' = Option.map (Code_Name.lookup_var vars') v;
haftmann@28060
   162
    val pat' = Option.map (pr_term thm true vars'' fxy) pat;
haftmann@28060
   163
  in (pr_bind ((v', pat'), ty), vars'') end;
haftmann@28060
   164
haftmann@28060
   165
haftmann@28060
   166
(* mixfix syntax *)
haftmann@28060
   167
haftmann@28060
   168
datatype 'a mixfix =
haftmann@28060
   169
    Arg of fixity
haftmann@28060
   170
  | Pretty of Pretty.T;
haftmann@28060
   171
haftmann@28060
   172
fun mk_mixfix prep_arg (fixity_this, mfx) =
haftmann@28060
   173
  let
haftmann@28060
   174
    fun is_arg (Arg _) = true
haftmann@28060
   175
      | is_arg _ = false;
haftmann@28060
   176
    val i = (length o filter is_arg) mfx;
haftmann@28060
   177
    fun fillin _ [] [] =
haftmann@28060
   178
          []
haftmann@28060
   179
      | fillin pr (Arg fxy :: mfx) (a :: args) =
haftmann@28060
   180
          (pr fxy o prep_arg) a :: fillin pr mfx args
haftmann@28060
   181
      | fillin pr (Pretty p :: mfx) args =
haftmann@28060
   182
          p :: fillin pr mfx args;
haftmann@28060
   183
  in
haftmann@28060
   184
    (i, fn pr => fn fixity_ctxt => fn args =>
haftmann@28060
   185
      gen_brackify (fixity fixity_this fixity_ctxt) (fillin pr mfx args))
haftmann@28060
   186
  end;
haftmann@28060
   187
haftmann@28060
   188
fun parse_infix prep_arg (x, i) s =
haftmann@28060
   189
  let
haftmann@28060
   190
    val l = case x of L => INFX (i, L) | _ => INFX (i, X);
haftmann@28060
   191
    val r = case x of R => INFX (i, R) | _ => INFX (i, X);
haftmann@28060
   192
  in
haftmann@28060
   193
    mk_mixfix prep_arg (INFX (i, x),
haftmann@28060
   194
      [Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r])
haftmann@28060
   195
  end;
haftmann@28060
   196
haftmann@28060
   197
fun parse_mixfix prep_arg s =
haftmann@28060
   198
  let
haftmann@28060
   199
    val sym_any = Scan.one Symbol.is_regular;
haftmann@28060
   200
    val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
haftmann@28060
   201
         ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
haftmann@28060
   202
      || ($$ "_" >> K (Arg BR))
haftmann@28060
   203
      || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
haftmann@28060
   204
      || (Scan.repeat1
haftmann@28060
   205
           (   $$ "'" |-- sym_any
haftmann@28060
   206
            || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
haftmann@28060
   207
                 sym_any) >> (Pretty o str o implode)));
haftmann@28060
   208
  in case Scan.finite Symbol.stopper parse (Symbol.explode s)
haftmann@28060
   209
   of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
haftmann@28060
   210
    | ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p)
haftmann@28060
   211
    | _ => Scan.!!
haftmann@28060
   212
        (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
haftmann@28060
   213
  end;
haftmann@28060
   214
haftmann@28060
   215
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr");
haftmann@28060
   216
haftmann@28060
   217
fun parse_syntax prep_arg xs =
haftmann@28060
   218
  Scan.option ((
haftmann@28060
   219
      ((OuterParse.$$$ infixK  >> K X)
haftmann@28060
   220
        || (OuterParse.$$$ infixlK >> K L)
haftmann@28060
   221
        || (OuterParse.$$$ infixrK >> K R))
haftmann@28060
   222
        -- OuterParse.nat >> parse_infix prep_arg
haftmann@28060
   223
      || Scan.succeed (parse_mixfix prep_arg))
haftmann@28060
   224
      -- OuterParse.string
haftmann@28060
   225
      >> (fn (parse, s) => parse s)) xs;
haftmann@28060
   226
haftmann@28060
   227
val _ = List.app OuterKeyword.keyword [infixK, infixlK, infixrK];
haftmann@28060
   228
haftmann@28064
   229
haftmann@28064
   230
(** pretty literals **)
haftmann@28064
   231
haftmann@28064
   232
datatype literals = Literals of {
haftmann@28064
   233
  literal_char: string -> string,
haftmann@28064
   234
  literal_string: string -> string,
haftmann@28064
   235
  literal_numeral: bool -> int -> string,
haftmann@28064
   236
  literal_list: Pretty.T list -> Pretty.T,
haftmann@28064
   237
  infix_cons: int * string
haftmann@28064
   238
};
haftmann@28064
   239
haftmann@28064
   240
fun dest_Literals (Literals lits) = lits;
haftmann@28064
   241
haftmann@28064
   242
val literal_char = #literal_char o dest_Literals;
haftmann@28064
   243
val literal_string = #literal_string o dest_Literals;
haftmann@28064
   244
val literal_numeral = #literal_numeral o dest_Literals;
haftmann@28064
   245
val literal_list = #literal_list o dest_Literals;
haftmann@28064
   246
val infix_cons = #infix_cons o dest_Literals;
haftmann@28064
   247
haftmann@28060
   248
end; (*struct*)