src/Doc/Datatypes/Datatypes.thy
changeset 59282 c5f6e2c4472c
parent 59280 2949ace404c3
child 59284 d418ac9727f2
equal deleted inserted replaced
59281:1b4dc8a9f7d9 59282:c5f6e2c4472c
   122 @{command datatype} and @{command codatatype}.
   122 @{command datatype} and @{command codatatype}.
   123 
   123 
   124 %\item Section \ref{sec:using-the-standard-ml-interface}, ``Using the Standard
   124 %\item Section \ref{sec:using-the-standard-ml-interface}, ``Using the Standard
   125 ML Interface,'' %describes the package's programmatic interface.
   125 ML Interface,'' %describes the package's programmatic interface.
   126 
   126 
   127 \item Section \ref{sec:controlling-plugins}, ``Controlling Plugins,''
   127 \item Section \ref{sec:selecting-plugins}, ``Selecting Plugins,'' is concerned
   128 is concerned with the package's interoperability with other Isabelle packages
   128 with the package's interoperability with other Isabelle packages and tools, such
   129 and tools, such as the code generator, Transfer, Lifting, and Quickcheck.
   129 as the code generator, Transfer, Lifting, and Quickcheck.
   130 
   130 
   131 \item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and
   131 \item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and
   132 Limitations,'' concludes with known open issues at the time of writing.
   132 Limitations,'' concludes with known open issues at the time of writing.
   133 \end{itemize}
   133 \end{itemize}
   134 
   134 
   182 
   182 
   183     datatype trool = Truue | Faalse | Perhaaps
   183     datatype trool = Truue | Faalse | Perhaaps
   184 
   184 
   185 text {*
   185 text {*
   186 \noindent
   186 \noindent
   187 Here, @{const Truue}, @{const Faalse}, and @{const Perhaaps} have the type @{typ trool}.
   187 @{const Truue}, @{const Faalse}, and @{const Perhaaps} have the type @{typ trool}.
   188 
   188 
   189 Polymorphic types are possible, such as the following option type, modeled after
   189 Polymorphic types are possible, such as the following option type, modeled after
   190 its homologue from the @{theory Option} theory:
   190 its homologue from the @{theory Option} theory:
   191 *}
   191 *}
   192 
   192 
   196 (*>*)
   196 (*>*)
   197     datatype 'a option = None | Some 'a
   197     datatype 'a option = None | Some 'a
   198 
   198 
   199 text {*
   199 text {*
   200 \noindent
   200 \noindent
   201 The constructors are @{text "None :: 'a option"} and
   201 The constructors are @{text "None \<Colon> 'a option"} and
   202 @{text "Some :: 'a \<Rightarrow> 'a option"}.
   202 @{text "Some \<Colon> 'a \<Rightarrow> 'a option"}.
   203 
   203 
   204 The next example has three type parameters:
   204 The next example has three type parameters:
   205 *}
   205 *}
   206 
   206 
   207     datatype ('a, 'b, 'c) triple = Triple 'a 'b 'c
   207     datatype ('a, 'b, 'c) triple = Triple 'a 'b 'c
   208 
   208 
   209 text {*
   209 text {*
   210 \noindent
   210 \noindent
   211 The constructor is
   211 The constructor is
   212 @{text "Triple :: 'a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> ('a, 'b, 'c) triple"}.
   212 @{text "Triple \<Colon> 'a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> ('a, 'b, 'c) triple"}.
   213 Unlike in Standard ML, curried constructors are supported. The uncurried variant
   213 Unlike in Standard ML, curried constructors are supported. The uncurried variant
   214 is also possible:
   214 is also possible:
   215 *}
   215 *}
   216 
   216 
   217     datatype ('a, 'b, 'c) triple\<^sub>u = Triple\<^sub>u "'a * 'b * 'c"
   217     datatype ('a, 'b, 'c) triple\<^sub>u = Triple\<^sub>u "'a * 'b * 'c"
   415 The discriminator associated with @{const Cons} is simply
   415 The discriminator associated with @{const Cons} is simply
   416 @{term "\<lambda>xs. \<not> null xs"}.
   416 @{term "\<lambda>xs. \<not> null xs"}.
   417 
   417 
   418 The \keyw{where} clause at the end of the command specifies a default value for
   418 The \keyw{where} clause at the end of the command specifies a default value for
   419 selectors applied to constructors on which they are not a priori specified.
   419 selectors applied to constructors on which they are not a priori specified.
   420 Here, it is used to ensure that the tail of the empty list is itself (instead of
   420 In the example, it is used to ensure that the tail of the empty list is itself
   421 being left unspecified).
   421 (instead of being left unspecified).
   422 
   422 
   423 Because @{const Nil} is nullary, it is also possible to use
   423 Because @{const Nil} is nullary, it is also possible to use
   424 @{term "\<lambda>xs. xs = Nil"} as a discriminator. This is the default behavior
   424 @{term "\<lambda>xs. xs = Nil"} as a discriminator. This is the default behavior
   425 if we omit the identifier @{const null} and the associated colon. Some users
   425 if we omit the identifier @{const null} and the associated colon. Some users
   426 argue against this, because the mixture of constructors and selectors in the
   426 argue against this, because the mixture of constructors and selectors in the
   469 \begin{matharray}{rcl}
   469 \begin{matharray}{rcl}
   470   @{command_def "datatype"} & : & @{text "local_theory \<rightarrow> local_theory"}
   470   @{command_def "datatype"} & : & @{text "local_theory \<rightarrow> local_theory"}
   471 \end{matharray}
   471 \end{matharray}
   472 
   472 
   473 @{rail \<open>
   473 @{rail \<open>
   474   @@{command datatype} target? @{syntax dt_options}? \<newline>
   474   @@{command datatype} target? @{syntax dt_options}? @{syntax dt_spec}
   475     (@{syntax dt_name} '=' (@{syntax dt_ctor} + '|') \<newline>
       
   476      @{syntax map_rel}? (@'where' (prop + '|'))? + @'and')
       
   477   ;
   475   ;
   478   @{syntax_def dt_options}: '(' ((@{syntax plugins} | 'discs_sels') + ',') ')'
   476   @{syntax_def dt_options}: '(' ((@{syntax plugins} | 'discs_sels') + ',') ')'
   479   ;
   477   ;
   480   @{syntax_def plugins}: 'plugins' ('only' | 'del') ':' (name +)
   478   @{syntax_def plugins}: 'plugins' ('only' | 'del') ':' (name +)
       
   479   ;
       
   480   @{syntax_def dt_spec}: (@{syntax dt_name} '=' (@{syntax dt_ctor} + '|') \<newline>
       
   481      @{syntax map_rel}? (@'where' (prop + '|'))? + @'and')
   481   ;
   482   ;
   482   @{syntax_def map_rel}: @'for' ((('map' | 'rel') ':' name) +)
   483   @{syntax_def map_rel}: @'for' ((('map' | 'rel') ':' name) +)
   483 \<close>}
   484 \<close>}
   484 
   485 
   485 \medskip
   486 \medskip
   651 
   652 
   652 \medskip
   653 \medskip
   653 
   654 
   654 \noindent
   655 \noindent
   655 In addition, some of the plugins introduce their own constants
   656 In addition, some of the plugins introduce their own constants
   656 (Section~\ref{sec:controlling-plugins}). The case combinator, discriminators,
   657 (Section~\ref{sec:selecting-plugins}). The case combinator, discriminators,
   657 and selectors are collectively called \emph{destructors}. The prefix
   658 and selectors are collectively called \emph{destructors}. The prefix
   658 ``@{text "t."}'' is an optional component of the names and is normally hidden.
   659 ``@{text "t."}'' is an optional component of the names and is normally hidden.
   659 *}
   660 *}
   660 
   661 
   661 
   662 
   684 
   685 
   685 \noindent
   686 \noindent
   686 The full list of named theorems can be obtained as usual by entering the
   687 The full list of named theorems can be obtained as usual by entering the
   687 command \keyw{print_theorems} immediately after the datatype definition.
   688 command \keyw{print_theorems} immediately after the datatype definition.
   688 This list includes theorems produced by plugins
   689 This list includes theorems produced by plugins
   689 (Section~\ref{sec:controlling-plugins}), but normally excludes low-level
   690 (Section~\ref{sec:selecting-plugins}), but normally excludes low-level
   690 theorems that reveal internal constructions. To make these accessible, add
   691 theorems that reveal internal constructions. To make these accessible, add
   691 the line
   692 the line
   692 *}
   693 *}
   693 
   694 
   694     declare [[bnf_note_all]]
   695     declare [[bnf_note_all]]
  1102 The new function returns @{text 1} instead of @{text 0} for some nonrecursive
  1103 The new function returns @{text 1} instead of @{text 0} for some nonrecursive
  1103 constructors. This departure from the old behavior made it possible to implement
  1104 constructors. This departure from the old behavior made it possible to implement
  1104 @{const size} in terms of the generic function @{text "t.size_t"}.
  1105 @{const size} in terms of the generic function @{text "t.size_t"}.
  1105 Moreover, the new function considers nested occurrences of a value, in the nested
  1106 Moreover, the new function considers nested occurrences of a value, in the nested
  1106 recursive case. The old behavior can be obtained by disabling the @{text size}
  1107 recursive case. The old behavior can be obtained by disabling the @{text size}
  1107 plugin (Section~\ref{sec:controlling-plugins}) and instantiating the
  1108 plugin (Section~\ref{sec:selecting-plugins}) and instantiating the
  1108 @{text size} type class manually.
  1109 @{text size} type class manually.
  1109 
  1110 
  1110 \item \emph{The internal constructions are completely different.} Proof texts
  1111 \item \emph{The internal constructions are completely different.} Proof texts
  1111 that unfold the definition of constants introduced by \keyw{old_datatype} will
  1112 that unfold the definition of constants introduced by \keyw{old_datatype} will
  1112 be difficult to port.
  1113 be difficult to port.
  1147   \label{sec:defining-recursive-functions} *}
  1148   \label{sec:defining-recursive-functions} *}
  1148 
  1149 
  1149 text {*
  1150 text {*
  1150 Recursive functions over datatypes can be specified using the @{command primrec}
  1151 Recursive functions over datatypes can be specified using the @{command primrec}
  1151 command, which supports primitive recursion, or using the more general
  1152 command, which supports primitive recursion, or using the more general
  1152 \keyw{fun} and \keyw{function} commands. Here, the focus is on
  1153 \keyw{fun} and \keyw{function} commands. In this tutorial, the focus is on
  1153 @{command primrec}; the other two commands are described in a separate
  1154 @{command primrec}; the other two commands are described in a separate
  1154 tutorial @{cite "isabelle-function"}.
  1155 tutorial @{cite "isabelle-function"}.
  1155 
  1156 
  1156 %%% TODO: partial_function
  1157 %%% TODO: partial_function
  1157 *}
  1158 *}
  1487 
  1488 
  1488 @{rail \<open>
  1489 @{rail \<open>
  1489   @@{command primrec} target? @{syntax pr_options}? fixes \<newline>
  1490   @@{command primrec} target? @{syntax pr_options}? fixes \<newline>
  1490   @'where' (@{syntax pr_equation} + '|')
  1491   @'where' (@{syntax pr_equation} + '|')
  1491   ;
  1492   ;
  1492   @{syntax_def pr_options}: '(' (('nonexhaustive' | 'transfer') + ',') ')'
  1493   @{syntax_def pr_options}: '(' ((@{syntax plugins} | 'nonexhaustive' | 'transfer') + ',') ')'
  1493   ;
  1494   ;
  1494   @{syntax_def pr_equation}: thmdecl? prop
  1495   @{syntax_def pr_equation}: thmdecl? prop
  1495 \<close>}
  1496 \<close>}
  1496 
  1497 
  1497 \medskip
  1498 \medskip
  1510 
  1511 
  1511 \begin{itemize}
  1512 \begin{itemize}
  1512 \setlength{\itemsep}{0pt}
  1513 \setlength{\itemsep}{0pt}
  1513 
  1514 
  1514 \item
  1515 \item
  1515 The @{text "nonexhaustive"} option indicates that the functions are not
  1516 The @{text plugins} option indicates which plugins should be enabled
       
  1517 (@{text only}) or disabled (@{text del}). By default, all plugins are enabled.
       
  1518 
       
  1519 \item
       
  1520 The @{text nonexhaustive} option indicates that the functions are not
  1516 necessarily specified for all constructors. It can be used to suppress the
  1521 necessarily specified for all constructors. It can be used to suppress the
  1517 warning that is normally emitted when some constructors are missing.
  1522 warning that is normally emitted when some constructors are missing.
  1518 
  1523 
  1519 \item
  1524 \item
  1520 The @{text "transfer"} option indicates that an unconditional transfer rule
  1525 The @{text transfer} option indicates that an unconditional transfer rule
  1521 should be generated and proved @{text "by transfer_prover"}. The
  1526 should be generated and proved @{text "by transfer_prover"}. The
  1522 @{text "[transfer_rule]"} attribute is set on the generated theorem.
  1527 @{text "[transfer_rule]"} attribute is set on the generated theorem.
  1523 \end{itemize}
  1528 \end{itemize}
  1524 
  1529 
  1525 %%% TODO: elaborate on format of the equations
  1530 %%% TODO: elaborate on format of the equations
  1752 \begin{matharray}{rcl}
  1757 \begin{matharray}{rcl}
  1753   @{command_def "codatatype"} & : & @{text "local_theory \<rightarrow> local_theory"}
  1758   @{command_def "codatatype"} & : & @{text "local_theory \<rightarrow> local_theory"}
  1754 \end{matharray}
  1759 \end{matharray}
  1755 
  1760 
  1756 @{rail \<open>
  1761 @{rail \<open>
  1757   @@{command codatatype} target? \<newline>
  1762   @@{command codatatype} target? @{syntax dt_options}? @{syntax dt_spec}
  1758     (@{syntax dt_name} '=' (@{syntax dt_ctor} + '|') + @'and')
       
  1759 \<close>}
  1763 \<close>}
  1760 
  1764 
  1761 \medskip
  1765 \medskip
  1762 
  1766 
  1763 \noindent
  1767 \noindent
  1910   \label{sec:defining-corecursive-functions} *}
  1914   \label{sec:defining-corecursive-functions} *}
  1911 
  1915 
  1912 text {*
  1916 text {*
  1913 Corecursive functions can be specified using the @{command primcorec} and
  1917 Corecursive functions can be specified using the @{command primcorec} and
  1914 \keyw{prim\-corec\-ursive} commands, which support primitive corecursion, or
  1918 \keyw{prim\-corec\-ursive} commands, which support primitive corecursion, or
  1915 using the more general \keyw{partial_function} command. Here, the focus is on
  1919 using the more general \keyw{partial_function} command. In this tutorial, the
  1916 the first two. More examples can be found in the directory
  1920 focus is on the first two. More examples can be found in the directory
  1917 \verb|~~/src/HOL/Datatype_Examples|.
  1921 \verb|~~/src/HOL/Datatype_|\allowbreak\verb|Examples|.
  1918 
  1922 
  1919 Whereas recursive functions consume datatypes one constructor at a time,
  1923 Whereas recursive functions consume datatypes one constructor at a time,
  1920 corecursive functions construct codatatypes one constructor at a time.
  1924 corecursive functions construct codatatypes one constructor at a time.
  1921 Partly reflecting a lack of agreement among proponents of coalgebraic methods,
  1925 Partly reflecting a lack of agreement among proponents of coalgebraic methods,
  1922 Isabelle supports three competing syntaxes for specifying a function $f$:
  1926 Isabelle supports three competing syntaxes for specifying a function $f$:
  2433 
  2437 
  2434 @{rail \<open>
  2438 @{rail \<open>
  2435   (@@{command primcorec} | @@{command primcorecursive}) target? \<newline>
  2439   (@@{command primcorec} | @@{command primcorecursive}) target? \<newline>
  2436     @{syntax pcr_options}? fixes @'where' (@{syntax pcr_formula} + '|')
  2440     @{syntax pcr_options}? fixes @'where' (@{syntax pcr_formula} + '|')
  2437   ;
  2441   ;
  2438   @{syntax_def pcr_options}: '(' (('sequential' | 'exhaustive' | 'transfer') + ',') ')'
  2442   @{syntax_def pcr_options}: '(' ((@{syntax plugins} | 'sequential' | 'exhaustive' | 'transfer') + ',') ')'
  2439   ;
  2443   ;
  2440   @{syntax_def pcr_formula}: thmdecl? prop (@'of' (term * ))?
  2444   @{syntax_def pcr_formula}: thmdecl? prop (@'of' (term * ))?
  2441 \<close>}
  2445 \<close>}
  2442 
  2446 
  2443 \medskip
  2447 \medskip
  2456 
  2460 
  2457 \begin{itemize}
  2461 \begin{itemize}
  2458 \setlength{\itemsep}{0pt}
  2462 \setlength{\itemsep}{0pt}
  2459 
  2463 
  2460 \item
  2464 \item
  2461 The @{text "sequential"} option indicates that the conditions in specifications
  2465 The @{text plugins} option indicates which plugins should be enabled
       
  2466 (@{text only}) or disabled (@{text del}). By default, all plugins are enabled.
       
  2467 
       
  2468 \item
       
  2469 The @{text sequential} option indicates that the conditions in specifications
  2462 expressed using the constructor or destructor view are to be interpreted
  2470 expressed using the constructor or destructor view are to be interpreted
  2463 sequentially.
  2471 sequentially.
  2464 
  2472 
  2465 \item
  2473 \item
  2466 The @{text "exhaustive"} option indicates that the conditions in specifications
  2474 The @{text exhaustive} option indicates that the conditions in specifications
  2467 expressed using the constructor or destructor view cover all possible cases.
  2475 expressed using the constructor or destructor view cover all possible cases.
  2468 
  2476 
  2469 \item
  2477 \item
  2470 The @{text "transfer"} option indicates that an unconditional transfer rule
  2478 The @{text transfer} option indicates that an unconditional transfer rule
  2471 should be generated and proved @{text "by transfer_prover"}. The
  2479 should be generated and proved @{text "by transfer_prover"}. The
  2472 @{text "[transfer_rule]"} attribute is set on the generated theorem.
  2480 @{text "[transfer_rule]"} attribute is set on the generated theorem.
  2473 \end{itemize}
  2481 \end{itemize}
  2474 
  2482 
  2475 The @{command primcorec} command is an abbreviation for @{command
  2483 The @{command primcorec} command is an abbreviation for @{command
  2830 The package's programmatic interface.
  2838 The package's programmatic interface.
  2831 *}
  2839 *}
  2832 *)
  2840 *)
  2833 
  2841 
  2834 
  2842 
  2835 section {* Controlling Plugins
  2843 section {* Selecting Plugins
  2836   \label{sec:controlling-plugins} *}
  2844   \label{sec:selecting-plugins} *}
  2837 
  2845 
  2838 text {*
  2846 text {*
  2839 Plugins extend the (co)datatype package to interoperate with other Isabelle
  2847 Plugins extend the (co)datatype package to interoperate with other Isabelle
  2840 packages and tools, such as the code generator, Transfer, Lifting, and
  2848 packages and tools, such as the code generator, Transfer, Lifting, and
  2841 Quickcheck. They can be enabled or disabled individually using the
  2849 Quickcheck. They can be enabled or disabled individually using the
  2962 \item[@{text "t."}\hthm{bi_unique_rel} @{text "[transfer_rule]"}\rm:] ~ \\
  2970 \item[@{text "t."}\hthm{bi_unique_rel} @{text "[transfer_rule]"}\rm:] ~ \\
  2963 @{thm list.bi_unique_rel[no_vars]}
  2971 @{thm list.bi_unique_rel[no_vars]}
  2964 
  2972 
  2965 \end{description}
  2973 \end{description}
  2966 \end{indentblock}
  2974 \end{indentblock}
       
  2975 
       
  2976 For @{command primrec}, @{command primcorec}, and @{command primcorecursive},
       
  2977 the \hthm{transfer} plugin implements the generation of the @{text "f.transfer"}
       
  2978 property, conditioned by the @{text transfer} option.
  2967 *}
  2979 *}
  2968 
  2980 
  2969 
  2981 
  2970 subsection {* Lifting
  2982 subsection {* Lifting
  2971   \label{ssec:lifting} *}
  2983   \label{ssec:lifting} *}
  2996 subsection {* Quickcheck
  3008 subsection {* Quickcheck
  2997   \label{ssec:quickcheck} *}
  3009   \label{ssec:quickcheck} *}
  2998 
  3010 
  2999 text {*
  3011 text {*
  3000 The integration of datatypes with Quickcheck is accomplished by the
  3012 The integration of datatypes with Quickcheck is accomplished by the
  3001 \hthm{quickcheck} plugin. It combines a number of subplugins that instantiate
  3013 \hthm{quick\-check} plugin. It combines a number of subplugins that instantiate
  3002 specific type classes. The subplugins can be enabled or disabled individually.
  3014 specific type classes. The subplugins can be enabled or disabled individually.
  3003 They are listed below:
  3015 They are listed below:
  3004 
  3016 
  3005 \begin{indentblock}
  3017 \begin{indentblock}
  3006 \hthm{quickcheck_random} \\
  3018 \hthm{quickcheck_random} \\