src/Tools/Code/code_target.ML
author haftmann
Mon, 30 Aug 2010 16:21:47 +0200
changeset 39139 d1d4d808be26
parent 39138 c79c1e4e1111
child 39140 0a49a34e5d37
permissions -rw-r--r--
tuned
haftmann@37744
     1
(*  Title:      Tools/Code/code_target.ML
haftmann@24219
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     3
haftmann@39136
     4
Generic infrastructure for target language data.
haftmann@24219
     5
*)
haftmann@24219
     6
haftmann@24219
     7
signature CODE_TARGET =
haftmann@24219
     8
sig
haftmann@36467
     9
  val cert_tyco: theory -> string -> string
haftmann@36467
    10
  val read_tyco: theory -> string -> string
haftmann@36467
    11
haftmann@28054
    12
  type serializer
haftmann@34141
    13
  type literals = Code_Printer.literals
haftmann@37822
    14
  val add_target: string * { serializer: serializer, literals: literals,
haftmann@37822
    15
    check: { env_var: string, make_destination: Path.T -> Path.T,
haftmann@39092
    16
      make_command: string -> string -> string } } -> theory -> theory
haftmann@28663
    17
  val extend_target: string *
haftmann@28663
    18
      (string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program))
haftmann@28054
    19
    -> theory -> theory
haftmann@28054
    20
  val assert_target: theory -> string -> string
haftmann@28054
    21
haftmann@28054
    22
  type destination
haftmann@28054
    23
  type serialization
wenzelm@36969
    24
  val parse_args: 'a parser -> Token.T list -> 'a
