src/Tools/Code/code_target.ML
author haftmann
Fri May 24 23:57:24 2013 +0200 (2013-05-24)
changeset 52137 7f7337447b1b
parent 52068 1abaea5d5a22
child 52138 e21426f244aa
permissions -rw-r--r--
use generic data for code symbols for unified "code_printing" syntax for custom serialisations
haftmann@37745
     1
(*  Title:      Tools/Code/code_target.ML
haftmann@24219
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@24219
     3
haftmann@38910
     4
Generic infrastructure for target language data.
haftmann@24219
     5
*)
haftmann@24219
     6
haftmann@24219
     7
signature CODE_TARGET =
haftmann@24219
     8
sig
haftmann@36471
     9
  val cert_tyco: theory -> string -> string
haftmann@36471
    10
  val read_tyco: theory -> string -> string
haftmann@39057
    11
  val read_const_exprs: theory -> string list -> string list
haftmann@36471
    12
haftmann@38933
    13
  val export_code_for: theory -> Path.T option -> string -> int option -> string -> Token.T list
haftmann@38918
    14
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
haftmann@38933
    15
  val produce_code_for: theory -> string -> int option -> string -> Token.T list
haftmann@48568
    16
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> (string * string) list * string option list
haftmann@38933
    17
  val present_code_for: theory -> string -> int option -> string -> Token.T list
haftmann@38929
    18
    -> Code_Thingol.naming -> Code_Thingol.program -> string list * string list -> string
haftmann@38918
    19
  val check_code_for: theory -> string -> bool -> Token.T list
haftmann@38918
    20
    -> Code_Thingol.naming -> Code_Thingol.program -> string list -> unit
haftmann@38918
    21
haftmann@38918
    22
  val export_code: theory -> string list
haftmann@38933
    23
    -> (((string * string) * Path.T option) * Token.T list) list -> unit
haftmann@38929
    24
  val produce_code: theory -> string list
haftmann@48568
    25
    -> string -> int option -> string -> Token.T list -> (string * string) list * string option list
haftmann@38929
    26
  val present_code: theory -> string list -> (Code_Thingol.naming -> string list)
haftmann@38933
    27
    -> string -> int option -> string -> Token.T list -> string
haftmann@38918
    28
  val check_code: theory -> string list
haftmann@38918
    29
    -> ((string * bool) * Token.T list) list -> unit
haftmann@38918
    30
haftmann@48568
    31
  val generatedN: string
haftmann@41344
    32
  val evaluator: theory -> string -> Code_Thingol.naming -> Code_Thingol.program
haftmann@41344
    33
    -> string list -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
haftmann@48568
    34
    -> (string * string) list * string
haftmann@41344
    35
haftmann@28054
    36
  type serializer
haftmann@34152
    37
  type literals = Code_Printer.literals
haftmann@37822
    38
  val add_target: string * { serializer: serializer, literals: literals,
wenzelm@41940
    39
    check: { env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string } }
wenzelm@41940
    40
    -> theory -> theory
haftmann@28663
    41
  val extend_target: string *
haftmann@28663
    42
      (string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program))
haftmann@28054
    43
    -> theory -> theory
haftmann@28054
    44
  val assert_target: theory -> string -> string
haftmann@38918
    45
  val the_literals: theory -> string -> literals
haftmann@28054
    46
  type serialization
wenzelm@36959
    47
  val parse_args: 'a parser -> Token.T list -> 'a
