src/Tools/code/code_target.ML
author haftmann
Wed, 28 May 2008 12:24:48 +0200
changeset 27001 d21bb9f73364
parent 27000 e8a40d8b7897
child 27014 a5f53d9d2b60
permissions -rw-r--r--
fixed utterly wrong print mode handling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     1
(*  Title:      Tools/code/code_target.ML
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     2
    ID:         $Id$
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     4
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     5
Serializer from intermediate language ("Thin-gol")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     6
to target languages (like SML or Haskell).
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     7
*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     8
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
     9
signature CODE_TARGET =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    10
sig
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    11
  include BASIC_CODE_THINGOL;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    12
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    13
  val add_syntax_class: string -> class
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
    14
    -> (string * (string * string) list) option -> theory -> theory;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    15
  val add_syntax_inst: string -> string * class -> bool -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    16
  val add_syntax_tycoP: string -> string -> OuterParse.token list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    17
    -> (theory -> theory) * OuterParse.token list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    18
  val add_syntax_constP: string -> string -> OuterParse.token list
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    19
    -> (theory -> theory) * OuterParse.token list;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    20
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    21
  val add_undefined: string -> string -> string -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    22
  val add_pretty_list: string -> string -> string -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    23
  val add_pretty_list_string: string -> string -> string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    24
    -> string -> string list -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    25
  val add_pretty_char: string -> string -> string list -> theory -> theory
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
    26
  val add_pretty_numeral: string -> bool -> bool -> string -> string -> string
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    27
    -> string -> string -> theory -> theory;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
    28
  val add_pretty_message: string -> string -> string list -> string
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    29
    -> string -> string -> theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    30
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
    31
  val allow_exception: string -> theory -> theory;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
    32
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    33
  type serialization;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    34
  type serializer;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    35
  val add_serializer: string * serializer -> theory -> theory;
26998
2c4032d59586 proper use of the Pretty module
haftmann
parents: 26753
diff changeset
    36
  val assert_serializer: theory -> string -> string;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    37
  val serialize: theory -> string -> bool -> string option -> Args.T list
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    38
    -> CodeThingol.code -> string list option -> serialization;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    39
  val compile: serialization -> unit;
27001
d21bb9f73364 fixed utterly wrong print mode handling
haftmann
parents: 27000
diff changeset
    40
  val write: serialization -> unit;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    41
  val file: Path.T -> serialization -> unit;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    42
  val string: serialization -> string;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    43
  val sml_of: theory -> CodeThingol.code -> string list -> string;
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    44
  val eval: theory -> (string * (unit -> 'a) option ref)
24918
22013215eece moved translation kernel to CodeThingol
haftmann
parents: 24867
diff changeset
    45
    -> CodeThingol.code -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    46
  val code_width: int ref;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    47
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    48
  val setup: theory -> theory;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    49
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
structure CodeTarget : CODE_TARGET =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    52
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
open BasicCodeThingol;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
(** basics **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
infixr 5 @@;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
infixr 5 @|;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
fun x @@ y = [x, y];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
fun xs @| y = xs @ [y];
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
    62
val str = PrintMode.setmp [] Pretty.str;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
val concat = Pretty.block o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
fun semicolon ps = Pretty.block [concat ps, str ";"];
25771
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
    66
fun enum_default default sep opn cls [] = str default
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
    67
  | enum_default default sep opn cls xs = Pretty.enum sep opn cls xs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
26998
2c4032d59586 proper use of the Pretty module
haftmann
parents: 26753
diff changeset
    69
val code_width = ref 80;
27001
d21bb9f73364 fixed utterly wrong print mode handling
haftmann
parents: 27000
diff changeset
    70
fun code_source p = PrintMode.setmp [] (Pretty.setmp_margin (!code_width) Pretty.string_of) p ^ "\n";
d21bb9f73364 fixed utterly wrong print mode handling
haftmann
parents: 27000
diff changeset
    71
fun code_writeln p = Pretty.setmp_margin (!code_width) Pretty.writeln p;
26998
2c4032d59586 proper use of the Pretty module
haftmann
parents: 26753
diff changeset
    72
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    73
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    74
(** generic syntax **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    75
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    76
datatype lrx = L | R | X;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    77
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    78
datatype fixity =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    79
    BR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    80
  | NOBR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    81
  | INFX of (int * lrx);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    82
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    83
val APP = INFX (~1, L);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    84
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    85
fun fixity_lrx L L = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    86
  | fixity_lrx R R = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    87
  | fixity_lrx _ _ = true;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    88
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    89
fun fixity NOBR NOBR = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    90
  | fixity BR NOBR = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    91
  | fixity NOBR BR = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    92
  | fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
      pr < pr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
      orelse pr = pr_ctxt
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    95
        andalso fixity_lrx lr lr_ctxt
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
      orelse pr_ctxt = ~1
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    97
  | fixity _ (INFX _) = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    98
  | fixity (INFX _) NOBR = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    99
  | fixity _ _ = true;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   100
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   101
fun gen_brackify _ [p] = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
  | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   103
  | gen_brackify false (ps as _::_) = Pretty.block ps;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   104
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   105
fun brackify fxy_ctxt ps =
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
   106
  gen_brackify (fixity BR fxy_ctxt) (Pretty.breaks ps);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   108
fun brackify_infix infx fxy_ctxt ps =
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
   109
  gen_brackify (fixity (INFX infx) fxy_ctxt) (Pretty.breaks ps);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   110
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   111
type class_syntax = string * (string -> string option);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
type typ_syntax = int * ((fixity -> itype -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
  -> fixity -> itype list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
type term_syntax = int * ((CodeName.var_ctxt -> fixity -> iterm -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
  -> CodeName.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   117
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   118
(** theory data **)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   119
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   120
val target_diag = "diag";
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   121
val target_SML = "SML";
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   122
val target_OCaml = "OCaml";
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   123
val target_Haskell = "Haskell";
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   124
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   125
datatype syntax_expr = SyntaxExpr of {
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   126
  class: (string * (string -> string option)) Symtab.table,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   127
  inst: unit Symtab.table,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   128
  tyco: typ_syntax Symtab.table,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   129
  const: term_syntax Symtab.table
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   130
};
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   131
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   132
fun mk_syntax_expr ((class, inst), (tyco, const)) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   133
  SyntaxExpr { class = class, inst = inst, tyco = tyco, const = const };
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   134
fun map_syntax_expr f (SyntaxExpr { class, inst, tyco, const }) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   135
  mk_syntax_expr (f ((class, inst), (tyco, const)));
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   136
fun merge_syntax_expr (SyntaxExpr { class = class1, inst = inst1, tyco = tyco1, const = const1 },
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   137
    SyntaxExpr { class = class2, inst = inst2, tyco = tyco2, const = const2 }) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   138
  mk_syntax_expr (
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   139
    (Symtab.join (K snd) (class1, class2),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   140
       Symtab.join (K snd) (inst1, inst2)),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   141
    (Symtab.join (K snd) (tyco1, tyco2),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   142
       Symtab.join (K snd) (const1, const2))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   143
  );
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   144
27001
d21bb9f73364 fixed utterly wrong print mode handling
haftmann
parents: 27000
diff changeset
   145
datatype serialization_dest = Compile | Write | File of Path.T | String;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   146
type serialization = serialization_dest -> string option;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   147
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   148
fun compile f = (f Compile; ());
27001
d21bb9f73364 fixed utterly wrong print mode handling
haftmann
parents: 27000
diff changeset
   149
fun write f = (f Write; ());
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   150
fun file p f = (f (File p); ());
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   151
fun string f = the (f String);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   152
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   153
type serializer =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   154
  string option
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   155
  -> Args.T list
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   156
  -> (string -> string)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   157
  -> string list
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   158
  -> (string * Pretty.T) list
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   159
  -> (string -> string option)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   160
  -> (string -> class_syntax option)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   161
  -> (string -> typ_syntax option)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   162
  -> (string -> term_syntax option)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   163
  -> CodeThingol.code -> string list -> serialization;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   164
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   165
datatype target = Target of {
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   166
  serial: serial,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   167
  serializer: serializer,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   168
  reserved: string list,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   169
  includes: Pretty.T Symtab.table,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   170
  syntax_expr: syntax_expr,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   171
  module_alias: string Symtab.table
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   172
};
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   173
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   174
fun mk_target ((serial, serializer), ((reserved, includes), (syntax_expr, module_alias))) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   175
  Target { serial = serial, serializer = serializer, reserved = reserved, 
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   176
    includes = includes, syntax_expr = syntax_expr, module_alias = module_alias };
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   177
fun map_target f ( Target { serial, serializer, reserved, includes, syntax_expr, module_alias } ) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   178
  mk_target (f ((serial, serializer), ((reserved, includes), (syntax_expr, module_alias))));
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   179
fun merge_target target (Target { serial = serial1, serializer = serializer,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   180
  reserved = reserved1, includes = includes1,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   181
  syntax_expr = syntax_expr1, module_alias = module_alias1 },
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   182
    Target { serial = serial2, serializer = _,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   183
      reserved = reserved2, includes = includes2,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   184
      syntax_expr = syntax_expr2, module_alias = module_alias2 }) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   185
  if serial1 = serial2 then
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   186
    mk_target ((serial1, serializer),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   187
      ((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   188
        (merge_syntax_expr (syntax_expr1, syntax_expr2),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   189
          Symtab.join (K snd) (module_alias1, module_alias2))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   190
    ))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   191
  else
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   192
    error ("Incompatible serializers: " ^ quote target);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   193
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   194
structure CodeTargetData = TheoryDataFun
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   195
(
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   196
  type T = target Symtab.table * string list;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   197
  val empty = (Symtab.empty, []);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   198
  val copy = I;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   199
  val extend = I;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   200
  fun merge _ ((target1, exc1) : T, (target2, exc2)) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   201
    (Symtab.join merge_target (target1, target2), Library.merge (op =) (exc1, exc2));
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   202
);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   203
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   204
fun the_serializer (Target { serializer, ... }) = serializer;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   205
fun the_reserved (Target { reserved, ... }) = reserved;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   206
fun the_includes (Target { includes, ... }) = includes;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   207
fun the_syntax_expr (Target { syntax_expr = SyntaxExpr x, ... }) = x;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   208
fun the_module_alias (Target { module_alias , ... }) = module_alias;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   209
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   210
fun assert_serializer thy target =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   211
  case Symtab.lookup (fst (CodeTargetData.get thy)) target
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   212
   of SOME data => target
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   213
    | NONE => error ("Unknown code target language: " ^ quote target);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   214
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   215
fun add_serializer (target, seri) thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   216
  let
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   217
    val _ = case Symtab.lookup (fst (CodeTargetData.get thy)) target
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   218
     of SOME _ => warning ("overwriting existing serializer " ^ quote target)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   219
      | NONE => ();
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   220
  in
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   221
    thy
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   222
    |> (CodeTargetData.map o apfst oo Symtab.map_default)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   223
          (target, mk_target ((serial (), seri), (([], Symtab.empty),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   224
            (mk_syntax_expr ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   225
              Symtab.empty))))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   226
          ((map_target o apfst o apsnd o K) seri)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   228
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   229
fun map_seri_data target f thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   230
  let
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   231
    val _ = assert_serializer thy target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   232
  in
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   233
    thy
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   234
    |> (CodeTargetData.map o apfst o Symtab.map_entry target o map_target) f
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   236
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   237
fun map_adaptions target =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   238
  map_seri_data target o apsnd o apfst;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   239
fun map_syntax_exprs target =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   240
  map_seri_data target o apsnd o apsnd o apfst o map_syntax_expr;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   241
fun map_module_alias target =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   242
  map_seri_data target o apsnd o apsnd o apsnd;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   243
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   244
fun get_serializer get_seri thy target permissive =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   245
  let
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   246
    val (seris, exc) = CodeTargetData.get thy;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   247
    val data = case Symtab.lookup seris target
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   248
     of SOME data => data
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   249
      | NONE => error ("Unknown code target language: " ^ quote target);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   250
    val seri = get_seri data;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   251
    val reserved = the_reserved data;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   252
    val includes = Symtab.dest (the_includes data);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   253
    val alias = the_module_alias data;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   254
    val { class, inst, tyco, const } = the_syntax_expr data;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   255
    val project = if target = target_diag then K I
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   256
      else CodeThingol.project_code permissive
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   257
        (Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   258
    fun check_empty_funs code = case filter_out (member (op =) exc)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   259
        (CodeThingol.empty_funs code)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   260
     of [] => code
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   261
      | names => error ("No defining equations for "
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   262
          ^ commas (map (CodeName.labelled_name thy) names));
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   263
  in fn module => fn args => fn code => fn cs =>
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   264
    seri module args (CodeName.labelled_name thy) reserved includes
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   265
      (Symtab.lookup alias) (Symtab.lookup class)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   266
      (Symtab.lookup tyco) (Symtab.lookup const)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   267
        ((check_empty_funs o project cs) code) (these cs)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   268
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   269
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   270
val serialize = get_serializer the_serializer;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   271
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   272
fun parse_args f args =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   273
  case Scan.read Args.stopper f args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   274
   of SOME x => x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   275
    | NONE => error "Bad serializer arguments";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   276
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   277
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   278
(** generic output combinators **)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   279
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   280
(* applications and bindings *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   281
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   282
fun gen_pr_app pr_app' pr_term const_syntax labelled_name is_cons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   283
      lhs vars fxy (app as ((c, (_, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   284
  case const_syntax c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   285
   of NONE => if lhs andalso not (is_cons c) then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
          error ("non-constructor on left hand side of equation: " ^ labelled_name c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
        else brackify fxy (pr_app' lhs vars app)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   288
    | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   289
        let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
          val k = if i < 0 then length tys else i;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
          fun pr' fxy ts = pr (pr_term lhs) vars fxy (ts ~~ curry Library.take k tys);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
        in if k = length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
          then pr' fxy ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
        else if k < length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
          then case chop k ts of (ts1, ts2) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   296
            brackify fxy (pr' APP ts1 :: map (pr_term lhs vars BR) ts2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
          else pr_term lhs vars fxy (CodeThingol.eta_expand app k)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
        end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   299
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   300
fun gen_pr_bind pr_bind' pr_term fxy ((v, pat), ty) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   301
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   302
    val vs = case pat
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
     of SOME pat => CodeThingol.fold_varnames (insert (op =)) pat []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
      | NONE => [];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   305
    val vars' = CodeName.intro_vars (the_list v) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
    val vars'' = CodeName.intro_vars vs vars';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
    val v' = Option.map (CodeName.lookup_var vars') v;
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   308
    val pat' = Option.map (pr_term true vars'' fxy) pat;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   309
  in (pr_bind' ((v', pat'), ty), vars'') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
(* list, char, string, numeral and monad abstract syntax transformations *)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
fun implode_list c_nil c_cons t =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
    fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
          if c = c_cons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
          then SOME (t1, t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
      | dest_cons _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
    val (ts, t') = CodeThingol.unfoldr dest_cons t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
  in case t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
   of IConst (c, _) => if c = c_nil then SOME ts else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
    | _ => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
        fun idx c = find_index (curry (op =) c) c_nibbles;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   330
        fun decode ~1 _ = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
          | decode _ ~1 = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
          | decode n m = SOME (chr (n * 16 + m));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   333
      in decode (idx c1) (idx c2) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
  | decode_char _ _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   335
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   336
fun implode_string c_char c_nibbles mk_char mk_string ts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   337
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   338
    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   339
          if c = c_char then decode_char c_nibbles (t1, t2) else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   340
      | implode_char _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   341
    val ts' = map implode_char ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   342
  in if forall is_some ts'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   343
    then (SOME o str o mk_string o implode o map_filter I) ts'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   344
    else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   345
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   346
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
   347
fun implode_numeral negative c_pls c_min c_bit0 c_bit1 =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
  let
25936
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   349
    fun dest_bit (IConst (c, _)) = if c = c_bit0 then 0
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   350
          else if c = c_bit1 then 1
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   351
          else error "Illegal numeral expression: illegal bit"
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   352
      | dest_bit _ = error "Illegal numeral expression: illegal bit";
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   353
    fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0
25936
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   354
          else if c = c_min then
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   355
            if negative then SOME ~1 else NONE
25969
haftmann
parents: 25936
diff changeset
   356
          else error "Illegal numeral expression: illegal leading digit"
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
   357
      | dest_numeral (t1 `$ t2) =
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
   358
          let val (n, b) = (dest_numeral t2, dest_bit t1)
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
   359
          in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
25969
haftmann
parents: 25936
diff changeset
   360
      | dest_numeral _ = error "Illegal numeral expression: illegal constant";
25936
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   361
  in dest_numeral #> the_default 0 end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   362
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   363
fun implode_monad c_bind t =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   364
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   365
    fun dest_monad (IConst (c, _) `$ t1 `$ t2) =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   366
          if c = c_bind
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   367
            then case CodeThingol.split_abs t2
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   368
             of SOME (((v, pat), ty), t') =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   369
                  SOME ((SOME (((SOME v, pat), ty), true), t1), t')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   370
              | NONE => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   371
            else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   372
      | dest_monad t = case CodeThingol.split_let t
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   373
           of SOME (((pat, ty), tbind), t') =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   374
                SOME ((SOME (((NONE, SOME pat), ty), false), tbind), t')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   375
            | NONE => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   376
  in CodeThingol.unfoldr dest_monad t end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   377
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   378
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   379
(* name auxiliary *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   380
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   381
val first_upper = implode o nth_map 0 Symbol.to_ascii_upper o explode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   382
val first_lower = implode o nth_map 0 Symbol.to_ascii_lower o explode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   383
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   384
val dest_name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   385
  apfst NameSpace.implode o split_last o fst o split_last o NameSpace.explode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   386
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   387
fun mk_modl_name_tab init_names prefix module_alias code =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   388
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   389
    fun nsp_map f = NameSpace.explode #> map f #> NameSpace.implode;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   390
    fun mk_alias name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   391
     case module_alias name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   392
      of SOME name' => name'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   393
       | NONE => nsp_map (fn name => (the_single o fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   394
            (Name.variants [name] init_names)) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   395
    fun mk_prefix name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   396
      case prefix
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   397
       of SOME prefix => NameSpace.append prefix name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   398
        | NONE => name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   399
    val tab =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   400
      Symtab.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   401
      |> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   402
           o fst o dest_name o fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   403
             code
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   404
  in fn name => (the o Symtab.lookup tab) name end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   405
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   406
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   407
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   408
(** SML/OCaml serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   409
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   410
datatype ml_def =
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   411
    MLFuns of (string * (typscheme * (iterm list * iterm) list)) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   412
  | MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
   413
  | MLClass of string * (vname * ((class * string) list * (string * itype) list))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   414
  | MLClassinst of string * ((class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   415
        * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   416
      * (string * const) list));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   417
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   418
fun pr_sml tyco_syntax const_syntax labelled_name init_syms deresolv is_cons ml_def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   419
  let
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   420
    val pr_label_classrel = translate_string (fn "." => "__" | c => c)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   421
      o NameSpace.qualifier;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   422
    val pr_label_classparam = NameSpace.base o NameSpace.qualifier;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   423
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   424
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   425
        fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   426
          | pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   427
        fun pr_proj [] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   428
              p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   429
          | pr_proj [p'] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   430
              brackets [p', p]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   431
          | pr_proj (ps as _ :: _) p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   432
              brackets [Pretty.enum " o" "(" ")" ps, p];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   433
        fun pr_dictc fxy (DictConst (inst, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   434
              brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   435
          | pr_dictc fxy (DictVar (classrels, v)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   436
              pr_proj (map (str o deresolv) classrels) ((str o pr_dictvar) v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   437
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   438
       of [] => str "()"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   439
        | [d] => pr_dictc fxy d
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   440
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   441
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   442
    fun pr_tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   443
      vs
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   444
      |> map (fn (v, sort) => map_index (fn (i, _) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   445
           DictVar ([], (v, (i, length sort)))) sort)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   446
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   447
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   448
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   449
        val tyco' = (str o deresolv) tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   450
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   451
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   452
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   453
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   454
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   455
    and pr_typ fxy (tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   456
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   457
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   458
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   459
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   460
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   461
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   462
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   463
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   464
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   465
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   466
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   467
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   468
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   469
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   470
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   471
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   472
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   473
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   474
                brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   475
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   476
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   477
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   478
            fun pr ((v, pat), ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   479
              pr_bind NOBR ((SOME v, pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   480
              #>> (fn p => concat [str "fn", p, str "=>"]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   481
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   482
          in brackets (ps @ [pr_term lhs vars' NOBR t']) end
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   483
      | pr_term lhs vars fxy (ICase (cases as (_, t0))) =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   484
          (case CodeThingol.unfold_const_app t0
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   485
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   486
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   487
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   488
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   489
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   490
      if is_cons c then let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   491
        val k = length tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   492
      in if k < 2 then 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   493
        (str o deresolv) c :: map (pr_term lhs vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   494
      else if k = length ts then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   495
        [(str o deresolv) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   496
      else [pr_term lhs vars BR (CodeThingol.eta_expand app k)] end else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   497
        (str o deresolv) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   498
          :: (map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   499
    and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   500
      labelled_name is_cons lhs vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   501
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   502
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   503
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   504
      | pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   505
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   506
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   507
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   508
            val (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   509
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   510
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   511
              |> pr_bind NOBR ((NONE, SOME pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   512
              |>> (fn p => semicolon [str "val", p, str "=", pr_term false vars NOBR t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   513
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   514
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   515
            Pretty.chunks [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   516
              [str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   517
              [str ("in"), Pretty.fbrk, pr_term false vars' NOBR t'] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   518
              str ("end")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   519
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   520
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   521
      | pr_case vars fxy (((td, ty), b::bs), _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   522
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   523
            fun pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   524
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   525
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   526
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   527
                concat [str delim, p, str "=>", pr_term false vars' NOBR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   528
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   529
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   530
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   531
              str "case"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   532
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   533
              :: pr "of" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   534
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   535
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   536
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   537
      | pr_case vars fxy ((_, []), _) = str "raise Fail \"empty case\""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   538
    fun pr_def (MLFuns (funns as (funn :: funns'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   539
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   540
            val definer =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   541
              let
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   542
                fun no_args _ ((ts, _) :: _) = length ts
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   543
                  | no_args ty [] = (length o fst o CodeThingol.unfold_fun) ty;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   544
                fun mk 0 [] = "val"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   545
                  | mk 0 vs = if (null o filter_out (null o snd)) vs
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   546
                      then "val" else "fun"
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   547
                  | mk k _ = "fun";
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   548
                fun chk (_, ((vs, ty), eqs)) NONE = SOME (mk (no_args ty eqs) vs)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   549
                  | chk (_, ((vs, ty), eqs)) (SOME defi) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   550
                      if defi = mk (no_args ty eqs) vs then SOME defi
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   551
                      else error ("Mixing simultaneous vals and funs not implemented: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   552
                        ^ commas (map (labelled_name o fst) funns));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   553
              in the (fold chk funns NONE) end;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   554
            fun pr_funn definer (name, ((raw_vs, ty), [])) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   555
                  let
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   556
                    val vs = filter_out (null o snd) raw_vs;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   557
                    val n = length vs + (length o fst o CodeThingol.unfold_fun) ty;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   558
                    val exc_str =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   559
                      (ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   560
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   561
                    concat (
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   562
                      str definer
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   563
                      :: (str o deresolv) name
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   564
                      :: map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   565
                      @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   566
                      :: str "raise"
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   567
                      :: str "(Fail"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   568
                      :: str exc_str
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   569
                      @@ str ")"
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   570
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   571
                  end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   572
              | pr_funn definer (name, ((raw_vs, ty), eqs as eq :: eqs')) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   573
                  let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   574
                    val vs = filter_out (null o snd) raw_vs;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   575
                    val shift = if null eqs' then I else
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   576
                      map (Pretty.block o single o Pretty.block o single);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   577
                    fun pr_eq definer (ts, t) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   578
                      let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   579
                        val consts = map_filter
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   580
                          (fn c => if (is_some o const_syntax) c
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   581
                            then NONE else (SOME o NameSpace.base o deresolv) c)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   582
                            ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   583
                        val vars = init_syms
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   584
                          |> CodeName.intro_vars consts
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   585
                          |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   586
                               (insert (op =)) ts []);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   587
                      in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   588
                        concat (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   589
                          [str definer, (str o deresolv) name]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   590
                          @ (if null ts andalso null vs
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   591
                             then [str ":", pr_typ NOBR ty]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   592
                             else
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   593
                               pr_tyvars vs
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   594
                               @ map (pr_term true vars BR) ts)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   595
                       @ [str "=", pr_term false vars NOBR t]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   596
                        )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   597
                      end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   598
                  in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   599
                    (Pretty.block o Pretty.fbreaks o shift) (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   600
                      pr_eq definer eq
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   601
                      :: map (pr_eq "|") eqs'
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   602
                    )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   603
                  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   604
            val (ps, p) = split_last (pr_funn definer funn :: map (pr_funn "and") funns');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   605
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   606
     | pr_def (MLDatas (datas as (data :: datas'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   607
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   608
            fun pr_co (co, []) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   609
                  str (deresolv co)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   610
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   611
                  concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   612
                    str (deresolv co),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   613
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   614
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   615
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   616
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   617
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   618
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   619
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   620
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   621
                    @@ str "EMPTY__" 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   622
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   623
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   624
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   625
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   626
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   627
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   628
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   629
                  );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   630
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   631
              (pr_data "datatype" data :: map (pr_data "and") datas');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   632
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   633
     | pr_def (MLClass (class, (v, (superclasses, classparams)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   634
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   635
            val w = first_upper v ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   636
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   637
              (concat o map str) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   638
                pr_label_classrel classrel, ":", "'" ^ v, deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   639
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   640
            fun pr_classparam_field (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   641
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   642
                (str o pr_label_classparam) classparam, str ":", pr_typ NOBR ty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   643
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   644
            fun pr_classparam_proj (classparam, _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   645
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   646
                str "fun",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   647
                (str o deresolv) classparam,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   648
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   649
                str "=",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   650
                str ("#" ^ pr_label_classparam classparam),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   651
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   652
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   653
            fun pr_superclass_proj (_, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   654
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   655
                str "fun",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   656
                (str o deresolv) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   657
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolv class)],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   658
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   659
                str ("#" ^ pr_label_classrel classrel),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   660
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   661
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   662
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   663
            Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   664
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   665
                str ("type '" ^ v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   666
                (str o deresolv) class,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   667
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   668
                Pretty.enum "," "{" "};" (
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   669
                  map pr_superclass_field superclasses @ map pr_classparam_field classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   670
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   671
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   672
              :: map pr_superclass_proj superclasses
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   673
              @ map pr_classparam_proj classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   674
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   675
          end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   676
     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   677
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   678
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   679
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   680
                (str o pr_label_classrel) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   681
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   682
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   683
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   684
            fun pr_classparam (classparam, c_inst) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   685
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   686
                (str o pr_label_classparam) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   687
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   688
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   689
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   690
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   691
            semicolon ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   692
              str (if null arity then "val" else "fun"),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   693
              (str o deresolv) inst ] @
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   694
              pr_tyvars arity @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   695
              str "=",
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   696
              Pretty.enum "," "{" "}"
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   697
                (map pr_superclass superarities @ map pr_classparam classparam_insts),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   698
              str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   699
              pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   700
            ])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   701
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   702
  in pr_def ml_def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   703
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   704
fun pr_sml_modl name content =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   705
  Pretty.chunks ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   706
    str ("structure " ^ name ^ " = "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   707
    str "struct",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   708
    str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   709
  ] @ content @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   710
    str "",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   711
    str ("end; (*struct " ^ name ^ "*)")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   712
  ]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   713
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   714
fun pr_ocaml tyco_syntax const_syntax labelled_name
24621
97d403d9ab54 clarified evaluation code
haftmann
parents: 24591
diff changeset
   715
    init_syms deresolv is_cons ml_def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   716
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   717
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   718
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   719
        fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   720
          | pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   721
        fun pr_proj ps p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   722
          fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   723
        fun pr_dictc fxy (DictConst (inst, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   724
              brackify fxy ((str o deresolv) inst :: map (pr_dicts BR) dss)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   725
          | pr_dictc fxy (DictVar (classrels, v)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   726
              pr_proj (map deresolv classrels) ((str o pr_dictvar) v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   727
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   728
       of [] => str "()"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   729
        | [d] => pr_dictc fxy d
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   730
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dictc NOBR)) ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   731
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   732
    fun pr_tyvars vs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   733
      vs
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   734
      |> map (fn (v, sort) => map_index (fn (i, _) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   735
           DictVar ([], (v, (i, length sort)))) sort)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   736
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   737
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   738
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   739
        val tyco' = (str o deresolv) tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   740
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   741
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   742
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   743
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   744
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   745
    and pr_typ fxy (tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   746
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   747
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   748
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   749
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   750
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   751
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   752
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   753
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   754
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   755
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   756
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   757
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   758
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   759
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   760
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   761
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   762
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   763
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   764
                brackify fxy [pr_term lhs vars NOBR t1, pr_term lhs vars BR t2])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   765
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   766
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   767
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   768
            fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   769
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   770
          in brackets (str "fun" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   771
      | pr_term lhs vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   772
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   773
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   774
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   775
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   776
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   777
      if is_cons c then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   778
        if length tys = length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   779
        then case ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   780
         of [] => [(str o deresolv) c]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   781
          | [t] => [(str o deresolv) c, pr_term lhs vars BR t]
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   782
          | _ => [(str o deresolv) c, Pretty.enum "," "(" ")"
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   783
                    (map (pr_term lhs vars NOBR) ts)]
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   784
        else [pr_term lhs vars BR (CodeThingol.eta_expand app (length tys))]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   785
      else (str o deresolv) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   786
        :: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   787
    and pr_app lhs vars = gen_pr_app pr_app' pr_term const_syntax
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   788
      labelled_name is_cons lhs vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   789
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   790
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   791
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   792
      | pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   793
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   794
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   795
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   796
            val (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   797
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   798
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   799
              |> pr_bind NOBR ((NONE, SOME pat), ty)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   800
              |>> (fn p => concat
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   801
                  [str "let", p, str "=", pr_term false vars NOBR t, str "in"])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   802
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   803
          in Pretty.chunks (ps @| pr_term false vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   804
      | pr_case vars fxy (((td, ty), b::bs), _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   805
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   806
            fun pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   807
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   808
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   809
              in concat [str delim, p, str "->", pr_term false vars' NOBR t] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   810
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   811
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   812
              str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   813
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   814
              :: pr "with" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   815
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   816
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   817
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   818
      | pr_case vars fxy ((_, []), _) = str "failwith \"empty case\"";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   819
    fun pr_def (MLFuns (funns as funn :: funns')) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   820
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   821
            fun fish_parm _ (w as SOME _) = w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   822
              | fish_parm (IVar v) NONE = SOME v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   823
              | fish_parm _ NONE = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   824
            fun fillup_parm _ (_, SOME v) = v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   825
              | fillup_parm x (i, NONE) = x ^ string_of_int i;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   826
            fun fish_parms vars eqs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   827
              let
24294
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   828
                val fished1 = fold (map2 fish_parm) eqs (replicate (length (hd eqs)) NONE);
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   829
                val x = Name.variant (map_filter I fished1) "x";
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   830
                val fished2 = map_index (fillup_parm x) fished1;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   831
                val (fished3, _) = Name.variants fished2 Name.context;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   832
                val vars' = CodeName.intro_vars fished3 vars;
edfe16773fd4 fixed OCaml bug
haftmann
parents: 24284
diff changeset
   833
              in map (CodeName.lookup_var vars') fished3 end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   834
            fun pr_eq (ts, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   835
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   836
                val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   837
                  (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   838
                    then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   839
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   840
                val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   841
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   842
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   843
                      (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   844
              in concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   845
                (Pretty.block o Pretty.commas) (map (pr_term true vars NOBR) ts),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   846
                str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   847
                pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   848
              ] end;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   849
            fun pr_eqs name ty [] =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   850
                  let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   851
                    val n = (length o fst o CodeThingol.unfold_fun) ty;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   852
                    val exc_str =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   853
                      (ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   854
                  in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   855
                    concat (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   856
                      map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   857
                      @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   858
                      :: str "failwith"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   859
                      @@ str exc_str
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   860
                    )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   861
                  end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   862
              | pr_eqs _ _ [(ts, t)] =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   863
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   864
                    val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   865
                      (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   866
                        then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   867
                        ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   868
                    val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   869
                      |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   870
                      |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   871
                          (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   872
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   873
                    concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   874
                      map (pr_term true vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   875
                      @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   876
                      @@ pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   877
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   878
                  end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   879
              | pr_eqs _ _ (eqs as (eq as ([_], _)) :: eqs') =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   880
                  Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   881
                    str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   882
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   883
                    :: str "function"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   884
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   885
                    :: pr_eq eq
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   886
                    :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   887
                          o single o pr_eq) eqs'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   888
                  )
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   889
              | pr_eqs _ _ (eqs as eq :: eqs') =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   890
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   891
                    val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   892
                      (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   893
                        then NONE else (SOME o NameSpace.base o deresolv) c)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   894
                        ((fold o CodeThingol.fold_constnames)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   895
                          (insert (op =)) (map snd eqs) []);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   896
                    val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   897
                      |> CodeName.intro_vars consts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   898
                    val dummy_parms = (map str o fish_parms vars o map fst) eqs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   899
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   900
                    Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   901
                      Pretty.breaks dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   902
                      @ Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   903
                      :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   904
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   905
                      :: str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   906
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   907
                      :: (Pretty.block o Pretty.commas) dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   908
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   909
                      :: str "with"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   910
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   911
                      :: pr_eq eq
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   912
                      :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   913
                           o single o pr_eq) eqs'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   914
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   915
                  end;
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   916
            fun pr_funn definer (name, ((vs, ty), eqs)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   917
              concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   918
                str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   919
                :: (str o deresolv) name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   920
                :: pr_tyvars (filter_out (null o snd) vs)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   921
                @| pr_eqs name ty eqs
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   922
              );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   923
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   924
              (pr_funn "let rec" funn :: map (pr_funn "and") funns');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   925
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   926
     | pr_def (MLDatas (datas as (data :: datas'))) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   927
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   928
            fun pr_co (co, []) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   929
                  str (deresolv co)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   930
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   931
                  concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   932
                    str (deresolv co),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   933
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   934
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   935
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   936
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   937
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   938
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   939
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   940
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   941
                    @@ str "EMPTY_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   942
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   943
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   944
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   945
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   946
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   947
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   948
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   949
                  );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   950
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   951
              (pr_data "type" data :: map (pr_data "and") datas');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   952
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   953
     | pr_def (MLClass (class, (v, (superclasses, classparams)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   954
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   955
            val w = "_" ^ first_upper v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   956
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   957
              (concat o map str) [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   958
                deresolv classrel, ":", "'" ^ v, deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   959
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   960
            fun pr_classparam_field (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   961
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   962
                (str o deresolv) classparam, str ":", pr_typ NOBR ty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   963
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   964
            fun pr_classparam_proj (classparam, _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   965
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   966
                str "let",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   967
                (str o deresolv) classparam,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   968
                str w,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   969
                str "=",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   970
                str (w ^ "." ^ deresolv classparam ^ ";;")
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   971
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   972
          in Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   973
            concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   974
              str ("type '" ^ v),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   975
              (str o deresolv) class,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   976
              str "=",
25771
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
   977
              enum_default "();;" ";" "{" "};;" (
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   978
                map pr_superclass_field superclasses
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   979
                @ map pr_classparam_field classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   980
              )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   981
            ]
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   982
            :: map pr_classparam_proj classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   983
          ) end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   984
     | pr_def (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   985
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   986
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   987
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   988
                (str o deresolv) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   989
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   990
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   991
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   992
            fun pr_classparam_inst (classparam, c_inst) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   993
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   994
                (str o deresolv) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   995
                str "=",
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   996
                pr_app false init_syms NOBR (c_inst, [])
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   997
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   998
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   999
            concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1000
              str "let"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1001
              :: (str o deresolv) inst
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1002
              :: pr_tyvars arity
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1003
              @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1004
              @@ (Pretty.enclose "(" ");;" o Pretty.breaks) [
25771
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
  1005
                enum_default "()" ";" "{" "}" (map pr_superclass superarities
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1006
                  @ map pr_classparam_inst classparam_insts),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1007
                str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1008
                pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1009
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1010
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1011
          end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1012
  in pr_def ml_def end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1013
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1014
fun pr_ocaml_modl name content =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1015
  Pretty.chunks ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1016
    str ("module " ^ name ^ " = "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1017
    str "struct",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1018
    str ""
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1019
  ] @ content @ [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1020
    str "",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1021
    str ("end;; (*struct " ^ name ^ "*)")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1022
  ]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1023
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1024
fun seri_ml internal output_cont pr_def pr_modl module labelled_name reserved_syms includes raw_module_alias
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1025
  (_ : string -> class_syntax option) tyco_syntax const_syntax code cs seri_dest =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1026
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1027
    val module_alias = if is_some module then K module else raw_module_alias;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1028
    val is_cons = CodeThingol.is_cons code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1029
    datatype node =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1030
        Def of string * ml_def option
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1031
      | Module of string * ((Name.context * Name.context) * node Graph.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1032
    val init_names = Name.make_context reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1033
    val init_module = ((init_names, init_names), Graph.empty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1034
    fun map_node [] f = f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1035
      | map_node (m::ms) f =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1036
          Graph.default_node (m, Module (m, init_module))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1037
          #> Graph.map_node m (fn (Module (dmodlname, (nsp, nodes))) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1038
               Module (dmodlname, (nsp, map_node ms f nodes)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1039
    fun map_nsp_yield [] f (nsp, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1040
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1041
            val (x, nsp') = f nsp
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1042
          in (x, (nsp', nodes)) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1043
      | map_nsp_yield (m::ms) f (nsp, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1044
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1045
            val (x, nodes') =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1046
              nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1047
              |> Graph.default_node (m, Module (m, init_module))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1048
              |> Graph.map_node_yield m (fn Module (dmodlname, nsp_nodes) => 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1049
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1050
                    val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1051
                  in (x, Module (dmodlname, nsp_nodes')) end)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1052
          in (x, (nsp, nodes')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1053
    val init_syms = CodeName.make_vars reserved_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1054
    val name_modl = mk_modl_name_tab init_names NONE module_alias code;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1055
    fun name_def upper name nsp =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1056
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1057
        val (_, base) = dest_name name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1058
        val base' = if upper then first_upper base else base;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1059
        val ([base''], nsp') = Name.variants [base'] nsp;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1060
      in (base'', nsp') end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1061
    fun map_nsp_fun f (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1062
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1063
        val (x, nsp_fun') = f nsp_fun
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1064
      in (x, (nsp_fun', nsp_typ)) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1065
    fun map_nsp_typ f (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1066
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1067
        val (x, nsp_typ') = f nsp_typ
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1068
      in (x, (nsp_fun, nsp_typ')) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1069
    fun mk_funs defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1070
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1071
        (fn (name, CodeThingol.Fun info) =>
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1072
              map_nsp_fun (name_def false name) >>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1073
                (fn base => (base, (name, (apsnd o map) fst info)))
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1074
          | (name, def) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1075
              error ("Function block containing illegal definition: " ^ labelled_name name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1076
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1077
      >> (split_list #> apsnd MLFuns);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1078
    fun mk_datatype defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1079
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1080
        (fn (name, CodeThingol.Datatype info) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1081
              map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1082
          | (name, CodeThingol.Datatypecons _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1083
              map_nsp_fun (name_def true name) >> (fn base => (base, NONE))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1084
          | (name, def) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1085
              error ("Datatype block containing illegal definition: " ^ labelled_name name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1086
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1087
      >> (split_list #> apsnd (map_filter I
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1088
        #> (fn [] => error ("Datatype block without data definition: "
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1089
                  ^ (commas o map (labelled_name o fst)) defs)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1090
             | infos => MLDatas infos)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1091
    fun mk_class defs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1092
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1093
        (fn (name, CodeThingol.Class info) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1094
              map_nsp_typ (name_def false name) >> (fn base => (base, SOME (name, info)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1095
          | (name, CodeThingol.Classrel _) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1096
              map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1097
          | (name, CodeThingol.Classparam _) =>
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1098
              map_nsp_fun (name_def false name) >> (fn base => (base, NONE))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1099
          | (name, def) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1100
              error ("Class block containing illegal definition: " ^ labelled_name name)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1101
        ) defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1102
      >> (split_list #> apsnd (map_filter I
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1103
        #> (fn [] => error ("Class block without class definition: "
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1104
                  ^ (commas o map (labelled_name o fst)) defs)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1105
             | [info] => MLClass info)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1106
    fun mk_inst [(name, CodeThingol.Classinst info)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1107
      map_nsp_fun (name_def false name)
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1108
      >> (fn base => ([base], MLClassinst (name, (apsnd o apsnd o map) fst info)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1109
    fun add_group mk defs nsp_nodes =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1110
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1111
        val names as (name :: names') = map fst defs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1112
        val deps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1113
          []
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1114
          |> fold (fold (insert (op =)) o Graph.imm_succs code) names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1115
          |> subtract (op =) names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1116
        val (modls, _) = (split_list o map dest_name) names;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1117
        val modl' = (the_single o distinct (op =) o map name_modl) modls
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1118
          handle Empty =>
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1119
            error ("Different namespace prefixes for mutual dependencies:\n"
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1120
              ^ commas (map labelled_name names)
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1121
              ^ "\n"
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1122
              ^ commas (map (NameSpace.qualifier o NameSpace.qualifier) names));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1123
        val modl_explode = NameSpace.explode modl';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1124
        fun add_dep name name'' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1125
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1126
            val modl'' = (name_modl o fst o dest_name) name'';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1127
          in if modl' = modl'' then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1128
            map_node modl_explode
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1129
              (Graph.add_edge (name, name''))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1130
          else let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1131
            val (common, (diff1::_, diff2::_)) = chop_prefix (op =)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1132
              (modl_explode, NameSpace.explode modl'');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1133
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1134
            map_node common
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1135
              (fn gr => Graph.add_edge_acyclic (diff1, diff2) gr
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1136
                handle Graph.CYCLES _ => error ("Dependency "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1137
                  ^ quote name ^ " -> " ^ quote name''
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1138
                  ^ " would result in module dependency cycle"))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1139
          end end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1140
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1141
        nsp_nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1142
        |> map_nsp_yield modl_explode (mk defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1143
        |-> (fn (base' :: bases', def') =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1144
           apsnd (map_node modl_explode (Graph.new_node (name, (Def (base', SOME def')))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1145
              #> fold2 (fn name' => fn base' =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1146
                   Graph.new_node (name', (Def (base', NONE)))) names' bases')))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1147
        |> apsnd (fold (fn name => fold (add_dep name) deps) names)
25538
58e8ba3b792b map_product and fold_product
haftmann
parents: 25204
diff changeset
  1148
        |> apsnd (fold_product (curry (map_node modl_explode o Graph.add_edge)) names names)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1149
      end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1150
    fun group_defs [(_, CodeThingol.Bot)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1151
          I
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1152
      | group_defs ((defs as (_, CodeThingol.Fun _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1153
          add_group mk_funs defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1154
      | group_defs ((defs as (_, CodeThingol.Datatypecons _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1155
          add_group mk_datatype defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1156
      | group_defs ((defs as (_, CodeThingol.Datatype _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1157
          add_group mk_datatype defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1158
      | group_defs ((defs as (_, CodeThingol.Class _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1159
          add_group mk_class defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1160
      | group_defs ((defs as (_, CodeThingol.Classrel _)::_)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1161
          add_group mk_class defs
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1162
      | group_defs ((defs as (_, CodeThingol.Classparam _)::_)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1163
          add_group mk_class defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1164
      | group_defs ((defs as [(_, CodeThingol.Classinst _)])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1165
          add_group mk_inst defs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1166
      | group_defs defs = error ("Illegal mutual dependencies: " ^
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1167
          (commas o map (labelled_name o fst)) defs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1168
    val (_, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1169
      init_module
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1170
      |> fold group_defs (map (AList.make (Graph.get_node code))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1171
          (rev (Graph.strong_conn code)))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1172
    fun deresolver prefix name = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1173
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1174
        val modl = (fst o dest_name) name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1175
        val modl' = (NameSpace.explode o name_modl) modl;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1176
        val (_, (_, remainder)) = chop_prefix (op =) (prefix, modl');
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1177
        val defname' =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1178
          nodes
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1179
          |> fold (fn m => fn g => case Graph.get_node g m
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1180
              of Module (_, (_, g)) => g) modl'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1181
          |> (fn g => case Graph.get_node g name of Def (defname, _) => defname);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1182
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1183
        NameSpace.implode (remainder @ [defname'])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1184
      end handle Graph.UNDEF _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1185
        error ("Unknown definition name: " ^ labelled_name name);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1186
    fun pr_node prefix (Def (_, NONE)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1187
          NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1188
      | pr_node prefix (Def (_, SOME def)) =
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1189
          SOME (pr_def tyco_syntax const_syntax labelled_name init_syms
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1190
            (deresolver prefix) is_cons def)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1191
      | pr_node prefix (Module (dmodlname, (_, nodes))) =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1192
          SOME (pr_modl dmodlname (
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1193
            separate (str "")
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1194
                ((map_filter (pr_node (prefix @ [dmodlname]) o Graph.get_node nodes)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1195
                o rev o flat o Graph.strong_conn) nodes)));
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1196
    val p = Pretty.chunks (separate (str "") (map snd includes @ (map_filter
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26086
diff changeset
  1197
      (pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes));
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26086
diff changeset
  1198
    val deresolv = try (deresolver (if is_some module then the_list module else []));
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1199
    val output = case seri_dest
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1200
     of Compile => K NONE o internal o code_source
27001
d21bb9f73364 fixed utterly wrong print mode handling
haftmann
parents: 27000
diff changeset
  1201
      | Write => K NONE o code_writeln
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1202
      | File file => K NONE o File.write file o code_source
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1203
      | String => SOME o code_source;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1204
  in output_cont (map_filter deresolv cs, output p) end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1205
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1206
fun isar_seri_sml module =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1207
  parse_args (Scan.succeed ())
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1208
  #> (fn () => seri_ml (use_text (1, "generated code") Output.ml_output false) snd pr_sml pr_sml_modl module);
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26086
diff changeset
  1209
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1210
fun isar_seri_ocaml module =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1211
  parse_args (Scan.succeed ())
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1212
  #> (fn () => seri_ml (fn _ => error "OCaml: no internal compilation") snd pr_ocaml pr_ocaml_modl module)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1213
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1214
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1215
(** Haskell serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1216
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1217
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1218
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1219
fun pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1220
  | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1221
  | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1222
  | pr_bind' ((SOME v, SOME p), _) = brackets [str v, str "@", p]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1223
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1224
val pr_bind_haskell = gen_pr_bind pr_bind';
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1225
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1226
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1227
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1228
fun pr_haskell class_syntax tyco_syntax const_syntax labelled_name
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1229
    init_syms deresolv_here deresolv is_cons contr_classparam_typs deriving_show def =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1230
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1231
    fun class_name class = case class_syntax class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1232
     of NONE => deresolv class
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1233
      | SOME (class, _) => class;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1234
    fun classparam_name class classparam = case class_syntax class
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1235
     of NONE => deresolv_here classparam
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1236
      | SOME (_, classparam_syntax) => case classparam_syntax classparam
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1237
         of NONE => (snd o dest_name) classparam
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1238
          | SOME classparam => classparam;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1239
    fun pr_typcontext tyvars vs = case maps (fn (v, sort) => map (pair v) sort) vs
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1240
     of [] => []
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1241
      | classbinds => Pretty.enum "," "(" ")" (
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1242
          map (fn (v, class) =>
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1243
            str (class_name class ^ " " ^ CodeName.lookup_var tyvars v)) classbinds)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1244
          @@ str " => ";
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1245
    fun pr_typforall tyvars vs = case map fst vs
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1246
     of [] => []
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1247
      | vnames => str "forall " :: Pretty.breaks
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1248
          (map (str o CodeName.lookup_var tyvars) vnames) @ str "." @@ Pretty.brk 1;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1249
    fun pr_tycoexpr tyvars fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1250
      brackify fxy (str tyco :: map (pr_typ tyvars BR) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1251
    and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1252
          (case tyco_syntax tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1253
           of NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1254
                pr_tycoexpr tyvars fxy (deresolv tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1255
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1256
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1257
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1258
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1259
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1260
                else pr (pr_typ tyvars) fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1261
      | pr_typ tyvars fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1262
          (str o CodeName.lookup_var tyvars) v;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1263
    fun pr_typdecl tyvars (vs, tycoexpr) =
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1264
      Pretty.block (pr_typcontext tyvars vs @| pr_tycoexpr tyvars NOBR tycoexpr);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1265
    fun pr_typscheme tyvars (vs, ty) =
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1266
      Pretty.block (pr_typforall tyvars vs @ pr_typcontext tyvars vs @| pr_typ tyvars NOBR ty);
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1267
    fun pr_term tyvars lhs vars fxy (IConst c) =
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1268
          pr_app tyvars lhs vars fxy (c, [])
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1269
      | pr_term tyvars lhs vars fxy (t as (t1 `$ t2)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1270
          (case CodeThingol.unfold_const_app t
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1271
           of SOME app => pr_app tyvars lhs vars fxy app
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1272
            | _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1273
                brackify fxy [
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1274
                  pr_term tyvars lhs vars NOBR t1,
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1275
                  pr_term tyvars lhs vars BR t2
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1276
                ])
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1277
      | pr_term tyvars lhs vars fxy (IVar v) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1278
          (str o CodeName.lookup_var vars) v
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1279
      | pr_term tyvars lhs vars fxy (t as _ `|-> _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1280
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1281
            val (binds, t') = CodeThingol.unfold_abs t;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1282
            fun pr ((v, pat), ty) = pr_bind tyvars BR ((SOME v, pat), ty);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1283
            val (ps, vars') = fold_map pr binds vars;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1284
          in brackets (str "\\" :: ps @ str "->" @@ pr_term tyvars lhs vars' NOBR t') end
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1285
      | pr_term tyvars lhs vars fxy (ICase (cases as (_, t0))) =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1286
          (case CodeThingol.unfold_const_app t0
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1287
           of SOME (c_ts as ((c, _), _)) => if is_none (const_syntax c)
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1288
                then pr_case tyvars vars fxy cases
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1289
                else pr_app tyvars lhs vars fxy c_ts
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1290
            | NONE => pr_case tyvars vars fxy cases)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1291
    and pr_app' tyvars lhs vars ((c, (_, tys)), ts) = case contr_classparam_typs c
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1292
     of [] => (str o deresolv) c :: map (pr_term tyvars lhs vars BR) ts
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1293
      | fingerprint => let
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1294
          val ts_fingerprint = ts ~~ curry Library.take (length ts) fingerprint;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1295
          val needs_annotation = forall (fn (_, NONE) => true | (t, SOME _) =>
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1296
            (not o CodeThingol.locally_monomorphic) t) ts_fingerprint;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1297
          fun pr_term_anno (t, NONE) _ = pr_term tyvars lhs vars BR t
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1298
            | pr_term_anno (t, SOME _) ty =
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1299
                brackets [pr_term tyvars lhs vars NOBR t, str "::", pr_typ tyvars NOBR ty];
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1300
        in
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1301
          if needs_annotation then
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1302
            (str o deresolv) c :: map2 pr_term_anno ts_fingerprint (curry Library.take (length ts) tys)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1303
          else (str o deresolv) c :: map (pr_term tyvars lhs vars BR) ts
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1304
        end
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1305
    and pr_app tyvars lhs vars  = gen_pr_app (pr_app' tyvars) (pr_term tyvars) const_syntax
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1306
      labelled_name is_cons lhs vars
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1307
    and pr_bind tyvars = pr_bind_haskell (pr_term tyvars)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1308
    and pr_case tyvars vars fxy (cases as ((_, [_]), _)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1309
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1310
            val (binds, t) = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1311
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1312
              vars
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1313
              |> pr_bind tyvars BR ((NONE, SOME pat), ty)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1314
              |>> (fn p => semicolon [p, str "=", pr_term tyvars false vars NOBR t])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1315
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1316
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1317
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1318
              str "let {",
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1319
              concat [str "}", str "in", pr_term tyvars false vars' NOBR t]
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1320
            ) ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1321
          end
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1322
      | pr_case tyvars vars fxy (((td, ty), bs as _ :: _), _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1323
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1324
            fun pr (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1325
              let
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1326
                val (p, vars') = pr_bind tyvars NOBR ((NONE, SOME pat), ty) vars;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1327
              in semicolon [p, str "->", pr_term tyvars false vars' NOBR t] end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1328
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1329
            Pretty.block_enclose (
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1330
              concat [str "(case", pr_term tyvars false vars NOBR td, str "of", str "{"],
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1331
              str "})"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1332
            ) (map pr bs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1333
          end
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1334
      | pr_case tyvars vars fxy ((_, []), _) = str "error \"empty case\"";
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1335
    fun pr_def (name, CodeThingol.Fun ((vs, ty), [])) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1336
          let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1337
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1338
            val n = (length o fst o CodeThingol.unfold_fun) ty;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1339
          in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1340
            Pretty.chunks [
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1341
              Pretty.block [
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1342
                (str o suffix " ::" o deresolv_here) name,
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1343
                Pretty.brk 1,
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1344
                pr_typscheme tyvars (vs, ty),
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1345
                str ";"
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1346
              ],
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1347
              concat (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1348
                (str o deresolv_here) name
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1349
                :: map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1350
                @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1351
                :: str "error"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1352
                @@ (str o (fn s => s ^ ";") o ML_Syntax.print_string
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1353
                    o NameSpace.base o NameSpace.qualifier) name
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1354
              )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1355
            ]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1356
          end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1357
      | pr_def (name, CodeThingol.Fun ((vs, ty), eqs)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1358
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1359
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1360
            fun pr_eq ((ts, t), _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1361
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1362
                val consts = map_filter
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1363
                  (fn c => if (is_some o const_syntax) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1364
                    then NONE else (SOME o NameSpace.base o deresolv) c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1365
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1366
                val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1367
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1368
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1369
                       (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1370
              in
e558fe311376 new structure for code generator modules
haftmann
parents: