src/HOL/Tools/datatype_codegen.ML
changeset 24626 85eceef2edc7
parent 24624 b8383b1bbae3
child 24699 c6674504103f
equal deleted inserted replaced
24625:0398a5e802d3 24626:85eceef2edc7
    15 
    15 
    16   type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list
    16   type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list
    17     -> theory -> theory
    17     -> theory -> theory
    18   val codetype_hook: hook
    18   val codetype_hook: hook
    19   val eq_hook: hook
    19   val eq_hook: hook
    20   val codetypes_dependency: theory -> (string * bool) list list
    20   val add_codetypes_hook: hook -> theory -> theory
    21   val add_codetypes_hook_bootstrap: hook -> theory -> theory
       
    22   val the_codetypes_mut_specs: theory -> (string * bool) list
    21   val the_codetypes_mut_specs: theory -> (string * bool) list
    23     -> ((string * sort) list * (string * (bool * (string * typ list) list)) list)
    22     -> ((string * sort) list * (string * (bool * (string * typ list) list)) list)
    24   val get_codetypes_arities: theory -> (string * bool) list -> sort
    23   val get_codetypes_arities: theory -> (string * bool) list -> sort
    25     -> (string * (arity * term list)) list option
    24     -> (string * (arity * term list)) list
    26   val prove_codetypes_arities: tactic -> (string * bool) list -> sort
    25   val prove_codetypes_arities: tactic -> (string * bool) list -> sort
    27     -> (arity list -> (string * term list) list -> theory
    26     -> (arity list -> (string * term list) list -> theory
    28       -> ((bstring * Attrib.src list) * term) list * theory)
    27       -> ((bstring * Attrib.src list) * term) list * theory)
    29     -> (arity list -> (string * term list) list -> thm list -> theory -> theory)
    28     -> (arity list -> (string * term list) list -> thm list -> theory -> theory)
    30     -> theory -> theory
    29     -> theory -> theory
   441 
   440 
   442 (** codetypes for code 2nd generation **)
   441 (** codetypes for code 2nd generation **)
   443 
   442 
   444 (* abstraction over datatypes vs. type copies *)
   443 (* abstraction over datatypes vs. type copies *)
   445 
   444 
       
   445 fun get_spec thy (dtco, true) =
       
   446       (the o DatatypePackage.get_datatype_spec thy) dtco
       
   447   | get_spec thy (tyco, false) =
       
   448       TypecopyPackage.get_spec thy tyco;
       
   449 
   446 fun codetypes_dependency thy =
   450 fun codetypes_dependency thy =
   447   let
   451   let
   448     val names =
   452     val names =
   449       map (rpair true) (Symtab.keys (DatatypePackage.get_datatypes thy))
   453       map (rpair true) (Symtab.keys (DatatypePackage.get_datatypes thy))
   450         @ map (rpair false) (TypecopyPackage.get_typecopies thy);
   454         @ map (rpair false) (TypecopyPackage.get_typecopies thy);
   469     Graph.empty
   473     Graph.empty
   470     |> fold add_node names
   474     |> fold add_node names
   471     |> Graph.strong_conn
   475     |> Graph.strong_conn
   472     |> map (AList.make (the o AList.lookup (op =) names))
   476     |> map (AList.make (the o AList.lookup (op =) names))
   473   end;
   477   end;
   474 
       
   475 fun get_spec thy (dtco, true) =
       
   476       (the o DatatypePackage.get_datatype_spec thy) dtco
       
   477   | get_spec thy (tyco, false) =
       
   478       TypecopyPackage.get_spec thy tyco;
       
   479 
   478 
   480 local
   479 local
   481   fun get_eq_thms thy tyco = case DatatypePackage.get_datatype thy tyco
   480   fun get_eq_thms thy tyco = case DatatypePackage.get_datatype thy tyco
   482    of SOME _ => get_eq_datatype thy tyco
   481    of SOME _ => get_eq_datatype thy tyco
   483     | NONE => [TypecopyPackage.get_eq thy tyco];
   482     | NONE => [TypecopyPackage.get_eq thy tyco];
   504 end;
   503 end;
   505 
   504 
   506 type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list
   505 type hook = (string * (bool * ((string * sort) list * (string * typ list) list))) list
   507   -> theory -> theory;
   506   -> theory -> theory;
   508 
   507 
   509 fun add_codetypes_hook_bootstrap hook thy =
   508 fun add_codetypes_hook hook thy =
   510   let
   509   let
   511     fun add_spec thy (tyco, is_dt) =
   510     fun add_spec thy (tyco, is_dt) =
   512       (tyco, (is_dt, get_spec thy (tyco, is_dt)));
   511       (tyco, (is_dt, get_spec thy (tyco, is_dt)));
   513     fun datatype_hook dtcos thy =
   512     fun datatype_hook dtcos thy =
   514       hook (map (add_spec thy) (map (rpair true) dtcos)) thy;
   513       hook (map (add_spec thy) (map (rpair true) dtcos)) thy;
   515     fun typecopy_hook ((tyco, _)) thy =
   514     fun typecopy_hook ((tyco, _)) thy =
   516       hook ([(tyco, (false, TypecopyPackage.get_spec thy tyco))]) thy;
   515       hook ([(tyco, (false, TypecopyPackage.get_spec thy tyco))]) thy;
   517   in
   516   in
   518     thy
   517     thy
   519     |> fold hook ((map o map) (add_spec thy) (codetypes_dependency thy))
   518     |> fold hook ((map o map) (add_spec thy) (codetypes_dependency thy))
   520     |> DatatypeHooks.add datatype_hook
   519     |> DatatypePackage.add_interpretator datatype_hook
   521     |> TypecopyPackage.add_hook typecopy_hook
   520     |> TypecopyPackage.add_interpretator typecopy_hook
   522   end;
   521   end;
   523 
   522 
   524 fun the_codetypes_mut_specs thy ([(tyco, is_dt)]) =
   523 fun the_codetypes_mut_specs thy ([(tyco, is_dt)]) =
   525       let
   524       let
   526         val (vs, cs) = get_spec thy (tyco, is_dt)
   525         val (vs, cs) = get_spec thy (tyco, is_dt)
   570         val tys' = (map o Term.map_type_tfree) (TFree o inst) tys;
   569         val tys' = (map o Term.map_type_tfree) (TFree o inst) tys;
   571         val ts = Name.names Name.context "a" tys';
   570         val ts = Name.names Name.context "a" tys';
   572         val ty = (tys' ---> Type (tyco, map TFree vs'));
   571         val ty = (tys' ---> Type (tyco, map TFree vs'));
   573       in list_comb (Const (c, ty), map Free ts) end;
   572       in list_comb (Const (c, ty), map Free ts) end;
   574   in
   573   in
   575     map (fn (tyco, cs) => (tyco, (mk_arity tyco, map (mk_cons tyco) cs))) css |> SOME
   574     map (fn (tyco, cs) => (tyco, (mk_arity tyco, map (mk_cons tyco) cs))) css
   576   end handle Class_Error => NONE;
   575   end;
   577 
   576 
   578 fun prove_codetypes_arities tac tycos sort f after_qed thy =
   577 fun prove_codetypes_arities tac tycos sort f after_qed thy =
   579   case get_codetypes_arities thy tycos sort
   578   case try (get_codetypes_arities thy tycos) sort
   580    of NONE => thy
   579    of NONE => thy
   581     | SOME insts => let
   580     | SOME insts => let
   582         fun proven (tyco, asorts, sort) =
   581         fun proven (tyco, asorts, sort) =
   583           Sorts.of_sort (Sign.classes_of thy)
   582           Sorts.of_sort (Sign.classes_of thy)
   584             (Type (tyco, map TFree (Name.names Name.context "'a" asorts)), sort);
   583             (Type (tyco, map TFree (Name.names Name.context "'a" asorts)), sort);
   632     fold_rev Code.add_default_func case_rewrites thy
   631     fold_rev Code.add_default_func case_rewrites thy
   633   end;
   632   end;
   634 
   633 
   635 val setup = 
   634 val setup = 
   636   add_codegen "datatype" datatype_codegen
   635   add_codegen "datatype" datatype_codegen
   637   #> add_tycodegen "datatype" datatype_tycodegen 
   636   #> add_tycodegen "datatype" datatype_tycodegen
   638   #> DatatypeHooks.add (fold add_datatype_case_const)
   637   #> DatatypePackage.add_interpretator (fold add_datatype_case_const)
   639   #> DatatypeHooks.add (fold add_datatype_case_defs)
   638   #> DatatypePackage.add_interpretator (fold add_datatype_case_defs)
   640 
   639 
   641 val setup_hooks =
   640 val setup_hooks =
   642   add_codetypes_hook_bootstrap codetype_hook
   641   add_codetypes_hook codetype_hook
   643   #> add_codetypes_hook_bootstrap eq_hook
   642   #> add_codetypes_hook eq_hook
   644 
   643 
   645 
   644 
   646 end;
   645 end;