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