1 (* Title: Doc/Datatypes/Datatypes.thy |
1 (* Title: Doc/Datatypes/Datatypes.thy |
|
2 Author: Julian Biendarra, TU Muenchen |
2 Author: Jasmin Blanchette, TU Muenchen |
3 Author: Jasmin Blanchette, TU Muenchen |
3 Author: Martin Desharnais, Ecole de technologie superieure |
4 Author: Martin Desharnais, Ecole de technologie superieure |
4 Author: Lorenz Panny, TU Muenchen |
5 Author: Lorenz Panny, TU Muenchen |
5 Author: Andrei Popescu, TU Muenchen |
6 Author: Andrei Popescu, TU Muenchen |
6 Author: Dmitriy Traytel, TU Muenchen |
7 Author: Dmitriy Traytel, TU Muenchen |
2790 necessary. See \verb|~~/src/HOL/Basic_BNFs.thy|, |
2791 necessary. See \verb|~~/src/HOL/Basic_BNFs.thy|, |
2791 \verb|~~/src/HOL/Library/FSet.thy|, and \verb|~~/src/HOL/Library/Multiset.thy| |
2792 \verb|~~/src/HOL/Library/FSet.thy|, and \verb|~~/src/HOL/Library/Multiset.thy| |
2792 for further examples of BNF registration, some of which feature custom |
2793 for further examples of BNF registration, some of which feature custom |
2793 witnesses. |
2794 witnesses. |
2794 |
2795 |
2795 For many typedefs (in particular for type copies) lifting the BNF structure |
2796 For many typedefs, lifting the BNF structure from the raw type to the abstract |
2796 from the raw type to the abstract type can be done uniformly. This is the task |
2797 type can be done uniformly. This is the task of the @{command lift_bnf} command. |
2797 of the @{command lift_bnf} command. Using @{command lift_bnf} the above |
2798 Using @{command lift_bnf}, the above registration of @{typ "('d, 'a) fn"} as a |
2798 registration of @{typ "('d, 'a) fn"} as a BNF becomes much shorter. |
2799 BNF becomes much shorter: |
2799 *} |
2800 *} |
2800 |
2801 |
2801 (*<*) context early begin (*>*) |
2802 (*<*) |
2802 lift_bnf (*<*)(no_warn_wits)(*>*) ('d, 'a) fn by auto |
2803 context early |
2803 (*<*) end (*>*) |
2804 begin |
2804 |
2805 (*>*) |
2805 text {* |
2806 lift_bnf (*<*)(no_warn_wits) (*>*)('d, 'a) fn |
2806 Indeed, for type copies the proof obligations are so simple that they can be |
2807 by auto |
2807 discharged automatically, yielding another command @{command copy_bnf} which |
2808 (*<*) |
2808 does not issue proof obligations. |
2809 end |
2809 *} |
2810 (*>*) |
2810 |
2811 |
2811 (*<*) context late begin (*>*) |
2812 text {* |
|
2813 For type copies (@{command typedef}s with @{term UNIV} as the representing set), |
|
2814 the proof obligations are so simple that they can be |
|
2815 discharged automatically, yielding another command, @{command copy_bnf}, which |
|
2816 does not emit any proof obligations: |
|
2817 *} |
|
2818 |
|
2819 (*<*) |
|
2820 context late |
|
2821 begin |
|
2822 (*>*) |
2812 copy_bnf ('d, 'a) fn |
2823 copy_bnf ('d, 'a) fn |
2813 (*<*) end (*>*) |
2824 (*<*) |
2814 |
2825 end |
2815 text {* |
2826 (*>*) |
2816 Since records (or rather record schemes) are particular type copies, |
2827 |
2817 the @{command copy_bnf} can be used to register records as BNFs. |
2828 text {* |
|
2829 Since record schemas are type copies, @{command copy_bnf} can be used to |
|
2830 register them as BNFs: |
2818 *} |
2831 *} |
2819 |
2832 |
2820 record 'a point = |
2833 record 'a point = |
2821 xval :: 'a |
2834 xval :: 'a |
2822 yval :: 'a |
2835 yval :: 'a |
|
2836 |
|
2837 text {* \blankline *} |
2823 |
2838 |
2824 copy_bnf ('a, 'z) point_ext |
2839 copy_bnf ('a, 'z) point_ext |
2825 |
2840 |
2826 text {* |
2841 text {* |
2827 The proof obligations handed over to the user by @{command lift_bnf} in |
2842 In the general case, the proof obligations generated by @{command lift_bnf} are |
2828 the general case are simpler than the acual BNF properties (in particular, |
2843 simpler than the acual BNF properties. In particular, no cardinality reasoning |
2829 no cardinality reasoning is required). They are best illustrated on an |
2844 is required. Consider the following type of nonempty lists: |
2830 example. Suppose we define the type of nonempty lists. |
|
2831 *} |
2845 *} |
2832 |
2846 |
2833 typedef 'a nonempty_list = "{xs :: 'a list. xs \<noteq> []}" by auto |
2847 typedef 'a nonempty_list = "{xs :: 'a list. xs \<noteq> []}" by auto |
2834 |
2848 |
2835 text {* |
2849 text {* |
2836 Then, @{command lift_bnf} requires us to prove that the set of nonempty lists |
2850 The @{command lift_bnf} command requires us to prove that the set of nonempty lists |
2837 is closed under the map function and the zip function (where the latter only |
2851 is closed under the map function and the zip function. The latter only |
2838 occurs implicitly in the goal, in form of the variable |
2852 occurs implicitly in the goal, in form of the variable |
2839 @{term "zs :: ('a \<times> 'b) list"}). |
2853 @{term "zs :: ('a \<times> 'b) list"}. |
2840 *} |
2854 *} |
2841 |
2855 |
2842 lift_bnf (*<*)(no_warn_wits)(*>*) 'a nonempty_list |
2856 lift_bnf (*<*)(no_warn_wits) (*>*)'a nonempty_list |
2843 proof - |
2857 proof - |
2844 fix f and xs :: "'a list" |
2858 fix f and xs :: "'a list" |
2845 assume "xs \<in> {xs. xs \<noteq> []}" |
2859 assume "xs \<in> {xs. xs \<noteq> []}" |
2846 then show "map f xs \<in> {xs. xs \<noteq> []}" by (cases xs(*<*)rule: list.exhaust(*>*)) auto |
2860 then show "map f xs \<in> {xs. xs \<noteq> []}" |
|
2861 by (cases xs(*<*) rule: list.exhaust(*>*)) auto |
2847 next |
2862 next |
2848 fix zs :: "('a \<times> 'b) list" |
2863 fix zs :: "('a \<times> 'b) list" |
2849 assume "map fst zs \<in> {xs. xs \<noteq> []}" "map snd zs \<in> {xs. xs \<noteq> []}" |
2864 assume "map fst zs \<in> {xs. xs \<noteq> []}" "map snd zs \<in> {xs. xs \<noteq> []}" |
2850 then show "zs \<in> {xs. xs \<noteq> []}" by (cases zs(*<*)rule: list.exhaust(*>*)) auto |
2865 then show "zs \<in> {xs. xs \<noteq> []}" |
|
2866 by (cases zs (*<*)rule: list.exhaust(*>*)) auto |
2851 qed |
2867 qed |
2852 |
2868 |
2853 text {* |
2869 text {* |
2854 The next example declares a BNF axiomatically. This can be convenient for |
2870 The next example declares a BNF axiomatically. This can be convenient for |
2855 reasoning abstractly about an arbitrary BNF. The @{command bnf_axiomatization} |
2871 reasoning abstractly about an arbitrary BNF. The @{command bnf_axiomatization} |
2903 (@{text only}) or disabled (@{text del}). By default, all plugins are enabled. |
2919 (@{text only}) or disabled (@{text del}). By default, all plugins are enabled. |
2904 |
2920 |
2905 %%% TODO: elaborate on proof obligations |
2921 %%% TODO: elaborate on proof obligations |
2906 *} |
2922 *} |
2907 |
2923 |
2908 subsubsection {* \keyw{lift_bnf} and \keyw{copy_bnf} |
2924 subsubsection {* \keyw{lift_bnf} |
2909 \label{sssec:lift-bnf} *} |
2925 \label{sssec:lift-bnf} *} |
2910 |
2926 |
2911 text {* |
2927 text {* |
2912 \begin{matharray}{rcl} |
2928 \begin{matharray}{rcl} |
2913 @{command_def "lift_bnf"} & : & @{text "local_theory \<rightarrow> proof(prove)"} \\ |
2929 @{command_def "lift_bnf"} & : & @{text "local_theory \<rightarrow> proof(prove)"} |
2914 @{command_def "copy_bnf"} & : & @{text "local_theory \<rightarrow> local_theory"} |
|
2915 \end{matharray} |
2930 \end{matharray} |
2916 |
2931 |
2917 @{rail \<open> |
2932 @{rail \<open> |
2918 @@{command lift_bnf} target? lb_options? \<newline> |
2933 @@{command lift_bnf} target? lb_options? \<newline> |
2919 @{syntax tyargs} name wit_terms? \<newline> |
2934 @{syntax tyargs} name wit_terms? \<newline> |
2920 ('via' @{syntax thmref})? @{syntax map_rel}? |
2935 ('via' @{syntax thmref})? @{syntax map_rel}? |
2921 ; |
2936 ; |
2922 @{syntax_def lb_options}: '(' ((@{syntax plugins} | 'no_warn_wits') + ',') ')' |
2937 @{syntax_def lb_options}: '(' ((@{syntax plugins} | 'no_warn_wits') + ',') ')' |
2923 ; |
2938 ; |
2924 @{syntax_def wit_terms}: '[' 'wits' ':' terms ']' |
2939 @{syntax_def wit_terms}: '[' 'wits' ':' terms ']' |
2925 ; |
2940 \<close>} |
|
2941 \medskip |
|
2942 |
|
2943 \noindent |
|
2944 The @{command lift_bnf} command registers as a BNF an existing type (the |
|
2945 \emph{abstract type}) that was defined as a subtype of a BNF (the \emph{raw |
|
2946 type}) using the @{command typedef} command. To achieve this, it lifts the BNF |
|
2947 structure on the raw type to the abstract type following a @{term |
|
2948 type_definition} theorem. The theorem is usually inferred from the type, but can |
|
2949 also be explicitly supplied by means of the optional @{text via} clause. In |
|
2950 addition, custom names for the set functions, the map function, and the relator, |
|
2951 as well as nonemptiness witnesses can be specified. |
|
2952 |
|
2953 Nonemptiness witnesses are not lifted from the raw type's BNF, as this would be |
|
2954 incomplete. They must be given as terms (on the raw type) and proved to be |
|
2955 witnesses. The command warns about witness types that are present in the raw |
|
2956 type's BNF but not supplied by the user. The warning can be disabled by |
|
2957 specifying the @{text no_warn_wits} option. |
|
2958 *} |
|
2959 |
|
2960 subsubsection {* \keyw{copy_bnf} |
|
2961 \label{sssec:copy-bnf} *} |
|
2962 |
|
2963 text {* |
|
2964 \begin{matharray}{rcl} |
|
2965 @{command_def "copy_bnf"} & : & @{text "local_theory \<rightarrow> local_theory"} |
|
2966 \end{matharray} |
|
2967 |
|
2968 @{rail \<open> |
2926 @@{command copy_bnf} target? ('(' @{syntax plugins} ')')? \<newline> |
2969 @@{command copy_bnf} target? ('(' @{syntax plugins} ')')? \<newline> |
2927 @{syntax tyargs} name ('via' @{syntax thmref})? @{syntax map_rel}? |
2970 @{syntax tyargs} name ('via' @{syntax thmref})? @{syntax map_rel}? |
2928 \<close>} |
2971 \<close>} |
2929 \medskip |
2972 \medskip |
2930 |
2973 |
2931 \noindent |
2974 \noindent |
2932 The @{command lift_bnf} command registers an existing type (abstract type), |
2975 The @{command copy_bnf} command performs the same lifting as @{command lift_bnf} |
2933 defined as a subtype of a BNF (raw type) using the @{command typedef} command, |
2976 for type copies (@{command typedef}s with @{term UNIV} as the representing set), |
2934 as a BNF. To do so, it lifts the BNF structure on the raw type to the abstract |
2977 without requiring the user to discharge any proof obligations or provide |
2935 type following a @{term type_definition} theorem (the theorem is usually inferred |
2978 nonemptiness witnesses. |
2936 from the type, but can also be explicitly supplied by the user using the |
|
2937 @{text via} slot). In addition, custom names for map, set functions, and the relator, |
|
2938 as well as nonemptiness witnesses can be specified; otherwise, default versions are used. |
|
2939 Nonemptiness witnesses are not lifted from the raw type's BNF (this would be |
|
2940 inherently incomplete), but must be given as terms (on the raw type) and proved |
|
2941 to be witnesses. The command warns aggresively about witness types that a present |
|
2942 in the raw type's BNF but not supplied by the user. The warning can be turned off |
|
2943 by passing the @{text no_warn_wits} option. |
|
2944 |
|
2945 The @{command copy_bnf} command, performes the same lifting for type copies |
|
2946 (@{command typedef}s with @{term UNIV} as the representing set) without bothering |
|
2947 the user with trivial proof obligations. (Here, all nonemptiness witnesses from the raw |
|
2948 type's BNF can also be lifted without problems.) |
|
2949 *} |
2979 *} |
2950 |
2980 |
2951 subsubsection {* \keyw{bnf_axiomatization} |
2981 subsubsection {* \keyw{bnf_axiomatization} |
2952 \label{sssec:bnf-axiomatization} *} |
2982 \label{sssec:bnf-axiomatization} *} |
2953 |
2983 |