src/Tools/Code/code_target.ML
author wenzelm
Sun Jan 13 18:40:26 2019 +0100 (7 months ago)
changeset 69645 e4e5bc6ac214
parent 69624 e02bdf853a4c
child 69646 aac49f64051d
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@55757
     9
  val cert_tyco: Proof.context -> string -> string
haftmann@55757
    10
  val read_tyco: Proof.context -> string -> string
haftmann@36471
    11
haftmann@69623
    12
  datatype pretty_modules = Singleton of string * Pretty.T | Hierarchy of (string list * Pretty.T) list;
haftmann@69623
    13
haftmann@69624
    14
  val export_code_for: Proof.context -> Path.T option option -> string -> string -> int option -> Token.T list
haftmann@55683
    15
    -> Code_Thingol.program -> bool -> Code_Symbol.T list -> unit
haftmann@69623
    16
  val produce_code_for: Proof.context -> string -> string -> int option -> Token.T list
haftmann@69623
    17
    -> Code_Thingol.program -> bool -> Code_Symbol.T list -> (string list * string) list * string option list
haftmann@69623
    18
  val present_code_for: Proof.context -> string -> string -> int option -> Token.T list
haftmann@55150
    19
    -> Code_Thingol.program -> Code_Symbol.T list * Code_Symbol.T list -> string
haftmann@55757
    20
  val check_code_for: Proof.context -> string -> bool -> Token.T list
haftmann@55683
    21
    -> Code_Thingol.program -> bool -> Code_Symbol.T list -> unit
haftmann@38918
    22
haftmann@55757
    23
  val export_code: Proof.context -> bool -> string list
haftmann@69624
    24
    -> (((string * string) * Path.T option option) * Token.T list) list -> unit
haftmann@55757
    25
  val produce_code: Proof.context -> bool -> string list
haftmann@69623
    26
    -> string -> string -> int option -> Token.T list -> (string list * string) list * string option list
haftmann@55757
    27
  val present_code: Proof.context -> string list -> Code_Symbol.T list
haftmann@69623
    28
    -> string -> string -> int option -> Token.T list -> string
haftmann@55757
    29
  val check_code: Proof.context -> bool -> string list
haftmann@38918
    30
    -> ((string * bool) * Token.T list) list -> unit
haftmann@38918
    31
haftmann@48568
    32
  val generatedN: string
haftmann@64957
    33
  val compilation_text: Proof.context -> string -> Code_Thingol.program
haftmann@55684
    34
    -> Code_Symbol.T list -> bool -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
haftmann@69623
    35
    -> (string list * string) list * string
haftmann@64959
    36
  val compilation_text': Proof.context -> string -> string option -> Code_Thingol.program
haftmann@64959
    37
    -> Code_Symbol.T list -> bool -> ((string * class list) list * Code_Thingol.itype) * Code_Thingol.iterm
haftmann@69623
    38
    -> ((string list * string) list * string) * (Code_Symbol.T -> string option)
haftmann@41344
    39
haftmann@28054
    40
  type serializer
haftmann@34152
    41
  type literals = Code_Printer.literals
haftmann@59104
    42
  type language
haftmann@59104
    43
  type ancestry
haftmann@59479
    44
  val assert_target: theory -> string -> string
haftmann@59104
    45
  val add_language: string * language -> theory -> theory
haftmann@59104
    46
  val add_derived_target: string * ancestry -> theory -> theory
haftmann@55757
    47
  val the_literals: Proof.context -> string -> literals
wenzelm@36959
    48
  val parse_args: 'a parser -> Token.T list -> 'a
haftmann@59324
    49
  val default_code_width: int Config.T
haftmann@38917
    50
haftmann@52137
    51
  type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl
haftmann@52138
    52
  val set_identifiers: (string, string, string, string, string, string) symbol_attr_decl
haftmann@52138
    53
    -> theory -> theory
haftmann@69485
    54
  val set_printings: (Code_Printer.raw_const_syntax, Code_Printer.tyco_syntax, string, unit, unit, string * Code_Symbol.T list) symbol_attr_decl
haftmann@52137
    55
    -> theory -> theory
haftmann@28054
    56
  val add_reserved: string -> string -> theory -> theory
haftmann@24219
    57
end;
haftmann@24219
    58
haftmann@28054
    59
structure Code_Target : CODE_TARGET =
haftmann@24219
    60
struct
haftmann@24219
    61
haftmann@55150
    62
open Basic_Code_Symbol;
haftmann@28054
    63
open Basic_Code_Thingol;
haftmann@34152
    64
haftmann@34152
    65
type literals = Code_Printer.literals;
haftmann@52137
    66
