src/Tools/Code/code_printer.ML
author haftmann
Mon, 04 Jan 2010 14:09:57 +0100
changeset 34247 d2803c7f6d52
parent 34178 a78b8d5b91cb
child 34944 970e1466028d
permissions -rw-r--r--
added applify combinator
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
    Author:     Florian Haftmann, TU Muenchen
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     3
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     4
Generic operations for pretty printing of target language code.
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     5
*)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     6
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     7
signature CODE_PRINTER =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
     8
sig
32908
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
     9
  type itype = Code_Thingol.itype
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
    10
  type iterm = Code_Thingol.iterm
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
    11
  type const = Code_Thingol.const
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
    12
  type dict = Code_Thingol.dict
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
    13
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
    14
  val eqn_error: thm -> string -> 'a
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    15
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    16
  val @@ : 'a * 'a -> 'a list
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    17
  val @| : 'a list * 'a -> 'a list
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    18
  val str: string -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    19
  val concat: Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    20
  val brackets: Pretty.T list -> Pretty.T
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
    21
  val enclose: string -> string -> Pretty.T list -> Pretty.T
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
    22
  val enum: string -> string -> string -> Pretty.T list -> Pretty.T
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
    23
  val enum_default: string -> string -> string -> string -> Pretty.T list -> Pretty.T
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    24
  val semicolon: Pretty.T list -> Pretty.T
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
    25
  val doublesemicolon: Pretty.T list -> Pretty.T
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
    26
  val indent: int -> Pretty.T -> Pretty.T
34071
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 33994
diff changeset
    27
  val string_of_pretty: int -> Pretty.T -> string
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 33994
diff changeset
    28
  val writeln_pretty: int -> Pretty.T -> unit
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    29
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    30
  val first_upper: string -> string
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    31
  val first_lower: string -> string
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    32
  type var_ctxt
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    33
  val make_vars: string list -> var_ctxt
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    34
  val intro_vars: string list -> var_ctxt -> var_ctxt
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    35
  val lookup_var: var_ctxt -> string -> string
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
    36
  val intro_base_names: (string -> bool) -> (string -> string)
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
    37
    -> string list -> var_ctxt -> var_ctxt
32908
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
    38
  val aux_params: var_ctxt -> iterm list list -> string list
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    39
31056
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    40
  type literals
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    41
  val Literals: { literal_char: string -> string, literal_string: string -> string,
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    42
        literal_numeral: bool -> int -> string,
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    43
        literal_list: Pretty.T list -> Pretty.T, infix_cons: int * string }
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    44
    -> literals
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    45
  val literal_char: literals -> string -> string
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    46
  val literal_string: literals -> string -> string
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    47
  val literal_numeral: literals -> bool -> int -> string
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    48
  val literal_list: literals -> Pretty.T list -> Pretty.T
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    49
  val infix_cons: literals -> int * string
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    50
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    51
  type lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    52
  val L: lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    53
  val R: lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    54
  val X: lrx
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    55
  type fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    56
  val BR: fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    57
  val NOBR: fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    58
  val INFX: int * lrx -> fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    59
  val APP: fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    60
  val brackify: fixity -> Pretty.T list -> Pretty.T
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    61
  val brackify_infix: int * lrx -> fixity -> Pretty.T list -> Pretty.T
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31056
diff changeset
    62
  val brackify_block: fixity -> Pretty.T -> Pretty.T list -> Pretty.T -> Pretty.T
34247
d2803c7f6d52 added applify combinator
haftmann
parents: 34178
diff changeset
    63
  val applify: string -> string -> fixity -> Pretty.T -> Pretty.T list -> Pretty.T
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    64
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    65
  type tyco_syntax
