src/Tools/code/code_target.ML
author haftmann
Tue, 10 Jun 2008 15:30:06 +0200
changeset 27103 d8549f4d900b
parent 27024 fcab2dd46872
child 27304 720c8115d723
permissions -rw-r--r--
major refactorings in code generator modules
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
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    31
  val allow_abort: string -> theory -> theory;
24841
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;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    35
  val add_target: string * serializer -> theory -> theory;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    36
  val assert_target: theory -> string -> string;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    37
  val serialize: theory -> string -> string option -> Args.T list
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    38
    -> CodeThingol.program -> string list -> serialization;
27000
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;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    43
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    44
  val code_of: theory -> string -> string -> string list -> string;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    45
  val eval_conv: string * (unit -> thm) option ref
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    46
    -> theory -> cterm -> string list -> thm;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    47
  val eval_term: string * (unit -> 'a) option ref
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    48
    -> theory -> term -> string list -> 'a;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    49
  val shell_command: string (*theory name*) -> string (*cg expr*) -> unit;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    50
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    51
  val setup: theory -> theory;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    52
  val code_width: int ref;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    53
end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    54
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    55
structure CodeTarget : CODE_TARGET =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    56
struct
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    57
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    58
open BasicCodeThingol;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    59
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    60
(** basics **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    61
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    62
infixr 5 @@;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    63
infixr 5 @|;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    64
fun x @@ y = [x, y];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    65
fun xs @| y = xs @ [y];
24634
38db11874724 simplified PrintMode interfaces;
wenzelm
parents: 24630
diff changeset
    66
val str = PrintMode.setmp [] Pretty.str;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    67
val concat = Pretty.block o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    68
val brackets = Pretty.enclose "(" ")" o Pretty.breaks;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    69
fun semicolon ps = Pretty.block [concat ps, str ";"];
25771
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
    70
fun enum_default default sep opn cls [] = str default
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
    71
  | 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
    72
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
    73
datatype destination = Compile | Write | File of Path.T | String;
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
    74
type serialization = destination -> string option;
27014
a5f53d9d2b60 yet another attempt to circumvent printmode problems
haftmann
parents: 27001
diff changeset
    75
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    76
val code_width = ref 80; (*FIXME after Pretty module no longer depends on print mode*)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    77
fun code_setmp f = PrintMode.setmp [] (Pretty.setmp_margin (!code_width) f);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    78
fun code_of_pretty p = code_setmp Pretty.string_of p ^ "\n";
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    79
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
    80
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    81
(*FIXME why another code_setmp?*)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    82
fun compile f = (code_setmp f Compile; ());
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    83
fun write f = (code_setmp f Write; ());
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    84
fun file p f = (code_setmp f (File p); ());
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
    85
fun string f = the (code_setmp f String);
27014
a5f53d9d2b60 yet another attempt to circumvent printmode problems
haftmann
parents: 27001
diff changeset
    86
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    87
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
    88
(** generic syntax **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    89
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    90
datatype lrx = L | R | X;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    91
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    92
datatype fixity =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    93
    BR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    94
  | NOBR
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    95
  | INFX of (int * lrx);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    96
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    97
val APP = INFX (~1, L);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
    98
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
    99
fun fixity_lrx L L = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
   100
  | fixity_lrx R R = false
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
   101
  | fixity_lrx _ _ = true;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   102
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   103
fun fixity NOBR _ = false
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   104
  | fixity _ NOBR = false
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
   105
  | fixity (INFX (pr, lr)) (INFX (pr_ctxt, lr_ctxt)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   106
      pr < pr_ctxt
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   107
      orelse pr = pr_ctxt
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
   108
        andalso fixity_lrx lr lr_ctxt
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   109
      orelse pr_ctxt = ~1
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   110
  | fixity BR (INFX _) = false
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
   111
  | fixity _ _ = true;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   112
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   113
fun gen_brackify _ [p] = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   114
  | gen_brackify true (ps as _::_) = Pretty.enclose "(" ")" ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   115
  | gen_brackify false (ps as _::_) = Pretty.block ps;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   116
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   117
fun brackify fxy_ctxt =
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   118
  gen_brackify (fixity BR fxy_ctxt) o Pretty.breaks;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   119
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   120
fun brackify_infix infx fxy_ctxt =
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   121
  gen_brackify (fixity (INFX infx) fxy_ctxt) o Pretty.breaks;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   122
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   123
type class_syntax = string * (string -> string option);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   124
type typ_syntax = int * ((fixity -> itype -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   125
  -> fixity -> itype list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   126
type term_syntax = int * ((CodeName.var_ctxt -> fixity -> iterm -> Pretty.T)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   127
  -> CodeName.var_ctxt -> fixity -> (iterm * itype) list -> Pretty.T);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   128
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   129
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   130
(** theory data **)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   131
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   132
val target_SML = "SML";
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   133
val target_OCaml = "OCaml";
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   134
val target_Haskell = "Haskell";
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   135
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   136
datatype name_syntax_table = NameSyntaxTable of {
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   137
  class: class_syntax Symtab.table,
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   138
  inst: unit Symtab.table,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   139
  tyco: typ_syntax Symtab.table,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   140
  const: term_syntax Symtab.table
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   141
};
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   142
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   143
fun mk_name_syntax_table ((class, inst), (tyco, const)) =
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   144
  NameSyntaxTable { class = class, inst = inst, tyco = tyco, const = const };
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   145
fun map_name_syntax_table f (NameSyntaxTable { class, inst, tyco, const }) =
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   146
  mk_name_syntax_table (f ((class, inst), (tyco, const)));
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   147
fun merge_name_syntax_table (NameSyntaxTable { class = class1, inst = inst1, tyco = tyco1, const = const1 },
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   148
    NameSyntaxTable { class = class2, inst = inst2, tyco = tyco2, const = const2 }) =
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   149
  mk_name_syntax_table (
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   150
    (Symtab.join (K snd) (class1, class2),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   151
       Symtab.join (K snd) (inst1, inst2)),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   152
    (Symtab.join (K snd) (tyco1, tyco2),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   153
       Symtab.join (K snd) (const1, const2))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   154
  );
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   155
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   156
type serializer = (*FIXME order of arguments*)
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   157
  string option                         (*module name*)
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   158
  -> Args.T list                        (*arguments*)
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   159
  -> (string -> string)                 (*labelled_name*)
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   160
  -> string list                        (*reserved symbols*)
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   161
  -> (string * Pretty.T) list           (*includes*)
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   162
  -> (string -> string option)          (*module aliasses*)
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   163
  -> (string -> class_syntax option)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   164
  -> (string -> typ_syntax option)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   165
  -> (string -> term_syntax option)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   166
  -> CodeThingol.program
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   167
  -> string list                        (*selected statements*)
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   168
  -> serialization;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   169
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   170
datatype target = Target of {
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   171
  serial: serial,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   172
  serializer: serializer,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   173
  reserved: string list,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   174
  includes: Pretty.T Symtab.table,
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   175
  name_syntax_table: name_syntax_table,
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   176
  module_alias: string Symtab.table
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   177
};
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   178
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   179
fun mk_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) =
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   180
  Target { serial = serial, serializer = serializer, reserved = reserved, 
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   181
    includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias };
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   182
fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) =
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   183
  mk_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))));
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   184
fun merge_target target (Target { serial = serial1, serializer = serializer,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   185
  reserved = reserved1, includes = includes1,
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   186
  name_syntax_table = name_syntax_table1, module_alias = module_alias1 },
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   187
    Target { serial = serial2, serializer = _,
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   188
      reserved = reserved2, includes = includes2,
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   189
      name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) =
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   190
  if serial1 = serial2 then
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   191
    mk_target ((serial1, serializer),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   192
      ((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)),
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   193
        (merge_name_syntax_table (name_syntax_table1, name_syntax_table2),
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   194
          Symtab.join (K snd) (module_alias1, module_alias2))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   195
    ))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   196
  else
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   197
    error ("Incompatible serializers: " ^ quote target);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   198
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   199
structure CodeTargetData = TheoryDataFun
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   200
(
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   201
  type T = target Symtab.table * string list;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   202
  val empty = (Symtab.empty, []);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   203
  val copy = I;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   204
  val extend = I;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   205
  fun merge _ ((target1, exc1) : T, (target2, exc2)) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   206
    (Symtab.join merge_target (target1, target2), Library.merge (op =) (exc1, exc2));
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   207
);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   208
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   209
fun the_serializer (Target { serializer, ... }) = serializer;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   210
fun the_reserved (Target { reserved, ... }) = reserved;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   211
fun the_includes (Target { includes, ... }) = includes;
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   212
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   213
fun the_module_alias (Target { module_alias , ... }) = module_alias;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   214
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   215
val abort_allowed = snd o CodeTargetData.get;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   216
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   217
fun assert_target thy target =
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   218
  case Symtab.lookup (fst (CodeTargetData.get thy)) target
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   219
   of SOME data => target
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   220
    | NONE => error ("Unknown code target language: " ^ quote target);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   221
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   222
fun add_target (target, seri) thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   223
  let
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   224
    val _ = case Symtab.lookup (fst (CodeTargetData.get thy)) target
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   225
     of SOME _ => warning ("Overwriting existing serializer " ^ quote target)
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   226
      | NONE => ();
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   227
  in
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   228
    thy
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   229
    |> (CodeTargetData.map o apfst oo Symtab.map_default)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   230
          (target, mk_target ((serial (), seri), (([], Symtab.empty),
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   231
            (mk_name_syntax_table ((Symtab.empty, Symtab.empty), (Symtab.empty, Symtab.empty)),
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   232
              Symtab.empty))))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   233
          ((map_target o apfst o apsnd o K) seri)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   234
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   235
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   236
fun map_target_data target f thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   237
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   238
    val _ = assert_target thy target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   239
  in
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   240
    thy
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   241
    |> (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
   242
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   243
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   244
fun map_reserved target =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   245
  map_target_data target o apsnd o apfst o apfst;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   246
fun map_includes target =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   247
  map_target_data target o apsnd o apfst o apsnd;
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   248
fun map_name_syntax target =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   249
  map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   250
fun map_module_alias target =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   251
  map_target_data target o apsnd o apsnd o apsnd;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   252
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   253
fun invoke_serializer thy abortable serializer reserved includes 
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   254
    module_alias class inst tyco const module args program1 cs1 =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   255
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   256
    val hidden = Symtab.keys class @ Symtab.keys inst @ Symtab.keys tyco @ Symtab.keys const;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   257
    val cs2 = subtract (op =) hidden cs1;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   258
    val program2 = Graph.subgraph (not o member (op =) hidden) program1;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   259
    val all_cs = Graph.all_succs program2 cs2;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   260
    val program3 = Graph.subgraph (member (op =) all_cs) program2;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   261
    val empty_funs = filter_out (member (op =) abortable)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   262
      (CodeThingol.empty_funs program3);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   263
    val _ = if null empty_funs then () else error ("No defining equations for "
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   264
      ^ commas (map (CodeName.labelled_name thy) empty_funs));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   265
  in
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   266
    serializer module args (CodeName.labelled_name thy) reserved includes
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   267
      (Symtab.lookup module_alias) (Symtab.lookup class)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   268
      (Symtab.lookup tyco) (Symtab.lookup const)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   269
      program3 cs2
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   270
  end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   271
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   272
fun mount_serializer thy alt_serializer target =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   273
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   274
    val (targets, abortable) = CodeTargetData.get thy;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   275
    val data = case Symtab.lookup targets target
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   276
     of SOME data => data
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   277
      | NONE => error ("Unknown code target language: " ^ quote target);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   278
    val serializer = the_default (the_serializer data) alt_serializer;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   279
    val reserved = the_reserved data;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   280
    val includes = Symtab.dest (the_includes data);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   281
    val module_alias = the_module_alias data;
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
   282
    val { class, inst, tyco, const } = the_name_syntax data;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   283
  in
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   284
    invoke_serializer thy abortable serializer reserved
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   285
      includes module_alias class inst tyco const
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   286
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   287
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   288
fun serialize thy = mount_serializer thy NONE;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   289
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   290
fun parse_args f args =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   291
  case Scan.read Args.stopper f args
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   292
   of SOME x => x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   293
    | NONE => error "Bad serializer arguments";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   294
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   295
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   296
(** generic code combinators **)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   297
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   298
(* list, char, string, numeral and monad abstract syntax transformations *)
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 implode_list c_nil c_cons t =
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
    fun dest_cons (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   303
          if c = c_cons
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   304
          then SOME (t1, t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   305
          else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   306
      | dest_cons _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   307
    val (ts, t') = CodeThingol.unfoldr dest_cons t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   308
  in case t'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   309
   of IConst (c, _) => if c = c_nil then SOME ts else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   310
    | _ => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   311
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   312
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   313
fun decode_char c_nibbles (IConst (c1, _), IConst (c2, _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   314
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   315
        fun idx c = find_index (curry (op =) c) c_nibbles;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   316
        fun decode ~1 _ = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   317
          | decode _ ~1 = NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   318
          | decode n m = SOME (chr (n * 16 + m));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   319
      in decode (idx c1) (idx c2) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   320
  | decode_char _ _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   321
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   322
fun implode_string c_char c_nibbles mk_char mk_string ts =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   323
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   324
    fun implode_char (IConst (c, _) `$ t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   325
          if c = c_char then decode_char c_nibbles (t1, t2) else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   326
      | implode_char _ = NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   327
    val ts' = map implode_char ts;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   328
  in if forall is_some ts'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   329
    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
   330
    else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   331
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   332
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
   333
fun implode_numeral negative c_pls c_min c_bit0 c_bit1 =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   334
  let
25936
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   335
    fun dest_bit (IConst (c, _)) = if c = c_bit0 then 0
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   336
          else if c = c_bit1 then 1
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   337
          else error "Illegal numeral expression: illegal bit"
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   338
      | dest_bit _ = error "Illegal numeral expression: illegal bit";
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
   339
    fun dest_numeral (IConst (c, _)) = if c = c_pls then SOME 0
25936
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   340
          else if c = c_min then
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   341
            if negative then SOME ~1 else NONE
25969
haftmann
parents: 25936
diff changeset
   342
          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
   343
      | 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
   344
          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
   345
          in case n of SOME n => SOME (2 * n + b) | NONE => NONE end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   346
      | dest_numeral _ = error "Illegal numeral expression: illegal term";
25936
f43bac9def6e non-negative numerals
haftmann
parents: 25771
diff changeset
   347
  in dest_numeral #> the_default 0 end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   348
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   349
fun implode_monad c_bind t =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   350
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   351
    fun dest_monad (IConst (c, _) `$ t1 `$ t2) =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   352
          if c = c_bind
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   353
            then case CodeThingol.split_abs t2
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   354
             of SOME (((v, pat), ty), t') =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   355
                  SOME ((SOME (((SOME v, pat), ty), true), t1), t')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   356
              | NONE => NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   357
            else NONE
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   358
      | dest_monad t = case CodeThingol.split_let t
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   359
           of SOME (((pat, ty), tbind), t') =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   360
                SOME ((SOME (((NONE, SOME pat), ty), false), tbind), t')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   361
            | NONE => NONE;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   362
  in CodeThingol.unfoldr dest_monad t end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   363
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   364
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   365
(* applications and bindings *)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   366
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   367
fun gen_pr_app pr_app pr_term syntax_const labelled_name is_cons
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   368
      lhs vars fxy (app as ((c, (_, tys)), ts)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   369
  case syntax_const c
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   370
   of NONE => if lhs andalso not (is_cons c) then
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   371
          error ("Non-constructor on left hand side of equation: " ^ labelled_name c)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   372
        else brackify fxy (pr_app lhs vars app)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   373
    | SOME (i, pr) =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   374
        let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   375
          val k = if i < 0 then length tys else i;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   376
          fun pr' fxy ts = pr (pr_term lhs) vars fxy (ts ~~ curry Library.take k tys);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   377
        in if k = length ts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   378
          then pr' fxy ts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   379
        else if k < length ts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   380
          then case chop k ts of (ts1, ts2) =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   381
            brackify fxy (pr' APP ts1 :: map (pr_term lhs vars BR) ts2)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   382
          else pr_term lhs vars fxy (CodeThingol.eta_expand app k)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   383
        end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   384
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   385
fun gen_pr_bind pr_bind pr_term (fxy : fixity) ((v, pat), ty : itype) vars =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   386
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   387
    val vs = case pat
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   388
     of SOME pat => CodeThingol.fold_varnames (insert (op =)) pat []
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   389
      | NONE => [];
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   390
    val vars' = CodeName.intro_vars (the_list v) vars;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   391
    val vars'' = CodeName.intro_vars vs vars';
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   392
    val v' = Option.map (CodeName.lookup_var vars') v;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   393
    val pat' = Option.map (pr_term true vars'' fxy) pat;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   394
  in (pr_bind ((v', pat'), ty), vars'') end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   395
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   396
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
   397
(* name auxiliary *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   398
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   399
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
   400
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
   401
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   402
val dest_name =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   403
  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
   404
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   405
fun mk_name_module reserved_names module_prefix module_alias program =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   406
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   407
    fun mk_alias name = case module_alias name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   408
     of SOME name' => name'
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   409
      | NONE => name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   410
          |> NameSpace.explode
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   411
          |> map (fn name => (the_single o fst) (Name.variants [name] reserved_names))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   412
          |> NameSpace.implode;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   413
    fun mk_prefix name = case module_prefix
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   414
     of SOME module_prefix => NameSpace.append module_prefix name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   415
      | NONE => name;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   416
    val tab =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   417
      Symtab.empty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   418
      |> Graph.fold ((fn name => Symtab.default (name, (mk_alias #> mk_prefix) name))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   419
           o fst o dest_name o fst)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   420
             program
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   421
  in the o Symtab.lookup tab end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   422
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   423
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   424
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   425
(** SML/OCaml serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   426
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   427
datatype ml_stmt =
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   428
    MLFuns of (string * (typscheme * (iterm list * iterm) list)) list
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   429
  | MLDatas of (string * ((vname * sort) list * (string * itype list) list)) list
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
   430
  | MLClass of string * (vname * ((class * string) list * (string * itype) list))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   431
  | MLClassinst of string * ((class * (string * (vname * sort) list))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   432
        * ((class * (string * (string * dict list list))) list
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   433
      * (string * const) list));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   434
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   435
fun pr_sml_stmt syntax_tyco syntax_const labelled_name reserved_names deresolve is_cons =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   436
  let
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   437
    val pr_label_classrel = translate_string (fn "." => "__" | c => c)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   438
      o NameSpace.qualifier;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   439
    val pr_label_classparam = NameSpace.base o NameSpace.qualifier;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   440
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   441
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   442
        fun pr_dictvar (v, (_, 1)) = first_upper v ^ "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   443
          | pr_dictvar (v, (i, _)) = first_upper v ^ string_of_int (i+1) ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   444
        fun pr_proj [] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   445
              p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   446
          | pr_proj [p'] p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   447
              brackets [p', p]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   448
          | pr_proj (ps as _ :: _) p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   449
              brackets [Pretty.enum " o" "(" ")" ps, p];
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   450
        fun pr_dict fxy (DictConst (inst, dss)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   451
              brackify fxy ((str o deresolve) inst :: map (pr_dicts BR) dss)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   452
          | pr_dict fxy (DictVar (classrels, v)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   453
              pr_proj (map (str o deresolve) classrels) ((str o pr_dictvar) v)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   454
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   455
       of [] => str "()"
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   456
        | [d] => pr_dict fxy d
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   457
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dict NOBR)) ds
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   458
      end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   459
    fun pr_tyvar_dicts vs =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   460
      vs
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   461
      |> map (fn (v, sort) => map_index (fn (i, _) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   462
           DictVar ([], (v, (i, length sort)))) sort)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   463
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   464
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   465
      let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   466
        val tyco' = (str o deresolve) tyco
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   467
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   468
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   469
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   470
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   471
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   472
    and pr_typ fxy (tyco `%% tys) =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   473
          (case syntax_tyco tyco
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   474
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   475
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   476
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   477
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   478
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   479
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   480
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   481
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   482
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   483
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   484
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   485
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   486
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   487
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   488
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   489
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   490
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   491
                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
   492
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   493
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   494
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   495
            fun pr ((v, pat), ty) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   496
              pr_bind NOBR ((SOME v, pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   497
              #>> (fn p => concat [str "fn", p, str "=>"]);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   498
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   499
          in brackets (ps @ [pr_term lhs vars' NOBR t']) end
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   500
      | pr_term lhs vars fxy (ICase (cases as (_, t0))) =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   501
          (case CodeThingol.unfold_const_app t0
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   502
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   503
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   504
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   505
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   506
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   507
      if is_cons c then let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   508
        val k = length tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   509
      in if k < 2 then 
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   510
        (str o deresolve) c :: map (pr_term lhs vars BR) ts
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   511
      else if k = length ts then
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   512
        [(str o deresolve) c, Pretty.enum "," "(" ")" (map (pr_term lhs vars NOBR) ts)]
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   513
      else [pr_term lhs vars BR (CodeThingol.eta_expand app k)] end else
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   514
        (str o deresolve) c
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   515
          :: (map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   516
    and pr_app lhs vars = gen_pr_app pr_app' pr_term syntax_const
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   517
      labelled_name is_cons lhs vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   518
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   519
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   520
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   521
      | pr_bind' ((SOME v, SOME p), _) = concat [str v, str "as", p]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   522
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   523
    and pr_case vars fxy (cases as ((_, [_]), _)) =
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 (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   526
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   527
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   528
              |> pr_bind NOBR ((NONE, SOME pat), ty)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   529
              |>> (fn p => semicolon [str "val", p, str "=", pr_term false vars NOBR t])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   530
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   531
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   532
            Pretty.chunks [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   533
              [str ("let"), Pretty.fbrk, Pretty.chunks ps] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   534
              [str ("in"), Pretty.fbrk, pr_term false vars' NOBR t'] |> Pretty.block,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   535
              str ("end")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   536
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   537
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   538
      | pr_case vars fxy (((td, ty), b::bs), _) =
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
            fun pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   541
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   542
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   543
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   544
                concat [str delim, p, str "=>", pr_term false vars' NOBR t]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   545
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   546
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   547
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   548
              str "case"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   549
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   550
              :: pr "of" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   551
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   552
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   553
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   554
      | pr_case vars fxy ((_, []), _) = str "raise Fail \"empty case\""
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   555
    fun pr_stmt (MLFuns (funns as (funn :: funns'))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   556
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   557
            val definer =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   558
              let
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   559
                fun no_args _ ((ts, _) :: _) = length ts
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   560
                  | no_args ty [] = (length o fst o CodeThingol.unfold_fun) ty;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   561
                fun mk 0 [] = "val"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   562
                  | mk 0 vs = if (null o filter_out (null o snd)) vs
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   563
                      then "val" else "fun"
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   564
                  | mk k _ = "fun";
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   565
                fun chk (_, ((vs, ty), eqs)) NONE = SOME (mk (no_args ty eqs) vs)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   566
                  | chk (_, ((vs, ty), eqs)) (SOME defi) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   567
                      if defi = mk (no_args ty eqs) vs then SOME defi
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   568
                      else error ("Mixing simultaneous vals and funs not implemented: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   569
                        ^ commas (map (labelled_name o fst) funns));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   570
              in the (fold chk funns NONE) end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   571
            fun pr_funn definer (name, ((vs, ty), [])) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   572
                  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   573
                    val vs_dict = filter_out (null o snd) vs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   574
                    val n = length vs_dict + (length o fst o CodeThingol.unfold_fun) ty;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   575
                    val exc_str =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   576
                      (ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   577
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   578
                    concat (
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   579
                      str definer
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   580
                      :: (str o deresolve) name
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   581
                      :: map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   582
                      @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   583
                      :: str "raise"
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   584
                      :: str "(Fail"
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   585
                      @@ str (exc_str ^ ")")
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   586
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   587
                  end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   588
              | pr_funn definer (name, ((vs, ty), eqs as eq :: eqs')) =
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   589
                  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   590
                    val vs_dict = filter_out (null o snd) vs;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   591
                    val shift = if null eqs' then I else
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   592
                      map (Pretty.block o single o Pretty.block o single);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   593
                    fun pr_eq definer (ts, t) =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   594
                      let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   595
                        val consts = map_filter
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   596
                          (fn c => if (is_some o syntax_const) c
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   597
                            then NONE else (SOME o NameSpace.base o deresolve) c)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   598
                            ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   599
                        val vars = reserved_names
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   600
                          |> CodeName.intro_vars consts
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   601
                          |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   602
                               (insert (op =)) ts []);
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   603
                      in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   604
                        concat (
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   605
                          [str definer, (str o deresolve) name]
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   606
                          @ (if null ts andalso null vs_dict
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   607
                             then [str ":", pr_typ NOBR ty]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   608
                             else
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   609
                               pr_tyvar_dicts vs_dict
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   610
                               @ map (pr_term true vars BR) ts)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   611
                       @ [str "=", pr_term false vars NOBR t]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   612
                        )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   613
                      end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   614
                  in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   615
                    (Pretty.block o Pretty.fbreaks o shift) (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   616
                      pr_eq definer eq
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   617
                      :: map (pr_eq "|") eqs'
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   618
                    )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   619
                  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   620
            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
   621
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   622
     | pr_stmt (MLDatas (datas as (data :: datas'))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   623
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   624
            fun pr_co (co, []) =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   625
                  str (deresolve co)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   626
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   627
                  concat [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   628
                    str (deresolve co),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   629
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   630
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   631
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   632
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   633
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   634
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   635
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   636
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   637
                    @@ str "EMPTY__" 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   638
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   639
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   640
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   641
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   642
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   643
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   644
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   645
                  );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   646
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   647
              (pr_data "datatype" data :: map (pr_data "and") datas');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   648
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";"])]) end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   649
     | pr_stmt (MLClass (class, (v, (superclasses, classparams)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   650
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   651
            val w = first_upper v ^ "_";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   652
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   653
              (concat o map str) [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   654
                pr_label_classrel classrel, ":", "'" ^ v, deresolve class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   655
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   656
            fun pr_classparam_field (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   657
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   658
                (str o pr_label_classparam) classparam, str ":", pr_typ NOBR ty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   659
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   660
            fun pr_classparam_proj (classparam, _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   661
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   662
                str "fun",
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   663
                (str o deresolve) classparam,
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   664
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolve class)],
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   665
                str "=",
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   666
                str ("#" ^ pr_label_classparam classparam),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   667
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   668
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   669
            fun pr_superclass_proj (_, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   670
              semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   671
                str "fun",
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   672
                (str o deresolve) classrel,
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   673
                Pretty.enclose "(" ")" [str (w ^ ":'" ^ v ^ " " ^ deresolve class)],
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   674
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   675
                str ("#" ^ pr_label_classrel classrel),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   676
                str w
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   677
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   678
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   679
            Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   680
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   681
                str ("type '" ^ v),
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   682
                (str o deresolve) class,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   683
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   684
                Pretty.enum "," "{" "};" (
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   685
                  map pr_superclass_field superclasses @ map pr_classparam_field classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   686
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   687
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   688
              :: map pr_superclass_proj superclasses
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   689
              @ map pr_classparam_proj classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   690
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   691
          end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   692
     | pr_stmt (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   693
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   694
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   695
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   696
                (str o pr_label_classrel) classrel,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   697
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   698
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   699
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   700
            fun pr_classparam (classparam, c_inst) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   701
              concat [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   702
                (str o pr_label_classparam) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   703
                str "=",
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   704
                pr_app false reserved_names NOBR (c_inst, [])
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
   705
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   706
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   707
            semicolon ([
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   708
              str (if null arity then "val" else "fun"),
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   709
              (str o deresolve) inst ] @
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   710
              pr_tyvar_dicts arity @ [
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   711
              str "=",
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   712
              Pretty.enum "," "{" "}"
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   713
                (map pr_superclass superarities @ map pr_classparam classparam_insts),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   714
              str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   715
              pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   716
            ])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   717
          end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   718
  in pr_stmt end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   719
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   720
fun pr_sml_module name content =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   721
  Pretty.chunks (
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   722
    str ("structure " ^ name ^ " = ")
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   723
    :: str "struct"
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   724
    :: str ""
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   725
    :: content
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   726
    @ str ""
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   727
    @@ str ("end; (*struct " ^ name ^ "*)")
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   728
  );
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   729
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   730
fun pr_ocaml_stmt syntax_tyco syntax_const labelled_name reserved_names deresolve is_cons =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   731
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   732
    fun pr_dicts fxy ds =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   733
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   734
        fun pr_dictvar (v, (_, 1)) = "_" ^ first_upper v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   735
          | pr_dictvar (v, (i, _)) = "_" ^ first_upper v ^ string_of_int (i+1);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   736
        fun pr_proj ps p =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   737
          fold_rev (fn p2 => fn p1 => Pretty.block [p1, str ".", str p2]) ps p
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   738
        fun pr_dict fxy (DictConst (inst, dss)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   739
              brackify fxy ((str o deresolve) inst :: map (pr_dicts BR) dss)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   740
          | pr_dict fxy (DictVar (classrels, v)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   741
              pr_proj (map deresolve classrels) ((str o pr_dictvar) v)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   742
      in case ds
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   743
       of [] => str "()"
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   744
        | [d] => pr_dict fxy d
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   745
        | _ :: _ => (Pretty.list "(" ")" o map (pr_dict NOBR)) ds
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   746
      end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   747
    fun pr_tyvar_dicts vs =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   748
      vs
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   749
      |> map (fn (v, sort) => map_index (fn (i, _) =>
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   750
           DictVar ([], (v, (i, length sort)))) sort)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   751
      |> map (pr_dicts BR);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   752
    fun pr_tycoexpr fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   753
      let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   754
        val tyco' = (str o deresolve) tyco
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   755
      in case map (pr_typ BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   756
       of [] => tyco'
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   757
        | [p] => Pretty.block [p, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   758
        | (ps as _::_) => Pretty.block [Pretty.list "(" ")" ps, Pretty.brk 1, tyco']
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   759
      end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   760
    and pr_typ fxy (tyco `%% tys) =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   761
          (case syntax_tyco tyco
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   762
           of NONE => pr_tycoexpr fxy (tyco, tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   763
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   764
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   765
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   766
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   767
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   768
                else pr pr_typ fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   769
      | pr_typ fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   770
          str ("'" ^ v);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   771
    fun pr_term lhs vars fxy (IConst c) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   772
          pr_app lhs vars fxy (c, [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   773
      | pr_term lhs vars fxy (IVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   774
          str (CodeName.lookup_var vars v)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   775
      | pr_term lhs vars fxy (t as t1 `$ t2) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   776
          (case CodeThingol.unfold_const_app t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   777
           of SOME c_ts => pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   778
            | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   779
                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
   780
      | pr_term lhs vars fxy (t as _ `|-> _) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   781
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   782
            val (binds, t') = CodeThingol.unfold_abs t;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   783
            fun pr ((v, pat), ty) = pr_bind BR ((SOME v, pat), ty);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   784
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   785
          in brackets (str "fun" :: ps @ str "->" @@ pr_term lhs vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   786
      | pr_term lhs vars fxy (ICase (cases as (_, t0))) = (case CodeThingol.unfold_const_app t0
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   787
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   788
                then pr_case vars fxy cases
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   789
                else pr_app lhs vars fxy c_ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   790
            | NONE => pr_case vars fxy cases)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   791
    and pr_app' lhs vars (app as ((c, (iss, tys)), ts)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   792
      if is_cons c then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   793
        if length tys = length ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   794
        then case ts
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   795
         of [] => [(str o deresolve) c]
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   796
          | [t] => [(str o deresolve) c, pr_term lhs vars BR t]
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   797
          | _ => [(str o deresolve) c, Pretty.enum "," "(" ")"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   798
                    (map (pr_term lhs vars NOBR) ts)]
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   799
        else [pr_term lhs vars BR (CodeThingol.eta_expand app (length tys))]
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   800
      else (str o deresolve) c
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   801
        :: ((map (pr_dicts BR) o filter_out null) iss @ map (pr_term lhs vars BR) ts)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   802
    and pr_app lhs vars = gen_pr_app pr_app' pr_term syntax_const
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   803
      labelled_name is_cons lhs vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   804
    and pr_bind' ((NONE, NONE), _) = str "_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   805
      | pr_bind' ((SOME v, NONE), _) = str v
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   806
      | pr_bind' ((NONE, SOME p), _) = p
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   807
      | pr_bind' ((SOME v, SOME p), _) = brackets [p, str "as", str v]
24284
f5afd33f5d02 fixed OCaml bug
haftmann
parents: 24251
diff changeset
   808
    and pr_bind fxy = gen_pr_bind pr_bind' pr_term fxy
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   809
    and pr_case vars fxy (cases as ((_, [_]), _)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   810
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   811
            val (binds, t') = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   812
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   813
              vars
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   814
              |> pr_bind NOBR ((NONE, SOME pat), ty)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   815
              |>> (fn p => concat
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   816
                  [str "let", p, str "=", pr_term false vars NOBR t, str "in"])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   817
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   818
          in Pretty.chunks (ps @| pr_term false vars' NOBR t') end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   819
      | pr_case vars fxy (((td, ty), b::bs), _) =
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 pr delim (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   822
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   823
                val (p, vars') = pr_bind NOBR ((NONE, SOME pat), ty) vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   824
              in concat [str delim, p, str "->", pr_term false vars' NOBR t] end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   825
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   826
            (Pretty.enclose "(" ")" o single o brackify fxy) (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   827
              str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   828
              :: pr_term false vars NOBR td
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   829
              :: pr "with" b
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   830
              :: map (pr "|") bs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   831
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   832
          end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   833
      | pr_case vars fxy ((_, []), _) = str "failwith \"empty case\"";
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   834
    fun fish_params vars eqs =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   835
      let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   836
        fun fish_param _ (w as SOME _) = w
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   837
          | fish_param (IVar v) NONE = SOME v
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   838
          | fish_param _ NONE = NONE;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   839
        fun fillup_param _ (_, SOME v) = v
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   840
          | fillup_param x (i, NONE) = x ^ string_of_int i;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   841
        val fished1 = fold (map2 fish_param) eqs (replicate (length (hd eqs)) NONE);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   842
        val x = Name.variant (map_filter I fished1) "x";
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   843
        val fished2 = map_index (fillup_param x) fished1;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   844
        val (fished3, _) = Name.variants fished2 Name.context;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   845
        val vars' = CodeName.intro_vars fished3 vars;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   846
      in map (CodeName.lookup_var vars') fished3 end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   847
    fun pr_stmt (MLFuns (funns as funn :: funns')) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   848
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   849
            fun pr_eq (ts, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   850
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   851
                val consts = map_filter
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   852
                  (fn c => if (is_some o syntax_const) c
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   853
                    then NONE else (SOME o NameSpace.base o deresolve) c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   854
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   855
                val vars = reserved_names
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   856
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   857
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   858
                      (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   859
              in concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   860
                (Pretty.block o Pretty.commas) (map (pr_term true vars NOBR) ts),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   861
                str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   862
                pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   863
              ] end;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   864
            fun pr_eqs name ty [] =
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   865
                  let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   866
                    val n = (length o fst o CodeThingol.unfold_fun) ty;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   867
                    val exc_str =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   868
                      (ML_Syntax.print_string o NameSpace.base o NameSpace.qualifier) name;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   869
                  in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   870
                    concat (
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   871
                      map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   872
                      @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   873
                      :: str "failwith"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   874
                      @@ str exc_str
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   875
                    )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   876
                  end
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   877
              | pr_eqs _ _ [(ts, t)] =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   878
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   879
                    val consts = map_filter
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   880
                      (fn c => if (is_some o syntax_const) c
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   881
                        then NONE else (SOME o NameSpace.base o deresolve) c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   882
                        ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   883
                    val vars = reserved_names
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   884
                      |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   885
                      |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   886
                          (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   887
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   888
                    concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   889
                      map (pr_term true vars BR) ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   890
                      @ str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   891
                      @@ pr_term false vars NOBR t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   892
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   893
                  end
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   894
              | pr_eqs _ _ (eqs as (eq as ([_], _)) :: eqs') =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   895
                  Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   896
                    str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   897
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   898
                    :: str "function"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   899
                    :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   900
                    :: pr_eq eq
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   901
                    :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   902
                          o single o pr_eq) eqs'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   903
                  )
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   904
              | pr_eqs _ _ (eqs as eq :: eqs') =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   905
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   906
                    val consts = map_filter
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   907
                      (fn c => if (is_some o syntax_const) c
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   908
                        then NONE else (SOME o NameSpace.base o deresolve) c)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   909
                        ((fold o CodeThingol.fold_constnames)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   910
                          (insert (op =)) (map snd eqs) []);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   911
                    val vars = reserved_names
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   912
                      |> CodeName.intro_vars consts;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   913
                    val dummy_parms = (map str o fish_params vars o map fst) eqs;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   914
                  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   915
                    Pretty.block (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   916
                      Pretty.breaks dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   917
                      @ Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   918
                      :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   919
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   920
                      :: str "match"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   921
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   922
                      :: (Pretty.block o Pretty.commas) dummy_parms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   923
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   924
                      :: str "with"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   925
                      :: Pretty.brk 1
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   926
                      :: pr_eq eq
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   927
                      :: maps (append [Pretty.fbrk, str "|", Pretty.brk 1]
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   928
                           o single o pr_eq) eqs'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   929
                    )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   930
                  end;
24381
560e8ecdf633 improved evaluation interface
haftmann
parents: 24294
diff changeset
   931
            fun pr_funn definer (name, ((vs, ty), eqs)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   932
              concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   933
                str definer
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   934
                :: (str o deresolve) name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   935
                :: pr_tyvar_dicts (filter_out (null o snd) vs)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   936
                @| pr_eqs name ty eqs
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   937
              );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   938
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   939
              (pr_funn "let rec" funn :: map (pr_funn "and") funns');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   940
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   941
     | pr_stmt (MLDatas (datas as (data :: datas'))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   942
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   943
            fun pr_co (co, []) =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   944
                  str (deresolve co)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   945
              | pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   946
                  concat [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   947
                    str (deresolve co),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   948
                    str "of",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   949
                    Pretty.enum " *" "" "" (map (pr_typ (INFX (2, X))) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   950
                  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   951
            fun pr_data definer (tyco, (vs, [])) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   952
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   953
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   954
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   955
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   956
                    @@ str "EMPTY_"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   957
                  )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   958
              | pr_data definer (tyco, (vs, cos)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   959
                  concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   960
                    str definer
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   961
                    :: pr_tycoexpr NOBR (tyco, map (ITyVar o fst) vs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   962
                    :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   963
                    :: separate (str "|") (map pr_co cos)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   964
                  );
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   965
            val (ps, p) = split_last
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   966
              (pr_data "type" data :: map (pr_data "and") datas');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   967
          in Pretty.chunks (ps @ [Pretty.block ([p, str ";;"])]) end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   968
     | pr_stmt (MLClass (class, (v, (superclasses, classparams)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   969
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   970
            val w = "_" ^ first_upper v;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   971
            fun pr_superclass_field (class, classrel) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   972
              (concat o map str) [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   973
                deresolve classrel, ":", "'" ^ v, deresolve class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   974
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   975
            fun pr_classparam_field (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   976
              concat [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   977
                (str o deresolve) classparam, str ":", pr_typ NOBR ty
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   978
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   979
            fun pr_classparam_proj (classparam, _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   980
              concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   981
                str "let",
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   982
                (str o deresolve) classparam,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   983
                str w,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   984
                str "=",
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   985
                str (w ^ "." ^ deresolve classparam ^ ";;")
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   986
              ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   987
          in Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   988
            concat [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   989
              str ("type '" ^ v),
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   990
              (str o deresolve) class,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   991
              str "=",
25771
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
   992
              enum_default "();;" ";" "{" "};;" (
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   993
                map pr_superclass_field superclasses
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
   994
                @ map pr_classparam_field classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   995
              )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   996
            ]
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
   997
            :: map pr_classparam_proj classparams
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
   998
          ) end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
   999
     | pr_stmt (MLClassinst (inst, ((class, (tyco, arity)), (superarities, classparam_insts)))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1000
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1001
            fun pr_superclass (_, (classrel, dss)) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1002
              concat [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1003
                (str o deresolve) classrel,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1004
                str "=",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1005
                pr_dicts NOBR [DictConst dss]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1006
              ];
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1007
            fun pr_classparam_inst (classparam, c_inst) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1008
              concat [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1009
                (str o deresolve) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1010
                str "=",
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1011
                pr_app false reserved_names NOBR (c_inst, [])
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1012
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1013
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1014
            concat (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1015
              str "let"
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1016
              :: (str o deresolve) inst
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1017
              :: pr_tyvar_dicts arity
24219
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
              @@ (Pretty.enclose "(" ");;" o Pretty.breaks) [
25771
a81c0ad97133 empty dictionaries for OCaml
haftmann
parents: 25621
diff changeset
  1020
                enum_default "()" ";" "{" "}" (map pr_superclass superarities
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1021
                  @ map pr_classparam_inst classparam_insts),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1022
                str ":",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1023
                pr_tycoexpr NOBR (class, [tyco `%% map (ITyVar o fst) arity])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1024
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1025
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1026
          end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1027
  in pr_stmt end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1028
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1029
fun pr_ocaml_module name content =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1030
  Pretty.chunks (
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1031
    str ("module " ^ name ^ " = ")
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1032
    :: str "struct"
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1033
    :: str ""
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1034
    :: content
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1035
    @ str ""
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1036
    @@ str ("end;; (*struct " ^ name ^ "*)")
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1037
  );
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1038
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1039
local
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1040
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1041
datatype ml_node =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1042
    Dummy of string
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1043
  | Stmt of string * ml_stmt
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1044
  | Module of string * ((Name.context * Name.context) * ml_node Graph.T);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1045
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1046
in
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1047
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1048
fun ml_node_of_program labelled_name module_name reserved_names raw_module_alias program =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1049
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1050
    val module_alias = if is_some module_name then K module_name else raw_module_alias;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1051
    val reserved_names = Name.make_context reserved_names;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1052
    val empty_module = ((reserved_names, reserved_names), Graph.empty);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1053
    fun map_node [] f = f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1054
      | map_node (m::ms) f =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1055
          Graph.default_node (m, Module (m, empty_module))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1056
          #> Graph.map_node m (fn (Module (module_name, (nsp, nodes))) =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1057
               Module (module_name, (nsp, map_node ms f nodes)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1058
    fun map_nsp_yield [] f (nsp, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1059
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1060
            val (x, nsp') = f nsp
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1061
          in (x, (nsp', nodes)) end
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1062
      | map_nsp_yield (m::ms) f (nsp, nodes) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1063
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1064
            val (x, nodes') =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1065
              nodes
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1066
              |> Graph.default_node (m, Module (m, empty_module))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1067
              |> Graph.map_node_yield m (fn Module (d_module_name, nsp_nodes) => 
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1068
                  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1069
                    val (x, nsp_nodes') = map_nsp_yield ms f nsp_nodes
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1070
                  in (x, Module (d_module_name, nsp_nodes')) end)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1071
          in (x, (nsp, nodes')) end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1072
    fun map_nsp_fun_yield f (nsp_fun, nsp_typ) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1073
      let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1074
        val (x, nsp_fun') = f nsp_fun
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1075
      in (x, (nsp_fun', nsp_typ)) end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1076
    fun map_nsp_typ_yield f (nsp_fun, nsp_typ) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1077
      let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1078
        val (x, nsp_typ') = f nsp_typ
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1079
      in (x, (nsp_fun, nsp_typ')) end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1080
    val mk_name_module = mk_name_module reserved_names NONE module_alias program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1081
    fun mk_name_stmt upper name nsp =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1082
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1083
        val (_, base) = dest_name name;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1084
        val base' = if upper then first_upper base else base;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1085
        val ([base''], nsp') = Name.variants [base'] nsp;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1086
      in (base'', nsp') end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1087
    fun add_funs stmts =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1088
      fold_map
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1089
        (fn (name, CodeThingol.Fun stmt) =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1090
              map_nsp_fun_yield (mk_name_stmt false name) #>>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1091
                rpair (name, (apsnd o map) fst stmt)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1092
          | (name, _) =>
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1093
              error ("Function block containing illegal statement: " ^ labelled_name name)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1094
        ) stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1095
      #>> (split_list #> apsnd MLFuns);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1096
    fun add_datatypes stmts =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1097
      fold_map
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1098
        (fn (name, CodeThingol.Datatype stmt) =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1099
              map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, stmt))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1100
          | (name, CodeThingol.Datatypecons _) =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1101
              map_nsp_fun_yield (mk_name_stmt true name) #>> rpair NONE
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1102
          | (name, _) =>
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1103
              error ("Datatype block containing illegal statement: " ^ labelled_name name)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1104
        ) stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1105
      #>> (split_list #> apsnd (map_filter I
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1106
        #> (fn [] => error ("Datatype block without data statement: "
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1107
                  ^ (commas o map (labelled_name o fst)) stmts)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1108
             | stmts => MLDatas stmts)));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1109
    fun add_class stmts =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1110
      fold_map
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1111
        (fn (name, CodeThingol.Class info) =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1112
              map_nsp_typ_yield (mk_name_stmt false name) #>> rpair (SOME (name, info))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1113
          | (name, CodeThingol.Classrel _) =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1114
              map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1115
          | (name, CodeThingol.Classparam _) =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1116
              map_nsp_fun_yield (mk_name_stmt false name) #>> rpair NONE
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1117
          | (name, _) =>
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1118
              error ("Class block containing illegal statement: " ^ labelled_name name)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1119
        ) stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1120
      #>> (split_list #> apsnd (map_filter I
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1121
        #> (fn [] => error ("Class block without class statement: "
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1122
                  ^ (commas o map (labelled_name o fst)) stmts)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1123
             | [stmt] => MLClass stmt)));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1124
    fun add_inst [(name, CodeThingol.Classinst stmt)] =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1125
      map_nsp_fun_yield (mk_name_stmt false name)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1126
      #>> (fn base => ([base], MLClassinst (name, (apsnd o apsnd o map) fst stmt)));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1127
    fun add_stmts ((stmts as (_, CodeThingol.Fun _)::_)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1128
          add_funs stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1129
      | add_stmts ((stmts as (_, CodeThingol.Datatypecons _)::_)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1130
          add_datatypes stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1131
      | add_stmts ((stmts as (_, CodeThingol.Datatype _)::_)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1132
          add_datatypes stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1133
      | add_stmts ((stmts as (_, CodeThingol.Class _)::_)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1134
          add_class stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1135
      | add_stmts ((stmts as (_, CodeThingol.Classrel _)::_)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1136
          add_class stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1137
      | add_stmts ((stmts as (_, CodeThingol.Classparam _)::_)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1138
          add_class stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1139
      | add_stmts ((stmts as [(_, CodeThingol.Classinst _)])) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1140
          add_inst stmts
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1141
      | add_stmts stmts = error ("Illegal mutual dependencies: " ^
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1142
          (commas o map (labelled_name o fst)) stmts);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1143
    fun add_stmts' stmts nsp_nodes =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1144
      let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1145
        val names as (name :: names') = map fst stmts;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1146
        val deps =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1147
          []
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1148
          |> fold (fold (insert (op =)) o Graph.imm_succs program) names
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1149
          |> subtract (op =) names;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1150
        val (module_names, _) = (split_list o map dest_name) names;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1151
        val module_name = (the_single o distinct (op =) o map mk_name_module) module_names
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1152
          handle Empty =>
24811
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1153
            error ("Different namespace prefixes for mutual dependencies:\n"
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1154
              ^ commas (map labelled_name names)
3bf788a0c49a clarified role of class relations
haftmann
parents: 24750
diff changeset
  1155
              ^ "\n"
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1156
              ^ commas module_names);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1157
        val module_name_path = NameSpace.explode module_name;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1158
        fun add_dep name name' =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1159
          let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1160
            val module_name' = (mk_name_module o fst o dest_name) name';
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1161
          in if module_name = module_name' then
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1162
            map_node module_name_path (Graph.add_edge (name, name'))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1163
          else let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1164
            val (common, (diff1::_, diff2::_)) = chop_prefix (op =)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1165
              (module_name_path, NameSpace.explode module_name');
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1166
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1167
            map_node common
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1168
              (fn node => Graph.add_edge_acyclic (diff1, diff2) node
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1169
                handle Graph.CYCLES _ => error ("Dependency "
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1170
                  ^ quote name ^ " -> " ^ quote name'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1171
                  ^ " would result in module dependency cycle"))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1172
          end end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1173
      in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1174
        nsp_nodes
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1175
        |> map_nsp_yield module_name_path (add_stmts stmts)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1176
        |-> (fn (base' :: bases', stmt') =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1177
           apsnd (map_node module_name_path (Graph.new_node (name, (Stmt (base', stmt')))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1178
              #> fold2 (fn name' => fn base' =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1179
                   Graph.new_node (name', (Dummy base'))) names' bases')))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1180
        |> apsnd (fold (fn name => fold (add_dep name) deps) names)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1181
        |> apsnd (fold_product (curry (map_node module_name_path o Graph.add_edge)) names names)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1182
      end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1183
    val (_, nodes) = empty_module
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1184
      |> fold add_stmts' (map (AList.make (Graph.get_node program))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1185
          (rev (Graph.strong_conn program)));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1186
    fun deresolver prefix name = 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1187
      let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1188
        val module_name = (fst o dest_name) name;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1189
        val module_name' = (NameSpace.explode o mk_name_module) module_name;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1190
        val (_, (_, remainder)) = chop_prefix (op =) (prefix, module_name');
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1191
        val stmt_name =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1192
          nodes
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1193
          |> fold (fn name => fn node => case Graph.get_node node name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1194
              of Module (_, (_, node)) => node) module_name'
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1195
          |> (fn node => case Graph.get_node node name of Stmt (stmt_name, _) => stmt_name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1196
               | Dummy stmt_name => stmt_name);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1197
      in
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1198
        NameSpace.implode (remainder @ [stmt_name])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1199
      end handle Graph.UNDEF _ =>
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1200
        error ("Unknown statement name: " ^ labelled_name name);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1201
  in (deresolver, nodes) end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1202
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1203
fun serialize_ml compile pr_module pr_stmt projection module_name labelled_name reserved_names includes raw_module_alias
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1204
  _ syntax_tyco syntax_const program cs destination =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1205
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1206
    val is_cons = CodeThingol.is_cons program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1207
    val (deresolver, nodes) = ml_node_of_program labelled_name module_name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1208
      reserved_names raw_module_alias program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1209
    val reserved_names = CodeName.make_vars reserved_names;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1210
    fun pr_node prefix (Dummy _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1211
          NONE
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1212
      | pr_node prefix (Stmt (_, def)) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1213
          SOME (pr_stmt syntax_tyco syntax_const labelled_name reserved_names
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1214
            (deresolver prefix) is_cons def)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1215
      | pr_node prefix (Module (module_name, (_, nodes))) =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1216
          SOME (pr_module module_name (
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1217
            separate (str "")
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1218
                ((map_filter (pr_node (prefix @ [module_name]) o Graph.get_node nodes)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1219
                o rev o flat o Graph.strong_conn) nodes)));
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1220
    val cs' = map_filter (try (deresolver (if is_some module_name then the_list module_name else [])))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1221
      cs;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1222
    val p = Pretty.chunks (separate (str "") (map snd includes @ (map_filter
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26086
diff changeset
  1223
      (pr_node [] o Graph.get_node nodes) o rev o flat o Graph.strong_conn) nodes));
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1224
    fun output Compile = K NONE o compile o code_of_pretty
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1225
      | output Write = K NONE o code_writeln
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1226
      | output (File file) = K NONE o File.write file o code_of_pretty
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1227
      | output String = SOME o code_of_pretty;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1228
  in projection (output destination p) cs' end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1229
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1230
end; (*local*)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1231
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1232
fun isar_seri_sml module_name =
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1233
  parse_args (Scan.succeed ())
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1234
  #> (fn () => serialize_ml (use_text (1, "generated code") Output.ml_output false)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1235
      pr_sml_module pr_sml_stmt K module_name);
26113
ba5909699cc3 non-operative code antiquotation
haftmann
parents: 26086
diff changeset
  1236
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1237
fun isar_seri_ocaml module_name =
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1238
  parse_args (Scan.succeed ())
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1239
  #> (fn () => serialize_ml (fn _ => error "OCaml: no internal compilation")
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1240
      pr_ocaml_module pr_ocaml_stmt K module_name);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1241
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1242
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1243
(** Haskell serializer **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1244
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1245
val pr_haskell_bind =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1246
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1247
    fun pr_bind ((NONE, NONE), _) = str "_"
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1248
      | pr_bind ((SOME v, NONE), _) = str v
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1249
      | pr_bind ((NONE, SOME p), _) = p
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1250
      | pr_bind ((SOME v, SOME p), _) = brackets [str v, str "@", p];
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1251
  in gen_pr_bind pr_bind end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1252
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1253
fun pr_haskell_stmt syntax_class syntax_tyco syntax_const labelled_name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1254
    init_syms deresolve is_cons contr_classparam_typs deriving_show =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1255
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1256
    val deresolve_base = NameSpace.base o deresolve;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1257
    fun class_name class = case syntax_class class
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1258
     of NONE => deresolve class
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1259
      | SOME (class, _) => class;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1260
    fun classparam_name class classparam = case syntax_class class
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1261
     of NONE => deresolve_base classparam
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1262
      | SOME (_, classparam_syntax) => case classparam_syntax classparam
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1263
         of NONE => (snd o dest_name) classparam
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1264
          | SOME classparam => classparam;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1265
    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
  1266
     of [] => []
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1267
      | classbinds => Pretty.enum "," "(" ")" (
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1268
          map (fn (v, class) =>
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1269
            str (class_name class ^ " " ^ CodeName.lookup_var tyvars v)) classbinds)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1270
          @@ str " => ";
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1271
    fun pr_typforall tyvars vs = case map fst vs
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1272
     of [] => []
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1273
      | vnames => str "forall " :: Pretty.breaks
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1274
          (map (str o CodeName.lookup_var tyvars) vnames) @ str "." @@ Pretty.brk 1;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1275
    fun pr_tycoexpr tyvars fxy (tyco, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1276
      brackify fxy (str tyco :: map (pr_typ tyvars BR) tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1277
    and pr_typ tyvars fxy (tycoexpr as tyco `%% tys) =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1278
          (case syntax_tyco tyco
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1279
           of NONE =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1280
                pr_tycoexpr tyvars fxy (deresolve tyco, tys)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1281
            | SOME (i, pr) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1282
                if not (i = length tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1283
                then error ("Number of argument mismatch in customary serialization: "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1284
                  ^ (string_of_int o length) tys ^ " given, "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1285
                  ^ string_of_int i ^ " expected")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1286
                else pr (pr_typ tyvars) fxy tys)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1287
      | pr_typ tyvars fxy (ITyVar v) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1288
          (str o CodeName.lookup_var tyvars) v;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1289
    fun pr_typdecl tyvars (vs, tycoexpr) =
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1290
      Pretty.block (pr_typcontext tyvars vs @| pr_tycoexpr tyvars NOBR tycoexpr);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1291
    fun pr_typscheme tyvars (vs, ty) =
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1292
      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
  1293
    fun pr_term tyvars lhs vars fxy (IConst c) =
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1294
          pr_app tyvars lhs vars fxy (c, [])
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1295
      | pr_term tyvars lhs vars fxy (t as (t1 `$ t2)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1296
          (case CodeThingol.unfold_const_app t
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1297
           of SOME app => pr_app tyvars lhs vars fxy app
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1298
            | _ =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1299
                brackify fxy [
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1300
                  pr_term tyvars lhs vars NOBR t1,
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1301
                  pr_term tyvars lhs vars BR t2
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1302
                ])
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1303
      | pr_term tyvars lhs vars fxy (IVar v) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1304
          (str o CodeName.lookup_var vars) v
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1305
      | pr_term tyvars lhs vars fxy (t as _ `|-> _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1306
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1307
            val (binds, t') = CodeThingol.unfold_abs t;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1308
            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
  1309
            val (ps, vars') = fold_map pr binds vars;
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1310
          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
  1311
      | pr_term tyvars lhs vars fxy (ICase (cases as (_, t0))) =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1312
          (case CodeThingol.unfold_const_app t0
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1313
           of SOME (c_ts as ((c, _), _)) => if is_none (syntax_const c)
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1314
                then pr_case tyvars vars fxy cases
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1315
                else pr_app tyvars lhs vars fxy c_ts
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1316
            | NONE => pr_case tyvars vars fxy cases)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1317
    and pr_app' tyvars lhs vars ((c, (_, tys)), ts) = case contr_classparam_typs c
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1318
     of [] => (str o deresolve) c :: map (pr_term tyvars lhs vars BR) ts
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1319
      | fingerprint => let
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1320
          val ts_fingerprint = ts ~~ curry Library.take (length ts) fingerprint;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1321
          val needs_annotation = forall (fn (_, NONE) => true | (t, SOME _) =>
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1322
            (not o CodeThingol.locally_monomorphic) t) ts_fingerprint;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1323
          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
  1324
            | pr_term_anno (t, SOME _) ty =
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1325
                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
  1326
        in
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1327
          if needs_annotation then
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1328
            (str o deresolve) c :: map2 pr_term_anno ts_fingerprint (curry Library.take (length ts) tys)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1329
          else (str o deresolve) c :: map (pr_term tyvars lhs vars BR) ts
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1330
        end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1331
    and pr_app tyvars lhs vars  = gen_pr_app (pr_app' tyvars) (pr_term tyvars) syntax_const
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1332
      labelled_name is_cons lhs vars
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1333
    and pr_bind tyvars = pr_haskell_bind (pr_term tyvars)
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1334
    and pr_case tyvars vars fxy (cases as ((_, [_]), _)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1335
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1336
            val (binds, t) = CodeThingol.unfold_let (ICase cases);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1337
            fun pr ((pat, ty), t) vars =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1338
              vars
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1339
              |> pr_bind tyvars BR ((NONE, SOME pat), ty)
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1340
              |>> (fn p => semicolon [p, str "=", pr_term tyvars false vars NOBR t])
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1341
            val (ps, vars') = fold_map pr binds vars;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1342
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1343
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1344
              str "let {",
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1345
              concat [str "}", str "in", pr_term tyvars false vars' NOBR t]
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1346
            ) ps
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1347
          end
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1348
      | pr_case tyvars vars fxy (((td, ty), bs as _ :: _), _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1349
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1350
            fun pr (pat, t) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1351
              let
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1352
                val (p, vars') = pr_bind tyvars NOBR ((NONE, SOME pat), ty) vars;
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1353
              in semicolon [p, str "->", pr_term tyvars false vars' NOBR t] end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1354
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1355
            Pretty.block_enclose (
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1356
              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
  1357
              str "})"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1358
            ) (map pr bs)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1359
          end
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1360
      | pr_case tyvars vars fxy ((_, []), _) = str "error \"empty case\"";
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1361
    fun pr_stmt (name, CodeThingol.Fun ((vs, ty), [])) =
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1362
          let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1363
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1364
            val n = (length o fst o CodeThingol.unfold_fun) ty;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1365
          in
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1366
            Pretty.chunks [
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1367
              Pretty.block [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1368
                (str o suffix " ::" o deresolve_base) name,
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1369
                Pretty.brk 1,
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1370
                pr_typscheme tyvars (vs, ty),
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1371
                str ";"
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1372
              ],
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1373
              concat (
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1374
                (str o deresolve_base) name
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1375
                :: map str (replicate n "_")
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1376
                @ str "="
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1377
                :: str "error"
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1378
                @@ (str o (fn s => s ^ ";") o ML_Syntax.print_string
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1379
                    o NameSpace.base o NameSpace.qualifier) name
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1380
              )
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1381
            ]
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1382
          end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1383
      | pr_stmt (name, CodeThingol.Fun ((vs, ty), eqs)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1384
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1385
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1386
            fun pr_eq ((ts, t), _) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1387
              let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1388
                val consts = map_filter
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1389
                  (fn c => if (is_some o syntax_const) c
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1390
                    then NONE else (SOME o NameSpace.base o deresolve) c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1391
                    ((fold o CodeThingol.fold_constnames) (insert (op =)) (t :: ts) []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1392
                val vars = init_syms
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1393
                  |> CodeName.intro_vars consts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1394
                  |> CodeName.intro_vars ((fold o CodeThingol.fold_unbound_varnames)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1395
                       (insert (op =)) ts []);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1396
              in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1397
                semicolon (
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1398
                  (str o deresolve_base) name
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1399
                  :: map (pr_term tyvars true vars BR) ts
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1400
                  @ str "="
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1401
                  @@ pr_term tyvars false vars NOBR t
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1402
                )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1403
              end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1404
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1405
            Pretty.chunks (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1406
              Pretty.block [
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1407
                (str o suffix " ::" o deresolve_base) name,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1408
                Pretty.brk 1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1409
                pr_typscheme tyvars (vs, ty),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1410
                str ";"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1411
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1412
              :: map pr_eq eqs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1413
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1414
          end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1415
      | pr_stmt (name, CodeThingol.Datatype (vs, [])) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1416
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1417
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1418
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1419
            semicolon [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1420
              str "data",
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1421
              pr_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1422
            ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1423
          end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1424
      | pr_stmt (name, CodeThingol.Datatype (vs, [(co, [ty])])) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1425
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1426
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1427
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1428
            semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1429
              str "newtype"
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1430
              :: pr_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1431
              :: str "="
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1432
              :: (str o deresolve_base) co
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1433
              :: pr_typ tyvars BR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1434
              :: (if deriving_show name then [str "deriving (Read, Show)"] else [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1435
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1436
          end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1437
      | pr_stmt (name, CodeThingol.Datatype (vs, co :: cos)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1438
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1439
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1440
            fun pr_co (co, tys) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1441
              concat (
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1442
                (str o deresolve_base) co
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1443
                :: map (pr_typ tyvars BR) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1444
              )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1445
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1446
            semicolon (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1447
              str "data"
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1448
              :: pr_typdecl tyvars (vs, (deresolve_base name, map (ITyVar o fst) vs))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1449
              :: str "="
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1450
              :: pr_co co
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1451
              :: map ((fn p => Pretty.block [str "| ", p]) o pr_co) cos
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1452
              @ (if deriving_show name then [str "deriving (Read, Show)"] else [])
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1453
            )
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1454
          end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1455
      | pr_stmt (name, CodeThingol.Class (v, (superclasses, classparams))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1456
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1457
            val tyvars = CodeName.intro_vars [v] init_syms;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1458
            fun pr_classparam (classparam, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1459
              semicolon [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1460
                (str o classparam_name name) classparam,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1461
                str "::",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1462
                pr_typ tyvars NOBR ty
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1463
              ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1464
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1465
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1466
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1467
                str "class ",
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1468
                Pretty.block (pr_typcontext tyvars [(v, map fst superclasses)]),
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1469
                str (deresolve_base name ^ " " ^ CodeName.lookup_var tyvars v),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1470
                str " where {"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1471
              ],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1472
              str "};"
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1473
            ) (map pr_classparam classparams)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1474
          end
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1475
      | pr_stmt (_, CodeThingol.Classinst ((class, (tyco, vs)), (_, classparam_insts))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1476
          let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1477
            val tyvars = CodeName.intro_vars (map fst vs) init_syms;
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1478
            fun pr_instdef ((classparam, c_inst), _) =
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1479
              semicolon [
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1480
                (str o classparam_name class) classparam,
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1481
                str "=",
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1482
                pr_app tyvars false init_syms NOBR (c_inst, [])
24591
6509626eb2c9 added explicit theorems
haftmann
parents: 24423
diff changeset
  1483
              ];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1484
          in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1485
            Pretty.block_enclose (
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1486
              Pretty.block [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1487
                str "instance ",
25621
97ebdbdb0299 heutistics for type annotations in Haskell
haftmann
parents: 25538
diff changeset
  1488
                Pretty.block (pr_typcontext tyvars vs),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1489
                str (class_name class ^ " "),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1490
                pr_typ tyvars BR (tyco `%% map (ITyVar o fst) vs),
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1491
                str " where {"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1492
              ],
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1493
              str "};"
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1494
            ) (map pr_instdef classparam_insts)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1495
          end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1496
  in pr_stmt end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1497
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1498
fun pretty_haskell_monad c_bind =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1499
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1500
    fun pretty pr vars fxy [(t, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1501
      let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1502
        val pr_bind = pr_haskell_bind (K pr);
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1503
        fun pr_monad (NONE, t) vars =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1504
              (semicolon [pr vars NOBR t], vars)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1505
          | pr_monad (SOME (bind, true), t) vars = vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1506
              |> pr_bind NOBR bind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1507
              |>> (fn p => semicolon [p, str "<-", pr vars NOBR t])
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1508
          | pr_monad (SOME (bind, false), t) vars = vars
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1509
              |> pr_bind NOBR bind
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1510
              |>> (fn p => semicolon [str "let", p, str "=", pr vars NOBR t]);
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1511
        val (binds, t) = implode_monad c_bind t;
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1512
        val (ps, vars') = fold_map pr_monad binds vars;
26010
a741416574e1 tuned names
haftmann
parents: 25985
diff changeset
  1513
        fun brack p = if fixity BR fxy then Pretty.block [str "(", p, str ")"] else p;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1514
      in (brack o Pretty.block_enclose (str "do {", str "}")) (ps @| pr vars' NOBR t) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1515
  in (1, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1516
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1517
fun haskell_program_of_program labelled_name module_name module_prefix reserved_names raw_module_alias program =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1518
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1519
    val module_alias = if is_some module_name then K module_name else raw_module_alias;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1520
    val reserved_names = Name.make_context reserved_names;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1521
    val mk_name_module = mk_name_module reserved_names module_prefix module_alias program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1522
    fun add_stmt (name, (stmt, deps)) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1523
      let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1524
        val (module_name, base) = dest_name name;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1525
        val module_name' = mk_name_module module_name;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1526
        val mk_name_stmt = yield_singleton Name.variants;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1527
        fun add_fun upper (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1528
          let
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1529
            val (base', nsp_fun') =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1530
              mk_name_stmt (if upper then first_upper base else base) nsp_fun
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1531
          in (base', (nsp_fun', nsp_typ)) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1532
        fun add_typ (nsp_fun, nsp_typ) =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1533
          let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1534
            val (base', nsp_typ') = mk_name_stmt (first_upper base) nsp_typ
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1535
          in (base', (nsp_fun, nsp_typ')) end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1536
        val add_name = case stmt
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1537
         of CodeThingol.Fun _ => add_fun false
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1538
          | CodeThingol.Datatype _ => add_typ
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1539
          | CodeThingol.Datatypecons _ => add_fun true
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1540
          | CodeThingol.Class _ => add_typ
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1541
          | CodeThingol.Classrel _ => pair base
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1542
          | CodeThingol.Classparam _ => add_fun false
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1543
          | CodeThingol.Classinst _ => pair base;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1544
        fun add_stmt' base' = case stmt
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1545
         of CodeThingol.Datatypecons _ =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1546
              cons (name, (NameSpace.append module_name' base', NONE))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1547
          | CodeThingol.Classrel _ => I
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1548
          | CodeThingol.Classparam _ =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1549
              cons (name, (NameSpace.append module_name' base', NONE))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1550
          | _ => cons (name, (NameSpace.append module_name' base', SOME stmt));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1551
      in
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1552
        Symtab.map_default (module_name', ([], ([], (reserved_names, reserved_names))))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1553
              (apfst (fold (insert (op = : string * string -> bool)) deps))
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1554
        #> `(fn program => add_name ((snd o snd o the o Symtab.lookup program) module_name'))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1555
        #-> (fn (base', names) =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1556
              (Symtab.map_entry module_name' o apsnd) (fn (stmts, _) =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1557
              (add_stmt' base' stmts, names)))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1558
      end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1559
    val hs_program = fold add_stmt (AList.make (fn name =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1560
      (Graph.get_node program name, Graph.imm_succs program name))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1561
      (Graph.strong_conn program |> flat)) Symtab.empty;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1562
    fun deresolver name =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1563
      (fst o the o AList.lookup (op =) ((fst o snd o the
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1564
        o Symtab.lookup hs_program) ((mk_name_module o fst o dest_name) name))) name
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1565
        handle Option => error ("Unknown statement name: " ^ labelled_name name);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1566
  in (deresolver, hs_program) end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1567
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1568
fun serialize_haskell module_prefix module_name string_classes labelled_name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1569
    reserved_names includes raw_module_alias
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1570
    syntax_class syntax_tyco syntax_const program cs destination =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1571
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1572
    val (deresolver, hs_program) = haskell_program_of_program labelled_name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1573
      module_name module_prefix reserved_names raw_module_alias program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1574
    val is_cons = CodeThingol.is_cons program;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1575
    val contr_classparam_typs = CodeThingol.contr_classparam_typs program;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1576
    fun deriving_show tyco =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1577
      let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1578
        fun deriv _ "fun" = false
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1579
          | deriv tycos tyco = member (op =) tycos tyco orelse
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1580
              case try (Graph.get_node program) tyco
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1581
                of SOME (CodeThingol.Datatype (_, cs)) => forall (deriv' (tyco :: tycos))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1582
                    (maps snd cs)
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1583
                 | NONE => true
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1584
        and deriv' tycos (tyco `%% tys) = deriv tycos tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1585
              andalso forall (deriv' tycos) tys
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1586
          | deriv' _ (ITyVar _) = true
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1587
      in deriv [] tyco end;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1588
    val reserved_names = CodeName.make_vars reserved_names;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1589
    fun pr_stmt qualified = pr_haskell_stmt syntax_class syntax_tyco
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1590
      syntax_const labelled_name reserved_names
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1591
      (if qualified then deresolver else NameSpace.base o deresolver)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1592
      is_cons contr_classparam_typs
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1593
      (if string_classes then deriving_show else K false);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1594
    fun pr_module name content =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1595
      (name, Pretty.chunks [
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1596
        str ("module " ^ name ^ " where {"),
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1597
        str "",
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1598
        content,
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1599
        str "",
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1600
        str "}"
27001
d21bb9f73364 fixed utterly wrong print mode handling
haftmann
parents: 27000
diff changeset
  1601
      ]);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1602
    fun serialize_module (module_name', (deps, (stmts, _))) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1603
      let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1604
        val stmt_names = map fst stmts;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1605
        val deps' = subtract (op =) stmt_names deps
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1606
          |> distinct (op =)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1607
          |> map_filter (try deresolver);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1608
        val qualified = is_none module_name andalso
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1609
          map deresolver stmt_names @ deps'
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1610
          |> map NameSpace.base
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1611
          |> has_duplicates (op =);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1612
        val imports = deps'
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1613
          |> map NameSpace.qualifier
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1614
          |> distinct (op =);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1615
        fun pr_import_include (name, _) = str ("import " ^ name ^ ";");
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1616
        val pr_import_module = str o (if qualified
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1617
          then prefix "import qualified "
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1618
          else prefix "import ") o suffix ";";
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1619
        val content = Pretty.chunks (
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1620
            map pr_import_include includes
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1621
            @ map pr_import_module imports
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1622
            @ str ""
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1623
            :: separate (str "") (map_filter
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1624
              (fn (name, (_, SOME stmt)) => SOME (pr_stmt qualified (name, stmt))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1625
                | (_, (_, NONE)) => NONE) stmts)
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1626
          )
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1627
      in pr_module module_name' content end;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1628
    fun write_module destination (modlname, content) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1629
      let
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1630
        val filename = case modlname
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1631
         of "" => Path.explode "Main.hs"
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1632
          | _ => (Path.ext "hs" o Path.explode o implode o separate "/"
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1633
                o NameSpace.explode) modlname;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1634
        val pathname = Path.append destination filename;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1635
        val _ = File.mkdir (Path.dir pathname);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1636
      in File.write pathname (code_of_pretty content) end
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1637
    fun output Compile = error ("Haskell: no internal compilation")
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1638
      | output Write = K NONE o map (code_writeln o snd)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1639
      | output (File destination) = K NONE o map (write_module destination)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1640
      | output String = SOME o cat_lines o map (code_of_pretty o snd);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1641
  in
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1642
    output destination (map (uncurry pr_module) includes
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1643
      @ map serialize_module (Symtab.dest hs_program))
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1644
  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1645
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1646
fun isar_seri_haskell module =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1647
  parse_args (Scan.option (Args.$$$ "root" -- Args.colon |-- Args.name)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1648
    -- Scan.optional (Args.$$$ "string_classes" >> K true) false
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1649
    >> (fn (module_prefix, string_classes) =>
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1650
      serialize_haskell module_prefix module string_classes));
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1651
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1652
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1653
(** optional pretty serialization **)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1654
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1655
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1656
26448
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1657
fun ocaml_char c =
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1658
  let
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1659
    fun chr i =
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1660
      let
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1661
        val xs = string_of_int i;
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1662
        val ys = replicate_string (3 - length (explode xs)) "0";
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1663
      in "\\" ^ ys ^ xs end;
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1664
    val i = ord c;
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1665
    val s = if i < 32 orelse i = 34 orelse i = 39 orelse i = 92 orelse i > 126
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1666
      then chr i else c
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1667
  in s end;
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1668
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1669
fun haskell_char c =
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1670
  let
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1671
    val s = ML_Syntax.print_char c;
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1672
  in if s = "'" then "\\'" else s end;
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1673
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1674
val pretty : (string * {
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1675
    pretty_char: string -> string,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1676
    pretty_string: string -> string,
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1677
    pretty_numeral: bool -> int -> string,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1678
    pretty_list: Pretty.T list -> Pretty.T,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1679
    infix_cons: int * string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1680
  }) list = [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1681
  ("SML", { pretty_char = prefix "#" o quote o ML_Syntax.print_char,
26448
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1682
      pretty_string = quote o translate_string ML_Syntax.print_char,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1683
      pretty_numeral = fn unbounded => fn k =>
24750
95a315591af8 added ocaml strings
haftmann
parents: 24702
diff changeset
  1684
        if unbounded then "(" ^ string_of_int k ^ " : IntInf.int)"
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1685
        else string_of_int k,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1686
      pretty_list = Pretty.enum "," "[" "]",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1687
      infix_cons = (7, "::")}),
26448
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1688
  ("OCaml", { pretty_char = enclose "'" "'" o ocaml_char,
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1689
      pretty_string = quote o translate_string ocaml_char,
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1690
      pretty_numeral = fn unbounded => fn k => if k >= 0 then
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1691
            if unbounded then
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1692
              "(Big_int.big_int_of_int " ^ string_of_int k ^ ")"
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1693
            else string_of_int k
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1694
          else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1695
            if unbounded then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1696
              "(Big_int.big_int_of_int " ^ (enclose "(" ")" o prefix "-"
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1697
                o string_of_int o op ~) k ^ ")"
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1698
            else (enclose "(" ")" o prefix "-" o string_of_int o op ~) k,
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1699
      pretty_list = Pretty.enum ";" "[" "]",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1700
      infix_cons = (6, "::")}),
26448
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1701
  ("Haskell", { pretty_char = enclose "'" "'" o haskell_char,
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1702
      pretty_string = quote o translate_string haskell_char,
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1703
      pretty_numeral = fn unbounded => fn k => if k >= 0 then string_of_int k
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24621
diff changeset
  1704
          else enclose "(" ")" (signed_string_of_int k),
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1705
      pretty_list = Pretty.enum "," "[" "]",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1706
      infix_cons = (5, ":")})
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1707
];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1708
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1709
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1710
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1711
fun pr_pretty target = case AList.lookup (op =) pretty target
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1712
 of SOME x => x
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1713
  | NONE => error ("Unknown code target language: " ^ quote target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1714
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1715
fun default_list (target_fxy, target_cons) pr fxy t1 t2 =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1716
  brackify_infix (target_fxy, R) fxy [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1717
    pr (INFX (target_fxy, X)) t1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1718
    str target_cons,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1719
    pr (INFX (target_fxy, R)) t2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1720
  ];
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1721
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1722
fun pretty_list c_nil c_cons target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1723
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1724
    val pretty_ops = pr_pretty target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1725
    val mk_list = #pretty_list pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1726
    fun pretty pr vars fxy [(t1, _), (t2, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1727
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1728
       of SOME ts => mk_list (map (pr vars NOBR) ts)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1729
        | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1730
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1731
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1732
fun pretty_list_string c_nil c_cons c_char c_nibbles target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1733
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1734
    val pretty_ops = pr_pretty target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1735
    val mk_list = #pretty_list pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1736
    val mk_char = #pretty_char pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1737
    val mk_string = #pretty_string pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1738
    fun pretty pr vars fxy [(t1, _), (t2, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1739
      case Option.map (cons t1) (implode_list c_nil c_cons t2)
26448
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1740
       of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1741
           of SOME p => p
26448
faf056ac64c4 clarified character serializations
haftmann
parents: 26385
diff changeset
  1742
            | NONE => mk_list (map (pr vars NOBR) ts))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1743
        | NONE => default_list (#infix_cons pretty_ops) (pr vars) fxy t1 t2;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1744
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1745
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1746
fun pretty_char c_char c_nibbles target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1747
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1748
    val mk_char = #pretty_char (pr_pretty target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1749
    fun pretty _ _ _ [(t1, _), (t2, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1750
      case decode_char c_nibbles (t1, t2)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1751
       of SOME c => (str o mk_char) c
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1752
        | NONE => error "Illegal character expression";
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1753
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1754
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
  1755
fun pretty_numeral unbounded negative c_pls c_min c_bit0 c_bit1 target =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1756
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1757
    val mk_numeral = #pretty_numeral (pr_pretty target);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1758
    fun pretty _ _ _ [(t, _)] =
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
  1759
      (str o mk_numeral unbounded o implode_numeral negative c_pls c_min c_bit0 c_bit1) t;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1760
  in (1, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1761
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1762
fun pretty_message c_char c_nibbles c_nil c_cons target =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1763
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1764
    val pretty_ops = pr_pretty target;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1765
    val mk_char = #pretty_char pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1766
    val mk_string = #pretty_string pretty_ops;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1767
    fun pretty pr vars fxy [(t, _)] =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1768
      case implode_list c_nil c_cons t
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1769
       of SOME ts => (case implode_string c_char c_nibbles mk_char mk_string ts
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1770
           of SOME p => p
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1771
            | NONE => error "Illegal message expression")
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1772
        | NONE => error "Illegal message expression";
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1773
  in (1, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1774
26753
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1775
fun pretty_imperative_monad_bind bind' return' unit' =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1776
  let
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1777
    val dummy_name = "";
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1778
    val dummy_type = ITyVar dummy_name;
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1779
    val dummy_case_term = IVar dummy_name;
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1780
    (*assumption: dummy values are not relevant for serialization*)
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1781
    val unitt = IConst (unit', ([], []));
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1782
    fun dest_abs ((v, ty) `|-> t, _) = ((v, ty), t)
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1783
      | dest_abs (t, ty) =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1784
          let
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1785
            val vs = CodeThingol.fold_varnames cons t [];
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1786
            val v = Name.variant vs "x";
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1787
            val ty' = (hd o fst o CodeThingol.unfold_fun) ty;
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1788
          in ((v, ty'), t `$ IVar v) end;
26753
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1789
    fun force (t as IConst (c, _) `$ t') = if c = return'
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1790
          then t' else t `$ unitt
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1791
      | force t = t `$ unitt;
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1792
    fun tr_bind' [(t1, _), (t2, ty2)] =
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1793
      let
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1794
        val ((v, ty), t) = dest_abs (t2, ty2);
26753
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1795
      in ICase (((force t1, ty), [(IVar v, tr_bind'' t)]), dummy_case_term) end
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1796
    and tr_bind'' t = case CodeThingol.unfold_app t
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1797
         of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if c = bind'
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1798
              then tr_bind' [(x1, ty1), (x2, ty2)]
26753
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1799
              else force t
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  1800
          | _ => force t;
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1801
    fun tr_bind ts = (dummy_name, dummy_type)
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  1802
      `|-> ICase (((IVar dummy_name, dummy_type), [(unitt, tr_bind' ts)]), dummy_case_term);
24702
f875049a13a1 ML monad support
haftmann
parents: 24662
diff changeset
  1803
    fun pretty pr vars fxy ts = pr vars fxy (tr_bind ts);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1804
  in (2, pretty) end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1805
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1806
fun no_bindings x = (Option.map o apsnd)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1807
  (fn pretty => fn pr => fn vars => pretty (pr vars)) x;
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1808
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1809
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1810
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1811
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1812
(** serializer interfaces **)
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1813
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1814
(* evaluation *)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1815
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1816
fun eval_serializer module [] = serialize_ml
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1817
  (use_text (1, "code to be evaluated") Output.ml_output false) 
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1818
  (K Pretty.chunks) pr_sml_stmt
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1819
  (fn SOME s => fn cs => SOME ("let\n" ^ s ^ "\nin fn () => " ^ enclose "(" ")" (commas cs) ^ " end"))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1820
  (SOME "Isabelle_Eval");
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1821
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1822
fun sml_code_of thy program cs = mount_serializer thy (SOME eval_serializer) target_SML NONE [] program cs String
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1823
  |> the;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1824
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1825
fun eval eval'' term_of reff thy ct args =
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1826
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1827
    val _ = if null (term_frees (term_of ct)) then () else error ("Term "
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1828
      ^ quote (Syntax.string_of_term_global thy (term_of ct))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1829
      ^ " to be evaluated contains free variables");
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1830
    fun eval' program ((vs, ty), t) deps =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1831
      let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1832
        val _ = if CodeThingol.contains_dictvar t then
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1833
          error "Term to be evaluated constains free dictionaries" else ();
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1834
        val program' = program
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1835
          |> Graph.new_node (CodeName.value_name, CodeThingol.Fun (([], ty), [(([], t), Drule.dummy_thm)]))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1836
          |> fold (curry Graph.add_edge CodeName.value_name) deps;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1837
        val value_code = sml_code_of thy program' [CodeName.value_name] ;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1838
        val sml_code = space_implode " " (value_code :: "()" :: map (enclose "(" ")") args);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1839
      in ML_Context.evaluate Output.ml_output false reff sml_code end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1840
  in eval'' thy (fn t => (t, eval')) ct end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1841
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1842
fun eval_conv reff = eval CodeThingol.eval_conv Thm.term_of reff;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1843
fun eval_term reff = eval CodeThingol.eval_term I reff;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1844
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1845
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1846
(* presentation *)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1847
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1848
fun code_of thy target module_name cs =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1849
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1850
    val (cs', program) = CodeThingol.consts_program thy cs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1851
  in
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1852
    string (serialize thy target (SOME module_name) [] program cs')
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1853
  end;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1854
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1855
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1856
(* infix syntax *)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1857
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1858
datatype 'a mixfix =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1859
    Arg of fixity
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1860
  | Pretty of Pretty.T;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1861
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1862
fun mk_mixfix prep_arg (fixity_this, mfx) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1863
  let
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1864
    fun is_arg (Arg _) = true
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1865
      | is_arg _ = false;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1866
    val i = (length o filter is_arg) mfx;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1867
    fun fillin _ [] [] =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1868
          []
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1869
      | fillin pr (Arg fxy :: mfx) (a :: args) =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1870
          (pr fxy o prep_arg) a :: fillin pr mfx args
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1871
      | fillin pr (Pretty p :: mfx) args =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1872
          p :: fillin pr mfx args
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1873
      | fillin _ [] _ =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1874
          error ("Inconsistent mixfix: too many arguments")
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1875
      | fillin _ _ [] =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1876
          error ("Inconsistent mixfix: too less arguments");
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1877
  in
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1878
    (i, fn pr => fn fixity_ctxt => fn args =>
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1879
      gen_brackify (fixity fixity_this fixity_ctxt) (fillin pr mfx args))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1880
  end;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1881
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1882
fun parse_infix prep_arg (x, i) s =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1883
  let
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1884
    val l = case x of L => INFX (i, L) | _ => INFX (i, X);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1885
    val r = case x of R => INFX (i, R) | _ => INFX (i, X);
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1886
  in
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1887
    mk_mixfix prep_arg (INFX (i, x),
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1888
      [Arg l, (Pretty o Pretty.brk) 1, (Pretty o str) s, (Pretty o Pretty.brk) 1, Arg r])
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1889
  end;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1890
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1891
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  1892
(* data access *)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1893
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1894
local
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1895
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1896
fun cert_class thy class =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1897
  let
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1898
    val _ = AxClass.get_info thy class;
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1899
  in class end;
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1900
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1901
fun read_class thy = cert_class thy o Sign.intern_class thy;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1902
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1903
fun cert_tyco thy tyco =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1904
  let
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1905
    val _ = if Sign.declared_tyname thy tyco then ()
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1906
      else error ("No such type constructor: " ^ quote tyco);
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1907
  in tyco end;
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1908
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1909
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1910
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1911
fun gen_add_syntax_class prep_class prep_const target raw_class raw_syn thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1912
  let
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1913
    val class = prep_class thy raw_class;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1914
    val class' = CodeName.class thy class;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1915
    fun mk_classparam c = case AxClass.class_of_param thy c
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1916
     of SOME class'' => if class = class'' then CodeName.const thy c
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1917
          else error ("Not a class operation for class " ^ quote class ^ ": " ^ quote c)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1918
      | NONE => error ("Not a class operation: " ^ quote c);
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1919
    fun mk_syntax_params raw_params = AList.lookup (op =)
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1920
      ((map o apfst) (mk_classparam o prep_const thy) raw_params);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1921
  in case raw_syn
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1922
   of SOME (syntax, raw_params) =>
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1923
      thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1924
      |> (map_name_syntax target o apfst o apfst)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1925
           (Symtab.update (class', (syntax, mk_syntax_params raw_params)))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1926
    | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1927
      thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1928
      |> (map_name_syntax target o apfst o apfst)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  1929
           (Symtab.delete_safe class')
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1930
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1931
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1932
fun gen_add_syntax_inst prep_class prep_tyco target (raw_tyco, raw_class) add_del thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1933
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1934
    val inst = CodeName.instance thy (prep_class thy raw_class, prep_tyco thy raw_tyco);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1935
  in if add_del then
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1936
    thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1937
    |> (map_name_syntax target o apfst o apsnd)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1938
        (Symtab.update (inst, ()))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1939
  else
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1940
    thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1941
    |> (map_name_syntax target o apfst o apsnd)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1942
        (Symtab.delete_safe inst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1943
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1944
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1945
fun gen_add_syntax_tyco prep_tyco target raw_tyco raw_syn thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1946
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1947
    val tyco = prep_tyco thy raw_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1948
    val tyco' = if tyco = "fun" then "fun" else CodeName.tyco thy tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1949
    fun check_args (syntax as (n, _)) = if n <> Sign.arity_number thy tyco
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1950
      then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1951
      else syntax
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1952
  in case raw_syn
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1953
   of SOME syntax =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1954
      thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1955
      |> (map_name_syntax target o apsnd o apfst)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1956
           (Symtab.update (tyco', check_args syntax))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1957
   | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1958
      thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1959
      |> (map_name_syntax target o apsnd o apfst)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1960
           (Symtab.delete_safe tyco')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1961
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1962
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1963
fun gen_add_syntax_const prep_const target raw_c raw_syn thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1964
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1965
    val c = prep_const thy raw_c;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1966
    val c' = CodeName.const thy c;
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1967
    fun check_args (syntax as (n, _)) = if n > CodeUnit.no_args thy c
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  1968
      then error ("Too many arguments in syntax for constant " ^ quote c)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1969
      else syntax;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1970
  in case raw_syn
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1971
   of SOME syntax =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1972
      thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1973
      |> (map_name_syntax target o apsnd o apsnd)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1974
           (Symtab.update (c', check_args syntax))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1975
   | NONE =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1976
      thy
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  1977
      |> (map_name_syntax target o apsnd o apsnd)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1978
           (Symtab.delete_safe c')
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1979
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1980
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1981
fun add_reserved target =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1982
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1983
    fun add sym syms = if member (op =) syms sym
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1984
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1985
      else insert (op =) sym syms
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1986
  in map_reserved target o add end;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1987
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1988
fun add_include target =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1989
  let
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1990
    fun add (name, SOME content) incls =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1991
          let
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1992
            val _ = if Symtab.defined incls name
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1993
              then warning ("Overwriting existing include " ^ name)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1994
              else ();
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1995
          in Symtab.update (name, str content) incls end
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1996
      | add (name, NONE) incls =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  1997
          Symtab.delete name incls;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  1998
  in map_includes target o add end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  1999
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2000
fun add_module_alias target =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2001
  map_module_alias target o Symtab.update o apsnd CodeName.check_modulename;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2002
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2003
fun add_monad target raw_c_run raw_c_bind raw_c_return_unit thy =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2004
  let
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2005
    val c_run = CodeUnit.read_const thy raw_c_run;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2006
    val c_bind = CodeUnit.read_const thy raw_c_bind;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2007
    val c_bind' = CodeName.const thy c_bind;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2008
    val c_return_unit' = (Option.map o pairself)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2009
      (CodeName.const thy o CodeUnit.read_const thy) raw_c_return_unit;
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  2010
    val is_haskell = target = target_Haskell;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2011
    val _ = if is_haskell andalso is_some c_return_unit'
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  2012
      then error ("No unit entry may be given for Haskell monad")
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  2013
      else ();
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2014
    val _ = if not is_haskell andalso is_none c_return_unit'
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  2015
      then error ("Unit entry must be given for SML/OCaml monad")
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  2016
      else ();
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2017
  in if target = target_Haskell then
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2018
    thy
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2019
    |> gen_add_syntax_const (K I) target_Haskell c_run
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2020
          (SOME (pretty_haskell_monad c_bind'))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2021
    |> gen_add_syntax_const (K I) target_Haskell c_bind
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2022
          (no_bindings (SOME (parse_infix fst (L, 1) ">>=")))
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2023
  else
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2024
    thy
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2025
    |> gen_add_syntax_const (K I) target c_bind
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2026
          (SOME (pretty_imperative_monad_bind c_bind'
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2027
            ((fst o the) c_return_unit') ((snd o the) c_return_unit')))
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2028
  end;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2029
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2030
fun gen_allow_abort prep_cs raw_c thy =
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  2031
  let
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  2032
    val c = prep_cs thy raw_c;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  2033
    val c' = CodeName.const thy c;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  2034
  in thy |> (CodeTargetData.map o apsnd) (insert (op =) c') end;
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  2035
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2036
fun zip_list (x::xs) f g =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2037
  f
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2038
  #-> (fn y =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2039
    fold_map (fn x => g |-- f >> pair x) xs
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2040
    #-> (fn xys => pair ((x, y) :: xys)));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2041
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2042
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2043
(* concrete syntax *)
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2044
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2045
structure P = OuterParse
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2046
and K = OuterKeyword
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2047
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2048
fun parse_multi_syntax parse_thing parse_syntax =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2049
  P.and_list1 parse_thing
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2050
  #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2051
        (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2052
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2053
val (infixK, infixlK, infixrK) = ("infix", "infixl", "infixr");
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2054
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2055
fun parse_mixfix prep_arg s =
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2056
  let
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2057
    val sym_any = Scan.one Symbol.is_regular;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2058
    val parse = Scan.optional ($$ "!" >> K true) false -- Scan.repeat (
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2059
         ($$ "(" -- $$ "_" -- $$ ")" >> K (Arg NOBR))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2060
      || ($$ "_" >> K (Arg BR))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2061
      || ($$ "/" |-- Scan.repeat ($$ " ") >> (Pretty o Pretty.brk o length))
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2062
      || (Scan.repeat1
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2063
           (   $$ "'" |-- sym_any
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2064
            || Scan.unless ($$ "_" || $$ "/" || $$ "(" |-- $$ "_" |-- $$ ")")
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2065
                 sym_any) >> (Pretty o str o implode)));
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2066
  in case Scan.finite Symbol.stopper parse (Symbol.explode s)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2067
   of ((_, p as [_]), []) => mk_mixfix prep_arg (NOBR, p)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2068
    | ((b, p as _ :: _ :: _), []) => mk_mixfix prep_arg (if b then NOBR else BR, p)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2069
    | _ => Scan.!!
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2070
        (the_default ("malformed mixfix annotation: " ^ quote s) o snd) Scan.fail ()
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2071
  end;
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2072
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2073
fun parse_syntax prep_arg xs =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2074
  Scan.option ((
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2075
      ((P.$$$ infixK  >> K X)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2076
        || (P.$$$ infixlK >> K L)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2077
        || (P.$$$ infixrK >> K R))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2078
        -- P.nat >> parse_infix prep_arg
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2079
      || Scan.succeed (parse_mixfix prep_arg))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2080
      -- P.string
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2081
      >> (fn (parse, s) => parse s)) xs;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2082
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2083
in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2084
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2085
val parse_syntax = parse_syntax;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2086
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2087
val add_syntax_class = gen_add_syntax_class cert_class (K I);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2088
val add_syntax_inst = gen_add_syntax_inst cert_class cert_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2089
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2090
val add_syntax_const = gen_add_syntax_const (K I);
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2091
val allow_abort = gen_allow_abort (K I);
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2092
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2093
val add_syntax_class_cmd = gen_add_syntax_class read_class CodeUnit.read_const;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2094
val add_syntax_inst_cmd = gen_add_syntax_inst read_class read_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2095
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2096
val add_syntax_const_cmd = gen_add_syntax_const CodeUnit.read_const;
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2097
val allow_abort_cmd = gen_allow_abort CodeUnit.read_const;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2098
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2099
fun add_syntax_tycoP target tyco = parse_syntax I >> add_syntax_tyco_cmd target tyco;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2100
fun add_syntax_constP target c = parse_syntax fst >> (add_syntax_const_cmd target c o no_bindings);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2101
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2102
fun add_undefined target undef target_undefined thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2103
  let
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2104
    fun pr _ _ _ _ = str target_undefined;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2105
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2106
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2107
    |> add_syntax_const target undef (SOME (~1, pr))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2108
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2109
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2110
fun add_pretty_list target nill cons thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2111
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2112
    val nil' = CodeName.const thy nill;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2113
    val cons' = CodeName.const thy cons;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2114
    val pr = pretty_list nil' cons' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2115
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2116
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2117
    |> add_syntax_const target cons (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2118
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2119
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2120
fun add_pretty_list_string target nill cons charr nibbles thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2121
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2122
    val nil' = CodeName.const thy nill;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2123
    val cons' = CodeName.const thy cons;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2124
    val charr' = CodeName.const thy charr;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2125
    val nibbles' = map (CodeName.const thy) nibbles;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2126
    val pr = pretty_list_string nil' cons' charr' nibbles' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2127
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2128
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2129
    |> add_syntax_const target cons (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2130
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2131
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2132
fun add_pretty_char target charr nibbles thy =
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2133
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2134
    val charr' = CodeName.const thy charr;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2135
    val nibbles' = map (CodeName.const thy) nibbles;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2136
    val pr = pretty_char charr' nibbles' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2137
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2138
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2139
    |> add_syntax_const target charr (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2140
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2141
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
  2142
fun add_pretty_numeral target unbounded negative number_of pls min bit0 bit1 thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2143
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2144
    val pls' = CodeName.const thy pls;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2145
    val min' = CodeName.const thy min;
26086
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
  2146
    val bit0' = CodeName.const thy bit0;
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
  2147
    val bit1' = CodeName.const thy bit1;
3c243098b64a New simpler representation of numerals, using Bit0 and Bit1 instead of BIT, B0, and B1
huffman
parents: 26010
diff changeset
  2148
    val pr = pretty_numeral unbounded negative pls' min' bit0' bit1' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2149
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2150
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2151
    |> add_syntax_const target number_of (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2152
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2153
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2154
fun add_pretty_message target charr nibbles nill cons str thy =
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2155
  let
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2156
    val charr' = CodeName.const thy charr;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2157
    val nibbles' = map (CodeName.const thy) nibbles;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2158
    val nil' = CodeName.const thy nill;
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2159
    val cons' = CodeName.const thy cons;
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2160
    val pr = pretty_message charr' nibbles' nil' cons' target;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2161
  in
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2162
    thy
24423
ae9cd0e92423 overloaded definitions accompanied by explicit constants
haftmann
parents: 24381
diff changeset
  2163
    |> add_syntax_const target str (SOME pr)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2164
  end;
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2165
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2166
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2167
(** code generation at a glance **)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2168
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2169
fun read_const_exprs thy cs =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2170
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2171
    val (cs1, cs2) = CodeName.read_const_exprs thy cs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2172
    val (cs3, program) = CodeThingol.consts_program thy cs2;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2173
    val cs4 = CodeThingol.transitivly_non_empty_funs program (abort_allowed thy);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2174
    val cs5 = map_filter
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2175
      (fn (c, c') => if member (op =) cs4 c' then SOME c else NONE) (cs2 ~~ cs3);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2176
  in fold (insert (op =)) cs5 cs1 end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2177
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2178
fun cached_program thy = 
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2179
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2180
    val program = CodeThingol.cached_program thy;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2181
  in (CodeThingol.transitivly_non_empty_funs program (abort_allowed thy), program) end
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2182
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2183
fun code thy cs seris =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2184
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2185
    val (cs', program) = if null cs
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2186
      then cached_program thy
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2187
      else CodeThingol.consts_program thy cs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2188
    fun mk_seri_dest dest = case dest
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2189
     of NONE => compile
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2190
      | SOME "-" => write
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2191
      | SOME f => file (Path.explode f)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2192
    val _ = map (fn (((target, module), dest), args) =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2193
      (mk_seri_dest dest (serialize thy target module args program cs'))) seris;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2194
  in () end;
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2195
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2196
fun sml_of thy cs = 
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2197
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2198
    val (cs', program) = CodeThingol.consts_program thy cs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2199
  in sml_code_of thy program cs' ^ " ()" end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2200
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2201
fun code_antiq (ctxt, args) = 
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2202
  let
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2203
    val thy = Context.theory_of ctxt;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2204
    val (ts, (ctxt', args')) = Scan.repeat1 Args.term (ctxt, args);
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2205
    val cs = map (CodeUnit.check_const thy) ts;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2206
    val s = sml_of thy cs;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2207
  in (("codevals", s), (ctxt', args')) end;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2208
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2209
fun export_code_cmd raw_cs seris thy = code thy (read_const_exprs thy raw_cs) seris;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2210
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2211
val (inK, module_nameK, fileK) = ("in", "module_name", "file");
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2212
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2213
fun code_exprP cmd =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2214
  (Scan.repeat P.term
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2215
  -- Scan.repeat (P.$$$ inK |-- P.name
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2216
     -- Scan.option (P.$$$ module_nameK |-- P.name)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2217
     -- Scan.option (P.$$$ fileK |-- P.name)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2218
     -- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") []
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2219
  ) >> (fn (raw_cs, seris) => cmd raw_cs seris));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2220
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2221
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2222
(** Isar setup **)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2223
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2224
val _ = OuterSyntax.keywords [infixK, infixlK, infixrK, inK, module_nameK, fileK];
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2225
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2226
val _ =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2227
  OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl (
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2228
    parse_multi_syntax P.xname
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2229
      (Scan.option (P.string -- Scan.optional (P.$$$ "where" |-- Scan.repeat1
25985
8d69087f6a4b avoid redundant escaping of Isabelle symbols;
wenzelm
parents: 25969
diff changeset
  2230
        (P.term --| (P.$$$ "\<equiv>" || P.$$$ "==") -- P.string)) []))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2231
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2232
          fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2233
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2234
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2235
val _ =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2236
  OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl (
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2237
    parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2238
      ((P.minus >> K true) || Scan.succeed false)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2239
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2240
          fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2241
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2242
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2243
val _ =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2244
  OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl (
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2245
    parse_multi_syntax P.xname (parse_syntax I)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2246
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2247
          fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2248
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2249
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2250
val _ =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2251
  OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl (
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2252
    parse_multi_syntax P.term (parse_syntax fst)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2253
    >> (Toplevel.theory oo fold) (fn (target, syns) =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2254
          fold (fn (raw_const, syn) => add_syntax_const_cmd target raw_const (no_bindings syn)) syns)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2255
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2256
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2257
val _ =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2258
  OuterSyntax.command "code_monad" "define code syntax for monads" K.thy_decl (
26753
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  2259
    P.term -- P.term -- ((P.term -- P.term >> SOME) -- Scan.repeat1 P.name
26752
6b276119139b corrected ML semantics
haftmann
parents: 26448
diff changeset
  2260
      || Scan.succeed NONE -- Scan.repeat1 P.name)
26753
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  2261
    >> (fn ((raw_run, raw_bind), (raw_unit_return, targets)) => Toplevel.theory 
094d70c81243 dropping return in imperative monad bindings
haftmann
parents: 26752
diff changeset
  2262
          (fold (fn target => add_monad target raw_run raw_bind raw_unit_return) targets))
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2263
  );
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2264
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2265
val _ =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2266
  OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl (
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2267
    P.name -- Scan.repeat1 P.name
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2268
    >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  2269
  );
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2270
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2271
val _ =
24992
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2272
  OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl (
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2273
    P.name -- P.name -- (P.text >> (fn "-" => NONE | s => SOME s))
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2274
    >> (fn ((target, name), content) => (Toplevel.theory o add_include target)
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2275
      (name, content))
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2276
  );
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2277
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2278
val _ =
d33713284207 code_include replaces code_moduleprolog
haftmann
parents: 24928
diff changeset
  2279
  OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl (
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2280
    P.name -- Scan.repeat1 (P.name -- P.name)
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2281
    >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2282
  );
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2283
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2284
val _ =
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2285
  OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl (
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2286
    Scan.repeat1 P.term >> (Toplevel.theory o fold allow_abort_cmd)
24841
df8448bc7a8b concept for exceptions
haftmann
parents: 24811
diff changeset
  2287
  );
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2288
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24841
diff changeset
  2289
val _ =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2290
  OuterSyntax.command "export_code" "generate executable code for constants"
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2291
    K.diag (P.!!! (code_exprP export_code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2292
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2293
fun shell_command thyname cmd = Toplevel.program (fn _ =>
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2294
  (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! (code_exprP export_code_cmd)) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2295
   of SOME f => (writeln "Now generating code..."; f (theory thyname))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2296
    | NONE => error ("Bad directive " ^ quote cmd)))
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2297
  handle TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2298
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2299
val _ = ML_Context.value_antiq "code" code_antiq;
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2300
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2301
27000
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2302
(* serializer setup, including serializer defaults *)
e8a40d8b7897 new serializer interface
haftmann
parents: 26998
diff changeset
  2303
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2304
val setup =
27103
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2305
  add_target (target_SML, isar_seri_sml)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2306
  #> add_target (target_OCaml, isar_seri_ocaml)
d8549f4d900b major refactorings in code generator modules
haftmann
parents: 27024
diff changeset
  2307
  #> add_target (target_Haskell, isar_seri_haskell)
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2308
  #> add_syntax_tyco "SML" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  2309
      brackify_infix (1, R) fxy [
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2310
        pr_typ (INFX (1, X)) ty1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2311
        str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2312
        pr_typ (INFX (1, R)) ty2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2313
      ]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2314
  #> add_syntax_tyco "OCaml" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
27024
fcab2dd46872 various code streamlining
haftmann
parents: 27014
diff changeset
  2315
      brackify_infix (1, R) fxy [
24219
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2316
        pr_typ (INFX (1, X)) ty1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2317
        str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2318
        pr_typ (INFX (1, R)) ty2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2319
      ]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2320
  #> add_syntax_tyco "Haskell" "fun" (SOME (2, fn pr_typ => fn fxy => fn [ty1, ty2] =>
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2321
      brackify_infix (1, R) fxy [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2322
        pr_typ (INFX (1, X)) ty1,
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2323
        str "->",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2324
        pr_typ (INFX (1, R)) ty2
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2325
      ]))
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2326
  #> fold (add_reserved "SML") ML_Syntax.reserved_names
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2327
  #> fold (add_reserved "SML")
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2328
      ["o" (*dictionary projections use it already*), "Fail", "div", "mod" (*standard infixes*)]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2329
  #> fold (add_reserved "OCaml") [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2330
      "and", "as", "assert", "begin", "class",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2331
      "constraint", "do", "done", "downto", "else", "end", "exception",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2332
      "external", "false", "for", "fun", "function", "functor", "if",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2333
      "in", "include", "inherit", "initializer", "lazy", "let", "match", "method",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2334
      "module", "mutable", "new", "object", "of", "open", "or", "private", "rec",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2335
      "sig", "struct", "then", "to", "true", "try", "type", "val",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2336
      "virtual", "when", "while", "with"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2337
    ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2338
  #> fold (add_reserved "OCaml") ["failwith", "mod"]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2339
  #> fold (add_reserved "Haskell") [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2340
      "hiding", "deriving", "where", "case", "of", "infix", "infixl", "infixr",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2341
      "import", "default", "forall", "let", "in", "class", "qualified", "data",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2342
      "newtype", "instance", "if", "then", "else", "type", "as", "do", "module"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2343
    ]
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2344
  #> fold (add_reserved "Haskell") [
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2345
      "Prelude", "Main", "Bool", "Maybe", "Either", "Ordering", "Char", "String", "Int",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2346
      "Integer", "Float", "Double", "Rational", "IO", "Eq", "Ord", "Enum", "Bounded",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2347
      "Num", "Real", "Integral", "Fractional", "Floating", "RealFloat", "Monad", "Functor",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2348
      "AlreadyExists", "ArithException", "ArrayException", "AssertionFailed", "AsyncException",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2349
      "BlockedOnDeadMVar", "Deadlock", "Denormal", "DivideByZero", "DotNetException", "DynException",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2350
      "Dynamic", "EOF", "EQ", "EmptyRec", "ErrorCall", "ExitException", "ExitFailure",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2351
      "ExitSuccess", "False", "GT", "HeapOverflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2352
      "IOError", "IOException", "IllegalOperation",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2353
      "IndexOutOfBounds", "Just", "Key", "LT", "Left", "LossOfPrecision", "NoMethodError",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2354
      "NoSuchThing", "NonTermination", "Nothing", "Obj", "OtherError", "Overflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2355
      "PatternMatchFail", "PermissionDenied", "ProtocolError", "RecConError", "RecSelError",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2356
      "RecUpdError", "ResourceBusy", "ResourceExhausted", "Right", "StackOverflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2357
      "ThreadKilled", "True", "TyCon", "TypeRep", "UndefinedElement", "Underflow",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2358
      "UnsupportedOperation", "UserError", "abs", "absReal", "acos", "acosh", "all",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2359
      "and", "any", "appendFile", "asTypeOf", "asciiTab", "asin", "asinh", "atan",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2360
      "atan2", "atanh", "basicIORun", "blockIO", "boundedEnumFrom", "boundedEnumFromThen",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2361
      "boundedEnumFromThenTo", "boundedEnumFromTo", "boundedPred", "boundedSucc", "break",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2362
      "catch", "catchException", "ceiling", "compare", "concat", "concatMap", "const",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2363
      "cos", "cosh", "curry", "cycle", "decodeFloat", "denominator", "div", "divMod",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2364
      "doubleToRatio", "doubleToRational", "drop", "dropWhile", "either", "elem",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2365
      "emptyRec", "encodeFloat", "enumFrom", "enumFromThen", "enumFromThenTo",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2366
      "enumFromTo", "error", "even", "exp", "exponent", "fail", "filter", "flip",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2367
      "floatDigits", "floatProperFraction", "floatRadix", "floatRange", "floatToRational",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2368
      "floor", "fmap", "foldl", "foldl'", "foldl1", "foldr", "foldr1", "fromDouble",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2369
      "fromEnum", "fromEnum_0", "fromInt", "fromInteger", "fromIntegral", "fromObj",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2370
      "fromRational", "fst", "gcd", "getChar", "getContents", "getLine", "head",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2371
      "id", "inRange", "index", "init", "intToRatio", "interact", "ioError", "isAlpha",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2372
      "isAlphaNum", "isDenormalized", "isDigit", "isHexDigit", "isIEEE", "isInfinite",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2373
      "isLower", "isNaN", "isNegativeZero", "isOctDigit", "isSpace", "isUpper", "iterate", "iterate'",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2374
      "last", "lcm", "length", "lex", "lexDigits", "lexLitChar", "lexmatch", "lines", "log",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2375
      "logBase", "lookup", "loop", "map", "mapM", "mapM_", "max", "maxBound", "maximum",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2376
      "maybe", "min", "minBound", "minimum", "mod", "negate", "nonnull", "not", "notElem",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2377
      "null", "numerator", "numericEnumFrom", "numericEnumFromThen", "numericEnumFromThenTo",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2378
      "numericEnumFromTo", "odd", "or", "otherwise", "pi", "pred", 
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2379
      "print", "product", "properFraction", "protectEsc", "putChar", "putStr", "putStrLn",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2380
      "quot", "quotRem", "range", "rangeSize", "rationalToDouble", "rationalToFloat",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2381
      "rationalToRealFloat", "read", "readDec", "readField", "readFieldName", "readFile",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2382
      "readFloat", "readHex", "readIO", "readInt", "readList", "readLitChar", "readLn",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2383
      "readOct", "readParen", "readSigned", "reads", "readsPrec", "realFloatToRational",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2384
      "realToFrac", "recip", "reduce", "rem", "repeat", "replicate", "return", "reverse",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2385
      "round", "scaleFloat", "scanl", "scanl1", "scanr", "scanr1", "seq", "sequence",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2386
      "sequence_", "show", "showChar", "showException", "showField", "showList",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2387
      "showLitChar", "showParen", "showString", "shows", "showsPrec", "significand",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2388
      "signum", "signumReal", "sin", "sinh", "snd", "span", "splitAt", "sqrt", "subtract",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2389
      "succ", "sum", "tail", "take", "takeWhile", "takeWhile1", "tan", "tanh", "threadToIOResult",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2390
      "throw", "toEnum", "toInt", "toInteger", "toObj", "toRational", "truncate", "uncurry",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2391
      "undefined", "unlines", "unsafeCoerce", "unsafeIndex", "unsafeRangeSize", "until", "unwords",
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2392
      "unzip", "unzip3", "userError", "words", "writeFile", "zip", "zip3", "zipWith", "zipWith3"
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2393
    ] (*due to weird handling of ':', we can't do anything else than to import *all* prelude symbols*);
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2394
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2395
end; (*local*)
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2396
e558fe311376 new structure for code generator modules
haftmann
parents:
diff changeset
  2397
end; (*struct*)