type ('a, 'b, 'c, 'd, 'e, 'f) symbol_attr_decl =
haftmann@52137
    67
  (string * (string * 'a option) list, string * (string * 'b option) list,
haftmann@52137
    68
    class * (string * 'c option) list, (class * class) * (string * 'd option) list,
haftmann@52137
    69
    (class * string) * (string * 'e option) list,
haftmann@52137
    70
    string * (string * 'f option) list) Code_Symbol.attr;
haftmann@52137
    71
haftmann@34152
    72
type tyco_syntax = Code_Printer.tyco_syntax;
haftmann@55153
    73
type raw_const_syntax = Code_Printer.raw_const_syntax;
haftmann@34152
    74
haftmann@24219
    75
haftmann@52377
    76
(** checking and parsing of symbols **)
haftmann@52377
    77
haftmann@55757
    78
fun cert_const ctxt const =
haftmann@52377
    79
  let
haftmann@55757
    80
    val _ = if Sign.declared_const (Proof_Context.theory_of ctxt) const then ()
haftmann@52377
    81
      else error ("No such constant: " ^ quote const);
haftmann@52377
    82
  in const end;
haftmann@52377
    83
haftmann@55757
    84
fun read_const ctxt = Code.read_const (Proof_Context.theory_of ctxt);
haftmann@55757
    85
haftmann@55757
    86
fun cert_tyco ctxt tyco =
haftmann@52377
    87
  let
haftmann@55757
    88
    val _ = if Sign.declared_tyname (Proof_Context.theory_of ctxt) tyco then ()
haftmann@52377
    89
      else error ("No such type constructor: " ^ quote tyco);
haftmann@52377
    90
  in tyco end;
haftmann@52377
    91
wenzelm@55951
    92
fun read_tyco ctxt =
wenzelm@56002
    93
  #1 o dest_Type o Proof_Context.read_type_name {proper = true, strict = true} ctxt;
haftmann@52377
    94
haftmann@55757
    95
fun cert_class ctxt class =
haftmann@52377
    96
  let
haftmann@55757
    97
    val _ = Axclass.get_info (Proof_Context.theory_of ctxt) class;
haftmann@52377
    98
  in class end;
haftmann@52377
    99
wenzelm@69593
   100
val parse_classrel_ident = Parse.class --| \<^keyword>\<open><\<close> -- Parse.class;
haftmann@52377
   101
haftmann@55757
   102
fun cert_inst ctxt (class, tyco) =
haftmann@55757
   103
  (cert_class ctxt class, cert_tyco ctxt tyco);
haftmann@52377
   104
haftmann@55757
   105
fun read_inst ctxt (raw_tyco, raw_class) =
haftmann@55757
   106
  (read_tyco ctxt raw_tyco, Proof_Context.read_class ctxt raw_class);
haftmann@52377
   107
wenzelm@69593
   108
val parse_inst_ident = Parse.name --| \<^keyword>\<open>::\<close> -- Parse.class;
haftmann@52377
   109
haftmann@55757
   110
fun cert_syms ctxt =
haftmann@69485
   111
  Code_Symbol.map_attr (cert_const ctxt) (cert_tyco ctxt)
haftmann@69485
   112
    (cert_class ctxt) (apply2 (cert_class ctxt)) (cert_inst ctxt) I;
haftmann@69485
   113
haftmann@69485
   114
fun read_syms ctxt =
haftmann@69485
   115
  Code_Symbol.map_attr (read_const ctxt) (read_tyco ctxt)
haftmann@69485
   116
    (Proof_Context.read_class ctxt) (apply2 (Proof_Context.read_class ctxt)) (read_inst ctxt) I;
haftmann@69485
   117
haftmann@69485
   118
fun cert_syms' ctxt =
haftmann@55757
   119
  Code_Symbol.map_attr (apfst (cert_const ctxt)) (apfst (cert_tyco ctxt))
wenzelm@59058
   120
    (apfst (cert_class ctxt)) ((apfst o apply2) (cert_class ctxt)) (apfst (cert_inst ctxt)) I;
haftmann@52377
   121
haftmann@69485
   122
fun read_syms' ctxt =
haftmann@55757
   123
  Code_Symbol.map_attr (apfst (read_const ctxt)) (apfst (read_tyco ctxt))
wenzelm@59058
   124
    (apfst (Proof_Context.read_class ctxt)) ((apfst o apply2) (Proof_Context.read_class ctxt)) (apfst (read_inst ctxt)) I;
haftmann@52377
   125
haftmann@52377
   126
fun check_name is_module s =
haftmann@52377
   127
  let
haftmann@52377
   128
    val _ = if s = "" then error "Bad empty code name" else ();
haftmann@52377
   129
    val xs = Long_Name.explode s;
haftmann@52377
   130
    val xs' = if is_module
haftmann@56826
   131
        then map (Name.desymbolize NONE) xs
haftmann@52377
   132
      else if length xs < 2
haftmann@52377
   133
        then error ("Bad code name without module component: " ^ quote s)
haftmann@52377
   134
      else
haftmann@52377
   135
        let
haftmann@52377
   136
          val (ys, y) = split_last xs;
haftmann@56826
   137
          val ys' = map (Name.desymbolize NONE) ys;
haftmann@56826
   138
          val y' = Name.desymbolize NONE y;
haftmann@52377
   139
        in ys' @ [y'] end;
haftmann@52377
   140
  in if xs' = xs
haftmann@55291
   141
    then if is_module then (xs, "") else split_last xs
haftmann@52377
   142
    else error ("Invalid code name: " ^ quote s ^ "\n"
haftmann@52377
   143
      ^ "better try " ^ quote (Long_Name.implode xs'))
haftmann@52377
   144
  end;
haftmann@52377
   145
haftmann@52377
   146
haftmann@69623
   147
(** serializers **)
haftmann@27014
   148
haftmann@69623
   149
datatype pretty_modules = Singleton of string * Pretty.T | Hierarchy of (string list * Pretty.T) list;
haftmann@28054
   150
haftmann@39142
   151
type serializer = Token.T list
haftmann@52138
   152
  -> Proof.context
haftmann@39142
   153
  -> {
haftmann@38926
   154
    reserved_syms: string list,
haftmann@59103
   155
    identifiers: Code_Printer.identifiers,
haftmann@38926
   156
    includes: (string * Pretty.T) list,
haftmann@38926
   157
    class_syntax: string -> string option,
haftmann@38926
   158
    tyco_syntax: string -> Code_Printer.tyco_syntax option,
haftmann@69623
   159
    const_syntax: string -> Code_Printer.const_syntax option,
haftmann@69623
   160
    module_name: string }
haftmann@41342
   161
  -> Code_Thingol.program
haftmann@69623
   162
  -> Code_Symbol.T list
haftmann@69623
   163
  -> pretty_modules * (Code_Symbol.T -> string option);
haftmann@69623
   164
haftmann@69624
   165
val generatedN = "Generated_Code";
haftmann@69624
   166
haftmann@69623
   167
fun flat_modules selects width (Singleton (_, p)) =
haftmann@69623
   168
      Code_Printer.format selects width p
haftmann@69623
   169
  | flat_modules selects width (Hierarchy code_modules) =
haftmann@69623
   170
      space_implode "\n\n" (map (Code_Printer.format selects width o snd) code_modules);
haftmann@69623
   171
haftmann@69623
   172
fun export_to_file root width (Singleton (_, p)) =
haftmann@69623
   173
      File.write root (Code_Printer.format [] width p)
haftmann@69623
   174
  | export_to_file root width (Hierarchy code_modules) = (
haftmann@69623
   175
      Isabelle_System.mkdirs root;
haftmann@69623
   176
      map (fn (names, p) => File.write (Path.appends (root :: map Path.basic names)) (Code_Printer.format [] width p)) code_modules;
haftmann@69623
   177
      ());
haftmann@27000
   178
haftmann@69624
   179
fun export_to_exports thy width (Singleton (extension, p)) =
haftmann@69624
   180
      Export.export_raw thy (generatedN ^ "." ^ extension) [Code_Printer.format [] width p]
haftmann@69624
   181
  | export_to_exports thy width (Hierarchy code_modules) = (
haftmann@69624
   182
      map (fn (names, p) => Export.export_raw thy (Path.implode (Path.make names))
haftmann@69624
   183
        [Code_Printer.format [] width p]) code_modules;
haftmann@69624
   184
      ());
haftmann@69624
   185
haftmann@69624
   186
fun export_result ctxt some_file width (pretty_code, _) =
haftmann@69624
   187
  (case some_file of
haftmann@69624
   188
    NONE =>
haftmann@69624
   189
      let
haftmann@69624
   190
        val thy = Proof_Context.theory_of ctxt
haftmann@69624
   191
      in export_to_exports thy width pretty_code end
haftmann@69624
   192
  | SOME NONE => writeln (flat_modules [] width pretty_code)
haftmann@69624
   193
  | SOME (SOME relative_root) =>
haftmann@69623
   194
      let
haftmann@69623
   195
        val root = File.full_path (Resources.master_directory (Proof_Context.theory_of ctxt)) relative_root;
haftmann@69623
   196
        val _ = File.check_dir (Path.dir root);
haftmann@69624
   197
      in export_to_file root width pretty_code end);
haftmann@69623
   198
haftmann@69623
   199
fun produce_result syms width (Singleton (_, p), deresolve) =
haftmann@69623
   200
      ([([], Code_Printer.format [] width p)], map deresolve syms)
haftmann@69623
   201
  | produce_result syms width (Hierarchy code_modules, deresolve) =
haftmann@69623
   202
      ((map o apsnd) (Code_Printer.format [] width) code_modules, map deresolve syms);
haftmann@69623
   203
haftmann@69623
   204
fun present_result selects width (pretty_code, _) =
haftmann@69623
   205
  flat_modules selects width pretty_code;
haftmann@69623
   206
haftmann@69623
   207
haftmann@52137
   208
(** theory data **)
haftmann@52137
   209
haftmann@67207
   210
type language = {serializer: serializer, literals: literals,
haftmann@67207
   211
  check: {env_var: string, make_destination: Path.T -> Path.T, make_command: string -> string},
haftmann@67207
   212
  evaluation_args: Token.T list}; 
haftmann@59102
   213
haftmann@59103
   214
type ancestry = (string * (Code_Thingol.program -> Code_Thingol.program)) list;
haftmann@27103
   215
haftmann@59103
   216
val merge_ancestry : ancestry * ancestry -> ancestry = AList.join (op =) (K snd);
haftmann@27103
   217
haftmann@67207
   218
type target = {serial: serial, language: language, ancestry: ancestry};
haftmann@27437
   219
haftmann@34248
   220
structure Targets = Theory_Data
haftmann@34071
   221
(
haftmann@59324
   222
  type T = (target * Code_Printer.data) Symtab.table;
haftmann@59324
   223
  val empty = Symtab.empty;
haftmann@34071
   224
  val extend = I;
haftmann@59324
   225
  fun merge (targets1, targets2) : T =
haftmann@59324
   226
    Symtab.join (fn target_name => fn ((target1, data1), (target2, data2)) =>
haftmann@59103
   227
      if #serial target1 = #serial target2 then
haftmann@67207
   228
      ({serial = #serial target1, language = #language target1,
haftmann@59103
   229
        ancestry = merge_ancestry (#ancestry target1, #ancestry target2)},
haftmann@59103
   230
        Code_Printer.merge_data (data1, data2))
haftmann@59102
   231
      else error ("Incompatible targets: " ^ quote target_name) 
haftmann@59324
   232
    ) (targets1, targets2)
haftmann@34071
   233
);
haftmann@27436
   234
haftmann@59324
   235
fun exists_target thy = Symtab.defined (Targets.get thy);
haftmann@59324
   236
fun lookup_target_data thy = Symtab.lookup (Targets.get thy);
haftmann@59479
   237
fun assert_target thy target_name =
haftmann@59479
   238
  if exists_target thy target_name
haftmann@59479
   239
  then target_name
haftmann@59479
   240
  else error ("Unknown code target language: " ^ quote target_name);
haftmann@59100
   241
haftmann@59104
   242
fun fold1 f xs = fold f (tl xs) (hd xs);
haftmann@59104
   243
haftmann@59101
   244
fun join_ancestry thy target_name =
haftmann@59101
   245
  let
haftmann@59479
   246
    val _ = assert_target thy target_name; 
haftmann@59103
   247
    val the_target_data = the o lookup_target_data thy;
haftmann@59103
   248
    val (target, this_data) = the_target_data target_name;
haftmann@59103
   249
    val ancestry = #ancestry target;
haftmann@59102
   250
    val modifies = rev (map snd ancestry);
haftmann@59102
   251
    val modify = fold (curry (op o)) modifies I;
haftmann@59103
   252
    val datas = rev (map (snd o the_target_data o fst) ancestry) @ [this_data];
haftmann@59104
   253
    val data = fold1 (fn data' => fn data => Code_Printer.merge_data (data, data')) datas;
haftmann@59103
   254
  in (modify, (target, data)) end;
haftmann@59101
   255
haftmann@69623
   256
fun allocate_target target_name target thy =
haftmann@27304
   257
  let
haftmann@59101
   258
    val _ = if exists_target thy target_name
haftmann@59101
   259
      then error ("Attempt to overwrite existing target " ^ quote target_name)
wenzelm@43564
   260
      else ();
haftmann@28054
   261
  in
haftmann@28054
   262
    thy
haftmann@59324
   263
    |> (Targets.map o Symtab.update) (target_name, (target, Code_Printer.empty_data))  
haftmann@28054
   264
  end;
haftmann@27436
   265
haftmann@59104
   266
fun add_language (target_name, language) =
haftmann@67207
   267
  allocate_target target_name {serial = serial (), language = language,
haftmann@67207
   268
    ancestry = []};
haftmann@27436
   269
haftmann@59104
   270
fun add_derived_target (target_name, initial_ancestry) thy =
haftmann@59101
   271
  let
haftmann@59104
   272
    val _ = if null initial_ancestry
haftmann@59104
   273
      then error "Must derive from existing target(s)" else ();
haftmann@59104
   274
    fun the_target_data target_name' = case lookup_target_data thy target_name' of
haftmann@59104
   275
      NONE => error ("Unknown code target language: " ^ quote target_name')
haftmann@59104
   276
    | SOME target_data' => target_data';
haftmann@59104
   277
    val targets = rev (map (fst o the_target_data o fst) initial_ancestry);
haftmann@59104
   278
    val supremum = fold1 (fn target' => fn target =>
haftmann@59104
   279
      if #serial target = #serial target'
haftmann@59104
   280
      then target else error "Incompatible targets") targets;
haftmann@59104
   281
    val ancestries = map #ancestry targets @ [initial_ancestry];
haftmann@59104
   282
    val ancestry = fold1 (fn ancestry' => fn ancestry =>
haftmann@59104
   283
      merge_ancestry (ancestry, ancestry')) ancestries;
haftmann@59101
   284
  in
haftmann@67207
   285
    allocate_target target_name {serial = #serial supremum, language = #language supremum,
haftmann@67207
   286
      ancestry = ancestry} thy 
haftmann@59101
   287
  end;
haftmann@59101
   288
  
haftmann@59102
   289
fun map_data target_name f thy =
haftmann@27436
   290
  let
haftmann@59479
   291
    val _ = assert_target thy target_name;
haftmann@28054
   292
  in
haftmann@28054
   293
    thy
haftmann@59324
   294
    |> (Targets.map o Symtab.map_entry target_name o apsnd o Code_Printer.map_data) f
haftmann@28054
   295
  end;
haftmann@27304
   296
haftmann@59099
   297
fun map_reserved target_name =
haftmann@59102
   298
  map_data target_name o @{apply 3 (1)};
haftmann@59099
   299
fun map_identifiers target_name =
haftmann@59102
   300
  map_data target_name o @{apply 3 (2)};
haftmann@59099
   301
fun map_printings target_name =
haftmann@59102
   302
  map_data target_name o @{apply 3 (3)};
haftmann@27000
   303
haftmann@27000
   304
haftmann@34021
   305
(** serializer usage **)
haftmann@34021
   306
haftmann@59324
   307
(* technical aside: pretty printing width *)
haftmann@59324
   308
wenzelm@69593
   309
val default_code_width = Attrib.setup_config_int \<^binding>\<open>default_code_width\<close> (K 80);
haftmann@59324
   310
haftmann@69623
   311
fun default_width ctxt = Config.get ctxt default_code_width;
haftmann@69623
   312
haftmann@69623
   313
val the_width = the_default o default_width;
haftmann@69623
   314
haftmann@59324
   315
haftmann@34021
   316
(* montage *)
haftmann@34021
   317
haftmann@59102
   318
fun the_language ctxt =
haftmann@59103
   319
  #language o fst o the o lookup_target_data (Proof_Context.theory_of ctxt);
haftmann@37822
   320
haftmann@59102
   321
fun the_literals ctxt = #literals o the_language ctxt;
haftmann@34021
   322
haftmann@67207
   323
fun the_evaluation_args ctxt = #evaluation_args o the_language ctxt;
haftmann@67207
   324
haftmann@39817
   325
local
haftmann@39817
   326
haftmann@59099
   327
fun activate_target ctxt target_name =
haftmann@39817
   328
  let
haftmann@59324
   329
    val thy = Proof_Context.theory_of ctxt;
haftmann@69623
   330
    val (modify, (target, data)) = join_ancestry thy target_name;
haftmann@69623
   331
    val serializer = (#serializer o #language) target;
haftmann@69623
   332
  in { serializer = serializer, data = data, modify = modify } end;
haftmann@38927
   333
haftmann@69623
   334
fun project_program_for_syms ctxt syms_hidden syms1 program1 =
haftmann@38927
   335
  let
haftmann@55147
   336
    val syms2 = subtract (op =) syms_hidden syms1;
haftmann@69527
   337
    val program2 = Code_Symbol.Graph.restrict (not o member (op =) syms_hidden) program1;
haftmann@69527
   338
    val unimplemented = Code_Thingol.unimplemented program2;
wenzelm@42359
   339
    val _ =
haftmann@54889
   340
      if null unimplemented then ()
wenzelm@42359
   341
      else error ("No code equations for " ^
wenzelm@55304
   342
        commas (map (Proof_Context.markup_const ctxt) unimplemented));
haftmann@69527
   343
    val syms3 = Code_Symbol.Graph.all_succs program2 syms2;
haftmann@69527
   344
    val program3 = Code_Symbol.Graph.restrict (member (op =) syms3) program2;
haftmann@69527
   345
  in program3 end;
haftmann@38927
   346
haftmann@69623
   347
fun project_includes_for_syms syms includes =
haftmann@69623
   348
   let
haftmann@69623
   349
     fun select_include (name, (content, cs)) =
haftmann@69623
   350
       if null cs orelse exists (member (op =) syms) cs
haftmann@69623
   351
       then SOME (name, content) else NONE;
haftmann@69623
   352
  in map_filter select_include includes end;
haftmann@69623
   353
haftmann@69623
   354
fun prepare_serializer ctxt target_name module_name args proto_program syms =
haftmann@38927
   355
  let
haftmann@69623
   356
    val { serializer, data, modify } = activate_target ctxt target_name;
haftmann@69623
   357
    val printings = Code_Printer.the_printings data;
haftmann@69623
   358
    val _ = if module_name = "" then () else (check_name true module_name; ())
haftmann@69623
   359
    val hidden_syms = Code_Symbol.symbols_of printings;
haftmann@69623
   360
    val prepared_program = project_program_for_syms ctxt hidden_syms syms proto_program;
haftmann@69623
   361
    val prepared_syms = subtract (op =) hidden_syms syms;
haftmann@69623
   362
    val all_syms = Code_Symbol.Graph.all_succs proto_program syms;
haftmann@69623
   363
    val includes = project_includes_for_syms all_syms
haftmann@69623
   364
      (Code_Symbol.dest_module_data printings);
haftmann@69623
   365
    val prepared_serializer = serializer args ctxt {
haftmann@69623
   366
      reserved_syms = Code_Printer.the_reserved data,
haftmann@69623
   367
      identifiers = Code_Printer.the_identifiers data,
haftmann@38926
   368
      includes = includes,
haftmann@55149
   369
      const_syntax = Code_Symbol.lookup_constant_data printings,
haftmann@55149
   370
      tyco_syntax = Code_Symbol.lookup_type_constructor_data printings,
haftmann@69623
   371
      class_syntax = Code_Symbol.lookup_type_class_data printings,
haftmann@69623
   372
      module_name = module_name };
haftmann@69623
   373
  in
haftmann@69623
   374
    (prepared_serializer o modify, (prepared_program, prepared_syms))
haftmann@34021
   375
  end;
haftmann@34021
   376
haftmann@69623
   377
fun invoke_serializer ctxt target_name module_name args program all_public syms =
haftmann@34021
   378
  let
haftmann@69623
   379
    val (prepared_serializer, (prepared_program, prepared_syms)) =
haftmann@69623
   380
      prepare_serializer ctxt target_name module_name args program syms;
haftmann@69623
   381
    val exports = if all_public then [] else prepared_syms;
haftmann@63164
   382
  in
haftmann@63164
   383
    Code_Preproc.timed_exec "serializing"
haftmann@69623
   384
      (fn () => prepared_serializer prepared_program exports) ctxt
haftmann@63164
   385
  end;
haftmann@34021
   386
haftmann@52138
   387
fun assert_module_name "" = error "Empty module name not allowed here"
haftmann@38933
   388
  | assert_module_name module_name = module_name;
haftmann@38933
   389
haftmann@34021
   390
in
haftmann@34021
   391
haftmann@69624
   392
fun export_code_for ctxt some_file target_name module_name some_width args =
haftmann@69624
   393
  export_result ctxt some_file (the_width ctxt some_width)
haftmann@69623
   394
  ooo invoke_serializer ctxt target_name module_name args;
haftmann@38918
   395
haftmann@69623
   396
fun produce_code_for ctxt target_name module_name some_width args =
haftmann@39102
   397
  let
haftmann@69623
   398
    val serializer = invoke_serializer ctxt target_name (assert_module_name module_name) args;
haftmann@55683
   399
  in fn program => fn all_public => fn syms =>
haftmann@69623
   400
    produce_result syms (the_width ctxt some_width)
haftmann@69623
   401
      (serializer program all_public syms)
haftmann@39484
   402
  end;
haftmann@38929
   403
haftmann@69623
   404
fun present_code_for ctxt target_name module_name some_width args =
haftmann@39484
   405
  let
haftmann@69623
   406
    val serializer = invoke_serializer ctxt target_name (assert_module_name module_name) args;
haftmann@55147
   407
  in fn program => fn (syms, selects) =>
haftmann@69623
   408
    present_result selects (the_width ctxt some_width) (serializer program false syms)
haftmann@39484
   409
  end;
haftmann@38918
   410
haftmann@59099
   411
fun check_code_for ctxt target_name strict args program all_public syms =
haftmann@37824
   412
  let
haftmann@37824
   413
    val { env_var, make_destination, make_command } =
haftmann@59102
   414
      (#check o the_language ctxt) target_name;
wenzelm@41939
   415
    fun ext_check p =
wenzelm@43564
   416
      let
haftmann@37824
   417
        val destination = make_destination p;
haftmann@69624
   418
        val _ = export_result ctxt (SOME (SOME destination)) 80
haftmann@69623
   419
          (invoke_serializer ctxt target_name generatedN args program all_public syms)
haftmann@48568
   420
        val cmd = make_command generatedN;
wenzelm@43850
   421
      in
wenzelm@62549
   422
        if Isabelle_System.bash ("cd " ^ File.bash_path p ^ " && " ^ cmd ^ " 2>&1") <> 0
haftmann@59099
   423
        then error ("Code check failed for " ^ target_name ^ ": " ^ cmd)
haftmann@37824
   424
        else ()
haftmann@37824
   425
      end;
wenzelm@43850
   426
  in
haftmann@63024
   427
    if not (env_var = "") andalso getenv env_var = ""
haftmann@37825
   428
    then if strict
haftmann@59099
   429
      then error (env_var ^ " not set; cannot check code for " ^ target_name)
haftmann@59099
   430
      else warning (env_var ^ " not set; skipped checking code for " ^ target_name)
wenzelm@41939
   431
    else Isabelle_System.with_tmp_dir "Code_Test" ext_check
haftmann@37824
   432
  end;
haftmann@37824
   433
haftmann@69623
   434
fun dynamic_compilation_text prepared_serializer width prepared_program syms all_public ((vs, ty), t) =
haftmann@41344
   435
  let
haftmann@41344
   436
    val _ = if Code_Thingol.contains_dict_var t then
haftmann@41344
   437
      error "Term to be evaluated contains free dictionaries" else ();
wenzelm@43324
   438
    val v' = singleton (Name.variant_list (map fst vs)) "a";
haftmann@41344
   439
    val vs' = (v', []) :: vs;
haftmann@55147
   440
    val ty' = ITyVar v' `-> ty;
haftmann@41344
   441
    val program = prepared_program
haftmann@55147
   442
      |> Code_Symbol.Graph.new_node (Code_Symbol.value,
haftmann@57249
   443
          Code_Thingol.Fun (((vs', ty'), [(([IVar (SOME "dummy")], t), (NONE, true))]), NONE))
haftmann@55147
   444
      |> fold (curry (perhaps o try o
haftmann@55147
   445
          Code_Symbol.Graph.add_edge) Code_Symbol.value) syms;
haftmann@69623
   446
    val (pretty_code, deresolve) =
haftmann@69623
   447
      prepared_serializer program (if all_public then [] else [Code_Symbol.value]);
haftmann@69623
   448
    val (compilation_code, [SOME value_name]) =
haftmann@69623
   449
      produce_result [Code_Symbol.value] width (pretty_code, deresolve);
haftmann@69623
   450
  in ((compilation_code, value_name), deresolve) end;
haftmann@41344
   451
haftmann@64959
   452
fun compilation_text' ctxt target_name some_module_name program syms =
haftmann@41344
   453
  let
haftmann@69623
   454
    val width = default_width ctxt;
haftmann@67207
   455
    val evaluation_args = the_evaluation_args ctxt target_name;
haftmann@69623
   456
    val (prepared_serializer, (prepared_program, _)) =
haftmann@69623
   457
      prepare_serializer ctxt target_name (the_default generatedN some_module_name) evaluation_args program syms;
haftmann@63164
   458
  in
haftmann@63164
   459
    Code_Preproc.timed_exec "serializing"
haftmann@69623
   460
      (fn () => dynamic_compilation_text prepared_serializer width prepared_program syms) ctxt
haftmann@63164
   461
  end;
haftmann@41344
   462
haftmann@64959
   463
fun compilation_text ctxt target_name program syms =
haftmann@64959
   464
  fst oo compilation_text' ctxt target_name NONE program syms
haftmann@64959
   465
  
haftmann@34021
   466
end; (* local *)
haftmann@34021
   467
haftmann@34021
   468
haftmann@34021
   469
(* code generation *)
haftmann@34021
   470
wenzelm@66586
   471
fun prep_destination (s, pos) =
wenzelm@66586
   472
  if s = "" then NONE
wenzelm@66586
   473
  else
wenzelm@66586
   474
    let
wenzelm@66586
   475
      val _ = Position.report pos Markup.language_path;
wenzelm@66586
   476
      val path = Path.explode s;
wenzelm@66586
   477
      val _ = Position.report pos (Markup.path (Path.smart_implode path));
wenzelm@66586
   478
    in SOME path end;
haftmann@38918
   479
haftmann@55757
   480
fun export_code ctxt all_public cs seris =
haftmann@34021
   481
  let
haftmann@63159
   482
    val program = Code_Thingol.consts_program ctxt cs;
haftmann@69624
   483
    val _ = map (fn (((target_name, module_name), some_file), args) =>
haftmann@69624
   484
      export_code_for ctxt some_file target_name module_name NONE args program all_public (map Constant cs)) seris;
haftmann@34021
   485
  in () end;
haftmann@34021
   486
haftmann@55757
   487
fun export_code_cmd all_public raw_cs seris ctxt =
haftmann@55757
   488
  export_code ctxt all_public
haftmann@55757
   489
    (Code_Thingol.read_const_exprs ctxt raw_cs)
haftmann@69624
   490
    ((map o apfst o apsnd o Option.map) prep_destination seris);
haftmann@38918
   491
haftmann@59099
   492
fun produce_code ctxt all_public cs target_name some_width some_module_name args =
haftmann@38918
   493
  let
haftmann@63159
   494
    val program = Code_Thingol.consts_program ctxt cs;
haftmann@59099
   495
  in produce_code_for ctxt target_name some_width some_module_name args program all_public (map Constant cs) end;
haftmann@38929
   496
haftmann@59099
   497
fun present_code ctxt cs syms target_name some_width some_module_name args =
haftmann@38929
   498
  let
haftmann@63159
   499
    val program = Code_Thingol.consts_program ctxt cs;
haftmann@59099
   500
  in present_code_for ctxt target_name some_width some_module_name args program (map Constant cs, syms) end;
haftmann@34021
   501
haftmann@55757
   502
fun check_code ctxt all_public cs seris =
haftmann@37824
   503
  let
haftmann@63159
   504
    val program = Code_Thingol.consts_program ctxt cs;
haftmann@59099
   505
    val _ = map (fn ((target_name, strict), args) =>
haftmann@59099
   506
      check_code_for ctxt target_name strict args program all_public (map Constant cs)) seris;
haftmann@37824
   507
  in () end;
haftmann@37824
   508
haftmann@55757
   509
fun check_code_cmd all_public raw_cs seris ctxt =
haftmann@55757
   510
  check_code ctxt all_public
haftmann@55757
   511
    (Code_Thingol.read_const_exprs ctxt raw_cs) seris;
haftmann@37824
   512
haftmann@39480
   513
local
haftmann@39480
   514
haftmann@39480
   515
val parse_const_terms = Scan.repeat1 Args.term
haftmann@55757
   516
  >> (fn ts => fn ctxt => map (Code.check_const (Proof_Context.theory_of ctxt)) ts);
haftmann@39480
   517
haftmann@55147
   518
fun parse_names category parse internalize mark_symbol =
haftmann@39480
   519
  Scan.lift (Args.parens (Args.$$$ category)) |-- Scan.repeat1 parse
haftmann@55757
   520
  >> (fn xs => fn ctxt => map (mark_symbol o internalize ctxt) xs);
wenzelm@43564
   521
haftmann@39480
   522
val parse_consts = parse_names "consts" Args.term
haftmann@55757
   523
  (Code.check_const o Proof_Context.theory_of) Constant;
haftmann@39480
   524
haftmann@39480
   525
val parse_types = parse_names "types" (Scan.lift Args.name)
haftmann@55757
   526
  (Sign.intern_type o Proof_Context.theory_of) Type_Constructor;
haftmann@39480
   527
haftmann@39480
   528
val parse_classes = parse_names "classes" (Scan.lift Args.name)
haftmann@55757
   529
  (Sign.intern_class o Proof_Context.theory_of) Type_Class;
haftmann@39480
   530
haftmann@39480
   531
val parse_instances = parse_names "instances" (Scan.lift (Args.name --| Args.$$$ "::" -- Args.name))
haftmann@55757
   532
  (fn ctxt => fn (raw_tyco, raw_class) =>
haftmann@55757
   533
    let
haftmann@55757
   534
      val thy = Proof_Context.theory_of ctxt;
haftmann@55757
   535
    in (Sign.intern_class thy raw_tyco, Sign.intern_type thy raw_class) end) Class_Instance;
haftmann@39480
   536
haftmann@39480
   537
in
haftmann@39480
   538
haftmann@59323
   539
val _ = Theory.setup
wenzelm@69593
   540
  (Thy_Output.antiquotation_raw \<^binding>\<open>code_stmts\<close>
wenzelm@43564
   541
    (parse_const_terms --
wenzelm@43564
   542
      Scan.repeat (parse_consts || parse_types || parse_classes || parse_instances)
wenzelm@43564
   543
      -- Scan.lift (Args.parens (Args.name -- Scan.option Parse.int)))
haftmann@69623
   544
    (fn ctxt => fn ((read_constants, reads_statements), (target_name, some_width)) =>
wenzelm@67463
   545
      Latex.string
haftmann@69623
   546
        (Latex.output_ascii (present_code ctxt (read_constants ctxt)
haftmann@69623
   547
          (maps (fn read_statements => read_statements ctxt) reads_statements)
haftmann@69623
   548
          target_name "Example" some_width []))));
haftmann@39480
   549
haftmann@39480
   550
end;
haftmann@39480
   551
haftmann@34021
   552
haftmann@28054
   553
(** serializer configuration **)
haftmann@27000
   554
haftmann@52138
   555
(* reserved symbol names *)
haftmann@52138
   556
haftmann@59099
   557
fun add_reserved target_name sym thy =
haftmann@52138
   558
  let
haftmann@59103
   559
    val (_, (_, data)) = join_ancestry thy target_name;
haftmann@59103
   560
    val _ = if member (op =) (Code_Printer.the_reserved data) sym
haftmann@52138
   561
      then error ("Reserved symbol " ^ quote sym ^ " already declared")
haftmann@52138
   562
      else ();
haftmann@52138
   563
  in
haftmann@52138
   564
    thy
haftmann@59099
   565
    |> map_reserved target_name (insert (op =) sym)
haftmann@52138
   566
  end;
haftmann@52138
   567
haftmann@52138
   568
haftmann@52377
   569
(* checking of syntax *)
haftmann@52138
   570
haftmann@59099
   571
fun check_const_syntax ctxt target_name c syn =
haftmann@55757
   572
  if Code_Printer.requires_args syn > Code.args_number (Proof_Context.theory_of ctxt) c
haftmann@52137
   573
  then error ("Too many arguments in syntax for constant " ^ quote c)
haftmann@59099
   574
  else Code_Printer.prep_const_syntax (Proof_Context.theory_of ctxt) (the_literals ctxt target_name) c syn;
haftmann@52137
   575
haftmann@59099
   576
fun check_tyco_syntax ctxt target_name tyco syn =
haftmann@55757
   577
  if fst syn <> Sign.arity_number (Proof_Context.theory_of ctxt) tyco
haftmann@52137
   578
  then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
haftmann@52137
   579
  else syn;
haftmann@34071
   580
haftmann@52138
   581
haftmann@52138
   582
(* custom symbol names *)
haftmann@52138
   583
wenzelm@52218
   584
fun arrange_name_decls x =
haftmann@52138
   585
  let
haftmann@52138
   586
    fun arrange is_module (sym, target_names) = map (fn (target, some_name) =>
haftmann@52138
   587
      (target, (sym, Option.map (check_name is_module) some_name))) target_names;
haftmann@52138
   588
  in
haftmann@52138
   589
    Code_Symbol.maps_attr' (arrange false) (arrange false) (arrange false)
wenzelm@52218
   590
      (arrange false) (arrange false) (arrange true) x
haftmann@52138
   591
  end;
haftmann@52138
   592
haftmann@69485
   593
fun cert_name_decls ctxt = cert_syms' ctxt #> arrange_name_decls;
haftmann@52138
   594
haftmann@69485
   595
fun read_name_decls ctxt = read_syms' ctxt #> arrange_name_decls;
haftmann@52138
   596
haftmann@59099
   597
fun set_identifier (target_name, sym_name) = map_identifiers target_name (Code_Symbol.set_data sym_name);
haftmann@52138
   598
haftmann@52138
   599
fun gen_set_identifiers prep_name_decl raw_name_decls thy =
haftmann@55757
   600
  fold set_identifier (prep_name_decl (Proof_Context.init_global thy) raw_name_decls) thy;
haftmann@52138
   601
haftmann@52138
   602
val set_identifiers = gen_set_identifiers cert_name_decls;
haftmann@52138
   603
val set_identifiers_cmd = gen_set_identifiers read_name_decls;
haftmann@52138
   604
haftmann@52138
   605
haftmann@52138
   606
(* custom printings *)
haftmann@52138
   607
haftmann@69485
   608
fun arrange_printings prep_syms ctxt =
haftmann@52137
   609
  let
haftmann@52137
   610
    fun arrange check (sym, target_syns) =
haftmann@59099
   611
      map (fn (target_name, some_syn) =>
haftmann@59099
   612
        (target_name, (sym, Option.map (check ctxt target_name sym) some_syn))) target_syns;
haftmann@52137
   613
  in
haftmann@52137
   614
    Code_Symbol.maps_attr'
haftmann@52137
   615
      (arrange check_const_syntax) (arrange check_tyco_syntax)
haftmann@55149
   616
        (arrange ((K o K o K) I)) (arrange ((K o K o K) I)) (arrange ((K o K o K) I))
haftmann@69485
   617
        (arrange (fn ctxt => fn _ => fn _ => fn (raw_content, raw_syms) =>
wenzelm@59430
   618
          (Pretty.blk (0, Pretty.fbreaks (map Code_Printer.str (split_lines raw_content))),
haftmann@69485
   619
            map (prep_syms ctxt) raw_syms)))
haftmann@52137
   620
  end;
haftmann@52137
   621
haftmann@69485
   622
fun cert_printings ctxt = cert_syms' ctxt #> arrange_printings cert_syms ctxt;
haftmann@24219
   623
haftmann@69485
   624
fun read_printings ctxt = read_syms' ctxt #> arrange_printings read_syms ctxt;
haftmann@52137
   625
haftmann@59099
   626
fun set_printing (target_name, sym_syn) = map_printings target_name (Code_Symbol.set_data sym_syn);
haftmann@52137
   627
haftmann@52137
   628
fun gen_set_printings prep_print_decl raw_print_decls thy =
haftmann@55757
   629
  fold set_printing (prep_print_decl (Proof_Context.init_global thy) raw_print_decls) thy;
haftmann@52137
   630
haftmann@52137
   631
val set_printings = gen_set_printings cert_printings;
haftmann@52137
   632
val set_printings_cmd = gen_set_printings read_printings;
haftmann@52137
   633
haftmann@52137
   634
haftmann@27103
   635
(* concrete syntax *)
haftmann@27000
   636
haftmann@28054
   637
fun parse_args f args =
wenzelm@36959
   638
  case Scan.read Token.stopper f args
haftmann@28054
   639
   of SOME x => x
haftmann@28054
   640
    | NONE => error "Bad serializer arguments";
haftmann@28054
   641
haftmann@28054
   642
haftmann@27304
   643
(** Isar setup **)
haftmann@27103
   644
haftmann@69485
   645
val (constantK, type_constructorK, type_classK, class_relationK, class_instanceK, code_moduleK) =
wenzelm@69593
   646
  (\<^keyword>\<open>constant\<close>, \<^keyword>\<open>type_constructor\<close>, \<^keyword>\<open>type_class\<close>,
wenzelm@69593
   647
    \<^keyword>\<open>class_relation\<close>, \<^keyword>\<open>class_instance\<close>, \<^keyword>\<open>code_module\<close>);
haftmann@69485
   648
haftmann@69485
   649
local
haftmann@69485
   650
haftmann@69485
   651
val parse_constants = constantK |-- Scan.repeat1 Parse.term >> map Constant;
haftmann@69485
   652
haftmann@69485
   653
val parse_type_constructors = type_constructorK |-- Scan.repeat1 Parse.type_const >> map Type_Constructor;
haftmann@69485
   654
haftmann@69485
   655
val parse_classes = type_classK |-- Scan.repeat1 Parse.class >> map Type_Class;
haftmann@69485
   656
haftmann@69485
   657
val parse_class_relations = class_relationK |-- Scan.repeat1 parse_classrel_ident >> map Class_Relation;
haftmann@69485
   658
haftmann@69485
   659
val parse_instances = class_instanceK |-- Scan.repeat1 parse_inst_ident >> map Class_Instance;
haftmann@69485
   660
haftmann@69485
   661
in
haftmann@69485
   662
haftmann@69485
   663
val parse_symbols = Scan.repeat1 (parse_constants || parse_type_constructors || parse_classes
haftmann@69485
   664
  || parse_class_relations || parse_instances) >> flat;
haftmann@69485
   665
haftmann@69485
   666
end;
haftmann@69485
   667
haftmann@52137
   668
fun parse_single_symbol_pragma parse_keyword parse_isa parse_target =
wenzelm@69593
   669
  parse_keyword |-- Parse.!!! (parse_isa --| (\<^keyword>\<open>\<rightharpoonup>\<close> || \<^keyword>\<open>=>\<close>)
wenzelm@69593
   670
    -- Parse.and_list1 (\<^keyword>\<open>(\<close> |-- (Parse.name --| \<^keyword>\<open>)\<close> -- Scan.option parse_target)));
haftmann@52137
   671
haftmann@52137
   672
fun parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
haftmann@69485
   673
  parse_single_symbol_pragma constantK Parse.term parse_const
haftmann@55150
   674
    >> Constant
haftmann@69485
   675
  || parse_single_symbol_pragma type_constructorK Parse.type_const parse_tyco
haftmann@55150
   676
    >> Type_Constructor
haftmann@69485
   677
  || parse_single_symbol_pragma type_classK Parse.class parse_class
haftmann@55150
   678
    >> Type_Class
haftmann@69485
   679
  || parse_single_symbol_pragma class_relationK parse_classrel_ident parse_classrel
haftmann@55150
   680
    >> Class_Relation
haftmann@69485
   681
  || parse_single_symbol_pragma class_instanceK parse_inst_ident parse_inst
haftmann@55150
   682
    >> Class_Instance
haftmann@69485
   683
  || parse_single_symbol_pragma code_moduleK Parse.name parse_module
haftmann@52137
   684
    >> Code_Symbol.Module;
haftmann@52137
   685
haftmann@52137
   686
fun parse_symbol_pragmas parse_const parse_tyco parse_class parse_classrel parse_inst parse_module =
haftmann@52137
   687
  Parse.enum1 "|" (Parse.group (fn () => "code symbol pragma")
haftmann@52137
   688
    (parse_symbol_pragma parse_const parse_tyco parse_class parse_classrel parse_inst parse_module));
haftmann@52137
   689
wenzelm@69593
   690
val code_expr_argsP = Scan.optional (\<^keyword>\<open>(\<close> |-- Parse.args --| \<^keyword>\<open>)\<close>) [];
haftmann@52138
   691
wenzelm@69645
   692
fun code_expr_inP (all_public, raw_cs) =
wenzelm@69593
   693
  Scan.repeat (\<^keyword>\<open>in\<close> |-- Parse.!!! (Parse.name
wenzelm@69593
   694
    -- Scan.optional (\<^keyword>\<open>module_name\<close> |-- Parse.name) ""
haftmann@69624
   695
    -- Scan.option (\<^keyword>\<open>file\<close> |-- Parse.position Parse.path)
haftmann@52434
   696
    -- code_expr_argsP))
haftmann@55683
   697
      >> (fn seri_args => export_code_cmd all_public raw_cs seri_args);
haftmann@52434
   698
wenzelm@69645
   699
fun code_expr_checkingP (all_public, raw_cs) =
wenzelm@69593
   700
  (\<^keyword>\<open>checking\<close> |-- Parse.!!!
wenzelm@69645
   701
    (Scan.repeat (Parse.name -- (Scan.optional (\<^keyword>\<open>?\<close> >> K false) true) -- code_expr_argsP)))
haftmann@55683
   702
      >> (fn seri_args => check_code_cmd all_public raw_cs seri_args);
haftmann@52434
   703
wenzelm@69645
   704
val code_exprP =
wenzelm@69645
   705
  (Scan.optional (\<^keyword>\<open>open\<close> >> K true) false -- Scan.repeat1 Parse.term)
wenzelm@69645
   706
    :|-- (fn args => (code_expr_checkingP args || code_expr_inP args));
haftmann@52138
   707
haftmann@52138
   708
val _ =
wenzelm@69593
   709
  Outer_Syntax.command \<^command_keyword>\<open>code_reserved\<close>
haftmann@52138
   710
    "declare words as reserved for target language"
haftmann@52138
   711
    (Parse.name -- Scan.repeat1 Parse.name
haftmann@52138
   712
      >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds));
haftmann@52138
   713
haftmann@52138
   714
val _ =
wenzelm@69593
   715
  Outer_Syntax.command \<^command_keyword>\<open>code_identifier\<close> "declare mandatory names for code symbols"
haftmann@52138
   716
    (parse_symbol_pragmas Parse.name Parse.name Parse.name Parse.name Parse.name Parse.name
haftmann@52138
   717
      >> (Toplevel.theory o fold set_identifiers_cmd));
haftmann@52138
   718
haftmann@52138
   719
val _ =
wenzelm@69593
   720
  Outer_Syntax.command \<^command_keyword>\<open>code_printing\<close> "declare dedicated printing for code symbols"
haftmann@52137
   721
    (parse_symbol_pragmas (Code_Printer.parse_const_syntax) (Code_Printer.parse_tyco_syntax)
haftmann@52137
   722
      Parse.string (Parse.minus >> K ()) (Parse.minus >> K ())
wenzelm@69593
   723
      (Parse.text -- Scan.optional (\<^keyword>\<open>for\<close> |-- parse_symbols) [])
haftmann@52137
   724
      >> (Toplevel.theory o fold set_printings_cmd));
haftmann@52137
   725
haftmann@52137
   726
val _ =
wenzelm@69593
   727
  Outer_Syntax.command \<^command_keyword>\<open>export_code\<close> "generate executable code for constants"
haftmann@55757
   728
    (Parse.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.context_of)));
haftmann@30494
   729
haftmann@24219
   730
end; (*struct*)