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