src/Tools/Code/code_target.ML
author haftmann
Wed Apr 21 15:20:57 2010 +0200 (2010-04-21)
changeset 36271 2ef9dbddfcb8
parent 36121 86b952fc31da
child 36471 5aae37575885
permissions -rw-r--r--
optionally ignore errors during translation of equations
     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 Targets = 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 Targets.get;
   167 
   168 fun assert_target thy target = if Symtab.defined ((fst o fst) (Targets.get thy)) target
   169   then target
   170   else error ("Unknown code target language: " ^ quote target);
   171 
   172 fun put_target (target, seri) thy =
   173   let
   174     val lookup_target = Symtab.lookup ((fst o fst) (Targets.get thy));
   175     val _ = case seri
   176      of Extends (super, _) => if is_some (lookup_target super) then ()
   177           else error ("Unknown code target language: " ^ quote super)
   178       | _ => ();
   179     val overwriting = case (Option.map the_serializer o lookup_target) target
   180      of NONE => false
   181       | SOME (Extends _) => true
   182       | SOME (Serializer _) => (case seri
   183          of Extends _ => error ("Will not overwrite existing target " ^ quote target)
   184           | _ => true);
   185     val _ = if overwriting
   186       then warning ("Overwriting existing target " ^ quote target)
   187       else (); 
   188   in
   189     thy
   190     |> (Targets.map o apfst o apfst o Symtab.update)
   191           (target, make_target ((serial (), seri), (([], Symtab.empty),
   192             (mk_name_syntax_table ((Symtab.empty, Symreltab.empty), (Symtab.empty, Symtab.empty)),
   193               Symtab.empty))))
   194   end;
   195 
   196 fun add_target (target, seri) = put_target (target, Serializer seri);
   197 fun extend_target (target, (super, modify)) =
   198   put_target (target, Extends (super, modify));
   199 
   200 fun map_target_data target f thy =
   201   let
   202     val _ = assert_target thy target;
   203   in
   204     thy
   205     |> (Targets.map o apfst o apfst o Symtab.map_entry target o map_target) f
   206   end;
   207 
   208 fun map_reserved target =
   209   map_target_data target o apsnd o apfst o apfst;
   210 fun map_includes target =
   211   map_target_data target o apsnd o apfst o apsnd;
   212 fun map_name_syntax target =
   213   map_target_data target o apsnd o apsnd o apfst o map_name_syntax_table;
   214 fun map_module_alias target =
   215   map_target_data target o apsnd o apsnd o apsnd;
   216 
   217 fun set_default_code_width k = (Targets.map o apsnd) (K k);
   218 
   219 
   220 (** serializer usage **)
   221 
   222 (* montage *)
   223 
   224 fun the_literals thy =
   225   let
   226     val ((targets, _), _) = Targets.get thy;
   227     fun literals target = case Symtab.lookup targets target
   228      of SOME data => (case the_serializer data
   229          of Serializer (_, literals) => literals
   230           | Extends (super, _) => literals super)
   231       | NONE => error ("Unknown code target language: " ^ quote target);
   232   in literals end;
   233 
   234 local
   235 
   236 fun activate_syntax lookup_name src_tab = Symtab.empty
   237   |> fold_map (fn thing_identifier => fn tab => case lookup_name thing_identifier
   238        of SOME name => (SOME name,
   239             Symtab.update_new (name, the (Symtab.lookup src_tab thing_identifier)) tab)
   240         | NONE => (NONE, tab)) (Symtab.keys src_tab)
   241   |>> map_filter I;
   242 
   243 fun activate_const_syntax thy literals src_tab naming = (Symtab.empty, naming)
   244   |> fold_map (fn thing_identifier => fn (tab, naming) =>
   245       case Code_Thingol.lookup_const naming thing_identifier
   246        of SOME name => let
   247               val (syn, naming') = Code_Printer.activate_const_syntax thy
   248                 literals (the (Symtab.lookup src_tab thing_identifier)) naming
   249             in (SOME name, (Symtab.update_new (name, syn) tab, naming')) end
   250         | NONE => (NONE, (tab, naming))) (Symtab.keys src_tab)
   251   |>> map_filter I;
   252 
   253 fun invoke_serializer thy abortable serializer literals reserved abs_includes 
   254     module_alias class instance tyco const module width args naming program2 names1 =
   255   let
   256     val (names_class, class') =
   257       activate_syntax (Code_Thingol.lookup_class naming) class;
   258     val names_inst = map_filter (Code_Thingol.lookup_instance naming)
   259       (Symreltab.keys instance);
   260     val (names_tyco, tyco') =
   261       activate_syntax (Code_Thingol.lookup_tyco naming) tyco;
   262     val (names_const, (const', _)) =
   263       activate_const_syntax thy literals const naming;
   264     val names_hidden = names_class @ names_inst @ names_tyco @ names_const;
   265     val names2 = subtract (op =) names_hidden names1;
   266     val program3 = Graph.subgraph (not o member (op =) names_hidden) program2;
   267     val names_all = Graph.all_succs program3 names2;
   268     val includes = abs_includes names_all;
   269     val program4 = Graph.subgraph (member (op =) names_all) program3;
   270     val empty_funs = filter_out (member (op =) abortable)
   271       (Code_Thingol.empty_funs program3);
   272     val _ = if null empty_funs then () else error ("No code equations for "
   273       ^ commas (map (Sign.extern_const thy) empty_funs));
   274   in
   275     serializer module args (Code_Thingol.labelled_name thy program2) reserved includes
   276       (Symtab.lookup module_alias) (Symtab.lookup class')
   277       (Symtab.lookup tyco') (Symtab.lookup const')
   278       (Code_Printer.string_of_pretty width, Code_Printer.writeln_pretty width)
   279       program4 names2
   280   end;
   281 
   282 fun mount_serializer thy alt_serializer target some_width module args naming program names =
   283   let
   284     val ((targets, abortable), default_width) = Targets.get thy;
   285     fun collapse_hierarchy target =
   286       let
   287         val data = case Symtab.lookup targets target
   288          of SOME data => data
   289           | NONE => error ("Unknown code target language: " ^ quote target);
   290       in case the_serializer data
   291        of Serializer _ => (I, data)
   292         | Extends (super, modify) => let
   293             val (modify', data') = collapse_hierarchy super
   294           in (modify' #> modify naming, merge_target false target (data', data)) end
   295       end;
   296     val (modify, data) = collapse_hierarchy target;
   297     val (serializer, _) = the_default (case the_serializer data
   298      of Serializer seri => seri) alt_serializer;
   299     val reserved = the_reserved data;
   300     fun select_include names_all (name, (content, cs)) =
   301       if null cs then SOME (name, content)
   302       else if exists (fn c => case Code_Thingol.lookup_const naming c
   303        of SOME name => member (op =) names_all name
   304         | NONE => false) cs
   305       then SOME (name, content) else NONE;
   306     fun includes names_all = map_filter (select_include names_all)
   307       ((Symtab.dest o the_includes) data);
   308     val module_alias = the_module_alias data;
   309     val { class, instance, tyco, const } = the_name_syntax data;
   310     val literals = the_literals thy target;
   311     val width = the_default default_width some_width;
   312   in
   313     invoke_serializer thy abortable serializer literals reserved
   314       includes module_alias class instance tyco const module width args naming (modify program) names
   315   end;
   316 
   317 in
   318 
   319 fun serialize thy = mount_serializer thy NONE;
   320 
   321 fun serialize_custom thy (target_name, seri) naming program names =
   322   mount_serializer thy (SOME seri) target_name NONE NONE [] naming program names (String [])
   323   |> the;
   324 
   325 end; (* local *)
   326 
   327 
   328 (* code presentation *)
   329 
   330 fun code_of thy target some_width module_name cs names_stmt =
   331   let
   332     val (names_cs, (naming, program)) = Code_Thingol.consts_program thy false cs;
   333   in
   334     string (names_stmt naming)
   335       (serialize thy target some_width (SOME module_name) [] naming program names_cs)
   336   end;
   337 
   338 
   339 (* code generation *)
   340 
   341 fun transitivly_non_empty_funs thy naming program =
   342   let
   343     val cs = subtract (op =) (abort_allowed thy) (Code_Thingol.empty_funs program);
   344     val names = map_filter (Code_Thingol.lookup_const naming) cs;
   345   in subtract (op =) (Graph.all_preds program names) (Graph.keys program) end;
   346 
   347 fun read_const_exprs thy cs =
   348   let
   349     val (cs1, cs2) = Code_Thingol.read_const_exprs thy cs;
   350     val (names2, (naming, program)) = Code_Thingol.consts_program thy true cs2;
   351     val names3 = transitivly_non_empty_funs thy naming program;
   352     val cs3 = map_filter (fn (c, name) =>
   353       if member (op =) names3 name then SOME c else NONE) (cs2 ~~ names2);
   354   in union (op =) cs3 cs1 end;
   355 
   356 fun export_code thy cs seris =
   357   let
   358     val (cs', (naming, program)) = Code_Thingol.consts_program thy false cs;
   359     fun mk_seri_dest dest = case dest
   360      of NONE => compile
   361       | SOME "-" => export
   362       | SOME f => file (Path.explode f)
   363     val _ = map (fn (((target, module), dest), args) =>
   364       (mk_seri_dest dest (serialize thy target NONE module args naming program cs'))) seris;
   365   in () end;
   366 
   367 fun export_code_cmd raw_cs seris thy = export_code thy (read_const_exprs thy raw_cs) seris;
   368 
   369 
   370 (** serializer configuration **)
   371 
   372 (* data access *)
   373 
   374 fun cert_class thy class =
   375   let
   376     val _ = AxClass.get_info thy class;
   377   in class end;
   378 
   379 fun read_class thy = cert_class thy o Sign.intern_class thy;
   380 
   381 fun cert_tyco thy tyco =
   382   let
   383     val _ = if Sign.declared_tyname thy tyco then ()
   384       else error ("No such type constructor: " ^ quote tyco);
   385   in tyco end;
   386 
   387 fun read_tyco thy = cert_tyco thy o Sign.intern_type thy;
   388 
   389 fun cert_inst thy (class, tyco) =
   390   (cert_class thy class, cert_tyco thy tyco);
   391 
   392 fun read_inst thy (raw_tyco, raw_class) =
   393   (read_class thy raw_class, read_tyco thy raw_tyco);
   394 
   395 fun gen_add_syntax upd del mapp check_x check_raw_syn prep_syn prep_x target raw_x some_raw_syn thy =
   396   let
   397     val x = prep_x thy raw_x |> tap (check_x thy);
   398     fun prep_check raw_syn = prep_syn (raw_syn |> tap (check_raw_syn thy target));
   399   in case some_raw_syn
   400    of SOME raw_syn => (map_name_syntax target o mapp) (upd (x, prep_check raw_syn)) thy
   401     | NONE => (map_name_syntax target o mapp) (del x) thy
   402   end;
   403 
   404 fun gen_add_syntax_class prep_class =
   405   gen_add_syntax Symtab.update Symtab.delete_safe (apfst o apfst) (K I) ((K o K o K) ()) I prep_class;
   406 
   407 fun gen_add_syntax_inst prep_inst =
   408   gen_add_syntax Symreltab.update Symreltab.delete_safe (apfst o apsnd) (K I) ((K o K o K) ()) I prep_inst;
   409 
   410 fun gen_add_syntax_tyco prep_tyco =
   411   gen_add_syntax Symtab.update Symtab.delete_safe (apsnd o apfst)
   412     (fn thy => fn tyco => fn (n, _) => if n <> Sign.arity_number thy tyco
   413       then error ("Number of arguments mismatch in syntax for type constructor " ^ quote tyco)
   414       else ()) ((K o K o K) ()) I prep_tyco;
   415 
   416 fun gen_add_syntax_const prep_const check_raw_syn prep_syn =
   417   gen_add_syntax Symtab.update Symtab.delete_safe (apsnd o apsnd)
   418     (fn thy => fn c => fn (n, _) => if n > Code.args_number thy c
   419       then error ("Too many arguments in syntax for constant " ^ quote c)
   420       else ()) check_raw_syn prep_syn prep_const;
   421 
   422 fun add_reserved target =
   423   let
   424     fun add sym syms = if member (op =) syms sym
   425       then error ("Reserved symbol " ^ quote sym ^ " already declared")
   426       else insert (op =) sym syms
   427   in map_reserved target o add end;
   428 
   429 fun gen_add_include read_const target args thy =
   430   let
   431     fun add (name, SOME (content, raw_cs)) incls =
   432           let
   433             val _ = if Symtab.defined incls name
   434               then warning ("Overwriting existing include " ^ name)
   435               else ();
   436             val cs = map (read_const thy) raw_cs;
   437           in Symtab.update (name, (Code_Printer.str content, cs)) incls end
   438       | add (name, NONE) incls = Symtab.delete name incls;
   439   in map_includes target (add args) thy end;
   440 
   441 val add_include = gen_add_include (K I);
   442 val add_include_cmd = gen_add_include Code.read_const;
   443 
   444 fun add_module_alias target (thyname, modlname) =
   445   let
   446     val xs = Long_Name.explode modlname;
   447     val xs' = map (Name.desymbolize true) xs;
   448   in if xs' = xs
   449     then map_module_alias target (Symtab.update (thyname, modlname))
   450     else error ("Invalid module name: " ^ quote modlname ^ "\n"
   451       ^ "perhaps try " ^ quote (Long_Name.implode xs'))
   452   end;
   453 
   454 fun gen_allow_abort prep_const raw_c thy =
   455   let
   456     val c = prep_const thy raw_c;
   457   in thy |> (Targets.map o apfst o apsnd) (insert (op =) c) end;
   458 
   459 
   460 (* concrete syntax *)
   461 
   462 local
   463 
   464 structure P = OuterParse
   465 and K = OuterKeyword
   466 
   467 fun zip_list (x::xs) f g =
   468   f
   469   #-> (fn y =>
   470     fold_map (fn x => g |-- f >> pair x) xs
   471     #-> (fn xys => pair ((x, y) :: xys)));
   472 
   473 fun parse_multi_syntax parse_thing parse_syntax =
   474   P.and_list1 parse_thing
   475   #-> (fn things => Scan.repeat1 (P.$$$ "(" |-- P.name --
   476         (zip_list things parse_syntax (P.$$$ "and")) --| P.$$$ ")"));
   477 
   478 in
   479 
   480 val add_syntax_class = gen_add_syntax_class cert_class;
   481 val add_syntax_inst = gen_add_syntax_inst cert_inst;
   482 val add_syntax_tyco = gen_add_syntax_tyco cert_tyco;
   483 val add_syntax_const_simple = gen_add_syntax_const (K I) ((K o K o K) ()) Code_Printer.simple_const_syntax;
   484 val add_syntax_const = gen_add_syntax_const (K I) ((K o K o K) ()) I;
   485 val allow_abort = gen_allow_abort (K I);
   486 val add_reserved = add_reserved;
   487 val add_include = add_include;
   488 
   489 val add_syntax_class_cmd = gen_add_syntax_class read_class;
   490 val add_syntax_inst_cmd = gen_add_syntax_inst read_inst;
   491 val add_syntax_tyco_cmd = gen_add_syntax_tyco read_tyco;
   492 val add_syntax_const_simple_cmd = gen_add_syntax_const Code.read_const ((K o K o K) ()) Code_Printer.simple_const_syntax;
   493 val add_syntax_const_cmd = gen_add_syntax_const Code.read_const ((K o K o K) ()) I;
   494 
   495 val allow_abort_cmd = gen_allow_abort Code.read_const;
   496 
   497 fun parse_args f args =
   498   case Scan.read OuterLex.stopper f args
   499    of SOME x => x
   500     | NONE => error "Bad serializer arguments";
   501 
   502 
   503 (** Isar setup **)
   504 
   505 val (inK, module_nameK, fileK) = ("in", "module_name", "file");
   506 
   507 val code_exprP =
   508   (Scan.repeat1 P.term_group
   509   -- Scan.repeat (P.$$$ inK |-- P.name
   510      -- Scan.option (P.$$$ module_nameK |-- P.name)
   511      -- Scan.option (P.$$$ fileK |-- P.name)
   512      -- Scan.optional (P.$$$ "(" |-- Args.parse --| P.$$$ ")") []
   513   ) >> (fn (raw_cs, seris) => export_code_cmd raw_cs seris));
   514 
   515 val _ = List.app OuterKeyword.keyword [inK, module_nameK, fileK];
   516 
   517 val _ =
   518   OuterSyntax.command "code_class" "define code syntax for class" K.thy_decl (
   519     parse_multi_syntax P.xname (Scan.option P.string)
   520     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   521           fold (fn (raw_class, syn) => add_syntax_class_cmd target raw_class syn) syns)
   522   );
   523 
   524 val _ =
   525   OuterSyntax.command "code_instance" "define code syntax for instance" K.thy_decl (
   526     parse_multi_syntax (P.xname --| P.$$$ "::" -- P.xname) (Scan.option (P.minus >> K ()))
   527     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   528           fold (fn (raw_inst, add_del) => add_syntax_inst_cmd target raw_inst add_del) syns)
   529   );
   530 
   531 val _ =
   532   OuterSyntax.command "code_type" "define code syntax for type constructor" K.thy_decl (
   533     parse_multi_syntax P.xname (Code_Printer.parse_syntax I)
   534     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   535           fold (fn (raw_tyco, syn) => add_syntax_tyco_cmd target raw_tyco syn) syns)
   536   );
   537 
   538 val _ =
   539   OuterSyntax.command "code_const" "define code syntax for constant" K.thy_decl (
   540     parse_multi_syntax P.term_group (Code_Printer.parse_syntax fst)
   541     >> (Toplevel.theory oo fold) (fn (target, syns) =>
   542       fold (fn (raw_const, syn) => add_syntax_const_simple_cmd target raw_const syn) syns)
   543   );
   544 
   545 val _ =
   546   OuterSyntax.command "code_reserved" "declare words as reserved for target language" K.thy_decl (
   547     P.name -- Scan.repeat1 P.name
   548     >> (fn (target, reserveds) => (Toplevel.theory o fold (add_reserved target)) reserveds)
   549   );
   550 
   551 val _ =
   552   OuterSyntax.command "code_include" "declare piece of code to be included in generated code" K.thy_decl (
   553     P.name -- P.name -- (P.text :|-- (fn "-" => Scan.succeed NONE
   554       | s => Scan.optional (P.$$$ "attach" |-- Scan.repeat1 P.term) [] >> pair s >> SOME))
   555     >> (fn ((target, name), content_consts) =>
   556         (Toplevel.theory o add_include_cmd target) (name, content_consts))
   557   );
   558 
   559 val _ =
   560   OuterSyntax.command "code_modulename" "alias module to other name" K.thy_decl (
   561     P.name -- Scan.repeat1 (P.name -- P.name)
   562     >> (fn (target, modlnames) => (Toplevel.theory o fold (add_module_alias target)) modlnames)
   563   );
   564 
   565 val _ =
   566   OuterSyntax.command "code_abort" "permit constant to be implemented as program abort" K.thy_decl (
   567     Scan.repeat1 P.term_group >> (Toplevel.theory o fold allow_abort_cmd)
   568   );
   569 
   570 val _ =
   571   OuterSyntax.command "export_code" "generate executable code for constants"
   572     K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
   573 
   574 fun shell_command thyname cmd = Toplevel.program (fn _ =>
   575   (use_thy thyname; case Scan.read OuterLex.stopper (P.!!! code_exprP)
   576     ((filter OuterLex.is_proper o OuterSyntax.scan Position.none) cmd)
   577    of SOME f => (writeln "Now generating code..."; f (theory thyname))
   578     | NONE => error ("Bad directive " ^ quote cmd)))
   579   handle Runtime.TOPLEVEL_ERROR => OS.Process.exit OS.Process.failure;
   580 
   581 end; (*local*)
   582 
   583 end; (*struct*)