haftmann@38916
    48
  val serialization: (int -> Path.T option -> 'a -> unit)
haftmann@48568
    49
    -> (string list -> int -> 'a -> (string * string) list * (string -> string option))
haftmann@38925
    50
    -> 'a -> serialization
haftmann@38918
    51
  val set_default_code_width: int -> theory -> theory
haftmann@38917
    52
haftmann@52137
    53
  type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
haftmann@52137
    54
  type const_syntax = Code_Printer.const_syntax
haftmann@34152
    55
  type tyco_syntax = Code_Printer.tyco_syntax
haftmann@52137
    56
  val set_printings: (const_syntax, tyco_syntax, string, unit, unit, (string * string list)) symbol_attr_decl
haftmann@52137
    57
    -> theory -> theory
haftmann@52137
    58
  val add_const_syntax: string -> string -> const_syntax option -> theory -> theory
haftmann@52137
    59
  val add_tyco_syntax: string -> string -> tyco_syntax option -> theory -> theory
haftmann@38923
    60
  val add_class_syntax: string -> class -> string option -> theory -> theory
haftmann@38923
    61
  val add_instance_syntax: string -> class * string -> unit option -> theory -> theory
haftmann@28054
    62
  val add_reserved: string -> string -> theory -> theory
haftmann@33969
    63
  val add_include: string -> string * (string * string list) option -> theory -> theory
haftmann@52137
    64
  val allow_abort: string -> theory -> theory
haftmann@39646
    65
haftmann@39750
    66
  val codegen_tool: string (*theory name*) -> string (*export_code expr*) -> unit
wenzelm@43564
    67
wenzelm@43564
    68
  val setup: theory -> theory
haftmann@24219
    69
end;
haftmann@24219
    70
haftmann@28054
    71
structure Code_Target : CODE_TARGET =
haftmann@24219
    72
struct
haftmann@24219
    73
haftmann@28054
    74
open Basic_Code_Thingol;
haftmann@34152
    75
haftmann@34152
    76
type literals = Code_Printer.literals;
haftmann@52137
    77
type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl =
haftmann@52137
    78
  (string * (string * 'a option) list, string * (string * 'b option) list,
haftmann@52137
    79
    class * (string * 'c option) list, (class * class) * (string * 'd option) list,
haftmann@52137
    80
    (class * string) * (string * 'e option) list,
haftmann@52137
    81
    string * (string * 'f option) list) Code_Symbol.attr;
haftmann@52137
    82
haftmann@34152
    83
type tyco_syntax = Code_Printer.tyco_syntax;
haftmann@37876
    84
type const_syntax = Code_Printer.const_syntax;
haftmann@34152
    85
haftmann@24219
    86
haftmann@52137
    87
(** serializations and serializer **)
haftmann@52137
    88
haftmann@52137
    89
(* serialization: abstract nonsense to cover different destinies for generated code *)
haftmann@24219
    90
haftmann@38929
    91
datatype destination = Export of Path.T option | Produce | Present of string list;
haftmann@48568
    92
type serialization = int -> destination -> ((string * string) list * (string -> string option)) option;
haftmann@27014
    93
haftmann@39659
    94
fun serialization output _ content width (Export some_path) =
haftmann@39661
    95
      (output width some_path content; NONE)
haftmann@39661
    96
  | serialization _ string content width Produce =
haftmann@39661
    97
      string [] width content |> SOME
haftmann@39659
    98
  | serialization _ string content width (Present stmt_names) =
haftmann@39661
    99
     string stmt_names width content
haftmann@48568
   100
     |> (apfst o map o apsnd) (Pretty.output (SOME width) o Pretty.str)
haftmann@39661
   101
     |> SOME;
haftmann@39659
   102
haftmann@39661
   103
fun export some_path f = (f (Export some_path); ());
haftmann@39661
   104
fun produce f = the (f Produce);
haftmann@48568
   105
fun present stmt_names f = space_implode "\n\n" (map snd (fst (the (f (Present stmt_names)))));
haftmann@38917
   106
haftmann@24219
   107
haftmann@52137
   108
(* serializers: functions producing serializations *)
haftmann@28054
   109
haftmann@39142
   110
type serializer = Token.T list
haftmann@39142
   111
  -> {
haftmann@38926
   112
    labelled_name: string -> string,
haftmann@38926
   113
    reserved_syms: string list,
haftmann@38926
   114
    includes: (string * Pretty.T) list,
haftmann@38926
   115
    module_alias: string -> string option,
haftmann@38926
   116
    class_syntax: string -> string option,
haftmann@38926
   117
    tyco_syntax: string -> Code_Printer.tyco_syntax option,
haftmann@41342
   118
    const_syntax: string -> Code_Printer.activated_const_syntax option }
haftmann@41342
   119
  -> Code_Thingol.program
haftmann@28054
   120
  -> serialization;
haftmann@27000
   121
haftmann@52137
   122
datatype description =
haftmann@52137
   123
    Fundamental of { serializer: serializer,
haftmann@38910
   124
      literals: literals,
haftmann@37822
   125
      check: { env_var: string, make_destination: Path.T -> Path.T,
wenzelm@41940
   126
        make_command: string -> string } }
haftmann@38909
   127
  | Extension of string *
haftmann@38909
   128
      (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
haftmann@28054
   129
haftmann@52137
   130
haftmann@52137
   131
(** theory data **)
haftmann@52137
   132
haftmann@28054
   133
datatype target = Target of {
haftmann@28054
   134
  serial: serial,
haftmann@37821
   135
  description: description,
haftmann@28054
   136
  reserved: string list,
haftmann@38925
   137
  module_alias: string Symtab.table,
haftmann@52137
   138
  printings: (Code_Printer.const_syntax, Code_Printer.tyco_syntax, string, unit, unit,
haftmann@52137
   139
    (Pretty.T * string list)) Code_Symbol.data
haftmann@28054
   140
};
haftmann@27103
   141
haftmann@52137
   142
fun make_target ((serial, description), ((reserved, module_alias), printings)) =
wenzelm@43564
   143
  Target { serial = serial, description = description, reserved = reserved,
haftmann@52137
   144
    module_alias = module_alias, printings = printings };
haftmann@52137
   145
fun map_target f ( Target { serial, description, reserved, module_alias, printings } ) =
haftmann@52137
   146
  make_target (f ((serial, description), ((reserved, module_alias), printings)));
haftmann@37821
   147
fun merge_target strict target (Target { serial = serial1, description = description,
haftmann@52137
   148
  reserved = reserved1, module_alias = module_alias1, printings = printings1 },
haftmann@37821
   149
    Target { serial = serial2, description = _,
haftmann@52137
   150
      reserved = reserved2, module_alias = module_alias2, printings = printings2 }) =
haftmann@28054
   151
  if serial1 = serial2 orelse not strict then
haftmann@37821
   152
    make_target ((serial1, description),
wenzelm@50768
   153
      ((merge (op =) (reserved1, reserved2),
haftmann@52137
   154
         Symtab.join (K snd) (module_alias1, module_alias2)),
haftmann@52137
   155
        (Code_Symbol.merge_data (printings1, printings2))
haftmann@28054
   156
    ))
haftmann@28054
   157
  else
haftmann@37821
   158
    error ("Incompatible targets: " ^ quote target);
haftmann@27103
   159
haftmann@37821
   160
fun the_description (Target { description, ... }) = description;
haftmann@28054
   161
fun the_reserved (Target { reserved, ... }) = reserved;
haftmann@28054
   162
fun the_module_alias (Target { module_alias , ... }) = module_alias;
haftmann@52137
   163
fun the_printings (Target { printings, ... }) = printings;
haftmann@27437
   164
haftmann@34248
   165
structure Targets = Theory_Data
haftmann@34071
   166
(
haftmann@34071
   167
  type T = (target Symtab.table * string list) * int;
haftmann@34071
   168
  val empty = ((Symtab.empty, []), 80);
haftmann@34071
   169
  val extend = I;
haftmann@34071
   170
  fun merge (((target1, exc1), width1), ((target2, exc2), width2)) : T =
haftmann@34071
   171
    ((Symtab.join (merge_target true) (target1, target2),
haftmann@34071
   172
      Library.merge (op =) (exc1, exc2)), Int.max (width1, width2));
haftmann@34071
   173
);
haftmann@27436
   174
haftmann@34248
   175
val abort_allowed = snd o fst o Targets.get;
haftmann@34071
   176
haftmann@34248
   177
fun assert_target thy target = if Symtab.defined ((fst o fst) (Targets.get thy)) target
haftmann@33969
   178
  then target
haftmann@33969
   179
  else error ("Unknown code target language: " ^ quote target);
haftmann@28054
   180
haftmann@28054
   181
fun put_target (target, seri) thy =
haftmann@27304
   182
  let
haftmann@34248
   183
    val lookup_target = Symtab.lookup ((fst o fst) (Targets.get thy));
haftmann@28054
   184
    val _ = case seri
haftmann@37821
   185
     of Extension (super, _) => if is_some (lookup_target super) then ()
haftmann@28054
   186
          else error ("Unknown code target language: " ^ quote super)
haftmann@28054
   187
      | _ => ();
haftmann@37821
   188
    val overwriting = case (Option.map the_description o lookup_target) target
haftmann@28663
   189
     of NONE => false
haftmann@37821
   190
      | SOME (Extension _) => true
haftmann@37821
   191
      | SOME (Fundamental _) => (case seri
haftmann@37821
   192
         of Extension _ => error ("Will not overwrite existing target " ^ quote target)
haftmann@28663
   193
          | _ => true);
haftmann@28663
   194
    val _ = if overwriting
haftmann@28054
   195
      then warning ("Overwriting existing target " ^ quote target)
wenzelm@43564
   196
      else ();
haftmann@28054
   197
  in
haftmann@28054
   198
    thy
haftmann@34248
   199
    |> (Targets.map o apfst o apfst o Symtab.update)
haftmann@31599
   200
          (target, make_target ((serial (), seri), (([], Symtab.empty),
haftmann@52137
   201
            Code_Symbol.empty_data)))
haftmann@28054
   202
  end;
haftmann@27436
   203
haftmann@37821
   204
fun add_target (target, seri) = put_target (target, Fundamental seri);
haftmann@28054
   205
fun extend_target (target, (super, modify)) =
haftmann@37821
   206
  put_target (target, Extension (super, modify));
haftmann@27436
   207
haftmann@28054
   208
fun map_target_data target f thy =
haftmann@27436
   209
  let
haftmann@28054
   210
    val _ = assert_target thy target;
haftmann@28054
   211
  in
haftmann@28054
   212
    thy
haftmann@34248
   213
    |> (Targets.map o apfst o apfst o Symtab.map_entry target o map_target) f
haftmann@28054
   214
  end;
haftmann@27304
   215
haftmann@28054
   216
fun map_reserved target =
haftmann@28054
   217
  map_target_data target o apsnd o apfst o apfst;
haftmann@52137
   218
fun map_module_alias target =
haftmann@28054
   219
  map_target_data target o apsnd o apfst o apsnd;
haftmann@52137
   220
fun map_printings target =
haftmann@52137
   221
  map_target_data target o apsnd o apsnd;
haftmann@27000
   222
haftmann@34248
   223
fun set_default_code_width k = (Targets.map o apsnd) (K k);
haftmann@34071
   224
haftmann@27000
   225
haftmann@34021
   226
(** serializer usage **)
haftmann@34021
   227
haftmann@34021
   228
(* montage *)
haftmann@34021
   229
haftmann@37822
   230
fun the_fundamental thy =
haftmann@34021
   231
  let
haftmann@34248
   232
    val ((targets, _), _) = Targets.get thy;
haftmann@37822
   233
    fun fundamental target = case Symtab.lookup targets target
haftmann@37821
   234
     of SOME data => (case the_description data
haftmann@37822
   235
         of Fundamental data => data
haftmann@37822
   236
          | Extension (super, _) => fundamental super)
haftmann@34021
   237
      | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@37822
   238
  in fundamental end;
haftmann@37822
   239
haftmann@37822
   240
fun the_literals thy = #literals o the_fundamental thy;
haftmann@34021
   241
haftmann@39817
   242
fun collapse_hierarchy thy =
haftmann@38927
   243
  let
haftmann@39817
   244
    val ((targets, _), _) = Targets.get thy;
wenzelm@43564
   245
    fun collapse target =
haftmann@38927
   246
      let
haftmann@38927
   247
        val data = case Symtab.lookup targets target
haftmann@38927
   248
         of SOME data => data
haftmann@38927
   249
          | NONE => error ("Unknown code target language: " ^ quote target);
haftmann@38927
   250
      in case the_description data
haftmann@39484
   251
       of Fundamental _ => (K I, data)
haftmann@38927
   252
        | Extension (super, modify) => let
haftmann@39817
   253
            val (modify', data') = collapse super
haftmann@39484
   254
          in (fn naming => modify' naming #> modify naming, merge_target false target (data', data)) end
haftmann@38927
   255
      end;
haftmann@39817
   256
  in collapse end;
haftmann@39817
   257
haftmann@39817
   258
local
haftmann@39817
   259
haftmann@39817
   260
fun activate_target thy target =
haftmann@39817
   261
  let
haftmann@47576
   262
    val ((_, abortable), default_width) = Targets.get thy;
haftmann@39817
   263
    val (modify, data) = collapse_hierarchy thy target;
haftmann@39484
   264
  in (default_width, abortable, data, modify) end;
haftmann@38927
   265
haftmann@52137
   266
fun activate_const_syntax thy literals cs_data naming =
haftmann@52137
   267
  (Symtab.empty, naming)
haftmann@52137
   268
  |> fold_map (fn (c, data) => fn (tab, naming) =>
haftmann@37881
   269
      case Code_Thingol.lookup_const naming c
haftmann@34021
   270
       of SOME name => let
haftmann@52137
   271
              val (syn, naming') =
haftmann@52137
   272
                Code_Printer.activate_const_syntax thy literals c data naming
haftmann@34021
   273
            in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
haftmann@52137
   274
        | NONE => (NONE, (tab, naming))) cs_data
haftmann@52137
   275
  |>> map_filter I;
haftmann@52137
   276
haftmann@52137
   277
fun activate_syntax lookup_name things =
haftmann@52137
   278
  Symtab.empty
haftmann@52137
   279
  |> fold_map (fn (thing_identifier, data) => fn tab => case lookup_name thing_identifier
haftmann@52137
   280
       of SOME name => (SOME name, Symtab.update_new (name, data) tab)
haftmann@52137
   281
        | NONE => (NONE, tab)) things
haftmann@34021
   282
  |>> map_filter I;
haftmann@34021
   283
haftmann@52137
   284
fun activate_symbol_syntax thy literals naming printings =
haftmann@34021
   285
  let
haftmann@52137
   286
    val (names_const, (const_syntax, _)) =
haftmann@52137
   287
      activate_const_syntax thy literals (Code_Symbol.dest_constant_data printings) naming;
haftmann@52137
   288
    val (names_tyco, tyco_syntax) =
haftmann@52137
   289
      activate_syntax (Code_Thingol.lookup_tyco naming) (Code_Symbol.dest_type_constructor_data printings);
haftmann@52137
   290
    val (names_class, class_syntax) =
haftmann@52137
   291
      activate_syntax (Code_Thingol.lookup_class naming) (Code_Symbol.dest_type_class_data printings);
haftmann@52137
   292
    val names_inst = map_filter (Code_Thingol.lookup_instance naming o fst)
haftmann@52137
   293
      (Code_Symbol.dest_class_instance_data printings);
haftmann@38927
   294
  in
haftmann@52137
   295
    (names_const @ names_tyco @ names_class @ names_inst,
haftmann@52137
   296
      (const_syntax, tyco_syntax, class_syntax))
haftmann@38927
   297
  end;
haftmann@38927
   298
haftmann@38927
   299
fun project_program thy abortable names_hidden names1 program2 =
haftmann@38927
   300
  let
wenzelm@42361
   301
    val ctxt = Proof_Context.init_global thy;
haftmann@34021
   302
    val names2 = subtract (op =) names_hidden names1;
wenzelm@46614
   303
    val program3 = Graph.restrict (not o member (op =) names_hidden) program2;
haftmann@38936
   304
    val names4 = Graph.all_succs program3 names2;
haftmann@34021
   305
    val empty_funs = filter_out (member (op =) abortable)
haftmann@34021
   306
      (Code_Thingol.empty_funs program3);
wenzelm@42359
   307
    val _ =
wenzelm@42359
   308
      if null empty_funs then ()
wenzelm@42359
   309
      else error ("No code equations for " ^
wenzelm@42361
   310
        commas (map (Proof_Context.extern_const ctxt) empty_funs));
wenzelm@46614
   311
    val program4 = Graph.restrict (member (op =) names4) program3;
haftmann@38936
   312
  in (names4, program4) end;
haftmann@38927
   313
haftmann@52137
   314
fun prepare_serializer thy abortable (serializer : serializer) literals reserved module_alias
haftmann@52137
   315
    printings module_name args naming proto_program names =
haftmann@38927
   316
  let
haftmann@52137
   317
    val (names_hidden, (const_syntax, tyco_syntax, class_syntax)) =
haftmann@52137
   318
      activate_symbol_syntax thy literals naming printings;
haftmann@38927
   319
    val (names_all, program) = project_program thy abortable names_hidden names proto_program;
haftmann@39484
   320
    fun select_include (name, (content, cs)) =
haftmann@39484
   321
      if null cs orelse exists (fn c => case Code_Thingol.lookup_const naming c
haftmann@39484
   322
       of SOME name => member (op =) names_all name
haftmann@39484
   323
        | NONE => false) cs
haftmann@39484
   324
      then SOME (name, content) else NONE;
haftmann@52137
   325
    val includes = map_filter select_include (Code_Symbol.dest_module_data printings);
haftmann@34021
   326
  in
haftmann@41342
   327
    (serializer args {
haftmann@38927
   328
      labelled_name = Code_Thingol.labelled_name thy proto_program,
haftmann@38926
   329
      reserved_syms = reserved,
haftmann@38926
   330
      includes = includes,
haftmann@38933
   331
      module_alias = if module_name = "" then Symtab.lookup module_alias else K (SOME module_name),
haftmann@52137
   332
      const_syntax = Symtab.lookup const_syntax,
haftmann@38927
   333
      tyco_syntax = Symtab.lookup tyco_syntax,
haftmann@52137
   334
      class_syntax = Symtab.lookup class_syntax },
haftmann@41342
   335
      program)
haftmann@34021
   336
  end;
haftmann@34021
   337
haftmann@41342
   338
fun mount_serializer thy target some_width module_name args naming program names =
haftmann@34021
   339
  let
haftmann@39484
   340
    val (default_width, abortable, data, modify) = activate_target thy target;
haftmann@38921
   341
    val serializer = case the_description data
haftmann@38927
   342
     of Fundamental seri => #serializer seri;
haftmann@34021
   343
    val reserved = the_reserved data;
wenzelm@43564
   344
    val module_alias = the_module_alias data
haftmann@34021
   345
    val literals = the_literals thy target;
haftmann@41342
   346
    val (prepared_serializer, prepared_program) = prepare_serializer thy
haftmann@52137
   347
      abortable serializer literals reserved module_alias (the_printings data)
haftmann@52137
   348
        module_name args naming (modify naming program) names
haftmann@34071
   349
    val width = the_default default_width some_width;
haftmann@41344
   350
  in (fn program => prepared_serializer program width, prepared_program) end;
haftmann@41344
   351
haftmann@41344
   352
fun invoke_serializer thy target some_width module_name args naming program names =
haftmann@41344
   353
  let
haftmann@41344
   354
    val (mounted_serializer, prepared_program) = mount_serializer thy
haftmann@41344
   355
      target some_width module_name args naming program names;
haftmann@41344
   356
  in mounted_serializer prepared_program end;
haftmann@34021
   357
haftmann@38933
   358
fun assert_module_name "" = error ("Empty module name not allowed.")
haftmann@38933
   359
  | assert_module_name module_name = module_name;
haftmann@38933
   360
haftmann@48426
   361
fun using_master_directory thy =
haftmann@48426
   362
  Option.map (Path.append (File.pwd ()) o Path.append (Thy_Load.master_directory thy));
haftmann@48371
   363
haftmann@34021
   364
in
haftmann@34021
   365
haftmann@48568
   366
val generatedN = "Generated_Code";
haftmann@48568
   367
haftmann@39484
   368
fun export_code_for thy some_path target some_width module_name args =
haftmann@48371
   369
  export (using_master_directory thy some_path)
haftmann@48371
   370
  ooo invoke_serializer thy target some_width module_name args;
haftmann@38918
   371
haftmann@39484
   372
fun produce_code_for thy target some_width module_name args =
haftmann@39102
   373
  let
haftmann@41344
   374
    val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
haftmann@39484
   375
  in fn naming => fn program => fn names =>
haftmann@39661
   376
    produce (serializer naming program names) |> apsnd (fn deresolve => map deresolve names)
haftmann@39484
   377
  end;
haftmann@38929
   378
haftmann@39484
   379
fun present_code_for thy target some_width module_name args =
haftmann@39484
   380
  let
haftmann@41344
   381
    val serializer = invoke_serializer thy target some_width (assert_module_name module_name) args;
haftmann@39484
   382
  in fn naming => fn program => fn (names, selects) =>
haftmann@39661
   383
    present selects (serializer naming program names)
haftmann@39484
   384
  end;
haftmann@38918
   385
haftmann@38918
   386
fun check_code_for thy target strict args naming program names_cs =
haftmann@37824
   387
  let
haftmann@37824
   388
    val { env_var, make_destination, make_command } =
haftmann@37824
   389
      (#check o the_fundamental thy) target;
wenzelm@41939
   390
    fun ext_check p =
wenzelm@43564
   391
      let
haftmann@37824
   392
        val destination = make_destination p;
haftmann@41344
   393
        val _ = export (SOME destination) (invoke_serializer thy target (SOME 80)
haftmann@48568
   394
          generatedN args naming program names_cs);
haftmann@48568
   395
        val cmd = make_command generatedN;
wenzelm@43850
   396
      in
wenzelm@43850
   397
        if Isabelle_System.bash ("cd " ^ File.shell_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
haftmann@37824
   398
        then error ("Code check failed for " ^ target ^ ": " ^ cmd)
haftmann@37824
   399
        else ()
haftmann@37824
   400
      end;
wenzelm@43850
   401
  in
wenzelm@43850
   402
    if getenv env_var = ""
haftmann@37825
   403
    then if strict
haftmann@37825
   404
      then error (env_var ^ " not set; cannot check code for " ^ target)
haftmann@37825
   405
      else warning (env_var ^ " not set; skipped checking code for " ^ target)
wenzelm@41939
   406
    else Isabelle_System.with_tmp_dir "Code_Test" ext_check
haftmann@37824
   407
  end;
haftmann@37824
   408
haftmann@41364
   409
fun evaluation mounted_serializer prepared_program consts ((vs, ty), t) =
haftmann@41344
   410
  let
haftmann@41344
   411
    val _ = if Code_Thingol.contains_dict_var t then
haftmann@41344
   412
      error "Term to be evaluated contains free dictionaries" else ();
wenzelm@43324
   413
    val v' = singleton (Name.variant_list (map fst vs)) "a";
haftmann@41344
   414
    val vs' = (v', []) :: vs;
haftmann@41344
   415
    val ty' = Code_Thingol.fun_tyco `%% [ITyVar v', ty];
haftmann@41344
   416
    val value_name = "Value.value.value"
haftmann@41344
   417
    val program = prepared_program
haftmann@41344
   418
      |> Graph.new_node (value_name,
haftmann@41344
   419
          Code_Thingol.Fun (Term.dummy_patternN, (((vs', ty'), [(([IVar NONE], t), (NONE, true))]), NONE)))
haftmann@41364
   420
      |> fold (curry (perhaps o try o Graph.add_edge) value_name) consts;
haftmann@41344
   421
    val (program_code, deresolve) = produce (mounted_serializer program);
haftmann@41344
   422
    val value_name' = the (deresolve value_name);
haftmann@41344
   423
  in (program_code, value_name') end;
haftmann@41344
   424
haftmann@41364
   425
fun evaluator thy target naming program consts =
haftmann@41344
   426
  let
haftmann@41344
   427
    val (mounted_serializer, prepared_program) = mount_serializer thy
haftmann@48568
   428
      target NONE generatedN [] naming program consts;
haftmann@41364
   429
  in evaluation mounted_serializer prepared_program consts end;
haftmann@41344
   430
haftmann@34021
   431
end; (* local *)
haftmann@34021
   432
haftmann@34021
   433
haftmann@34021
   434
(* code generation *)
haftmann@34021
   435
haftmann@34021
   436
fun transitivly_non_empty_funs thy naming program =
haftmann@34021
   437
  let
haftmann@34021
   438
    val cs = subtract (op =) (abort_allowed thy) (Code_Thingol.empty_funs program);
haftmann@34021
   439
    val names = map_filter (Code_Thingol.lookup_const naming) cs;
haftmann@34021
   440
  in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end;
haftmann@34021
   441
haftmann@34021
   442
fun read_const_exprs thy cs =
haftmann@34021
   443
  let
haftmann@34021
   444
    val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs;
haftmann@36271
   445
    val (names2, (naming, program)) = Code_Thingol.consts_program thy true cs2;
haftmann@36271
   446
    val names3 = transitivly_non_empty_funs thy naming program;
haftmann@36271
   447
    val cs3 = map_filter (fn (c, name) =>
haftmann@36271
   448
      if member (op =) names3 name then SOME c else NONE) (cs2 ~~ names2);
haftmann@36271
   449
  in union (op =) cs3 cs1 end;
haftmann@34021
   450
haftmann@38918
   451
fun prep_destination "" = NONE
haftmann@38918
   452
  | prep_destination "-" = NONE
haftmann@38918
   453
  | prep_destination s = SOME (Path.explode s);
haftmann@38918
   454
haftmann@34021
   455
fun export_code thy cs seris =
haftmann@34021
   456
  let
haftmann@37824
   457
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@38918
   458
    val _ = map (fn (((target, module_name), some_path), args) =>
haftmann@38918
   459
      export_code_for thy some_path target NONE module_name args naming program names_cs) seris;
haftmann@34021
   460
  in () end;
haftmann@34021
   461
haftmann@38918
   462
fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs)
haftmann@38918
   463
  ((map o apfst o apsnd) prep_destination seris);
haftmann@38918
   464
haftmann@38929
   465
fun produce_code thy cs target some_width some_module_name args =
haftmann@38918
   466
  let
haftmann@38918
   467
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@38929
   468
  in produce_code_for thy target some_width some_module_name args naming program names_cs end;
haftmann@38929
   469
haftmann@38929
   470
fun present_code thy cs names_stmt target some_width some_module_name args =
haftmann@38929
   471
  let
haftmann@38929
   472
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@38929
   473
  in present_code_for thy target some_width some_module_name args naming program (names_cs, names_stmt naming) end;
haftmann@34021
   474
haftmann@37824
   475
fun check_code thy cs seris =
haftmann@37824
   476
  let
haftmann@37824
   477
    val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
haftmann@38918
   478
    val _ = map (fn ((target, strict), args) =>
haftmann@38918
   479
      check_code_for thy target strict args naming program names_cs) seris;
haftmann@37824
   480
  in () end;
haftmann@37824
   481
haftmann@37824
   482
fun check_code_cmd raw_cs seris thy = check_code thy (read_const_exprs thy raw_cs) seris;
haftmann@37824
   483
haftmann@39480
   484
local
haftmann@39480
   485
haftmann@39480
   486
val parse_const_terms = Scan.repeat1 Args.term
haftmann@39480
   487
  >> (fn ts => fn thy => map (Code.check_const thy) ts);
haftmann@39480
   488
haftmann@39480
   489
fun parse_names category parse internalize lookup =
haftmann@39480
   490
  Scan.lift (Args.parens (Args.$$$ category)) |-- Scan.repeat1 parse
haftmann@39480
   491
  >> (fn xs => fn thy => fn naming => map_filter (lookup naming o internalize thy) xs);
wenzelm@43564
   492
haftmann@39480
   493
val parse_consts = parse_names "consts" Args.term
haftmann@39480
   494
  Code.check_const Code_Thingol.lookup_const ;
haftmann@39480
   495
haftmann@39480
   496
val parse_types = parse_names "types" (Scan.lift Args.name)
haftmann@39480
   497
  Sign.intern_type Code_Thingol.lookup_tyco;
haftmann@39480
   498
haftmann@39480
   499
val parse_classes = parse_names "classes" (Scan.lift Args.name)
haftmann@39480
   500
  Sign.intern_class Code_Thingol.lookup_class;
haftmann@39480
   501
haftmann@39480
   502
val parse_instances = parse_names "instances" (Scan.lift (Args.name --| Args.$$$ "::" -- Args.name))
haftmann@39480
   503
  (fn thy => fn (raw_tyco, raw_class) => (Sign.intern_class thy raw_class, Sign.intern_type thy raw_tyco))
haftmann@39480
   504
    Code_Thingol.lookup_instance;
haftmann@39480
   505
haftmann@39480
   506
in
haftmann@39480
   507
wenzelm@43564
   508
val antiq_setup =
wenzelm@43564
   509
  Thy_Output.antiquotation @{binding code_stmts}
wenzelm@43564
   510
    (parse_const_terms --
wenzelm@43564
   511
      Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances)
wenzelm@43564
   512
      -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int)))
wenzelm@43564
   513
    (fn {context = ctxt, ...} => fn ((mk_cs, mk_stmtss), (target, some_width)) =>
wenzelm@43564
   514
      let val thy = Proof_Context.theory_of ctxt in
wenzelm@43564
   515
        present_code thy (mk_cs thy)
wenzelm@43564
   516
          (fn naming => maps (fn f => f thy naming) mk_stmtss)
wenzelm@43564
   517
          target some_width "Example" []
wenzelm@43564
   518
      end);
haftmann@39480
   519
haftmann@39480
   520
end;
haftmann@39480
   521
haftmann@34021
   522
haftmann@28054
   523
(** serializer configuration **)
haftmann@27000
   524
haftmann@52137
   525
(* checking and parsing *)
haftmann@24219
   526
haftmann@52137
   527
fun cert_const thy const =
haftmann@24992
   528
  let
haftmann@52137
   529
    val _ = if Sign.declared_const thy const then ()
haftmann@52137
   530
      else error ("No such constant: " ^ quote const);
haftmann@52137
   531
  in const end;
haftmann@24992
   532
haftmann@24992
   533
fun cert_tyco thy tyco =
haftmann@24992
   534
  let
haftmann@24992
   535
    val _ = if Sign.declared_tyname thy tyco then ()
haftmann@24992
   536
      else error ("No such type constructor: " ^ quote tyco);
haftmann@24992
   537
  in tyco end;
haftmann@24992
   538
haftmann@27103
   539
fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
haftmann@24219
   540
haftmann@52137
   541
fun cert_class thy class =
haftmann@52137
   542
  let
haftmann@52137
   543
    val _ = Axclass.get_info thy class;
haftmann@52137
   544
  in class end;
haftmann@52137
   545
haftmann@52137
   546
fun read_class thy = cert_class thy o Sign.intern_class thy;
haftmann@52137
   547
haftmann@52137
   548
val parse_classrel_ident = Parse.class --| @{keyword "<"} -- Parse.class;
haftmann@52137
   549
haftmann@34071
   550
fun cert_inst thy (class, tyco) =
haftmann@34071
   551
  (cert_class thy class, cert_tyco thy tyco);
haftmann@34071
   552
haftmann@34071
   553
fun read_inst thy (raw_tyco, raw_class) =
haftmann@34071
   554
  (read_class thy raw_class, read_tyco thy raw_tyco);
haftmann@34071
   555
haftmann@52137
   556
val parse_inst_ident = Parse.xname --| @{keyword "::"} -- Parse.class;
haftmann@52137
   557
haftmann@52137
   558
fun cert_syms thy =
haftmann@52137
   559
  Code_Symbol.map_attr (apfst (cert_const thy)) (apfst (cert_tyco thy))
haftmann@52137
   560
    (apfst (cert_class thy)) ((apfst o pairself) (cert_class thy)) (apfst (cert_inst thy)) I;
haftmann@52137
   561
haftmann@52137
   562
fun read_syms thy =
haftmann@52137
   563
  Code_Symbol.map_attr (apfst (Code.read_const thy)) (apfst (read_tyco thy))
haftmann@52137
   564
    (apfst (read_class thy)) ((apfst o pairself) (read_class thy)) (apfst (read_inst thy)) I;
haftmann@24219
   565
haftmann@52137
   566
fun check_const_syntax thy c syn =
haftmann@52137
   567
  if Code_Printer.requires_args syn > Code.args_number thy c
haftmann@52137
   568
  then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@52137
   569
  else syn;
haftmann@52137
   570
haftmann@52137
   571
fun check_tyco_syntax thy tyco syn =
haftmann@52137
   572
  if fst syn <> Sign.arity_number thy tyco
haftmann@52137
   573
  then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@52137
   574
  else syn;
haftmann@34071
   575
haftmann@52137
   576
fun arrange_printings prep_const thy =
haftmann@52137
   577
  let
haftmann@52137
   578
    fun arrange check (sym, target_syns) =
haftmann@52137
   579
      map (fn (target, some_syn) => (target, (sym, Option.map (check thy sym) some_syn))) target_syns;
haftmann@52137
   580
  in
haftmann@52137
   581
    Code_Symbol.maps_attr'
haftmann@52137
   582
      (arrange check_const_syntax) (arrange check_tyco_syntax)
haftmann@52137
   583
        (arrange ((K o K) I)) (arrange ((K o K) I)) (arrange ((K o K) I))
haftmann@52137
   584
        (arrange (fn thy => fn _ => fn (raw_content, raw_cs) =>
haftmann@52137
   585
          (Code_Printer.str raw_content, map (prep_const thy) raw_cs)))
haftmann@52137
   586
  end;
haftmann@52137
   587
haftmann@52137
   588
fun cert_printings thy = cert_syms thy #> arrange_printings cert_const thy;
haftmann@24219
   589
haftmann@52137
   590
fun read_printings thy = read_syms thy #> arrange_printings Code.read_const thy;
haftmann@52137
   591
haftmann@52137
   592
haftmann@52137
   593
(* custom symbol names *)
haftmann@34071
   594
haftmann@52137
   595
fun add_module_alias target (thyname, "") =
haftmann@52137
   596
      map_module_alias target (Symtab.delete thyname)
haftmann@52137
   597
  | add_module_alias target (thyname, modlname) =
haftmann@52137
   598
      let
haftmann@52137
   599
        val xs = Long_Name.explode modlname;
haftmann@52137
   600
        val xs' = map (Name.desymbolize true) xs;
haftmann@52137
   601
      in if xs' = xs
haftmann@52137
   602
        then map_module_alias target (Symtab.update (thyname, modlname))
haftmann@52137
   603
        else error ("Invalid module name: " ^ quote modlname ^ "\n"
haftmann@52137
   604
          ^ "perhaps try " ^ quote (Long_Name.implode xs'))
haftmann@52137
   605
      end;
haftmann@24219
   606
haftmann@39817
   607
fun add_reserved target sym thy =
haftmann@24219
   608
  let
haftmann@39817
   609
    val (_, data) = collapse_hierarchy thy target;
haftmann@39817
   610
    val _ = if member (op =) (the_reserved data) sym
haftmann@24219
   611
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@39817
   612
      else ();
haftmann@39817
   613
  in
haftmann@39817
   614
    thy
haftmann@39817
   615
    |> map_reserved target (insert (op =) sym)
haftmann@39817
   616
  end;
haftmann@24992
   617
haftmann@52137
   618
haftmann@52137
   619
(* custom printings *)
haftmann@52137
   620
haftmann@52137
   621
fun set_printing (target, sym_syn) = map_printings target (Code_Symbol.set_data sym_syn);
haftmann@52137
   622
haftmann@52137
   623
fun gen_set_printings prep_print_decl raw_print_decls thy =
haftmann@52137
   624
  fold set_printing (prep_print_decl thy raw_print_decls) thy;
haftmann@52137
   625
haftmann@52137
   626
val set_printings = gen_set_printings cert_printings;
haftmann@52137
   627
val set_printings_cmd = gen_set_printings read_printings;
haftmann@52137
   628
haftmann@52137
   629
fun gen_add_syntax Symbol prep_x prep_syn target raw_x some_raw_syn thy =
haftmann@24992
   630
  let
haftmann@52137
   631
    val x = prep_x thy raw_x;
haftmann@52137
   632
  in set_printing (target, Symbol (x, Option.map (prep_syn thy x) some_raw_syn)) thy end;
haftmann@28926
   633
haftmann@52137
   634
fun gen_add_const_syntax prep_const =
haftmann@52137
   635
  gen_add_syntax Code_Symbol.Constant prep_const check_const_syntax;
haftmann@52137
   636
haftmann@52137
   637
fun gen_add_tyco_syntax prep_tyco =
haftmann@52137
   638
  gen_add_syntax Code_Symbol.Type_Constructor prep_tyco check_tyco_syntax;
haftmann@52137
   639
haftmann@52137
   640
fun gen_add_class_syntax prep_class =
haftmann@52137
   641
  gen_add_syntax Code_Symbol.Type_Class prep_class ((K o K) I);
haftmann@24219
   642
haftmann@52137
   643
fun gen_add_instance_syntax prep_inst =
haftmann@52137
   644
  gen_add_syntax Code_Symbol.Class_Instance prep_inst ((K o K) I);
haftmann@52137
   645
haftmann@52137
   646
fun gen_add_include prep_const target (name, some_content) thy =
haftmann@52137
   647
  gen_add_syntax Code_Symbol.Module (K I)
haftmann@52137
   648
    (fn thy => fn _ => fn (raw_content, raw_cs) => (Code_Printer.str raw_content, map (prep_const thy) raw_cs))
haftmann@52137
   649
    target name some_content thy;
haftmann@52137
   650
haftmann@52137
   651
haftmann@52137
   652
(* abortable constants *)
haftmann@24219
   653
haftmann@28663
   654
fun gen_allow_abort prep_const raw_c thy =
haftmann@24841
   655
  let
haftmann@28663
   656
    val c = prep_const thy raw_c;
haftmann@34248
   657
  in thy |> (Targets.map o apfst o apsnd) (insert (op =) c) end;
haftmann@24841
   658
haftmann@27000
   659
haftmann@27103
   660
(* concrete syntax *)
haftmann@27000
   661
haftmann@34021
   662
local
haftmann@34021
   663
haftmann@47576
   664
fun zip_list (x :: xs) f g =
haftmann@34152
   665
  f
haftmann@37881
   666
  :|-- (fn y =>
haftmann@34152
   667
    fold_map (fn x => g |-- f >> pair x) xs
haftmann@37881
   668
    :|-- (fn xys => pair ((x, y) :: xys)));
haftmann@34152
   669
haftmann@37881
   670
fun process_multi_syntax parse_thing parse_syntax change =
haftmann@37881
   671
  (Parse.and_list1 parse_thing
wenzelm@46949
   672
  :|-- (fn things => Scan.repeat1 (@{keyword "("} |-- Parse.name --
haftmann@52068
   673
        (zip_list things (Scan.option parse_syntax) @{keyword "and"}) --| @{keyword ")"})))
haftmann@37881
   674
  >> (Toplevel.theory oo fold)
haftmann@37881
   675
    (fn (target, syns) => fold (fn (raw_x, syn) => change target raw_x syn) syns);
haftmann@24219
   676
haftmann@24219
   677
in
haftmann@24219
   678
haftmann@52137
   679
val add_reserved = add_reserved;
haftmann@52137
   680
val add_const_syntax = gen_add_const_syntax (K I);
haftmann@52137
   681
val add_tyco_syntax = gen_add_tyco_syntax cert_tyco;
haftmann@38923
   682
val add_class_syntax = gen_add_class_syntax cert_class;
haftmann@38923
   683
val add_instance_syntax = gen_add_instance_syntax cert_inst;
haftmann@52137
   684
val add_include = gen_add_include (K I);
haftmann@27103
   685
val allow_abort = gen_allow_abort (K I);
haftmann@24219
   686
haftmann@52137
   687
val add_const_syntax_cmd = gen_add_const_syntax Code.read_const;
haftmann@52137
   688
val add_tyco_syntax_cmd = gen_add_tyco_syntax read_tyco;
haftmann@38923
   689
val add_class_syntax_cmd = gen_add_class_syntax read_class;
haftmann@38923
   690
val add_instance_syntax_cmd = gen_add_instance_syntax read_inst;
haftmann@52137
   691
val add_include_cmd = gen_add_include Code.read_const;
haftmann@31156
   692
val allow_abort_cmd = gen_allow_abort Code.read_const;
haftmann@24219
   693
haftmann@28054
   694
fun parse_args f args =
wenzelm@36959
   695
  case Scan.read Token.stopper f args
haftmann@28054
   696
   of SOME x => x
haftmann@28054
   697
    | NONE => error "Bad serializer arguments";
haftmann@28054
   698
haftmann@28054
   699
haftmann@27304
   700
(** Isar setup **)
haftmann@27103
   701
wenzelm@46949
   702
val code_expr_argsP = Scan.optional (@{keyword "("} |-- Args.parse --| @{keyword ")"}) [];
haftmann@27103
   703
haftmann@30494
   704
val code_exprP =
haftmann@37824
   705
  Scan.repeat1 Parse.term_group :|-- (fn raw_cs =>
wenzelm@46949
   706
    ((@{keyword "checking"} |-- Scan.repeat (Parse.name
wenzelm@46949
   707
      -- ((@{keyword "?"} |-- Scan.succeed false) || Scan.succeed true) -- code_expr_argsP))
haftmann@37824
   708
      >> (fn seris => check_code_cmd raw_cs seris)
wenzelm@46949
   709
    || Scan.repeat (@{keyword "in"} |-- Parse.name
wenzelm@46949
   710
       -- Scan.optional (@{keyword "module_name"} |-- Parse.name) ""
wenzelm@46949
   711
       -- Scan.optional (@{keyword "file"} |-- Parse.name) ""
haftmann@37824
   712
       -- code_expr_argsP) >> (fn seris => export_code_cmd raw_cs seris)));
haftmann@27103
   713
haftmann@52137
   714
fun parse_single_symbol_pragma parse_keyword parse_isa parse_target =
haftmann@52137
   715
  parse_keyword |-- Parse.!!! parse_isa --| (@{keyword "\<rightharpoonup>"} || @{keyword "=>"})
haftmann@52137
   716
    -- Parse.and_list1 (@{keyword "("} |-- (Parse.name --| @{keyword ")"} -- Scan.option parse_target));
haftmann@52137
   717
haftmann@52137
   718
fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
haftmann@52137
   719
  parse_single_symbol_pragma @{keyword "constant"} Parse.term_group parse_const
haftmann@52137
   720
    >> Code_Symbol.Constant
haftmann@52137
   721
  || parse_single_symbol_pragma @{keyword "type_constructor"} Parse.xname parse_tyco
haftmann@52137
   722
    >> Code_Symbol.Type_Constructor
haftmann@52137
   723
  || parse_single_symbol_pragma @{keyword "type_class"} Parse.xname parse_class
haftmann@52137
   724
    >> Code_Symbol.Type_Class
haftmann@52137
   725
  || parse_single_symbol_pragma @{keyword "class_relation"} parse_classrel_ident parse_classrel
haftmann@52137
   726
    >> Code_Symbol.Class_Relation
haftmann@52137
   727
  || parse_single_symbol_pragma @{keyword "class_instance"} parse_inst_ident parse_inst
haftmann@52137
   728
    >> Code_Symbol.Class_Instance
haftmann@52137
   729
  || parse_single_symbol_pragma @{keyword "code_module"} Parse.name parse_module
haftmann@52137
   730
    >> Code_Symbol.Module;
haftmann@52137
   731
haftmann@52137
   732
fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
haftmann@52137
   733
  Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma")
haftmann@52137
   734
    (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module));
haftmann@52137
   735
haftmann@52137
   736
val _ =
haftmann@52137
   737
  Outer_Syntax.command @{command_spec "code_printing"} "declare dedicated printing for code symbols"
haftmann@52137
   738
    (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
haftmann@52137
   739
      Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
haftmann@52137
   740
      (Parse.text -- Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [])
haftmann@52137
   741
      >> (Toplevel.theory o fold set_printings_cmd));
haftmann@52137
   742
haftmann@52137
   743
val _ =
haftmann@52137
   744
  Outer_Syntax.command @{command_spec "code_const"} "define code syntax for constant"
haftmann@52137
   745
    (process_multi_syntax Parse.term_group Code_Printer.parse_const_syntax
haftmann@52137
   746
      add_const_syntax_cmd);
haftmann@52137
   747
haftmann@52137
   748
val _ =
haftmann@52137
   749
  Outer_Syntax.command @{command_spec "code_type"} "define code syntax for type constructor"
haftmann@52137
   750
    (process_multi_syntax Parse.xname Code_Printer.parse_tyco_syntax
haftmann@52137
   751
      add_tyco_syntax_cmd);
haftmann@52137
   752
wenzelm@24867
   753
val _ =
wenzelm@46961
   754
  Outer_Syntax.command @{command_spec "code_class"} "define code syntax for class"
haftmann@52068
   755
    (process_multi_syntax Parse.xname Parse.string
wenzelm@46961
   756
      add_class_syntax_cmd);
haftmann@24219
   757
wenzelm@24867
   758
val _ =
wenzelm@46961
   759
  Outer_Syntax.command @{command_spec "code_instance"} "define code syntax for instance"
haftmann@52068
   760
    (process_multi_syntax (Parse.xname --| @{keyword "::"} -- Parse.xname) (Parse.minus >> K ())
wenzelm@46961
   761
      add_instance_syntax_cmd);
haftmann@24219
   762
wenzelm@24867
   763
val _ =
wenzelm@46961
   764
  Outer_Syntax.command @{command_spec "code_reserved"}
wenzelm@46961
   765
    "declare words as reserved for target language"
wenzelm@46961
   766
    (Parse.name -- Scan.repeat1 Parse.name
wenzelm@46961
   767
      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
haftmann@24219
   768
wenzelm@24867
   769
val _ =
wenzelm@46961
   770
  Outer_Syntax.command @{command_spec "code_include"}
wenzelm@46961
   771
    "declare piece of code to be included in generated code"
wenzelm@46961
   772
    (Parse.name -- Parse.name -- (Parse.text :|--
wenzelm@46961
   773
      (fn "-" => Scan.succeed NONE
wenzelm@46961
   774
        | s => Scan.optional (@{keyword "attach"} |-- Scan.repeat1 Parse.term) [] >> pair s >> SOME))
wenzelm@46961
   775
      >> (fn ((target, name), content_consts) =>
wenzelm@46961
   776
          (Toplevel.theory o add_include_cmd target) (name, content_consts)));
haftmann@24992
   777
haftmann@24992
   778
val _ =
wenzelm@46961
   779
  Outer_Syntax.command @{command_spec "code_modulename"} "alias module to other name"
wenzelm@46961
   780
    (Parse.name -- Scan.repeat1 (Parse.name -- Parse.name)
wenzelm@46961
   781
      >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames));
haftmann@27103
   782
haftmann@27103
   783
val _ =
wenzelm@46961
   784
  Outer_Syntax.command @{command_spec "code_abort"}
wenzelm@46961
   785
    "permit constant to be implemented as program abort"
wenzelm@46961
   786
    (Scan.repeat1 Parse.term_group >> (Toplevel.theory o fold allow_abort_cmd));
haftmann@24219
   787
wenzelm@24867
   788
val _ =
wenzelm@46961
   789
  Outer_Syntax.command @{command_spec "export_code"} "generate executable code for constants"
wenzelm@46961
   790
    (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
haftmann@30494
   791
haftmann@24219
   792
end; (*local*)
haftmann@24219
   793
haftmann@39646
   794
haftmann@39646
   795
(** external entrance point -- for codegen tool **)
haftmann@39646
   796
haftmann@39750
   797
fun codegen_tool thyname cmd_expr =
haftmann@39646
   798
  let
haftmann@39646
   799
    val thy = Thy_Info.get_theory thyname;
haftmann@39646
   800
    val parse = Scan.read Token.stopper (Parse.!!! code_exprP) o
haftmann@39646
   801
      (filter Token.is_proper o Outer_Syntax.scan Position.none);
haftmann@39646
   802
  in case parse cmd_expr
haftmann@39646
   803
   of SOME f => (writeln "Now generating code..."; f thy)
haftmann@39646
   804
    | NONE => error ("Bad directive " ^ quote cmd_expr)
haftmann@39646
   805
  end;
haftmann@39646
   806
wenzelm@43564
   807
wenzelm@43564
   808
(** theory setup **)
wenzelm@43564
   809
wenzelm@43564
   810
val setup = antiq_setup;
wenzelm@43564
   811
haftmann@24219
   812
end; (*struct*)