34152
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
    66
  type simple_const_syntax
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
    67
  type proto_const_syntax
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    68
  type const_syntax
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    69
  val parse_infix: ('a -> 'b) -> lrx * int -> string
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    70
    -> int * ((fixity -> 'b -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    71
    -> fixity -> 'a list -> Pretty.T)
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    72
  val parse_syntax: ('a -> 'b) -> OuterParse.token list
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    73
    -> (int * ((fixity -> 'b -> Pretty.T)
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    74
    -> fixity -> 'a list -> Pretty.T)) option * OuterParse.token list
34152
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
    75
  val simple_const_syntax: simple_const_syntax -> proto_const_syntax
31056
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    76
  val activate_const_syntax: theory -> literals
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    77
    -> proto_const_syntax -> Code_Thingol.naming -> const_syntax * Code_Thingol.naming
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
    78
  val gen_print_app: (thm -> var_ctxt -> const * iterm list -> Pretty.T list)
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    79
    -> (thm -> var_ctxt -> fixity -> iterm -> Pretty.T)
31056
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
    80
    -> (string -> const_syntax option)
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    81
    -> thm -> var_ctxt -> fixity -> const * iterm list -> Pretty.T
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
    82
  val gen_print_bind: (thm -> var_ctxt -> fixity -> iterm -> Pretty.T)
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28064
diff changeset
    83
    -> thm -> fixity
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
    84
    -> iterm -> var_ctxt -> Pretty.T * var_ctxt
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    85
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    86
  val mk_name_module: Name.context -> string option -> (string -> string option)
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    87
    -> 'a Graph.T -> string -> string
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
    88
  val dest_name: string -> string * string
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    89
end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    90
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    91
structure Code_Printer : CODE_PRINTER =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    92
struct
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    93
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    94
open Code_Thingol;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    95
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
    96
fun eqn_error thm s = error (s ^ ",\nin equation " ^ Display.string_of_thm_without_context thm);
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    97
34071
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 33994
diff changeset
    98
(** assembling and printing text pieces **)
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
    99
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   100
infixr 5 @@;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   101
infixr 5 @|;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   102
fun x @@ y = [x, y];
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   103
fun xs @| y = xs @ [y];
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   104
val str = PrintMode.setmp [] Pretty.str;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   105
val concat = Pretty.block o Pretty.breaks;
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   106
fun enclose l r = PrintMode.setmp [] (Pretty.enclose l r);
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   107
val brackets = enclose "(" ")" o Pretty.breaks;
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   108
fun enum sep l r = PrintMode.setmp [] (Pretty.enum sep l r);
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   109
fun enum_default default sep l r [] = str default
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   110
  | enum_default default sep l r xs = enum sep l r xs;
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   111
fun semicolon ps = Pretty.block [concat ps, str ";"];
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   112
fun doublesemicolon ps = Pretty.block [concat ps, str ";;"];
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   113
fun indent i = PrintMode.setmp [] (Pretty.indent i);
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   114
34071
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 33994
diff changeset
   115
fun string_of_pretty width p = PrintMode.setmp [] (Pretty.setmp_margin_CRITICAL width Pretty.string_of) p ^ "\n";
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 33994
diff changeset
   116
fun writeln_pretty width p = PrintMode.setmp [] (Pretty.setmp_margin_CRITICAL width Pretty.writeln) p;
93bfbb557e2e default_code_width is now proper theory data
haftmann
parents: 33994
diff changeset
   117
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   118
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   119
(** names and variable name contexts **)
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   120
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   121
type var_ctxt = string Symtab.table * Name.context;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   122
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   123
fun make_vars names = (fold (fn name => Symtab.update_new (name, name)) names Symtab.empty,
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   124
  Name.make_context names);
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   125
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   126
fun intro_vars names (namemap, namectxt) =
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   127
  let
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   128
    val (names', namectxt') = Name.variants names namectxt;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   129
    val namemap' = fold2 (curry Symtab.update) names names' namemap;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   130
  in (namemap', namectxt') end;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   131
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   132
fun lookup_var (namemap, _) name = case Symtab.lookup namemap name
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   133
 of SOME name' => name'
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   134
  | NONE => error ("Invalid name in context: " ^ quote name);
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   135
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   136
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   137
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   138
32908
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   139
fun aux_params vars lhss =
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   140
  let
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   141
    fun fish_param _ (w as SOME _) = w
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   142
      | fish_param (IVar (SOME v)) NONE = SOME v
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   143
      | fish_param _ NONE = NONE;
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   144
    fun fillup_param _ (_, SOME v) = v
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   145
      | fillup_param x (i, NONE) = x ^ string_of_int i;
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   146
    val fished1 = fold (map2 fish_param) lhss (replicate (length (hd lhss)) NONE);
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   147
    val x = Name.variant (map_filter I fished1) "x";
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   148
    val fished2 = map_index (fillup_param x) fished1;
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   149
    val (fished3, _) = Name.variants fished2 Name.context;
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   150
    val vars' = intro_vars fished3 vars;
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   151
  in map (lookup_var vars') fished3 end;
9aa8dfef16ff factored out Code_Printer.aux_params
haftmann
parents: 32091
diff changeset
   152
32913
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   153
fun intro_base_names no_syntax deresolve names = names 
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   154
  |> map_filter (fn name => if no_syntax name then
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   155
      let val name' = deresolve name in
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   156
        if Long_Name.is_qualified name' then NONE else SOME name'
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   157
      end else NONE)
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   158
  |> intro_vars;
3e9809678574 intro_base_names combinator
haftmann
parents: 32908
diff changeset
   159
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   160
31056
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   161
(** pretty literals **)
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   162
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   163
datatype literals = Literals of {
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   164
  literal_char: string -> string,
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   165
  literal_string: string -> string,
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   166
  literal_numeral: bool -> int -> string,
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   167
  literal_list: Pretty.T list -> Pretty.T,
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   168
  infix_cons: int * string
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   169
};
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   170
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   171
fun dest_Literals (Literals lits) = lits;
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   172
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   173
val literal_char = #literal_char o dest_Literals;
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   174
val literal_string = #literal_string o dest_Literals;
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   175
val literal_numeral = #literal_numeral o dest_Literals;
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   176
val literal_list = #literal_list o dest_Literals;
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   177
val infix_cons = #infix_cons o dest_Literals;
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   178
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   179
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   180
(** syntax printer **)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   181
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   182
(* binding priorities *)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   183
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   184
datatype lrx = L | R | X;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   185
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   186
datatype fixity =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   187
    BR
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   188
  | NOBR
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   189
  | INFX of (int * lrx);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   190
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   191
val APP = INFX (~1, L);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   192
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   193
fun fixity_lrx L L = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   194
  | fixity_lrx R R = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   195
  | fixity_lrx _ _ = true;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   196
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   197
fun fixity NOBR _ = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   198
  | fixity _ NOBR = false
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   199
  | fixity (INFX (pr, lr)) (INFX (print_ctxt, lr_ctxt)) =
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   200
      pr < print_ctxt
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   201
      orelse pr = print_ctxt
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   202
        andalso fixity_lrx lr lr_ctxt
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   203
      orelse print_ctxt = ~1
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   204
  | fixity BR (INFX _) = false
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   205
  | fixity _ _ = true;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   206
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   207
fun gen_brackify _ [p] = p
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   208
  | gen_brackify true (ps as _::_) = enclose "(" ")" ps
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   209
  | gen_brackify false (ps as _::_) = Pretty.block ps;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   210
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   211
fun brackify fxy_ctxt =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   212
  gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   213
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   214
fun brackify_infix infx fxy_ctxt =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   215
  gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   216
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31056
diff changeset
   217
fun brackify_block fxy_ctxt p1 ps p2 =
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31056
diff changeset
   218
  let val p = Pretty.block_enclose (p1, p2) ps
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31056
diff changeset
   219
  in if fixity BR fxy_ctxt
34178
a78b8d5b91cb take care for destructive print mode properly using dedicated pretty builders
haftmann
parents: 34176
diff changeset
   220
    then enclose "(" ")" [p]
31665
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31056
diff changeset
   221
    else p
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31056
diff changeset
   222
  end;
a1f4d3b3f6c8 tuned brackets for let expressions etc.
haftmann
parents: 31056
diff changeset
   223
34247
d2803c7f6d52 added applify combinator
haftmann
parents: 34178
diff changeset
   224
fun applify opn cls fxy_ctxt p [] = p
d2803c7f6d52 added applify combinator
haftmann
parents: 34178
diff changeset
   225
  | applify opn cls fxy_ctxt p ps =
d2803c7f6d52 added applify combinator
haftmann
parents: 34178
diff changeset
   226
      (if (fixity BR fxy_ctxt) then enclose "(" ")" else Pretty.block)
d2803c7f6d52 added applify combinator
haftmann
parents: 34178
diff changeset
   227
        (p @@ enum "," opn cls ps);
d2803c7f6d52 added applify combinator
haftmann
parents: 34178
diff changeset
   228
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   229
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   230
(* generic syntax *)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   231
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   232
type tyco_syntax = int * ((fixity -> itype -> Pretty.T)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   233
  -> fixity -> itype list -> Pretty.T);
34152
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
   234
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
   235
type simple_const_syntax = int * ((fixity -> iterm -> Pretty.T)
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
   236
  -> fixity -> (iterm * itype) list -> Pretty.T);
31056
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   237
type proto_const_syntax = int * (string list * (literals -> string list
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   238
  -> (var_ctxt -> fixity -> iterm -> Pretty.T)
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   239
    -> thm -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T));
34152
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
   240
type const_syntax = int * ((var_ctxt -> fixity -> iterm -> Pretty.T)
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
   241
  -> thm -> var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   242
34176
412cf41a92a0 made sml/nj happy
haftmann
parents: 34152
diff changeset
   243
fun simple_const_syntax syn =
412cf41a92a0 made sml/nj happy
haftmann
parents: 34152
diff changeset
   244
  apsnd (fn f => ([], (fn _ => fn _ => fn print => fn _ => fn vars => f (print vars)))) syn;
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   245
31056
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   246
fun activate_const_syntax thy literals (n, (cs, f)) naming =
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   247
  fold_map (Code_Thingol.ensure_declared_const thy) cs naming
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   248
  |-> (fn cs' => pair (n, f literals cs'));
01ac77eb660b robustifed infrastructure for complex term syntax during code generation
haftmann
parents: 30648
diff changeset
   249
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   250
fun gen_print_app print_app_expr print_term syntax_const thm vars fxy (app as ((c, (_, tys)), ts)) =
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   251
  case syntax_const c
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   252
   of NONE => brackify fxy (print_app_expr thm vars app)
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   253
    | SOME (k, print) =>
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   254
        let
33994
fc8af744f63c merged, resolving minor conflicts
haftmann
parents: 33957 33989
diff changeset
   255
          fun print' fxy ts = print (print_term thm) thm vars fxy (ts ~~ take k tys);
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   256
        in if k = length ts
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   257
          then print' fxy ts
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   258
        else if k < length ts
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   259
          then case chop k ts of (ts1, ts2) =>
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   260
            brackify fxy (print' APP ts1 :: map (print_term thm vars BR) ts2)
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   261
          else print_term thm vars fxy (Code_Thingol.eta_expand k app)
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   262
        end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   263
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   264
fun gen_print_bind print_term thm (fxy : fixity) pat vars =
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   265
  let
31889
fb2c8a687529 all variable names are optional
haftmann
parents: 31874
diff changeset
   266
    val vs = Code_Thingol.fold_varnames (insert (op =)) pat [];
31874
f172346ba805 simplified binding concept
haftmann
parents: 31775
diff changeset
   267
    val vars' = intro_vars vs vars;
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   268
  in (print_term thm vars' fxy pat, vars') end;
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   269
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   270
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   271
(* mixfix syntax *)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   272
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   273
datatype 'a mixfix =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   274
    Arg of fixity
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   275
  | Pretty of Pretty.T;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   276
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   277
fun mk_mixfix prep_arg (fixity_this, mfx) =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   278
  let
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   279
    fun is_arg (Arg _) = true
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   280
      | is_arg _ = false;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   281
    val i = (length o filter is_arg) mfx;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   282
    fun fillin _ [] [] =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   283
          []
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   284
      | fillin print (Arg fxy :: mfx) (a :: args) =
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   285
          (print fxy o prep_arg) a :: fillin print mfx args
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   286
      | fillin print (Pretty p :: mfx) args =
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   287
          p :: fillin print mfx args;
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   288
  in
33989
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   289
    (i, fn print => fn fixity_ctxt => fn args =>
cb136b5f6050 more speaking function names for Code_Printer; added doublesemicolon
haftmann
parents: 32924
diff changeset
   290
      gen_brackify (fixity fixity_this fixity_ctxt) (fillin print mfx args))
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   291
  end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   292
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   293
fun parse_infix prep_arg (x, i) s =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   294
  let
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   295
    val l = case x of L => INFX (i, L) | _ => INFX (i, X);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   296
    val r = case x of R => INFX (i, R) | _ => INFX (i, X);
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   297
  in
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   298
    mk_mixfix prep_arg (INFX (i, x),
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   299
      [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
   300
  end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   301
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   302
fun parse_mixfix prep_arg s =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   303
  let
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   304
    val sym_any = Scan.one Symbol.is_regular;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   305
    val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   306
         ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   307
      || ($$ "_" >> K (Arg BR))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   308
      || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   309
      || (Scan.repeat1
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   310
           (   $$ "'" |-- sym_any
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   311
            || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   312
                 sym_any) >> (Pretty o str o implode)));
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   313
  in case Scan.finite Symbol.stopper parse (Symbol.explode s)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   314
   of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   315
    | ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   316
    | _ => Scan.!!
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   317
        (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   318
  end;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   319
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   320
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr");
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   321
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   322
fun parse_syntax prep_arg xs =
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   323
  Scan.option ((
34152
8e5b596d8c73 clarified various user-defined syntax issues
haftmann
parents: 34071
diff changeset
   324
      ((OuterParse.$$$ infixK >> K X)
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   325
        || (OuterParse.$$$ infixlK >> K L)
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   326
        || (OuterParse.$$$ infixrK >> K R))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   327
        -- OuterParse.nat >> parse_infix prep_arg
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   328
      || Scan.succeed (parse_mixfix prep_arg))
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   329
      -- OuterParse.string
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   330
      >> (fn (parse, s) => parse s)) xs;
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   331
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   332
val _ = List.app OuterKeyword.keyword [infixK, infixlK, infixrK];
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   333
28064
d4a6460c53d1 restructured code generation of literals
haftmann
parents: 28060
diff changeset
   334
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   335
(** module name spaces **)
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   336
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   337
val dest_name =
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   338
  apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   339
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   340
fun mk_name_module reserved module_prefix module_alias program =
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   341
  let
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   342
    fun mk_alias name = case module_alias name
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   343
     of SOME name' => name'
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   344
      | NONE => name
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   345
          |> Long_Name.explode
32924
d2e9b2dab760 dropped Code_Printer prefix where feasible; fixed whitespace issues; more coherent terminology
haftmann
parents: 32913
diff changeset
   346
          |> map (fn name => (the_single o fst) (Name.variants [name] reserved))
30648
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   347
          |> Long_Name.implode;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   348
    fun mk_prefix name = case module_prefix
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   349
     of SOME module_prefix => Long_Name.append module_prefix name
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   350
      | NONE => name;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   351
    val tab =
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   352
      Symtab.empty
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   353
      |> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name))
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   354
           o fst o dest_name o fst)
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   355
             program
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   356
  in the o Symtab.lookup tab end;
17365ef082f3 clarified relationship of modules Code_Name and Code_Printer
haftmann
parents: 30161
diff changeset
   357
28060
1ee2d3bc25db separate module for code output
haftmann
parents:
diff changeset
   358
end; (*struct*)