src/Tools/code/code_package.ML
changeset 24835 8c26128f8997
parent 24811 3bf788a0c49a
child 24844 98c006a30218
equal deleted inserted replaced
24834:5684cbf8c895 24835:8c26128f8997
    11     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    11     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    12        -> string list -> cterm -> thm)
    12        -> string list -> cterm -> thm)
    13     -> cterm -> thm;
    13     -> cterm -> thm;
    14   val eval_term: theory
    14   val eval_term: theory
    15     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    15     -> (CodeThingol.code -> CodeThingol.typscheme * CodeThingol.iterm
    16        -> string list -> cterm -> 'a)
    16        -> string list -> term -> 'a)
    17     -> cterm -> 'a;
    17     -> term -> 'a;
    18   val satisfies_ref: (unit -> bool) option ref;
    18   val satisfies_ref: (unit -> bool) option ref;
    19   val satisfies: theory -> cterm -> string list -> bool;
    19   val satisfies: theory -> term -> string list -> bool;
    20   val eval_invoke: theory -> (string * (unit -> 'a) option ref) -> CodeThingol.code
    20   val eval_invoke: theory -> (string * (unit -> 'a) option ref) -> CodeThingol.code
    21     -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
    21     -> CodeThingol.iterm * CodeThingol.itype -> string list -> 'a;
    22   val codegen_command: theory -> string -> unit;
    22   val codegen_command: theory -> string -> unit;
    23 
    23 
    24   type appgen;
    24   type appgen;
   211     #>> rpair thm
   211     #>> rpair thm
   212   end
   212   end
   213 and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) =
   213 and ensure_def_inst thy (algbr as ((_, algebra), _)) funcgr (class, tyco) =
   214   let
   214   let
   215     val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
   215     val superclasses = (Sorts.certify_sort algebra o Sorts.super_classes algebra) class;
   216     val (var, classops) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
   216     val (var, classparams) = try (AxClass.params_of_class thy) class |> the_default ("'a", [])
   217     val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
   217     val vs = Name.names Name.context "'a" (Sorts.mg_domain algebra tyco [class]);
   218     val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
   218     val sorts' = Sorts.mg_domain (Sign.classes_of thy) tyco [class];
   219     val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
   219     val vs' = map2 (fn (v, sort1) => fn sort2 => (v,
   220       Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
   220       Sorts.inter_sort (Sign.classes_of thy) (sort1, sort2))) vs sorts';
   221     val arity_typ = Type (tyco, map TFree vs);
   221     val arity_typ = Type (tyco, map TFree vs);
   222     val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
   222     val arity_typ' = Type (tyco, map (fn (v, sort) => TVar ((v, 0), sort)) vs');
   223     fun gen_superarity superclass =
   223     fun exprgen_superarity superclass =
   224       ensure_def_class thy algbr funcgr superclass
   224       ensure_def_class thy algbr funcgr superclass
   225       ##>> ensure_def_classrel thy algbr funcgr (class, superclass)
   225       ##>> ensure_def_classrel thy algbr funcgr (class, superclass)
   226       ##>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass])
   226       ##>> exprgen_dicts thy algbr funcgr (arity_typ, [superclass])
   227       #>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
   227       #>> (fn ((superclass, classrel), [DictConst (inst, dss)]) =>
   228             (superclass, (classrel, (inst, dss))));
   228             (superclass, (classrel, (inst, dss))));
   229     fun gen_classop_def (c, ty) =
   229     fun exprgen_classparam_inst (c, ty) =
   230       let
   230       let
   231         val c_inst = Const (c, map_type_tfree (K arity_typ') ty);
   231         val c_inst = Const (c, map_type_tfree (K arity_typ') ty);
   232         val thm = Class.unoverload thy (Thm.cterm_of thy c_inst);
   232         val thm = Class.unoverload thy (Thm.cterm_of thy c_inst);
   233         val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd
   233         val c_ty = (apsnd Logic.unvarifyT o dest_Const o snd
   234           o Logic.dest_equals o Thm.prop_of) thm;
   234           o Logic.dest_equals o Thm.prop_of) thm;
   239       end;
   239       end;
   240     val defgen_inst =
   240     val defgen_inst =
   241       ensure_def_class thy algbr funcgr class
   241       ensure_def_class thy algbr funcgr class
   242       ##>> ensure_def_tyco thy algbr funcgr tyco
   242       ##>> ensure_def_tyco thy algbr funcgr tyco
   243       ##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   243       ##>> fold_map (exprgen_tyvar_sort thy algbr funcgr) vs
   244       ##>> fold_map gen_superarity superclasses
   244       ##>> fold_map exprgen_superarity superclasses
   245       ##>> fold_map gen_classop_def classops
   245       ##>> fold_map exprgen_classparam_inst classparams
   246       #>> (fn ((((class, tyco), arity), superarities), classops) =>
   246       #>> (fn ((((class, tyco), arity), superarities), classparams) =>
   247              CodeThingol.Classinst ((class, (tyco, arity)), (superarities, classops)));
   247              CodeThingol.Classinst ((class, (tyco, arity)), (superarities, classparams)));
   248     val inst = CodeName.instance thy (class, tyco);
   248     val inst = CodeName.instance thy (class, tyco);
   249   in
   249   in
   250     ensure_def thy defgen_inst inst
   250     ensure_def thy defgen_inst inst
   251     #> pair inst
   251     #> pair inst
   252   end
   252   end
   254   let
   254   let
   255     val c' = CodeName.const thy c;
   255     val c' = CodeName.const thy c;
   256     fun defgen_datatypecons tyco =
   256     fun defgen_datatypecons tyco =
   257       ensure_def_tyco thy algbr funcgr tyco
   257       ensure_def_tyco thy algbr funcgr tyco
   258       #>> K (CodeThingol.Datatypecons c');
   258       #>> K (CodeThingol.Datatypecons c');
   259     fun defgen_classop class =
   259     fun defgen_classparam class =
   260       ensure_def_class thy algbr funcgr class
   260       ensure_def_class thy algbr funcgr class
   261       #>> K (CodeThingol.Classop c');
   261       #>> K (CodeThingol.Classparam c');
   262     fun defgen_fun trns =
   262     fun defgen_fun trns =
   263       let
   263       let
   264         val raw_thms = CodeFuncgr.funcs funcgr c;
   264         val raw_thms = CodeFuncgr.funcs funcgr c;
   265         val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) c;
   265         val ty = (Logic.unvarifyT o CodeFuncgr.typ funcgr) c;
   266         val vs = (map dest_TFree o Consts.typargs consts) (c, ty);
   266         val vs = (map dest_TFree o Consts.typargs consts) (c, ty);
   275         |>> (fn ((vs, ty), eqs) => CodeThingol.Fun ((vs, ty), eqs))
   275         |>> (fn ((vs, ty), eqs) => CodeThingol.Fun ((vs, ty), eqs))
   276       end;
   276       end;
   277     val defgen = case Code.get_datatype_of_constr thy c
   277     val defgen = case Code.get_datatype_of_constr thy c
   278      of SOME tyco => defgen_datatypecons tyco
   278      of SOME tyco => defgen_datatypecons tyco
   279       | NONE => (case AxClass.class_of_param thy c
   279       | NONE => (case AxClass.class_of_param thy c
   280          of SOME class => defgen_classop class
   280          of SOME class => defgen_classparam class
   281           | NONE => defgen_fun)
   281           | NONE => defgen_fun)
   282   in
   282   in
   283     ensure_def thy defgen c'
   283     ensure_def thy defgen c'
   284     #> pair c'
   284     #> pair c'
   285   end
   285   end
   286 and exprgen_term thy algbr funcgr (Const (c, ty)) =
   286 and exprgen_term thy algbr funcgr (Const (c, ty)) =
   287       select_appgen thy algbr funcgr ((c, ty), [])
   287       exprgen_app thy algbr funcgr ((c, ty), [])
   288   | exprgen_term thy algbr funcgr (Free (v, _)) =
   288   | exprgen_term thy algbr funcgr (Free (v, _)) =
   289       pair (IVar v)
   289       pair (IVar v)
   290   | exprgen_term thy algbr funcgr (Abs (abs as (_, ty, _))) =
   290   | exprgen_term thy algbr funcgr (Abs (abs as (_, ty, _))) =
   291       let
   291       let
   292         val (v, t) = Syntax.variant_abs abs;
   292         val (v, t) = Syntax.variant_abs abs;
   296         #>> (fn (ty, t) => (v, ty) `|-> t)
   296         #>> (fn (ty, t) => (v, ty) `|-> t)
   297       end
   297       end
   298   | exprgen_term thy algbr funcgr (t as _ $ _) =
   298   | exprgen_term thy algbr funcgr (t as _ $ _) =
   299       case strip_comb t
   299       case strip_comb t
   300        of (Const (c, ty), ts) =>
   300        of (Const (c, ty), ts) =>
   301             select_appgen thy algbr funcgr ((c, ty), ts)
   301             exprgen_app thy algbr funcgr ((c, ty), ts)
   302         | (t', ts) =>
   302         | (t', ts) =>
   303             exprgen_term thy algbr funcgr t'
   303             exprgen_term thy algbr funcgr t'
   304             ##>> fold_map (exprgen_term thy algbr funcgr) ts
   304             ##>> fold_map (exprgen_term thy algbr funcgr) ts
   305             #>> (fn (t, ts) => t `$$ ts)
   305             #>> (fn (t, ts) => t `$$ ts)
   306 and exprgen_const thy algbr funcgr (c, ty) =
   306 and exprgen_const thy algbr funcgr (c, ty) =
   311   #>> (fn ((c, iss), tys) => IConst (c, (iss, tys)))
   311   #>> (fn ((c, iss), tys) => IConst (c, (iss, tys)))
   312 and exprgen_app_default thy algbr funcgr (c_ty, ts) =
   312 and exprgen_app_default thy algbr funcgr (c_ty, ts) =
   313   exprgen_const thy algbr funcgr c_ty
   313   exprgen_const thy algbr funcgr c_ty
   314   ##>> fold_map (exprgen_term thy algbr funcgr) ts
   314   ##>> fold_map (exprgen_term thy algbr funcgr) ts
   315   #>> (fn (t, ts) => t `$$ ts)
   315   #>> (fn (t, ts) => t `$$ ts)
   316 and select_appgen thy algbr funcgr ((c, ty), ts) =
   316 and exprgen_app thy algbr funcgr ((c, ty), ts) =
   317   case Symtab.lookup (Appgens.get thy) c
   317   case Symtab.lookup (Appgens.get thy) c
   318    of SOME (i, (appgen, _)) =>
   318    of SOME (i, (appgen, _)) =>
   319         if length ts < i then
   319         if length ts < i then
   320           let
   320           let
   321             val k = length ts;
   321             val k = length ts;
   428 fun code thy permissive cs seris =
   428 fun code thy permissive cs seris =
   429   let
   429   let
   430     val code = Program.get thy;
   430     val code = Program.get thy;
   431     val seris' = map (fn (((target, module), file), args) =>
   431     val seris' = map (fn (((target, module), file), args) =>
   432       CodeTarget.get_serializer thy target permissive module file args
   432       CodeTarget.get_serializer thy target permissive module file args
   433         CodeName.labelled_name (K false) cs) seris;
   433         CodeName.labelled_name cs) seris;
   434   in (map (fn f => f code) seris' : unit list; ()) end;
   434   in (map (fn f => f code) seris' : unit list; ()) end;
   435 
   435 
   436 fun raw_eval f thy g =
   436 fun raw_eval evaluate term_of thy g =
   437   let
   437   let
   438     val value_name = "Isabelle_Eval.EVAL.EVAL";
   438     val value_name = "Isabelle_Eval.EVAL.EVAL";
   439     fun ensure_eval thy algbr funcgr t = 
   439     fun ensure_eval thy algbr funcgr t = 
   440       let
   440       let
   441         val ty = fastype_of t;
   441         val ty = fastype_of t;
   457         ensure_def thy defgen_eval value_name
   457         ensure_def thy defgen_eval value_name
   458         #> result
   458         #> result
   459       end;
   459       end;
   460     fun h funcgr ct =
   460     fun h funcgr ct =
   461       let
   461       let
   462         val (code, vs_ty_t, deps) = generate thy funcgr ensure_eval (Thm.term_of ct);
   462         val (code, vs_ty_t, deps) = generate thy funcgr ensure_eval (term_of ct);
   463       in g code vs_ty_t deps ct end;
   463       in g code vs_ty_t deps ct end;
   464   in f thy h end;
   464   in evaluate thy h end;
   465 
   465 
   466 fun eval_conv thy = raw_eval CodeFuncgr.eval_conv thy;
   466 fun eval_conv thy = raw_eval CodeFuncgr.eval_conv Thm.term_of thy;
   467 fun eval_term thy = raw_eval CodeFuncgr.eval_term thy;
   467 fun eval_term thy = raw_eval CodeFuncgr.eval_term I thy;
   468 
   468 
   469 fun eval_invoke thy = CodeTarget.eval_invoke thy CodeName.labelled_name (K false);
   469 fun eval_invoke thy = CodeTarget.eval_invoke thy CodeName.labelled_name;
   470 
   470 
   471 val satisfies_ref : (unit -> bool) option ref = ref NONE;
   471 val satisfies_ref : (unit -> bool) option ref = ref NONE;
   472 
   472 
   473 fun satisfies thy ct witnesses =
   473 fun satisfies thy t witnesses =
   474   let
   474   let
   475     fun evl code ((vs, ty), t) deps ct =
   475     fun evl code ((vs, ty), t) deps ct =
   476       eval_invoke thy ("CodePackage.satisfies_ref", satisfies_ref)
   476       eval_invoke thy ("CodePackage.satisfies_ref", satisfies_ref)
   477         code (t, ty) witnesses;
   477         code (t, ty) witnesses;
   478   in eval_term thy evl ct end;
   478   in eval_term thy evl t end;
   479 
   479 
   480 fun filter_generatable thy consts =
   480 fun filter_generatable thy consts =
   481   let
   481   let
   482     val (consts', funcgr) = CodeFuncgr.make_consts thy consts;
   482     val (consts', funcgr) = CodeFuncgr.make_consts thy consts;
   483     val consts'' = generate thy funcgr (fold_map ooo perhaps_def_const) consts';
   483     val consts'' = generate thy funcgr (fold_map ooo perhaps_def_const) consts';