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