haftmann@39136
    25
  val mk_serialization: (int -> Path.T option -> 'a -> unit)
haftmann@39136
    26
    -> (int -> 'a -> string * string option list)
haftmann@39136
    27
    -> 'a -> int -> serialization
wenzelm@36969
    28
  val serialize: theory -> string -> int option -> string option -> Token.T list
haftmann@28663
    29
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> serialization
haftmann@39017
    30
  val serialize_custom: theory -> string * serializer -> string option
haftmann@39135
    31
    -> Code_Thingol.naming -> Code_Thingol.program -> string list
haftmann@39135
    32
    -> string * string option list
haftmann@28090
    33
  val the_literals: theory -> string -> literals
haftmann@28054
    34
  val export: serialization -> unit
haftmann@28054
    35
  val file: Path.T -> serialization -> unit
haftmann@28054
    36
  val string: string list -> serialization -> string
haftmann@34071
    37
  val code_of: theory -> string -> int option -> string
haftmann@28690
    38
    -> string list -> (Code_Thingol.naming -> string list) -> string
haftmann@34071
    39
  val set_default_code_width: int -> theory -> theory
haftmann@30492
    40
  val shell_command: string (*theory name*) -> string (*export_code expr*) -> unit
haftmann@28054
    41
haftmann@28054
    42
  val allow_abort: string -> theory -> theory
haftmann@34141
    43
  type tyco_syntax = Code_Printer.tyco_syntax
haftmann@37849
    44
  type const_syntax = Code_Printer.const_syntax
haftmann@28690
    45
  val add_syntax_class: string -> class -> string option -> theory -> theory
haftmann@34071
    46
  val add_syntax_inst: string -> class * string -> unit option -> theory -> theory
haftmann@28054
    47
  val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory
haftmann@37849
    48
  val add_syntax_const: string -> string -> const_syntax option -> theory -> theory
haftmann@28054
    49
  val add_reserved: string -> string -> theory -> theory
haftmann@33968
    50
  val add_include: string -> string * (string * string list) option -> theory -> theory
haftmann@24219
    51
end;
haftmann@24219
    52
haftmann@28054
    53
structure Code_Target : CODE_TARGET =
haftmann@24219
    54
struct
haftmann@24219
    55
haftmann@28054
    56
open Basic_Code_Thingol;
haftmann@34141
    57
haftmann@34141
    58
type literals = Code_Printer.literals;
haftmann@34141
    59
type tyco_syntax = Code_Printer.tyco_syntax;
haftmann@37849
    60
type const_syntax = Code_Printer.const_syntax;
haftmann@34141
    61
haftmann@24219
    62
haftmann@24219
    63
(** basics **)
haftmann@24219
    64
haftmann@39136
    65
datatype destination = File of Path.T option | String of string list;
haftmann@28663
    66
type serialization = destination -> (string * string option list) option;
haftmann@27014
    67
haftmann@39136
    68
fun export f = (f (File NONE); ());
haftmann@39139
    69
fun file path f = (f (File (SOME path)); ());
haftmann@39139
    70
fun string stmt_names f = fst (the (f (String stmt_names)));
haftmann@27304
    71
haftmann@39139
    72
fun stmt_names_of_destination (String stmt_names) = stmt_names
haftmann@27304
    73
  | stmt_names_of_destination _ = [];
haftmann@27014
    74
haftmann@39139
    75
fun mk_serialization output _ pretty width (File path) = (output width path pretty; NONE)
haftmann@39139
    76
  | mk_serialization _ string pretty width (String _) = SOME (string width pretty);
haftmann@24219
    77
haftmann@24219
    78
haftmann@28054
    79
(** theory data **)
haftmann@27000
    80
haftmann@28054
    81
datatype name_syntax_table = NameSyntaxTable of {
haftmann@28690
    82
  class: string Symtab.table,
haftmann@30636
    83
  instance: unit Symreltab.table,
haftmann@34141
    84
  tyco: Code_Printer.tyco_syntax Symtab.table,
haftmann@37849
    85
  const: Code_Printer.const_syntax Symtab.table
haftmann@28054
    86
};
haftmann@27000
    87
haftmann@28663
    88
fun mk_name_syntax_table ((class, instance), (tyco, const)) =
haftmann@28663
    89
  NameSyntaxTable { class = class, instance = instance, tyco = tyco, const = const };
haftmann@28663
    90
fun map_name_syntax_table f (NameSyntaxTable { class, instance, tyco, const }) =
haftmann@28663
    91
  mk_name_syntax_table (f ((class, instance), (tyco, const)));
haftmann@39135
    92
fun merge_name_syntax_table
haftmann@39135
    93
  (NameSyntaxTable { class = class1, instance = instance1, tyco = tyco1, const = const1 },
haftmann@28663
    94
    NameSyntaxTable { class = class2, instance = instance2, tyco = tyco2, const = const2 }) =
haftmann@28054
    95
  mk_name_syntax_table (
haftmann@28054
    96
    (Symtab.join (K snd) (class1, class2),
haftmann@30636
    97
       Symreltab.join (K snd) (instance1, instance2)),
haftmann@28054
    98
    (Symtab.join (K snd) (tyco1, tyco2),
haftmann@28054
    99
       Symtab.join (K snd) (const1, const2))
haftmann@28054
   100
  );
haftmann@27103
   101
haftmann@28054
   102
type serializer =
haftmann@28054
   103
  string option                         (*module name*)
wenzelm@36969
   104
  -> Token.T list                       (*arguments*)
haftmann@28054
   105
  -> (string -> string)                 (*labelled_name*)
haftmann@28054
   106
  -> string list                        (*reserved symbols*)
haftmann@28054
   107
  -> (string * Pretty.T) list           (*includes*)
haftmann@28054
   108
  -> (string -> string option)          (*module aliasses*)
haftmann@28690
   109
  -> (string -> string option)          (*class syntax*)
haftmann@34141
   110
  -> (string -> Code_Printer.tyco_syntax option)
haftmann@37849
   111
  -> (string -> Code_Printer.activated_const_syntax option)
haftmann@28054
   112
  -> Code_Thingol.program
haftmann@39012
   113
  -> (string list * string list)        (*selected statements*)
haftmann@39136
   114
  -> int
haftmann@28054
   115
  -> serialization;
haftmann@27103
   116
haftmann@39135
   117
datatype description = Fundamental of { serializer: serializer,
haftmann@39136
   118
      literals: literals,
haftmann@37822
   119
      check: { env_var: string, make_destination: Path.T -> Path.T,
haftmann@39092
   120
        make_command: string -> string -> string } }
haftmann@39135
   121
  | Extension of string *
haftmann@39135
   122
      (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
haftmann@27103
   123
haftmann@28054
   124
datatype target = Target of {
haftmann@28054
   125
  serial: serial,
haftmann@37821
   126
  description: description,
haftmann@28054
   127
  reserved: string list,
haftmann@28926
   128
  includes: (Pretty.T * string list) Symtab.table,
haftmann@28054
   129
  name_syntax_table: name_syntax_table,
haftmann@28054
   130
  module_alias: string Symtab.table
haftmann@28054
   131
};
haftmann@27103
   132
haftmann@37821
   133
fun make_target ((serial, description), ((reserved, includes), (name_syntax_table, module_alias))) =
haftmann@37821
   134
  Target { serial = serial, description = description, reserved = reserved, 
haftmann@28054
   135
    includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias };
haftmann@37821
   136
fun map_target f ( Target { serial, description, reserved, includes, name_syntax_table, module_alias } ) =
haftmann@37821
   137
  make_target (f ((serial, description), ((reserved, includes), (name_syntax_table, module_alias))));
haftmann@37821
   138
fun merge_target strict target (Target { serial = serial1, description = description,
haftmann@28054
   139
  reserved = reserved1, includes = includes1,
haftmann@28054
   140
  name_syntax_table = name_syntax_table1, module_alias = module_alias1 },
haftmann@37821
   141
    Target { serial = serial2, description = _,
haftmann@28054
   142
      reserved = reserved2, includes = includes2,
haftmann@28054
   143
      name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) =
haftmann@28054
   144
  if serial1 = serial2 orelse not strict then
haftmann@37821
   145
    make_target ((serial1, description),
haftmann@38209
   146
      ((merge (op =) (reserved1, reserved2), Symtab.join (K snd) (includes1, includes2)),
haftmann@28054
   147
        (merge_name_syntax_table (name_syntax_table1, name_syntax_table2),
haftmann@38209
   148
          Symtab.join (K snd) (module_alias1, module_alias2))
haftmann@28054
   149
    ))
haftmann@28054
   150
  else
haftmann@37821
   151
    error ("Incompatible targets: " ^ quote target);
haftmann@27436
   152
haftmann@37821
   153
fun the_description (Target { description, ... }) = description;
haftmann@28054
   154
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@28054
   155
fun the_includes (Target { includes, ... }) = includes;
haftmann@28054
   156
fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
haftmann@28054
   157
fun the_module_alias (Target { module_alias , ... }) = module_alias;
haftmann@27436
   158
haftmann@34243
   159
structure Targets = Theory_Data
haftmann@34071
   160
(
haftmann@34071
   161
  type T = (target Symtab.table * string list) * int;
haftmann@34071
   162
  val empty = ((Symtab.empty, []), 80);
haftmann@34071
   163
  val extend = I;
haftmann@34071
   164
  fun merge (((target1, exc1), width1), ((target2, exc2), width2)) : T =
haftmann@34071
   165
    ((Symtab.join (merge_target true) (target1, target2),
haftmann@34071
   166
      Library.merge (op =) (exc1, exc2)), Int.max (width1, width2));
haftmann@34071
   167
);
haftmann@28054
   168
haftmann@34243
   169
val abort_allowed = snd o fst o Targets.get;
haftmann@34071
   170
haftmann@34243
   171
fun assert_target thy target = if Symtab.defined ((fst o fst) (Targets.get thy)) target
haftmann@33968
   172
  then target
haftmann@33968
   173
  else error ("Unknown code target language: " ^ quote target);
haftmann@28054
   174
haftmann@28054
   175
fun put_target (target, seri) thy =
haftmann@27437
   176
  let
haftmann@34243
   177
    val lookup_target = Symtab.lookup ((fst o fst) (Targets.get thy));
haftmann@28054
   178
    val _ = case seri
haftmann@37821
   179
     of Extension (super, _) => if is_some (lookup_target super) then ()
haftmann@28054
   180
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   181
      | _ => ();
haftmann@37821
   182
    val overwriting = case (Option.map the_description o lookup_target) target
haftmann@28663
   183
     of NONE => false
haftmann@37821
   184
      | SOME (Extension _) => true
haftmann@37821
   185
      | SOME (Fundamental _) => (case seri
haftmann@37821
   186
         of Extension _ => error ("Will not overwrite existing target " ^ quote target)
haftmann@28663
   187
          | _ => true);
haftmann@28663
   188
    val _ = if overwriting
haftmann@28054
   189
      then warning ("Overwriting existing target " ^ quote target)
haftmann@28663
   190
      else (); 
haftmann@27103
   191
  in
haftmann@28054
   192
    thy
haftmann@34243
   193
    |> (Targets.map o apfst o apfst o Symtab.update)
haftmann@31599
   194
          (target, make_target ((serial (), seri), (([], Symtab.empty),
haftmann@39135
   195
            (mk_name_syntax_table ((Symtab.empty, Symreltab.empty),
haftmann@39135
   196
              (Symtab.empty, Symtab.empty)), Symtab.empty))))
haftmann@27103
   197
  end;
haftmann@27000
   198
haftmann@37821
   199
fun add_target (target, seri) = put_target (target, Fundamental seri);
haftmann@28054
   200
fun extend_target (target, (super, modify)) =
haftmann@37821
   201
  put_target (target, Extension (super, modify));
haftmann@27000
   202
haftmann@28054
   203
fun map_target_data target f thy =
haftmann@27304
   204
  let
haftmann@28054
   205
    val _ = assert_target thy target;
haftmann@27000
   206
  in
haftmann@28054
   207
    thy
haftmann@34243
   208
    |> (Targets.map o apfst o apfst o Symtab.map_entry target o map_target) f
haftmann@27000
   209
  end;
haftmann@27000
   210
haftmann@28054
   211
fun map_reserved target =
haftmann@28054
   212
  map_target_data target o apsnd o apfst o apfst;
haftmann@28054
   213
fun map_includes target =
haftmann@28054
   214
  map_target_data target o apsnd o apfst o apsnd;
haftmann@28054
   215
fun map_name_syntax target =
haftmann@28054
   216
  map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table;
haftmann@28054
   217
fun map_module_alias target =
haftmann@28054
   218
  map_target_data target o apsnd o apsnd o apsnd;
haftmann@27000
   219
haftmann@34243
   220
fun set_default_code_width k = (Targets.map o apsnd) (K k);
haftmann@34071
   221
haftmann@27000
   222
haftmann@34021
   223
(** serializer usage **)
haftmann@34021
   224
haftmann@34021
   225
(* montage *)
haftmann@34021
   226
haftmann@37822
   227
fun the_fundamental thy =
haftmann@34021
   228
  let
haftmann@34243
   229
    val ((targets, _), _) = Targets.get thy;
haftmann@37822
   230
    fun fundamental target = case Symtab.lookup targets target
haftmann@37821
   231
     of SOME data => (case the_description data
haftmann@37822
   232
         of Fundamental data => data
haftmann@37822
   233
          | Extension (super, _) => fundamental super)
haftmann@34021
   234
      | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@37822
   235
  in fundamental end;
haftmann@37822
   236
haftmann@37822
   237
fun the_literals thy = #literals o the_fundamental thy;
haftmann@34021
   238
haftmann@34021
   239
local
haftmann@34021
   240
haftmann@34021
   241
fun activate_syntax lookup_name src_tab = Symtab.empty
haftmann@34021
   242
  |> fold_map (fn thing_identifier => fn tab => case lookup_name thing_identifier
haftmann@34021
   243
       of SOME name => (SOME name,
haftmann@34021
   244
            Symtab.update_new (name, the (Symtab.lookup src_tab thing_identifier)) tab)
haftmann@34021
   245
        | NONE => (NONE, tab)) (Symtab.keys src_tab)
haftmann@34021
   246
  |>> map_filter I;
haftmann@34021
   247
haftmann@34021
   248
fun activate_const_syntax thy literals src_tab naming = (Symtab.empty, naming)
haftmann@37854
   249
  |> fold_map (fn c => fn (tab, naming) =>
haftmann@37854
   250
      case Code_Thingol.lookup_const naming c
haftmann@34021
   251
       of SOME name => let
haftmann@34021
   252
              val (syn, naming') = Code_Printer.activate_const_syntax thy
haftmann@37854
   253
                literals c (the (Symtab.lookup src_tab c)) naming
haftmann@34021
   254
            in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
haftmann@34021
   255
        | NONE => (NONE, (tab, naming))) (Symtab.keys src_tab)
haftmann@34021
   256
  |>> map_filter I;
haftmann@34021
   257
haftmann@34021
   258
fun invoke_serializer thy abortable serializer literals reserved abs_includes 
haftmann@39136
   259
    module_alias class instance tyco const module_name args naming program2 (names1, presentation_names) width =
haftmann@34021
   260
  let
haftmann@34021
   261
    val (names_class, class') =
haftmann@34021
   262
      activate_syntax (Code_Thingol.lookup_class naming) class;
haftmann@34021
   263
    val names_inst = map_filter (Code_Thingol.lookup_instance naming)
haftmann@34021
   264
      (Symreltab.keys instance);
haftmann@34021
   265
    val (names_tyco, tyco') =
haftmann@34021
   266
      activate_syntax (Code_Thingol.lookup_tyco naming) tyco;
haftmann@34021
   267
    val (names_const, (const', _)) =
haftmann@34021
   268
      activate_const_syntax thy literals const naming;
haftmann@34021
   269
    val names_hidden = names_class @ names_inst @ names_tyco @ names_const;
haftmann@34021
   270
    val names2 = subtract (op =) names_hidden names1;
haftmann@34021
   271
    val program3 = Graph.subgraph (not o member (op =) names_hidden) program2;
haftmann@34021
   272
    val names_all = Graph.all_succs program3 names2;
haftmann@34021
   273
    val includes = abs_includes names_all;
haftmann@34021
   274
    val program4 = Graph.subgraph (member (op =) names_all) program3;
haftmann@34021
   275
    val empty_funs = filter_out (member (op =) abortable)
haftmann@34021
   276
      (Code_Thingol.empty_funs program3);
haftmann@34021
   277
    val _ = if null empty_funs then () else error ("No code equations for "
haftmann@34021
   278
      ^ commas (map (Sign.extern_const thy) empty_funs));
haftmann@34021
   279
  in
haftmann@39012
   280
    serializer module_name args (Code_Thingol.labelled_name thy program2) reserved includes
haftmann@39012
   281
      (if is_some module_name then K module_name else Symtab.lookup module_alias)
haftmann@39012
   282
      (Symtab.lookup class') (Symtab.lookup tyco') (Symtab.lookup const')
haftmann@39136
   283
      program4 (names1, presentation_names) width
haftmann@34021
   284
  end;
haftmann@34021
   285
haftmann@39012
   286
fun mount_serializer thy alt_serializer target some_width raw_module_name args naming program names destination =
haftmann@34021
   287
  let
haftmann@34243
   288
    val ((targets, abortable), default_width) = Targets.get thy;
haftmann@34021
   289
    fun collapse_hierarchy target =
haftmann@34021
   290
      let
haftmann@34021
   291
        val data = case Symtab.lookup targets target
haftmann@34021
   292
         of SOME data => data
haftmann@34021
   293
          | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@37821
   294
      in case the_description data
haftmann@37821
   295
       of Fundamental _ => (I, data)
haftmann@37821
   296
        | Extension (super, modify) => let
haftmann@34021
   297
            val (modify', data') = collapse_hierarchy super
haftmann@34021
   298
          in (modify' #> modify naming, merge_target false target (data', data)) end
haftmann@34021
   299
      end;
haftmann@34021
   300
    val (modify, data) = collapse_hierarchy target;
haftmann@37821
   301
    val serializer = the_default (case the_description data
haftmann@37821
   302
     of Fundamental seri => #serializer seri) alt_serializer;
haftmann@39012
   303
    val presentation_names = stmt_names_of_destination destination;
haftmann@39012
   304
    val module_name = if null presentation_names
haftmann@39012
   305
      then raw_module_name else SOME "Code";
haftmann@34021
   306
    val reserved = the_reserved data;
haftmann@34021
   307
    fun select_include names_all (name, (content, cs)) =
haftmann@34021
   308
      if null cs then SOME (name, content)
haftmann@34021
   309
      else if exists (fn c => case Code_Thingol.lookup_const naming c
haftmann@34021
   310
       of SOME name => member (op =) names_all name
haftmann@34021
   311
        | NONE => false) cs
haftmann@34021
   312
      then SOME (name, content) else NONE;
haftmann@34021
   313
    fun includes names_all = map_filter (select_include names_all)
haftmann@34021
   314
      ((Symtab.dest o the_includes) data);
haftmann@39012
   315
    val module_alias = the_module_alias data 
haftmann@34021
   316
    val { class, instance, tyco, const } = the_name_syntax data;
haftmann@34021
   317
    val literals = the_literals thy target;
haftmann@34071
   318
    val width = the_default default_width some_width;
haftmann@34021
   319
  in
haftmann@34021
   320
    invoke_serializer thy abortable serializer literals reserved
haftmann@39136
   321
      includes module_alias class instance tyco const module_name args
haftmann@39136
   322
        naming (modify program) (names, presentation_names) width destination
haftmann@34021
   323
  end;
haftmann@34021
   324
haftmann@34021
   325
in
haftmann@34021
   326
haftmann@34021
   327
fun serialize thy = mount_serializer thy NONE;
haftmann@34021
   328
haftmann@37825
   329
fun check thy names_cs naming program target strict args =
haftmann@37824
   330
  let
haftmann@37824
   331
    val module_name = "Code_Test";
haftmann@37824
   332
    val { env_var, make_destination, make_command } =
haftmann@37824
   333
      (#check o the_fundamental thy) target;
haftmann@37824
   334
    val env_param = getenv env_var;
haftmann@37824
   335
    fun ext_check env_param p =
haftmann@37824
   336
      let 
haftmann@37824
   337
        val destination = make_destination p;
haftmann@37824
   338
        val _ = file destination (serialize thy target (SOME 80)
haftmann@37824
   339
          (SOME module_name) args naming program names_cs);
haftmann@39092
   340
        val cmd = make_command env_param module_name;
haftmann@37824
   341
      in if bash ("cd " ^ File.shell_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
haftmann@37824
   342
        then error ("Code check failed for " ^ target ^ ": " ^ cmd)
haftmann@37824
   343
        else ()
haftmann@37824
   344
      end;
haftmann@37824
   345
  in if env_param = ""
haftmann@37825
   346
    then if strict
haftmann@37825
   347
      then error (env_var ^ " not set; cannot check code for " ^ target)
haftmann@37825
   348
      else warning (env_var ^ " not set; skipped checking code for " ^ target)
haftmann@37824
   349
    else Cache_IO.with_tmp_dir "Code_Test" (ext_check env_param)
haftmann@37824
   350
  end;
haftmann@37824
   351
haftmann@39017
   352
fun serialize_custom thy (target_name, seri) module_name naming program names =
haftmann@39017
   353
  mount_serializer thy (SOME seri) target_name NONE module_name [] naming program names (String [])
haftmann@34021
   354
  |> the;
haftmann@34021
   355
haftmann@34021
   356
end; (* local *)
haftmann@34021
   357
haftmann@34021
   358
haftmann@34021
   359
(* code presentation *)
haftmann@34021
   360
haftmann@34071
   361
fun code_of thy target some_width module_name cs names_stmt =
haftmann@34021
   362
  let
haftmann@36271
   363
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@34021
   364
  in
haftmann@34071
   365
    string (names_stmt naming)
haftmann@34071
   366
      (serialize thy target some_width (SOME module_name) [] naming program names_cs)
haftmann@34021
   367
  end;
haftmann@34021
   368
haftmann@34021
   369
haftmann@34021
   370
(* code generation *)
haftmann@34021
   371
haftmann@34021
   372
fun transitivly_non_empty_funs thy naming program =
haftmann@34021
   373
  let
haftmann@34021
   374
    val cs = subtract (op =) (abort_allowed thy) (Code_Thingol.empty_funs program);
haftmann@34021
   375
    val names = map_filter (Code_Thingol.lookup_const naming) cs;
haftmann@34021
   376
  in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end;
haftmann@34021
   377
haftmann@34021
   378
fun read_const_exprs thy cs =
haftmann@34021
   379
  let
haftmann@34021
   380
    val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs;
haftmann@36271
   381
    val (names2, (naming, program)) = Code_Thingol.consts_program thy true cs2;
haftmann@36271
   382
    val names3 = transitivly_non_empty_funs thy naming program;
haftmann@36271
   383
    val cs3 = map_filter (fn (c, name) =>
haftmann@36271
   384
      if member (op =) names3 name then SOME c else NONE) (cs2 ~~ names2);
haftmann@36271
   385
  in union (op =) cs3 cs1 end;
haftmann@34021
   386
haftmann@34021
   387
fun export_code thy cs seris =
haftmann@34021
   388
  let
haftmann@37824
   389
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@37821
   390
    fun mk_seri_dest dest = if dest = "" orelse dest = "-" then export
haftmann@37821
   391
      else file (Path.explode dest);
haftmann@34021
   392
    val _ = map (fn (((target, module), dest), args) =>
haftmann@37824
   393
      (mk_seri_dest dest (serialize thy target NONE module args naming program names_cs))) seris;
haftmann@34021
   394
  in () end;
haftmann@34021
   395
haftmann@34021
   396
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
haftmann@34021
   397
haftmann@37824
   398
fun check_code thy cs seris =
haftmann@37824
   399
  let
haftmann@37824
   400
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@37825
   401
    val _ = map (fn ((target, strict), args) => check thy names_cs naming program
haftmann@37825
   402
      target strict args) seris;
haftmann@37824
   403
  in () end;
haftmann@37824
   404
haftmann@37824
   405
fun check_code_cmd raw_cs seris thy = check_code thy (read_const_exprs thy raw_cs) seris;
haftmann@37824
   406
haftmann@34021
   407
haftmann@28054
   408
(** serializer configuration **)
haftmann@28054
   409
haftmann@27000
   410
(* data access *)
haftmann@24219
   411
haftmann@24992
   412
fun cert_class thy class =
haftmann@24992
   413
  let
haftmann@24992
   414
    val _ = AxClass.get_info thy class;
haftmann@24992
   415
  in class end;
haftmann@24992
   416
haftmann@27103
   417
fun read_class thy = cert_class thy o Sign.intern_class thy;
haftmann@24992
   418
haftmann@24992
   419
fun cert_tyco thy tyco =
haftmann@24992
   420
  let
haftmann@24992
   421
    val _ = if Sign.declared_tyname thy tyco then ()
haftmann@24992
   422
      else error ("No such type constructor: " ^ quote tyco);
haftmann@24992
   423
  in tyco end;
haftmann@24992
   424
haftmann@27103
   425
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
haftmann@24219
   426
haftmann@34071
   427
fun cert_inst thy (class, tyco) =
haftmann@34071
   428
  (cert_class thy class, cert_tyco thy tyco);
haftmann@34071
   429
haftmann@34071
   430
fun read_inst thy (raw_tyco, raw_class) =
haftmann@34071
   431
  (read_class thy raw_class, read_tyco thy raw_tyco);
haftmann@34071
   432
haftmann@37844
   433
fun gen_add_syntax (mapp, upd, del) prep_x prep_syn target raw_x some_raw_syn thy =
haftmann@24219
   434
  let
haftmann@37840
   435
    val x = prep_x thy raw_x;
haftmann@37844
   436
    val change = case some_raw_syn
haftmann@37844
   437
     of SOME raw_syn => upd (x, prep_syn thy x raw_syn)
haftmann@37844
   438
      | NONE => del x;
haftmann@37844
   439
  in (map_name_syntax target o mapp) change thy end;
haftmann@24219
   440
haftmann@34141
   441
fun gen_add_syntax_class prep_class =
haftmann@37844
   442
  gen_add_syntax (apfst o apfst, Symtab.update, Symtab.delete_safe) prep_class ((K o K) I);
haftmann@24219
   443
haftmann@34141
   444
fun gen_add_syntax_inst prep_inst =
haftmann@37844
   445
  gen_add_syntax (apfst o apsnd, Symreltab.update, Symreltab.delete_safe) prep_inst ((K o K) I);
haftmann@34071
   446
haftmann@34141
   447
fun gen_add_syntax_tyco prep_tyco =
haftmann@37844
   448
  gen_add_syntax (apsnd o apfst, Symtab.update, Symtab.delete_safe) prep_tyco
haftmann@37844
   449
    (fn thy => fn tyco => fn syn => if fst syn <> Sign.arity_number thy tyco
haftmann@24219
   450
      then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@37844
   451
      else syn);
haftmann@24219
   452
haftmann@37854
   453
fun gen_add_syntax_const prep_const =
haftmann@37844
   454
  gen_add_syntax (apsnd o apsnd, Symtab.update, Symtab.delete_safe) prep_const
haftmann@37854
   455
    (fn thy => fn c => fn syn =>
haftmann@37854
   456
      if Code_Printer.requires_args syn > Code.args_number thy c
haftmann@24423
   457
      then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@37854
   458
      else syn);
haftmann@24219
   459
haftmann@24219
   460
fun add_reserved target =
haftmann@24219
   461
  let
haftmann@24219
   462
    fun add sym syms = if member (op =) syms sym
haftmann@24219
   463
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@24219
   464
      else insert (op =) sym syms
haftmann@27103
   465
  in map_reserved target o add end;
haftmann@24992
   466
haftmann@28926
   467
fun gen_add_include read_const target args thy =
haftmann@24992
   468
  let
haftmann@28926
   469
    fun add (name, SOME (content, raw_cs)) incls =
haftmann@24992
   470
          let
haftmann@24992
   471
            val _ = if Symtab.defined incls name
haftmann@24992
   472
              then warning ("Overwriting existing include " ^ name)
haftmann@24992
   473
              else ();
haftmann@28926
   474
            val cs = map (read_const thy) raw_cs;
haftmann@34141
   475
          in Symtab.update (name, (Code_Printer.str content, cs)) incls end
haftmann@28926
   476
      | add (name, NONE) incls = Symtab.delete name incls;
haftmann@28926
   477
  in map_includes target (add args) thy end;
haftmann@28926
   478
haftmann@33968
   479
val add_include = gen_add_include (K I);
haftmann@31156
   480
val add_include_cmd = gen_add_include Code.read_const;
haftmann@24219
   481
haftmann@31013
   482
fun add_module_alias target (thyname, modlname) =
haftmann@31013
   483
  let
haftmann@31013
   484
    val xs = Long_Name.explode modlname;
haftmann@31036
   485
    val xs' = map (Name.desymbolize true) xs;
haftmann@31013
   486
  in if xs' = xs
haftmann@31013
   487
    then map_module_alias target (Symtab.update (thyname, modlname))
haftmann@31013
   488
    else error ("Invalid module name: " ^ quote modlname ^ "\n"
haftmann@31013
   489
      ^ "perhaps try " ^ quote (Long_Name.implode xs'))
haftmann@31013
   490
  end;
haftmann@24219
   491
haftmann@28663
   492
fun gen_allow_abort prep_const raw_c thy =
haftmann@24841
   493
  let
haftmann@28663
   494
    val c = prep_const thy raw_c;
haftmann@34243
   495
  in thy |> (Targets.map o apfst o apsnd) (insert (op =) c) end;
haftmann@24841
   496
haftmann@27000
   497
haftmann@27103
   498
(* concrete syntax *)
haftmann@27000
   499
haftmann@34021
   500
local
haftmann@34021
   501
haftmann@34141
   502
fun zip_list (x::xs) f g =
haftmann@34141
   503
  f
haftmann@37854
   504
  :|-- (fn y =>
haftmann@34141
   505
    fold_map (fn x => g |-- f >> pair x) xs
haftmann@37854
   506
    :|-- (fn xys => pair ((x, y) :: xys)));
haftmann@34141
   507
haftmann@37854
   508
fun process_multi_syntax parse_thing parse_syntax change =
haftmann@37854
   509
  (Parse.and_list1 parse_thing
haftmann@37854
   510
  :|-- (fn things => Scan.repeat1 (Parse.$$$ "(" |-- Parse.name --
haftmann@37854
   511
        (zip_list things parse_syntax (Parse.$$$ "and")) --| Parse.$$$ ")")))
haftmann@37854
   512
  >> (Toplevel.theory oo fold)
haftmann@37854
   513
    (fn (target, syns) => fold (fn (raw_x, syn) => change target raw_x syn) syns);
haftmann@24219
   514
haftmann@24219
   515
in
haftmann@24219
   516
haftmann@33968
   517
val add_syntax_class = gen_add_syntax_class cert_class;
haftmann@34071
   518
val add_syntax_inst = gen_add_syntax_inst cert_inst;
haftmann@24219
   519
val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
haftmann@37854
   520
val add_syntax_const = gen_add_syntax_const (K I);
haftmann@27103
   521
val allow_abort = gen_allow_abort (K I);
haftmann@28054
   522
val add_reserved = add_reserved;
haftmann@33968
   523
val add_include = add_include;
haftmann@24219
   524
haftmann@33968
   525
val add_syntax_class_cmd = gen_add_syntax_class read_class;
haftmann@34071
   526
val add_syntax_inst_cmd = gen_add_syntax_inst read_inst;
haftmann@24219
   527
val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
haftmann@37854
   528
val add_syntax_const_cmd = gen_add_syntax_const Code.read_const;
haftmann@31156
   529
val allow_abort_cmd = gen_allow_abort Code.read_const;
haftmann@24219
   530
haftmann@28054
   531
fun parse_args f args =
wenzelm@36969
   532
  case Scan.read Token.stopper f args
haftmann@28054
   533
   of SOME x => x
haftmann@28054
   534
    | NONE => error "Bad serializer arguments";
haftmann@28054
   535
haftmann@28054
   536
haftmann@27304
   537
(** Isar setup **)
haftmann@27103
   538
haftmann@37824
   539
val (inK, module_nameK, fileK, checkingK) = ("in", "module_name", "file", "checking");
haftmann@37824
   540
haftmann@37824
   541
val code_expr_argsP = Scan.optional (Parse.$$$ "(" |-- Args.parse --| Parse.$$$ ")") [];
haftmann@27103
   542
haftmann@30492
   543
val code_exprP =
haftmann@37824
   544
  Scan.repeat1 Parse.term_group :|-- (fn raw_cs =>
haftmann@37825
   545
    ((Parse.$$$ checkingK |-- Scan.repeat (Parse.name
haftmann@37825
   546
      -- ((Parse.$$$ "?" |-- Scan.succeed false) || Scan.succeed true) -- code_expr_argsP))
haftmann@37824
   547
      >> (fn seris => check_code_cmd raw_cs seris)
haftmann@37824
   548
    || Scan.repeat (Parse.$$$ inK |-- Parse.name
haftmann@37824
   549
       -- Scan.option (Parse.$$$ module_nameK |-- Parse.name)
haftmann@37824
   550
       -- Scan.optional (Parse.$$$ fileK |-- Parse.name) ""
haftmann@37824
   551
       -- code_expr_argsP) >> (fn seris => export_code_cmd raw_cs seris)));
haftmann@27103
   552
haftmann@37824
   553
val _ = List.app Keyword.keyword [inK, module_nameK, fileK, checkingK];
wenzelm@24867
   554
wenzelm@24867
   555
val _ =
wenzelm@36970
   556
  Outer_Syntax.command "code_class" "define code syntax for class" Keyword.thy_decl (
haftmann@37854
   557
    process_multi_syntax Parse.xname (Scan.option Parse.string)
haftmann@37854
   558
    add_syntax_class_cmd);
haftmann@24219
   559
wenzelm@24867
   560
val _ =
wenzelm@36970
   561
  Outer_Syntax.command "code_instance" "define code syntax for instance" Keyword.thy_decl (
haftmann@37854
   562
    process_multi_syntax (Parse.xname --| Parse.$$$ "::" -- Parse.xname)
wenzelm@36970
   563
      (Scan.option (Parse.minus >> K ()))
haftmann@37854
   564
    add_syntax_inst_cmd);
haftmann@24219
   565
wenzelm@24867
   566
val _ =
wenzelm@36970
   567
  Outer_Syntax.command "code_type" "define code syntax for type constructor" Keyword.thy_decl (
haftmann@37854
   568
    process_multi_syntax Parse.xname Code_Printer.parse_tyco_syntax
haftmann@37854
   569
    add_syntax_tyco_cmd);
haftmann@24219
   570
wenzelm@24867
   571
val _ =
wenzelm@36970
   572
  Outer_Syntax.command "code_const" "define code syntax for constant" Keyword.thy_decl (
haftmann@37854
   573
    process_multi_syntax Parse.term_group Code_Printer.parse_const_syntax
haftmann@37854
   574
    add_syntax_const_cmd);
haftmann@24219
   575
wenzelm@24867
   576
val _ =
wenzelm@36970
   577
  Outer_Syntax.command "code_reserved" "declare words as reserved for target language"
wenzelm@36970
   578
    Keyword.thy_decl (
wenzelm@36970
   579
    Parse.name -- Scan.repeat1 Parse.name
haftmann@24219
   580
    >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
haftmann@24841
   581
  );
haftmann@24219
   582
wenzelm@24867
   583
val _ =
wenzelm@36970
   584
  Outer_Syntax.command "code_include" "declare piece of code to be included in generated code"
wenzelm@36970
   585
    Keyword.thy_decl (
wenzelm@36970
   586
    Parse.name -- Parse.name -- (Parse.text :|-- (fn "-" => Scan.succeed NONE
wenzelm@36970
   587
      | s => Scan.optional (Parse.$$$ "attach" |-- Scan.repeat1 Parse.term) [] >> pair s >> SOME))
haftmann@28926
   588
    >> (fn ((target, name), content_consts) =>
haftmann@28926
   589
        (Toplevel.theory o add_include_cmd target) (name, content_consts))
haftmann@24992
   590
  );
haftmann@24992
   591
haftmann@24992
   592
val _ =
wenzelm@36970
   593
  Outer_Syntax.command "code_modulename" "alias module to other name" Keyword.thy_decl (
wenzelm@36970
   594
    Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
haftmann@27103
   595
    >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames)
haftmann@24841
   596
  );
haftmann@24219
   597
wenzelm@24867
   598
val _ =
wenzelm@36970
   599
  Outer_Syntax.command "code_abort" "permit constant to be implemented as program abort"
wenzelm@36970
   600
    Keyword.thy_decl (
wenzelm@36970
   601
    Scan.repeat1 Parse.term_group >> (Toplevel.theory o fold allow_abort_cmd)
haftmann@24841
   602
  );
haftmann@24219
   603
haftmann@27103
   604
val _ =
wenzelm@36970
   605
  Outer_Syntax.command "export_code" "generate executable code for constants"
wenzelm@36970
   606
    Keyword.diag (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
haftmann@30492
   607
haftmann@30492
   608
fun shell_command thyname cmd = Toplevel.program (fn _ =>
wenzelm@36970
   609
  (use_thy thyname; case Scan.read Token.stopper (Parse.!!! code_exprP)
wenzelm@36970
   610
    ((filter Token.is_proper o Outer_Syntax.scan Position.none) cmd)
wenzelm@38131
   611
   of SOME f => (writeln "Now generating code..."; f (Thy_Info.get_theory thyname))
haftmann@30492
   612
    | NONE => error ("Bad directive " ^ quote cmd)))
haftmann@33968
   613
  handle Runtime.TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
haftmann@27103
   614
haftmann@24219
   615
end; (*local*)
haftmann@24219
   616
haftmann@24219
   617
end; (*struct*)