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