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