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