src/Tools/code/code_package.ML
author haftmann
Fri Aug 24 14:14:20 2007 +0200 (2007-08-24)
changeset 24423 ae9cd0e92423
parent 24381 560e8ecdf633
child 24436 b694324cd2be
permissions -rw-r--r--
overloaded definitions accompanied by explicit constants
     1 (*  Title:      Tools/code/code_package.ML
     2     ID:         $Id$
     3     Author:     Florian Haftmann, TU Muenchen
     4 
     5 Code generator translation kernel.  Code generator Isar setup.
     6 *)
     7 
     8 signature CODE_PACKAGE =
     9 sig
    10   (* interfaces *)
    11   val eval_conv: theory
    12     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    13        -> string list -> cterm -> thm)
    14     -> cterm -> thm;
    15   val eval_term: theory
    16     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    17        -> string list -> cterm -> 'a)
    18     -> cterm -> 'a;
    19   val satisfies_ref: bool option ref;
    20   val satisfies: theory -> cterm -> string list -> bool;
    21   val codegen_command: theory -> string -> unit;
    22 
    23   (* axiomatic interfaces *)
    24   type appgen;
    25   val add_appconst: string * appgen -> theory -> theory;
    26   val appgen_let: appgen;
    27   val appgen_if: appgen;
    28   val appgen_case: (theory -> term
    29     -> ((string * typ) list * ((term * typ) * (term * term) list)) option)
    30     -> appgen;
    31 
    32   val timing: bool ref;
    33 end;
    34 
    35 structure CodePackage : CODE_PACKAGE =
    36 struct
    37 
    38 open BasicCodeThingol;
    39 
    40 (** code translation **)
    41 
    42 (* theory data *)
    43 
    44 type appgen = theory -> ((sort -> sort) * Sorts.algebra) * Consts.T
    45   -> CodeFuncgr.T
    46   -> (string * typ) * term list -> CodeThingol.transact -> iterm * CodeThingol.transact;
    47 
    48 structure Appgens = TheoryDataFun
    49 (
    50   type T = (int * (appgen * stamp)) Symtab.table;
    51   val empty = Symtab.empty;
    52   val copy = I;
    53   val extend = I;
    54   fun merge _ = Symtab.merge (fn ((bounds1, (_, stamp1)), (bounds2, (_, stamp2))) =>
    55     bounds1 = bounds2 andalso stamp1 = stamp2);
    56 );
    57 
    58 fun code_depgr thy [] = CodeFuncgr.make thy []
    59   | code_depgr thy consts =
    60       let
    61         val gr = CodeFuncgr.make thy consts;
    62         val select = Graph.all_succs gr consts;
    63       in
    64         gr
    65         |> Graph.subgraph (member (op =) select) 
    66         |> Graph.map_nodes ((apsnd o map) (Conv.fconv_rule (Class.overload thy)))
    67       end;
    68 
    69 fun code_thms thy =
    70   Pretty.writeln o CodeFuncgr.pretty thy o code_depgr thy;
    71 
    72 fun code_deps thy consts =
    73   let
    74     val gr = code_depgr thy consts;
    75     fun mk_entry (const, (_, (_, parents))) =
    76       let
    77         val name = CodeUnit.string_of_const thy const;
    78         val nameparents = map (CodeUnit.string_of_const thy) parents;
    79       in { name = name, ID = name, dir = "", unfold = true,
    80         path = "", parents = nameparents }
    81       end;
    82     val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
    83   in Present.display_graph prgr end;
    84 
    85 structure Program = CodeDataFun
    86 (
    87   type T = CodeThingol.code;
    88   val empty = CodeThingol.empty_code;
    89   fun merge _ = CodeThingol.merge_code;
    90   fun purge _ NONE _ = CodeThingol.empty_code
    91     | purge NONE _ _ = CodeThingol.empty_code
    92     | purge (SOME thy) (SOME cs) code =
    93         let
    94           val cs_exisiting =
    95             map_filter (CodeName.const_rev thy) (Graph.keys code);
    96           val dels = (Graph.all_preds code
    97               o map (CodeName.const thy)
    98               o filter (member (op =) cs_exisiting)
    99             ) cs;
   100         in Graph.del_nodes dels code end;
   101 );
   102 
   103 
   104 (* translation kernel *)
   105 
   106 val value_name = "Isabelle_Eval.EVAL.EVAL";
   107 
   108 fun ensure_def thy = CodeThingol.ensure_def
   109   (fn s => if s = value_name then "<term>" else CodeName.labelled_name thy s);
   110 
   111 fun ensure_def_class thy (algbr as ((_, algebra), _)) funcgr class =
   112   let
   113     val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
   114     val (v, cs) = AxClass.params_of_class thy class;
   115     val class' = CodeName.class thy class;
   116     val classrels' = map (curry (CodeName.classrel thy) class) superclasses;
   117     val classops' = map (CodeName.const thy o fst) cs;
   118     val defgen_class =
   119       fold_map (ensure_def_class thy algbr funcgr) superclasses
   120       ##>> (fold_map (exprgen_typ thy algbr funcgr) o map snd) cs
   121       #>> (fn (superclasses, classoptyps) =>
   122         CodeThingol.Class (superclasses ~~ classrels', (unprefix "'" v, classops' ~~ classoptyps)))
   123   in
   124     ensure_def thy defgen_class ("generating class " ^ quote class) class'
   125     #> pair class'
   126   end
   127 and ensure_def_classrel thy algbr funcgr (subclass, superclass) =
   128   ensure_def_class thy algbr funcgr subclass
   129   #>> (fn _ => CodeName.classrel thy (subclass, superclass))
   130 and ensure_def_tyco thy algbr funcgr "fun" =
   131       pair "fun"
   132   | ensure_def_tyco thy algbr funcgr tyco =
   133       let
   134         val defgen_datatype =
   135           let
   136             val (vs, cos) = Code.get_datatype thy tyco;
   137           in
   138             fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   139             ##>> fold_map (fn (c, tys) =>
   140               fold_map (exprgen_typ thy algbr funcgr) tys
   141               #>> (fn tys' => (CodeName.const thy c, tys'))) cos
   142             #>> (fn (vs, cos) => CodeThingol.Datatype (vs, cos))
   143           end;
   144         val tyco' = CodeName.tyco thy tyco;
   145       in
   146         ensure_def thy defgen_datatype ("generating type constructor " ^ quote tyco) tyco'
   147         #> pair tyco'
   148       end
   149 and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr (v, sort) trns =
   150   trns
   151   |> fold_map (ensure_def_class thy algbr funcgr) (proj_sort sort)
   152   |>> (fn sort => (unprefix "'" v, sort))
   153 and exprgen_typ thy algbr funcgr (TFree vs) trns =
   154       trns
   155       |> exprgen_tyvar_sort thy algbr funcgr vs
   156       |>> (fn (v, sort) => ITyVar v)
   157   | exprgen_typ thy algbr funcgr (Type (tyco, tys)) trns =
   158       trns
   159       |> ensure_def_tyco thy algbr funcgr tyco
   160       ||>> fold_map (exprgen_typ thy algbr funcgr) tys
   161       |>> (fn (tyco, tys) => tyco `%% tys);
   162 
   163 exception CONSTRAIN of (string * typ) * typ;
   164 val timing = ref false;
   165 
   166 fun exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr (ty_ctxt, sort_decl) =
   167   let
   168     val pp = Sign.pp thy;
   169     datatype typarg =
   170         Global of (class * string) * typarg list list
   171       | Local of (class * class) list * (string * (int * sort));
   172     fun class_relation (Global ((_, tyco), yss), _) class =
   173           Global ((class, tyco), yss)
   174       | class_relation (Local (classrels, v), subclass) superclass =
   175           Local ((subclass, superclass) :: classrels, v);
   176     fun type_constructor tyco yss class =
   177       Global ((class, tyco), (map o map) fst yss);
   178     fun type_variable (TFree (v, sort)) =
   179       let
   180         val sort' = proj_sort sort;
   181       in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
   182     val typargs = Sorts.of_sort_derivation pp algebra
   183       {class_relation = class_relation, type_constructor = type_constructor,
   184        type_variable = type_variable}
   185       (ty_ctxt, proj_sort sort_decl);
   186     fun mk_dict (Global (inst, yss)) =
   187           ensure_def_inst thy algbr funcgr inst
   188           ##>> (fold_map o fold_map) mk_dict yss
   189           #>> (fn (inst, dss) => DictConst (inst, dss))
   190       | mk_dict (Local (classrels, (v, (k, sort)))) =
   191           fold_map (ensure_def_classrel thy algbr funcgr) classrels
   192           #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
   193   in
   194     fold_map mk_dict typargs
   195   end
   196 and exprgen_dict_parms thy (algbr as (_, consts)) funcgr (c, ty_ctxt) =
   197   let
   198     val ty_decl = Consts.the_declaration consts c;
   199     val (tys, tys_decl) = pairself (curry (Consts.typargs consts) c) (ty_ctxt, ty_decl);
   200     val sorts = map (snd o dest_TVar) tys_decl;
   201   in
   202     fold_map (exprgen_dicts thy algbr funcgr) (tys ~~ sorts)
   203   end
   204 and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) trns =
   205   let
   206     val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
   207     val (var, classops) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
   208     val vs = Name.names (Name.declare var Name.context) "'a" (Sorts.mg_domain algebra tyco [class]);
   209     val arity_typ = Type (tyco, map TFree vs);
   210     fun gen_superarity superclass trns =
   211       trns
   212       |> ensure_def_class thy algbr funcgr superclass
   213       ||>> ensure_def_classrel thy algbr funcgr (class, superclass)
   214       ||>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass])
   215       |>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
   216             (superclass, (classrel, (inst, dss))));
   217     fun gen_classop_def (c, ty) trns =
   218       trns
   219       |> ensure_def_const thy algbr funcgr c
   220       ||>> exprgen_term thy algbr funcgr
   221             (Const (Class.inst_const thy (c, tyco), map_type_tfree (K arity_typ) ty));
   222     fun defgen_inst trns =
   223       trns
   224       |> ensure_def_class thy algbr funcgr class
   225       ||>> ensure_def_tyco thy algbr funcgr tyco
   226       ||>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   227       ||>> fold_map gen_superarity superclasses
   228       ||>> fold_map gen_classop_def classops
   229       |>> (fn ((((class, tyco), arity), superarities), classops) =>
   230              CodeThingol.Classinst ((class, (tyco, arity)), (superarities, classops)));
   231     val inst = CodeName.instance thy (class, tyco);
   232   in
   233     trns
   234     |> ensure_def thy defgen_inst ("generating instance " ^ quote class ^ " / " ^ quote tyco) inst
   235     |> pair inst
   236   end
   237 and ensure_def_const thy (algbr as (_, consts)) funcgr c =
   238   let
   239     val c' = CodeName.const thy c;
   240     fun defgen_datatypecons trns =
   241       trns 
   242       |> ensure_def_tyco thy algbr funcgr
   243           ((the o Code.get_datatype_of_constr thy) c)
   244       |>> (fn _ => CodeThingol.Bot);
   245     fun defgen_classop trns =
   246       trns 
   247       |> ensure_def_class thy algbr funcgr
   248         ((the o AxClass.class_of_param thy) c)
   249       |>> (fn _ => CodeThingol.Bot);
   250     fun defgen_fun trns =
   251       let
   252         val raw_thms = CodeFuncgr.funcs funcgr c;
   253         val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) c;
   254         val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
   255           then raw_thms
   256           else map (CodeUnit.expand_eta 1) raw_thms;
   257         val timeap = if !timing then Output.timeap_msg ("time for " ^ c)
   258           else I;
   259         val vs = (map dest_TFree o Consts.typargs consts) (c, ty);
   260         val dest_eqthm =
   261           apfst (snd o strip_comb) o Logic.dest_equals o Logic.unvarify o prop_of;
   262         fun exprgen_eq (args, rhs) =
   263           fold_map (exprgen_term thy algbr funcgr) args
   264           ##>> exprgen_term thy algbr funcgr rhs;
   265       in
   266         trns
   267         |> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   268         ||>> exprgen_typ thy algbr funcgr ty
   269         ||>> timeap (fold_map (exprgen_eq o dest_eqthm) thms)
   270         |>> (fn ((vs, ty), eqs) => CodeThingol.Fun ((vs, ty), eqs))
   271       end;
   272     val defgen = if (is_some o Code.get_datatype_of_constr thy) c
   273       then defgen_datatypecons
   274       else if (is_some o AxClass.class_of_param thy) c
   275       then defgen_classop
   276       else defgen_fun
   277   in
   278     ensure_def thy defgen ("generating constant " ^ CodeUnit.string_of_const thy c) c'
   279     #> pair c'
   280   end
   281 and exprgen_term thy algbr funcgr (Const (c, ty)) trns =
   282       trns
   283       |> select_appgen thy algbr funcgr ((c, ty), [])
   284   | exprgen_term thy algbr funcgr (Free (v, ty)) trns =
   285       trns
   286       |> exprgen_typ thy algbr funcgr ty
   287       |>> (fn _ => IVar v)
   288   | exprgen_term thy algbr funcgr (Abs (raw_v, ty, raw_t)) trns =
   289       let
   290         val (v, t) = Syntax.variant_abs (raw_v, ty, raw_t);
   291       in
   292         trns
   293         |> exprgen_typ thy algbr funcgr ty
   294         ||>> exprgen_term thy algbr funcgr t
   295         |>> (fn (ty, t) => (v, ty) `|-> t)
   296       end
   297   | exprgen_term thy algbr funcgr (t as _ $ _) trns =
   298       case strip_comb t
   299        of (Const (c, ty), ts) =>
   300             trns
   301             |> select_appgen thy algbr funcgr ((c, ty), ts)
   302         | (t', ts) =>
   303             trns
   304             |> exprgen_term thy algbr funcgr t'
   305             ||>> fold_map (exprgen_term thy algbr funcgr) ts
   306             |>> (fn (t, ts) => t `$$ ts)
   307 and appgen_default thy algbr funcgr ((c, ty), ts) trns =
   308   trns
   309   |> ensure_def_const thy algbr funcgr c
   310   ||>> fold_map (exprgen_typ thy algbr funcgr) ((fst o Term.strip_type) ty)
   311   ||>> exprgen_typ thy algbr funcgr ((snd o Term.strip_type) ty)
   312   ||>> exprgen_dict_parms thy algbr funcgr (c, ty)
   313   ||>> fold_map (exprgen_term thy algbr funcgr) ts
   314   |>> (fn ((((c, tys), ty), iss), ts) => IConst (c, (iss, tys)) `$$ ts)
   315 and select_appgen thy algbr funcgr ((c, ty), ts) trns =
   316   case Symtab.lookup (Appgens.get thy) c
   317    of SOME (i, (appgen, _)) =>
   318         if length ts < i then
   319           let
   320             val k = length ts;
   321             val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
   322             val ctxt = (fold o fold_aterms)
   323               (fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
   324             val vs = Name.names ctxt "a" tys;
   325           in
   326             trns
   327             |> fold_map (exprgen_typ thy algbr funcgr) tys
   328             ||>> appgen thy algbr funcgr ((c, ty), ts @ map Free vs)
   329             |>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
   330           end
   331         else if length ts > i then
   332           trns
   333           |> appgen thy algbr funcgr ((c, ty), Library.take (i, ts))
   334           ||>> fold_map (exprgen_term thy algbr funcgr) (Library.drop (i, ts))
   335           |>> (fn (t, ts) => t `$$ ts)
   336         else
   337           trns
   338           |> appgen thy algbr funcgr ((c, ty), ts)
   339     | NONE =>
   340         trns
   341         |> appgen_default thy algbr funcgr ((c, ty), ts);
   342 
   343 
   344 (* entrance points into translation kernel *)
   345 
   346 fun ensure_def_const' thy algbr funcgr c trns =
   347   ensure_def_const thy algbr funcgr c trns
   348   handle CONSTRAIN ((c, ty), ty_decl) => error (
   349     "Constant " ^ c ^ " with most general type\n"
   350     ^ CodeUnit.string_of_typ thy ty
   351     ^ "\noccurs with type\n"
   352     ^ CodeUnit.string_of_typ thy ty_decl);
   353 
   354 fun perhaps_def_const thy algbr funcgr c trns =
   355   case try (ensure_def_const thy algbr funcgr c) trns
   356    of SOME (c, trns) => (SOME c, trns)
   357     | NONE => (NONE, trns);
   358 
   359 fun exprgen_term' thy algbr funcgr t trns =
   360   exprgen_term thy algbr funcgr t trns
   361   handle CONSTRAIN ((c, ty), ty_decl) => error ("In term " ^ (quote o Sign.string_of_term thy) t
   362     ^ ",\nconstant " ^ c ^ " with most general type\n"
   363     ^ CodeUnit.string_of_typ thy ty
   364     ^ "\noccurs with type\n"
   365     ^ CodeUnit.string_of_typ thy ty_decl);
   366 
   367 
   368 (* parametrized application generators, for instantiation in object logic *)
   369 (* (axiomatic extensions of translation kernel) *)
   370 
   371 fun appgen_case dest_case_expr thy algbr funcgr (app as (c_ty, ts)) =
   372   let
   373     val SOME ([], ((st, sty), ds)) = dest_case_expr thy (list_comb (Const c_ty, ts));
   374     fun clause_gen (dt, bt) =
   375       exprgen_term thy algbr funcgr
   376         (map_aterms (fn Const (c_ty as (c, ty)) => Const
   377           (Class.unoverload_const thy c_ty, ty) | t => t) dt)
   378       ##>> exprgen_term thy algbr funcgr bt;
   379   in
   380     exprgen_term thy algbr funcgr st
   381     ##>> exprgen_typ thy algbr funcgr sty
   382     ##>> fold_map clause_gen ds
   383     ##>> appgen_default thy algbr funcgr app
   384     #>> (fn (((se, sty), ds), t0) => ICase (((se, sty), ds), t0))
   385   end;
   386 
   387 fun appgen_let thy algbr funcgr (app as (_, [st, ct])) =
   388   exprgen_term thy algbr funcgr ct
   389   ##>> exprgen_term thy algbr funcgr st
   390   ##>> appgen_default thy algbr funcgr app
   391   #>> (fn (((v, ty) `|-> be, se), t0) =>
   392             ICase (CodeThingol.collapse_let (((v, ty), se), be), t0)
   393         | (_, t0) => t0);
   394 
   395 fun appgen_if thy algbr funcgr (app as (_, [tb, tt, tf])) =
   396   exprgen_term thy algbr funcgr tb
   397   ##>> exprgen_typ thy algbr funcgr (Type ("bool", []))
   398   ##>> exprgen_term thy algbr funcgr (Const ("True", Type ("bool", [])))
   399   ##>> exprgen_term thy algbr funcgr tt
   400   ##>> exprgen_term thy algbr funcgr (Const ("False", Type ("bool", [])))
   401   ##>> exprgen_term thy algbr funcgr tf
   402   ##>> appgen_default thy algbr funcgr app
   403   #>> (fn ((((((tb, B), T), tt), F), tf), t0) => ICase (((tb, B), [(T, tt), (F, tf)]), t0));
   404 
   405 fun add_appconst (c, appgen) thy =
   406   let
   407     val i = (length o fst o strip_type o Sign.the_const_type thy) c;
   408     val _ = Program.change thy (K CodeThingol.empty_code);
   409   in
   410     Appgens.map (Symtab.update (c, (i, (appgen, stamp ())))) thy
   411   end;
   412 
   413 
   414 (** code generation interfaces **)
   415 
   416 (* generic generation combinators *)
   417 
   418 fun generate thy funcgr gen it =
   419   let
   420     val naming = NameSpace.qualified_names NameSpace.default_naming;
   421     val consttab = Consts.empty
   422       |> fold (fn c => Consts.declare naming
   423            ((c, CodeFuncgr.typ funcgr c), true))
   424            (CodeFuncgr.all funcgr);
   425     val algbr = (Code.operational_algebra thy, consttab);
   426   in   
   427     Program.change_yield thy
   428       (CodeThingol.start_transact (gen thy algbr funcgr it))
   429     |> fst
   430   end;
   431 
   432 fun raw_eval f thy g =
   433   let
   434     val value_name = "Isabelle_Eval.EVAL.EVAL";
   435     fun ensure_eval thy algbr funcgr t = 
   436       let
   437         val ty = fastype_of t;
   438         val vs = typ_tfrees ty;
   439         val defgen_eval =
   440           fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   441           ##>> exprgen_typ thy algbr funcgr ty
   442           ##>> exprgen_term' thy algbr funcgr t
   443           #>> (fn ((vs, ty), t) => CodeThingol.Fun ((vs, ty), [([], t)]));
   444         fun result (dep, code) =
   445           let
   446             val CodeThingol.Fun ((vs, ty), [([], t)]) = Graph.get_node code value_name;
   447             val deps = Graph.imm_succs code value_name;
   448             val code' = Graph.del_nodes [value_name] code;
   449             val code'' = CodeThingol.project_code false [] (SOME deps) code';
   450           in ((code'', ((vs, ty), t), deps), (dep, code')) end;
   451       in
   452         ensure_def thy defgen_eval "evaluation" value_name
   453         #> result
   454       end;
   455     fun h funcgr ct =
   456       let
   457         val (code, vs_ty_t, deps) = generate thy funcgr ensure_eval (Thm.term_of ct);
   458       in g code vs_ty_t deps ct end;
   459   in f thy h end;
   460 
   461 fun eval_conv thy = raw_eval CodeFuncgr.eval_conv thy;
   462 fun eval_term thy = raw_eval CodeFuncgr.eval_term thy;
   463 
   464 val satisfies_ref : bool option ref = ref NONE;
   465 
   466 fun satisfies thy ct witnesses =
   467   let
   468     fun evl code ((vs, ty), t) deps ct =
   469       let
   470         val t0 = Thm.term_of ct
   471         val _ = (Term.map_types o Term.map_atyps) (fn _ =>
   472           error ("Term " ^ Sign.string_of_term thy t0 ^ " contains polymorphic type"))
   473           t0;
   474       in
   475         CodeTarget.eval_term thy ("CodePackage.satisfies_ref", satisfies_ref)
   476           code (t, ty) witnesses
   477       end;
   478   in eval_term thy evl ct end;
   479 
   480 fun filter_generatable thy consts =
   481   let
   482     val (consts', funcgr) = CodeFuncgr.make_consts thy consts;
   483     val consts'' = generate thy funcgr (fold_map ooo perhaps_def_const) consts';
   484     val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE)
   485       (consts' ~~ consts'');
   486   in consts''' end;
   487 
   488 
   489 (** toplevel interface and setup **)
   490 
   491 local
   492 
   493 structure P = OuterParse
   494 and K = OuterKeyword
   495 
   496 fun code raw_cs seris thy =
   497   let
   498     val (perm1, cs) = CodeUnit.read_const_exprs thy
   499       (filter_generatable thy) raw_cs;
   500     val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs) (fold_map ooo ensure_def_const') cs
   501      of [] => (true, NONE)
   502       | cs => (false, SOME cs);
   503     val code = Program.get thy;
   504     val seris' = map (fn (((target, module), file), args) =>
   505       CodeTarget.get_serializer thy target (perm1 orelse perm2) module file args
   506         CodeName.labelled_name cs') seris;
   507   in
   508     (map (fn f => f code) seris' : unit list; ())
   509   end;
   510 
   511 fun code_thms_cmd thy =
   512   code_thms thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
   513 
   514 fun code_deps_cmd thy =
   515   code_deps thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
   516 
   517 val (inK, module_nameK, fileK) = ("in", "module_name", "file");
   518 
   519 val code_exprP =
   520   (Scan.repeat P.term
   521   -- Scan.repeat (P.$$$ inK |-- P.name
   522      -- Scan.option (P.$$$ module_nameK |-- P.name)
   523      -- Scan.option (P.$$$ fileK |-- P.name)
   524      -- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") []
   525   ) >> (fn (raw_cs, seris) => code raw_cs seris));
   526 
   527 val _ = OuterSyntax.add_keywords [inK, module_nameK, fileK];
   528 
   529 val (codeK, code_thmsK, code_depsK) = ("export_code", "code_thms", "code_deps");
   530 
   531 in
   532 
   533 val codeP =
   534   OuterSyntax.improper_command codeK "generate executable code for constants"
   535     K.diag (P.!!! code_exprP >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
   536 
   537 fun codegen_command thy cmd =
   538   case Scan.read OuterLex.stopper (P.!!! code_exprP) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd)
   539    of SOME f => (writeln "Now generating code..."; f thy)
   540     | NONE => error ("Bad directive " ^ quote cmd);
   541 
   542 val code_thmsP =
   543   OuterSyntax.improper_command code_thmsK "print system of defining equations for code" OuterKeyword.diag
   544     (Scan.repeat P.term
   545       >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
   546         o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
   547 
   548 val code_depsP =
   549   OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations for code" OuterKeyword.diag
   550     (Scan.repeat P.term
   551       >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
   552         o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
   553 
   554 val _ = OuterSyntax.add_parsers [codeP, code_thmsP, code_depsP];
   555 
   556 end; (* local *)
   557 
   558 end; (* struct *)