src/Tools/code/code_package.ML
author wenzelm
Sat Oct 06 16:50:04 2007 +0200 (2007-10-06)
changeset 24867 e5b55d7be9bb
parent 24844 98c006a30218
child 24918 22013215eece
permissions -rw-r--r--
simplified interfaces for outer syntax;
     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   val eval_conv: theory
    11     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    12        -> string list -> cterm -> thm)
    13     -> cterm -> thm;
    14   val eval_term: theory
    15     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    16        -> string list -> term -> 'a)
    17     -> term -> 'a;
    18   val satisfies_ref: (unit -> bool) option ref;
    19   val satisfies: theory -> term -> string list -> bool;
    20   val eval_invoke: theory -> (string * (unit -> 'a) option ref) -> CodeThingol.code
    21     -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
    22   val codegen_command: theory -> string -> unit;
    23 end;
    24 
    25 structure CodePackage : CODE_PACKAGE =
    26 struct
    27 
    28 open BasicCodeThingol;
    29 
    30 (** code theorems **)
    31 
    32 fun code_depgr thy [] = CodeFuncgr.make thy []
    33   | code_depgr thy consts =
    34       let
    35         val gr = CodeFuncgr.make thy consts;
    36         val select = Graph.all_succs gr consts;
    37       in
    38         gr
    39         |> Graph.subgraph (member (op =) select) 
    40         |> Graph.map_nodes ((apsnd o map) (Conv.fconv_rule (Class.overload thy)))
    41       end;
    42 
    43 fun code_thms thy =
    44   Pretty.writeln o CodeFuncgr.pretty thy o code_depgr thy;
    45 
    46 fun code_deps thy consts =
    47   let
    48     val gr = code_depgr thy consts;
    49     fun mk_entry (const, (_, (_, parents))) =
    50       let
    51         val name = CodeUnit.string_of_const thy const;
    52         val nameparents = map (CodeUnit.string_of_const thy) parents;
    53       in { name = name, ID = name, dir = "", unfold = true,
    54         path = "", parents = nameparents }
    55       end;
    56     val prgr = Graph.fold ((fn x => fn xs => xs @ [x]) o mk_entry) gr [];
    57   in Present.display_graph prgr end;
    58 
    59 
    60 (** code translation **)
    61 
    62 structure Program = CodeDataFun
    63 (
    64   type T = CodeThingol.code;
    65   val empty = CodeThingol.empty_code;
    66   fun merge _ = CodeThingol.merge_code;
    67   fun purge _ NONE _ = CodeThingol.empty_code
    68     | purge NONE _ _ = CodeThingol.empty_code
    69     | purge (SOME thy) (SOME cs) code =
    70         let
    71           val cs_exisiting =
    72             map_filter (CodeName.const_rev thy) (Graph.keys code);
    73           val dels = (Graph.all_preds code
    74               o map (CodeName.const thy)
    75               o filter (member (op =) cs_exisiting)
    76             ) cs;
    77         in Graph.del_nodes dels code end;
    78 );
    79 
    80 
    81 (* translation kernel *)
    82 
    83 val value_name = "Isabelle_Eval.EVAL.EVAL";
    84 
    85 fun ensure_def thy = CodeThingol.ensure_def
    86   (fn s => if s = value_name then "<term>" else CodeName.labelled_name thy s);
    87 
    88 exception CONSTRAIN of (string * typ) * typ;
    89 
    90 fun ensure_def_class thy (algbr as ((_, algebra), _)) funcgr class =
    91   let
    92     val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
    93     val (v, cs) = AxClass.params_of_class thy class;
    94     val class' = CodeName.class thy class;
    95     val defgen_class =
    96       fold_map (fn superclass => ensure_def_class thy algbr funcgr superclass
    97         ##>> ensure_def_classrel thy algbr funcgr (class, superclass)) superclasses
    98       ##>> fold_map (fn (c, ty) => ensure_def_const thy algbr funcgr c
    99         ##>> exprgen_typ thy algbr funcgr ty) cs
   100       #>> (fn info => CodeThingol.Class (unprefix "'" v, info))
   101   in
   102     ensure_def thy defgen_class class'
   103     #> pair class'
   104   end
   105 and ensure_def_classrel thy algbr funcgr (subclass, superclass) =
   106   let
   107     val classrel' = CodeName.classrel thy (subclass, superclass);
   108     val defgen_classrel =
   109       ensure_def_class thy algbr funcgr subclass
   110       ##>> ensure_def_class thy algbr funcgr superclass
   111       #>> CodeThingol.Classrel;
   112   in
   113     ensure_def thy defgen_classrel classrel'
   114     #> pair classrel'
   115   end
   116 and ensure_def_tyco thy algbr funcgr "fun" =
   117       pair "fun"
   118   | ensure_def_tyco thy algbr funcgr tyco =
   119       let
   120         val defgen_datatype =
   121           let
   122             val (vs, cos) = Code.get_datatype thy tyco;
   123           in
   124             fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   125             ##>> fold_map (fn (c, tys) =>
   126               ensure_def_const thy algbr funcgr c
   127               ##>> fold_map (exprgen_typ thy algbr funcgr) tys) cos
   128             #>> CodeThingol.Datatype
   129           end;
   130         val tyco' = CodeName.tyco thy tyco;
   131       in
   132         ensure_def thy defgen_datatype tyco'
   133         #> pair tyco'
   134       end
   135 and exprgen_tyvar_sort thy (algbr as ((proj_sort, _), _)) funcgr (v, sort) =
   136   fold_map (ensure_def_class thy algbr funcgr) (proj_sort sort)
   137   #>> (fn sort => (unprefix "'" v, sort))
   138 and exprgen_typ thy algbr funcgr (TFree vs) =
   139       exprgen_tyvar_sort thy algbr funcgr vs
   140       #>> (fn (v, sort) => ITyVar v)
   141   | exprgen_typ thy algbr funcgr (Type (tyco, tys)) =
   142       ensure_def_tyco thy algbr funcgr tyco
   143       ##>> fold_map (exprgen_typ thy algbr funcgr) tys
   144       #>> (fn (tyco, tys) => tyco `%% tys)
   145 and exprgen_dicts thy (algbr as ((proj_sort, algebra), consts)) funcgr (ty_ctxt, sort_decl) =
   146   let
   147     val pp = Sign.pp thy;
   148     datatype typarg =
   149         Global of (class * string) * typarg list list
   150       | Local of (class * class) list * (string * (int * sort));
   151     fun class_relation (Global ((_, tyco), yss), _) class =
   152           Global ((class, tyco), yss)
   153       | class_relation (Local (classrels, v), subclass) superclass =
   154           Local ((subclass, superclass) :: classrels, v);
   155     fun type_constructor tyco yss class =
   156       Global ((class, tyco), (map o map) fst yss);
   157     fun type_variable (TFree (v, sort)) =
   158       let
   159         val sort' = proj_sort sort;
   160       in map_index (fn (n, class) => (Local ([], (v, (n, sort'))), class)) sort' end;
   161     val typargs = Sorts.of_sort_derivation pp algebra
   162       {class_relation = class_relation, type_constructor = type_constructor,
   163        type_variable = type_variable}
   164       (ty_ctxt, proj_sort sort_decl);
   165     fun mk_dict (Global (inst, yss)) =
   166           ensure_def_inst thy algbr funcgr inst
   167           ##>> (fold_map o fold_map) mk_dict yss
   168           #>> (fn (inst, dss) => DictConst (inst, dss))
   169       | mk_dict (Local (classrels, (v, (k, sort)))) =
   170           fold_map (ensure_def_classrel thy algbr funcgr) classrels
   171           #>> (fn classrels => DictVar (classrels, (unprefix "'" v, (k, length sort))))
   172   in
   173     fold_map mk_dict typargs
   174   end
   175 and exprgen_dict_parms thy (algbr as (_, consts)) funcgr (c, ty_ctxt) =
   176   let
   177     val ty_decl = Consts.the_declaration consts c;
   178     val (tys, tys_decl) = pairself (curry (Consts.typargs consts) c) (ty_ctxt, ty_decl);
   179     val sorts = map (snd o dest_TVar) tys_decl;
   180   in
   181     fold_map (exprgen_dicts thy algbr funcgr) (tys ~~ sorts)
   182   end
   183 and exprgen_eq thy algbr funcgr thm =
   184   let
   185     val (args, rhs) = (apfst (snd o strip_comb) o Logic.dest_equals
   186       o Logic.unvarify o prop_of) thm;
   187   in
   188     fold_map (exprgen_term thy algbr funcgr) args
   189     ##>> exprgen_term thy algbr funcgr rhs
   190     #>> rpair thm
   191   end
   192 and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) =
   193   let
   194     val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
   195     val (var, classparams) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
   196     val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
   197     val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
   198     val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
   199       Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
   200     val arity_typ = Type (tyco, map TFree vs);
   201     val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
   202     fun exprgen_superarity superclass =
   203       ensure_def_class thy algbr funcgr superclass
   204       ##>> ensure_def_classrel thy algbr funcgr (class, superclass)
   205       ##>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass])
   206       #>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
   207             (superclass, (classrel, (inst, dss))));
   208     fun exprgen_classparam_inst (c, ty) =
   209       let
   210         val c_inst = Const (c, map_type_tfree (K arity_typ') ty);
   211         val thm = Class.unoverload thy (Thm.cterm_of thy c_inst);
   212         val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd
   213           o Logic.dest_equals o Thm.prop_of) thm;
   214       in
   215         ensure_def_const thy algbr funcgr c
   216         ##>> exprgen_const thy algbr funcgr c_ty
   217         #>> (fn (c, IConst c_inst) => ((c, c_inst), thm))
   218       end;
   219     val defgen_inst =
   220       ensure_def_class thy algbr funcgr class
   221       ##>> ensure_def_tyco thy algbr funcgr tyco
   222       ##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   223       ##>> fold_map exprgen_superarity superclasses
   224       ##>> fold_map exprgen_classparam_inst classparams
   225       #>> (fn ((((class, tyco), arity), superarities), classparams) =>
   226              CodeThingol.Classinst ((class, (tyco, arity)), (superarities, classparams)));
   227     val inst = CodeName.instance thy (class, tyco);
   228   in
   229     ensure_def thy defgen_inst inst
   230     #> pair inst
   231   end
   232 and ensure_def_const thy (algbr as (_, consts)) funcgr c =
   233   let
   234     val c' = CodeName.const thy c;
   235     fun defgen_datatypecons tyco =
   236       ensure_def_tyco thy algbr funcgr tyco
   237       #>> K (CodeThingol.Datatypecons c');
   238     fun defgen_classparam class =
   239       ensure_def_class thy algbr funcgr class
   240       #>> K (CodeThingol.Classparam c');
   241     fun defgen_fun trns =
   242       let
   243         val raw_thms = CodeFuncgr.funcs funcgr c;
   244         val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) c;
   245         val vs = (map dest_TFree o Consts.typargs consts) (c, ty);
   246         val thms = if (null o Term.typ_tfrees) ty orelse (null o fst o strip_type) ty
   247           then raw_thms
   248           else map (CodeUnit.expand_eta 1) raw_thms;
   249       in
   250         trns
   251         |> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   252         ||>> exprgen_typ thy algbr funcgr ty
   253         ||>> fold_map (exprgen_eq thy algbr funcgr) thms
   254         |>> (fn ((vs, ty), eqs) => CodeThingol.Fun ((vs, ty), eqs))
   255       end;
   256     val defgen = case Code.get_datatype_of_constr thy c
   257      of SOME tyco => defgen_datatypecons tyco
   258       | NONE => (case AxClass.class_of_param thy c
   259          of SOME class => defgen_classparam class
   260           | NONE => defgen_fun)
   261   in
   262     ensure_def thy defgen c'
   263     #> pair c'
   264   end
   265 and exprgen_term thy algbr funcgr (Const (c, ty)) =
   266       exprgen_app thy algbr funcgr ((c, ty), [])
   267   | exprgen_term thy algbr funcgr (Free (v, _)) =
   268       pair (IVar v)
   269   | exprgen_term thy algbr funcgr (Abs (abs as (_, ty, _))) =
   270       let
   271         val (v, t) = Syntax.variant_abs abs;
   272       in
   273         exprgen_typ thy algbr funcgr ty
   274         ##>> exprgen_term thy algbr funcgr t
   275         #>> (fn (ty, t) => (v, ty) `|-> t)
   276       end
   277   | exprgen_term thy algbr funcgr (t as _ $ _) =
   278       case strip_comb t
   279        of (Const (c, ty), ts) =>
   280             exprgen_app thy algbr funcgr ((c, ty), ts)
   281         | (t', ts) =>
   282             exprgen_term thy algbr funcgr t'
   283             ##>> fold_map (exprgen_term thy algbr funcgr) ts
   284             #>> (fn (t, ts) => t `$$ ts)
   285 and exprgen_const thy algbr funcgr (c, ty) =
   286   ensure_def_const thy algbr funcgr c
   287   ##>> exprgen_dict_parms thy algbr funcgr (c, ty)
   288   ##>> fold_map (exprgen_typ thy algbr funcgr) ((fst o Term.strip_type) ty)
   289   (*##>> exprgen_typ thy algbr funcgr ((snd o Term.strip_type) ty)*)
   290   #>> (fn ((c, iss), tys) => IConst (c, (iss, tys)))
   291 and exprgen_app_default thy algbr funcgr (c_ty, ts) =
   292   exprgen_const thy algbr funcgr c_ty
   293   ##>> fold_map (exprgen_term thy algbr funcgr) ts
   294   #>> (fn (t, ts) => t `$$ ts)
   295 and exprgen_case thy algbr funcgr n cases (app as ((c, ty), ts)) =
   296   let
   297     (*FIXME rework this code*)
   298     val (all_tys, _) = strip_type ty;
   299     val (tys, _) = chop (1 + (if null cases then 1 else length cases)) all_tys;
   300     val st = nth ts n;
   301     val sty = nth tys n;
   302     fun is_undefined (Const (c, _)) = Code.is_undefined thy c
   303       | is_undefined _ = false;
   304     fun mk_case (co, n) t =
   305       let
   306         val (vs, body) = Term.strip_abs_eta n t;
   307         val selector = list_comb (Const (co, map snd vs ---> sty), map Free vs);
   308       in if is_undefined body then NONE else SOME (selector, body) end;
   309     val ds = case cases
   310      of [] => let val ([v_ty], body) = Term.strip_abs_eta 1 (the_single (nth_drop n ts))
   311           in [(Free v_ty, body)] end
   312       | cases => map_filter (uncurry mk_case) (AList.make (CodeUnit.no_args thy) cases
   313           ~~ nth_drop n ts);
   314   in
   315     exprgen_term thy algbr funcgr st
   316     ##>> exprgen_typ thy algbr funcgr sty
   317     ##>> fold_map (fn (pat, body) => exprgen_term thy algbr funcgr pat
   318           ##>> exprgen_term thy algbr funcgr body) ds
   319     ##>> exprgen_app_default thy algbr funcgr app
   320     #>> (fn (((st, sty), ds), t0) => ICase (((st, sty), ds), t0))
   321   end
   322 and exprgen_app thy algbr funcgr ((c, ty), ts) = case Code.get_case_data thy c
   323  of SOME (n, cases) => let val i = 1 + (if null cases then 1 else length cases) in
   324       if length ts < i then
   325         let
   326           val k = length ts;
   327           val tys = (curry Library.take (i - k) o curry Library.drop k o fst o strip_type) ty;
   328           val ctxt = (fold o fold_aterms)
   329             (fn Free (v, _) => Name.declare v | _ => I) ts Name.context;
   330           val vs = Name.names ctxt "a" tys;
   331         in
   332           fold_map (exprgen_typ thy algbr funcgr) tys
   333           ##>> exprgen_case thy algbr funcgr n cases ((c, ty), ts @ map Free vs)
   334           #>> (fn (tys, t) => map2 (fn (v, _) => pair v) vs tys `|--> t)
   335         end
   336       else if length ts > i then
   337         exprgen_case thy algbr funcgr n cases ((c, ty), Library.take (i, ts))
   338         ##>> fold_map (exprgen_term thy algbr funcgr) (Library.drop (i, ts))
   339         #>> (fn (t, ts) => t `$$ ts)
   340       else
   341         exprgen_case thy algbr funcgr n cases ((c, ty), ts)
   342       end
   343   | NONE => exprgen_app_default thy algbr funcgr ((c, ty), ts);
   344 
   345 
   346 (* entrance points into translation kernel *)
   347 
   348 fun ensure_def_const' thy algbr funcgr c trns =
   349   ensure_def_const thy algbr funcgr c trns
   350   handle CONSTRAIN ((c, ty), ty_decl) => error (
   351     "Constant " ^ c ^ " with most general type\n"
   352     ^ CodeUnit.string_of_typ thy ty
   353     ^ "\noccurs with type\n"
   354     ^ CodeUnit.string_of_typ thy ty_decl);
   355 
   356 fun perhaps_def_const thy algbr funcgr c trns =
   357   case try (ensure_def_const thy algbr funcgr c) trns
   358    of SOME (c, trns) => (SOME c, trns)
   359     | NONE => (NONE, trns);
   360 
   361 fun exprgen_term' thy algbr funcgr t trns =
   362   exprgen_term thy algbr funcgr t trns
   363   handle CONSTRAIN ((c, ty), ty_decl) => error ("In term " ^ (quote o Sign.string_of_term thy) t
   364     ^ ",\nconstant " ^ c ^ " with most general type\n"
   365     ^ CodeUnit.string_of_typ thy ty
   366     ^ "\noccurs with type\n"
   367     ^ CodeUnit.string_of_typ thy ty_decl);
   368 
   369 
   370 (** code generation interfaces **)
   371 
   372 (* generic generation combinators *)
   373 
   374 fun generate thy funcgr gen it =
   375   let
   376     val naming = NameSpace.qualified_names NameSpace.default_naming;
   377     val consttab = Consts.empty
   378       |> fold (fn c => Consts.declare true naming [] (c, CodeFuncgr.typ funcgr c))
   379            (CodeFuncgr.all funcgr);
   380     val algbr = (Code.operational_algebra thy, consttab);
   381   in   
   382     Program.change_yield thy
   383       (CodeThingol.start_transact (gen thy algbr funcgr it))
   384     |> fst
   385   end;
   386 
   387 fun code thy permissive cs seris =
   388   let
   389     val code = Program.get thy;
   390     val seris' = map (fn (((target, module), file), args) =>
   391       CodeTarget.get_serializer thy target permissive module file args
   392         CodeName.labelled_name cs) seris;
   393   in (map (fn f => f code) seris' : unit list; ()) end;
   394 
   395 fun raw_eval evaluate term_of thy g =
   396   let
   397     val value_name = "Isabelle_Eval.EVAL.EVAL";
   398     fun ensure_eval thy algbr funcgr t = 
   399       let
   400         val ty = fastype_of t;
   401         val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
   402           o dest_TFree))) t [];
   403         val defgen_eval =
   404           fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   405           ##>> exprgen_typ thy algbr funcgr ty
   406           ##>> exprgen_term' thy algbr funcgr t
   407           #>> (fn ((vs, ty), t) => CodeThingol.Fun ((vs, ty), [(([], t), Drule.dummy_thm)]));
   408         fun result (dep, code) =
   409           let
   410             val CodeThingol.Fun ((vs, ty), [(([], t), _)]) = Graph.get_node code value_name;
   411             val deps = Graph.imm_succs code value_name;
   412             val code' = Graph.del_nodes [value_name] code;
   413             val code'' = CodeThingol.project_code false [] (SOME deps) code';
   414           in ((code'', ((vs, ty), t), deps), (dep, code')) end;
   415       in
   416         ensure_def thy defgen_eval value_name
   417         #> result
   418       end;
   419     fun h funcgr ct =
   420       let
   421         val (code, vs_ty_t, deps) = generate thy funcgr ensure_eval (term_of ct);
   422       in g code vs_ty_t deps ct end;
   423   in evaluate thy h end;
   424 
   425 fun eval_conv thy = raw_eval CodeFuncgr.eval_conv Thm.term_of thy;
   426 fun eval_term thy = raw_eval CodeFuncgr.eval_term I thy;
   427 
   428 fun eval_invoke thy = CodeTarget.eval_invoke thy CodeName.labelled_name;
   429 
   430 val satisfies_ref : (unit -> bool) option ref = ref NONE;
   431 
   432 fun satisfies thy t witnesses =
   433   let
   434     fun evl code ((vs, ty), t) deps ct =
   435       eval_invoke thy ("CodePackage.satisfies_ref", satisfies_ref)
   436         code (t, ty) witnesses;
   437   in eval_term thy evl t end;
   438 
   439 fun filter_generatable thy consts =
   440   let
   441     val (consts', funcgr) = CodeFuncgr.make_consts thy consts;
   442     val consts'' = generate thy funcgr (fold_map ooo perhaps_def_const) consts';
   443     val consts''' = map_filter (fn (const, SOME _) => SOME const | (_, NONE) => NONE)
   444       (consts' ~~ consts'');
   445   in consts''' end;
   446 
   447 fun generate_const_exprs thy raw_cs =
   448   let
   449     val (perm1, cs) = CodeUnit.read_const_exprs thy
   450       (filter_generatable thy) raw_cs;
   451     val (perm2, cs') = case generate thy (CodeFuncgr.make thy cs)
   452       (fold_map ooo ensure_def_const') cs
   453      of [] => (true, NONE)
   454       | cs => (false, SOME cs);
   455   in (perm1 orelse perm2, cs') end;
   456 
   457 
   458 (** code properties **)
   459 
   460 fun mk_codeprops thy all_cs sel_cs =
   461   let
   462     fun select (thmref, thm) = case try (Drule.unvarify o Drule.zero_var_indexes) thm
   463      of NONE => NONE
   464       | SOME thm => let
   465           val t = (ObjectLogic.drop_judgment thy o Thm.prop_of) thm;
   466           val cs = fold_aterms (fn Const (c, ty) =>
   467             cons (Class.unoverload_const thy (c, ty)) | _ => I) t [];
   468         in if exists (member (op =) sel_cs) cs
   469           andalso forall (member (op =) all_cs) cs
   470           then SOME (thmref, thm) else NONE end;
   471     fun mk_codeprop (thmref, thm) =
   472       let
   473         val t = ObjectLogic.drop_judgment thy (Thm.prop_of thm);
   474         val ty_judg = fastype_of t;
   475         val tfrees1 = fold_aterms (fn Const (c, ty) =>
   476           Term.add_tfreesT ty | _ => I) t [];
   477         val vars = Term.add_frees t [];
   478         val tfrees2 = fold (Term.add_tfreesT o snd) vars [];
   479         val tfrees' = subtract (op =) tfrees2 tfrees1 |> map TFree;
   480         val ty = map Term.itselfT tfrees' @ map snd vars ---> ty_judg;
   481         val tfree_vars = map Logic.mk_type tfrees';
   482         val c = PureThy.string_of_thmref thmref
   483           |> NameSpace.explode
   484           |> (fn [x] => [x] | (x::xs) => xs)
   485           |> space_implode "_"
   486         val propdef = (((c, ty), tfree_vars @ map Free vars), t);
   487       in if c = "" then NONE else SOME (thmref, propdef) end;
   488   in
   489     PureThy.thms_containing thy ([], [])
   490     |> maps PureThy.selections
   491     |> map_filter select
   492     |> map_filter mk_codeprop
   493   end;
   494 
   495 fun add_codeprops all_cs sel_cs thy =
   496   let
   497     val codeprops = mk_codeprops thy all_cs sel_cs;
   498     fun lift_name_yield f x = (Name.context, x) |> f ||> snd;
   499     fun add (thmref, (((raw_c, ty), ts), t)) (names, thy) =
   500       let
   501         val _ = warning ("Adding theorem " ^ quote (PureThy.string_of_thmref thmref)
   502           ^ " as code property " ^ quote raw_c);
   503         val ([raw_c'], names') = Name.variants [raw_c] names;
   504       in
   505         thy
   506         |> PureThy.simple_def ("", []) (((raw_c', ty, Syntax.NoSyn), ts), t)
   507         ||> pair names'
   508       end;
   509   in
   510     thy
   511     |> Sign.sticky_prefix "codeprop"
   512     |> lift_name_yield (fold_map add codeprops)
   513     ||> Sign.restore_naming thy
   514     |-> (fn c_thms => fold (Code.add_func o snd) c_thms #> pair c_thms)
   515   end;
   516 
   517 
   518 
   519 (** toplevel interface and setup **)
   520 
   521 local
   522 
   523 structure P = OuterParse
   524 and K = OuterKeyword
   525 
   526 fun code_cmd raw_cs seris thy =
   527   let
   528     val (permissive, cs) = generate_const_exprs thy raw_cs;
   529     val _ = code thy permissive cs seris;
   530   in () end;
   531 
   532 fun code_thms_cmd thy =
   533   code_thms thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
   534 
   535 fun code_deps_cmd thy =
   536   code_deps thy o snd o CodeUnit.read_const_exprs thy (fst o CodeFuncgr.make_consts thy);
   537 
   538 fun code_props_cmd raw_cs seris thy =
   539   let
   540     val (_, all_cs) = generate_const_exprs thy ["*"];
   541     val (permissive, cs) = generate_const_exprs thy raw_cs;
   542     val (c_thms, thy') = add_codeprops (map (the o CodeName.const_rev thy) (these all_cs))
   543       (map (the o CodeName.const_rev thy) (these cs)) thy;
   544     val prop_cs = (filter_generatable thy' o map fst) c_thms;
   545     val _ = if null seris then [] else generate thy' (CodeFuncgr.make thy' prop_cs)
   546       (fold_map ooo ensure_def_const') prop_cs;
   547     val _ = if null seris then () else code thy' permissive
   548       (SOME (map (CodeName.const thy') prop_cs)) seris;
   549   in thy' end;
   550 
   551 val (inK, module_nameK, fileK) = ("in", "module_name", "file");
   552 
   553 fun code_exprP cmd =
   554   (Scan.repeat P.term
   555   -- Scan.repeat (P.$$$ inK |-- P.name
   556      -- Scan.option (P.$$$ module_nameK |-- P.name)
   557      -- Scan.option (P.$$$ fileK |-- P.name)
   558      -- Scan.optional (P.$$$ "(" |-- P.arguments --| P.$$$ ")") []
   559   ) >> (fn (raw_cs, seris) => cmd raw_cs seris));
   560 
   561 val _ = OuterSyntax.keywords [inK, module_nameK, fileK];
   562 
   563 val (codeK, code_thmsK, code_depsK, code_propsK) =
   564   ("export_code", "code_thms", "code_deps", "code_props");
   565 
   566 in
   567 
   568 val _ =
   569   OuterSyntax.improper_command codeK "generate executable code for constants"
   570     K.diag (P.!!! (code_exprP code_cmd) >> (fn f => Toplevel.keep (f o Toplevel.theory_of)));
   571 
   572 fun codegen_command thy cmd =
   573   case Scan.read OuterLex.stopper (P.!!! (code_exprP code_cmd)) ((filter OuterLex.is_proper o OuterSyntax.scan) cmd)
   574    of SOME f => (writeln "Now generating code..."; f thy)
   575     | NONE => error ("Bad directive " ^ quote cmd);
   576 
   577 val _ =
   578   OuterSyntax.improper_command code_thmsK "print system of defining equations for code" OuterKeyword.diag
   579     (Scan.repeat P.term
   580       >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
   581         o Toplevel.keep ((fn thy => code_thms_cmd thy cs) o Toplevel.theory_of)));
   582 
   583 val _ =
   584   OuterSyntax.improper_command code_depsK "visualize dependencies of defining equations for code" OuterKeyword.diag
   585     (Scan.repeat P.term
   586       >> (fn cs => Toplevel.no_timing o Toplevel.unknown_theory
   587         o Toplevel.keep ((fn thy => code_deps_cmd thy cs) o Toplevel.theory_of)));
   588 
   589 val _ =
   590   OuterSyntax.command code_propsK "generate characteristic properties for executable constants"
   591     K.thy_decl (P.!!! (code_exprP code_props_cmd) >> Toplevel.theory);
   592 
   593 end; (*local*)
   594 
   595 end; (*struct*)