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