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