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