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