src/Tools/Code/code_target.ML
author haftmann
Mon Dec 21 08:32:04 2009 +0100 (2009-12-21)
changeset 34152 8e5b596d8c73
parent 34081 bb01fd325c6b
child 34173 458ced35abb8
permissions -rw-r--r--
clarified various user-defined syntax issues
     1 (*  Title:      Tools/code/code_target.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Serializer from intermediate language ("Thin-gol") to target languages.
     5 *)
     6 
     7 signature CODE_TARGET =
     8 sig
     9   type serializer
    10   type literals = Code_Printer.literals
    11   val add_target: string * (serializer * literals) -> theory -> theory
    12   val extend_target: string *
    13       (string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program))
    14     -> theory -> theory
    15   val assert_target: theory -> string -> string
    16 
    17   type destination
    18   type serialization
    19   val parse_args: (OuterLex.token list -> 'a * OuterLex.token list)
    20     -> OuterLex.token list -> 'a
    21   val stmt_names_of_destination: destination -> string list
    22   val mk_serialization: string -> ('a -> unit) option
    23     -> (Path.T option -> 'a -> unit)
    24     -> ('a -> string * string option list)
    25     -> 'a -> serialization
    26   val serialize: theory -> string -> int option -> string option -> OuterLex.token list
    27     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> serialization
    28   val serialize_custom: theory -> string * (serializer * literals)
    29     -> Code_Thingol.naming -> Code_Thingol.program -> string list -> string * string option list
    30   val the_literals: theory -> string -> literals
    31   val compile: serialization -> unit
    32   val export: serialization -> unit
    33   val file: Path.T -> serialization -> unit
    34   val string: string list -> serialization -> string
    35   val code_of: theory -> string -> int option -> string
    36     -> string list -> (Code_Thingol.naming -> string list) -> string
    37   val set_default_code_width: int -> theory -> theory
    38   val shell_command: string (*theory name*) -> string (*export_code expr*) -> unit
    39 
    40   val allow_abort: string -> theory -> theory
    41   type tyco_syntax = Code_Printer.tyco_syntax
    42   type proto_const_syntax = Code_Printer.proto_const_syntax
    43   val add_syntax_class: string -> class -> string option -> theory -> theory
    44   val add_syntax_inst: string -> class * string -> unit option -> theory -> theory
    45   val add_syntax_tyco: string -> string -> tyco_syntax option -> theory -> theory
    46   val add_syntax_const: string -> string -> proto_const_syntax option -> theory -> theory
    47   val add_reserved: string -> string -> theory -> theory
    48   val add_include: string -> string * (string * string list) option -> theory -> theory
    49 end;
    50 
    51 structure Code_Target : CODE_TARGET =
    52 struct
    53 
    54 open Basic_Code_Thingol;
    55 
    56 type literals = Code_Printer.literals;
    57 type tyco_syntax = Code_Printer.tyco_syntax;
    58 type proto_const_syntax = Code_Printer.proto_const_syntax;
    59 
    60 
    61 (** basics **)
    62 
    63 datatype destination = Compile | Export | File of Path.T | String of string list;
    64 type serialization = destination -> (string * string option list) option;
    65 
    66 fun compile f = (f Compile; ());
    67 fun export f = (f Export; ());
    68 fun file p f = (f (File p); ());
    69 fun string stmts f = fst (the (f (String stmts)));
    70 
    71 fun stmt_names_of_destination (String stmts) = stmts
    72   | stmt_names_of_destination _ = [];
    73 
    74 fun mk_serialization target (SOME comp) _ _ code Compile = (comp code; NONE)
    75   | mk_serialization target NONE _ _ _ Compile = error (target ^ ": no internal compilation")
    76   | mk_serialization target _ output _ code Export = (output NONE code ; NONE)
    77   | mk_serialization target _ output _ code (File file) = (output (SOME file) code; NONE)
    78   | mk_serialization target _ _ string code (String _) = SOME (string code);
    79 
    80 
    81 (** theory data **)
    82 
    83 datatype name_syntax_table = NameSyntaxTable of {
    84   class: string Symtab.table,
    85   instance: unit Symreltab.table,
    86   tyco: Code_Printer.tyco_syntax Symtab.table,
    87   const: Code_Printer.proto_const_syntax Symtab.table
    88 };
    89 
    90 fun mk_name_syntax_table ((class, instance), (tyco, const)) =
    91   NameSyntaxTable { class = class, instance = instance, tyco = tyco, const = const };
    92 fun map_name_syntax_table f (NameSyntaxTable { class, instance, tyco, const }) =
    93   mk_name_syntax_table (f ((class, instance), (tyco, const)));
    94 fun merge_name_syntax_table (NameSyntaxTable { class = class1, instance = instance1, tyco = tyco1, const = const1 },
    95     NameSyntaxTable { class = class2, instance = instance2, tyco = tyco2, const = const2 }) =
    96   mk_name_syntax_table (
    97     (Symtab.join (K snd) (class1, class2),
    98        Symreltab.join (K snd) (instance1, instance2)),
    99     (Symtab.join (K snd) (tyco1, tyco2),
   100        Symtab.join (K snd) (const1, const2))
   101   );
   102 
   103 type serializer =
   104   string option                         (*module name*)
   105   -> OuterLex.token list                (*arguments*)
   106   -> (string -> string)                 (*labelled_name*)
   107   -> string list                        (*reserved symbols*)
   108   -> (string * Pretty.T) list           (*includes*)
   109   -> (string -> string option)          (*module aliasses*)
   110   -> (string -> string option)          (*class syntax*)
   111   -> (string -> Code_Printer.tyco_syntax option)
   112   -> (string -> Code_Printer.const_syntax option)
   113   -> ((Pretty.T -> string) * (Pretty.T -> unit))
   114   -> Code_Thingol.program
   115   -> string list                        (*selected statements*)
   116   -> serialization;
   117 
   118 datatype serializer_entry = Serializer of serializer * Code_Printer.literals
   119   | Extends of string * (Code_Thingol.naming -> Code_Thingol.program -> Code_Thingol.program);
   120 
   121 datatype target = Target of {
   122   serial: serial,
   123   serializer: serializer_entry,
   124   reserved: string list,
   125   includes: (Pretty.T * string list) Symtab.table,
   126   name_syntax_table: name_syntax_table,
   127   module_alias: string Symtab.table
   128 };
   129 
   130 fun make_target ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))) =
   131   Target { serial = serial, serializer = serializer, reserved = reserved, 
   132     includes = includes, name_syntax_table = name_syntax_table, module_alias = module_alias };
   133 fun map_target f ( Target { serial, serializer, reserved, includes, name_syntax_table, module_alias } ) =
   134   make_target (f ((serial, serializer), ((reserved, includes), (name_syntax_table, module_alias))));
   135 fun merge_target strict target (Target { serial = serial1, serializer = serializer,
   136   reserved = reserved1, includes = includes1,
   137   name_syntax_table = name_syntax_table1, module_alias = module_alias1 },
   138     Target { serial = serial2, serializer = _,
   139       reserved = reserved2, includes = includes2,
   140       name_syntax_table = name_syntax_table2, module_alias = module_alias2 }) =
   141   if serial1 = serial2 orelse not strict then
   142     make_target ((serial1, serializer),
   143       ((merge (op =) (reserved1, reserved2), Symtab.merge (op =) (includes1, includes2)),
   144         (merge_name_syntax_table (name_syntax_table1, name_syntax_table2),
   145           Symtab.join (K snd) (module_alias1, module_alias2))
   146     ))
   147   else
   148     error ("Incompatible serializers: " ^ quote target);
   149 
   150 fun the_serializer (Target { serializer, ... }) = serializer;
   151 fun the_reserved (Target { reserved, ... }) = reserved;
   152 fun the_includes (Target { includes, ... }) = includes;
   153 fun the_name_syntax (Target { name_syntax_table = NameSyntaxTable x, ... }) = x;
   154 fun the_module_alias (Target { module_alias , ... }) = module_alias;
   155 
   156 structure CodeTargetData = Theory_Data
   157 (
   158   type T = (target Symtab.table * string list) * int;
   159   val empty = ((Symtab.empty, []), 80);
   160   val extend = I;
   161   fun merge (((target1, exc1), width1), ((target2, exc2), width2)) : T =
   162     ((Symtab.join (merge_target true) (target1, target2),
   163       Library.merge (op =) (exc1, exc2)), Int.max (width1, width2));
   164 );
   165 
   166 val abort_allowed = snd o fst o CodeTargetData.get;
   167 
   168 val the_default_width = snd o CodeTargetData.get;
   169 
   170 fun assert_target thy target = if Symtab.defined ((fst o fst) (CodeTargetData.get thy)) target
   171   then target
   172   else error ("Unknown code target language: " ^ quote target);
   173 
   174 fun put_target (target, seri) thy =
   175   let
   176     val lookup_target = Symtab.lookup ((fst o fst) (CodeTargetData.get thy));
   177     val _ = case seri
   178      of Extends (super, _) => if is_some (lookup_target super) then ()
   179           else error ("Unknown code target language: " ^ quote super)
   180       | _ => ();
   181     val overwriting = case (Option.map the_serializer o lookup_target) target
   182      of NONE => false
   183       | SOME (Extends _) => true
   184       | SOME (Serializer _) => (case seri
   185          of Extends _ => error ("Will not overwrite existing target " ^ quote target)
   186           | _ => true);
   187     val _ = if overwriting
   188       then warning ("Overwriting existing target " ^ quote target)
   189       else (); 
   190   in
   191     thy
   192     |> (CodeTargetData.map o apfst o apfst oo Symtab.map_default)
   193           (target, make_target ((serial (), seri), (([], Symtab.empty),
   194             (mk_name_syntax_table ((Symtab.empty, Symreltab.empty), (Symtab.empty, Symtab.empty)),
   195               Symtab.empty))))
   196           ((map_target o apfst o apsnd o K) seri)
   197   end;
   198 
   199 fun add_target (target, seri) = put_target (target, Serializer seri);
   200 fun extend_target (target, (super, modify)) =
   201   put_target (target, Extends (super, modify));
   202 
   203 fun map_target_data target f thy =
   204   let
   205     val _ = assert_target thy target;
   206   in
   207     thy
   208     |> (CodeTargetData.map o apfst o apfst o Symtab.map_entry target o map_target) f
   209   end;
   210 
   211 fun map_reserved target =
   212   map_target_data target o apsnd o apfst o apfst;
   213 fun map_includes target =
   214   map_target_data target o apsnd o apfst o apsnd;
   215 fun map_name_syntax target =
   216   map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table;
   217 fun map_module_alias target =
   218   map_target_data target o apsnd o apsnd o apsnd;
   219 
   220 fun set_default_code_width k = (CodeTargetData.map o apsnd) (K k);
   221 
   222 
   223 (** serializer usage **)
   224 
   225 (* montage *)
   226 
   227 fun the_literals thy =
   228   let
   229     val ((targets, _), _) = CodeTargetData.get thy;
   230     fun literals target = case Symtab.lookup targets target
   231      of SOME data => (case the_serializer data
   232          of Serializer (_, literals) => literals
   233           | Extends (super, _) => literals super)
   234       | NONE => error ("Unknown code target language: " ^ quote target);
   235   in literals end;
   236 
   237 local
   238 
   239 fun activate_syntax lookup_name src_tab = Symtab.empty
   240   |> fold_map (fn thing_identifier => fn tab => case lookup_name thing_identifier
   241        of SOME name => (SOME name,
   242             Symtab.update_new (name, the (Symtab.lookup src_tab thing_identifier)) tab)
   243         | NONE => (NONE, tab)) (Symtab.keys src_tab)
   244   |>> map_filter I;
   245 
   246 fun activate_const_syntax thy literals src_tab naming = (Symtab.empty, naming)
   247   |> fold_map (fn thing_identifier => fn (tab, naming) =>
   248       case Code_Thingol.lookup_const naming thing_identifier
   249        of SOME name => let
   250               val (syn, naming') = Code_Printer.activate_const_syntax thy
   251                 literals (the (Symtab.lookup src_tab thing_identifier)) naming
   252             in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
   253         | NONE => (NONE, (tab, naming))) (Symtab.keys src_tab)
   254   |>> map_filter I;
   255 
   256 fun invoke_serializer thy abortable serializer literals reserved abs_includes 
   257     module_alias class instance tyco const module width args naming program2 names1 =
   258   let
   259     val (names_class, class') =
   260       activate_syntax (Code_Thingol.lookup_class naming) class;
   261     val names_inst = map_filter (Code_Thingol.lookup_instance naming)
   262       (Symreltab.keys instance);
   263     val (names_tyco, tyco') =
   264       activate_syntax (Code_Thingol.lookup_tyco naming) tyco;
   265     val (names_const, (const', _)) =
   266       activate_const_syntax thy literals const naming;
   267     val names_hidden = names_class @ names_inst @ names_tyco @ names_const;
   268     val names2 = subtract (op =) names_hidden names1;
   269     val program3 = Graph.subgraph (not o member (op =) names_hidden) program2;
   270     val names_all = Graph.all_succs program3 names2;
   271     val includes = abs_includes names_all;
   272     val program4 = Graph.subgraph (member (op =) names_all) program3;
   273     val empty_funs = filter_out (member (op =) abortable)
   274       (Code_Thingol.empty_funs program3);
   275     val _ = if null empty_funs then () else error ("No code equations for "
   276       ^ commas (map (Sign.extern_const thy) empty_funs));
   277   in
   278     serializer module args (Code_Thingol.labelled_name thy program2) reserved includes
   279       (Symtab.lookup module_alias) (Symtab.lookup class')
   280       (Symtab.lookup tyco') (Symtab.lookup const')
   281       (Code_Printer.string_of_pretty width, Code_Printer.writeln_pretty width)
   282       program4 names2
   283   end;
   284 
   285 fun mount_serializer thy alt_serializer target some_width module args naming program names =
   286   let
   287     val ((targets, abortable), default_width) = CodeTargetData.get thy;
   288     fun collapse_hierarchy target =
   289       let
   290         val data = case Symtab.lookup targets target
   291          of SOME data => data
   292           | NONE => error ("Unknown code target language: " ^ quote target);
   293       in case the_serializer data
   294        of Serializer _ => (I, data)
   295         | Extends (super, modify) => let
   296             val (modify', data') = collapse_hierarchy super
   297           in (modify' #> modify naming, merge_target false target (data', data)) end
   298       end;
   299     val (modify, data) = collapse_hierarchy target;
   300     val (serializer, _) = the_default (case the_serializer data
   301      of Serializer seri => seri) alt_serializer;
   302     val reserved = the_reserved data;
   303     fun select_include names_all (name, (content, cs)) =
   304       if null cs then SOME (name, content)
   305       else if exists (fn c => case Code_Thingol.lookup_const naming c
   306        of SOME name => member (op =) names_all name
   307         | NONE => false) cs
   308       then SOME (name, content) else NONE;
   309     fun includes names_all = map_filter (select_include names_all)
   310       ((Symtab.dest o the_includes) data);
   311     val module_alias = the_module_alias data;
   312     val { class, instance, tyco, const } = the_name_syntax data;
   313     val literals = the_literals thy target;
   314     val width = the_default default_width some_width;
   315   in
   316     invoke_serializer thy abortable serializer literals reserved
   317       includes module_alias class instance tyco const module width args naming (modify program) names
   318   end;
   319 
   320 in
   321 
   322 fun serialize thy = mount_serializer thy NONE;
   323 
   324 fun serialize_custom thy (target_name, seri) naming program names =
   325   mount_serializer thy (SOME seri) target_name NONE NONE [] naming program names (String [])
   326   |> the;
   327 
   328 end; (* local *)
   329 
   330 
   331 (* code presentation *)
   332 
   333 fun code_of thy target some_width module_name cs names_stmt =
   334   let
   335     val (names_cs, (naming, program)) = Code_Thingol.consts_program thy cs;
   336   in
   337     string (names_stmt naming)
   338       (serialize thy target some_width (SOME module_name) [] naming program names_cs)
   339   end;
   340 
   341 
   342 (* code generation *)
   343 
   344 fun transitivly_non_empty_funs thy naming program =
   345   let
   346     val cs = subtract (op =) (abort_allowed thy) (Code_Thingol.empty_funs program);
   347     val names = map_filter (Code_Thingol.lookup_const naming) cs;
   348   in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end;
   349 
   350 fun read_const_exprs thy cs =
   351   let
   352     val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs;
   353     val (names3, (naming, program)) = Code_Thingol.consts_program thy cs2;
   354     val names4 = transitivly_non_empty_funs thy naming program;
   355     val cs5 = map_filter
   356       (fn (c, name) => if member (op =) names4 name then SOME c else NONE) (cs2 ~~ names3);
   357   in fold (insert (op =)) cs5 cs1 end;
   358 
   359 fun cached_program thy = 
   360   let
   361     val (naming, program) = Code_Thingol.cached_program thy;
   362   in (transitivly_non_empty_funs thy naming program, (naming, program)) end
   363 
   364 fun export_code thy cs seris =
   365   let
   366     val (cs', (naming, program)) = if null cs then cached_program thy
   367       else Code_Thingol.consts_program thy cs;
   368     fun mk_seri_dest dest = case dest
   369      of NONE => compile
   370       | SOME "-" => export
   371       | SOME f => file (Path.explode f)
   372     val _ = map (fn (((target, module), dest), args) =>
   373       (mk_seri_dest dest (serialize thy target NONE module args naming program cs'))) seris;
   374   in () end;
   375 
   376 fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
   377 
   378 
   379 (** serializer configuration **)
   380 
   381 (* data access *)
   382 
   383 fun cert_class thy class =
   384   let
   385     val _ = AxClass.get_info thy class;
   386   in class end;
   387 
   388 fun read_class thy = cert_class thy o Sign.intern_class thy;
   389 
   390 fun cert_tyco thy tyco =
   391   let
   392     val _ = if Sign.declared_tyname thy tyco then ()
   393       else error ("No such type constructor: " ^ quote tyco);
   394   in tyco end;
   395 
   396 fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
   397 
   398 fun cert_inst thy (class, tyco) =
   399   (cert_class thy class, cert_tyco thy tyco);
   400 
   401 fun read_inst thy (raw_tyco, raw_class) =
   402   (read_class thy raw_class, read_tyco thy raw_tyco);
   403 
   404 fun gen_add_syntax upd del mapp check_x check_raw_syn prep_syn prep_x target raw_x some_raw_syn thy =
   405   let
   406     val x = prep_x thy raw_x |> tap (check_x thy);
   407     fun prep_check raw_syn = prep_syn (raw_syn |> tap (check_raw_syn thy target));
   408   in case some_raw_syn
   409    of SOME raw_syn => (map_name_syntax target o mapp) (upd (x, prep_check raw_syn)) thy
   410     | NONE => (map_name_syntax target o mapp) (del x) thy
   411   end;
   412 
   413 fun gen_add_syntax_class prep_class =
   414   gen_add_syntax Symtab.update Symtab.delete_safe (apfst o apfst) (K I) ((K o K o K) ()) I prep_class;
   415 
   416 fun gen_add_syntax_inst prep_inst =
   417   gen_add_syntax Symreltab.update Symreltab.delete_safe (apfst o apsnd) (K I) ((K o K o K) ()) I prep_inst;
   418 
   419 fun gen_add_syntax_tyco prep_tyco =
   420   gen_add_syntax Symtab.update Symtab.delete_safe (apsnd o apfst)
   421     (fn thy => fn tyco => fn (n, _) => if n <> Sign.arity_number thy tyco
   422       then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
   423       else ()) ((K o K o K) ()) I prep_tyco;
   424 
   425 fun gen_add_syntax_const prep_const check_raw_syn prep_syn =
   426   gen_add_syntax Symtab.update Symtab.delete_safe (apsnd o apsnd)
   427     (fn thy => fn c => fn (n, _) => if n > Code.args_number thy c
   428       then error ("Too many arguments in syntax for constant " ^ quote c)
   429       else ()) check_raw_syn prep_syn prep_const;
   430 
   431 fun add_reserved target =
   432   let
   433     fun add sym syms = if member (op =) syms sym
   434       then error ("Reserved symbol " ^ quote sym ^ " already declared")
   435       else insert (op =) sym syms
   436   in map_reserved target o add end;
   437 
   438 fun gen_add_include read_const target args thy =
   439   let
   440     fun add (name, SOME (content, raw_cs)) incls =
   441           let
   442             val _ = if Symtab.defined incls name
   443               then warning ("Overwriting existing include " ^ name)
   444               else ();
   445             val cs = map (read_const thy) raw_cs;
   446           in Symtab.update (name, (Code_Printer.str content, cs)) incls end
   447       | add (name, NONE) incls = Symtab.delete name incls;
   448   in map_includes target (add args) thy end;
   449 
   450 val add_include = gen_add_include (K I);
   451 val add_include_cmd = gen_add_include Code.read_const;
   452 
   453 fun add_module_alias target (thyname, modlname) =
   454   let
   455     val xs = Long_Name.explode modlname;
   456     val xs' = map (Name.desymbolize true) xs;
   457   in if xs' = xs
   458     then map_module_alias target (Symtab.update (thyname, modlname))
   459     else error ("Invalid module name: " ^ quote modlname ^ "\n"
   460       ^ "perhaps try " ^ quote (Long_Name.implode xs'))
   461   end;
   462 
   463 fun gen_allow_abort prep_const raw_c thy =
   464   let
   465     val c = prep_const thy raw_c;
   466   in thy |> (CodeTargetData.map o apfst o apsnd) (insert (op =) c) end;
   467 
   468 
   469 (* concrete syntax *)
   470 
   471 local
   472 
   473 structure P = OuterParse
   474 and K = OuterKeyword
   475 
   476 fun zip_list (x::xs) f g =
   477   f
   478   #-> (fn y =>
   479     fold_map (fn x => g |-- f >> pair x) xs
   480     #-> (fn xys => pair ((x, y) :: xys)));
   481 
   482 fun parse_multi_syntax parse_thing parse_syntax =
   483   P.and_list1 parse_thing
   484   #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
   485         (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
   486 
   487 in
   488 
   489 val add_syntax_class = gen_add_syntax_class cert_class;
   490 val add_syntax_inst = gen_add_syntax_inst cert_inst;
   491 val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
   492 val add_syntax_const_simple = gen_add_syntax_const (K I) ((K o K o K) ()) Code_Printer.simple_const_syntax;
   493 val add_syntax_const = gen_add_syntax_const (K I) ((K o K o K) ()) I;
   494 val allow_abort = gen_allow_abort (K I);
   495 val add_reserved = add_reserved;
   496 val add_include = add_include;
   497 
   498 val add_syntax_class_cmd = gen_add_syntax_class read_class;
   499 val add_syntax_inst_cmd = gen_add_syntax_inst read_inst;
   500 val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
   501 val add_syntax_const_simple_cmd = gen_add_syntax_const Code.read_const ((K o K o K) ()) Code_Printer.simple_const_syntax;
   502 val add_syntax_const_cmd = gen_add_syntax_const Code.read_const ((K o K o K) ()) I;
   503 
   504 val allow_abort_cmd = gen_allow_abort Code.read_const;
   505 
   506 fun parse_args f args =
   507   case Scan.read OuterLex.stopper f args
   508    of SOME x => x
   509     | NONE => error "Bad serializer arguments";
   510 
   511 
   512 (** Isar setup **)
   513 
   514 val (inK, module_nameK, fileK) = ("in", "module_name", "file");
   515 
   516 val code_exprP =
   517   (Scan.repeat P.term_group
   518   -- Scan.repeat (P.$$$ inK |-- P.name
   519      -- Scan.option (P.$$$ module_nameK |-- P.name)
   520      -- Scan.option (P.$$$ fileK |-- P.name)
   521      -- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") []
   522   ) >> (fn (raw_cs, seris) => export_code_cmd raw_cs seris));
   523 
   524 val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK];
   525 
   526 val _ =
   527   OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl (
   528     parse_multi_syntax P.xname (Scan.option P.string)
   529     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   530           fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
   531   );
   532 
   533 val _ =
   534   OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl (
   535     parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) (Scan.option (P.minus >> K ()))
   536     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   537           fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
   538   );
   539 
   540 val _ =
   541   OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl (
   542     parse_multi_syntax P.xname (Code_Printer.parse_syntax I)
   543     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   544           fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
   545   );
   546 
   547 val _ =
   548   OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl (
   549     parse_multi_syntax P.term_group (Code_Printer.parse_syntax fst)
   550     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   551       fold (fn (raw_const, syn) => add_syntax_const_simple_cmd target raw_const syn) syns)
   552   );
   553 
   554 val _ =
   555   OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl (
   556     P.name -- Scan.repeat1 P.name
   557     >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
   558   );
   559 
   560 val _ =
   561   OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl (
   562     P.name -- P.name -- (P.text :|-- (fn "-" => Scan.succeed NONE
   563       | s => Scan.optional (P.$$$ "attach" |-- Scan.repeat1 P.term) [] >> pair s >> SOME))
   564     >> (fn ((target, name), content_consts) =>
   565         (Toplevel.theory o add_include_cmd target) (name, content_consts))
   566   );
   567 
   568 val _ =
   569   OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl (
   570     P.name -- Scan.repeat1 (P.name -- P.name)
   571     >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames)
   572   );
   573 
   574 val _ =
   575   OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl (
   576     Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd)
   577   );
   578 
   579 val _ =
   580   OuterSyntax.command "export_code" "generate executable code for constants"
   581     K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
   582 
   583 fun shell_command thyname cmd = Toplevel.program (fn _ =>
   584   (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! code_exprP)
   585     ((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd)
   586    of SOME f => (writeln "Now generating code..."; f (theory thyname))
   587     | NONE => error ("Bad directive " ^ quote cmd)))
   588   handle Runtime.TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
   589 
   590 end; (*local*)
   591 
   592 end; (*struct*)